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, mkDataInstCoercion, 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,
40 coreEqType, splitAppTys, isTyVarTy, splitTyConApp_maybe
42 import TyCon ( TyCon, tyConArity, mkCoercionTyCon, isClosedNewTyCon,
43 newTyConRhs, newTyConCo_maybe,
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
53 import Util ( lengthIs, snocView )
54 import Unique ( hasKey )
55 import BasicTypes ( Arity )
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
69 go n co cos = go (n-1) (mkLeftCoercion co)
70 (mkRightCoercion co : cos)
72 ------------------------------
74 -------------------------------------------------------
75 -- and some coercion kind stuff
77 isEqPredTy (PredTy pred) = isEqPred pred
78 isEqPredTy other = False
80 mkEqPred :: (Type, Type) -> PredType
81 mkEqPred (ty1, ty2) = EqPred ty1 ty2
83 getEqPredTys :: PredType -> (Type,Type)
84 getEqPredTys (EqPred ty1 ty2) = (ty1, ty2)
85 getEqPredTys other = pprPanic "getEqPredTys" (ppr other)
87 mkCoKind :: Type -> Type -> CoercionKind
88 mkCoKind ty1 ty2 = PredTy (EqPred ty1 ty2)
90 mkReflCoKind :: Type -> CoercionKind
91 mkReflCoKind ty = mkCoKind ty ty
93 splitCoercionKind :: CoercionKind -> (Type, Type)
94 splitCoercionKind co | Just co' <- kindView co = splitCoercionKind co'
95 splitCoercionKind (PredTy (EqPred ty1 ty2)) = (ty1, ty2)
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
102 isCoVar :: Var -> Bool
103 isCoVar tv = isTyVar tv && isCoercionKind (tyVarKind tv)
106 type CoercionKind = Kind -- A CoercionKind is always of form (ty1 :=: ty2)
108 coercionKind :: Coercion -> (Type, Type)
110 -- Then (coercionKind c) = (t1,t2)
111 coercionKind ty@(TyVarTy a) | isCoVar a = splitCoercionKind (tyVarKind a)
112 | otherwise = (ty, ty)
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 = ASSERT( length args >= ar ) -- Always saturated
121 let (ty1,ty2) = rule (take ar args) -- Apply the rule to the right number of args
122 (tys1, tys2) = coercionKinds (drop ar args)
123 in (mkAppTys ty1 tys1, mkAppTys ty2 tys2)
126 = let (lArgs, rArgs) = coercionKinds args in
127 (TyConApp tc lArgs, TyConApp tc rArgs)
128 coercionKind (FunTy ty1 ty2)
129 = let (t1, t2) = coercionKind ty1
130 (s1, s2) = coercionKind ty2 in
131 (mkFunTy t1 s1, mkFunTy t2 s2)
132 coercionKind (ForAllTy tv ty)
133 = let (ty1, ty2) = coercionKind ty in
134 (ForAllTy tv ty1, ForAllTy tv ty2)
135 coercionKind (NoteTy _ ty) = coercionKind ty
136 coercionKind (PredTy (EqPred c1 c2))
137 = let k1 = coercionKindPredTy c1
138 k2 = coercionKindPredTy c2 in
140 coercionKind (PredTy (ClassP cl args))
141 = let (lArgs, rArgs) = coercionKinds args in
142 (PredTy (ClassP cl lArgs), PredTy (ClassP cl rArgs))
143 coercionKind (PredTy (IParam name ty))
144 = let (ty1, ty2) = coercionKind ty in
145 (PredTy (IParam name ty1), PredTy (IParam name ty2))
147 coercionKindPredTy :: Coercion -> CoercionKind
148 coercionKindPredTy c = let (t1, t2) = coercionKind c in mkCoKind t1 t2
150 coercionKinds :: [Coercion] -> ([Type], [Type])
151 coercionKinds tys = unzip $ map coercionKind tys
153 -------------------------------------
154 -- Coercion kind and type mk's
155 -- (make saturated TyConApp CoercionTyCon{...} args)
157 mkCoercion coCon args = ASSERT( tyConArity coCon == length args )
160 mkAppCoercion, mkFunCoercion, mkTransCoercion, mkInstCoercion :: Coercion -> Coercion -> Coercion
161 mkSymCoercion, mkLeftCoercion, mkRightCoercion :: Coercion -> Coercion
163 mkAppCoercion co1 co2 = mkAppTy co1 co2
164 mkAppsCoercion co1 tys = foldl mkAppTy co1 tys
165 -- note that a TyVar should be used here, not a CoVar (nor a TcTyVar)
166 mkForAllCoercion tv co = ASSERT ( isTyVar tv ) mkForAllTy tv co
167 mkFunCoercion co1 co2 = mkFunTy co1 co2
170 -- This smart constructor creates a sym'ed version its argument,
171 -- but tries to push the sym's down to the leaves. If we come to
172 -- sym tv or sym tycon then we can drop the sym because tv and tycon
173 -- are reflexive coercions
175 | Just co2 <- splitSymCoercion_maybe co = co2
176 -- sym (sym co) --> co
177 | Just (co1, arg_tys) <- splitTyConApp_maybe co
178 , not (isCoercionTyCon co1) = mkTyConApp co1 (map mkSymCoercion arg_tys)
179 -- we can drop the sym for a TyCon
180 -- sym (ty [t1, ..., tn]) --> ty [sym t1, ..., sym tn]
181 | (co1, arg_tys) <- splitAppTys co
182 , isTyVarTy co1 = mkAppTys (maybe_drop co1) (map mkSymCoercion arg_tys)
183 -- sym (tv [t1, ..., tn]) --> tv [sym t1, ..., sym tn]
184 -- if tv type variable
185 -- sym (cv [t1, ..., tn]) --> (sym cv) [sym t1, ..., sym tn]
186 -- if cv is a coercion variable
187 -- fall through if head is a CoercionTyCon
188 | Just (co1, co2) <- splitTransCoercion_maybe co
189 -- sym (co1 `trans` co2) --> (sym co2) `trans (sym co2)
190 = mkTransCoercion (mkSymCoercion co2) (mkSymCoercion co1)
191 | Just (co, ty) <- splitInstCoercion_maybe co
192 -- sym (co @ ty) --> (sym co) @ ty
193 = mkInstCoercion (mkSymCoercion co) ty
194 | Just co <- splitLeftCoercion_maybe co
195 -- sym (left co) --> left (sym co)
196 = mkLeftCoercion (mkSymCoercion co)
197 | Just co <- splitRightCoercion_maybe co
198 -- sym (right co) --> right (sym co)
199 = mkRightCoercion (mkSymCoercion co)
201 maybe_drop (TyVarTy tv)
202 | isCoVar tv = mkCoercion symCoercionTyCon [TyVarTy tv]
203 | otherwise = TyVarTy tv
204 maybe_drop other = other
205 mkSymCoercion (ForAllTy tv ty) = ForAllTy tv (mkSymCoercion ty)
206 -- for atomic types and constructors, we can just ignore sym since these
207 -- are reflexive coercions
208 mkSymCoercion (TyVarTy tv)
209 | isCoVar tv = mkCoercion symCoercionTyCon [TyVarTy tv]
210 | otherwise = TyVarTy tv
211 mkSymCoercion co = mkCoercion symCoercionTyCon [co]
213 -- Smart constructors for left and right
215 | Just (co', _) <- splitAppCoercion_maybe co = co'
216 | otherwise = mkCoercion leftCoercionTyCon [co]
219 | Just (co1, co2) <- splitAppCoercion_maybe co = co2
220 | otherwise = mkCoercion rightCoercionTyCon [co]
222 mkTransCoercion co1 co2 = mkCoercion transCoercionTyCon [co1, co2]
224 mkInstCoercion co ty = mkCoercion instCoercionTyCon [co, ty]
226 mkInstsCoercion co tys = foldl mkInstCoercion co tys
228 splitSymCoercion_maybe :: Coercion -> Maybe Coercion
229 splitSymCoercion_maybe (TyConApp tc [co]) =
230 if tc `hasKey` symCoercionTyConKey
233 splitSymCoercion_maybe co = Nothing
235 splitAppCoercion_maybe :: Coercion -> Maybe (Coercion, Coercion)
236 -- Splits a coercion application, being careful *not* to split (left c), etc
237 -- which are really sytactic constructs, not applications
238 splitAppCoercion_maybe co | Just co' <- coreView co = splitAppCoercion_maybe co'
239 splitAppCoercion_maybe (FunTy ty1 ty2) = Just (TyConApp funTyCon [ty1], ty2)
240 splitAppCoercion_maybe (AppTy ty1 ty2) = Just (ty1, ty2)
241 splitAppCoercion_maybe (TyConApp tc tys)
242 | not (isCoercionTyCon tc)
243 = case snocView tys of
244 Just (tys', ty') -> Just (TyConApp tc tys', ty')
246 splitAppCoercion_maybe co = Nothing
248 splitTransCoercion_maybe :: Coercion -> Maybe (Coercion, Coercion)
249 splitTransCoercion_maybe (TyConApp tc [ty1, ty2])
250 = if tc `hasKey` transCoercionTyConKey then
254 splitTransCoercion_maybe other = Nothing
256 splitInstCoercion_maybe :: Coercion -> Maybe (Coercion, Type)
257 splitInstCoercion_maybe (TyConApp tc [ty1, ty2])
258 = if tc `hasKey` instCoercionTyConKey then
262 splitInstCoercion_maybe other = Nothing
264 splitLeftCoercion_maybe :: Coercion -> Maybe Coercion
265 splitLeftCoercion_maybe (TyConApp tc [co])
266 = if tc `hasKey` leftCoercionTyConKey then
270 splitLeftCoercion_maybe other = Nothing
272 splitRightCoercion_maybe :: Coercion -> Maybe Coercion
273 splitRightCoercion_maybe (TyConApp tc [co])
274 = if tc `hasKey` rightCoercionTyConKey then
278 splitRightCoercion_maybe other = Nothing
280 -- Unsafe coercion is not safe, it is used when we know we are dealing with
281 -- bottom, which is one case in which it is safe. It is also used to
282 -- implement the unsafeCoerce# primitive.
283 mkUnsafeCoercion :: Type -> Type -> Coercion
284 mkUnsafeCoercion ty1 ty2
285 = mkCoercion unsafeCoercionTyCon [ty1, ty2]
288 -- See note [Newtype coercions] in TyCon
289 mkNewTypeCoercion :: Name -> TyCon -> ([TyVar], Type) -> TyCon
290 mkNewTypeCoercion name tycon (tvs, rhs_ty)
291 = mkCoercionTyCon name co_con_arity rule
293 co_con_arity = length tvs
295 rule args = ASSERT( co_con_arity == length args )
296 (TyConApp tycon args, substTyWith tvs args rhs_ty)
298 -- Coercion identifying a data/newtype representation type and its family
299 -- instance. It has the form `Co tvs :: F ts :=: R tvs', where `Co' is the
300 -- coercion tycon built here, `F' the family tycon and `R' the (derived)
301 -- representation tycon.
303 mkDataInstCoercion :: Name -- unique name for the coercion tycon
304 -> [TyVar] -- type parameters of the coercion (`tvs')
305 -> TyCon -- family tycon (`F')
306 -> [Type] -- type instance (`ts')
307 -> TyCon -- representation tycon (`R')
308 -> TyCon -- => coercion tycon (`Co')
309 mkDataInstCoercion name tvs family instTys rep_tycon
310 = mkCoercionTyCon name coArity rule
313 rule args = (substTyWith tvs args $ -- with sigma = [tys/tvs],
314 TyConApp family instTys, -- sigma (F ts)
315 TyConApp rep_tycon args) -- :=: R tys
317 --------------------------------------
318 -- Coercion Type Constructors...
320 -- Example. The coercion ((sym c) (sym d) (sym e))
321 -- will be represented by (TyConApp sym [c, sym d, sym e])
325 -- then ((sym c) (sym d) (sym e)) :: (p1 p2 p3)=(q1 q2 q3)
327 symCoercionTyCon, transCoercionTyCon, leftCoercionTyCon, rightCoercionTyCon, instCoercionTyCon :: TyCon
328 -- Each coercion TyCon is built with the special CoercionTyCon record and
329 -- carries its own kinding rule. Such CoercionTyCons must be fully applied
330 -- by any TyConApp in which they are applied, however they may also be over
331 -- applied (see example above) and the kinding function must deal with this.
333 mkCoercionTyCon symCoercionTyConName 1 flipCoercionKindOf
335 flipCoercionKindOf (co:rest) = ASSERT( null rest ) (ty2, ty1)
337 (ty1, ty2) = coercionKind co
340 mkCoercionTyCon transCoercionTyConName 2 composeCoercionKindsOf
342 composeCoercionKindsOf (co1:co2:rest)
343 = ASSERT( null rest )
344 WARN( not (r1 `coreEqType` a2), text "Strange! Type mismatch in trans coercion, probably a bug")
347 (a1, r1) = coercionKind co1
348 (a2, r2) = coercionKind co2
351 mkCoercionTyCon leftCoercionTyConName 1 leftProjectCoercionKindOf
353 leftProjectCoercionKindOf (co:rest) = ASSERT( null rest ) (ty1, ty2)
355 (ty1,ty2) = fst (splitCoercionKindOf co)
358 mkCoercionTyCon rightCoercionTyConName 1 rightProjectCoercionKindOf
360 rightProjectCoercionKindOf (co:rest) = ASSERT( null rest ) (ty1, ty2)
362 (ty1,ty2) = snd (splitCoercionKindOf co)
364 splitCoercionKindOf :: Type -> ((Type,Type), (Type,Type))
365 -- Helper for left and right. Finds coercion kind of its input and
366 -- returns the left and right projections of the coercion...
368 -- if c :: t1 s1 :=: t2 s2 then splitCoercionKindOf c = ((t1, t2), (s1, s2))
369 splitCoercionKindOf co
370 | Just (ty1, ty2) <- splitCoercionKind_maybe (coercionKindPredTy co)
371 , Just (ty_fun1, ty_arg1) <- splitAppTy_maybe ty1
372 , Just (ty_fun2, ty_arg2) <- splitAppTy_maybe ty2
373 = ((ty_fun1, ty_fun2),(ty_arg1, ty_arg2))
376 = mkCoercionTyCon instCoercionTyConName 2 instCoercionKind
379 let Just (tv, ty) = splitForAllTy_maybe t in
380 substTyWith [tv] [s] ty
382 instCoercionKind (co1:ty:rest) = ASSERT( null rest )
383 (instantiateCo t1 ty, instantiateCo t2 ty)
384 where (t1, t2) = coercionKind co1
387 = mkCoercionTyCon unsafeCoercionTyConName 2 unsafeCoercionKind
389 unsafeCoercionKind (ty1:ty2:rest) = ASSERT( null rest ) (ty1,ty2)
391 --------------------------------------
392 -- ...and their names
394 mkCoConName occ key coCon = mkWiredInName gHC_PRIM (mkOccNameFS tcName occ)
395 key Nothing (ATyCon coCon) BuiltInSyntax
397 transCoercionTyConName = mkCoConName FSLIT("trans") transCoercionTyConKey transCoercionTyCon
398 symCoercionTyConName = mkCoConName FSLIT("sym") symCoercionTyConKey symCoercionTyCon
399 leftCoercionTyConName = mkCoConName FSLIT("left") leftCoercionTyConKey leftCoercionTyCon
400 rightCoercionTyConName = mkCoConName FSLIT("right") rightCoercionTyConKey rightCoercionTyCon
401 instCoercionTyConName = mkCoConName FSLIT("inst") instCoercionTyConKey instCoercionTyCon
402 unsafeCoercionTyConName = mkCoConName FSLIT("CoUnsafe") unsafeCoercionTyConKey unsafeCoercionTyCon
406 -- this is here to avoid module loops
407 splitNewTypeRepCo_maybe :: Type -> Maybe (Type, Coercion)
408 -- Sometimes we want to look through a newtype and get its associated coercion
409 -- It only strips *one layer* off, so the caller will usually call itself recursively
410 -- Only applied to types of kind *, hence the newtype is always saturated
411 splitNewTypeRepCo_maybe ty
412 | Just ty' <- coreView ty = splitNewTypeRepCo_maybe ty'
413 splitNewTypeRepCo_maybe (TyConApp tc tys)
414 | isClosedNewTyCon tc
415 = ASSERT( tys `lengthIs` tyConArity tc ) -- splitNewTypeRepCo_maybe only be applied
416 -- to *types* (of kind *)
417 case newTyConRhs tc of
419 ASSERT( length tvs == length tys )
420 Just (substTyWith tvs tys rep_ty, mkTyConApp co_con tys)
422 co_con = maybe (pprPanic "splitNewTypeRepCo_maybe" (ppr tc)) id (newTyConCo_maybe tc)
423 splitNewTypeRepCo_maybe other = Nothing