Skip to content

Project heterogeneous constraints (v2) - #5234

Draft
vlopezj wants to merge 125 commits into
masterfrom
future-hetcon-no-compat
Draft

vlopezj wants to merge 125 commits into
masterfrom
future-hetcon-no-compat

Conversation

@vlopezj

@vlopezj vlopezj commented Feb 25, 2021 •

Copy link
Copy Markdown
Collaborator

This pull request supersedes #4605.

What?

Constraints in Agda are currently of the form:

Γ ⊢ t ≈ u : A

These constraints are homogeneous, which means that:

  • Each variable in the context has a single type.
  • Both t and u have type A.

When unifying a Π-types (Γ ⊢ ΠAB ≈ ΠA′B′ type) we obtain the following two constraints:

  1. Γ ⊢ A ≈ A′ type
  2. Γ,_α ⊢ B ≈ B′ type

What the type of _α should be is not obvious:

  • We can wait until the first constraint is solved. But this may be too restrictive, specially in Agda which only produces locally unique solutions.
  • We can leave _α as an uninterpreted constant (c.f. Norell (2007)). This solves some, but not all problems (e.g. Internal error in the presence of unsatisfiable constraints #3027), and is the current approach in Agda.
  • We can take _α := A, but this means that the right-hand side may be ill-typed.
  • We can take _α := A′, but this means that the right-hand side may be ill-typed.

Regarding the last two, this pull request can be summarized as:
“¿Por qué no los dos?” (Gundry (and McBride) (2013), López Juan and Danielsson (2020)).

How?

This pull request affects conversion checking. Other parts of the code-base, such as term reduction, unification for pattern matching, Auto, the size solver, etc. are unaffected.

The most important changes to the infrastructure are described in this gist.
For the theory and motivation behind those changes, I suggest this short paper.

Main improvements

Before merging/What doesn't work

This pull request is quite large and complex, so a merge rather than a rebase would be called for.

Here are the points that need to be address and/or acknowledged before this can be merged:

  • Projection likeness: In the current implementation, only one side of the constraint is used to compute the type of a projection-like function. Using both sides in practice is tricky; like a snake biting its own tail.
  • Std-lib: In one file, some implicit arguments now need to be given explicitly.
    See agda/agda-stdlib@d00ba75
  • Updated test cases: Some test cases have been updated.
  • Merging the current Agda: This pull request is based on commit 8f24ee6b. The last commit from master that was merged 47179128 (January 7th). Some conflicting changes (not to the Conversion module, luckily) have been done since.
  • Cubical: The cubical code for conversion is not aware of twin
    types. For instance, it can tackle a constraint of type Γ ⊢ t ≈ u : Path A, but what about a heterogeneous constraint of type Γ ⊢ t : Path A ≈ u : Path A'?
    Also, context variables, etc. Currently, cubical-related code that unexpectedly encounters a twin type will crash with an __UNIMPLEMENTED__ error.
    Some experiments in separate branches show that the a big part of the cubical library code (except for one definition in one module) can still type-check by (unsoundly?) replacing twin-types by uninterpreted constants, as was done before.
  • Sizes: A couple of test cases fail in a mysterious way. Also, sizes are related to subtyping; the point below applies in that respect.
  • Subtyping: Twin types are underpinned by equality constraints. Subtyping constraints are tackled in a somewhat ad-hoc manner, which seems to work.
  • Sorts: Each side of a twin type has a separate sort. When the sort of a variable of twin type is requested, the sort of the smallest side is returned. This is somewhat ad-hoc, but seems to work.
  • Naming: The names of some functions and types (e.g. Het, TwinT) may not be the most intuitive.
  • Pattern synonyms: Support for completeness declarations of pattern synonyms is buggy or non-existent before GHC 8.8. Thus, pattern completeness checks have been disabled for those versions of GHC.
  • Auto: Auto is currently assuming that it runs on a single-sided context; but I don't know if this is the case.

2021-08-27

Here is a summary of the work that would absolutely need to be done before this PR can be merged into master:

  • Further testing
    The pull request has been tested with agda-prelude, agda-stdlib, and HoTT-Intro. It should also be tested with other large projects, among them:
    • Martin Escardo's TypeTopology There is a point at which type-checking does not continue, possibly due to an unsolved metavariable which suddenly renders type-checking very slow.
    • The agda-categories library. I was unable to test it due to it using a different version of the standard library than the one the current base commit was tested on.
    • …
  • Merging with latest Agda release
    This PR is based on a January 2020 commit (4717912820a7). It should be brought up to date with the latest release.
  • Support for Cubical Agda
    Type-checking Cubical Agda involves novel constraint manipulations which, as currently written, don't take into account the existence of twin types. At least the following operations need to be adapted in order to support constraints and contexts with twin types:
    • Comparing mGlue, mHComp and mSub headed neutral terms.
    • Comparing Paths, where the type of the constraint is a twin type with different Path types on each side (similar to how λ abstractions can be compared even if their types have different domains and codomains).
    • Comparing terms of type isOne …, where the type of the constraint is a twin type with differing parameters to isOne on each side.
    • Manipulation of the context by forallFaceMaps.
    • …
  • Updating existing Agda user code
    Due to the more stringent preconditions for assigning metavariables, there are cases of existing well-typed programs in which some implicit arguments would now need to be given explicitly. These users should be warned so that they can adapt their code.

rwe and others added 30 commits July 24, 2020 10:24
Fixes #4803.

Previously only the Windows job was uploading that binary, which meant
that the `install_agda_user.sh` script would fail.
Displays the concerned instances in case of an inconsistence
instances error.
GHC 8.0 does not support the {-# COMPLETE #-} pragma,
leading to false positives.
@nad

nad commented Mar 11, 2021

Copy link
Copy Markdown
Collaborator

I could build 57e31fe using GHC 8.10.4.

I'd like to test some of my code, but the standard library (agda/agda-stdlib@d66a21f) is not accepted by this version of Agda.

@vlopezj

vlopezj commented Mar 11, 2021

Copy link
Copy Markdown
Collaborator Author

@nad You can try the version of the standard library that this version of Agda is built against:
agda/agda-stdlib@d00ba75

@nad

nad commented Mar 11, 2021

Copy link
Copy Markdown
Collaborator

The following code is accepted by Agda 2.6.2-f9a1816, but rejected by 57e31fe:

variable
  A B : Set
  P   : A → Set
  x y : A

_$_ : ((x : A) → P x) → ((x : A) → P x)
f $ x = f x

postulate
  _≡_       : A → A → Set
  refl      : (x : A) → x ≡ x
  elim      : (P : {x y : A} → x ≡ y → Set) →
              (∀ x → P (refl x)) →
              (x≡y : x ≡ y) → P x≡y
  elim-refl : (P : {x y : A} → x ≡ y → Set)
              (r : ∀ x → P (refl x)) →
              elim P r (refl x) ≡ r x
  cong      : (f : A → B) → x ≡ y → f x ≡ f y

subst : (P : A → Set) → x ≡ y → P x → P y
subst P = elim (λ {u v} _ → P u → P v) (λ _ p → p)

subst-refl : (P : A → Set) (p : P x) → subst P (refl x) p ≡ p
subst-refl _ p = cong (_$ p) (elim-refl _ _)
Failed to solve the following constraints:
  _P_121 (refl _x_120) = (x : P x₁) → P x₁ : Set
    (blocked on _P_121, belongs to problem 133)
  _y_113 p = p : P x₁ alternatively P x₁ pending [130]
    (blocked on _y_113)
  _x_112 p = elim (λ {u} {v} _ → P u → P v) (λ _ p₁ → p₁) (refl x₁) p
    : P x₁
    (blocked on _x_112)
  elim _P_121 _r_122 (refl _x_120) = _x_112
    : _P_121 (refl _x_120) alternatively (x : P x₁) →
                                         P x₁ pending [133]
    (blocked on any(_x_112, _P_121, problem 133),
     belongs to problem 134)
  _r_122 _x_120 = _y_113
    : _P_121 (refl _x_120) alternatively (x : P x₁) →
                                         P x₁ pending [133, 134]
    (blocked on any(all(problem 133, problem 134), _y_113, _P_121,
                    _r_122))

@vlopezj

vlopezj commented Mar 12, 2021

Copy link
Copy Markdown
Collaborator Author

For the record, the regression was introduced from d41a104 to 88e00e0 .

The crux of the issue lies in the following two constraints:

elim _P_121 _r_122 (refl _x_120) = _x_112
: _P_121 (refl _x_120) alternatively (x : P x₁) →
P x₁ pending [133]
(blocked on any(_x_112, _P_121, problem 133),
belongs to problem 134)

_r_122 _x_120 = _y_113
: _P_121 (refl _x_120) alternatively (x : P x₁) →
P x₁ pending [133, 134]
(blocked on any(all(problem 133, problem 134), _y_113, _P_121,
_r_122))

In Agda, _x_112 and _y_13 are instantiated without issues, and this enables the other metavariables to be solved.
Here are the constraints in all their glory:
constraints.txt

With heterogeneous unification, this instantiation is potentially unsound (as the types of the LHS and RHS do not match).

A work around is to give the first argument of elim-refl explicitly.

subst-refl : (P : A → Set) (p : P x) → subst P (refl x) p ≡ p
subst-refl P p = cong (_$ p) (elim-refl (λ {u v} _ → P u → P v) _)

This type checks with 57e31fe.

@nad

nad commented Mar 12, 2021

Copy link
Copy Markdown
Collaborator

With heterogeneous unification, this instantiation is potentially unsound (as the types of the LHS and RHS do not match).

A work around is to give the first argument of elim-refl explicitly.

That might be annoying, I encountered unsolved metavariables for a number of pieces of similar-looking code.

@vlopezj

vlopezj commented Mar 12, 2021

Copy link
Copy Markdown
Collaborator Author

That might be annoying, I encountered unsolved metavariables for a number of pieces of similar-looking code.

One possibility is to add a flag which, using the new constraint prioritization mechanism, will perform instantiations of metavariables which are only well-typed modulo as a last resort, and emit a warning when doing so. But it's kind of a hack.

@nad

nad commented Mar 12, 2021

Copy link
Copy Markdown
Collaborator

One possibility is to add a flag which, using the new constraint prioritization mechanism, will perform instantiations of metavariables which are only well-typed modulo as a last resort, and emit a warning when doing so. But it's kind of a hack.

Are the solutions that Agda comes up with at the moment incorrect?

@nad

nad commented May 6, 2021

Copy link
Copy Markdown
Collaborator

I tried to make one of my developments work with this branch (8fb611e). This failed, because Cubical Agda is not supported yet, but I made more than 20000 lines (wc -l) of code work with this version of Agda. In one (long) file I made 16 changes, but in total I could get away with 21 changes. I guess about one change per 1000 lines of code is acceptable.

Once support for Cubical Agda is added I might see how much work the remaining ∼60000 lines of code require, and I have a bunch of other developments as well.

@nad

nad commented Mar 30, 2022

Copy link
Copy Markdown
Collaborator

I think we should try to get this pull request into a state in which it can be merged before the code bit-rots too much. If I understand correctly the main problem relates to Cubical Agda, and there seems to be some code somewhere which addresses that problem, seemingly by using the hack that is currently in use everywhere:

Some experiments in separate branches show that the a big part of the cubical library code (except for one definition in one module) can still type-check by (unsoundly?) replacing twin-types by uninterpreted constants, as was done before.

@vlopezj, can you give us access to that code, and explain how it relates to the future-hetcon-no-compat branch?

@nad

nad commented May 3, 2022

Copy link
Copy Markdown
Collaborator

@vlopezj, can you give us access to that code, and explain how it relates to the future-hetcon-no-compat branch?

@vlopezj, did you see this?

This change was done in collaboration with Andreas Abel.
@nad
nad force-pushed the future-hetcon-no-compat branch from 15f86ca to 456a32a Compare May 18, 2022 14:57
@nad

nad commented May 18, 2022 •

Copy link
Copy Markdown
Collaborator

I brought the code up to date. As part of that I made some changes to the standard library (@vlopezj had already fixed one of the problems before, in a different way). I also disabled some test cases. The following ones do not make use of Cubical Agda:

  • examples/TT.agda
  • test/Succeed/Issue1592.agda
  • test/Succeed/SST.agda

These three test cases, as well as one standard library file that I changed, use mutual recursion. I constructed a small test case from the standard library code:

{-# OPTIONS --without-K #-}

P : Set → Set₁

Q : (A : Set) → P A → Set₁
Q _ _ = Set

E : Set₁
E = (A : Set) (x : P A) → Q _ x

P _ = Set

We get the following error message:

(P A) !=< (P _A_3)
when checking that the expression x has type P _A_3

This code should not be rejected, because it is accepted if Q _ x is replaced by Q A x.

Previously Agda.TypeChecking.Records.expandRecordVar was commented out, but this procedure is now used in Agda.TypeChecking.Rules.LHS.Unify.LeftInverse.buildEquiv, so I restored the commented-out code without knowing if the restored code is correct.

Some things that remain to be done before the code can be merged:

  • Investigate and perhaps fix the problem related to mutual recursion.
  • Re-enable disabled test cases (and make sure that they are handled correctly).
  • Add support for Cubical Agda. One could start by repeating an experiment @vlopezj performed, in his words, "replacing the type of either the constraint or of the context variables by a blocked constant at those points in Conversion.hs where it is checked whether a type is of the form of one of the cubical-Agda-specific types".
  • Review the changes. (I saw several occurrences of "TODO".)

@nad
nad force-pushed the future-hetcon-no-compat branch from 456a32a to c56058d Compare May 18, 2022 15:14
Some test cases were disabled:

* examples/TT.agda
* test/Fail/InjectivityHITs.agda
* test/Fail/Issue5237.agda
* test/Fail/Issue5838.agda
* test/Fail/Issue5838b.agda
* test/Succeed/InjectivityHITs.agda
* test/Succeed/Issue1592.agda
* test/Succeed/Issue5576.agda
* test/Succeed/Issue5579.agda
* test/Succeed/SST.agda
* test/Compiler/simple/Erased-cubical.agda

Furthermore some changes were made to the standard library.

Previously Agda.TypeChecking.Records.expandRecordVar was commented
out, but this procedure is used in
Agda.TypeChecking.Rules.LHS.Unify.LeftInverse.buildEquiv, so the
commented-out code was restored. It is not known to the author of this
commit whether the restored code is correct.
@nad
nad force-pushed the future-hetcon-no-compat branch from c56058d to f7a535d Compare May 18, 2022 15:32
@nad

nad commented May 18, 2022 •

Copy link
Copy Markdown
Collaborator

One could start by repeating an experiment @vlopezj performed, in his words, "replacing the type of either the constraint or of the context variables by a blocked constant at those points in Conversion.hs where it is checked whether a type is of the form of one of the cubical-Agda-specific types".

I wonder if, and if so how, the following code should be changed:

instance SideIsSingle s => TwinAt s (TwinT' a) where
type TwinAt_ s (TwinT' a) = a
{-# INLINE twinAt #-}
twinAt (SingleT a) = twinAt @s a
twinAt TwinT{direction,twinPid,twinLHS,twinRHS} = case (sing :: SingT s) of
SLHS -> twinAt @s twinLHS
SRHS -> twinAt @s twinRHS
SSingle | ISet.null twinPid ->
selectSmaller direction (twinAt @'LHS twinLHS) (twinAt @'RHS twinRHS)
| otherwise -> __IMPOSSIBLE__
SCompat -> __UNIMPLEMENTED__

Several Cubical Agda test cases trigger the error on the last line above. The use of __UNIMPLEMENTED__ might lead one to believe that the idea is that the final case should be implemented in some way. However, I wonder if the idea is rather to avoid triggering this case, by avoiding uses of Compat in the code. I'm not quite sure how to interpret the documentation of Compat:

-- | Describes on which side of the context a term resides
data ContextSide = LHS -- ^ Left side of the context
| RHS -- ^ Right side of the context
| Both -- ^ Resides on both sides of the context
| Compat -- ^ A (non-existent) side of the context
-- in which both terms that reside on the
-- left and on the right sides can be typed
| Single -- ^ Resides on a single-sided context

The following pieces of code which use twinAt for Compat should perhaps be modified in some way:

All occurrences of twinAt @'Compat in Agda.TypeChecking.Conversion are in code related to Cubical Agda, and the function expandRecordVar is used by code that is related to Cubical Agda. Then there is one occurrence in code related to cumulativity, and one occurrence (of MB.twinAt @'MB.Compat) in code related to the Auto command.

Previously Agda.TypeChecking.Records.expandRecordVar was commented out, but this procedure is now used in Agda.TypeChecking.Rules.LHS.Unify.LeftInverse.buildEquiv, so I restored the commented-out code without knowing if the restored code is correct.

Perhaps the code is not correct, see above.

@nad

nad commented May 18, 2022

Copy link
Copy Markdown
Collaborator

As an experiment I replaced __UNIMPLEMENTED__ with selectSmaller direction (coerce twinLHS) (coerce twinRHS) in twinAt. I don't expect this implementation to be entirely correct, but a number of test cases that used to trigger the __UNIMPLEMENTED__ error go through with this change applied.

The cubical library test triggered another error:

Pi dom cod -> __IMPOSSIBLE__ -- equalFun s (asTwin dom) (asTwin cod) m n

I could fix this by replacing __IMPOSSIBLE__ with the commented-out code (but I wonder what the reason was for commenting out the code). With that change in place I could type-check quite a bit of cubical library code, sometimes after making small changes. Some observations:

  • I could make code that Agda claimed had type errors correct by adding or removing implicit arguments, suggesting that this variant of Agda is buggy.
  • I observed some problems related to function extensionality, perhaps paths are not treated correctly.
  • Sometimes Agda became very slow.

I don't advocate blindly applying the changes I discuss above, but this experiment suggests to me that it might be feasible to make the code "work".

@andreasabel

Copy link
Copy Markdown
Member

@nad wrote:

I constructed a small test case from the standard library code:

{-# OPTIONS --without-K #-}

P : Set → Set₁

Q : (A : Set) → P A → Set₁
Q _ _ = Set

E : Set₁
E = (A : Set) (x : P A) → Q _ x

P _ = Set

We get the following error message:

(P A) !=< (P _A_3)
when checking that the expression x has type P _A_3

According to bisection, this indeed breaks first on your merge commit:

f7a535d is the first bad commit
Merge: a321808 3ca4db8
Date: Tue May 17 22:24:06 2022 +0200

Merged the master branch into a heterogeneous unification branch.

Some test cases were disabled:
...

@jespercockx

Copy link
Copy Markdown
Member

Unfortunately, it does not seem like anyone has had the time to do the remaining work to bring this PR in a state that can be merged. Since we are now just two months from the (planned) release of 2.6.3, I don't think we should merge this now.

Unfortunately, this means further bitrot of this PR is likely to occur, but I don't see a good alternative.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: do-not-merge So please don't merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Internal error in the presence of unsatisfiable constraints

6 participants