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