f29069e4e863db01dd63f3b5d1abf1930f5182f3
[ghc-hetmet.git] / ghc / compiler / typecheck / TcSplice.lhs
1 2%
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3 %
4 \section[TcSplice]{Template Haskell splices}
5
6 \begin{code}
7 module TcSplice( tcSpliceExpr, tcSpliceDecls, tcBracket ) where
8
9 #include "HsVersions.h"
10
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
15
16 import qualified Language.Haskell.THSyntax as Meta
17
18 import HscTypes         ( HscEnv(..), GhciMode(..), PersistentCompilerState(..), unQualInScope )
19 import HsSyn            ( HsBracket(..) )
20 import Convert          ( convertToHsExpr, convertToHsDecls )
21 import RnExpr           ( rnExpr )
22 import RdrHsSyn         ( RdrNameHsExpr, RdrNameHsDecl )
23 import RnHsSyn          ( RenamedHsExpr )
24 import TcExpr           ( tcMonoExpr )
25 import TcHsSyn          ( TcExpr, TypecheckedHsExpr, mkHsLet, zonkTopExpr )
26 import TcSimplify       ( tcSimplifyTop )
27 import TcType           ( TcType, openTypeKind )
28 import TcEnv            ( spliceOK, tcMetaTy )
29 import TcRnTypes        ( TopEnv(..) )
30 import TcMType          ( newTyVarTy )
31 import Name             ( Name )
32 import TcRnMonad
33
34 import TysWiredIn       ( mkListTy )
35 import DsMeta           ( exprTyConName, declTyConName )
36 import Outputable
37 import GHC.Base         ( unsafeCoerce# )       -- Should have a better home in the module hierarchy
38 \end{code}
39
40
41 %************************************************************************
42 %*                                                                      *
43 \subsection{Main interface + stubs for the non-GHCI case
44 %*                                                                      *
45 %************************************************************************
46
47 \begin{code}
48 tcSpliceDecls :: RenamedHsExpr -> TcM [RdrNameHsDecl]
49
50 tcSpliceExpr :: Name 
51              -> RenamedHsExpr
52              -> TcType
53              -> TcM TcExpr
54
55 #ifndef GHCI
56 tcSpliceExpr n e ty = pprPanic "Cant do tcSpliceExpr without GHCi" (ppr e)
57 tcSpliceDecls e     = pprPanic "Cant do tcSpliceDecls without GHCi" (ppr e)
58 #else
59 \end{code}
60
61 %************************************************************************
62 %*                                                                      *
63 \subsection{Splicing an expression}
64 %*                                                                      *
65 %************************************************************************
66
67 \begin{code}
68 tcBracket :: HsBracket Name -> TcM TcType
69 tcBracket (ExpBr expr) 
70   = newTyVarTy openTypeKind             `thenM` \ any_ty ->
71     tcMonoExpr expr any_ty              `thenM_`
72     tcMetaTy exprTyConName
73
74 tcBracket (DecBr decls)
75   = tcTopSrcDecls decls                 `thenM_`
76     tcMetaTy declTyConName              `thenM` \ decl_ty ->
77     returnM (mkListTy decl_ty)
78 \end{code}
79
80 %************************************************************************
81 %*                                                                      *
82 \subsection{Splicing an expression}
83 %*                                                                      *
84 %************************************************************************
85
86 \begin{code}
87 tcSpliceExpr name expr res_ty
88   = getStage            `thenM` \ level ->
89     case spliceOK level of {
90         Nothing         -> failWithTc (illegalSplice level) ;
91         Just next_level -> 
92
93     case level of {
94         Comp                   -> tcTopSplice expr res_ty ;
95         Brack _ ps_var lie_var ->  
96
97         -- A splice inside brackets
98         -- NB: ignore res_ty
99         -- e.g.   [| reverse $(h 4) |]
100         -- Here (h 4) :: Q Exp
101         -- but $(h 4) :: forall a.a     i.e. anything!
102
103     tcMetaTy exprTyConName                      `thenM` \ meta_exp_ty ->
104     setStage (Splice next_level) (
105         setLIEVar lie_var          $
106         tcMonoExpr expr meta_exp_ty
107     )                                           `thenM` \ expr' ->
108
109         -- Write the pending splice into the bucket
110     readMutVar ps_var                           `thenM` \ ps ->
111     writeMutVar ps_var ((name,expr') : ps)      `thenM_`
112
113     returnM (panic "tcSpliceExpr")      -- The returned expression is ignored
114     }} 
115
116 -- tcTopSplice used to have this:
117 -- Note that we do not decrement the level (to -1) before 
118 -- typechecking the expression.  For example:
119 --      f x = $( ...$(g 3) ... )
120 -- The recursive call to tcMonoExpr will simply expand the 
121 -- inner escape before dealing with the outer one
122
123 tcTopSplice expr res_ty
124   = tcMetaTy exprTyConName              `thenM` \ meta_exp_ty ->
125     setStage topSpliceStage (
126         getLIE (tcMonoExpr expr meta_exp_ty)
127     )                                   `thenM` \ (expr', lie) ->
128
129         -- Solve the constraints
130     tcSimplifyTop lie                   `thenM` \ const_binds ->
131     let 
132         q_expr = mkHsLet const_binds expr'
133     in
134     zonkTopExpr q_expr                  `thenM` \ zonked_q_expr ->
135
136         -- Run the expression
137     traceTc (text "About to run" <+> ppr zonked_q_expr)         `thenM_`
138     runMetaE zonked_q_expr              `thenM` \ simple_expr ->
139   
140     let 
141         -- simple_expr :: Meta.Exp
142
143         expr2 :: RdrNameHsExpr
144         expr2 = convertToHsExpr simple_expr 
145     in
146     traceTc (text "Got result" <+> ppr expr2)   `thenM_`
147
148     showSplice "expression" 
149                zonked_q_expr (ppr expr2)        `thenM_`
150     initRn SourceMode (rnExpr expr2)            `thenM` \ (exp3, fvs) ->
151     importSupportingDecls fvs                   `thenM` \ env ->
152
153     setGblEnv env (tcMonoExpr exp3 res_ty)
154 \end{code}
155
156
157 %************************************************************************
158 %*                                                                      *
159 \subsection{Splicing an expression}
160 %*                                                                      *
161 %************************************************************************
162
163 \begin{code}
164 -- Always at top level
165 tcSpliceDecls expr
166   = tcMetaTy declTyConName              `thenM` \ meta_dec_ty ->
167     setStage topSpliceStage (
168         getLIE (tcMonoExpr expr (mkListTy meta_dec_ty))
169     )                                   `thenM` \ (expr', lie) ->
170         -- Solve the constraints
171     tcSimplifyTop lie                   `thenM` \ const_binds ->
172     let 
173         q_expr = mkHsLet const_binds expr'
174     in
175     zonkTopExpr q_expr                  `thenM` \ zonked_q_expr ->
176
177         -- Run the expression
178     traceTc (text "About to run" <+> ppr zonked_q_expr)         `thenM_`
179     runMetaD zonked_q_expr              `thenM` \ simple_expr ->
180     let 
181         -- simple_expr :: [Meta.Dec]
182         decls :: [RdrNameHsDecl]
183         decls = convertToHsDecls simple_expr 
184     in
185     traceTc (text "Got result" <+> vcat (map ppr decls))        `thenM_`
186     showSplice "declarations"
187                zonked_q_expr (vcat (map ppr decls))             `thenM_`
188     returnM decls
189 \end{code}
190
191
192 %************************************************************************
193 %*                                                                      *
194 \subsection{Running an expression}
195 %*                                                                      *
196 %************************************************************************
197
198 \begin{code}
199 runMetaE :: TypecheckedHsExpr   -- Of type (Q Exp)
200          -> TcM Meta.Exp        -- Of type Exp
201 runMetaE e = runMeta tcRunQ e
202
203 runMetaD :: TypecheckedHsExpr   -- Of type [Q Dec]
204          -> TcM [Meta.Dec]      -- Of type [Dec]
205 runMetaD e = runMeta run_decl e
206            where
207              run_decl :: [Meta.Decl] -> TcM [Meta.Dec]
208              run_decl ds = mappM tcRunQ ds
209
210 -- Warning: if Q is anything other than IO, we need to change this
211 tcRunQ :: Meta.Q a -> TcM a
212 tcRunQ thing = ioToTcRn thing
213
214
215 runMeta :: (x -> TcM t)         -- :: X -> IO t
216         -> TypecheckedHsExpr    -- Of type X
217         -> TcM t                -- Of type t
218 runMeta run_it expr :: TcM t
219   = getTopEnv           `thenM` \ top_env ->
220     getEps              `thenM` \ eps ->
221     getNameCache        `thenM` \ name_cache -> 
222     getModule           `thenM` \ this_mod ->
223     getGlobalRdrEnv     `thenM` \ rdr_env -> 
224     let
225         ghci_mode = top_mode top_env
226
227         hsc_env = HscEnv { hsc_mode = ghci_mode, hsc_HPT = top_hpt top_env,
228                            hsc_dflags = top_dflags top_env }
229
230         pcs = PCS { pcs_nc = name_cache, pcs_EPS = eps }
231
232         print_unqual = unQualInScope rdr_env
233     in
234     if (ghci_mode == OneShot) then
235         failWithTc (ptext SLIT("You must use --make or --interactive to run splice expressions"))
236         -- The reason for this is that the demand-linker doesn't have
237         -- enough information available to link all the things that
238         -- are needed when you try to run a splice
239     else
240
241     ioToTcRn (HscMain.compileExpr hsc_env pcs this_mod 
242                                   print_unqual expr) `thenM` \ hval ->
243
244     tryM (run_it (unsafeCoerce# hval))  `thenM` \ either_tval ->
245
246     case either_tval of
247           Left exn -> failWithTc (vcat [text "Exception when running compile-time code:", 
248                                         nest 4 (vcat [text "Code:" <+> ppr expr,
249                                                       text ("Exn: " ++ show exn)])])
250           Right v  -> returnM v
251 \end{code}
252
253
254
255 -----------------------------------
256         Random comments
257
258
259       module Foo where
260         import Lib( g :: Int -> M Exp )
261         h x = not x     
262         f x y = [| \z -> (x, $(g y), z, map, h) |]
263
264         h p = $( (\q r -> if q then [| \s -> (p,r,s) |] 
265                                else ... ) True 3)   )
266
267 ==> core
268
269         f :: Liftable a => a -> Int -> M Exp
270         f = /\a -> \d::Liftable a ->
271             \ x y -> genSym "z"         `bindM` \ z::String ->
272                      g y                `bindM` \ vv::Exp ->
273                      Lam z (Tup [lift d x, v, Var z, 
274                                  Glob "Prelude" "map",
275                                  Glob "Foo" "h"])
276
277
278         h :: Tree Int -> M Exp
279         h = \p -> \s' -> (p,3,s')
280
281
282                 Bound   Used
283
284         map:    C0      C1      (top-level/imp)
285         x:      C0      C1      (lam/case)
286         y:      C0      C0
287         z:      C1      C1
288
289         p:      C0      S1
290         r:      S0      S1
291         q:      S0      S0
292         s:      S1      S1
293
294 -------
295
296         f x y = lam "z" (tup [lift x, g y, var "z", 
297                               [| map |], [| h |] ])
298 ==> core
299         
300         f = \x y -> lam "z" (tup [lift d x, g y, var "z",
301                                   return (Glob "Prelude" "map"),
302                                   return (Glob "Foo" "h")])
303
304
305
306
307
308
309
310         h :: M Exp -> M Exp
311         h v = [| \x -> map $v x |]
312
313         g :: Tree Int -> M Exp
314         g x = $(h [| x |])
315 ==>
316         g x = \x' -> map x x'
317
318 *** Simon claims x does not have to be liftable! **
319         
320 Level 0 compile time
321 Level 1 run time
322 Level 2 code returned by run time (generation time)
323
324 Non-top-level variables
325         x occurs at level 1
326           inside brackets
327             bound at level 0    --> x
328             bound at level 1    --> var "x"
329
330           not inside brackets   --> x
331
332         x at level 2
333           inside brackets
334             bound at level 0    --> x
335             bound at level 1    --> var "x"
336
337         f x = x
338
339 Two successive brackets aren't allowed
340
341
342 %************************************************************************
343 %*                                                                      *
344 \subsection{Errors and contexts}
345 %*                                                                      *
346 %************************************************************************
347
348 \begin{code}
349 showSplice :: String -> TypecheckedHsExpr -> SDoc -> TcM ()
350 showSplice what before after
351   = getSrcLocM          `thenM` \ loc ->
352     traceSplice (hang (ppr loc <> colon <+> text "Splicing" <+> text what) 4
353                       (sep [nest 2 (ppr before),
354                             text "======>",
355                             nest 2 after]))
356
357 illegalSplice level
358   = ptext SLIT("Illegal splice at level") <+> ppr level
359
360 #endif  /* GHCI */
361 \end{code}