GHC.Core.Unify

ghc-9.2.5: The GHC API

Safe Haskell | Safe-Inferred
Language | Haskell2010

GHC.Core.Unify

Contents

- Rough matching

Synopsis

- tcMatchTy :: Type -> Type -> Maybe TCvSubst
- tcMatchTyKi :: Type -> Type -> Maybe TCvSubst
- tcMatchTys :: [Type] -> [Type] -> Maybe TCvSubst
- tcMatchTyKis :: [Type] -> [Type] -> Maybe TCvSubst
- tcMatchTyX :: TCvSubst -> Type -> Type -> Maybe TCvSubst
- tcMatchTysX :: TCvSubst -> [Type] -> [Type] -> Maybe TCvSubst
- tcMatchTyKisX :: TCvSubst -> [Type] -> [Type] -> Maybe TCvSubst
- tcMatchTyX_BM :: BindFun -> TCvSubst -> Type -> Type -> Maybe TCvSubst
- ruleMatchTyKiX :: TyCoVarSet -> RnEnv2 -> TvSubstEnv -> Type -> Type -> Maybe TvSubstEnv
- data RoughMatchTc
  - = KnownTc Name
  - | OtherTc
- roughMatchTcs :: [Type] -> [RoughMatchTc]
- instanceCantMatch :: [RoughMatchTc] -> [RoughMatchTc] -> Bool
- typesCantMatch :: [(Type, Type)] -> Bool
- isRoughOtherTc :: RoughMatchTc -> Bool
- tcUnifyTy :: Type -> Type -> Maybe TCvSubst
- tcUnifyTyKi :: Type -> Type -> Maybe TCvSubst
- tcUnifyTys :: BindFun -> [Type] -> [Type] -> Maybe TCvSubst
- tcUnifyTyKis :: BindFun -> [Type] -> [Type] -> Maybe TCvSubst
- tcUnifyTysFG :: BindFun -> [Type] -> [Type] -> UnifyResult
- tcUnifyTyWithTFs :: Bool -> Type -> Type -> Maybe TCvSubst
- type BindFun = TyCoVar -> Type -> BindFlag
- data BindFlag
  - = BindMe
  - | Apart
- matchBindFun :: TyCoVarSet -> BindFun
- alwaysBindFun :: BindFun
- type UnifyResult = UnifyResultM TCvSubst
- data UnifyResultM a
  - = Unifiable a
  - | MaybeApart MaybeApartReason a
  - | SurelyApart
- data MaybeApartReason
  - = MARTypeFamily
  - | MARInfinite
- liftCoMatch :: TyCoVarSet -> Type -> Coercion -> Maybe LiftingContext
- flattenTys :: InScopeSet -> [Type] -> [Type]
- flattenTysX :: InScopeSet -> [Type] -> ([Type], TyVarEnv (TyCon, [Type]))

# Documentation

tcMatchTy :: Type -> Type -> Maybe TCvSubst Source #

`tcMatchTy t1 t2` produces a substitution (over fvs(t1)) `s` such that `s(t1)` equals `t2`. The returned substitution might bind coercion variables, if the variable is an argument to a GADT constructor.

Precondition: typeKind ty1 `eqType` typeKind ty2

We don't pass in a set of "template variables" to be bound by the match, because tcMatchTy (and similar functions) are always used on top-level types, so we can bind any of the free variables of the LHS. See also Note [tcMatchTy vs tcMatchTyKi]

tcMatchTyKi :: Type -> Type -> Maybe TCvSubst Source #

Like `tcMatchTy`, but allows the kinds of the types to differ, and thus matches them as well. See also Note [tcMatchTy vs tcMatchTyKi]

tcMatchTys Source #

Arguments

:: [Type] | Template
-> [Type] | Target
-> Maybe TCvSubst | One-shot; in principle the template variables could be free in the target

Like `tcMatchTy` but over a list of types. See also Note [tcMatchTy vs tcMatchTyKi]

tcMatchTyKis Source #

Arguments

:: [Type] | Template
-> [Type] | Target
-> Maybe TCvSubst | One-shot substitution

Like `tcMatchTyKi` but over a list of types. See also Note [tcMatchTy vs tcMatchTyKi]

tcMatchTyX Source #

Arguments

:: TCvSubst | Substitution to extend
-> Type | Template
-> Type | Target
-> Maybe TCvSubst |

This is similar to `tcMatchTy`, but extends a substitution See also Note [tcMatchTy vs tcMatchTyKi]

tcMatchTysX Source #

