2 % (c) The University of Glasgow 2006
5 Module for type coercions, as in System FC.
7 Coercions are represented as types, and their kinds tell what types the
10 The coercion kind constructor is a special TyCon that must always be saturated
12 typeKind (symCoercion type) :: TyConApp CoercionTyCon{...} [type, type]
15 {-# OPTIONS -fno-warn-incomplete-patterns #-}
16 -- The above warning supression flag is a temporary kludge.
17 -- While working on this module you are encouraged to remove it and fix
18 -- any warnings in the module. See
19 -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
25 mkCoKind, mkReflCoKind, splitCoercionKind_maybe, splitCoercionKind,
26 coercionKind, coercionKinds, coercionKindPredTy,
28 -- Equality predicates
29 isEqPred, mkEqPred, getEqPredTys, isEqPredTy,
31 -- Coercion transformations
33 mkSymCoercion, mkTransCoercion,
34 mkLeftCoercion, mkRightCoercion, mkRightCoercions,
35 mkInstCoercion, mkAppCoercion,
36 mkForAllCoercion, mkFunCoercion, mkInstsCoercion, mkUnsafeCoercion,
37 mkNewTypeCoercion, mkFamInstCoercion, mkAppsCoercion,
39 splitNewTypeRepCo_maybe, instNewTyCon_maybe, decomposeCo,
41 unsafeCoercionTyCon, symCoercionTyCon,
42 transCoercionTyCon, leftCoercionTyCon,
43 rightCoercionTyCon, instCoercionTyCon, -- needed by TysWiredIn
49 mkTyConAppCoI, mkAppTyCoI, mkFunTyCoI,
50 mkNoteTyCoI, mkForAllTyCoI,
52 mkClassPPredCoI, mkIParamPredCoI, mkEqPredCoI
56 #include "HsVersions.h"
73 type CoercionKind = Kind -- A CoercionKind is always of form (ty1 :=: ty2)
75 ------------------------------
76 decomposeCo :: Arity -> Coercion -> [Coercion]
77 -- (decomposeCo 3 c) = [right (left (left c)), right (left c), right c]
78 -- So this breaks a coercion with kind T A B C :=: T D E F into
79 -- a list of coercions of kinds A :=: D, B :=: E and E :=: F
84 go n co cos = go (n-1) (mkLeftCoercion co)
85 (mkRightCoercion co : cos)
87 ------------------------------
89 -------------------------------------------------------
90 -- and some coercion kind stuff
92 isEqPredTy :: Type -> Bool
93 isEqPredTy (PredTy pred) = isEqPred pred
96 mkEqPred :: (Type, Type) -> PredType
97 mkEqPred (ty1, ty2) = EqPred ty1 ty2
99 getEqPredTys :: PredType -> (Type,Type)
100 getEqPredTys (EqPred ty1 ty2) = (ty1, ty2)
101 getEqPredTys other = pprPanic "getEqPredTys" (ppr other)
103 mkCoKind :: Type -> Type -> CoercionKind
104 mkCoKind ty1 ty2 = PredTy (EqPred ty1 ty2)
106 mkReflCoKind :: Type -> CoercionKind
107 mkReflCoKind ty = mkCoKind ty ty
109 splitCoercionKind :: CoercionKind -> (Type, Type)
110 splitCoercionKind co | Just co' <- kindView co = splitCoercionKind co'
111 splitCoercionKind (PredTy (EqPred ty1 ty2)) = (ty1, ty2)
113 splitCoercionKind_maybe :: Kind -> Maybe (Type, Type)
114 splitCoercionKind_maybe co | Just co' <- kindView co = splitCoercionKind_maybe co'
115 splitCoercionKind_maybe (PredTy (EqPred ty1 ty2)) = Just (ty1, ty2)
116 splitCoercionKind_maybe _ = Nothing
118 coercionKind :: Coercion -> (Type, Type)
120 -- Then (coercionKind c) = (t1,t2)
121 coercionKind ty@(TyVarTy a) | isCoVar a = splitCoercionKind (tyVarKind a)
122 | otherwise = (ty, ty)
123 coercionKind (AppTy ty1 ty2)
124 = let (t1, t2) = coercionKind ty1
125 (s1, s2) = coercionKind ty2 in
126 (mkAppTy t1 s1, mkAppTy t2 s2)
127 coercionKind (TyConApp tc args)
128 | Just (ar, rule) <- isCoercionTyCon_maybe tc
129 -- CoercionTyCons carry their kinding rule, so we use it here
130 = ASSERT( length args >= ar ) -- Always saturated
131 let (ty1,ty2) = rule (take ar args) -- Apply the rule to the right number of args
132 (tys1, tys2) = coercionKinds (drop ar args)
133 in (mkAppTys ty1 tys1, mkAppTys ty2 tys2)
136 = let (lArgs, rArgs) = coercionKinds args in
137 (TyConApp tc lArgs, TyConApp tc rArgs)
138 coercionKind (FunTy ty1 ty2)
139 = let (t1, t2) = coercionKind ty1
140 (s1, s2) = coercionKind ty2 in
141 (mkFunTy t1 s1, mkFunTy t2 s2)
142 coercionKind (ForAllTy tv ty)
143 = let (ty1, ty2) = coercionKind ty in
144 (ForAllTy tv ty1, ForAllTy tv ty2)
145 coercionKind (NoteTy _ ty) = coercionKind ty
146 coercionKind (PredTy (EqPred c1 c2))
147 = let k1 = coercionKindPredTy c1
148 k2 = coercionKindPredTy c2 in
150 coercionKind (PredTy (ClassP cl args))
151 = let (lArgs, rArgs) = coercionKinds args in
152 (PredTy (ClassP cl lArgs), PredTy (ClassP cl rArgs))
153 coercionKind (PredTy (IParam name ty))
154 = let (ty1, ty2) = coercionKind ty in
155 (PredTy (IParam name ty1), PredTy (IParam name ty2))
157 coercionKindPredTy :: Coercion -> CoercionKind
158 coercionKindPredTy c = let (t1, t2) = coercionKind c in mkCoKind t1 t2
160 coercionKinds :: [Coercion] -> ([Type], [Type])
161 coercionKinds tys = unzip $ map coercionKind tys
163 -------------------------------------
164 -- Coercion kind and type mk's
165 -- (make saturated TyConApp CoercionTyCon{...} args)
167 mkCoercion :: TyCon -> [Type] -> Coercion
168 mkCoercion coCon args = ASSERT( tyConArity coCon == length args )
171 mkAppCoercion, mkFunCoercion, mkTransCoercion, mkInstCoercion :: Coercion -> Coercion -> Coercion
172 mkSymCoercion, mkLeftCoercion, mkRightCoercion :: Coercion -> Coercion
173 mkAppsCoercion, mkInstsCoercion :: Coercion -> [Coercion] -> Coercion
174 mkForAllCoercion :: Var -> Coercion -> Coercion
176 mkAppCoercion co1 co2 = mkAppTy co1 co2
177 mkAppsCoercion co1 tys = foldl mkAppTy co1 tys
178 -- note that a TyVar should be used here, not a CoVar (nor a TcTyVar)
179 mkForAllCoercion tv co = ASSERT ( isTyVar tv ) mkForAllTy tv co
180 mkFunCoercion co1 co2 = mkFunTy co1 co2
183 -------------------------------
184 -- This smart constructor creates a sym'ed version its argument,
185 -- but tries to push the sym's down to the leaves. If we come to
186 -- sym tv or sym tycon then we can drop the sym because tv and tycon
187 -- are reflexive coercions
189 | Just co' <- coreView co = mkSymCoercion co'
191 mkSymCoercion (ForAllTy tv ty) = ForAllTy tv (mkSymCoercion ty)
192 mkSymCoercion (AppTy co1 co2) = AppTy (mkSymCoercion co1) (mkSymCoercion co2)
193 mkSymCoercion (FunTy co1 co2) = FunTy (mkSymCoercion co1) (mkSymCoercion co2)
195 mkSymCoercion (TyConApp tc cos)
196 | not (isCoercionTyCon tc) = mkTyConApp tc (map mkSymCoercion cos)
198 mkSymCoercion (TyConApp tc [co])
199 | tc `hasKey` symCoercionTyConKey = co -- sym (sym co) --> co
200 | tc `hasKey` leftCoercionTyConKey = mkLeftCoercion (mkSymCoercion co)
201 | tc `hasKey` rightCoercionTyConKey = mkRightCoercion (mkSymCoercion co)
203 mkSymCoercion (TyConApp tc [co1,co2])
204 | tc `hasKey` transCoercionTyConKey
205 -- sym (co1 `trans` co2) --> (sym co2) `trans (sym co2)
206 -- Note reversal of arguments!
207 = mkTransCoercion (mkSymCoercion co2) (mkSymCoercion co1)
209 | tc `hasKey` instCoercionTyConKey
210 -- sym (co @ ty) --> (sym co) @ ty
211 -- Note: sym is not applied to 'ty'
212 = mkInstCoercion (mkSymCoercion co1) co2
214 mkSymCoercion (TyConApp tc cos) -- Other coercion tycons, such as those
215 = mkCoercion symCoercionTyCon [TyConApp tc cos] -- arising from newtypes
217 mkSymCoercion (TyVarTy tv)
218 | isCoVar tv = mkCoercion symCoercionTyCon [TyVarTy tv]
219 | otherwise = TyVarTy tv -- Reflexive
221 -------------------------------
222 -- ToDo: we should be cleverer about transitivity
223 mkTransCoercion g1 g2 -- sym g `trans` g = id
224 | (t1,_) <- coercionKind g1
225 , (_,t2) <- coercionKind g2
230 = mkCoercion transCoercionTyCon [g1, g2]
233 -------------------------------
234 -- Smart constructors for left and right
236 | Just (co', _) <- splitAppCoercion_maybe co = co'
237 | otherwise = mkCoercion leftCoercionTyCon [co]
240 | Just (_, co2) <- splitAppCoercion_maybe co = co2
241 | otherwise = mkCoercion rightCoercionTyCon [co]
243 mkRightCoercions :: Int -> Coercion -> [Coercion]
244 mkRightCoercions n co
249 = case splitAppCoercion_maybe co of
250 Just (co1,co2) -> go (n-1) co1 (co2:acc)
251 Nothing -> go (n-1) (mkCoercion leftCoercionTyCon [co]) (mkCoercion rightCoercionTyCon [co]:acc)
256 | Just (tv,co') <- splitForAllTy_maybe co
257 = substTyWith [tv] [ty] co' -- (forall a.co) @ ty --> co[ty/a]
259 = mkCoercion instCoercionTyCon [co, ty]
261 mkInstsCoercion co tys = foldl mkInstCoercion co tys
264 splitSymCoercion_maybe :: Coercion -> Maybe Coercion
265 splitSymCoercion_maybe (TyConApp tc [co]) =
266 if tc `hasKey` symCoercionTyConKey
269 splitSymCoercion_maybe co = Nothing
272 splitAppCoercion_maybe :: Coercion -> Maybe (Coercion, Coercion)
273 -- Splits a coercion application, being careful *not* to split (left c), etc
274 -- which are really sytactic constructs, not applications
275 splitAppCoercion_maybe co | Just co' <- coreView co = splitAppCoercion_maybe co'
276 splitAppCoercion_maybe (FunTy ty1 ty2) = Just (TyConApp funTyCon [ty1], ty2)
277 splitAppCoercion_maybe (AppTy ty1 ty2) = Just (ty1, ty2)
278 splitAppCoercion_maybe (TyConApp tc tys)
279 | not (isCoercionTyCon tc)
280 = case snocView tys of
281 Just (tys', ty') -> Just (TyConApp tc tys', ty')
283 splitAppCoercion_maybe _ = Nothing
286 splitTransCoercion_maybe :: Coercion -> Maybe (Coercion, Coercion)
287 splitTransCoercion_maybe (TyConApp tc [ty1, ty2])
288 = if tc `hasKey` transCoercionTyConKey then
292 splitTransCoercion_maybe other = Nothing
294 splitInstCoercion_maybe :: Coercion -> Maybe (Coercion, Type)
295 splitInstCoercion_maybe (TyConApp tc [ty1, ty2])
296 = if tc `hasKey` instCoercionTyConKey then
300 splitInstCoercion_maybe other = Nothing
302 splitLeftCoercion_maybe :: Coercion -> Maybe Coercion
303 splitLeftCoercion_maybe (TyConApp tc [co])
304 = if tc `hasKey` leftCoercionTyConKey then
308 splitLeftCoercion_maybe other = Nothing
310 splitRightCoercion_maybe :: Coercion -> Maybe Coercion
311 splitRightCoercion_maybe (TyConApp tc [co])
312 = if tc `hasKey` rightCoercionTyConKey then
316 splitRightCoercion_maybe other = Nothing
319 -- Unsafe coercion is not safe, it is used when we know we are dealing with
320 -- bottom, which is one case in which it is safe. It is also used to
321 -- implement the unsafeCoerce# primitive.
322 mkUnsafeCoercion :: Type -> Type -> Coercion
323 mkUnsafeCoercion ty1 ty2
324 = mkCoercion unsafeCoercionTyCon [ty1, ty2]
327 -- See note [Newtype coercions] in TyCon
328 mkNewTypeCoercion :: Name -> TyCon -> [TyVar] -> Type -> TyCon
329 mkNewTypeCoercion name tycon tvs rhs_ty
330 = mkCoercionTyCon name co_con_arity rule
332 co_con_arity = length tvs
334 rule args = ASSERT( co_con_arity == length args )
335 (TyConApp tycon args, substTyWith tvs args rhs_ty)
337 -- Coercion identifying a data/newtype/synonym representation type and its
338 -- family instance. It has the form `Co tvs :: F ts :=: R tvs', where `Co' is
339 -- the coercion tycon built here, `F' the family tycon and `R' the (derived)
340 -- representation tycon.
342 mkFamInstCoercion :: Name -- unique name for the coercion tycon
343 -> [TyVar] -- type parameters of the coercion (`tvs')
344 -> TyCon -- family tycon (`F')
345 -> [Type] -- type instance (`ts')
346 -> TyCon -- representation tycon (`R')
347 -> TyCon -- => coercion tycon (`Co')
348 mkFamInstCoercion name tvs family instTys rep_tycon
349 = mkCoercionTyCon name coArity rule
352 rule args = (substTyWith tvs args $ -- with sigma = [tys/tvs],
353 TyConApp family instTys, -- sigma (F ts)
354 TyConApp rep_tycon args) -- :=: R tys
356 --------------------------------------
357 -- Coercion Type Constructors...
359 -- Example. The coercion ((sym c) (sym d) (sym e))
360 -- will be represented by (TyConApp sym [c, sym d, sym e])
364 -- then ((sym c) (sym d) (sym e)) :: (p1 p2 p3)=(q1 q2 q3)
366 symCoercionTyCon, transCoercionTyCon, leftCoercionTyCon, rightCoercionTyCon, instCoercionTyCon, unsafeCoercionTyCon :: TyCon
367 -- Each coercion TyCon is built with the special CoercionTyCon record and
368 -- carries its own kinding rule. Such CoercionTyCons must be fully applied
369 -- by any TyConApp in which they are applied, however they may also be over
370 -- applied (see example above) and the kinding function must deal with this.
372 mkCoercionTyCon symCoercionTyConName 1 flipCoercionKindOf
374 flipCoercionKindOf (co:rest) = ASSERT( null rest ) (ty2, ty1)
376 (ty1, ty2) = coercionKind co
379 mkCoercionTyCon transCoercionTyConName 2 composeCoercionKindsOf
381 composeCoercionKindsOf (co1:co2:rest)
382 = ASSERT( null rest )
383 WARN( not (r1 `coreEqType` a2),
384 text "Strange! Type mismatch in trans coercion, probably a bug"
386 ppr r1 <+> text "=/=" <+> ppr a2)
389 (a1, r1) = coercionKind co1
390 (a2, r2) = coercionKind co2
393 mkCoercionTyCon leftCoercionTyConName 1 leftProjectCoercionKindOf
395 leftProjectCoercionKindOf (co:rest) = ASSERT( null rest ) (ty1, ty2)
397 (ty1,ty2) = fst (splitCoercionKindOf co)
400 mkCoercionTyCon rightCoercionTyConName 1 rightProjectCoercionKindOf
402 rightProjectCoercionKindOf (co:rest) = ASSERT( null rest ) (ty1, ty2)
404 (ty1,ty2) = snd (splitCoercionKindOf co)
406 splitCoercionKindOf :: Type -> ((Type,Type), (Type,Type))
407 -- Helper for left and right. Finds coercion kind of its input and
408 -- returns the left and right projections of the coercion...
410 -- if c :: t1 s1 :=: t2 s2 then splitCoercionKindOf c = ((t1, t2), (s1, s2))
411 splitCoercionKindOf co
412 | Just (ty1, ty2) <- splitCoercionKind_maybe (coercionKindPredTy co)
413 , Just (ty_fun1, ty_arg1) <- splitAppTy_maybe ty1
414 , Just (ty_fun2, ty_arg2) <- splitAppTy_maybe ty2
415 = ((ty_fun1, ty_fun2),(ty_arg1, ty_arg2))
416 splitCoercionKindOf co
417 = pprPanic "Coercion.splitCoercionKindOf"
418 (ppr co $$ ppr (coercionKindPredTy co))
421 = mkCoercionTyCon instCoercionTyConName 2 instCoercionKind
424 let Just (tv, ty) = splitForAllTy_maybe t in
425 substTyWith [tv] [s] ty
427 instCoercionKind (co1:ty:rest) = ASSERT( null rest )
428 (instantiateCo t1 ty, instantiateCo t2 ty)
429 where (t1, t2) = coercionKind co1
432 = mkCoercionTyCon unsafeCoercionTyConName 2 unsafeCoercionKind
434 unsafeCoercionKind (ty1:ty2:rest) = ASSERT( null rest ) (ty1,ty2)
436 --------------------------------------
437 -- ...and their names
439 mkCoConName :: FS.FastString -> Unique -> TyCon -> Name
440 mkCoConName occ key coCon = mkWiredInName gHC_PRIM (mkOccNameFS tcName occ)
441 key (ATyCon coCon) BuiltInSyntax
443 transCoercionTyConName, symCoercionTyConName, leftCoercionTyConName, rightCoercionTyConName, instCoercionTyConName, unsafeCoercionTyConName :: Name
445 transCoercionTyConName = mkCoConName FSLIT("trans") transCoercionTyConKey transCoercionTyCon
446 symCoercionTyConName = mkCoConName FSLIT("sym") symCoercionTyConKey symCoercionTyCon
447 leftCoercionTyConName = mkCoConName FSLIT("left") leftCoercionTyConKey leftCoercionTyCon
448 rightCoercionTyConName = mkCoConName FSLIT("right") rightCoercionTyConKey rightCoercionTyCon
449 instCoercionTyConName = mkCoConName FSLIT("inst") instCoercionTyConKey instCoercionTyCon
450 unsafeCoercionTyConName = mkCoConName FSLIT("CoUnsafe") unsafeCoercionTyConKey unsafeCoercionTyCon
454 instNewTyCon_maybe :: TyCon -> [Type] -> Maybe (Type, CoercionI)
455 -- instNewTyCon_maybe T ts
456 -- = Just (rep_ty, co) if co : T ts ~ rep_ty
457 instNewTyCon_maybe tc tys
458 | Just (tvs, ty, mb_co_tc) <- unwrapNewTyCon_maybe tc
459 = ASSERT( tys `lengthIs` tyConArity tc )
460 Just (substTyWith tvs tys ty,
463 Just co_tc -> ACo (mkTyConApp co_tc tys))
467 -- this is here to avoid module loops
468 splitNewTypeRepCo_maybe :: Type -> Maybe (Type, Coercion)
469 -- Sometimes we want to look through a newtype and get its associated coercion
470 -- It only strips *one layer* off, so the caller will usually call itself recursively
471 -- Only applied to types of kind *, hence the newtype is always saturated
472 -- splitNewTypeRepCo_maybe ty
473 -- = Just (ty', co) if co : ty ~ ty'
474 -- Returns Nothing for non-newtypes or fully-transparent newtypes
475 splitNewTypeRepCo_maybe ty
476 | Just ty' <- coreView ty = splitNewTypeRepCo_maybe ty'
477 splitNewTypeRepCo_maybe (TyConApp tc tys)
478 | Just (ty', coi) <- instNewTyCon_maybe tc tys
480 ACo co -> Just (ty', co)
481 IdCo -> panic "splitNewTypeRepCo_maybe"
482 -- This case handled by coreView
483 splitNewTypeRepCo_maybe _
488 --------------------------------------
489 -- CoercionI smart constructors
490 -- lifted smart constructors of ordinary coercions
493 -- CoercionI is either
494 -- (a) proper coercion
495 -- (b) the identity coercion
496 data CoercionI = IdCo | ACo Coercion
498 isIdentityCoercion :: CoercionI -> Bool
499 isIdentityCoercion IdCo = True
500 isIdentityCoercion _ = False
502 allIdCos :: [CoercionI] -> Bool
503 allIdCos = all isIdentityCoercion
505 zipCoArgs :: [CoercionI] -> [Type] -> [Coercion]
506 zipCoArgs cois tys = zipWith fromCoI cois tys
508 fromCoI :: CoercionI -> Type -> Type
509 fromCoI IdCo ty = ty -- Identity coercion represented
510 fromCoI (ACo co) _ = co -- by the type itself
512 mkSymCoI :: CoercionI -> CoercionI
514 mkSymCoI (ACo co) = ACo $ mkCoercion symCoercionTyCon [co]
515 -- the smart constructor
516 -- is too smart with tyvars
518 mkTransCoI :: CoercionI -> CoercionI -> CoercionI
519 mkTransCoI IdCo aco = aco
520 mkTransCoI aco IdCo = aco
521 mkTransCoI (ACo co1) (ACo co2) = ACo $ mkTransCoercion co1 co2
523 mkTyConAppCoI :: TyCon -> [Type] -> [CoercionI] -> CoercionI
524 mkTyConAppCoI tyCon tys cois
525 | allIdCos cois = IdCo
526 | otherwise = ACo (TyConApp tyCon (zipCoArgs cois tys))
528 mkAppTyCoI :: Type -> CoercionI -> Type -> CoercionI -> CoercionI
529 mkAppTyCoI _ IdCo _ IdCo = IdCo
530 mkAppTyCoI ty1 coi1 ty2 coi2 =
531 ACo $ AppTy (fromCoI coi1 ty1) (fromCoI coi2 ty2)
533 mkFunTyCoI :: Type -> CoercionI -> Type -> CoercionI -> CoercionI
534 mkFunTyCoI _ IdCo _ IdCo = IdCo
535 mkFunTyCoI ty1 coi1 ty2 coi2 =
536 ACo $ FunTy (fromCoI coi1 ty1) (fromCoI coi2 ty2)
538 mkNoteTyCoI :: TyNote -> CoercionI -> CoercionI
539 mkNoteTyCoI _ IdCo = IdCo
540 mkNoteTyCoI note (ACo co) = ACo $ NoteTy note co
542 mkForAllTyCoI :: TyVar -> CoercionI -> CoercionI
543 mkForAllTyCoI _ IdCo = IdCo
544 mkForAllTyCoI tv (ACo co) = ACo $ ForAllTy tv co
546 fromACo :: CoercionI -> Coercion
547 fromACo (ACo co) = co
549 mkClassPPredCoI :: Class -> [Type] -> [CoercionI] -> CoercionI
550 -- mkClassPPredCoI cls tys cois = coi
551 -- coi : PredTy (cls tys) ~ predTy (cls (tys `cast` cois))
552 mkClassPPredCoI cls tys cois
553 | allIdCos cois = IdCo
554 | otherwise = ACo $ PredTy $ ClassP cls (zipCoArgs cois tys)
556 mkIParamPredCoI :: (IPName Name) -> CoercionI -> CoercionI
557 -- Similar invariant to mkclassPPredCoI
558 mkIParamPredCoI _ IdCo = IdCo
559 mkIParamPredCoI ipn (ACo co) = ACo $ PredTy $ IParam ipn co
561 mkEqPredCoI :: Type -> CoercionI -> Type -> CoercionI -> CoercionI
562 -- Similar invariant to mkclassPPredCoI
563 mkEqPredCoI _ IdCo _ IdCo = IdCo
564 mkEqPredCoI ty1 IdCo _ (ACo co2) = ACo $ PredTy $ EqPred ty1 co2
565 mkEqPredCoI _ (ACo co1) ty2 coi2 = ACo $ PredTy $ EqPred co1 (fromCoI coi2 ty2)