2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section{Typecheck arrow notation}
7 module TcArrows ( tcProc ) where
9 #include "HsVersions.h"
11 import {-# SOURCE #-} TcExpr( tcCheckRho )
14 import TcHsSyn ( mkHsLet )
16 import TcMatches ( TcStmtCtxt(..), tcMatchPats, matchCtxt, tcStmts,
17 TcMatchCtxt(..), tcMatchesCase )
19 import TcType ( TcType, TcTauType, TcRhoType, mkFunTys, mkTyConApp,
20 mkTyVarTy, mkAppTys, tcSplitTyConApp_maybe, tcEqType )
21 import TcMType ( newTyVarTy, newTyVarTys, newSigTyVar, zonkTcType )
22 import TcBinds ( tcBindsAndThen )
23 import TcSimplify ( tcSimplifyCheck )
24 import TcUnify ( Expected(..), checkSigTyVarsWrt, zapExpectedTo )
26 import Inst ( tcSyntaxName )
28 import TysWiredIn ( boolTy, pairTyCon )
30 import Type ( Kind, mkArrowKinds, liftedTypeKind, openTypeKind, tyVarsOfTypes )
32 import SrcLoc ( Located(..) )
34 import Util ( lengthAtLeast )
38 %************************************************************************
42 %************************************************************************
45 tcProc :: InPat Name -> LHsCmdTop Name -- proc pat -> expr
46 -> Expected TcRhoType -- Expected type of whole proc expression
47 -> TcM (OutPat TcId, LHsCmdTop TcId)
50 = do { arr_ty <- newTyVarTy arrowTyConKind
51 ; [arg_ty, res_ty] <- newTyVarTys 2 liftedTypeKind
52 ; zapExpectedTo exp_ty (mkAppTys arr_ty [arg_ty,res_ty])
54 ; let cmd_env = CmdEnv { cmd_arr = arr_ty }
55 ; ([pat'], cmd', ex_binds) <- incProcLevel $
56 tcMatchPats [(pat, Check arg_ty)] (Check res_ty) $
57 tcCmdTop cmd_env cmd ([], res_ty)
59 ; return (pat', glueBindsOnCmd ex_binds cmd') }
63 %************************************************************************
67 %************************************************************************
70 type CmdStack = [TcTauType]
71 data CmdEnv = CmdEnv { cmd_arr :: TcType } -- The arrow type constructor, of kind *->*->*
73 mkCmdArrTy :: CmdEnv -> TcTauType -> TcTauType -> TcTauType
74 mkCmdArrTy env t1 t2 = mkAppTys (cmd_arr env) [t1, t2]
76 ---------------------------------------
79 -> (CmdStack, TcTauType) -- Expected result type; always a monotype
80 -- We know exactly how many cmd args are expected,
81 -- albeit perhaps not their types; so we can pass
83 -> TcM (LHsCmdTop TcId)
85 tcCmdTop env (L loc (HsCmdTop cmd _ _ names)) (cmd_stk, res_ty)
87 do { cmd' <- tcCmd env cmd (cmd_stk, res_ty)
88 ; names' <- mapM (tcSyntaxName ProcOrigin (cmd_arr env)) names
89 ; return (L loc $ HsCmdTop cmd' cmd_stk res_ty names') }
92 ----------------------------------------
93 tcCmd :: CmdEnv -> LHsExpr Name -> (CmdStack, TcTauType) -> TcM (LHsExpr TcId)
94 -- The main recursive function
95 tcCmd env (L loc expr) res_ty
97 { expr' <- tc_cmd env expr res_ty
98 ; return (L loc expr') }
100 tc_cmd env (HsPar cmd) res_ty
101 = do { cmd' <- tcCmd env cmd res_ty
102 ; return (HsPar cmd') }
104 tc_cmd env (HsLet binds (L body_loc body)) res_ty
105 = tcBindsAndThen glue binds $
106 addSrcSpan body_loc $
107 tc_cmd env body res_ty
109 glue binds expr = HsLet [binds] (L body_loc expr)
111 tc_cmd env in_cmd@(HsCase scrut matches) (stk, res_ty)
112 = addErrCtxt (cmdCtxt in_cmd) $
113 tcMatchesCase match_ctxt matches (Check res_ty)
114 `thenM` \ (scrut_ty, matches') ->
115 addErrCtxt (caseScrutCtxt scrut) (
116 tcCheckRho scrut scrut_ty
117 ) `thenM` \ scrut' ->
118 returnM (HsCase scrut' matches')
120 match_ctxt = MC { mc_what = CaseAlt,
122 mc_body body (Check res_ty') = tcCmd env body (stk, res_ty')
124 tc_cmd env (HsIf pred b1 b2) res_ty
125 = do { pred' <- tcCheckRho pred boolTy
126 ; b1' <- tcCmd env b1 res_ty
127 ; b2' <- tcCmd env b2 res_ty
128 ; return (HsIf pred' b1' b2')
131 -------------------------------------------
133 -- (f -< a) or (f -<< a)
135 tc_cmd env cmd@(HsArrApp fun arg _ ho_app lr) (cmd_stk, res_ty)
136 = addErrCtxt (cmdCtxt cmd) $
137 do { arg_ty <- newTyVarTy openTypeKind
138 ; let fun_ty = mkCmdArrTy env (foldl mkPairTy arg_ty cmd_stk) res_ty
140 ; fun' <- pop_arrow_binders (tcCheckRho fun fun_ty)
142 ; arg' <- tcCheckRho arg arg_ty
144 ; return (HsArrApp fun' arg' fun_ty ho_app lr) }
146 -- Before type-checking f, remove the "arrow binders" from the
147 -- environment in the (-<) case.
148 -- Local bindings, inside the enclosing proc, are not in scope
149 -- inside f. In the higher-order case (-<<), they are.
150 pop_arrow_binders tc = case ho_app of
151 HsHigherOrderApp -> tc
152 HsFirstOrderApp -> popArrowBinders tc
154 -------------------------------------------
155 -- Command application
157 tc_cmd env cmd@(HsApp fun arg) (cmd_stk, res_ty)
158 = addErrCtxt (cmdCtxt cmd) $
159 do { arg_ty <- newTyVarTy openTypeKind
161 ; fun' <- tcCmd env fun (arg_ty:cmd_stk, res_ty)
163 ; arg' <- tcCheckRho arg arg_ty
165 ; return (HsApp fun' arg') }
167 -------------------------------------------
170 tc_cmd env cmd@(HsLam (L mtch_loc match@(Match pats maybe_rhs_sig grhss))) (cmd_stk, res_ty)
171 = addErrCtxt (matchCtxt match_ctxt match) $
173 do { -- Check the cmd stack is big enough
174 ; checkTc (lengthAtLeast cmd_stk n_pats)
176 ; let pats_w_tys = zip pats (map Check cmd_stk)
178 -- Check the patterns, and the GRHSs inside
179 ; (pats', grhss', ex_binds) <- addSrcSpan mtch_loc $
180 tcMatchPats pats_w_tys (Check res_ty) $
183 ; return (HsLam (L mtch_loc (Match pats' Nothing (glueBindsOnGRHSs ex_binds grhss'))))
188 stk' = drop n_pats cmd_stk
189 match_ctxt = LambdaExpr -- Maybe KappaExpr?
191 tc_grhss (GRHSs grhss binds _)
192 = tcBindsAndThen glueBindsOnGRHSs binds $
193 do { grhss' <- mappM (wrapLocM tc_grhs) grhss
194 ; return (GRHSs grhss' [] res_ty) }
196 stmt_ctxt = SC { sc_what = PatGuard match_ctxt,
198 sc_body = \ body -> tcCmd env body (stk', res_ty),
199 sc_ty = res_ty } -- ToDo: Is this right?
200 tc_grhs (GRHS guarded)
201 = do { guarded' <- tcStmts stmt_ctxt guarded
202 ; return (GRHS guarded') }
204 -------------------------------------------
207 tc_cmd env cmd@(HsDo do_or_lc stmts _ ty) (cmd_stk, res_ty)
208 = do { checkTc (null cmd_stk) (nonEmptyCmdStkErr cmd)
209 ; stmts' <- tcStmts stmt_ctxt stmts
210 ; return (HsDo do_or_lc stmts' [] res_ty) }
211 -- The 'methods' needed for the HsDo are in the enclosing HsCmd
212 -- hence the empty list here
214 stmt_ctxt = SC { sc_what = do_or_lc,
219 tc_rhs rhs ty = tcCmd env rhs ([], ty)
220 tc_ret body = tcCmd env body ([], res_ty)
223 -----------------------------------------------------------------
224 -- Arrow ``forms'' (| e |) c1 .. cn
226 -- G |-b c : [s1 .. sm] s
227 -- pop(G) |- e : forall w. b ((w,s1) .. sm) s
228 -- -> a ((w,t1) .. tn) t
229 -- e \not\in (s, s1..sm, t, t1..tn)
230 -- ----------------------------------------------
231 -- G |-a (| e |) c : [t1 .. tn] t
233 tc_cmd env cmd@(HsArrForm expr fixity cmd_args) (cmd_stk, res_ty)
234 = addErrCtxt (cmdCtxt cmd) $
235 do { cmds_w_tys <- zipWithM new_cmd_ty cmd_args [1..]
236 ; w_tv <- newSigTyVar liftedTypeKind
237 ; let w_ty = mkTyVarTy w_tv
239 -- a ((w,t1) .. tn) t
240 ; let e_res_ty = mkCmdArrTy env (foldl mkPairTy w_ty cmd_stk) res_ty
242 -- b ((w,s1) .. sm) s
243 -- -> a ((w,t1) .. tn) t
244 ; let e_ty = mkFunTys [mkAppTys b [tup,s] | (_,_,b,tup,s) <- cmds_w_tys]
248 ; (expr', lie) <- getLIE (tcCheckRho expr e_ty)
249 ; inst_binds <- tcSimplifyCheck sig_msg [w_tv] [] lie
251 -- Check that the polymorphic variable hasn't been unified with anything
252 -- and is not free in res_ty or the cmd_stk (i.e. t, t1..tn)
253 ; [w_tv'] <- checkSigTyVarsWrt (tyVarsOfTypes (res_ty:cmd_stk))
256 -- OK, now we are in a position to unscramble
257 -- the s1..sm and check each cmd
258 ; cmds' <- mapM (tc_cmd w_tv') cmds_w_tys
260 ; returnM (HsArrForm (mkHsTyLam [w_tv'] (mkHsLet inst_binds expr')) fixity cmds')
264 -- b, ((e,s1) .. sm), s
265 new_cmd_ty :: LHsCmdTop Name -> Int
266 -> TcM (LHsCmdTop Name, Int, TcType, TcType, TcType)
268 = do { b_ty <- newTyVarTy arrowTyConKind
269 ; tup_ty <- newTyVarTy liftedTypeKind
270 -- We actually make a type variable for the tuple
271 -- because we don't know how deeply nested it is yet
272 ; s_ty <- newTyVarTy liftedTypeKind
273 ; return (cmd, i, b_ty, tup_ty, s_ty)
276 tc_cmd w_tv (cmd, i, b, tup_ty, s)
277 = do { tup_ty' <- zonkTcType tup_ty
278 ; let (corner_ty, arg_tys) = unscramble tup_ty'
280 -- Check that it has the right shape:
282 -- where the si do not mention w
283 ; checkTc (corner_ty `tcEqType` mkTyVarTy w_tv &&
284 not (w_tv `elemVarSet` tyVarsOfTypes arg_tys))
285 (badFormFun i tup_ty')
287 ; tcCmdTop (CmdEnv { cmd_arr = b }) cmd (arg_tys, s) }
289 unscramble :: TcType -> (TcType, [TcType])
290 -- unscramble ((w,s1) .. sn) = (w, [s1..sn])
292 = case tcSplitTyConApp_maybe ty of
293 Just (tc, [t,s]) | tc == pairTyCon
295 (w,ss) = unscramble t
300 sig_msg = ptext SLIT("expected type of a command form")
302 -----------------------------------------------------------------
303 -- Base case for illegal commands
304 -- This is where expressions that aren't commands get rejected
307 = failWithTc (vcat [ptext SLIT("The expression"), nest 2 (ppr cmd),
308 ptext SLIT("was found where an arrow command was expected")])
312 %************************************************************************
316 %************************************************************************
320 glueBindsOnCmd binds (L loc (HsCmdTop cmd stk res_ty names))
321 = L loc (HsCmdTop (L loc (HsLet [binds] cmd)) stk res_ty names)
322 -- Existential bindings become local bindings in the command
325 mkPairTy t1 t2 = mkTyConApp pairTyCon [t1,t2]
327 arrowTyConKind :: Kind -- *->*->*
328 arrowTyConKind = mkArrowKinds [liftedTypeKind, liftedTypeKind] liftedTypeKind
332 %************************************************************************
336 %************************************************************************
339 cmdCtxt cmd = ptext SLIT("In the command:") <+> ppr cmd
342 = hang (ptext SLIT("In the scrutinee of a case command:")) 4 (ppr cmd)
344 nonEmptyCmdStkErr cmd
345 = hang (ptext SLIT("Non-empty command stack at command:"))
349 = hang (ptext SLIT("Command stack underflow at command:"))
353 = hang (ptext SLIT("The type of the") <+> speakNth i <+> ptext SLIT("argument of a command form has the wrong shape"))
354 4 (ptext SLIT("Argument type:") <+> ppr tup_ty')