Arguments

:: TCvSubst | Substitution to extend
-> [Type] | Template
-> [Type] | Target
-> Maybe TCvSubst | One-shot substitution

Like `tcMatchTys`, but extending a substitution See also Note [tcMatchTy vs tcMatchTyKi]

tcMatchTyKisX Source #

Arguments

:: TCvSubst | Substitution to extend
-> [Type] | Template
-> [Type] | Target
-> Maybe TCvSubst | One-shot substitution

Like `tcMatchTyKis`, but extending a substitution See also Note [tcMatchTy vs tcMatchTyKi]

tcMatchTyX_BM :: BindFun -> TCvSubst -> Type -> Type -> Maybe TCvSubst Source #

ruleMatchTyKiX Source #

Arguments

:: TyCoVarSet | template variables
-> RnEnv2 |
-> TvSubstEnv | type substitution to extend
-> Type | Template
-> Type | Target
-> Maybe TvSubstEnv |

This one is called from the expression matcher, which already has a MatchEnv in hand

# Rough matching

data RoughMatchTc Source #

Constructors

KnownTc Name |
OtherTc |

#### Instances

Instances details

Data RoughMatchTc Source # |
Instance details Defined in GHC.Core.Unify Methods gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> RoughMatchTc -> c RoughMatchTc Source # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c RoughMatchTc Source # toConstr :: RoughMatchTc -> Constr Source # dataTypeOf :: RoughMatchTc -> DataType Source # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c RoughMatchTc) Source # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c RoughMatchTc) Source # gmapT :: (forall b. Data b => b -> b) -> RoughMatchTc -> RoughMatchTc Source # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> RoughMatchTc -> r Source # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> RoughMatchTc -> r Source # gmapQ :: (forall d. Data d => d -> u) -> RoughMatchTc -> [u] Source # gmapQi :: Int -> (forall d. Data d => d -> u) -> RoughMatchTc -> u Source # gmapM :: Monad m => (forall d. Data d => d -> m d) -> RoughMatchTc -> m RoughMatchTc Source # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> RoughMatchTc -> m RoughMatchTc Source # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> RoughMatchTc -> m RoughMatchTc Source #

roughMatchTcs :: [Type] -> [RoughMatchTc] Source #

instanceCantMatch :: [RoughMatchTc] -> [RoughMatchTc] -> Bool Source #

typesCantMatch :: [(Type, Type)] -> Bool Source #

Given a list of pairs of types, are any two members of a pair surely apart, even after arbitrary type function evaluation and substitution?

isRoughOtherTc :: RoughMatchTc -> Bool Source #

tcUnifyTy :: Type -> Type -> Maybe TCvSubst Source #

Simple unification of two types; all type variables are bindable Precondition: the kinds are already equal

tcUnifyTyKi :: Type -> Type -> Maybe TCvSubst Source #

Like `tcUnifyTy`, but also unifies the kinds

tcUnifyTys Source #

Arguments

:: BindFun |
-> [Type] |
-> [Type] |
-> Maybe TCvSubst | A regular one-shot (idempotent) substitution that unifies the erased types. See comments for `tcUnifyTysFG`

tcUnifyTyKis :: BindFun -> [Type] -> [Type] -> Maybe TCvSubst Source #

Like `tcUnifyTys` but also unifies the kinds

tcUnifyTysFG :: BindFun -> [Type] -> [Type] -> UnifyResult Source #

`tcUnifyTysFG bind_tv tys1 tys2` attempts to find a substitution `s` (whose domain elements all respond `BindMe` to `bind_tv`) such that `s(tys1)` and that of `s(tys2)` are equal, as witnessed by the returned Coercions. This version requires that the kinds of the types are the same, if you unify left-to-right.

tcUnifyTyWithTFs Source #

Arguments

:: Bool | True = do two-way unification; False = do one-way matching. See end of sec 5.2 from the paper
-> Type |
-> Type |
-> Maybe TCvSubst |

Unify two types, treating type family applications as possibly unifying with anything and looking through injective type family applications. Precondition: kinds are the same

type BindFun = TyCoVar -> Type -> BindFlag Source #

Some unification functions are parameterised by a `BindFun`, which says whether or not to allow a certain unification to take place. A `BindFun` takes the `TyVar` involved along with the `Type` it will potentially be bound to.

It is possible for the variable to actually be a coercion variable (Note [Matching coercion variables]), but only when one-way matching. In this case, the `Type` will be a `CoercionTy`.

data BindFlag Source #

