Coercion

ghc-7.6.3: The GHC API

Safe Haskell | None

Coercion

Contents

- Main data type
  - Functions over coercions
  - Functions over coercion axioms
  - Constructing coercions
  - Decomposition
  - Coercion variables
  - Free variables
  - Substitution
  - Lifting
  - Comparison
  - Forcing evaluation of coercions
- Pretty-printing
- Other

Description

Module for (a) type kinds and (b) type coercions, as used in System FC. See `Expr` for more on System FC and how coercions fit into it.

Synopsis

- data Coercion
  - = Refl Type
  - | TyConAppCo TyCon [Coercion]
  - | AppCo Coercion Coercion
  - | ForAllCo TyVar Coercion
  - | CoVarCo CoVar
  - | AxiomInstCo CoAxiom [Coercion]
  - | UnsafeCo Type Type
  - | SymCo Coercion
  - | TransCo Coercion Coercion
  - | NthCo Int Coercion
  - | InstCo Coercion Type
- data Var
- type CoVar = Id
- coVarKind :: CoVar -> (Type, Type)
- coercionType :: Coercion -> Type
- coercionKind :: Coercion -> Pair Type
- coercionKinds :: [Coercion] -> Pair [Type]
- isReflCo :: Coercion -> Bool
- isReflCo_maybe :: Coercion -> Maybe Type
- mkCoercionType :: Type -> Type -> Type
- coAxiomSplitLHS :: CoAxiom -> (TyCon, [Type])
- mkReflCo :: Type -> Coercion
- mkCoVarCo :: CoVar -> Coercion
- mkAxInstCo :: CoAxiom -> [Type] -> Coercion
- mkAxInstRHS :: CoAxiom -> [Type] -> Type
- mkPiCo :: Var -> Coercion -> Coercion
- mkPiCos :: [Var] -> Coercion -> Coercion
- mkCoCast :: Coercion -> Coercion -> Coercion
- mkSymCo :: Coercion -> Coercion
- mkTransCo :: Coercion -> Coercion -> Coercion
- mkNthCo :: Int -> Coercion -> Coercion
- mkInstCo :: Coercion -> Type -> Coercion
- mkAppCo :: Coercion -> Coercion -> Coercion
- mkTyConAppCo :: TyCon -> [Coercion] -> Coercion
- mkFunCo :: Coercion -> Coercion -> Coercion
- mkForAllCo :: Var -> Coercion -> Coercion
- mkUnsafeCo :: Type -> Type -> Coercion
- mkNewTypeCo :: Name -> TyCon -> [TyVar] -> Type -> CoAxiom
- splitNewTypeRepCo_maybe :: Type -> Maybe (Type, Coercion)
- instNewTyCon_maybe :: TyCon -> [Type] -> Maybe (Type, Coercion)
- decomposeCo :: Arity -> Coercion -> [Coercion]
- getCoVar_maybe :: Coercion -> Maybe CoVar
- splitTyConAppCo_maybe :: Coercion -> Maybe (TyCon, [Coercion])
- splitAppCo_maybe :: Coercion -> Maybe (Coercion, Coercion)
- splitForAllCo_maybe :: Coercion -> Maybe (TyVar, Coercion)
- mkCoVar :: Name -> Type -> CoVar
- isCoVar :: Var -> Bool
- isCoVarType :: Type -> Bool
- coVarName :: CoVar -> Name
- setCoVarName :: CoVar -> Name -> CoVar
- setCoVarUnique :: CoVar -> Unique -> CoVar
- tyCoVarsOfCo :: Coercion -> VarSet
- tyCoVarsOfCos :: [Coercion] -> VarSet
- coVarsOfCo :: Coercion -> VarSet
- coercionSize :: Coercion -> Int
- type CvSubstEnv = VarEnv Coercion
- emptyCvSubstEnv :: CvSubstEnv
- data CvSubst = CvSubst InScopeSet TvSubstEnv CvSubstEnv
- emptyCvSubst :: CvSubst
- lookupTyVar :: CvSubst -> TyVar -> Maybe Type
- lookupCoVar :: CvSubst -> Var -> Maybe Coercion
- isEmptyCvSubst :: CvSubst -> Bool
- zapCvSubstEnv :: CvSubst -> CvSubst
- getCvInScope :: CvSubst -> InScopeSet
- substCo :: CvSubst -> Coercion -> Coercion
- substCos :: CvSubst -> [Coercion] -> [Coercion]
- substCoVar :: CvSubst -> CoVar -> Coercion
- substCoVars :: CvSubst -> [CoVar] -> [Coercion]
- substCoWithTy :: InScopeSet -> TyVar -> Type -> Coercion -> Coercion
- substCoWithTys :: InScopeSet -> [TyVar] -> [Type] -> Coercion -> Coercion
- cvTvSubst :: CvSubst -> TvSubst
- tvCvSubst :: TvSubst -> CvSubst
- mkCvSubst :: InScopeSet -> [(Var, Coercion)] -> CvSubst
- zipOpenCvSubst :: [Var] -> [Coercion] -> CvSubst
- substTy :: CvSubst -> Type -> Type
- extendTvSubst :: CvSubst -> TyVar -> Type -> CvSubst
- extendCvSubstAndInScope :: CvSubst -> CoVar -> Coercion -> CvSubst
- substTyVarBndr :: CvSubst -> TyVar -> (CvSubst, TyVar)
- substCoVarBndr :: CvSubst -> CoVar -> (CvSubst, CoVar)
- liftCoMatch :: TyVarSet -> Type -> Coercion -> Maybe LiftCoSubst
- liftCoSubstTyVar :: LiftCoSubst -> TyVar -> Maybe Coercion
- liftCoSubstWith :: [TyVar] -> [Coercion] -> Type -> Coercion
- coreEqCoercion :: Coercion -> Coercion -> Bool
- coreEqCoercion2 :: RnEnv2 -> Coercion -> Coercion -> Bool
- seqCo :: Coercion -> ()
- pprCo :: Coercion -> SDoc
- pprParendCo :: Coercion -> SDoc
- pprCoAxiom :: CoAxiom -> SDoc
- applyCo :: Type -> Coercion -> Type

