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