HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Moduleghc-9.10.3GHC2021

GHC.Core.Unify

  • 5 types
  • 22 values
  • Packageghc-9.10.3
  • Exports27
  • LanguageGHC2021
  • LicenceBSD-3-Clause
  • SourceUnify.hs
valuetcMatchTy :: Type -> Type -> Maybe Subst
#

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]

valuetcMatchTys
  1. :: [Type]

    Template

  2. -> [Type]

    Target

  3. -> Maybe Subst

    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]

valuetcUnifyTy :: Type -> Type -> Maybe Subst
#

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

valuetcUnifyTysFG :: BindFun -> [Type] -> [Type] -> UnifyResult
#

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.

valuetcUnifyTyWithTFs
  1. :: Bool

    True = do two-way unification; False = do one-way matching. See end of sec 5.2 from the paper

  2. -> InScopeSet
  3. -> Type
  4. -> Type
  5. -> Maybe Subst
#

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

typetype BindFun = TyCoVar -> Type -> BindFlag
#

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.

datadata BindFlag
#

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.

Instances1Eq
  • Eq BindFlagDefined in ghc-9.10.3 · GHC.Core.Unify

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)
datadata MaybeApartReason
#

Why are two types MaybeApart? MARInfinite takes precedence: This is used (only) in Note [Infinitary substitution in lookup] in GHC.Core.InstEnv As of Feb 2022, we never differentiate between MARTypeFamily and MARTypeVsConstraint; it's really only MARInfinite that's interesting here.

Constructors

Instances2Semigroup, Outputable
valuetypesCantMatch :: [(Type, Type)] -> Bool
#

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

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.