# Main data type

data Coercion

A `Coercion` is concrete evidence of the equality/convertibility of two types.

Constructors

Refl Type |
TyConAppCo TyCon [Coercion] |
AppCo Coercion Coercion |
ForAllCo TyVar Coercion |
CoVarCo CoVar |
AxiomInstCo CoAxiom [Coercion] |
UnsafeCo Type Type |
SymCo Coercion |
TransCo Coercion Coercion |
NthCo Int Coercion |
InstCo Coercion Type |

Instances

Data Coercion |
Typeable Coercion |
Outputable Coercion |

data Var

Essentially a typed `Name`, that may also contain some additional information about the `Var` and it's use sites.

Instances

Eq Var |
Data Var |
Ord Var |
Typeable Var |
OutputableBndr Var |
Outputable Var |
Uniquable Var |
NamedThing Var |

type CoVar = Id

## Functions over coercions

coVarKind :: CoVar -> (Type, Type)

coercionType :: Coercion -> Type

coercionKind :: Coercion -> Pair Type

If it is the case that

     c :: (t1 ~ t2)

i.e. the kind of `c` relates `t1` and `t2`, then `coercionKind c = Pair t1 t2`.

coercionKinds :: [Coercion] -> Pair [Type]

Apply `coercionKind` to multiple `Coercion`s

isReflCo :: Coercion -> Bool

isReflCo_maybe :: Coercion -> Maybe Type

mkCoercionType :: Type -> Type -> Type

Makes a coercion type from two types: the types whose equality is proven by the relevant `Coercion`

## Functions over coercion axioms

coAxiomSplitLHS :: CoAxiom -> (TyCon, [Type])

## Constructing coercions

mkReflCo :: Type -> Coercion

mkCoVarCo :: CoVar -> Coercion

mkAxInstCo :: CoAxiom -> [Type] -> Coercion

mkAxInstRHS :: CoAxiom -> [Type] -> Type

mkPiCo :: Var -> Coercion -> Coercion

mkPiCos :: [Var] -> Coercion -> Coercion

mkCoCast :: Coercion -> Coercion -> Coercion

mkSymCo :: Coercion -> Coercion

Create a symmetric version of the given `Coercion` that asserts equality between the same types but in the other direction, so a kind of `t1 ~ t2` becomes the kind `t2 ~ t1`.

mkTransCo :: Coercion -> Coercion -> Coercion

Create a new `Coercion` by composing the two given `Coercion`s transitively.

mkNthCo :: Int -> Coercion -> Coercion

mkInstCo :: Coercion -> Type -> Coercion

Instantiates a `Coercion` with a `Type` argument.

mkAppCo :: Coercion -> Coercion -> Coercion

Apply a `Coercion` to another `Coercion`.

mkTyConAppCo :: TyCon -> [Coercion] -> Coercion

Apply a type constructor to a list of coercions.

mkFunCo :: Coercion -> Coercion -> Coercion

Make a function `Coercion` between two other `Coercion`s

mkForAllCo :: Var -> Coercion -> Coercion

Make a `Coercion` which binds a variable within an inner `Coercion`

mkUnsafeCo :: Type -> Type -> Coercion

Manufacture a coercion from thin air. Needless to say, this is not usually safe, but it is used when we know we are dealing with bottom, which is one case in which it is safe. This is also used to implement the `unsafeCoerce#` primitive. Optimise by pushing down through type constructors.

mkNewTypeCo :: Name -> TyCon -> [TyVar] -> Type -> CoAxiom

Create a coercion constructor (axiom) suitable for the given newtype `TyCon`. The `Name` should be that of a new coercion `CoAxiom`, the `TyVar`s the arguments expected by the `newtype` and the type the appropriate right hand side of the `newtype`, with the free variables a subset of those `TyVar`s.

## Decomposition

splitNewTypeRepCo_maybe :: Type -> Maybe (Type, Coercion)

