8f08d350aee8a6b98fcf7f8c7f2d53a1bca6faad
[ghc-hetmet.git] / compiler / types / Coercion.lhs
1
2  Module for type coercions, as in System FC.
3
4 Coercions are represented as types, and their kinds tell what types the 
5 coercion works on. 
6
7 The coercion kind constructor is a special TyCon that must always be saturated
8
9   typeKind (symCoercion type) :: TyConApp CoercionTyCon{...} [type, type]
10
11 \begin{code}
12 module Coercion (
13         Coercion,
14  
15         mkCoKind, mkReflCoKind, splitCoercionKind_maybe, splitCoercionKind,
16         coercionKind, coercionKinds, coercionKindPredTy,
17
18         -- Equality predicates
19         isEqPred, mkEqPred, getEqPredTys, isEqPredTy,  
20
21         -- Coercion transformations
22         mkSymCoercion, mkTransCoercion,
23         mkLeftCoercion, mkRightCoercion, mkInstCoercion, mkAppCoercion,
24         mkForAllCoercion, mkFunCoercion, mkInstsCoercion, mkUnsafeCoercion,
25         mkNewTypeCoercion, mkAppsCoercion,
26
27         splitNewTypeRepCo_maybe, decomposeCo,
28
29         unsafeCoercionTyCon, symCoercionTyCon,
30         transCoercionTyCon, leftCoercionTyCon, 
31         rightCoercionTyCon, instCoercionTyCon -- needed by TysWiredIn
32        ) where 
33
34 #include "HsVersions.h"
35
36 import TypeRep
37 import Type       ( Type, Kind, PredType, substTyWith, mkAppTy, mkForAllTy,
38                     mkFunTy, splitAppTy_maybe, splitForAllTy_maybe, coreView,
39                     kindView, mkTyConApp, isCoercionKind, isEqPred, mkAppTys
40                   )
41 import TyCon      ( TyCon, tyConArity, mkCoercionTyCon, isNewTyCon,
42                     newTyConRhs, newTyConCo, 
43                     isCoercionTyCon, isCoercionTyCon_maybe )
44 import Var        ( Var, TyVar, isTyVar, tyVarKind )
45 import Name       ( BuiltInSyntax(..), Name, mkWiredInName, tcName )
46 import OccName    ( mkOccNameFS )
47 import PrelNames  ( symCoercionTyConKey, 
48                     transCoercionTyConKey, leftCoercionTyConKey,
49                     rightCoercionTyConKey, instCoercionTyConKey, 
50                     unsafeCoercionTyConKey, gHC_PRIM
51                   )
52 import Util       ( lengthIs, snocView )
53 import Unique     ( hasKey )
54 import BasicTypes ( Arity )
55 import Outputable
56
57
58
59 ------------------------------
60 decomposeCo :: Arity -> Coercion -> [Coercion]
61 -- (decomposeCo 3 c) = [right (left (left c)), right (left c), right c]
62 decomposeCo n co
63   = go n co []
64   where
65     go 0 co cos = cos
66     go n co cos = go (n-1) (mkLeftCoercion co)
67                            (mkRightCoercion co : cos)
68
69 ------------------------------
70
71 -------------------------------------------------------
72 -- and some coercion kind stuff
73
74 isEqPredTy (PredTy pred) = isEqPred pred
75 isEqPredTy other         = False
76
77 mkEqPred :: (Type, Type) -> PredType
78 mkEqPred (ty1, ty2) = EqPred ty1 ty2
79
80 getEqPredTys :: PredType -> (Type,Type)
81 getEqPredTys (EqPred ty1 ty2) = (ty1, ty2)
82 getEqPredTys other            = pprPanic "getEqPredTys" (ppr other)
83
84 mkCoKind :: Type -> Type -> CoercionKind
85 mkCoKind ty1 ty2 = PredTy (EqPred ty1 ty2)
86
87 mkReflCoKind :: Type -> CoercionKind
88 mkReflCoKind ty = mkCoKind ty ty
89
90 splitCoercionKind :: CoercionKind -> (Type, Type)
91 splitCoercionKind co | Just co' <- kindView co = splitCoercionKind co'
92 splitCoercionKind (PredTy (EqPred ty1 ty2))    = (ty1, ty2)
93
94 splitCoercionKind_maybe :: Kind -> Maybe (Type, Type)
95 splitCoercionKind_maybe co | Just co' <- kindView co = splitCoercionKind_maybe co'
96 splitCoercionKind_maybe (PredTy (EqPred ty1 ty2)) = Just (ty1, ty2)
97 splitCoercionKind_maybe other = Nothing
98
99 isCoVar :: Var -> Bool
100 isCoVar tv = isTyVar tv && isCoercionKind (tyVarKind tv)
101
102 type Coercion     = Type
103 type CoercionKind = Kind        -- A CoercionKind is always of form (ty1 :=: ty2)
104
105 coercionKind :: Coercion -> (Type, Type)
106 --      c :: (t1 :=: t2)
107 -- Then (coercionKind c) = (t1,t2)
108
109 coercionKind (TyVarTy a) | isCoVar a = splitCoercionKind (tyVarKind a)
110                          | otherwise = let t = (TyVarTy a) in (t, t)
111 coercionKind (AppTy ty1 ty2) 
112   = let (t1, t2) = coercionKind ty1
113         (s1, s2) = coercionKind ty2 in
114     (mkAppTy t1 s1, mkAppTy t2 s2)
115 coercionKind (TyConApp tc args)
116   | Just (ar, rule) <- isCoercionTyCon_maybe tc 
117   = if length args >= ar 
118     then splitCoercionKind (rule args)
119     else pprPanic ("arity/arguments mismatch in coercionKind:") 
120              (ppr ar $$ ppr tc <+> ppr args)
121   | otherwise
122   = let (lArgs, rArgs) = coercionKinds args in
123     (TyConApp tc lArgs, TyConApp tc rArgs)
124 coercionKind (FunTy ty1 ty2) 
125   = let (t1, t2) = coercionKind ty1
126         (s1, s2) = coercionKind ty2 in
127     (mkFunTy t1 s1, mkFunTy t2 s2)
128 coercionKind (ForAllTy tv ty) 
129   = let (ty1, ty2) = coercionKind ty in
130     (ForAllTy tv ty1, ForAllTy tv ty2)
131 coercionKind (NoteTy _ ty) = coercionKind ty
132 coercionKind (PredTy (EqPred c1 c2)) 
133   = let k1 = coercionKindPredTy c1
134         k2 = coercionKindPredTy c2 in
135     (k1,k2)
136 coercionKind (PredTy (ClassP cl args)) 
137   = let (lArgs, rArgs) = coercionKinds args in
138     (PredTy (ClassP cl lArgs), PredTy (ClassP cl rArgs))
139 coercionKind (PredTy (IParam name ty))
140   = let (ty1, ty2) = coercionKind ty in
141     (PredTy (IParam name ty1), PredTy (IParam name ty2))
142
143 coercionKindPredTy :: Coercion -> CoercionKind
144 coercionKindPredTy c = let (t1, t2) = coercionKind c in mkCoKind t1 t2
145
146 coercionKinds :: [Coercion] -> ([Type], [Type])
147 coercionKinds tys = unzip $ map coercionKind tys
148
149 -------------------------------------
150 -- Coercion kind and type mk's
151 -- (make saturated TyConApp CoercionTyCon{...} args)
152
153 mkCoercion coCon args = ASSERT( tyConArity coCon == length args ) 
154                         TyConApp coCon args
155
156 mkAppCoercion, mkFunCoercion, mkTransCoercion, mkInstCoercion :: Coercion -> Coercion -> Coercion
157 mkSymCoercion, mkLeftCoercion, mkRightCoercion :: Coercion -> Coercion
158
159 mkAppCoercion    co1 co2 = mkAppTy co1 co2
160 mkAppsCoercion   co1 tys = foldl mkAppTy co1 tys
161 -- note that a TyVar should be used here, not a CoVar (nor a TcTyVar)
162 mkForAllCoercion tv  co  = ASSERT ( isTyVar tv ) mkForAllTy tv co
163 mkFunCoercion    co1 co2 = mkFunTy co1 co2
164
165 mkSymCoercion co      
166   | Just co2 <- splitSymCoercion_maybe co = co2
167   | Just (co1, co2) <- splitAppCoercion_maybe co 
168     -- should make this case better
169   = mkAppCoercion (mkSymCoercion co1) (mkSymCoercion co2)
170   | Just (co1, co2) <- splitTransCoercion_maybe co
171   = mkTransCoercion (mkSymCoercion co1) (mkSymCoercion co2)
172   | Just (co, ty) <- splitInstCoercion_maybe co
173   = mkInstCoercion (mkSymCoercion co) ty
174   | Just co <- splitLeftCoercion_maybe co
175   = mkLeftCoercion (mkSymCoercion co)
176   | Just co <- splitRightCoercion_maybe co
177   = mkRightCoercion (mkSymCoercion co)
178 mkSymCoercion (ForAllTy tv ty) = ForAllTy tv (mkSymCoercion ty)
179 -- for atomic types and constructors, we can just ignore sym since these
180 -- are reflexive coercions
181 mkSymCoercion (TyVarTy tv) 
182   | isCoVar tv = mkCoercion symCoercionTyCon [TyVarTy tv]
183   | otherwise  = TyVarTy tv
184 mkSymCoercion co = mkCoercion symCoercionTyCon [co] 
185                    -- this should not happen but does
186
187 -- Smart constructors for left and right
188 mkLeftCoercion co 
189   | Just (co', _) <- splitAppCoercion_maybe co = co'
190   | otherwise                            = mkCoercion leftCoercionTyCon [co]
191
192 mkRightCoercion  co      
193   | Just (co1, co2) <- splitAppCoercion_maybe co = co2
194   | otherwise = mkCoercion rightCoercionTyCon [co]
195
196 mkTransCoercion co1 co2 = mkCoercion transCoercionTyCon [co1, co2]
197
198 mkInstCoercion  co ty = mkCoercion instCoercionTyCon  [co, ty]
199
200 mkInstsCoercion co tys = foldl mkInstCoercion co tys
201
202 splitSymCoercion_maybe :: Coercion -> Maybe Coercion
203 splitSymCoercion_maybe (TyConApp tc [co]) = 
204   if tc `hasKey` symCoercionTyConKey
205   then Just co
206   else Nothing
207 splitSymCoercion_maybe co = Nothing
208
209 splitAppCoercion_maybe :: Coercion -> Maybe (Coercion, Coercion)
210 -- Splits a coercion application, being careful *not* to split (left c), etc
211 -- which are really sytactic constructs, not applications
212 splitAppCoercion_maybe co  | Just co' <- coreView co = splitAppCoercion_maybe co'
213 splitAppCoercion_maybe (FunTy ty1 ty2)   = Just (TyConApp funTyCon [ty1], ty2)
214 splitAppCoercion_maybe (AppTy ty1 ty2)   = Just (ty1, ty2)
215 splitAppCoercion_maybe (TyConApp tc tys) 
216    | not (isCoercionTyCon tc)
217    = case snocView tys of
218        Just (tys', ty') -> Just (TyConApp tc tys', ty')
219        Nothing          -> Nothing
220 splitAppCoercion_maybe co = Nothing
221
222 splitTransCoercion_maybe :: Coercion -> Maybe (Coercion, Coercion)
223 splitTransCoercion_maybe (TyConApp tc [ty1, ty2]) 
224  = if tc `hasKey` transCoercionTyConKey then
225        Just (ty1, ty2)
226    else
227        Nothing
228 splitTransCoercion_maybe other = Nothing
229
230 splitInstCoercion_maybe :: Coercion -> Maybe (Coercion, Type)
231 splitInstCoercion_maybe (TyConApp tc [ty1, ty2])
232  = if tc `hasKey` instCoercionTyConKey then
233        Just (ty1, ty2)
234     else
235        Nothing
236 splitInstCoercion_maybe other = Nothing
237
238 splitLeftCoercion_maybe :: Coercion -> Maybe Coercion
239 splitLeftCoercion_maybe (TyConApp tc [co])
240  = if tc `hasKey` leftCoercionTyConKey then
241        Just co
242    else
243        Nothing
244 splitLeftCoercion_maybe other = Nothing
245
246 splitRightCoercion_maybe :: Coercion -> Maybe Coercion
247 splitRightCoercion_maybe (TyConApp tc [co])
248  = if tc `hasKey` rightCoercionTyConKey then
249        Just co
250    else
251        Nothing
252 splitRightCoercion_maybe other = Nothing
253
254 -- Unsafe coercion is not safe, it is used when we know we are dealing with
255 -- bottom, which is the one case in which it is safe
256 mkUnsafeCoercion :: Type -> Type -> Coercion
257 mkUnsafeCoercion ty1 ty2 
258   = mkCoercion unsafeCoercionTyCon [ty1, ty2]
259
260
261 -- make the coercion associated with a newtype
262 mkNewTypeCoercion :: Name -> TyCon -> [TyVar] -> Type -> TyCon
263 mkNewTypeCoercion name tycon tvs rhs_ty 
264   = ASSERT (length tvs == tyConArity tycon)
265     mkCoercionTyCon name (tyConArity tycon) rule
266   where
267     rule args = mkCoKind (substTyWith tvs args rhs_ty) (TyConApp tycon args)
268
269 --------------------------------------
270 -- Coercion Type Constructors...
271
272 -- Example.  The coercion ((sym c) (sym d) (sym e))
273 -- will be represented by (TyConApp sym [c, sym d, sym e])
274 -- If sym c :: p1=q1
275 --    sym d :: p2=q2
276 --    sym e :: p3=q3
277 -- then ((sym c) (sym d) (sym e)) :: (p1 p2 p3)=(q1 q2 q3)
278 --
279 -- (mkKindingFun f) is given the args [c, sym d, sym e]
280 mkKindingFun :: ([Type] -> (Type, Type, [Type])) -> [Type] -> Kind
281 mkKindingFun f args = 
282   let (ty1, ty2, rest) = f args in 
283   let (argtys1, argtys2) = unzip (map coercionKind rest) in
284   mkCoKind (mkAppTys ty1 argtys1) (mkAppTys ty2 argtys2)
285         
286
287 symCoercionTyCon, transCoercionTyCon, leftCoercionTyCon, rightCoercionTyCon, instCoercionTyCon :: TyCon
288 -- Each coercion TyCon is built with the special CoercionTyCon record and
289 -- carries its won kinding rule.  Such CoercionTyCons must be fully applied
290 -- by any TyConApp in which they are applied, however they may also be over
291 -- applied (see example above) and the kinding function must deal with this.
292 symCoercionTyCon = 
293   mkCoercionTyCon symCoercionTyConName 1 (mkKindingFun flipCoercionKindOf)
294   where
295     flipCoercionKindOf (co:rest) = (ty2, ty1, rest)
296         where
297           (ty1, ty2) = coercionKind co
298
299 transCoercionTyCon = 
300   mkCoercionTyCon transCoercionTyConName 2 (mkKindingFun composeCoercionKindsOf)
301   where
302     composeCoercionKindsOf (co1:co2:rest) = (a1, r2, rest)
303       where
304         (a1, r1) = coercionKind co1
305         (a2, r2) = coercionKind co2 
306
307 leftCoercionTyCon =
308   mkCoercionTyCon leftCoercionTyConName 1 (mkKindingFun leftProjectCoercionKindOf)
309   where
310     leftProjectCoercionKindOf (co:rest) = (ty1, ty2, rest)
311       where
312         (ty1,ty2) = fst (splitCoercionKindOf co)
313
314 rightCoercionTyCon =
315   mkCoercionTyCon rightCoercionTyConName 1 (mkKindingFun rightProjectCoercionKindOf)
316   where
317     rightProjectCoercionKindOf (co:rest) = (ty1, ty2, rest)
318       where
319         (ty1,ty2) = snd (splitCoercionKindOf co)
320
321 splitCoercionKindOf :: Type -> ((Type,Type), (Type,Type))
322 -- Helper for left and right.  Finds coercion kind of its input and
323 -- returns the left and right projections of the coercion...
324 --
325 -- if c :: t1 s1 :=: t2 s2 then splitCoercionKindOf c = ((t1, t2), (s1, s2))
326 splitCoercionKindOf co
327   | Just (ty1, ty2) <- splitCoercionKind_maybe (coercionKindPredTy co)
328   , Just (ty_fun1, ty_arg1) <- splitAppTy_maybe ty1
329   , Just (ty_fun2, ty_arg2) <- splitAppTy_maybe ty2
330   = ((ty_fun1, ty_fun2),(ty_arg1, ty_arg2))
331
332 instCoercionTyCon 
333   =  mkCoercionTyCon instCoercionTyConName 2 (mkKindingFun instCoercionKind)
334   where
335     instantiateCo t s =
336       let Just (tv, ty) = splitForAllTy_maybe t in
337       substTyWith [tv] [s] ty
338
339     instCoercionKind (co1:ty:rest) = (instantiateCo t1 ty, instantiateCo t2 ty, rest)
340       where (t1, t2) = coercionKind co1
341
342 unsafeCoercionTyCon 
343   = mkCoercionTyCon unsafeCoercionTyConName 2 (mkKindingFun unsafeCoercionKind)
344   where
345    unsafeCoercionKind (ty1:ty2:rest) = (ty1,ty2,rest) 
346         
347 --------------------------------------
348 -- ...and their names
349
350 mkCoConName occ key coCon = mkWiredInName gHC_PRIM (mkOccNameFS tcName occ)
351                             key Nothing (ATyCon coCon) BuiltInSyntax
352
353 transCoercionTyConName = mkCoConName FSLIT("trans") transCoercionTyConKey transCoercionTyCon
354 symCoercionTyConName   = mkCoConName FSLIT("sym") symCoercionTyConKey symCoercionTyCon
355 leftCoercionTyConName  = mkCoConName FSLIT("left") leftCoercionTyConKey leftCoercionTyCon
356 rightCoercionTyConName = mkCoConName FSLIT("right") rightCoercionTyConKey rightCoercionTyCon
357 instCoercionTyConName  = mkCoConName FSLIT("inst") instCoercionTyConKey instCoercionTyCon
358 unsafeCoercionTyConName = mkCoConName FSLIT("CoUnsafe") unsafeCoercionTyConKey unsafeCoercionTyCon
359
360
361
362 -- this is here to avoid module loops
363 splitNewTypeRepCo_maybe :: Type -> Maybe (Type, Coercion)  
364 -- Sometimes we want to look through a recursive newtype, and that's what happens here
365 -- It only strips *one layer* off, so the caller will usually call itself recursively
366 -- Only applied to types of kind *, hence the newtype is always saturated
367 splitNewTypeRepCo_maybe ty 
368   | Just ty' <- coreView ty = splitNewTypeRepCo_maybe ty'
369 splitNewTypeRepCo_maybe (TyConApp tc tys)
370   | isNewTyCon tc 
371   = ASSERT( tys `lengthIs` tyConArity tc )      -- splitNewTypeRepCo_maybe only be applied 
372                                                 --      to *types* (of kind *)
373         case newTyConRhs tc of
374           (tvs, rep_ty) -> 
375               ASSERT( length tvs == length tys )
376               Just (substTyWith tvs tys rep_ty, mkTyConApp co_con tys)
377   where
378     co_con = maybe (pprPanic "splitNewTypeRepCo_maybe" (ppr tc)) id (newTyConCo tc)
379
380 splitNewTypeRepCo_maybe other = Nothing
381 \end{code}