Normalising and Solving Type Equalities

The following is based on ideas for the new, post-ICFP'08 solving algorithm described in CVS papers/type-synonym/new-single.tex. A revised version of new-single.tex that integrates the core ideas from this wiki page is in papers/type-synonym/normalised_equations_algorithm.tex. Most of the code is in the module TcTyFuns.

Terminology

Wanted equality

An equality constraint that we need to derive during type checking. Failure to derive it leads to rejection of the checked program.

Local equality, given equality

An equality constraint that -in a certain scope- may be used to derive wanted equalities.

Flexible type variable, unification variable, HM variable

Type variables that may be globally instantiated by unification.

Rigid type variable, skolem type variable

Type variable that cannot be globally instantiated, but it may be locally refined by a local equality constraint.

Overall algorithm

The overall structure is as in new-single.tex, namely

normalise all constraints (both locals and wanteds),

solve the wanteds, and

finalise.

However, the three phases differ in important ways. In particular, normalisation includes decompositions & the occurs check, and we don't instantiate any flexible type variables before we finalise (i.e., solving is purely local).

Normal equalities

Central to the algorithm are normal equalities, which can be regarded as a set of rewrite rules. Normal equalities are carefully oriented and contain synonym families only as the head symbols of left-hand sides. They assume one of the following three forms:

Rigid variable equality:co :: a ~ t, where a is a rigid type variable (skolem) and t is not a flexible type variable.

where

the types t, t1, ..., tn may not contain any occurrences of synonym families and

left-hand side of an equality may not occur in the right-hand side.

The second bullet of the where clause is trivially true for equalities of Form (1) and it implies that the left- and right-hand sides are different.

Observations

The following is interesting to note:

We explicitly permit equalities of the form x ~ y and a ~ b, where both sides are either flexible or rigid type variables.

Normal equalities are similar to equalities meeting the Orientation Invariant and Flattening Invariant of new-single, but they are not the same.

Normal equalities are never self-recursive. They can be mutually recursive. A mutually recursive group will exclusively contain variable equalities.

Coercions

Coercions co are either wanteds (represented by a flexible type variable) or givens aka locals (represented by a type term of kind CO). In GHC, they are represented by TcRnTypes.EqInstCo, which is defined as

type EqInstCo = Either
TcTyVar -- case for wanteds (variable to be filled with a witness)
Coercion -- case for locals

Whenever an equality of Form (2) or (3) would be recursive, the program can be rejected on the basis of a failed occurs check. (Immediate rejection is always justified, as right-hand sides do not contain synonym familles; hence, any recursive occurrences of a left-hand side imply that the equality is unsatisfiable.)

Use flexible tyvars for flattening of locals, too. (We have flexibles in locals anyway and don't use (Unify) on locals, so the flexibles shouldn't cause any harm, but the elimination of skolems is much easier for flexibles - we just instantiate them.)

TODO Do we need to record a skolem elimination substitution for skolems introduced during flattening for wanteds, too?

Solving

TODO

Rules applying to family equalities:

IdenticalLHS only applies to family equalities (both local and wanteds) - MAYBE only when at least one of the equalities is a local (see email).

Top only applies to family equalities (both locals and wanteds)

We should apply IdenticalLHS first as it cheaper and potentially reduces the number of applications of Top.

Rules applying to variable equalities:

Unify only applies to all wanted flexible variable equalities

Subst (= local for variable equalities) applies to all variable equalities, where local variable equalities are substituted into all equalities, but wanted variable equalities are only substituted into wanted equalities.

We should first apply Unify where possible and, only when that is exhausted, apply Subst. This is as Unify is cheaper and has global impact (instantiates variables in the environment, too).

Notes:

(Unify) is an asymmetric rule, and hence, only fires for equalities of the form x ~ c, where c is free of synonym families. Moreover, it only applies to wanted equalities. (Rationale: Local equality constraints don't justify global instantiation of flexible type variables - just as in new-single.)

(Local) only substitutes normal variable equalities - and currently also only local equalities, not wanteds. (We should call this Rule (Subst) again.)

In principle, a rewrite rule could be discarded after an exhaustive application of (Local). However, while the set of class constraints is kept separate, we may always have some occurrences of the supposedly eliminated variable in a class constraint, and hence, need to keep all local equalities around.

(IdenticalLHS) I don't think it is useful to apply that rule when both equalities are wanted, which makes it a variant of (Local). BUT as SLPJ points out what about F a ~ x1, F a ~ Int? Then we could unify the HM variable x with Int.

Observations

Rule (Local) -substituting variable equalities- is the most expensive rule as it needs to traverse all type terms.

Only (Local) when replacing a variable in the left-hand side of an equality of Form (1) can lead to recursion with (Top).

Termination

SkolemOccurs

The Note [skolemOccurs loop] in the old code explains that equalities of the form x ~ t (where x is a flexible type variable) may not be used as rewrite rules, but only be solved by applying Rule Unify. As Unify carefully avoids cycles, this prevents the use of equalities introduced by the Rule SkolemOccurs as rewrite rules. For this to work, SkolemOccurs also had to apply to equalities of the form a ~ t[[a]]. This was a somewhat intricate set up that we seek to simplify here. Whether equalities of the form x ~ t are used as rewrite rules or solved by Unify doesn't matter anymore. Instead, we disallow recursive equalities after normalisation completely (both locals and wanteds). This is possible as right-hand sides are free of synonym families.

To look at this in more detail, let's consider the following notorious example:

E_t: forall x. F [x] ~ [F x]
[F v] ~ v ||- [F v] ~ v

New-single: The following derivation shows how the algorithm in new-single fails to terminate for this example.

New-single using flexible tyvars to flatten locals, but w/o Rule (Local) for flexible type variables: With (SkolemOccurs) it is crucial to avoid using Rule (Local) with flexible type variables. We can achieve a similar effect with new-single if we (a) use flexible type variables to flatten local equalities and (b) at the same time do not use Rule (Local) for variable equalities with flexible type variables. NB: Point (b) was necessary for the ICFP'08 algorithm, too.

A serious disadvantage of this approach is that we do want to use Rule (Local) with flexible type variables as soon as we have rank-n signatures. In fact, the lack of doing so is responsible for a few failing tests in the testsuite in the GHC implementation of (SkolemOccurs).

De-prioritise Rule (Local): Instead of outright forbidding the use of Rule (Local) with flexible type variables, we can simply require that Local is only used if no other rule is applicable. (That has the same effect on satisfiable queries, and in particular, the present example.)