From 7437af6f36b8201fba7a9dea98685da4d35f167f Mon Sep 17 00:00:00 2001 From: Ross Paterson Date: Wed, 26 Oct 2011 18:23:57 +0100 Subject: fix#5380: arrows if command given too general a type There were two bugs with the implementation of rebindable syntax, so I adapted the code for if-expressions. Also noted that rebinding of if is a bit more restricted in the arrows case. --- compiler/typecheck/TcArrows.lhs | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/compiler/typecheck/TcArrows.lhs b/compiler/typecheck/TcArrows.lhs index 774cea5c51..0dfe8b0f1e 100644 --- a/compiler/typecheck/TcArrows.lhs +++ b/compiler/typecheck/TcArrows.lhs @@ -119,17 +119,28 @@ tc_cmd env in_cmd@(HsCase scrut matches) (stk, res_ty) mc_body = mc_body } mc_body body res_ty' = tcCmd env body (stk, res_ty') -tc_cmd env (HsIf mb_fun pred b1 b2) (stack_ty,res_ty) +tc_cmd env (HsIf Nothing pred b1 b2) res_ty -- Ordinary 'if' + = do { pred' <- tcMonoExpr pred boolTy + ; b1' <- tcCmd env b1 res_ty + ; b2' <- tcCmd env b2 res_ty + ; return (HsIf Nothing pred' b1' b2') + } + +tc_cmd env (HsIf (Just fun) pred b1 b2) res_ty -- Rebindable syntax for if = do { pred_ty <- newFlexiTyVarTy openTypeKind - ; b_ty <- newFlexiTyVarTy openTypeKind - ; let if_ty = mkFunTys [pred_ty, b_ty, b_ty] res_ty - ; mb_fun' <- case mb_fun of - Nothing -> return Nothing - Just fun -> liftM Just (tcSyntaxOp IfOrigin fun if_ty) + -- For arrows, need ifThenElse :: forall r. T -> r -> r -> r + -- because we're going to apply it to the environment, not + -- the return value. + ; [r_tv] <- tcInstSkolTyVars [alphaTyVar] + ; let r_ty = mkTyVarTy r_tv + ; let if_ty = mkFunTys [pred_ty, r_ty, r_ty] r_ty + ; checkTc (not (r_tv `elemVarSet` tyVarsOfType pred_ty)) + (ptext (sLit "Predicate type of `ifThenElse' depends on result type")) + ; fun' <- tcSyntaxOp IfOrigin fun if_ty ; pred' <- tcMonoExpr pred pred_ty - ; b1' <- tcCmd env b1 (stack_ty,b_ty) - ; b2' <- tcCmd env b2 (stack_ty,b_ty) - ; return (HsIf mb_fun' pred' b1' b2') + ; b1' <- tcCmd env b1 res_ty + ; b2' <- tcCmd env b2 res_ty + ; return (HsIf (Just fun') pred' b1' b2') } ------------------------------------------- -- cgit v1.2.1