2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[TcGRHSs]{Typecheck guarded right-hand-sides}
7 module TcGRHSs ( tcGRHSsAndBinds, tcStmts ) where
9 #include "HsVersions.h"
11 import {-# SOURCE #-} TcExpr( tcExpr )
13 import HsSyn ( HsBinds(..), GRHSsAndBinds(..), GRHS(..), StmtCtxt(..),
16 import RnHsSyn ( RenamedGRHSsAndBinds, RenamedGRHS, RenamedStmt )
17 import TcHsSyn ( TcGRHSsAndBinds, TcGRHS, TcStmt )
19 import TcEnv ( tcExtendGlobalTyVars, tcExtendEnvWithPat )
21 import Inst ( LIE, plusLIE )
22 import TcBinds ( tcBindsAndThen )
23 import TcSimplify ( tcSimplifyAndCheck )
24 import TcPat ( tcPat )
25 import TcMonoType ( checkSigTyVars, noSigs, existentialPatCtxt )
26 import TcType ( TcType, newTyVarTy )
27 import TysWiredIn ( boolTy )
28 import Type ( tyVarsOfType, openTypeKind, boxedTypeKind )
29 import BasicTypes ( RecFlag(..) )
36 %************************************************************************
40 %************************************************************************
43 tcGRHSs :: [RenamedGRHS] -> TcType s -> StmtCtxt -> TcM s ([TcGRHS s], LIE s)
45 tcGRHSs [grhs] expected_ty ctxt
46 = tcGRHS grhs expected_ty ctxt `thenTc` \ (grhs', lie) ->
47 returnTc ([grhs'], lie)
49 tcGRHSs (grhs:grhss) expected_ty ctxt
50 = tcGRHS grhs expected_ty ctxt `thenTc` \ (grhs', lie1) ->
51 tcGRHSs grhss expected_ty ctxt `thenTc` \ (grhss', lie2) ->
52 returnTc (grhs' : grhss', lie1 `plusLIE` lie2)
54 tcGRHS (GRHS guarded locn) expected_ty ctxt
56 tcStmts ctxt (\ty -> ty) guarded expected_ty `thenTc` \ (guarded', lie) ->
57 returnTc (GRHS guarded' locn, lie)
61 %************************************************************************
63 \subsection{GRHSsAndBinds}
65 %************************************************************************
67 @tcGRHSsAndBinds@ typechecks (grhss where binds), returning suitable
71 tcGRHSsAndBinds :: RenamedGRHSsAndBinds
72 -> TcType s -- Expected type of RHSs
74 -> TcM s (TcGRHSsAndBinds s, LIE s)
76 tcGRHSsAndBinds (GRHSsAndBindsIn grhss binds) expected_ty ctxt
79 (tcGRHSs grhss expected_ty ctxt `thenTc` \ (grhss, lie) ->
80 returnTc (GRHSsAndBindsOut grhss EmptyBinds expected_ty, lie))
82 combiner is_rec mbinds (GRHSsAndBindsOut grhss binds expected_ty)
83 = GRHSsAndBindsOut grhss (MonoBind mbinds [] is_rec `ThenBinds` binds) expected_ty
87 %************************************************************************
89 \subsection{Record bindings}
91 %************************************************************************
96 -> (TcType s -> TcType s) -- m, the relationship type of pat and rhs in pat <- rhs
98 -> TcType s -- elt_ty, where type of the comprehension is (m elt_ty)
99 -> TcM s ([TcStmt s], LIE s)
101 tcStmts do_or_lc m (stmt@(ReturnStmt exp) : stmts) elt_ty
102 = ASSERT( null stmts )
103 tcSetErrCtxt (stmtCtxt do_or_lc stmt) $
104 tcExpr exp elt_ty `thenTc` \ (exp', exp_lie) ->
105 returnTc ([ReturnStmt exp'], exp_lie)
107 -- ExprStmt at the end
108 tcStmts do_or_lc m [stmt@(ExprStmt exp src_loc)] elt_ty
109 = tcSetErrCtxt (stmtCtxt do_or_lc stmt) $
110 tcExpr exp (m elt_ty) `thenTc` \ (exp', exp_lie) ->
111 returnTc ([ExprStmt exp' src_loc], exp_lie)
113 -- ExprStmt not at the end
114 tcStmts do_or_lc m (stmt@(ExprStmt exp src_loc) : stmts) elt_ty
115 = ASSERT( isDoStmt do_or_lc )
116 tcAddSrcLoc src_loc (
117 tcSetErrCtxt (stmtCtxt do_or_lc stmt) $
118 -- exp has type (m tau) for some tau (doesn't matter what)
119 newTyVarTy openTypeKind `thenNF_Tc` \ any_ty ->
120 tcExpr exp (m any_ty)
121 ) `thenTc` \ (exp', exp_lie) ->
122 tcStmts do_or_lc m stmts elt_ty `thenTc` \ (stmts', stmts_lie) ->
123 returnTc (ExprStmt exp' src_loc : stmts',
124 exp_lie `plusLIE` stmts_lie)
126 tcStmts do_or_lc m (stmt@(GuardStmt exp src_loc) : stmts) elt_ty
127 = ASSERT( not (isDoStmt do_or_lc) )
128 tcSetErrCtxt (stmtCtxt do_or_lc stmt) (
129 tcAddSrcLoc src_loc $
131 ) `thenTc` \ (exp', exp_lie) ->
132 tcStmts do_or_lc m stmts elt_ty `thenTc` \ (stmts', stmts_lie) ->
133 returnTc (GuardStmt exp' src_loc : stmts',
134 exp_lie `plusLIE` stmts_lie)
136 tcStmts do_or_lc m (stmt@(BindStmt pat exp src_loc) : stmts) elt_ty
137 = tcAddSrcLoc src_loc (
138 tcSetErrCtxt (stmtCtxt do_or_lc stmt) $
139 newTyVarTy boxedTypeKind `thenNF_Tc` \ pat_ty ->
140 tcPat noSigs pat pat_ty `thenTc` \ (pat', pat_lie, pat_tvs, pat_ids, avail) ->
141 tcExpr exp (m pat_ty) `thenTc` \ (exp', exp_lie) ->
142 returnTc (pat', exp',
143 pat_lie `plusLIE` exp_lie,
144 pat_tvs, pat_ids, avail)
145 ) `thenTc` \ (pat', exp', lie_req, pat_tvs, pat_ids, lie_avail) ->
147 -- Do the rest; we don't need to add the pat_tvs to the envt
148 -- because they all appear in the pat_ids's types
149 tcExtendEnvWithPat pat_ids (
150 tcStmts do_or_lc m stmts elt_ty
151 ) `thenTc` \ (stmts', stmts_lie) ->
154 -- Reinstate context for existential checks
155 tcSetErrCtxt (stmtCtxt do_or_lc stmt) $
156 tcExtendGlobalTyVars (tyVarsOfType (m elt_ty)) $
157 tcAddErrCtxtM (existentialPatCtxt pat_tvs pat_ids) $
159 checkSigTyVars (bagToList pat_tvs) `thenTc` \ zonked_pat_tvs ->
162 (text ("the existential context of a data constructor"))
163 (mkVarSet zonked_pat_tvs)
164 lie_avail stmts_lie `thenTc` \ (final_lie, dict_binds) ->
166 returnTc (BindStmt pat' exp' src_loc :
167 LetStmt (MonoBind dict_binds [] Recursive) :
169 lie_req `plusLIE` final_lie)
171 tcStmts do_or_lc m (LetStmt binds : stmts) elt_ty
172 = tcBindsAndThen -- No error context, but a binding group is
173 combine -- rather a large thing for an error context anyway
175 (tcStmts do_or_lc m stmts elt_ty)
177 combine is_rec binds' stmts' = LetStmt (MonoBind binds' [] is_rec) : stmts'
180 isDoStmt DoStmt = True
181 isDoStmt other = False
183 stmtCtxt do_or_lc stmt
184 = hang (ptext SLIT("In") <+> what <> colon)
187 what = case do_or_lc of
188 ListComp -> ptext SLIT("a list-comprehension qualifier")
189 DoStmt -> ptext SLIT("a do statement:")
190 PatBindRhs -> thing <+> ptext SLIT("a pattern binding")
191 FunRhs f -> thing <+> ptext SLIT("an equation for") <+> quotes (ppr f)
192 CaseAlt -> thing <+> ptext SLIT("a case alternative")
193 LambdaBody -> thing <+> ptext SLIT("a lambda abstraction")
195 BindStmt _ _ _ -> ptext SLIT("a pattern guard for")
196 GuardStmt _ _ -> ptext SLIT("a guard for")
197 ExprStmt _ _ -> ptext SLIT("the right-hand side of")