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