Commit 7437af6f authored by Ross Paterson's avatar Ross Paterson
Browse files

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.
parent d6905920
......@@ -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')
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment