HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Moduleghc-typelits-natnormalise-0.7.10Haskell2010

GHC.TypeLits.Normalise.Unify

  • 5 types
  • 16 values

Nat expressions <-> SOP terms

6 declarations
newtypenewtype CType
#

Constructors

Instances3Eq, Ord, Outputable
  • Eq CTypeDefined in ghc-typelits-natnormalise-0.7.10 · GHC.TypeLits.Normalise.Unify
  • Ord CTypeDefined in ghc-typelits-natnormalise-0.7.10 · GHC.TypeLits.Normalise.Unify
  • Outputable CTypeDefined in ghc-typelits-natnormalise-0.7.10 · GHC.TypeLits.Normalise.Unify
valuenormaliseNat :: Type -> Writer [(Type, Type)] CoreSOP
#

Convert a type of kind Nat to an SOP term, but only when the type is constructed out of:

  • literals

  • type variables

  • Applications of the arithmetic operators (+,-,*,^)

Substitution on SOP terms

4 declarations

A substitution is essentially a list of (variable, SOP) pairs, but we keep the original Ct that lead to the substitution being made, for use when turning the substitution back into constraints.

Find unifiers

3 declarations
datadata UnifyResult
#

Result of comparing two SOP terms, returning a potential substitution list under which the two terms are equal.

Constructors

  • Win

    Two terms are equal

  • Lose

    Two terms are not equal

  • Draw [CoreUnify]

    Two terms are only equal if the given substitution holds

Instances1Outputable

Given two SOPs u and v, when their free variables (fvSOP) are the same, then we Win if u and v are equal, and Lose otherwise.

If u and v do not have the same free variables, we result in a Draw, ware u and v are only equal when the returned CoreSubst holds.

valueunifiers :: Ct -> CoreSOP -> CoreSOP -> [CoreUnify]
#

Find unifiers for two SOP terms

Can find the following unifiers:

t ~ a + b          ==>  [t := a + b]
a + b ~ t          ==>  [t := a + b]
(a + c) ~ (b + c)  ==>  a := b
(2*a) ~ (2*b)      ==>  [a := b]
(2 + a) ~ 5        ==>  [a := 3]
(i * a) ~ j        ==>  [a := div j i], when (mod j i == 0)

However, given a wanted:

[W] t ~ a + b

this function returns [], or otherwise we "solve" the constraint by finding a unifier equal to the constraint.

However, given a wanted:

[W] (a + c) ~ (b + c)

we do return the unifier:

[a := b]

Free variables in SOP terms

1 declaration

Inequalities

6 declarations

Subtract an inequality, in order to either:

  • See if the smallest solution is a natural number

  • Cancel sums, i.e. monotonicity of addition

subtractIneq (2*y <=? 3*x ~ True)  = (-2*y + 3*x)
subtractIneq (2*y <=? 3*x ~ False) = (-3*x + (-1) + 2*y)
valuesolveIneq
  1. :: Word

    Solving depth

  2. -> Ineq

    Inequality we want to solve

  3. -> Ineq

    Given/proven inequality

  4. -> WriterT (Set CType) Maybe Bool

    Solver result

    • Nothing: exhausted solver steps

    • Just True: inequality is solved

    • Just False: solver is unable to solve inequality, note that this does not mean the wanted inequality does not hold.

#

Try to solve inequalities

valueinstantSolveIneq
  1. :: Word

    Solving depth

  2. -> Ineq

    Inequality we want to solve

  3. -> WriterT (Set CType) Maybe Bool
#

Try to instantly solve an inequality by using the inequality solver using 1 <=? 1 ~ True as the given constraint.

Properties

1 declaration