2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[TcSplice]{Template Haskell splices}
7 module TcSplice( tcSpliceExpr, tcSpliceDecls, tcBracket ) where
9 #include "HsVersions.h"
11 import HscMain ( compileExpr )
12 import TcRnDriver ( importSupportingDecls, tcTopSrcDecls )
13 -- These imports are the reason that TcSplice
14 -- is very high up the module hierarchy
16 import qualified Language.Haskell.THSyntax as Meta
18 import HscTypes ( HscEnv(..), GhciMode(..), PersistentCompilerState(..), unQualInScope )
19 import HsSyn ( HsBracket(..), HsExpr(..) )
20 import Convert ( convertToHsExpr, convertToHsDecls )
21 import RnExpr ( rnExpr )
22 import RdrHsSyn ( RdrNameHsExpr, RdrNameHsDecl )
23 import RnHsSyn ( RenamedHsExpr )
24 import TcExpr ( tcCheckRho, tcMonoExpr )
25 import TcHsSyn ( TcExpr, TypecheckedHsExpr, mkHsLet, zonkTopExpr )
26 import TcSimplify ( tcSimplifyTop, tcSimplifyBracket )
27 import TcUnify ( Expected, unifyTauTy, zapExpectedTo, zapExpectedType )
28 import TcType ( TcType, openTypeKind, mkAppTy )
29 import TcEnv ( spliceOK, tcMetaTy, tcWithTempInstEnv, bracketOK )
30 import TcRnTypes ( TopEnv(..) )
31 import TcMType ( newTyVarTy, UserTypeCtxt(ExprSigCtxt) )
32 import TcMonoType ( tcHsSigType )
36 import TysWiredIn ( mkListTy )
37 import DsMeta ( expQTyConName, decQTyConName, typeQTyConName, decTyConName, qTyConName )
38 import ErrUtils (Message)
40 import Panic ( showException )
41 import GHC.Base ( unsafeCoerce# ) -- Should have a better home in the module hierarchy
46 %************************************************************************
48 \subsection{Main interface + stubs for the non-GHCI case
50 %************************************************************************
53 tcSpliceDecls :: RenamedHsExpr -> TcM [RdrNameHsDecl]
61 tcSpliceExpr n e ty = pprPanic "Cant do tcSpliceExpr without GHCi" (ppr e)
62 tcSpliceDecls e = pprPanic "Cant do tcSpliceDecls without GHCi" (ppr e)
66 %************************************************************************
68 \subsection{Quoting an expression}
70 %************************************************************************
73 tcBracket :: HsBracket Name -> Expected TcType -> TcM TcExpr
74 tcBracket brack res_ty
75 = getStage `thenM` \ level ->
76 case bracketOK level of {
77 Nothing -> failWithTc (illegalBracket level) ;
80 -- Typecheck expr to make sure it is valid,
81 -- but throw away the results. We'll type check
82 -- it again when we actually use it.
83 newMutVar [] `thenM` \ pending_splices ->
84 getLIEVar `thenM` \ lie_var ->
86 setStage (Brack next_level pending_splices lie_var) (
87 getLIE (tc_bracket brack)
88 ) `thenM` \ (meta_ty, lie) ->
89 tcSimplifyBracket lie `thenM_`
91 -- Make the expected type have the right shape
92 zapExpectedTo res_ty meta_ty `thenM_`
94 -- Return the original expression, not the type-decorated one
95 readMutVar pending_splices `thenM` \ pendings ->
96 returnM (HsBracketOut brack pendings)
99 tc_bracket :: HsBracket Name -> TcM TcType
100 tc_bracket (ExpBr expr)
101 = newTyVarTy openTypeKind `thenM` \ any_ty ->
102 tcCheckRho expr any_ty `thenM_`
103 tcMetaTy expQTyConName
104 -- Result type is Expr (= Q Exp)
106 tc_bracket (TypBr typ)
107 = tcHsSigType ExprSigCtxt typ `thenM_`
108 tcMetaTy typeQTyConName
109 -- Result type is Type (= Q Typ)
111 tc_bracket (DecBr decls)
112 = tcWithTempInstEnv (tcTopSrcDecls decls) `thenM_`
113 -- Typecheck the declarations, dicarding any side effects
114 -- on the instance environment (which is in a mutable variable)
115 -- and the extended environment. We'll get all that stuff
116 -- later, when we splice it in
118 tcMetaTy decTyConName `thenM` \ decl_ty ->
119 tcMetaTy qTyConName `thenM` \ q_ty ->
120 returnM (mkAppTy q_ty (mkListTy decl_ty))
121 -- Result type is Q [Dec]
125 %************************************************************************
127 \subsection{Splicing an expression}
129 %************************************************************************
132 tcSpliceExpr name expr res_ty
133 = getStage `thenM` \ level ->
134 case spliceOK level of {
135 Nothing -> failWithTc (illegalSplice level) ;
139 Comp -> tcTopSplice expr res_ty ;
140 Brack _ ps_var lie_var ->
142 -- A splice inside brackets
143 -- NB: ignore res_ty, apart from zapping it to a mono-type
144 -- e.g. [| reverse $(h 4) |]
145 -- Here (h 4) :: Q Exp
146 -- but $(h 4) :: forall a.a i.e. anything!
148 zapExpectedType res_ty `thenM_`
149 tcMetaTy expQTyConName `thenM` \ meta_exp_ty ->
150 setStage (Splice next_level) (
152 tcCheckRho expr meta_exp_ty
155 -- Write the pending splice into the bucket
156 readMutVar ps_var `thenM` \ ps ->
157 writeMutVar ps_var ((name,expr') : ps) `thenM_`
159 returnM (panic "tcSpliceExpr") -- The returned expression is ignored
162 -- tcTopSplice used to have this:
163 -- Note that we do not decrement the level (to -1) before
164 -- typechecking the expression. For example:
165 -- f x = $( ...$(g 3) ... )
166 -- The recursive call to tcMonoExpr will simply expand the
167 -- inner escape before dealing with the outer one
169 tcTopSplice expr res_ty
170 = tcMetaTy expQTyConName `thenM` \ meta_exp_ty ->
172 -- Typecheck the expression
173 tcTopSpliceExpr expr meta_exp_ty `thenM` \ zonked_q_expr ->
175 -- Run the expression
176 traceTc (text "About to run" <+> ppr zonked_q_expr) `thenM_`
177 runMetaE zonked_q_expr `thenM` \ simple_expr ->
180 -- simple_expr :: Meta.Exp
182 expr2 :: RdrNameHsExpr
183 expr2 = convertToHsExpr simple_expr
185 traceTc (text "Got result" <+> ppr expr2) `thenM_`
187 showSplice "expression"
188 zonked_q_expr (ppr expr2) `thenM_`
189 initRn SourceMode (rnExpr expr2) `thenM` \ (exp3, fvs) ->
190 importSupportingDecls fvs `thenM` \ env ->
192 setGblEnv env (tcMonoExpr exp3 res_ty)
195 tcTopSpliceExpr :: RenamedHsExpr -> TcType -> TcM TypecheckedHsExpr
196 -- Type check an expression that is the body of a top-level splice
197 -- (the caller will compile and run it)
198 tcTopSpliceExpr expr meta_ty
199 = checkNoErrs $ -- checkNoErrs: must not try to run the thing
200 -- if the type checker fails!
202 setStage topSpliceStage $
204 -- Typecheck the expression
205 getLIE (tcCheckRho expr meta_ty) `thenM` \ (expr', lie) ->
207 -- Solve the constraints
208 tcSimplifyTop lie `thenM` \ const_binds ->
211 zonkTopExpr (mkHsLet const_binds expr')
215 %************************************************************************
217 \subsection{Splicing an expression}
219 %************************************************************************
222 -- Always at top level
224 = tcMetaTy decTyConName `thenM` \ meta_dec_ty ->
225 tcMetaTy qTyConName `thenM` \ meta_q_ty ->
227 list_q = mkAppTy meta_q_ty (mkListTy meta_dec_ty)
229 tcTopSpliceExpr expr list_q `thenM` \ zonked_q_expr ->
231 -- Run the expression
232 traceTc (text "About to run" <+> ppr zonked_q_expr) `thenM_`
233 runMetaD zonked_q_expr `thenM` \ simple_expr ->
234 -- simple_expr :: [Meta.Dec]
235 -- decls :: [RdrNameHsDecl]
236 handleErrors (convertToHsDecls simple_expr) `thenM` \ decls ->
237 traceTc (text "Got result" <+> vcat (map ppr decls)) `thenM_`
238 showSplice "declarations"
239 zonked_q_expr (vcat (map ppr decls)) `thenM_`
242 where handleErrors :: [Either a Message] -> TcM [a]
243 handleErrors [] = return []
244 handleErrors (Left x:xs) = liftM (x:) (handleErrors xs)
245 handleErrors (Right m:xs) = do addErrTc m
250 %************************************************************************
252 \subsection{Running an expression}
254 %************************************************************************
257 runMetaE :: TypecheckedHsExpr -- Of type (Q Exp)
258 -> TcM Meta.Exp -- Of type Exp
259 runMetaE e = runMeta e
261 runMetaD :: TypecheckedHsExpr -- Of type Q [Dec]
262 -> TcM [Meta.Dec] -- Of type [Dec]
263 runMetaD e = runMeta e
265 runMeta :: TypecheckedHsExpr -- Of type X
266 -> TcM t -- Of type t
268 = getTopEnv `thenM` \ top_env ->
269 getGblEnv `thenM` \ tcg_env ->
270 getEps `thenM` \ eps ->
271 getNameCache `thenM` \ name_cache ->
272 getModule `thenM` \ this_mod ->
274 ghci_mode = top_mode top_env
276 hsc_env = HscEnv { hsc_mode = ghci_mode, hsc_HPT = top_hpt top_env,
277 hsc_dflags = top_dflags top_env }
279 pcs = PCS { pcs_nc = name_cache, pcs_EPS = eps }
281 type_env = tcg_type_env tcg_env
282 rdr_env = tcg_rdr_env tcg_env
284 -- Wrap the compile-and-run in an exception-catcher
285 -- Compiling might fail if linking fails
286 -- Running might fail if it throws an exception
288 hval <- HscMain.compileExpr
290 rdr_env type_env expr
291 Meta.runQ (unsafeCoerce# hval) -- Coerce it to Q t, and run it
292 )) `thenM` \ either_tval ->
295 Left exn -> failWithTc (vcat [text "Exception when trying to run compile-time code:",
296 nest 4 (vcat [text "Code:" <+> ppr expr,
297 text ("Exn: " ++ Panic.showException exn)])])
303 -----------------------------------
308 import Lib( g :: Int -> M Exp )
310 f x y = [| \z -> (x, $(g y), z, map, h) |]
312 h p = $( (\q r -> if q then [| \s -> (p,r,s) |]
317 f :: Liftable a => a -> Int -> M Exp
318 f = /\a -> \d::Liftable a ->
319 \ x y -> genSym "z" `bindM` \ z::String ->
320 g y `bindM` \ vv::Exp ->
321 Lam z (Tup [lift d x, v, Var z,
322 Glob "Prelude" "map",
326 h :: Tree Int -> M Exp
327 h = \p -> \s' -> (p,3,s')
332 map: C0 C1 (top-level/imp)
344 f x y = lam "z" (tup [lift x, g y, var "z",
345 [| map |], [| h |] ])
348 f = \x y -> lam "z" (tup [lift d x, g y, var "z",
349 return (Glob "Prelude" "map"),
350 return (Glob "Foo" "h")])
359 h v = [| \x -> map $v x |]
361 g :: Tree Int -> M Exp
364 g x = \x' -> map x x'
366 *** Simon claims x does not have to be liftable! **
370 Level 2 code returned by run time (generation time)
372 Non-top-level variables
375 bound at level 0 --> x
376 bound at level 1 --> var "x"
378 not inside brackets --> x
382 bound at level 0 --> x
383 bound at level 1 --> var "x"
387 Two successive brackets aren't allowed
390 %************************************************************************
392 \subsection{Errors and contexts}
394 %************************************************************************
397 showSplice :: String -> TypecheckedHsExpr -> SDoc -> TcM ()
398 showSplice what before after
399 = getSrcLocM `thenM` \ loc ->
400 traceSplice (vcat [ppr loc <> colon <+> text "Splicing" <+> text what,
401 nest 2 (sep [nest 2 (ppr before),
406 = ptext SLIT("Illegal bracket at level") <+> ppr level
409 = ptext SLIT("Illegal splice at level") <+> ppr level