[project @ 2002-11-09 09:58:56 by chak]
[ghc-hetmet.git] / ghc / compiler / deSugar / DsMeta.hs
index ab94bdc..3f00e7f 100644 (file)
@@ -42,7 +42,7 @@ import HsSyn            ( Pat(..), HsExpr(..), Stmt(..), HsLit(..), HsOverLit(..),
                    toHsType
                  )
 
-import PrelNames  ( mETA_META_Name, rationalTyConName )
+import PrelNames  ( mETA_META_Name, rationalTyConName, negateName )
 import MkIface   ( ifaceTyThing )
 import Name       ( Name, nameOccName, nameModule )
 import OccName   ( isDataOcc, isTvOcc, occNameUserString )
@@ -131,7 +131,7 @@ dsReify r@(ReifyOut ReifyDecl name)
 --                     Declarations
 -------------------------------------------------------
 
-repTopDs :: HsGroup Name -> DsM (Core [M.Decl])
+repTopDs :: HsGroup Name -> DsM (Core (M.Q [M.Dec]))
 repTopDs group
  = do { let { bndrs = groupBinders group } ;
        ss    <- mkGenSyms bndrs ;
@@ -151,8 +151,11 @@ repTopDs group
                        -- more needed
                        return (val_ds ++ catMaybes tycl_ds ++ inst_ds) }) ;
 
-       core_list <- coreList declTyConName decls ;
-       wrapNongenSyms ss core_list
+       decl_ty <- lookupType declTyConName ;
+       let { core_list = coreList' decl_ty decls } ;
+       q_decs  <- repSequenceQ decl_ty core_list ;
+
+       wrapNongenSyms ss q_decs
        -- Do *not* gensym top-level binders
       }
 
@@ -367,7 +370,10 @@ repE (OpApp e1 op fix e2) =
                     the_op <- lookupOcc op ;
                     repInfixApp arg1 the_op arg2 } 
     _        -> panic "DsMeta.repE: Operator is not a variable"
-repE (NegApp x nm)        = repE x >>= repNeg
+repE (NegApp x nm)        = do
+                             a         <- repE x
+                             negateVar <- lookupOcc negateName >>= repVar
+                             negateVar `repApp` a
 repE (HsPar x)            = repE x
 repE (SectionL x y)       = do { a <- repE x; b <- repE y; repSectionL a b } 
 repE (SectionR x y)       = do { a <- repE x; b <- repE y; repSectionR a b } 
@@ -384,16 +390,15 @@ repE (HsLet bs e)         = do { (ss,ds) <- repBinds bs
                               ; z <- repLetE ds e2
                               ; wrapGenSyns expTyConName ss z }
 -- FIXME: I haven't got the types here right yet
-repE (HsDo ctxt sts _ ty loc) 
-  | isComprCtxt ctxt      = do { (ss,zs) <- repSts sts; 
-                                e       <- repDoE (nonEmptyCoreList zs);
-                                wrapGenSyns expTyConName ss e }
-  | otherwise             = 
-    panic "DsMeta.repE: Can't represent mdo and [: :] yet"
-  where
-    isComprCtxt ListComp = True
-    isComprCtxt DoExpr  = True
-    isComprCtxt _       = False
+repE (HsDo DoExpr sts _ ty loc) 
+ = do { (ss,zs) <- repSts sts; 
+        e       <- repDoE (nonEmptyCoreList zs);
+        wrapGenSyns expTyConName ss e }
+repE (HsDo ListComp sts _ ty loc) 
+ = do { (ss,zs) <- repSts sts; 
+        e       <- repComp (nonEmptyCoreList zs);
+        wrapGenSyns expTyConName ss e }
+repE (HsDo _ _ _ _ _) = panic "DsMeta.repE: Can't represent mdo and [: :] yet"
 repE (ExplicitList ty es) = do { xs <- repEs es; repListExp xs } 
 repE (ExplicitPArr ty es) = 
   panic "DsMeta.repE: No explicit parallel arrays yet"
@@ -404,7 +409,7 @@ repE (RecordConOut _ _ _) = panic "DsMeta.repE: No record construction yet"
 repE (RecordUpdOut _ _ _ _) = panic "DsMeta.repE: No record update yet"
 
 repE (ExprWithTySig e ty) = do { e1 <- repE e; t1 <- repTy ty; repSigExp e1 t1 }