Sometimes we want to look through a `newtype` and get its associated coercion. This function only strips *one layer* of `newtype` off, so the caller will usually call itself recursively. Furthermore, this function should only be applied to types of kind `*`, hence the newtype is always saturated. If `co : ty ~ ty'` then:

     splitNewTypeRepCo_maybe ty = Just (ty', co)

The function returns `Nothing` for non-`newtypes` or fully-transparent `newtype`s.

instNewTyCon_maybe :: TyCon -> [Type] -> Maybe (Type, Coercion)

If `co :: T ts ~ rep_ty` then:

     instNewTyCon_maybe T ts = Just (rep_ty, co)

decomposeCo :: Arity -> Coercion -> [Coercion]

This breaks a `Coercion` with type `T A B C ~ T D E F` into a list of `Coercion`s of kinds `A ~ D`, `B ~ E` and `E ~ F`. Hence:

     decomposeCo 3 c = [nth 0 c, nth 1 c, nth 2 c]

getCoVar_maybe :: Coercion -> Maybe CoVar

Attempts to obtain the type variable underlying a `Coercion`

splitTyConAppCo_maybe :: Coercion -> Maybe (TyCon, [Coercion])

Attempts to tease a coercion apart into a type constructor and the application of a number of coercion arguments to that constructor

splitAppCo_maybe :: Coercion -> Maybe (Coercion, Coercion)

Attempt to take a coercion application apart.

splitForAllCo_maybe :: Coercion -> Maybe (TyVar, Coercion)

## Coercion variables

mkCoVar :: Name -> Type -> CoVar

isCoVar :: Var -> Bool

isCoVarType :: Type -> Bool

coVarName :: CoVar -> Name

setCoVarName :: CoVar -> Name -> CoVar

setCoVarUnique :: CoVar -> Unique -> CoVar

## Free variables

tyCoVarsOfCo :: Coercion -> VarSet

tyCoVarsOfCos :: [Coercion] -> VarSet

coVarsOfCo :: Coercion -> VarSet

coercionSize :: Coercion -> Int

## Substitution

type CvSubstEnv = VarEnv Coercion

A substitution of `Coercion`s for `CoVar`s (OR `TyVar`s, when doing a "lifting" substitution)

emptyCvSubstEnv :: CvSubstEnv

data CvSubst

Constructors

CvSubst InScopeSet TvSubstEnv CvSubstEnv |

Instances

Outputable CvSubst |

emptyCvSubst :: CvSubst

lookupTyVar :: CvSubst -> TyVar -> Maybe Type

lookupCoVar :: CvSubst -> Var -> Maybe Coercion

isEmptyCvSubst :: CvSubst -> Bool

zapCvSubstEnv :: CvSubst -> CvSubst

getCvInScope :: CvSubst -> InScopeSet

substCo :: CvSubst -> Coercion -> Coercion

Substitute within a `Coercion`

substCos :: CvSubst -> [Coercion] -> [Coercion]

Substitute within several `Coercion`s

substCoVar :: CvSubst -> CoVar -> Coercion

substCoVars :: CvSubst -> [CoVar] -> [Coercion]

substCoWithTy :: InScopeSet -> TyVar -> Type -> Coercion -> Coercion

substCoWithTys :: InScopeSet -> [TyVar] -> [Type] -> Coercion -> Coercion

cvTvSubst :: CvSubst -> TvSubst

tvCvSubst :: TvSubst -> CvSubst

mkCvSubst :: InScopeSet -> [(Var, Coercion)] -> CvSubst

zipOpenCvSubst :: [Var] -> [Coercion] -> CvSubst

substTy :: CvSubst -> Type -> Type

extendTvSubst :: CvSubst -> TyVar -> Type -> CvSubst

extendCvSubstAndInScope :: CvSubst -> CoVar -> Coercion -> CvSubst

substTyVarBndr :: CvSubst -> TyVar -> (CvSubst, TyVar)

substCoVarBndr :: CvSubst -> CoVar -> (CvSubst, CoVar)

## Lifting

liftCoMatch :: TyVarSet -> Type -> Coercion -> Maybe LiftCoSubst

`liftCoMatch` is sort of inverse to `liftCoSubst`. In particular, if `liftCoMatch vars ty co == Just s`, then `tyCoSubst s ty == co`. That is, it matches a type against a coercion of the same shape, and returns a lifting substitution which could have been used to produce the given coercion from the given type.

liftCoSubstTyVar :: LiftCoSubst -> TyVar -> Maybe Coercion

liftCoSubstWith :: [TyVar] -> [Coercion] -> Type -> Coercion

## Comparison

coreEqCoercion :: Coercion -> Coercion -> Bool

Determines syntactic equality of coercions

coreEqCoercion2 :: RnEnv2 -> Coercion -> Coercion -> Bool

## Forcing evaluation of coercions

seqCo :: Coercion -> ()

# Pretty-printing

pprCo :: Coercion -> SDoc

pprParendCo :: Coercion -> SDoc

pprCoAxiom :: CoAxiom -> SDoc

# Other

applyCo :: Type -> Coercion -> Type