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