-repE (ArithSeqOut _ aseq) =
+repE (ArithSeqIn aseq) =
   case aseq of
     From e              -> do { ds1 <- repE e; repFrom ds1 }
     FromThen e1 e2      -> do 
@@ -650,6 +655,8 @@ repP (ConPatIn dc details)
          RecCon pairs   -> error "No records in template haskell yet"
          InfixCon p1 p2 -> do { qs <- repPs [p1,p2]; repPcon con_str qs }
    }
+repP (NPatIn l (Just _)) = panic "Can't cope with negative overloaded patterns yet (repP (NPatIn _ (Just _)))"
+repP (NPatIn l Nothing) = do { a <- repOverloadedLiteral l; repPlit a }
 repP other = panic "Exotic pattern inside meta brackets"
 
 repListPat :: [Pat Name] -> DsM (Core M.Patt)     
@@ -733,17 +740,14 @@ wrapGenSyns tc_name binds body@(MkC b)
 -- Just like wrapGenSym, but don't actually do the gensym
 -- Instead use the existing name
 -- Only used for [Decl]
-wrapNongenSyms :: [GenSymBind] 
-              -> Core [M.Decl] -> DsM (Core [M.Decl])
-wrapNongenSyms binds body@(MkC b)
-  = go binds
+wrapNongenSyms :: [GenSymBind] -> Core a -> DsM (Core a)
+wrapNongenSyms binds (MkC body)
+  = do { binds' <- mapM do_one binds ;
+        return (MkC (mkLets binds' body)) }
   where
-    go [] = return body
-    go ((name,id) : binds)
-      = do { MkC body'   <- go binds
-          ; MkC lit_str <- localVar name       -- No gensym
-          ; return (MkC (Let (NonRec id lit_str) body'))
-          }
+    do_one (name,id) 
+       = do { MkC lit_str <- localVar name     -- No gensym
+            ; return (NonRec id lit_str) }
 
 void = placeHolderType
 
@@ -847,9 +851,6 @@ repSigExp (MkC e) (MkC t) = rep2 sigExpName [e,t]
 repInfixApp :: Core M.Expr -> Core String -> Core M.Expr -> DsM (Core M.Expr)
 repInfixApp (MkC x) (MkC y) (MkC z) = rep2 infixAppName [x,y,z]
 
-repNeg :: Core M.Expr -> DsM (Core M.Expr)
-repNeg (MkC x) = rep2 negName [x]
-
 repSectionL :: Core M.Expr -> Core M.Expr -> DsM (Core M.Expr)
 repSectionL (MkC x) (MkC y) = rep2 infixAppName [x,y]
 
@@ -951,16 +952,16 @@ repLiteral lit
   = do { lit_expr <- dsLit lit; rep2 lit_name [lit_expr] }
   where
     lit_name = case lit of
-                HsInt _    -> intLName
-                HsChar _   -> charLName
-                HsString _ -> stringLName
-                HsRat _ _  -> rationalLName
-                other      -> uh_oh
+                HsInteger _ -> integerLName
+                HsChar _    -> charLName
+                HsString _  -> stringLName
+                HsRat _ _   -> rationalLName
+                other       -> uh_oh
     uh_oh = pprPanic "DsMeta.repLiteral: trying to represent exotic literal"
                    (ppr lit)
 
 repOverloadedLiteral :: HsOverLit -> DsM (Core M.Lit)
-repOverloadedLiteral (HsIntegral i _)   = repLiteral (HsInt i)
+repOverloadedLiteral (HsIntegral i _)   = repLiteral (HsInteger i)
 repOverloadedLiteral (HsFractional f _) = do { rat_ty <- lookupType rationalTyConName ;
                                               repLiteral (HsRat f rat_ty) }
        -- The type Rational will be in the environment, becuase 
@@ -980,6 +981,10 @@ repBindQ :: Type -> Type   -- a and b
 repBindQ ty_a ty_b (MkC x) (MkC y) 
   = rep2 bindQName [Type ty_a, Type ty_b, x, y] 
 
+repSequenceQ :: Type -> Core [M.Q a] -> DsM (Core (M.Q [a]))
+repSequenceQ ty_a (MkC list)
+  = rep2 sequenceQName [Type ty_a, list]
+
 ------------ Lists and Tuples -------------------
 -- turn a list of patterns into a single pattern matching a list
 
@@ -1025,18 +1030,18 @@ templateHaskellNames :: NameSet
 -- The names that are implicitly mentioned by ``bracket''
 -- Should stay in sync with the import list of DsMeta
 templateHaskellNames
-  = mkNameSet [ intLName,charLName, stringLName, rationalLName,
+  = mkNameSet [ integerLName,charLName, stringLName, rationalLName,
                plitName, pvarName, ptupName, 
                pconName, ptildeName, paspatName, pwildName, 
                 varName, conName, litName, appName, infixEName, lamName,
                 tupName, doEName, compName, 
                 listExpName, sigExpName, condName, letEName, caseEName,
-                infixAppName, negName, sectionLName, sectionRName,
+                infixAppName, sectionLName, sectionRName,
                 guardedName, normalName, 
                bindStName, letStName, noBindStName, parStName,
                fromName, fromThenName, fromToName, fromThenToName,
                funName, valName, liftName,
-               gensymName, returnQName, bindQName, 
+               gensymName, returnQName, bindQName, sequenceQName,
                matchName, clauseName, funName, valName, dataDName, classDName,
                instName, protoName, tvarName, tconName, tappName, 
                arrowTyConName, tupleTyConName, listTyConName, namedTyConName,
@@ -1057,7 +1062,7 @@ thModule = mkThPkgModule mETA_META_Name
 mk_known_key_name space str uniq 
   = mkKnownKeyExternalName thModule (mkOccFS space str) uniq 
 
-intLName       = varQual FSLIT("intL")          intLIdKey
+integerLName   = varQual FSLIT("integerL")      integerLIdKey
 charLName      = varQual FSLIT("charL")         charLIdKey
 stringLName    = varQual FSLIT("stringL")       stringLIdKey
 rationalLName  = varQual FSLIT("rationalL")     rationalLIdKey
@@ -1083,7 +1088,6 @@ condName       = varQual FSLIT("cond")          condIdKey
 letEName       = varQual FSLIT("letE")          letEIdKey
 caseEName      = varQual FSLIT("caseE")         caseEIdKey
 infixAppName   = varQual FSLIT("infixApp")      infixAppIdKey
-negName        = varQual FSLIT("neg")           negIdKey
 sectionLName   = varQual FSLIT("sectionL")      sectionLIdKey
 sectionRName   = varQual FSLIT("sectionR")      sectionRIdKey
 guardedName    = varQual FSLIT("guarded")       guardedIdKey
@@ -1100,6 +1104,7 @@ liftName       = varQual FSLIT("lift")          liftIdKey
 gensymName     = varQual FSLIT("gensym")        gensymIdKey
 returnQName    = varQual FSLIT("returnQ")       returnQIdKey
 bindQName      = varQual FSLIT("bindQ")         bindQIdKey
+sequenceQName  = varQual FSLIT("sequenceQ")     sequenceQIdKey
 
 -- type Mat = ...
 matchName      = varQual FSLIT("match")         matchIdKey
@@ -1180,13 +1185,14 @@ valIdKey        = mkPreludeMiscIdUnique 209
 protoIdKey      = mkPreludeMiscIdUnique 210
 matchIdKey      = mkPreludeMiscIdUnique 211
 clauseIdKey     = mkPreludeMiscIdUnique 212
-intLIdKey       = mkPreludeMiscIdUnique 213
+integerLIdKey   = mkPreludeMiscIdUnique 213
 charLIdKey      = mkPreludeMiscIdUnique 214
 
 classDIdKey     = mkPreludeMiscIdUnique 215
 instIdKey       = mkPreludeMiscIdUnique 216
 dataDIdKey      = mkPreludeMiscIdUnique 217
 
+sequenceQIdKey  = mkPreludeMiscIdUnique 218
 
 plitIdKey       = mkPreludeMiscIdUnique 220
 pvarIdKey       = mkPreludeMiscIdUnique 221
@@ -1209,7 +1215,7 @@ condIdKey       = mkPreludeMiscIdUnique 238
 letEIdKey       = mkPreludeMiscIdUnique 239
 caseEIdKey      = mkPreludeMiscIdUnique 240
 infixAppIdKey   = mkPreludeMiscIdUnique 241
-negIdKey       = mkPreludeMiscIdUnique 242
+-- 242 unallocated
 sectionLIdKey   = mkPreludeMiscIdUnique 243
 sectionRIdKey   = mkPreludeMiscIdUnique 244
 guardedIdKey    = mkPreludeMiscIdUnique 245