3bae06ad5626d462e422fe60b7f67fa5b9d8157c
[ghc-hetmet.git] / ghc / compiler / deSugar / DsMeta.hs
1 -----------------------------------------------------------------------------
2 -- The purpose of this module is to transform an HsExpr into a CoreExpr which
3 -- when evaluated, returns a (Meta.Q Meta.Exp) computation analogous to the
4 -- input HsExpr. We do this in the DsM monad, which supplies access to
5 -- CoreExpr's of the "smart constructors" of the Meta.Exp datatype.
6 --
7 -- It also defines a bunch of knownKeyNames, in the same way as is done
8 -- in prelude/PrelNames.  It's much more convenient to do it here, becuase
9 -- otherwise we have to recompile PrelNames whenever we add a Name, which is
10 -- a Royal Pain (triggers other recompilation).
11 -----------------------------------------------------------------------------
12
13
14 module DsMeta( dsBracket, 
15                templateHaskellNames, qTyConName, nameTyConName,
16                liftName, expQTyConName, decQTyConName, typeQTyConName,
17                decTyConName, typeTyConName, mkNameG_dName, mkNameG_vName, mkNameG_tcName
18                 ) where
19
20 #include "HsVersions.h"
21
22 import {-# SOURCE #-}   DsExpr ( dsExpr )
23
24 import MatchLit   ( dsLit )
25 import DsUtils    ( mkListExpr, mkStringExpr, mkCoreTup, mkIntExpr )
26 import DsMonad
27
28 import qualified Language.Haskell.TH as TH
29
30 import HsSyn
31 import PrelNames  ( rationalTyConName, integerTyConName, negateName )
32 import OccName    ( isDataOcc, isTvOcc, occNameUserString )
33 -- To avoid clashes with DsMeta.varName we must make a local alias for OccName.varName
34 -- we do this by removing varName from the import of OccName above, making
35 -- a qualified instance of OccName and using OccNameAlias.varName where varName
36 -- ws previously used in this file.
37 import qualified OccName
38
39 import Module     ( Module, mkModule, mkModuleName, moduleUserString )
40 import Id         ( Id, mkLocalId )
41 import OccName    ( mkOccFS )
42 import Name       ( Name, mkExternalName, localiseName, nameOccName, nameModule, 
43                     isExternalName, getSrcLoc )
44 import NameEnv
45 import Type       ( Type, mkGenTyConApp )
46 import TcType     ( tcTyConAppArgs )
47 import TyCon      ( tyConName )
48 import TysWiredIn ( parrTyCon )
49 import CoreSyn
50 import CoreUtils  ( exprType )
51 import SrcLoc     ( noSrcLoc, unLoc, Located(..), SrcSpan, srcLocSpan )
52 import Maybe      ( catMaybes )
53 import Unique     ( mkPreludeTyConUnique, mkPreludeMiscIdUnique, getKey, Uniquable(..) )
54 import BasicTypes ( isBoxed ) 
55 import Packages   ( thPackage )
56 import Outputable
57 import Bag        ( bagToList )
58 import FastString ( unpackFS )
59 import ForeignCall ( Safety(..), ForeignCall(..), CCallConv(..),
60                      CCallTarget(..) )
61
62 import Monad ( zipWithM )
63 import List ( sortBy )
64  
65 -----------------------------------------------------------------------------
66 dsBracket :: HsBracket Name -> [PendingSplice] -> DsM CoreExpr
67 -- Returns a CoreExpr of type TH.ExpQ
68 -- The quoted thing is parameterised over Name, even though it has
69 -- been type checked.  We don't want all those type decorations!
70
71 dsBracket brack splices
72   = dsExtendMetaEnv new_bit (do_brack brack)
73   where
74     new_bit = mkNameEnv [(n, Splice (unLoc e)) | (n,e) <- splices]
75
76     do_brack (VarBr n)  = do { MkC e1  <- lookupOcc n ; return e1 }
77     do_brack (ExpBr e)  = do { MkC e1  <- repLE e     ; return e1 }
78     do_brack (PatBr p)  = do { MkC p1  <- repLP p     ; return p1 }
79     do_brack (TypBr t)  = do { MkC t1  <- repLTy t     ; return t1 }
80     do_brack (DecBr ds) = do { MkC ds1 <- repTopDs ds ; return ds1 }
81
82 {- -------------- Examples --------------------
83
84   [| \x -> x |]
85 ====>
86   gensym (unpackString "x"#) `bindQ` \ x1::String ->
87   lam (pvar x1) (var x1)
88
89
90   [| \x -> $(f [| x |]) |]
91 ====>
92   gensym (unpackString "x"#) `bindQ` \ x1::String ->
93   lam (pvar x1) (f (var x1))
94 -}
95
96
97 -------------------------------------------------------
98 --                      Declarations
99 -------------------------------------------------------
100
101 repTopDs :: HsGroup Name -> DsM (Core (TH.Q [TH.Dec]))
102 repTopDs group
103  = do { let { bndrs = map unLoc (groupBinders group) } ;
104         ss <- mkGenSyms bndrs ;
105
106         -- Bind all the names mainly to avoid repeated use of explicit strings.
107         -- Thus we get
108         --      do { t :: String <- genSym "T" ;
109         --           return (Data t [] ...more t's... }
110         -- The other important reason is that the output must mention
111         -- only "T", not "Foo:T" where Foo is the current module
112
113         
114         decls <- addBinds ss (do {
115                         val_ds  <- mapM rep_bind_group (hs_valds group) ;
116                         tycl_ds <- mapM repTyClD (hs_tyclds group) ;
117                         inst_ds <- mapM repInstD' (hs_instds group) ;
118                         for_ds <- mapM repForD (hs_fords group) ;
119                         -- more needed
120                         return (de_loc $ sort_by_loc $ concat val_ds ++ catMaybes tycl_ds ++ inst_ds ++ for_ds) }) ;
121
122         decl_ty <- lookupType decQTyConName ;
123         let { core_list = coreList' decl_ty decls } ;
124
125         dec_ty <- lookupType decTyConName ;
126         q_decs  <- repSequenceQ dec_ty core_list ;
127
128         wrapNongenSyms ss q_decs
129         -- Do *not* gensym top-level binders
130       }
131
132 groupBinders (HsGroup { hs_valds = val_decls, hs_tyclds = tycl_decls,
133                         hs_fords = foreign_decls })
134 -- Collect the binders of a Group
135   = collectGroupBinders val_decls ++
136     [n | d <- tycl_decls, n <- tyClDeclNames (unLoc d)] ++
137     [n | L _ (ForeignImport n _ _ _) <- foreign_decls]
138
139
140 {-      Note [Binders and occurrences]
141         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
142 When we desugar [d| data T = MkT |]
143 we want to get
144         Data "T" [] [Con "MkT" []] []
145 and *not*
146         Data "Foo:T" [] [Con "Foo:MkT" []] []
147 That is, the new data decl should fit into whatever new module it is
148 asked to fit in.   We do *not* clone, though; no need for this:
149         Data "T79" ....
150
151 But if we see this:
152         data T = MkT 
153         foo = reifyDecl T
154
155 then we must desugar to
156         foo = Data "Foo:T" [] [Con "Foo:MkT" []] []
157
158 So in repTopDs we bring the binders into scope with mkGenSyms and addBinds.
159 And we use lookupOcc, rather than lookupBinder
160 in repTyClD and repC.
161
162 -}
163
164 repTyClD :: LTyClDecl Name -> DsM (Maybe (SrcSpan, Core TH.DecQ))
165
166 repTyClD (L loc (TyData { tcdND = DataType, tcdCtxt = cxt, 
167                     tcdLName = tc, tcdTyVars = tvs, 
168                     tcdCons = cons, tcdDerivs = mb_derivs }))
169  = do { tc1 <- lookupLOcc tc ;          -- See note [Binders and occurrences] 
170         dec <- addTyVarBinds tvs $ \bndrs -> do {
171                cxt1    <- repLContext cxt ;
172                cons1   <- mapM repC cons ;
173                cons2   <- coreList conQTyConName cons1 ;
174                derivs1 <- repDerivs mb_derivs ;
175                bndrs1  <- coreList nameTyConName bndrs ;
176                repData cxt1 tc1 bndrs1 cons2 derivs1 } ;
177         return $ Just (loc, dec) }
178
179 repTyClD (L loc (TyData { tcdND = NewType, tcdCtxt = cxt, 
180                     tcdLName = tc, tcdTyVars = tvs, 
181                     tcdCons = [con], tcdDerivs = mb_derivs }))
182  = do { tc1 <- lookupLOcc tc ;          -- See note [Binders and occurrences] 
183         dec <- addTyVarBinds tvs $ \bndrs -> do {
184                cxt1   <- repLContext cxt ;
185                con1   <- repC con ;
186                derivs1 <- repDerivs mb_derivs ;
187                bndrs1  <- coreList nameTyConName bndrs ;
188                repNewtype cxt1 tc1 bndrs1 con1 derivs1 } ;
189         return $ Just (loc, dec) }
190
191 repTyClD (L loc (TySynonym { tcdLName = tc, tcdTyVars = tvs, tcdSynRhs = ty }))
192  = do { tc1 <- lookupLOcc tc ;          -- See note [Binders and occurrences] 
193         dec <- addTyVarBinds tvs $ \bndrs -> do {
194                ty1     <- repLTy ty ;
195                bndrs1  <- coreList nameTyConName bndrs ;
196                repTySyn tc1 bndrs1 ty1 } ;
197         return (Just (loc, dec)) }
198
199 repTyClD (L loc (ClassDecl { tcdCtxt = cxt, tcdLName = cls, 
200                       tcdTyVars = tvs, 
201                       tcdFDs = [],      -- We don't understand functional dependencies
202                       tcdSigs = sigs, tcdMeths = meth_binds }))
203  = do { cls1 <- lookupLOcc cls ;                -- See note [Binders and occurrences] 
204         dec  <- addTyVarBinds tvs $ \bndrs -> do {
205                   cxt1   <- repLContext cxt ;
206                   sigs1  <- rep_sigs sigs ;
207                   binds1 <- rep_binds meth_binds ;
208                   decls1 <- coreList decQTyConName (sigs1 ++ binds1) ;
209                   bndrs1 <- coreList nameTyConName bndrs ;
210                   repClass cxt1 cls1 bndrs1 decls1 } ;
211         return $ Just (loc, dec) }
212
213 -- Un-handled cases
214 repTyClD (L loc d) = do { dsWarn (loc, hang ds_msg 4 (ppr d)) ;
215                            return Nothing
216                          }
217
218
219 repInstD' (L loc (InstDecl ty binds _))         -- Ignore user pragmas for now
220  = do   { i <- addTyVarBinds tvs $ \tv_bndrs ->
221                 -- We must bring the type variables into scope, so their occurrences
222                 -- don't fail,  even though the binders don't appear in the resulting 
223                 -- data structure
224                 do {  cxt1 <- repContext cxt
225                    ; inst_ty1 <- repPred (HsClassP cls tys)
226                    ; ss <- mkGenSyms (collectHsBindBinders binds)
227                    ; binds1 <- addBinds ss (rep_binds binds)
228                    ; decls1 <- coreList decQTyConName binds1
229                    ; decls2 <- wrapNongenSyms ss decls1
230                    -- wrapNonGenSyms: do not clone the class op names!
231                    -- They must be called 'op' etc, not 'op34'
232                    ; repInst cxt1 inst_ty1 decls2 }
233
234         ; return (loc, i)}
235  where
236    (tvs, cxt, cls, tys) = splitHsInstDeclTy (unLoc ty)
237
238 repForD :: Located (ForeignDecl Name) -> DsM (SrcSpan, Core TH.DecQ)
239 repForD (L loc (ForeignImport name typ (CImport cc s ch cn cis) _))
240  = do MkC name' <- lookupLOcc name
241       MkC typ' <- repLTy typ
242       MkC cc' <- repCCallConv cc
243       MkC s' <- repSafety s
244       MkC str <- coreStringLit $ static
245                               ++ unpackFS ch ++ " "
246                               ++ unpackFS cn ++ " "
247                               ++ conv_cimportspec cis
248       dec <- rep2 forImpDName [cc', s', str, name', typ']
249       return (loc, dec)
250  where
251     conv_cimportspec (CLabel cls) = panic "repForD': CLabel Not handled"
252     conv_cimportspec (CFunction DynamicTarget) = "dynamic"
253     conv_cimportspec (CFunction (StaticTarget fs)) = unpackFS fs
254     conv_cimportspec CWrapper = "wrapper"
255     static = case cis of
256                  CFunction (StaticTarget _) -> "static "
257                  _ -> ""
258
259 repCCallConv :: CCallConv -> DsM (Core TH.Callconv)
260 repCCallConv CCallConv = rep2 cCallName []
261 repCCallConv StdCallConv = rep2 stdCallName []
262
263 repSafety :: Safety -> DsM (Core TH.Safety)
264 repSafety PlayRisky = rep2 unsafeName []
265 repSafety (PlaySafe False) = rep2 safeName []
266 repSafety (PlaySafe True) = rep2 threadsafeName []
267
268 ds_msg = ptext SLIT("Cannot desugar this Template Haskell declaration:")
269
270 -------------------------------------------------------
271 --                      Constructors
272 -------------------------------------------------------
273
274 repC :: LConDecl Name -> DsM (Core TH.ConQ)
275 repC (L loc (ConDecl con [] (L _ []) details))
276   = do { con1 <- lookupLOcc con ;               -- See note [Binders and occurrences] 
277          repConstr con1 details }
278
279 repC (L loc con_decl)
280   = do  { dsWarn (loc, hang ds_msg 4 (ppr con_decl))
281         ; return (panic "DsMeta:repC") }
282   where
283 -- gaw 2004 FIX! Need a case for GadtDecl
284
285 repBangTy :: LBangType Name -> DsM (Core (TH.StrictTypeQ))
286 repBangTy ty= do 
287   MkC s <- rep2 str []
288   MkC t <- repLTy ty'
289   rep2 strictTypeName [s, t]
290   where 
291     (str, ty') = case ty of
292                    L _ (HsBangTy _ ty) -> (isStrictName,  ty)
293                    other               -> (notStrictName, ty)
294
295 -------------------------------------------------------
296 --                      Deriving clause
297 -------------------------------------------------------
298
299 repDerivs :: Maybe [LHsType Name] -> DsM (Core [TH.Name])
300 repDerivs Nothing = coreList nameTyConName []
301 repDerivs (Just ctxt)
302   = do { strs <- mapM rep_deriv ctxt ; 
303          coreList nameTyConName strs }
304   where
305     rep_deriv :: LHsType Name -> DsM (Core TH.Name)
306         -- Deriving clauses must have the simple H98 form
307     rep_deriv (L _ (HsPredTy (HsClassP cls []))) = lookupOcc cls
308     rep_deriv other                              = panic "rep_deriv"
309
310
311 -------------------------------------------------------
312 --   Signatures in a class decl, or a group of bindings
313 -------------------------------------------------------
314
315 rep_sigs :: [LSig Name] -> DsM [Core TH.DecQ]
316 rep_sigs sigs = do locs_cores <- rep_sigs' sigs
317                    return $ de_loc $ sort_by_loc locs_cores
318
319 rep_sigs' :: [LSig Name] -> DsM [(SrcSpan, Core TH.DecQ)]
320         -- We silently ignore ones we don't recognise
321 rep_sigs' sigs = do { sigs1 <- mapM rep_sig sigs ;
322                      return (concat sigs1) }
323
324 rep_sig :: LSig Name -> DsM [(SrcSpan, Core TH.DecQ)]
325         -- Singleton => Ok
326         -- Empty     => Too hard, signature ignored
327 rep_sig (L loc (Sig nm ty)) = rep_proto nm ty loc
328 rep_sig other               = return []
329
330 rep_proto :: Located Name -> LHsType Name -> SrcSpan -> DsM [(SrcSpan, Core TH.DecQ)]
331 rep_proto nm ty loc = do { nm1 <- lookupLOcc nm ; 
332                        ty1 <- repLTy ty ; 
333                        sig <- repProto nm1 ty1 ;
334                        return [(loc, sig)] }
335
336
337 -------------------------------------------------------
338 --                      Types
339 -------------------------------------------------------
340
341 -- gensym a list of type variables and enter them into the meta environment;
342 -- the computations passed as the second argument is executed in that extended
343 -- meta environment and gets the *new* names on Core-level as an argument
344 --
345 addTyVarBinds :: [LHsTyVarBndr Name]             -- the binders to be added
346               -> ([Core TH.Name] -> DsM (Core (TH.Q a))) -- action in the ext env
347               -> DsM (Core (TH.Q a))
348 addTyVarBinds tvs m =
349   do
350     let names = map (hsTyVarName.unLoc) tvs
351     freshNames <- mkGenSyms names
352     term       <- addBinds freshNames $ do
353                     bndrs <- mapM lookupBinder names 
354                     m bndrs
355     wrapGenSyns freshNames term
356
357 -- represent a type context
358 --
359 repLContext :: LHsContext Name -> DsM (Core TH.CxtQ)
360 repLContext (L _ ctxt) = repContext ctxt
361
362 repContext :: HsContext Name -> DsM (Core TH.CxtQ)
363 repContext ctxt = do 
364                     preds    <- mapM repLPred ctxt
365                     predList <- coreList typeQTyConName preds
366                     repCtxt predList
367
368 -- represent a type predicate
369 --
370 repLPred :: LHsPred Name -> DsM (Core TH.TypeQ)
371 repLPred (L _ p) = repPred p
372
373 repPred :: HsPred Name -> DsM (Core TH.TypeQ)
374 repPred (HsClassP cls tys) = do
375                                tcon <- repTy (HsTyVar cls)
376                                tys1 <- repLTys tys
377                                repTapps tcon tys1
378 repPred (HsIParam _ _)     = 
379   panic "DsMeta.repTy: Can't represent predicates with implicit parameters"
380
381 -- yield the representation of a list of types
382 --
383 repLTys :: [LHsType Name] -> DsM [Core TH.TypeQ]
384 repLTys tys = mapM repLTy tys
385
386 -- represent a type
387 --
388 repLTy :: LHsType Name -> DsM (Core TH.TypeQ)
389 repLTy (L _ ty) = repTy ty
390
391 repTy :: HsType Name -> DsM (Core TH.TypeQ)
392 repTy (HsForAllTy _ tvs ctxt ty)  = 
393   addTyVarBinds tvs $ \bndrs -> do
394     ctxt1  <- repLContext ctxt
395     ty1    <- repLTy ty
396     bndrs1 <- coreList nameTyConName bndrs
397     repTForall bndrs1 ctxt1 ty1
398
399 repTy (HsTyVar n)
400   | isTvOcc (nameOccName n)       = do 
401                                       tv1 <- lookupBinder n
402                                       repTvar tv1
403   | otherwise                     = do 
404                                       tc1 <- lookupOcc n
405                                       repNamedTyCon tc1
406 repTy (HsAppTy f a)               = do 
407                                       f1 <- repLTy f
408                                       a1 <- repLTy a
409                                       repTapp f1 a1
410 repTy (HsFunTy f a)               = do 
411                                       f1   <- repLTy f
412                                       a1   <- repLTy a
413                                       tcon <- repArrowTyCon
414                                       repTapps tcon [f1, a1]
415 repTy (HsListTy t)                = do
416                                       t1   <- repLTy t
417                                       tcon <- repListTyCon
418                                       repTapp tcon t1
419 repTy (HsPArrTy t)                = do
420                                       t1   <- repLTy t
421                                       tcon <- repTy (HsTyVar (tyConName parrTyCon))
422                                       repTapp tcon t1
423 repTy (HsTupleTy tc tys)          = do
424                                       tys1 <- repLTys tys 
425                                       tcon <- repTupleTyCon (length tys)
426                                       repTapps tcon tys1
427 repTy (HsOpTy ty1 n ty2)          = repLTy ((nlHsTyVar (unLoc n) `nlHsAppTy` ty1) 
428                                            `nlHsAppTy` ty2)
429 repTy (HsParTy t)                 = repLTy t
430 repTy (HsNumTy i)                 =
431   panic "DsMeta.repTy: Can't represent number types (for generics)"
432 repTy (HsPredTy pred)             = repPred pred
433 repTy (HsKindSig ty kind)         = 
434   panic "DsMeta.repTy: Can't represent explicit kind signatures yet"
435
436
437 -----------------------------------------------------------------------------
438 --              Expressions
439 -----------------------------------------------------------------------------
440
441 repLEs :: [LHsExpr Name] -> DsM (Core [TH.ExpQ])
442 repLEs es = do { es'  <- mapM repLE es ;
443                  coreList expQTyConName es' }
444
445 -- FIXME: some of these panics should be converted into proper error messages
446 --        unless we can make sure that constructs, which are plainly not
447 --        supported in TH already lead to error messages at an earlier stage
448 repLE :: LHsExpr Name -> DsM (Core TH.ExpQ)
449 repLE (L _ e) = repE e
450
451 repE :: HsExpr Name -> DsM (Core TH.ExpQ)
452 repE (HsVar x)            =
453   do { mb_val <- dsLookupMetaEnv x 
454      ; case mb_val of
455         Nothing          -> do { str <- globalVar x
456                                ; repVarOrCon x str }
457         Just (Bound y)   -> repVarOrCon x (coreVar y)
458         Just (Splice e)  -> do { e' <- dsExpr e
459                                ; return (MkC e') } }
460 repE (HsIPVar x) = panic "DsMeta.repE: Can't represent implicit parameters"
461
462         -- Remember, we're desugaring renamer output here, so
463         -- HsOverlit can definitely occur
464 repE (HsOverLit l) = do { a <- repOverloadedLiteral l; repLit a }
465 repE (HsLit l)     = do { a <- repLiteral l;           repLit a }
466 repE (HsLam (MatchGroup [m] _)) = repLambda m
467 repE (HsApp x y)   = do {a <- repLE x; b <- repLE y; repApp a b}
468
469 repE (OpApp e1 op fix e2) =
470   do { arg1 <- repLE e1; 
471        arg2 <- repLE e2; 
472        the_op <- repLE op ;
473        repInfixApp arg1 the_op arg2 } 
474 repE (NegApp x nm)        = do
475                               a         <- repLE x
476                               negateVar <- lookupOcc negateName >>= repVar
477                               negateVar `repApp` a
478 repE (HsPar x)            = repLE x
479 repE (SectionL x y)       = do { a <- repLE x; b <- repLE y; repSectionL a b } 
480 repE (SectionR x y)       = do { a <- repLE x; b <- repLE y; repSectionR a b } 
481 repE (HsCase e (MatchGroup ms _)) = do { arg <- repLE e
482                                        ; ms2 <- mapM repMatchTup ms
483                                        ; repCaseE arg (nonEmptyCoreList ms2) }
484 repE (HsIf x y z)         = do
485                               a <- repLE x
486                               b <- repLE y
487                               c <- repLE z
488                               repCond a b c
489 repE (HsLet bs e)         = do { (ss,ds) <- repBinds bs
490                                ; e2 <- addBinds ss (repLE e)
491                                ; z <- repLetE ds e2
492                                ; wrapGenSyns ss z }
493 -- FIXME: I haven't got the types here right yet
494 repE (HsDo DoExpr sts _ ty) 
495  = do { (ss,zs) <- repLSts sts; 
496         e       <- repDoE (nonEmptyCoreList zs);
497         wrapGenSyns ss e }
498 repE (HsDo ListComp sts _ ty) 
499  = do { (ss,zs) <- repLSts sts; 
500         e       <- repComp (nonEmptyCoreList zs);
501         wrapGenSyns ss e }
502 repE (HsDo _ _ _ _) = panic "DsMeta.repE: Can't represent mdo and [: :] yet"
503 repE (ExplicitList ty es) = do { xs <- repLEs es; repListExp xs } 
504 repE (ExplicitPArr ty es) = 
505   panic "DsMeta.repE: No explicit parallel arrays yet"
506 repE (ExplicitTuple es boxed) 
507   | isBoxed boxed         = do { xs <- repLEs es; repTup xs }
508   | otherwise             = panic "DsMeta.repE: Can't represent unboxed tuples"
509 repE (RecordCon c flds)
510  = do { x <- lookupLOcc c;
511         fs <- repFields flds;
512         repRecCon x fs }
513 repE (RecordUpd e flds)
514  = do { x <- repLE e;
515         fs <- repFields flds;
516         repRecUpd x fs }
517
518 repE (ExprWithTySig e ty) = do { e1 <- repLE e; t1 <- repLTy ty; repSigExp e1 t1 }
519 repE (ArithSeqIn aseq) =
520   case aseq of
521     From e              -> do { ds1 <- repLE e; repFrom ds1 }
522     FromThen e1 e2      -> do 
523                              ds1 <- repLE e1
524                              ds2 <- repLE e2
525                              repFromThen ds1 ds2
526     FromTo   e1 e2      -> do 
527                              ds1 <- repLE e1
528                              ds2 <- repLE e2
529                              repFromTo ds1 ds2
530     FromThenTo e1 e2 e3 -> do 
531                              ds1 <- repLE e1
532                              ds2 <- repLE e2
533                              ds3 <- repLE e3
534                              repFromThenTo ds1 ds2 ds3
535 repE (PArrSeqOut _ aseq)  = panic "DsMeta.repE: parallel array seq.s missing"
536 repE (HsCoreAnn _ _)      = panic "DsMeta.repE: Can't represent CoreAnn" -- hdaume: core annotations
537 repE (HsSCC _ _)          = panic "DsMeta.repE: Can't represent SCC"
538 repE (HsBracketOut _ _)   = panic "DsMeta.repE: Can't represent Oxford brackets"
539 repE (HsSpliceE (HsSplice n _)) 
540   = do { mb_val <- dsLookupMetaEnv n
541        ; case mb_val of
542                  Just (Splice e) -> do { e' <- dsExpr e
543                                        ; return (MkC e') }
544                  other       -> pprPanic "HsSplice" (ppr n) }
545
546 repE e = pprPanic "DsMeta.repE: Illegal expression form" (ppr e)
547
548 -----------------------------------------------------------------------------
549 -- Building representations of auxillary structures like Match, Clause, Stmt, 
550
551 repMatchTup ::  LMatch Name -> DsM (Core TH.MatchQ) 
552 repMatchTup (L _ (Match [p] ty (GRHSs guards wheres))) =
553   do { ss1 <- mkGenSyms (collectPatBinders p) 
554      ; addBinds ss1 $ do {
555      ; p1 <- repLP p
556      ; (ss2,ds) <- repBinds wheres
557      ; addBinds ss2 $ do {
558      ; gs    <- repGuards guards
559      ; match <- repMatch p1 gs ds
560      ; wrapGenSyns (ss1++ss2) match }}}
561
562 repClauseTup ::  LMatch Name -> DsM (Core TH.ClauseQ)
563 repClauseTup (L _ (Match ps ty (GRHSs guards wheres))) =
564   do { ss1 <- mkGenSyms (collectPatsBinders ps) 
565      ; addBinds ss1 $ do {
566        ps1 <- repLPs ps
567      ; (ss2,ds) <- repBinds wheres
568      ; addBinds ss2 $ do {
569        gs <- repGuards guards
570      ; clause <- repClause ps1 gs ds
571      ; wrapGenSyns (ss1++ss2) clause }}}
572
573 repGuards ::  [LGRHS Name] ->  DsM (Core TH.BodyQ)
574 repGuards [L _ (GRHS [L _ (ResultStmt e)])]
575   = do {a <- repLE e; repNormal a }
576 repGuards other 
577   = do { zs <- mapM process other;
578      let {(xs, ys) = unzip zs};
579          gd <- repGuarded (nonEmptyCoreList ys);
580      wrapGenSyns (concat xs) gd }
581   where 
582     process :: LGRHS Name -> DsM ([GenSymBind], (Core (TH.Q (TH.Guard, TH.Exp))))
583     process (L _ (GRHS [])) = panic "No guards in guarded body"
584     process (L _ (GRHS [L _ (ExprStmt e1 ty),
585                         L _ (ResultStmt e2)]))
586            = do { x <- repLNormalGE e1 e2;
587                   return ([], x) }
588     process (L _ (GRHS ss))
589            = do (gs, ss') <- repLSts ss
590                 g <- repPatGE (nonEmptyCoreList ss')
591                 return (gs, g)
592
593 repFields :: [(Located Name, LHsExpr Name)] -> DsM (Core [TH.Q TH.FieldExp])
594 repFields flds = do
595         fnames <- mapM lookupLOcc (map fst flds)
596         es <- mapM repLE (map snd flds)
597         fs <- zipWithM repFieldExp fnames es
598         coreList fieldExpQTyConName fs
599
600
601 -----------------------------------------------------------------------------
602 -- Representing Stmt's is tricky, especially if bound variables
603 -- shadow each other. Consider:  [| do { x <- f 1; x <- f x; g x } |]
604 -- First gensym new names for every variable in any of the patterns.
605 -- both static (x'1 and x'2), and dynamic ((gensym "x") and (gensym "y"))
606 -- if variables didn't shaddow, the static gensym wouldn't be necessary
607 -- and we could reuse the original names (x and x).
608 --
609 -- do { x'1 <- gensym "x"
610 --    ; x'2 <- gensym "x"   
611 --    ; doE [ BindSt (pvar x'1) [| f 1 |]
612 --          , BindSt (pvar x'2) [| f x |] 
613 --          , NoBindSt [| g x |] 
614 --          ]
615 --    }
616
617 -- The strategy is to translate a whole list of do-bindings by building a
618 -- bigger environment, and a bigger set of meta bindings 
619 -- (like:  x'1 <- gensym "x" ) and then combining these with the translations
620 -- of the expressions within the Do
621       
622 -----------------------------------------------------------------------------
623 -- The helper function repSts computes the translation of each sub expression
624 -- and a bunch of prefix bindings denoting the dynamic renaming.
625
626 repLSts :: [LStmt Name] -> DsM ([GenSymBind], [Core TH.StmtQ])
627 repLSts stmts = repSts (map unLoc stmts)
628
629 repSts :: [Stmt Name] -> DsM ([GenSymBind], [Core TH.StmtQ])
630 repSts [ResultStmt e] = 
631    do { a <- repLE e
632       ; e1 <- repNoBindSt a
633       ; return ([], [e1]) }
634 repSts (BindStmt p e : ss) =
635    do { e2 <- repLE e 
636       ; ss1 <- mkGenSyms (collectPatBinders p) 
637       ; addBinds ss1 $ do {
638       ; p1 <- repLP p; 
639       ; (ss2,zs) <- repSts ss
640       ; z <- repBindSt p1 e2
641       ; return (ss1++ss2, z : zs) }}
642 repSts (LetStmt bs : ss) =
643    do { (ss1,ds) <- repBinds bs
644       ; z <- repLetSt ds
645       ; (ss2,zs) <- addBinds ss1 (repSts ss)
646       ; return (ss1++ss2, z : zs) } 
647 repSts (ExprStmt e ty : ss) =       
648    do { e2 <- repLE e
649       ; z <- repNoBindSt e2 
650       ; (ss2,zs) <- repSts ss
651       ; return (ss2, z : zs) }
652 repSts [] = panic "repSts ran out of statements"      
653 repSts other = panic "Exotic Stmt in meta brackets"      
654
655
656 -----------------------------------------------------------
657 --                      Bindings
658 -----------------------------------------------------------
659
660 repBinds :: [HsBindGroup Name] -> DsM ([GenSymBind], Core [TH.DecQ]) 
661 repBinds decs
662  = do   { let { bndrs = map unLoc (collectGroupBinders decs) }
663                 -- No need to worrry about detailed scopes within
664                 -- the binding group, because we are talking Names
665                 -- here, so we can safely treat it as a mutually 
666                 -- recursive group
667         ; ss        <- mkGenSyms bndrs
668         ; core      <- addBinds ss (rep_bind_groups decs)
669         ; core_list <- coreList decQTyConName core 
670         ; return (ss, core_list) }
671
672 rep_bind_groups :: [HsBindGroup Name] -> DsM [Core TH.DecQ]
673 -- Assumes: all the binders of the binding are alrady in the meta-env
674 rep_bind_groups binds = do 
675   locs_cores_s <- mapM rep_bind_group binds
676   return $ de_loc $ sort_by_loc (concat locs_cores_s)
677
678 rep_bind_group :: HsBindGroup Name -> DsM [(SrcSpan, Core TH.DecQ)]
679 -- Assumes: all the binders of the binding are alrady in the meta-env
680 rep_bind_group (HsBindGroup bs sigs _)
681  = do { core1 <- mapM rep_bind (bagToList bs)
682       ; core2 <- rep_sigs' sigs
683       ; return (core1 ++ core2) }
684 rep_bind_group (HsIPBinds _)
685   = panic "DsMeta:repBinds: can't do implicit parameters"
686
687 rep_binds :: LHsBinds Name -> DsM [Core TH.DecQ]
688 -- Assumes: all the binders of the binding are alrady in the meta-env
689 rep_binds binds = do 
690   locs_cores <- mapM rep_bind (bagToList binds)
691   return $ de_loc $ sort_by_loc locs_cores
692
693 rep_bind :: LHsBind Name -> DsM (SrcSpan, Core TH.DecQ)
694 -- Assumes: all the binders of the binding are alrady in the meta-env
695
696 -- Note GHC treats declarations of a variable (not a pattern) 
697 -- e.g.  x = g 5 as a Fun MonoBinds. This is indicated by a single match 
698 -- with an empty list of patterns
699 rep_bind (L loc (FunBind fn infx (MatchGroup [L _ (Match [] ty (GRHSs guards wheres))] _)))
700  = do { (ss,wherecore) <- repBinds wheres
701         ; guardcore <- addBinds ss (repGuards guards)
702         ; fn'  <- lookupLBinder fn
703         ; p    <- repPvar fn'
704         ; ans  <- repVal p guardcore wherecore
705         ; ans' <- wrapGenSyns ss ans
706         ; return (loc, ans') }
707
708 rep_bind (L loc (FunBind fn infx (MatchGroup ms _)))
709  =   do { ms1 <- mapM repClauseTup ms
710         ; fn' <- lookupLBinder fn
711         ; ans <- repFun fn' (nonEmptyCoreList ms1)
712         ; return (loc, ans) }
713
714 rep_bind (L loc (PatBind pat (GRHSs guards wheres) ty2))
715  =   do { patcore <- repLP pat 
716         ; (ss,wherecore) <- repBinds wheres
717         ; guardcore <- addBinds ss (repGuards guards)
718         ; ans  <- repVal patcore guardcore wherecore
719         ; ans' <- wrapGenSyns ss ans
720         ; return (loc, ans') }
721
722 rep_bind (L loc (VarBind v e))
723  =   do { v' <- lookupBinder v 
724         ; e2 <- repLE e
725         ; x <- repNormal e2
726         ; patcore <- repPvar v'
727         ; empty_decls <- coreList decQTyConName [] 
728         ; ans <- repVal patcore x empty_decls
729         ; return (srcLocSpan (getSrcLoc v), ans) }
730
731 -----------------------------------------------------------------------------
732 -- Since everything in a Bind is mutually recursive we need rename all
733 -- all the variables simultaneously. For example: 
734 -- [| AndMonoBinds (f x = x + g 2) (g x = f 1 + 2) |] would translate to
735 -- do { f'1 <- gensym "f"
736 --    ; g'2 <- gensym "g"
737 --    ; [ do { x'3 <- gensym "x"; fun f'1 [pvar x'3] [| x + g2 |]},
738 --        do { x'4 <- gensym "x"; fun g'2 [pvar x'4] [| f 1 + 2 |]}
739 --      ]}
740 -- This requires collecting the bindings (f'1 <- gensym "f"), and the 
741 -- environment ( f |-> f'1 ) from each binding, and then unioning them 
742 -- together. As we do this we collect GenSymBinds's which represent the renamed 
743 -- variables bound by the Bindings. In order not to lose track of these 
744 -- representations we build a shadow datatype MB with the same structure as 
745 -- MonoBinds, but which has slots for the representations
746
747
748 -----------------------------------------------------------------------------
749 -- GHC allows a more general form of lambda abstraction than specified
750 -- by Haskell 98. In particular it allows guarded lambda's like : 
751 -- (\  x | even x -> 0 | odd x -> 1) at the moment we can't represent this in
752 -- Haskell Template's Meta.Exp type so we punt if it isn't a simple thing like
753 -- (\ p1 .. pn -> exp) by causing an error.  
754
755 repLambda :: LMatch Name -> DsM (Core TH.ExpQ)
756 repLambda (L _ (Match ps _ (GRHSs [L _ (GRHS [L _ (ResultStmt e)])] [])))
757  = do { let bndrs = collectPatsBinders ps ;
758       ; ss  <- mkGenSyms bndrs
759       ; lam <- addBinds ss (
760                 do { xs <- repLPs ps; body <- repLE e; repLam xs body })
761       ; wrapGenSyns ss lam }
762
763 repLambda z = panic "Can't represent a guarded lambda in Template Haskell"  
764
765   
766 -----------------------------------------------------------------------------
767 --                      Patterns
768 -- repP deals with patterns.  It assumes that we have already
769 -- walked over the pattern(s) once to collect the binders, and 
770 -- have extended the environment.  So every pattern-bound 
771 -- variable should already appear in the environment.
772
773 -- Process a list of patterns
774 repLPs :: [LPat Name] -> DsM (Core [TH.PatQ])
775 repLPs ps = do { ps' <- mapM repLP ps ;
776                  coreList patQTyConName ps' }
777
778 repLP :: LPat Name -> DsM (Core TH.PatQ)
779 repLP (L _ p) = repP p
780
781 repP :: Pat Name -> DsM (Core TH.PatQ)
782 repP (WildPat _)     = repPwild 
783 repP (LitPat l)      = do { l2 <- repLiteral l; repPlit l2 }
784 repP (VarPat x)      = do { x' <- lookupBinder x; repPvar x' }
785 repP (LazyPat p)     = do { p1 <- repLP p; repPtilde p1 }
786 repP (AsPat x p)     = do { x' <- lookupLBinder x; p1 <- repLP p; repPaspat x' p1 }
787 repP (ParPat p)      = repLP p 
788 repP (ListPat ps _)  = do { qs <- repLPs ps; repPlist qs }
789 repP (TuplePat ps _) = do { qs <- repLPs ps; repPtup qs }
790 repP (ConPatIn dc details)
791  = do { con_str <- lookupLOcc dc
792       ; case details of
793          PrefixCon ps   -> do { qs <- repLPs ps; repPcon con_str qs }
794          RecCon pairs -> do { vs <- sequence $ map lookupLOcc (map fst pairs)
795                             ; ps <- sequence $ map repLP (map snd pairs)
796                             ; fps <- zipWithM (\x y -> rep2 fieldPatName [unC x,unC y]) vs ps
797                             ; fps' <- coreList fieldPatQTyConName fps
798                             ; repPrec con_str fps' }
799          InfixCon p1 p2 -> do { p1' <- repLP p1;
800                                 p2' <- repLP p2;
801                                 repPinfix p1' con_str p2' }
802    }
803 repP (NPatIn l (Just _)) = panic "Can't cope with negative overloaded patterns yet (repP (NPatIn _ (Just _)))"
804 repP (NPatIn l Nothing) = do { a <- repOverloadedLiteral l; repPlit a }
805 repP (SigPatIn p t)  = do { p' <- repLP p; t' <- repLTy t; repPsig p' t' }
806 repP other = panic "Exotic pattern inside meta brackets"
807
808 ----------------------------------------------------------
809 -- Declaration ordering helpers
810
811 sort_by_loc :: [(SrcSpan, a)] -> [(SrcSpan, a)]
812 sort_by_loc xs = sortBy comp xs
813     where comp x y = compare (fst x) (fst y)
814
815 de_loc :: [(a, b)] -> [b]
816 de_loc = map snd
817
818 ----------------------------------------------------------
819 --      The meta-environment
820
821 -- A name/identifier association for fresh names of locally bound entities
822 type GenSymBind = (Name, Id)    -- Gensym the string and bind it to the Id
823                                 -- I.e.         (x, x_id) means
824                                 --      let x_id = gensym "x" in ...
825
826 -- Generate a fresh name for a locally bound entity
827
828 mkGenSyms :: [Name] -> DsM [GenSymBind]
829 -- We can use the existing name.  For example:
830 --      [| \x_77 -> x_77 + x_77 |]
831 -- desugars to
832 --      do { x_77 <- genSym "x"; .... }
833 -- We use the same x_77 in the desugared program, but with the type Bndr
834 -- instead of Int
835 --
836 -- We do make it an Internal name, though (hence localiseName)
837 --
838 -- Nevertheless, it's monadic because we have to generate nameTy
839 mkGenSyms ns = do { var_ty <- lookupType nameTyConName
840                   ; return [(nm, mkLocalId (localiseName nm) var_ty) | nm <- ns] }
841
842              
843 addBinds :: [GenSymBind] -> DsM a -> DsM a
844 -- Add a list of fresh names for locally bound entities to the 
845 -- meta environment (which is part of the state carried around 
846 -- by the desugarer monad) 
847 addBinds bs m = dsExtendMetaEnv (mkNameEnv [(n,Bound id) | (n,id) <- bs]) m
848
849 -- Look up a locally bound name
850 --
851 lookupLBinder :: Located Name -> DsM (Core TH.Name)
852 lookupLBinder (L _ n) = lookupBinder n
853
854 lookupBinder :: Name -> DsM (Core TH.Name)
855 lookupBinder n 
856   = do { mb_val <- dsLookupMetaEnv n;
857          case mb_val of
858             Just (Bound x) -> return (coreVar x)
859             other          -> pprPanic "Failed binder lookup:" (ppr n) }
860
861 -- Look up a name that is either locally bound or a global name
862 --
863 -- * If it is a global name, generate the "original name" representation (ie,
864 --   the <module>:<name> form) for the associated entity
865 --
866 lookupLOcc :: Located Name -> DsM (Core TH.Name)
867 -- Lookup an occurrence; it can't be a splice.
868 -- Use the in-scope bindings if they exist
869 lookupLOcc (L _ n) = lookupOcc n
870
871 lookupOcc :: Name -> DsM (Core TH.Name)
872 lookupOcc n
873   = do {  mb_val <- dsLookupMetaEnv n ;
874           case mb_val of
875                 Nothing         -> globalVar n
876                 Just (Bound x)  -> return (coreVar x)
877                 Just (Splice _) -> pprPanic "repE:lookupOcc" (ppr n) 
878     }
879
880 globalVar :: Name -> DsM (Core TH.Name)
881 -- Not bound by the meta-env
882 -- Could be top-level; or could be local
883 --      f x = $(g [| x |])
884 -- Here the x will be local
885 globalVar name
886   | isExternalName name
887   = do  { MkC mod <- coreStringLit name_mod
888         ; MkC occ <- occNameLit name
889         ; rep2 mk_varg [mod,occ] }
890   | otherwise
891   = do  { MkC occ <- occNameLit name
892         ; MkC uni <- coreIntLit (getKey (getUnique name))
893         ; rep2 mkNameUName [occ,uni] }
894   where
895       name_mod = moduleUserString (nameModule name)
896       name_occ = nameOccName name
897       mk_varg | OccName.isDataOcc name_occ = mkNameG_dName
898               | OccName.isVarOcc  name_occ = mkNameG_vName
899               | OccName.isTcOcc   name_occ = mkNameG_tcName
900               | otherwise                  = pprPanic "DsMeta.globalVar" (ppr name)
901
902 lookupType :: Name      -- Name of type constructor (e.g. TH.ExpQ)
903            -> DsM Type  -- The type
904 lookupType tc_name = do { tc <- dsLookupTyCon tc_name ;
905                           return (mkGenTyConApp tc []) }
906
907 wrapGenSyns :: [GenSymBind] 
908             -> Core (TH.Q a) -> DsM (Core (TH.Q a))
909 -- wrapGenSyns [(nm1,id1), (nm2,id2)] y 
910 --      --> bindQ (gensym nm1) (\ id1 -> 
911 --          bindQ (gensym nm2 (\ id2 -> 
912 --          y))
913
914 wrapGenSyns binds body@(MkC b)
915   = do  { var_ty <- lookupType nameTyConName
916         ; go var_ty binds }
917   where
918     [elt_ty] = tcTyConAppArgs (exprType b) 
919         -- b :: Q a, so we can get the type 'a' by looking at the
920         -- argument type. NB: this relies on Q being a data/newtype,
921         -- not a type synonym
922
923     go var_ty [] = return body
924     go var_ty ((name,id) : binds)
925       = do { MkC body'  <- go var_ty binds
926            ; lit_str    <- occNameLit name
927            ; gensym_app <- repGensym lit_str
928            ; repBindQ var_ty elt_ty 
929                       gensym_app (MkC (Lam id body')) }
930
931 -- Just like wrapGenSym, but don't actually do the gensym
932 -- Instead use the existing name:
933 --      let x = "x" in ...
934 -- Only used for [Decl], and for the class ops in class 
935 -- and instance decls
936 wrapNongenSyms :: [GenSymBind] -> Core a -> DsM (Core a)
937 wrapNongenSyms binds (MkC body)
938   = do { binds' <- mapM do_one binds ;
939          return (MkC (mkLets binds' body)) }
940   where
941     do_one (name,id) 
942         = do { MkC lit_str <- occNameLit name
943              ; MkC var <- rep2 mkNameName [lit_str]
944              ; return (NonRec id var) }
945
946 occNameLit :: Name -> DsM (Core String)
947 occNameLit n = coreStringLit (occNameUserString (nameOccName n))
948
949
950 -- %*********************************************************************
951 -- %*                                                                   *
952 --              Constructing code
953 -- %*                                                                   *
954 -- %*********************************************************************
955
956 -----------------------------------------------------------------------------
957 -- PHANTOM TYPES for consistency. In order to make sure we do this correct 
958 -- we invent a new datatype which uses phantom types.
959
960 newtype Core a = MkC CoreExpr
961 unC (MkC x) = x
962
963 rep2 :: Name -> [ CoreExpr ] -> DsM (Core a)
964 rep2 n xs = do { id <- dsLookupGlobalId n
965                ; return (MkC (foldl App (Var id) xs)) }
966
967 -- Then we make "repConstructors" which use the phantom types for each of the
968 -- smart constructors of the Meta.Meta datatypes.
969
970
971 -- %*********************************************************************
972 -- %*                                                                   *
973 --              The 'smart constructors'
974 -- %*                                                                   *
975 -- %*********************************************************************
976
977 --------------- Patterns -----------------
978 repPlit   :: Core TH.Lit -> DsM (Core TH.PatQ) 
979 repPlit (MkC l) = rep2 litPName [l]
980
981 repPvar :: Core TH.Name -> DsM (Core TH.PatQ)
982 repPvar (MkC s) = rep2 varPName [s]
983
984 repPtup :: Core [TH.PatQ] -> DsM (Core TH.PatQ)
985 repPtup (MkC ps) = rep2 tupPName [ps]
986
987 repPcon   :: Core TH.Name -> Core [TH.PatQ] -> DsM (Core TH.PatQ)
988 repPcon (MkC s) (MkC ps) = rep2 conPName [s, ps]
989
990 repPrec   :: Core TH.Name -> Core [(TH.Name,TH.PatQ)] -> DsM (Core TH.PatQ)
991 repPrec (MkC c) (MkC rps) = rep2 recPName [c,rps]
992
993 repPinfix :: Core TH.PatQ -> Core TH.Name -> Core TH.PatQ -> DsM (Core TH.PatQ)
994 repPinfix (MkC p1) (MkC n) (MkC p2) = rep2 infixPName [p1, n, p2]
995
996 repPtilde :: Core TH.PatQ -> DsM (Core TH.PatQ)
997 repPtilde (MkC p) = rep2 tildePName [p]
998
999 repPaspat :: Core TH.Name -> Core TH.PatQ -> DsM (Core TH.PatQ)
1000 repPaspat (MkC s) (MkC p) = rep2 asPName [s, p]
1001
1002 repPwild  :: DsM (Core TH.PatQ)
1003 repPwild = rep2 wildPName []
1004
1005 repPlist :: Core [TH.PatQ] -> DsM (Core TH.PatQ)
1006 repPlist (MkC ps) = rep2 listPName [ps]
1007
1008 repPsig :: Core TH.PatQ -> Core TH.TypeQ -> DsM (Core TH.PatQ)
1009 repPsig (MkC p) (MkC t) = rep2 sigPName [p, t]
1010
1011 --------------- Expressions -----------------
1012 repVarOrCon :: Name -> Core TH.Name -> DsM (Core TH.ExpQ)
1013 repVarOrCon vc str | isDataOcc (nameOccName vc) = repCon str
1014                    | otherwise                  = repVar str
1015
1016 repVar :: Core TH.Name -> DsM (Core TH.ExpQ)
1017 repVar (MkC s) = rep2 varEName [s] 
1018
1019 repCon :: Core TH.Name -> DsM (Core TH.ExpQ)
1020 repCon (MkC s) = rep2 conEName [s] 
1021
1022 repLit :: Core TH.Lit -> DsM (Core TH.ExpQ)
1023 repLit (MkC c) = rep2 litEName [c] 
1024
1025 repApp :: Core TH.ExpQ -> Core TH.ExpQ -> DsM (Core TH.ExpQ)
1026 repApp (MkC x) (MkC y) = rep2 appEName [x,y] 
1027
1028 repLam :: Core [TH.PatQ] -> Core TH.ExpQ -> DsM (Core TH.ExpQ)
1029 repLam (MkC ps) (MkC e) = rep2 lamEName [ps, e]
1030
1031 repTup :: Core [TH.ExpQ] -> DsM (Core TH.ExpQ)
1032 repTup (MkC es) = rep2 tupEName [es]
1033
1034 repCond :: Core TH.ExpQ -> Core TH.ExpQ -> Core TH.ExpQ -> DsM (Core TH.ExpQ)
1035 repCond (MkC x) (MkC y) (MkC z) =  rep2 condEName [x,y,z] 
1036
1037 repLetE :: Core [TH.DecQ] -> Core TH.ExpQ -> DsM (Core TH.ExpQ)
1038 repLetE (MkC ds) (MkC e) = rep2 letEName [ds, e] 
1039
1040 repCaseE :: Core TH.ExpQ -> Core [TH.MatchQ] -> DsM( Core TH.ExpQ)
1041 repCaseE (MkC e) (MkC ms) = rep2 caseEName [e, ms]
1042
1043 repDoE :: Core [TH.StmtQ] -> DsM (Core TH.ExpQ)
1044 repDoE (MkC ss) = rep2 doEName [ss]
1045
1046 repComp :: Core [TH.StmtQ] -> DsM (Core TH.ExpQ)
1047 repComp (MkC ss) = rep2 compEName [ss]
1048
1049 repListExp :: Core [TH.ExpQ] -> DsM (Core TH.ExpQ)
1050 repListExp (MkC es) = rep2 listEName [es]
1051
1052 repSigExp :: Core TH.ExpQ -> Core TH.TypeQ -> DsM (Core TH.ExpQ)
1053 repSigExp (MkC e) (MkC t) = rep2 sigEName [e,t]
1054
1055 repRecCon :: Core TH.Name -> Core [TH.Q TH.FieldExp]-> DsM (Core TH.ExpQ)
1056 repRecCon (MkC c) (MkC fs) = rep2 recConEName [c,fs]
1057
1058 repRecUpd :: Core TH.ExpQ -> Core [TH.Q TH.FieldExp] -> DsM (Core TH.ExpQ)
1059 repRecUpd (MkC e) (MkC fs) = rep2 recUpdEName [e,fs]
1060
1061 repFieldExp :: Core TH.Name -> Core TH.ExpQ -> DsM (Core (TH.Q TH.FieldExp))
1062 repFieldExp (MkC n) (MkC x) = rep2 fieldExpName [n,x]
1063
1064 repInfixApp :: Core TH.ExpQ -> Core TH.ExpQ -> Core TH.ExpQ -> DsM (Core TH.ExpQ)
1065 repInfixApp (MkC x) (MkC y) (MkC z) = rep2 infixAppName [x,y,z]
1066
1067 repSectionL :: Core TH.ExpQ -> Core TH.ExpQ -> DsM (Core TH.ExpQ)
1068 repSectionL (MkC x) (MkC y) = rep2 sectionLName [x,y]
1069
1070 repSectionR :: Core TH.ExpQ -> Core TH.ExpQ -> DsM (Core TH.ExpQ)
1071 repSectionR (MkC x) (MkC y) = rep2 sectionRName [x,y]
1072
1073 ------------ Right hand sides (guarded expressions) ----
1074 repGuarded :: Core [TH.Q (TH.Guard, TH.Exp)] -> DsM (Core TH.BodyQ)
1075 repGuarded (MkC pairs) = rep2 guardedBName [pairs]
1076
1077 repNormal :: Core TH.ExpQ -> DsM (Core TH.BodyQ)
1078 repNormal (MkC e) = rep2 normalBName [e]
1079
1080 ------------ Guards ----
1081 repLNormalGE :: LHsExpr Name -> LHsExpr Name -> DsM (Core (TH.Q (TH.Guard, TH.Exp)))
1082 repLNormalGE g e = do g' <- repLE g
1083                       e' <- repLE e
1084                       repNormalGE g' e'
1085
1086 repNormalGE :: Core TH.ExpQ -> Core TH.ExpQ -> DsM (Core (TH.Q (TH.Guard, TH.Exp)))
1087 repNormalGE (MkC g) (MkC e) = rep2 normalGEName [g, e]
1088
1089 repPatGE :: Core [TH.StmtQ] -> DsM (Core (TH.Q (TH.Guard, TH.Exp)))
1090 repPatGE (MkC ss) = rep2 patGEName [ss]
1091
1092 ------------- Stmts -------------------
1093 repBindSt :: Core TH.PatQ -> Core TH.ExpQ -> DsM (Core TH.StmtQ)
1094 repBindSt (MkC p) (MkC e) = rep2 bindSName [p,e]
1095
1096 repLetSt :: Core [TH.DecQ] -> DsM (Core TH.StmtQ)
1097 repLetSt (MkC ds) = rep2 letSName [ds]
1098
1099 repNoBindSt :: Core TH.ExpQ -> DsM (Core TH.StmtQ)
1100 repNoBindSt (MkC e) = rep2 noBindSName [e]
1101
1102 -------------- Range (Arithmetic sequences) -----------
1103 repFrom :: Core TH.ExpQ -> DsM (Core TH.ExpQ)
1104 repFrom (MkC x) = rep2 fromEName [x]
1105
1106 repFromThen :: Core TH.ExpQ -> Core TH.ExpQ -> DsM (Core TH.ExpQ)
1107 repFromThen (MkC x) (MkC y) = rep2 fromThenEName [x,y]
1108
1109 repFromTo :: Core TH.ExpQ -> Core TH.ExpQ -> DsM (Core TH.ExpQ)
1110 repFromTo (MkC x) (MkC y) = rep2 fromToEName [x,y]
1111
1112 repFromThenTo :: Core TH.ExpQ -> Core TH.ExpQ -> Core TH.ExpQ -> DsM (Core TH.ExpQ)
1113 repFromThenTo (MkC x) (MkC y) (MkC z) = rep2 fromThenToEName [x,y,z]
1114
1115 ------------ Match and Clause Tuples -----------
1116 repMatch :: Core TH.PatQ -> Core TH.BodyQ -> Core [TH.DecQ] -> DsM (Core TH.MatchQ)
1117 repMatch (MkC p) (MkC bod) (MkC ds) = rep2 matchName [p, bod, ds]
1118
1119 repClause :: Core [TH.PatQ] -> Core TH.BodyQ -> Core [TH.DecQ] -> DsM (Core TH.ClauseQ)
1120 repClause (MkC ps) (MkC bod) (MkC ds) = rep2 clauseName [ps, bod, ds]
1121
1122 -------------- Dec -----------------------------
1123 repVal :: Core TH.PatQ -> Core TH.BodyQ -> Core [TH.DecQ] -> DsM (Core TH.DecQ)
1124 repVal (MkC p) (MkC b) (MkC ds) = rep2 valDName [p, b, ds]
1125
1126 repFun :: Core TH.Name -> Core [TH.ClauseQ] -> DsM (Core TH.DecQ)  
1127 repFun (MkC nm) (MkC b) = rep2 funDName [nm, b]
1128
1129 repData :: Core TH.CxtQ -> Core TH.Name -> Core [TH.Name] -> Core [TH.ConQ] -> Core [TH.Name] -> DsM (Core TH.DecQ)
1130 repData (MkC cxt) (MkC nm) (MkC tvs) (MkC cons) (MkC derivs)
1131     = rep2 dataDName [cxt, nm, tvs, cons, derivs]
1132
1133 repNewtype :: Core TH.CxtQ -> Core TH.Name -> Core [TH.Name] -> Core TH.ConQ -> Core [TH.Name] -> DsM (Core TH.DecQ)
1134 repNewtype (MkC cxt) (MkC nm) (MkC tvs) (MkC con) (MkC derivs)
1135     = rep2 newtypeDName [cxt, nm, tvs, con, derivs]
1136
1137 repTySyn :: Core TH.Name -> Core [TH.Name] -> Core TH.TypeQ -> DsM (Core TH.DecQ)
1138 repTySyn (MkC nm) (MkC tvs) (MkC rhs) = rep2 tySynDName [nm, tvs, rhs]
1139
1140 repInst :: Core TH.CxtQ -> Core TH.TypeQ -> Core [TH.DecQ] -> DsM (Core TH.DecQ)
1141 repInst (MkC cxt) (MkC ty) (MkC ds) = rep2 instanceDName [cxt, ty, ds]
1142
1143 repClass :: Core TH.CxtQ -> Core TH.Name -> Core [TH.Name] -> Core [TH.DecQ] -> DsM (Core TH.DecQ)
1144 repClass (MkC cxt) (MkC cls) (MkC tvs) (MkC ds) = rep2 classDName [cxt, cls, tvs, ds]
1145
1146 repProto :: Core TH.Name -> Core TH.TypeQ -> DsM (Core TH.DecQ)
1147 repProto (MkC s) (MkC ty) = rep2 sigDName [s, ty]
1148
1149 repCtxt :: Core [TH.TypeQ] -> DsM (Core TH.CxtQ)
1150 repCtxt (MkC tys) = rep2 cxtName [tys]
1151
1152 repConstr :: Core TH.Name -> HsConDetails Name (LBangType Name)
1153           -> DsM (Core TH.ConQ)
1154 repConstr con (PrefixCon ps)
1155     = do arg_tys  <- mapM repBangTy ps
1156          arg_tys1 <- coreList strictTypeQTyConName arg_tys
1157          rep2 normalCName [unC con, unC arg_tys1]
1158 repConstr con (RecCon ips)
1159     = do arg_vs   <- mapM lookupLOcc (map fst ips)
1160          arg_tys  <- mapM repBangTy (map snd ips)
1161          arg_vtys <- zipWithM (\x y -> rep2 varStrictTypeName [unC x, unC y])
1162                               arg_vs arg_tys
1163          arg_vtys' <- coreList varStrictTypeQTyConName arg_vtys
1164          rep2 recCName [unC con, unC arg_vtys']
1165 repConstr con (InfixCon st1 st2)
1166     = do arg1 <- repBangTy st1
1167          arg2 <- repBangTy st2
1168          rep2 infixCName [unC arg1, unC con, unC arg2]
1169
1170 ------------ Types -------------------
1171
1172 repTForall :: Core [TH.Name] -> Core TH.CxtQ -> Core TH.TypeQ -> DsM (Core TH.TypeQ)
1173 repTForall (MkC tvars) (MkC ctxt) (MkC ty)
1174     = rep2 forallTName [tvars, ctxt, ty]
1175
1176 repTvar :: Core TH.Name -> DsM (Core TH.TypeQ)
1177 repTvar (MkC s) = rep2 varTName [s]
1178
1179 repTapp :: Core TH.TypeQ -> Core TH.TypeQ -> DsM (Core TH.TypeQ)
1180 repTapp (MkC t1) (MkC t2) = rep2 appTName [t1,t2]
1181
1182 repTapps :: Core TH.TypeQ -> [Core TH.TypeQ] -> DsM (Core TH.TypeQ)
1183 repTapps f []     = return f
1184 repTapps f (t:ts) = do { f1 <- repTapp f t; repTapps f1 ts }
1185
1186 --------- Type constructors --------------
1187
1188 repNamedTyCon :: Core TH.Name -> DsM (Core TH.TypeQ)
1189 repNamedTyCon (MkC s) = rep2 conTName [s]
1190
1191 repTupleTyCon :: Int -> DsM (Core TH.TypeQ)
1192 -- Note: not Core Int; it's easier to be direct here
1193 repTupleTyCon i = rep2 tupleTName [mkIntExpr (fromIntegral i)]
1194
1195 repArrowTyCon :: DsM (Core TH.TypeQ)
1196 repArrowTyCon = rep2 arrowTName []
1197
1198 repListTyCon :: DsM (Core TH.TypeQ)
1199 repListTyCon = rep2 listTName []
1200
1201
1202 ----------------------------------------------------------
1203 --              Literals
1204
1205 repLiteral :: HsLit -> DsM (Core TH.Lit)
1206 repLiteral lit 
1207   = do lit' <- case lit of
1208                    HsIntPrim i    -> mk_integer i
1209                    HsInt i        -> mk_integer i
1210                    HsFloatPrim r  -> mk_rational r
1211                    HsDoublePrim r -> mk_rational r
1212                    _ -> return lit
1213        lit_expr <- dsLit lit'
1214        rep2 lit_name [lit_expr]
1215   where
1216     lit_name = case lit of
1217                  HsInteger _ _  -> integerLName
1218                  HsInt     _    -> integerLName
1219                  HsIntPrim _    -> intPrimLName
1220                  HsFloatPrim _  -> floatPrimLName
1221                  HsDoublePrim _ -> doublePrimLName
1222                  HsChar _       -> charLName
1223                  HsString _     -> stringLName
1224                  HsRat _ _      -> rationalLName
1225                  other          -> uh_oh
1226     uh_oh = pprPanic "DsMeta.repLiteral: trying to represent exotic literal"
1227                     (ppr lit)
1228
1229 mk_integer  i = do integer_ty <- lookupType integerTyConName
1230                    return $ HsInteger i integer_ty
1231 mk_rational r = do rat_ty <- lookupType rationalTyConName
1232                    return $ HsRat r rat_ty
1233
1234 repOverloadedLiteral :: HsOverLit -> DsM (Core TH.Lit)
1235 repOverloadedLiteral (HsIntegral i _)   = do { lit <- mk_integer  i; repLiteral lit }
1236 repOverloadedLiteral (HsFractional f _) = do { lit <- mk_rational f; repLiteral lit }
1237         -- The type Rational will be in the environment, becuase 
1238         -- the smart constructor 'TH.Syntax.rationalL' uses it in its type,
1239         -- and rationalL is sucked in when any TH stuff is used
1240               
1241 --------------- Miscellaneous -------------------
1242
1243 repGensym :: Core String -> DsM (Core (TH.Q TH.Name))
1244 repGensym (MkC lit_str) = rep2 newNameName [lit_str]
1245
1246 repBindQ :: Type -> Type        -- a and b
1247          -> Core (TH.Q a) -> Core (a -> TH.Q b) -> DsM (Core (TH.Q b))
1248 repBindQ ty_a ty_b (MkC x) (MkC y) 
1249   = rep2 bindQName [Type ty_a, Type ty_b, x, y] 
1250
1251 repSequenceQ :: Type -> Core [TH.Q a] -> DsM (Core (TH.Q [a]))
1252 repSequenceQ ty_a (MkC list)
1253   = rep2 sequenceQName [Type ty_a, list]
1254
1255 ------------ Lists and Tuples -------------------
1256 -- turn a list of patterns into a single pattern matching a list
1257
1258 coreList :: Name        -- Of the TyCon of the element type
1259          -> [Core a] -> DsM (Core [a])
1260 coreList tc_name es 
1261   = do { elt_ty <- lookupType tc_name; return (coreList' elt_ty es) }
1262
1263 coreList' :: Type       -- The element type
1264           -> [Core a] -> Core [a]
1265 coreList' elt_ty es = MkC (mkListExpr elt_ty (map unC es ))
1266
1267 nonEmptyCoreList :: [Core a] -> Core [a]
1268   -- The list must be non-empty so we can get the element type
1269   -- Otherwise use coreList
1270 nonEmptyCoreList []           = panic "coreList: empty argument"
1271 nonEmptyCoreList xs@(MkC x:_) = MkC (mkListExpr (exprType x) (map unC xs))
1272
1273 corePair :: (Core a, Core b) -> Core (a,b)
1274 corePair (MkC x, MkC y) = MkC (mkCoreTup [x,y])
1275
1276 coreStringLit :: String -> DsM (Core String)
1277 coreStringLit s = do { z <- mkStringExpr s; return(MkC z) }
1278
1279 coreIntLit :: Int -> DsM (Core Int)
1280 coreIntLit i = return (MkC (mkIntExpr (fromIntegral i)))
1281
1282 coreVar :: Id -> Core TH.Name   -- The Id has type Name
1283 coreVar id = MkC (Var id)
1284
1285
1286
1287 -- %************************************************************************
1288 -- %*                                                                   *
1289 --              The known-key names for Template Haskell
1290 -- %*                                                                   *
1291 -- %************************************************************************
1292
1293 -- To add a name, do three things
1294 -- 
1295 --  1) Allocate a key
1296 --  2) Make a "Name"
1297 --  3) Add the name to knownKeyNames
1298
1299 templateHaskellNames :: [Name]
1300 -- The names that are implicitly mentioned by ``bracket''
1301 -- Should stay in sync with the import list of DsMeta
1302
1303 templateHaskellNames = [
1304     returnQName, bindQName, sequenceQName, newNameName, liftName,
1305     mkNameName, mkNameG_vName, mkNameG_dName, mkNameG_tcName, mkNameUName, 
1306
1307     -- Lit
1308     charLName, stringLName, integerLName, intPrimLName,
1309     floatPrimLName, doublePrimLName, rationalLName,
1310     -- Pat
1311     litPName, varPName, tupPName, conPName, tildePName, infixPName,
1312     asPName, wildPName, recPName, listPName, sigPName,
1313     -- FieldPat
1314     fieldPatName,
1315     -- Match
1316     matchName,
1317     -- Clause
1318     clauseName,
1319     -- Exp
1320     varEName, conEName, litEName, appEName, infixEName,
1321     infixAppName, sectionLName, sectionRName, lamEName, tupEName,
1322     condEName, letEName, caseEName, doEName, compEName,
1323     fromEName, fromThenEName, fromToEName, fromThenToEName,
1324     listEName, sigEName, recConEName, recUpdEName,
1325     -- FieldExp
1326     fieldExpName,
1327     -- Body
1328     guardedBName, normalBName,
1329     -- Guard
1330     normalGEName, patGEName,
1331     -- Stmt
1332     bindSName, letSName, noBindSName, parSName,
1333     -- Dec
1334     funDName, valDName, dataDName, newtypeDName, tySynDName,
1335     classDName, instanceDName, sigDName, forImpDName,
1336     -- Cxt
1337     cxtName,
1338     -- Strict
1339     isStrictName, notStrictName,
1340     -- Con
1341     normalCName, recCName, infixCName,
1342     -- StrictType
1343     strictTypeName,
1344     -- VarStrictType
1345     varStrictTypeName,
1346     -- Type
1347     forallTName, varTName, conTName, appTName,
1348     tupleTName, arrowTName, listTName,
1349     -- Callconv
1350     cCallName, stdCallName,
1351     -- Safety
1352     unsafeName,
1353     safeName,
1354     threadsafeName,
1355
1356     -- And the tycons
1357     qTyConName, nameTyConName, patTyConName, fieldPatTyConName, matchQTyConName,
1358     clauseQTyConName, expQTyConName, fieldExpTyConName, stmtQTyConName,
1359     decQTyConName, conQTyConName, strictTypeQTyConName,
1360     varStrictTypeQTyConName, typeQTyConName, expTyConName, decTyConName,
1361     typeTyConName, matchTyConName, clauseTyConName, patQTyConName,
1362     fieldPatQTyConName, fieldExpQTyConName]
1363
1364 tH_SYN_Name = mkModuleName "Language.Haskell.TH.Syntax"
1365 tH_LIB_Name = mkModuleName "Language.Haskell.TH.Lib"
1366
1367 thSyn :: Module
1368 -- NB: the TH.Syntax module comes from the "template-haskell" package
1369 thSyn = mkModule thPackage  tH_SYN_Name
1370 thLib = mkModule thPackage  tH_LIB_Name
1371
1372 mk_known_key_name mod space str uniq 
1373   = mkExternalName uniq mod (mkOccFS space str) 
1374                    Nothing noSrcLoc
1375
1376 libFun = mk_known_key_name thLib OccName.varName
1377 libTc  = mk_known_key_name thLib OccName.tcName
1378 thFun  = mk_known_key_name thSyn OccName.varName
1379 thTc   = mk_known_key_name thSyn OccName.tcName
1380
1381 -------------------- TH.Syntax -----------------------
1382 qTyConName        = thTc FSLIT("Q")             qTyConKey
1383 nameTyConName      = thTc FSLIT("Name")           nameTyConKey
1384 fieldExpTyConName = thTc FSLIT("FieldExp")      fieldExpTyConKey
1385 patTyConName      = thTc FSLIT("Pat")           patTyConKey
1386 fieldPatTyConName = thTc FSLIT("FieldPat")      fieldPatTyConKey
1387 expTyConName      = thTc  FSLIT("Exp")          expTyConKey
1388 decTyConName      = thTc  FSLIT("Dec")          decTyConKey
1389 typeTyConName     = thTc  FSLIT("Type")         typeTyConKey
1390 matchTyConName    = thTc  FSLIT("Match")        matchTyConKey
1391 clauseTyConName   = thTc  FSLIT("Clause")       clauseTyConKey
1392
1393 returnQName   = thFun FSLIT("returnQ")   returnQIdKey
1394 bindQName     = thFun FSLIT("bindQ")     bindQIdKey
1395 sequenceQName = thFun FSLIT("sequenceQ") sequenceQIdKey
1396 newNameName    = thFun FSLIT("newName")   newNameIdKey
1397 liftName      = thFun FSLIT("lift")      liftIdKey
1398 mkNameName     = thFun FSLIT("mkName")     mkNameIdKey
1399 mkNameG_vName  = thFun FSLIT("mkNameG_v")  mkNameG_vIdKey
1400 mkNameG_dName  = thFun FSLIT("mkNameG_d")  mkNameG_dIdKey
1401 mkNameG_tcName = thFun FSLIT("mkNameG_tc") mkNameG_tcIdKey
1402 mkNameUName    = thFun FSLIT("mkNameU")    mkNameUIdKey
1403
1404
1405 -------------------- TH.Lib -----------------------
1406 -- data Lit = ...
1407 charLName       = libFun FSLIT("charL")       charLIdKey
1408 stringLName     = libFun FSLIT("stringL")     stringLIdKey
1409 integerLName    = libFun FSLIT("integerL")    integerLIdKey
1410 intPrimLName    = libFun FSLIT("intPrimL")    intPrimLIdKey
1411 floatPrimLName  = libFun FSLIT("floatPrimL")  floatPrimLIdKey
1412 doublePrimLName = libFun FSLIT("doublePrimL") doublePrimLIdKey
1413 rationalLName   = libFun FSLIT("rationalL")     rationalLIdKey
1414
1415 -- data Pat = ...
1416 litPName   = libFun FSLIT("litP")   litPIdKey
1417 varPName   = libFun FSLIT("varP")   varPIdKey
1418 tupPName   = libFun FSLIT("tupP")   tupPIdKey
1419 conPName   = libFun FSLIT("conP")   conPIdKey
1420 infixPName = libFun FSLIT("infixP") infixPIdKey
1421 tildePName = libFun FSLIT("tildeP") tildePIdKey
1422 asPName    = libFun FSLIT("asP")    asPIdKey
1423 wildPName  = libFun FSLIT("wildP")  wildPIdKey
1424 recPName   = libFun FSLIT("recP")   recPIdKey
1425 listPName  = libFun FSLIT("listP")  listPIdKey
1426 sigPName   = libFun FSLIT("sigP")   sigPIdKey
1427
1428 -- type FieldPat = ...
1429 fieldPatName = libFun FSLIT("fieldPat") fieldPatIdKey
1430
1431 -- data Match = ...
1432 matchName = libFun FSLIT("match") matchIdKey
1433
1434 -- data Clause = ...     
1435 clauseName = libFun FSLIT("clause") clauseIdKey
1436
1437 -- data Exp = ...
1438 varEName        = libFun FSLIT("varE")        varEIdKey
1439 conEName        = libFun FSLIT("conE")        conEIdKey
1440 litEName        = libFun FSLIT("litE")        litEIdKey
1441 appEName        = libFun FSLIT("appE")        appEIdKey
1442 infixEName      = libFun FSLIT("infixE")      infixEIdKey
1443 infixAppName    = libFun FSLIT("infixApp")    infixAppIdKey
1444 sectionLName    = libFun FSLIT("sectionL")    sectionLIdKey
1445 sectionRName    = libFun FSLIT("sectionR")    sectionRIdKey
1446 lamEName        = libFun FSLIT("lamE")        lamEIdKey
1447 tupEName        = libFun FSLIT("tupE")        tupEIdKey
1448 condEName       = libFun FSLIT("condE")       condEIdKey
1449 letEName        = libFun FSLIT("letE")        letEIdKey
1450 caseEName       = libFun FSLIT("caseE")       caseEIdKey
1451 doEName         = libFun FSLIT("doE")         doEIdKey
1452 compEName       = libFun FSLIT("compE")       compEIdKey
1453 -- ArithSeq skips a level
1454 fromEName       = libFun FSLIT("fromE")       fromEIdKey
1455 fromThenEName   = libFun FSLIT("fromThenE")   fromThenEIdKey
1456 fromToEName     = libFun FSLIT("fromToE")     fromToEIdKey
1457 fromThenToEName = libFun FSLIT("fromThenToE") fromThenToEIdKey
1458 -- end ArithSeq
1459 listEName       = libFun FSLIT("listE")       listEIdKey
1460 sigEName        = libFun FSLIT("sigE")        sigEIdKey
1461 recConEName     = libFun FSLIT("recConE")     recConEIdKey
1462 recUpdEName     = libFun FSLIT("recUpdE")     recUpdEIdKey
1463
1464 -- type FieldExp = ...
1465 fieldExpName = libFun FSLIT("fieldExp") fieldExpIdKey
1466
1467 -- data Body = ...
1468 guardedBName = libFun FSLIT("guardedB") guardedBIdKey
1469 normalBName  = libFun FSLIT("normalB")  normalBIdKey
1470
1471 -- data Guard = ...
1472 normalGEName = libFun FSLIT("normalGE") normalGEIdKey
1473 patGEName    = libFun FSLIT("patGE")    patGEIdKey
1474
1475 -- data Stmt = ...
1476 bindSName   = libFun FSLIT("bindS")   bindSIdKey
1477 letSName    = libFun FSLIT("letS")    letSIdKey
1478 noBindSName = libFun FSLIT("noBindS") noBindSIdKey
1479 parSName    = libFun FSLIT("parS")    parSIdKey
1480
1481 -- data Dec = ...
1482 funDName      = libFun FSLIT("funD")      funDIdKey
1483 valDName      = libFun FSLIT("valD")      valDIdKey
1484 dataDName     = libFun FSLIT("dataD")     dataDIdKey
1485 newtypeDName  = libFun FSLIT("newtypeD")  newtypeDIdKey
1486 tySynDName    = libFun FSLIT("tySynD")    tySynDIdKey
1487 classDName    = libFun FSLIT("classD")    classDIdKey
1488 instanceDName = libFun FSLIT("instanceD") instanceDIdKey
1489 sigDName      = libFun FSLIT("sigD")      sigDIdKey
1490 forImpDName   = libFun FSLIT("forImpD")   forImpDIdKey
1491
1492 -- type Ctxt = ...
1493 cxtName = libFun FSLIT("cxt") cxtIdKey
1494
1495 -- data Strict = ...
1496 isStrictName      = libFun  FSLIT("isStrict")      isStrictKey
1497 notStrictName     = libFun  FSLIT("notStrict")     notStrictKey
1498
1499 -- data Con = ...        
1500 normalCName = libFun FSLIT("normalC") normalCIdKey
1501 recCName    = libFun FSLIT("recC")    recCIdKey
1502 infixCName  = libFun FSLIT("infixC")  infixCIdKey
1503                          
1504 -- type StrictType = ...
1505 strictTypeName    = libFun  FSLIT("strictType")    strictTKey
1506
1507 -- type VarStrictType = ...
1508 varStrictTypeName = libFun  FSLIT("varStrictType") varStrictTKey
1509
1510 -- data Type = ...
1511 forallTName = libFun FSLIT("forallT") forallTIdKey
1512 varTName    = libFun FSLIT("varT")    varTIdKey
1513 conTName    = libFun FSLIT("conT")    conTIdKey
1514 tupleTName  = libFun FSLIT("tupleT") tupleTIdKey
1515 arrowTName  = libFun FSLIT("arrowT") arrowTIdKey
1516 listTName   = libFun FSLIT("listT")  listTIdKey
1517 appTName    = libFun FSLIT("appT")    appTIdKey
1518                          
1519 -- data Callconv = ...
1520 cCallName = libFun FSLIT("cCall") cCallIdKey
1521 stdCallName = libFun FSLIT("stdCall") stdCallIdKey
1522
1523 -- data Safety = ...
1524 unsafeName     = libFun FSLIT("unsafe") unsafeIdKey
1525 safeName       = libFun FSLIT("safe") safeIdKey
1526 threadsafeName = libFun FSLIT("threadsafe") threadsafeIdKey
1527              
1528 matchQTyConName         = libTc FSLIT("MatchQ")        matchQTyConKey
1529 clauseQTyConName        = libTc FSLIT("ClauseQ")       clauseQTyConKey
1530 expQTyConName           = libTc FSLIT("ExpQ")          expQTyConKey
1531 stmtQTyConName          = libTc FSLIT("StmtQ")         stmtQTyConKey
1532 decQTyConName           = libTc FSLIT("DecQ")          decQTyConKey
1533 conQTyConName           = libTc FSLIT("ConQ")          conQTyConKey
1534 strictTypeQTyConName    = libTc FSLIT("StrictTypeQ")    strictTypeQTyConKey
1535 varStrictTypeQTyConName = libTc FSLIT("VarStrictTypeQ") varStrictTypeQTyConKey
1536 typeQTyConName          = libTc FSLIT("TypeQ")          typeQTyConKey
1537 fieldExpQTyConName      = libTc FSLIT("FieldExpQ")      fieldExpQTyConKey
1538 patQTyConName           = libTc FSLIT("PatQ")           patQTyConKey
1539 fieldPatQTyConName      = libTc FSLIT("FieldPatQ")      fieldPatQTyConKey
1540
1541 --      TyConUniques available: 100-129
1542 --      Check in PrelNames if you want to change this
1543
1544 expTyConKey             = mkPreludeTyConUnique 100
1545 matchTyConKey           = mkPreludeTyConUnique 101
1546 clauseTyConKey          = mkPreludeTyConUnique 102
1547 qTyConKey               = mkPreludeTyConUnique 103
1548 expQTyConKey            = mkPreludeTyConUnique 104
1549 decQTyConKey            = mkPreludeTyConUnique 105
1550 patTyConKey             = mkPreludeTyConUnique 106
1551 matchQTyConKey          = mkPreludeTyConUnique 107
1552 clauseQTyConKey         = mkPreludeTyConUnique 108
1553 stmtQTyConKey           = mkPreludeTyConUnique 109
1554 conQTyConKey            = mkPreludeTyConUnique 110
1555 typeQTyConKey           = mkPreludeTyConUnique 111
1556 typeTyConKey            = mkPreludeTyConUnique 112
1557 decTyConKey             = mkPreludeTyConUnique 113
1558 varStrictTypeQTyConKey  = mkPreludeTyConUnique 114
1559 strictTypeQTyConKey     = mkPreludeTyConUnique 115
1560 fieldExpTyConKey        = mkPreludeTyConUnique 116
1561 fieldPatTyConKey        = mkPreludeTyConUnique 117
1562 nameTyConKey            = mkPreludeTyConUnique 118
1563 patQTyConKey            = mkPreludeTyConUnique 119
1564 fieldPatQTyConKey       = mkPreludeTyConUnique 120
1565 fieldExpQTyConKey       = mkPreludeTyConUnique 121
1566
1567 --      IdUniques available: 200-399
1568 --      If you want to change this, make sure you check in PrelNames
1569
1570 returnQIdKey        = mkPreludeMiscIdUnique 200
1571 bindQIdKey          = mkPreludeMiscIdUnique 201
1572 sequenceQIdKey      = mkPreludeMiscIdUnique 202
1573 liftIdKey           = mkPreludeMiscIdUnique 203
1574 newNameIdKey         = mkPreludeMiscIdUnique 204
1575 mkNameIdKey          = mkPreludeMiscIdUnique 205
1576 mkNameG_vIdKey       = mkPreludeMiscIdUnique 206
1577 mkNameG_dIdKey       = mkPreludeMiscIdUnique 207
1578 mkNameG_tcIdKey      = mkPreludeMiscIdUnique 208
1579 mkNameUIdKey         = mkPreludeMiscIdUnique 209
1580
1581
1582 -- data Lit = ...
1583 charLIdKey        = mkPreludeMiscIdUnique 210
1584 stringLIdKey      = mkPreludeMiscIdUnique 211
1585 integerLIdKey     = mkPreludeMiscIdUnique 212
1586 intPrimLIdKey     = mkPreludeMiscIdUnique 213
1587 floatPrimLIdKey   = mkPreludeMiscIdUnique 214
1588 doublePrimLIdKey  = mkPreludeMiscIdUnique 215
1589 rationalLIdKey    = mkPreludeMiscIdUnique 216
1590
1591 -- data Pat = ...
1592 litPIdKey         = mkPreludeMiscIdUnique 220
1593 varPIdKey         = mkPreludeMiscIdUnique 221
1594 tupPIdKey         = mkPreludeMiscIdUnique 222
1595 conPIdKey         = mkPreludeMiscIdUnique 223
1596 infixPIdKey       = mkPreludeMiscIdUnique 312
1597 tildePIdKey       = mkPreludeMiscIdUnique 224
1598 asPIdKey          = mkPreludeMiscIdUnique 225
1599 wildPIdKey        = mkPreludeMiscIdUnique 226
1600 recPIdKey         = mkPreludeMiscIdUnique 227
1601 listPIdKey        = mkPreludeMiscIdUnique 228
1602 sigPIdKey         = mkPreludeMiscIdUnique 229
1603
1604 -- type FieldPat = ...
1605 fieldPatIdKey       = mkPreludeMiscIdUnique 230
1606
1607 -- data Match = ...
1608 matchIdKey          = mkPreludeMiscIdUnique 231
1609
1610 -- data Clause = ...
1611 clauseIdKey         = mkPreludeMiscIdUnique 232
1612
1613 -- data Exp = ...
1614 varEIdKey         = mkPreludeMiscIdUnique 240
1615 conEIdKey         = mkPreludeMiscIdUnique 241
1616 litEIdKey         = mkPreludeMiscIdUnique 242
1617 appEIdKey         = mkPreludeMiscIdUnique 243
1618 infixEIdKey       = mkPreludeMiscIdUnique 244
1619 infixAppIdKey       = mkPreludeMiscIdUnique 245
1620 sectionLIdKey       = mkPreludeMiscIdUnique 246
1621 sectionRIdKey       = mkPreludeMiscIdUnique 247
1622 lamEIdKey         = mkPreludeMiscIdUnique 248
1623 tupEIdKey         = mkPreludeMiscIdUnique 249
1624 condEIdKey        = mkPreludeMiscIdUnique 250
1625 letEIdKey         = mkPreludeMiscIdUnique 251
1626 caseEIdKey        = mkPreludeMiscIdUnique 252
1627 doEIdKey          = mkPreludeMiscIdUnique 253
1628 compEIdKey        = mkPreludeMiscIdUnique 254
1629 fromEIdKey        = mkPreludeMiscIdUnique 255
1630 fromThenEIdKey    = mkPreludeMiscIdUnique 256
1631 fromToEIdKey      = mkPreludeMiscIdUnique 257
1632 fromThenToEIdKey  = mkPreludeMiscIdUnique 258
1633 listEIdKey        = mkPreludeMiscIdUnique 259
1634 sigEIdKey         = mkPreludeMiscIdUnique 260
1635 recConEIdKey      = mkPreludeMiscIdUnique 261
1636 recUpdEIdKey      = mkPreludeMiscIdUnique 262
1637
1638 -- type FieldExp = ...
1639 fieldExpIdKey       = mkPreludeMiscIdUnique 265
1640
1641 -- data Body = ...
1642 guardedBIdKey     = mkPreludeMiscIdUnique 266
1643 normalBIdKey      = mkPreludeMiscIdUnique 267
1644
1645 -- data Guard = ...
1646 normalGEIdKey     = mkPreludeMiscIdUnique 310
1647 patGEIdKey        = mkPreludeMiscIdUnique 311
1648
1649 -- data Stmt = ...
1650 bindSIdKey       = mkPreludeMiscIdUnique 268
1651 letSIdKey        = mkPreludeMiscIdUnique 269
1652 noBindSIdKey     = mkPreludeMiscIdUnique 270
1653 parSIdKey        = mkPreludeMiscIdUnique 271
1654
1655 -- data Dec = ...
1656 funDIdKey         = mkPreludeMiscIdUnique 272
1657 valDIdKey         = mkPreludeMiscIdUnique 273
1658 dataDIdKey        = mkPreludeMiscIdUnique 274
1659 newtypeDIdKey     = mkPreludeMiscIdUnique 275
1660 tySynDIdKey       = mkPreludeMiscIdUnique 276
1661 classDIdKey       = mkPreludeMiscIdUnique 277
1662 instanceDIdKey    = mkPreludeMiscIdUnique 278
1663 sigDIdKey         = mkPreludeMiscIdUnique 279
1664 forImpDIdKey      = mkPreludeMiscIdUnique 297
1665
1666 -- type Cxt = ...
1667 cxtIdKey            = mkPreludeMiscIdUnique 280
1668
1669 -- data Strict = ...
1670 isStrictKey         = mkPreludeMiscIdUnique 281
1671 notStrictKey        = mkPreludeMiscIdUnique 282
1672
1673 -- data Con = ...
1674 normalCIdKey      = mkPreludeMiscIdUnique 283
1675 recCIdKey         = mkPreludeMiscIdUnique 284
1676 infixCIdKey       = mkPreludeMiscIdUnique 285
1677
1678 -- type StrictType = ...
1679 strictTKey        = mkPreludeMiscIdUnique 286
1680
1681 -- type VarStrictType = ...
1682 varStrictTKey     = mkPreludeMiscIdUnique 287
1683
1684 -- data Type = ...
1685 forallTIdKey      = mkPreludeMiscIdUnique 290
1686 varTIdKey         = mkPreludeMiscIdUnique 291
1687 conTIdKey         = mkPreludeMiscIdUnique 292
1688 tupleTIdKey       = mkPreludeMiscIdUnique 294
1689 arrowTIdKey       = mkPreludeMiscIdUnique 295
1690 listTIdKey        = mkPreludeMiscIdUnique 296
1691 appTIdKey         = mkPreludeMiscIdUnique 293
1692
1693 -- data Callconv = ...
1694 cCallIdKey      = mkPreludeMiscIdUnique 300
1695 stdCallIdKey    = mkPreludeMiscIdUnique 301
1696
1697 -- data Safety = ...
1698 unsafeIdKey     = mkPreludeMiscIdUnique 305
1699 safeIdKey       = mkPreludeMiscIdUnique 306
1700 threadsafeIdKey = mkPreludeMiscIdUnique 307
1701