2 Module for type coercions, as in System FC.
4 Coercions are represented as types, and their kinds tell what types the
7 The coercion kind constructor is a special TyCon that must always be saturated
9 typeKind (symCoercion type) :: TyConApp CoercionTyCon{...} [type, type]
15 mkCoKind, mkReflCoKind, splitCoercionKind_maybe, splitCoercionKind,
16 coercionKind, coercionKinds, coercionKindPredTy,
18 -- Equality predicates
19 isEqPred, mkEqPred, getEqPredTys, isEqPredTy,
21 -- Coercion transformations
22 mkSymCoercion, mkTransCoercion,
23 mkLeftCoercion, mkRightCoercion, mkInstCoercion, mkAppCoercion,
24 mkForAllCoercion, mkFunCoercion, mkInstsCoercion, mkUnsafeCoercion,
25 mkNewTypeCoercion, mkAppsCoercion,
27 splitNewTypeRepCo_maybe, decomposeCo,
29 unsafeCoercionTyCon, symCoercionTyCon,
30 transCoercionTyCon, leftCoercionTyCon,
31 rightCoercionTyCon, instCoercionTyCon -- needed by TysWiredIn
34 #include "HsVersions.h"
37 import Type ( Type, Kind, PredType, substTyWith, mkAppTy, mkForAllTy,
38 mkFunTy, splitAppTy_maybe, splitForAllTy_maybe, coreView,
39 kindView, mkTyConApp, isCoercionKind, isEqPred, mkAppTys
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
52 import Util ( lengthIs, snocView )
53 import Unique ( hasKey )
54 import BasicTypes ( Arity )
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
68 go n co cos = go (n-1) (mkLeftCoercion co)
69 (mkRightCoercion co : cos)
71 ------------------------------
73 -------------------------------------------------------
74 -- and some coercion kind stuff
76 isEqPredTy (PredTy pred) = isEqPred pred
77 isEqPredTy other = False
79 mkEqPred :: (Type, Type) -> PredType
80 mkEqPred (ty1, ty2) = EqPred ty1 ty2
82 getEqPredTys :: PredType -> (Type,Type)
83 getEqPredTys (EqPred ty1 ty2) = (ty1, ty2)
84 getEqPredTys other = pprPanic "getEqPredTys" (ppr other)
86 mkCoKind :: Type -> Type -> CoercionKind
87 mkCoKind ty1 ty2 = PredTy (EqPred ty1 ty2)
89 mkReflCoKind :: Type -> CoercionKind
90 mkReflCoKind ty = mkCoKind ty ty
92 splitCoercionKind :: CoercionKind -> (Type, Type)
93 splitCoercionKind co | Just co' <- kindView co = splitCoercionKind co'
94 splitCoercionKind (PredTy (EqPred ty1 ty2)) = (ty1, ty2)
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
101 isCoVar :: Var -> Bool
102 isCoVar tv = isTyVar tv && isCoercionKind (tyVarKind tv)
105 type CoercionKind = Kind -- A CoercionKind is always of form (ty1 :=: ty2)
107 coercionKind :: Coercion -> (Type, Type)
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)
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
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))
145 coercionKindPredTy :: Coercion -> CoercionKind
146 coercionKindPredTy c = let (t1, t2) = coercionKind c in mkCoKind t1 t2
148 coercionKinds :: [Coercion] -> ([Type], [Type])
149 coercionKinds tys = unzip $ map coercionKind tys
151 -------------------------------------
152 -- Coercion kind and type mk's
153 -- (make saturated TyConApp CoercionTyCon{...} args)
155 mkCoercion coCon args = ASSERT( tyConArity coCon == length args )
158 mkAppCoercion, mkFunCoercion, mkTransCoercion, mkInstCoercion :: Coercion -> Coercion -> Coercion
159 mkSymCoercion, mkLeftCoercion, mkRightCoercion :: Coercion -> Coercion
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
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
189 -- Smart constructors for left and right
191 | Just (co', _) <- splitAppCoercion_maybe co = co'
192 | otherwise = mkCoercion leftCoercionTyCon [co]
195 | Just (co1, co2) <- splitAppCoercion_maybe co = co2
196 | otherwise = mkCoercion rightCoercionTyCon [co]
198 mkTransCoercion co1 co2 = mkCoercion transCoercionTyCon [co1, co2]
200 mkInstCoercion co ty = mkCoercion instCoercionTyCon [co, ty]
202 mkInstsCoercion co tys = foldl mkInstCoercion co tys
204 splitSymCoercion_maybe :: Coercion -> Maybe Coercion
205 splitSymCoercion_maybe (TyConApp tc [co]) =
206 if tc `hasKey` symCoercionTyConKey
209 splitSymCoercion_maybe co = Nothing
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')
222 splitAppCoercion_maybe co = Nothing
224 splitTransCoercion_maybe :: Coercion -> Maybe (Coercion, Coercion)
225 splitTransCoercion_maybe (TyConApp tc [ty1, ty2])
226 = if tc `hasKey` transCoercionTyConKey then
230 splitTransCoercion_maybe other = Nothing
232 splitInstCoercion_maybe :: Coercion -> Maybe (Coercion, Type)
233 splitInstCoercion_maybe (TyConApp tc [ty1, ty2])
234 = if tc `hasKey` instCoercionTyConKey then
238 splitInstCoercion_maybe other = Nothing
240 splitLeftCoercion_maybe :: Coercion -> Maybe Coercion
241 splitLeftCoercion_maybe (TyConApp tc [co])
242 = if tc `hasKey` leftCoercionTyConKey then
246 splitLeftCoercion_maybe other = Nothing
248 splitRightCoercion_maybe :: Coercion -> Maybe Coercion
249 splitRightCoercion_maybe (TyConApp tc [co])
250 = if tc `hasKey` rightCoercionTyConKey then
254 splitRightCoercion_maybe other = Nothing
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]
264 -- Make the coercion associated with a newtype. If we have
266 -- newtype T a b = MkT (Int, a, b)
268 -- Then (mkNewTypeCoercion CoT T [a,b] (Int, a, b)) creates the coercion
269 -- CoT, such kinding rule such that
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
277 rule args = mkCoKind (substTyWith tvs args rhs_ty) (TyConApp tycon args)
279 --------------------------------------
280 -- Coercion Type Constructors...
282 -- Example. The coercion ((sym c) (sym d) (sym e))
283 -- will be represented by (TyConApp sym [c, sym d, sym e])
287 -- then ((sym c) (sym d) (sym e)) :: (p1 p2 p3)=(q1 q2 q3)
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)
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.
303 mkCoercionTyCon symCoercionTyConName 1 (mkKindingFun flipCoercionKindOf)
305 flipCoercionKindOf (co:rest) = (ty2, ty1, rest)
307 (ty1, ty2) = coercionKind co
310 mkCoercionTyCon transCoercionTyConName 2 (mkKindingFun composeCoercionKindsOf)
312 composeCoercionKindsOf (co1:co2:rest) = (a1, r2, rest)
314 (a1, r1) = coercionKind co1
315 (a2, r2) = coercionKind co2
318 mkCoercionTyCon leftCoercionTyConName 1 (mkKindingFun leftProjectCoercionKindOf)
320 leftProjectCoercionKindOf (co:rest) = (ty1, ty2, rest)
322 (ty1,ty2) = fst (splitCoercionKindOf co)
325 mkCoercionTyCon rightCoercionTyConName 1 (mkKindingFun rightProjectCoercionKindOf)
327 rightProjectCoercionKindOf (co:rest) = (ty1, ty2, rest)
329 (ty1,ty2) = snd (splitCoercionKindOf co)
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...
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))
343 = mkCoercionTyCon instCoercionTyConName 2 (mkKindingFun instCoercionKind)
346 let Just (tv, ty) = splitForAllTy_maybe t in
347 substTyWith [tv] [s] ty
349 instCoercionKind (co1:ty:rest) = (instantiateCo t1 ty, instantiateCo t2 ty, rest)
350 where (t1, t2) = coercionKind co1
353 = mkCoercionTyCon unsafeCoercionTyConName 2 (mkKindingFun unsafeCoercionKind)
355 unsafeCoercionKind (ty1:ty2:rest) = (ty1,ty2,rest)
357 --------------------------------------
358 -- ...and their names
360 mkCoConName occ key coCon = mkWiredInName gHC_PRIM (mkOccNameFS tcName occ)
361 key Nothing (ATyCon coCon) BuiltInSyntax
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
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)
381 = ASSERT( tys `lengthIs` tyConArity tc ) -- splitNewTypeRepCo_maybe only be applied
382 -- to *types* (of kind *)
383 case newTyConRhs tc of
385 ASSERT( length tvs == length tys )
386 Just (substTyWith tvs tys rep_ty, mkTyConApp co_con tys)
388 co_con = maybe (pprPanic "splitNewTypeRepCo_maybe" (ppr tc)) id (newTyConCo tc)
389 splitNewTypeRepCo_maybe other = Nothing