Constructors

BindMe | A regular type variable
Apart | Declare that this type variable is apart from the type provided. That is, the type variable will never be instantiated to that type. See also Note [Binding when looking up instances] in GHC.Core.InstEnv.

#### Instances

Instances details

Eq BindFlag Source # |
Instance details Defined in GHC.Core.Unify Methods (==) :: BindFlag -> BindFlag -> Bool # (/=) :: BindFlag -> BindFlag -> Bool #

matchBindFun :: TyCoVarSet -> BindFun Source #

Allow binding only for any variable in the set. Variables may be bound to any type. Used when doing simple matching; e.g. can we find a substitution

    S = [a :-> t1, b :-> t2] such that
        S( Maybe (a, b->Int )  =   Maybe (Bool, Char -> Int)

alwaysBindFun :: BindFun Source #

Allow the binding of any variable to any type

type UnifyResult = UnifyResultM TCvSubst Source #

data UnifyResultM a Source #

See Note [Unificiation result]

Constructors

Unifiable a |
MaybeApart MaybeApartReason a |
SurelyApart |

#### Instances

Instances details

Applicative UnifyResultM Source # |
Instance details Defined in GHC.Core.Unify Methods pure :: a -> UnifyResultM a Source # (<*>) :: UnifyResultM (a -> b) -> UnifyResultM a -> UnifyResultM b Source # liftA2 :: (a -> b -> c) -> UnifyResultM a -> UnifyResultM b -> UnifyResultM c Source # (*>) :: UnifyResultM a -> UnifyResultM b -> UnifyResultM b Source # (<*) :: UnifyResultM a -> UnifyResultM b -> UnifyResultM a Source #
Functor UnifyResultM Source # |
Instance details Defined in GHC.Core.Unify Methods fmap :: (a -> b) -> UnifyResultM a -> UnifyResultM b Source # (<$) :: a -> UnifyResultM b -> UnifyResultM a Source #
Monad UnifyResultM Source # |
Instance details Defined in GHC.Core.Unify Methods (>>=) :: UnifyResultM a -> (a -> UnifyResultM b) -> UnifyResultM b Source # (>>) :: UnifyResultM a -> UnifyResultM b -> UnifyResultM b Source # return :: a -> UnifyResultM a Source #
Outputable a => Outputable (UnifyResultM a) Source # |
Instance details Defined in GHC.Core.Unify Methods ppr :: UnifyResultM a -> SDoc Source #

data MaybeApartReason Source #

Why are two types `MaybeApart`? `MARTypeFamily` takes precedence: This is used (only) in Note [Infinitary substitution in lookup] in GHC.Core.InstEnv

Constructors

MARTypeFamily | matching e.g. F Int ~? Bool
MARInfinite | matching e.g. a ~? Maybe a

#### Instances

Instances details

Semigroup MaybeApartReason Source # |
Instance details Defined in GHC.Core.Unify Methods (<>) :: MaybeApartReason -> MaybeApartReason -> MaybeApartReason Source # sconcat :: NonEmpty MaybeApartReason -> MaybeApartReason Source # stimes :: Integral b => b -> MaybeApartReason -> MaybeApartReason Source #
Outputable MaybeApartReason Source # |
Instance details Defined in GHC.Core.Unify Methods ppr :: MaybeApartReason -> SDoc Source #

liftCoMatch :: TyCoVarSet -> Type -> Coercion -> Maybe LiftingContext Source #

`liftCoMatch` is sort of inverse to `liftCoSubst`. In particular, if `liftCoMatch vars ty co == Just s`, then `liftCoSubst s ty == co`, where `==` there means that the result of `liftCoSubst` has the same type as the original co; but may be different under the hood. 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. Note that this function is incomplete -- it might return Nothing when there does indeed exist a possible lifting context.

This function is incomplete in that it doesn't respect the equality in `eqType`. That is, it's possible that this will succeed for t1 and fail for t2, even when t1 `eqType` t2. That's because it depends on there being a very similar structure between the type and the coercion. This incompleteness shouldn't be all that surprising, especially because it depends on the structure of the coercion, which is a silly thing to do.

The lifting context produced doesn't have to be exacting in the roles of the mappings. This is because any use of the lifting context will also require a desired role. Thus, this algorithm prefers mapping to nominal coercions where it can do so.

flattenTys :: InScopeSet -> [Type] -> [Type] Source #

flattenTysX :: InScopeSet -> [Type] -> ([Type], TyVarEnv (TyCon, [Type])) Source #