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