1 {-# OPTIONS -fno-warn-missing-signatures #-}
2 module Language.Core.Core where
4 import Language.Core.Encoding
7 import Data.List (elemIndex)
10 = Module AnMname [Tdef] [Vdefg]
11 deriving (Data, Typeable)
14 = Data (Qual Tcon) [Tbind] [Cdef]
15 -- type constructor; coercion name; type arguments; type rep
16 -- If we have: (Newtype tc co tbs (Just t))
17 -- there is an implicit axiom:
18 -- co tbs :: tc tbs :=: t
19 | Newtype (Qual Tcon) (Qual Tcon) [Tbind] Ty
20 deriving (Data, Typeable)
23 = Constr (Qual Dcon) [Tbind] [Ty]
24 deriving (Data, Typeable)
29 deriving (Data, Typeable)
31 newtype Vdef = Vdef (Qual Var,Ty,Exp)
32 deriving (Data, Typeable)
42 | Case Exp Vbind Ty [Alt] {- non-empty list -}
46 deriving (Data, Typeable)
51 deriving (Data, Typeable)
54 = Acon (Qual Dcon) [Tbind] [Vbind] Exp
57 deriving (Data, Typeable)
60 type Tbind = (Tvar,Kind)
67 -- Wired-in coercions:
68 -- These are primitive tycons in GHC, but in ext-core,
69 -- we make them explicit, to make the typechecker
70 -- somewhat more clear.
73 | UnsafeCoercion Ty Ty
77 deriving (Data, Typeable)
85 deriving (Data, Typeable)
87 -- A CoercionKind isn't really a Kind at all, but rather,
88 -- corresponds to an arbitrary user-declared axiom.
89 -- A tycon whose CoercionKind is (DefinedCoercion <tbs> (from, to))
90 -- represents a tycon with arity (length tbs), whose kind is
91 -- (from :=: to) (modulo substituting type arguments.
92 -- It's not a Kind because a coercion must always be fully applied:
93 -- whenever we see a tycon that has such a CoercionKind, it must
94 -- be fully applied if it's to be assigned an actual Kind.
95 -- So, a CoercionKind *only* appears in the environment (mapping
96 -- newtype axioms onto CoercionKinds).
99 DefinedCoercion [Tbind] (Ty,Ty)
101 -- The type constructor environment maps names that are
102 -- either type constructors or coercion names onto either
103 -- kinds or coercion kinds.
104 data KindOrCoercion = Kind Kind | Coercion CoercionKind
106 data Lit = Literal CoreLit Ty
107 deriving (Data, Typeable, Eq)
109 data CoreLit = Lint Integer
113 deriving (Data, Typeable, Eq)
115 -- Right now we represent module names as triples:
116 -- (package name, hierarchical names, leaf name)
117 -- An alternative to this would be to flatten the
118 -- module namespace, either when printing out
119 -- Core or (probably preferably) in a
121 -- We represent the empty module name (as in an unqualified name)
124 type Mname = Maybe AnMname
125 newtype AnMname = M (Pname, [Id], Id)
126 deriving (Eq, Ord, Data, Typeable)
128 deriving (Eq, Ord, Data, Typeable)
134 type Qual t = (Mname,t)
136 qual :: AnMname -> t -> Qual t
137 qual mn t = (Just mn, t)
139 unqual :: t -> Qual t
142 getModule :: Qual t -> Mname
147 eqKind :: Kind -> Kind -> Bool
148 eqKind Klifted Klifted = True
149 eqKind Kunlifted Kunlifted = True
150 eqKind Kopen Kopen = True
151 eqKind (Karrow k1 k2) (Karrow l1 l2) = k1 `eqKind` l1
153 eqKind (Keq t1 t2) (Keq u1 u2) = t1 == u1
157 splitTyConApp_maybe :: Ty -> Maybe (Qual Tcon,[Ty])
158 splitTyConApp_maybe (Tvar _) = Nothing
159 splitTyConApp_maybe (Tcon t) = Just (t,[])
160 splitTyConApp_maybe (Tapp rator rand) =
161 case (splitTyConApp_maybe rator) of
162 Just (r,rs) -> Just (r,rs++[rand])
163 Nothing -> case rator of
164 Tcon tc -> Just (tc,[rand])
166 splitTyConApp_maybe (Tforall _ _) = Nothing
168 splitTyConApp_maybe _ = Nothing
170 -- This used to be called nearlyEqualTy, but now that
171 -- we don't need to expand newtypes anymore, it seems
172 -- like equality to me!
173 equalTy :: Ty -> Ty -> Bool
174 equalTy t1 t2 = eqTy [] [] t1 t2
175 where eqTy e1 e2 (Tvar v1) (Tvar v2) =
176 case (elemIndex v1 e1,elemIndex v2 e2) of
177 (Just i1, Just i2) -> i1 == i2
178 (Nothing, Nothing) -> v1 == v2
180 eqTy _ _ (Tcon c1) (Tcon c2) = c1 == c2
181 eqTy e1 e2 (Tapp t1a t1b) (Tapp t2a t2b) =
182 eqTy e1 e2 t1a t2a && eqTy e1 e2 t1b t2b
183 eqTy e1 e2 (Tforall (tv1,tk1) b1) (Tforall (tv2,tk2) b2) =
184 tk1 `eqKind` tk2 && eqTy (tv1:e1) (tv2:e2) b1 b2
186 instance Eq Ty where (==) = equalTy
189 subKindOf :: Kind -> Kind -> Bool
190 _ `subKindOf` Kopen = True
191 (Karrow a1 r1) `subKindOf` (Karrow a2 r2) =
192 a2 `subKindOf` a1 && (r1 `subKindOf` r2)
193 k1 `subKindOf` k2 = k1 `eqKind` k2 -- doesn't worry about higher kinds
195 baseKind :: Kind -> Bool
196 baseKind (Karrow _ _ ) = False
199 isPrimVar (Just mn,_) = mn == primMname
202 primMname = mkPrimMname "Prim"
203 errMname = mkBaseMname "Err"
204 mkBaseMname,mkPrimMname :: Id -> AnMname
205 mkBaseMname mn = M (basePkg, ghcPrefix, mn)
206 mkPrimMname mn = M (primPkg, ghcPrefix, mn)
209 primPkg = P $ zEncodeString "ghc-prim"
212 baseMname = mkBaseMname "Base"
213 boolMname = mkPrimMname "Bool"
214 mainVar = qual mainMname "main"
215 wrapperMainVar = qual wrapperMainMname "main"
216 mainMname = M (mainPkg, mainPrefix, "Main")
217 wrapperMainMname = M (mainPkg, mainPrefix, "ZCMain")
218 wrapperMainAnMname = Just wrapperMainMname
226 tcArrow = (Just primMname, "ZLzmzgZR")
228 tArrow :: Ty -> Ty -> Ty
229 tArrow t1 t2 = Tapp (Tapp (Tcon tcArrow) t1) t2
231 mkFunTy :: Ty -> Ty -> Ty
232 mkFunTy randTy resultTy =
233 Tapp (Tapp (Tcon tcArrow) randTy) resultTy
236 ktArrow = Karrow Kopen (Karrow Kopen Klifted)
243 tcUtuple :: Int -> Qual Tcon
244 tcUtuple n = (Just primMname,"Z"++ (show n) ++ "H")
246 ktUtuple :: Int -> Kind
247 ktUtuple n = foldr Karrow Kunlifted (replicate n Kopen)
249 tUtuple :: [Ty] -> Ty
250 tUtuple ts = foldl Tapp (Tcon (tcUtuple (length ts))) ts
252 isUtupleTy :: Ty -> Bool
253 isUtupleTy (Tapp t _) = isUtupleTy t
254 isUtupleTy (Tcon tc) = tc `elem` [tcUtuple n | n <- [1..maxUtuple]]
257 dcUtuple :: Int -> Qual Dcon
258 -- TODO: Seems like Z2H etc. appears in ext-core files,
259 -- not $wZ2H etc. Is this right?
260 dcUtuple n = (Just primMname,"Z" ++ (show n) ++ "H")
262 isUtupleDc :: Qual Dcon -> Bool
263 isUtupleDc dc = dc `elem` [dcUtuple n | n <- [1..maxUtuple]]
265 dcUtupleTy :: Int -> Ty
267 foldr ( \tv t -> Tforall (tv,Kopen) t)
268 (foldr ( \tv t -> tArrow (Tvar tv) t)
269 (tUtuple (map Tvar tvs)) tvs)
271 where tvs = map ( \i -> ("a" ++ (show i))) [1..n]
273 utuple :: [Ty] -> [Exp] -> Exp
274 utuple ts es = foldl App (foldl Appt (Dcon (dcUtuple (length es))) ts) es
276 ---- snarfed from GHC's CoreSyn
277 flattenBinds :: [Vdefg] -> [Vdef] -- Get all the lhs/rhs pairs
278 flattenBinds (Nonrec vd : binds) = vd : flattenBinds binds
279 flattenBinds (Rec prs1 : binds) = prs1 ++ flattenBinds binds