Repository navigation
Description
Background
The user experience for Cubical Agda is subpar, and one of the biggest problems is that, even though interaction points appear to have boundaries, there is a very limited set of circumstances that will actually cause an interaction point's boundary to be restricted. To wit: this only happens in checkIApplyConfluence, so if the interaction point does not have a boundary specified by the LHS of its clause, it will never have a boundary at all.
This poses some problems! For example, consider the following example:
double-comp
: ∀ {ℓ} {A : Type ℓ} {w x y z : A} (p : w ≡ x) (q : x ≡ y) (r : y ≡ z)
→ w ≡ z
double-comp {ℓ = ℓ} {A = A} p q r i =
hcomp {ℓ = ℓ} {A = A}
(λ { j (i = i0) → {! !} -- 0
; j (i = i1) → {! !} -- 1
})
{! !} -- 2Here, all the interaction points are "cubical", and thus have some boundary (even though IP 2's boundary is fairly unspecified). But these boundaries are not fixed by the LHS, so the interaction points do not carry information about their boundary. Put concretely, the problems with the current interaction point boundary mechanism are as follows:
Problem 1: The boundary of an interaction point is usually not fully determined simply by the LHS of the clause.
Problem 2: Interaction points in the same clause can have different boundaries.
Problem 3: The data which specifies an interaction point's boundary is underspecified for the purposes of computation.
A proposal
-
The type of interaction point boundaries (
IPBoundary') should contain more specific information about what specific face is being described. Rather than carrying around a list of(Term, Term)equations (ipbEquations), each boundary condition should have a list of(Var, Bool)mappings specifying the specific face. (I'll get to why we would want this refinement in a second). The rest of the type can stay as-is. -
The list of
Closure IPBoundaryshould not be attached to the clause. It should be attached directly to theInteractionPoint. This is a matter of both semantics and cleanliness: Semantically, the clause has nothing to do with the interaction points' boundary, as thedouble-compexample shows, and for cleanliness, this would keep the
invariant thatIPClauseis set only once by the coverage checker, and that sameIPClauseis used by all the IPs in that clause. -
Rather than collecting the boundary up-front, any suitable unification problem against an interaction point should refine its boundary. Concretely: In
Agda.TypeChecking.MetaVars.assign, theinverseSubstfunction would have a new failure case: a refinement ofNeutralArgfor when the argument is one of the endpoints of the interval --- let's say it'sBoundaryArg, but I'm not too concerned about the name. On this specific failure case, we would try to recover the unification problem as follows.
Let's use one of the unsolved constraints from double-comp as a representative example.
z = ?1 (p = p) (q = q) (r = r) (j = i1) : A (blocked on _17)
inverseSubst, faced with these Args, would throw our new BoundaryArg exception, indicating the position of the first interval argument, which in this case is position n = 3. Then, we run the following checks:
-
Taking the first n arguments should give a list of
Argsin the pattern fragment, i.e.,inverseSubstshould succeed for thatArgs. If this fails, we abort. -
Arguments in positions n and up should consist only of applications to endpoints. I believe that the
Argsis guaranteed to be well-typed, but in case it is not, we can also enforce that dropping the firstnbinders fromtelViewUpToPath (length args)leaves us withlength args - n-many interval-typed binders.
If both these checks succeed and we are in one of the cubical modes and the MetaId refers to an interaction point, then we are looking at a boundary, and not an unsolved constraint. In that case I believe we can:
- Record the n-and-up'th arguments as making up the
Face; - Record the RHS of the equation as making up.. well, the right-hand-side of the boundary;
- Add this boundary constraint to the interaction point's data
- Ignore the unification problem. This is the part I'm iffiest about: I believe it'd be fine, since it would otherwise go unsolved after all, but perhaps it is more correct for it to be "blocked on boundary".
Benefits
The biggest immediate benefit is that, assuming it is alright to drop the unification problem, cubical programs in the process of being written would have a lot less type errors than they usually appear to have. Hopefully this would reduce the amount of yellow being shown to the user but I'm a bit less sure of that.
This refactoring means that IPBoundary now has a semantic meaning. Specifically, we can look at an interaction point (say the same ?0 above) and construct a precise cubical type specifying what terms would fit there. An interaction point in telescope Γ of type T with boundary conditions given by φ₁ ⊢ t, φ₂ ⊢ s, ..., is really outS of some term typable in the empty context as
Π Γ → Sub T (φ₁ ∨ φ₂ ∨ ...) λ { (φ₁ = i1) → t ; (φ₂ = i1) → s ; ... }
While it appears that this doesn't immediately translate to a benefit for the user, it does: If ⋁ φ is logically equivalent to the disjunction ⋁ (i ∨ ~ i) (i ranges over the variables mentioned in φ), then the Sub above is really an iterated PathP, and we can tell the user that! The interaction command which computes a helper function type can look for this special case and show the helper function type as a Path-type.
As an example, after specifying the base for double-comp (i.e. the user specifying that ?2 = q i), we would be left with the following "type-and-boundaries" for the interaction points 0 and 1:
?0 :
∀ {l} {A : Type l} {w x y z : A} (p : w ≡ x) (q : x ≡ y) (r : y ≡ z)
→ (j : I) → Sub A (j ∨ ~ j) λ { (j = i0) → x ; (j = i1) → w }
?1 :
∀ {l} {A : Type l} {w x y z : A} (p : w ≡ x) (q : x ≡ y) (r : y ≡ z)
→ (j : I) → Sub A (j ∨ ~ j) λ { (j = i0) → y ; (j = i1) → z }
which, since j ∨ ~ j is the disjunction of the boundary of j, could be displayed in simplified form as
?0 :
∀ {l} {A : Type l} {w x y z : A} (p : w ≡ x) (q : x ≡ y) (r : y ≡ z)
→ Path A x w
?1 :
∀ {l} {A : Type l} {w x y z : A} (p : w ≡ x) (q : x ≡ y) (r : y ≡ z)
→ Path A y z
Activity
[-]"Promote" unsolved cubical constraints to interaction boundaries?[/-][+]Collect interaction point boundaries while elaborating right-hand-sides?[/+]plt-amy commentedon Jul 27, 2022
I edited the issue to include a more concrete proposal of the design I was thinking about. To be clear: I'm volunteering to implement this, as long as someone experienced with the Agda codebase can look at this proposal and say "yeah, could work" or "nah, don't even bother".
This is, in a sense, an adaptation of redtt/cooltt's treatment of "cubical interaction points" that can be implemented with fairly non-invasive changes to the Agda codebase (the new "recovery" strategy for pattern-condition violations is the only significant change)
Btw: another benefit of the more precise specification of faces in boundary conditions that I missed while writing the wall of text up there is that we could extend the command powering "goal type, context, and inferred type" to also print what the user's term normalises to under those substitutions, i.e. the "inferred boundary" of what they've written out so far.
jespercockx commentedon Jul 27, 2022
I applaud the initiative to make Cubical Agda more user-friendly, and the proposal makes sense to me from the implementation perspective. However, I am not an expert on the implementation of
--cubical, and neither at the semantics of cubical type theory. Since this seems to be a change to the semantics of Cubical Agda and not just the UI, I think it would be a good idea to check this with someone who matches that description. Ideally, @Saizan if he is available, but otherwise the opinion of another expert at semantics of Cubical TT would also be good to have.(Also note that @Saizan is currently not very active here as he has moved to industry, and currently we don't really have anyone else who's active in the development of
--cubical. So in general, these kind of contributions are very welcome!)Saizan commentedon Jul 27, 2022
The current implementation for Boundaries for interaction points is indeed not ideal, and improvements are welcome!
To give a bit of context, extracting information from the unification machinery did not seem appealing because of these:
pred (?1 (j = i0)) = 0can't give you a boundary.Worrying about (2) might be a red herring and good things can be done anyway. Maybe (1) is not as much of a problem or can be worked around, but you should have tests where the expected boundary is a redex.
Currently IPBoundary is not at all taken into consideration when checking that what you
giveis correct for the goal. Only the type of theMetaVarand anyConstraintsthat mention it matter, so it would be incorrect to ignore the unification problem that generated the boundary. You also cannot just change the type of the underlying meta to a path type, as then you'd have to change how it's applied in terms too.Given that, and the way constraints might be attempted more than once in general, another concern is that you'd end up with duplicated boundaries? I guess you can tell when you are on the same Face and you can use conversion to tell if it'd be the same boundary term? Be warned that Agda can be quite slow at checking that two identical things are equal if redexes are involved. (Of course if you extract a boundary from a unification constraint you can explicitly mark it as already handled for this purpose).
Overall getting boundaries the way
checkIApplyConfluencedoes has the advantage of working from the rather simpler typechecker rather than the fairly more involved conversion checker, but if you make it work that would be great.plt-amy commentedon Jul 27, 2022
This is good to know. I assume that's why, even when
checkIApplyConfluencecan work out a boundary, we still get unsolved constraint errors? I won't get rid of the constraint, to make sure that nothing actually explodes... but after a first prototype it'd be nice for constraints that got marked as "being a boundary" became "silent" somehow.I don't think we should worry too much about constraints like (2) --- unless I'm severely misunderstanding things, that constraint wouldn't get to
assignin the first place, because neither side of the equation is a meta-headed term? And in that case, it's no different from any other unsolved constraint.Right — can't change it to a Sub either, but we can pretend! The idea of turning boundary constraints into path types was more for the
C-c C-hcommand, which, if asked to abstract over an interval variable, could try building aPathtype for the helper function instead. But this is probably me getting ahead of myself, hahaanuyts commentedon Aug 2, 2022
I'm a cubical Agda user and a semantics person, not an Agda developer.
I can't follow the details about the current implementation, but the above proposal sounds sensible and would greatly improve the user experience.
To my understanding, especially after @Saizan's remarks, the proposal would not change anything to the semantics of the language, but present information in a prettier form and also as part of the "goal type and context" information, which would be very helpful: I often find myself searching for and then squinting at the sort of constraints that you would now identify as a boundary.
A potential criticism is that this solution is not type-theoretic:
Information is gathered from constraints and presented to the user, but suppose you would actually include this information as part of the judgement structure of cubical Agda, then it will become very difficult to write down typing rules that align with what you propose.
That's not in itself a reason not to proceed, but there is a possibility (by no means a certainty) that this problem can be solved with less work (for Agda developers, perhaps more work for cubical library developers) by sorting out the theory of this problem, for which I've laid out some ideas in #5884.
Other than that, I had some smaller remarks:
funExt(i.e. by abstracting over a non-interval variable after abstracting over an interval variable).plt-amy commentedon Aug 2, 2022
@anuyts you're right that there is no clear motivation for forbidding non-endpoints after the first endpoint; the current implementation allows these. It's still very rough around the edges, but using funext as an example:
the interaction point there has goal-type-and-boundary
xshouldn't be recorded as part of the face, so for now please pretend that it isn't 🙂plt-amy commentedon Aug 2, 2022
As for refactoring the type checker to keep precise track of boundaries... I'm not sure that it'd actually be worth the effort. You're correct that doing so would be, from an elegance/correctness perspective, a much better approach, but it'd also be a significant undertaking and, as a personal conjecture, I believe that it'd be less general than the slightly wonky reconstruction of boundaries in the conversion checker.
anuyts commentedon Aug 2, 2022
Well I guess all the combinators that I propose become silent in your approach, which is a clear advantage.