Repository navigation
Description
For a while, I've been quite annoyed by the fact that in the following code, I cannot let Agda solve (C-c C-s) both goals below or just write underscores, even though it seemed clear to me that x and y are the only possible solutions:
{-# OPTIONS --cubical #-}
open import Cubical.Foundations.Prelude
module InvokeHLevelSimple where
variable A : Type
prop : (A : Type) → isProp A → isProp A
prop A isPropA x y i = isPropA {!!} {!!} iA bit to my surprise, I realized this is not true. The first goal is only constrained at i = i0 and the second one at i = i1:
prop1 : (A : Type) → isProp A → isProp A
prop1 A isPropA x y i = isPropA x y i
prop2 : (A : Type) → isProp A → isProp A
prop2 A isPropA x y i = isPropA (isPropA x y i) (isPropA x y i) iThe consequences of this are grave: whenever you want to invoke a type's HLevel when you have already abstracted over the dimensions, you are forced to repeat its entire boundary. (And of course knowing a type's HLevel is a relief in particular when this boundary is complicated.)
I was hoping to write a macro to remove a variable from the context, so that we could at least write
prop3 : (A : Type) → isProp A → isProp A
prop3 A isPropA x y i = isPropA (i # _) (i # _) ibut I ran into at least two problems:
- Agda's reflection mechanism doesn't have a substitution operation,
- Macro invocations one of whose arguments is a metavariable or underscore, seem to wait for it to be solved rather than being called with a quotation of the metavariable/underscore.
A macro would probably be just one of multiple solutions to this problem, but I do consider this a severe problem w.r.t. the usability of cubical agda.
The core problem described above seems superficially related to proposals #5884 and #5997.
Activity
plt-amy commentedon Aug 6, 2022
I agree completely: filling "dumb" squares cubes that follow from the codomain having a specific h-level is entirely mechanical work that should be handled by the proof assistant. I'm at a loss, though.
For starters, what you propose in #5884 would not help. I don't mean this as a dig against you (or against the cooltt team, whose work I deeply appreciate!), it's just that the particular shape of this elaboration problem is one where keeping track of boundaries the correct way can't help us. We can confirm this by running a similar program through cooltt and seeing what it thinks of the interaction points:
We see that for the first hole, our goal is (I've reformatted it for vertical space)
No boundary in sight, and indeed, there is no way to, in general, propagate a boundary from the context into the elaboration of a function argument. I'll cite @jonsterling's thesis proposal here (link), since it has a fairly comprehensive listing of boundary-sensitive elaboration: The rule for applications is given as
We can see that the argument
Γ ⊢ N ⇐ a [·] ~> Nis elaborated against the empty boundary. Going back to cooltt, we have (after the second hole, which is essentially the same thing as the first, so I won't paste it to save on vertical space) their equivalent of an unsolved unification problem:The problem for type inference is, as you point out correctly, that the boundaries of the first and second holes are only constrained on the left and right endpoints, respectively. Indeed, my PR #5999 can recover the boundary constraints from the unification problems: The first goal has a boundary of
(i = i0) ⊢ ? = xand the second of(i = i1) ⊢ ? = y. For the other endpoint, we're free to choose whatever inhabitant ofAwe want, even one that has no relation toxandy!anuyts commentedon Aug 6, 2022
Yes, I didn't mean to claim that either issue could solve this problem, it just seemed useful to have the reference here.
Thanks for providing me with yet another reason to read Jon's work though :)
ncfavier commentedon Aug 18, 2022
Maybe when multiple solutions exist,
C-c C-sshould prefer the "most constrained" one? i.e. first search for a solution forgetting all boundary conditions, then if it finds none add boundaries one at a time. (I have no idea how any of this works internally, apologies if this is completely off base)anuyts commentedon Aug 18, 2022
Well I think the "promise" of
C-c C-sis that it does not produce anything unless there is a unique solution, so I think it does behave correctly. There is an unmaintained shortcutC-c C-afor filling a hole with some term, it would not be inappropriate to tie a solution to that shortcut.plt-amy commentedon Aug 18, 2022
Right, so
C-c C-sisn't a proof search procedure at all: It just reifies the solution to the metavariable associated with the hole, if any. This means that forC-c C-sto succeed, the hole has to have been solved through unification, i.e. you need at least one constraint to have been in the pattern fragment (metavariable applied to a telescope of distinct bound variables ± some η-expansion stuff). The endpoints of the interval, not being bound variables, do not do this.As far as I know,
C-c C-ais not aware of unification constraints, so it can't use those to solve the goal. I think! I'll have a proper look lateranuyts commentedon Aug 18, 2022
From what I've heard, the implementation
C-c C-ais severely outdated, so I didn't mean it as a practical suggestion, I just meant to say that the behaviour that @ncfavier proposes is something you would expect in proof search, not in a solve goal command.ice1000 commentedon Aug 25, 2022
Does this look like a salvation?

anuyts commentedon Aug 25, 2022
Hi @ice1000,
fillCube?ice1000 commentedon Aug 25, 2022
fillCubeis a macro. The code comes from agda/cubical#910anuyts commentedon Aug 26, 2022
That's certainly an interesting pull request but I don't think it solves the current problem:
In the following hole:
I could hope to use
fillCube1:but since I have already abstracted over
i, the argumentuwill have two interval variables in scope which will later be contracted. So if I writethen I think even theoretically it is not possible to infer
u, as the following two definitions are both valid:ncfavier commentedon Apr 10, 2023
I've managed to implement the macro suggested in the OP, with a few changes to Agda!
Here's the code and demo, here's the corresponding branch of Agda.
The basic idea is simple: drop the unwanted variables from the context; create a new meta in this stronger context; unify it with the goal. Then, pruning does its job and drops the unwanted boundary constraints (or errors out if the goal has a rigid dependency on the dropped variables), so we're left with solvable constraints in the pattern fragment.
The implementation of context strengthening relies on the 1lab's implementation of substitution, though it should be easily portable to other libraries.
Making this work in practice required the following changes:
There is no way to drop things from the context with the current reflection primitives: since fairly recently (Fix the reflection primitive
getTypeignoring module parameters #5858),inContextrestores the original context in which we started the TC computation, instead of the empty context. I simply exposedinTopContextas a new reflection primitive, but it doesn't play well with the other primitives: I ran into__IMPOSSIBLE__errors when trying to usedeclareDef/defineFunin the empty context. I haven't looked into it, but it probably has to do with the module stack. I don't know if we want to exposeinTopContextand make it work correctly, or expose something weaker (all that's needed is a function that creates a new meta in a specified context). Possibly related: Get rid of unsafeModifyContext and unsafeInTopContext #4388 (comment)In order for the constraints generated by IApplyConfluence to actually be solved, metas need to be unfrozen when
checkIApplyConfluenceruns. I fixed this in the most straightforward possible way, by moving the mutual checks before the freezing of metas. Again, we might want something more refined, I don't know if this causes any problems that the tests didn't catch (ping @jespercockx ?).This change means that Agda can now infer things like
whereas previously it would complain about unsolved constraints.
(EDIT: outdated) The way the
i # xmacro works is that it first expands to something like0 n# x, which has a type like{@(tactic unbind-tactic (0 th) x) y : A} → A(and the tactic then unifiesxandyin a strengthened context). In order for this to work, I had to change the application rule forunquoteto usecoerceinstead of checking that that type is equal toA.This is quite hacky and doesn't work properly with nesting (I'm not entirely sure why yet). Other paths I explored:
n#takei : Iinstead ofi : Natand do the quoting in the tactic (this would require adding aquoteITCprimitive asIlives in its own universe). The tactic would then only work with interval variables, which is probably okay._0 x, if you only have control over_0, how to getxto be unified against a strengthened hole? I had no luck with this, all my attempts resulted in_0being some flavour of the identity function, so the expression reduces toxbefore anything interesting happens.My branch of Agda passes the test suite and checks stdlib, cubical and 1lab, so I'm at least somewhat confident that the changes are feasible, but I need feedback now.
jespercockx commentedon Apr 10, 2023
Perhaps it would make sense to instead expose
escapeContextas a new primitive?I don't see any immediate negative effects of doing this, but I'd want to check with @UlfNorell or @andreasabel first. We did have some discussions in the past on the desirability of having the definition of a function affect the metavariables from its type signature, but this a separate concern (though it could mean that your example is not guaranteed to keep working in future versions of Agda).
I think using
coercehere actually more sensible thanequalType, since it will also work with subtyping (e.g. --sized-types or --cumulativity).To continue the discussion, perhaps it makes sense for you to make a separate PR for each change to the reflection API that is needed, so we can discuss each of them in more detail?
ncfavier commentedon Apr 10, 2023
Thanks, done.
unquoteapplications #6570ncfavier commentedon Apr 21, 2023
Using the trick in #5738 (comment) I was able to simplify my macros to not use tactic arguments, which also solves the issue with nesting.
The code does not depend on #6570 any more.
Thanks @tomsmeding for bringing this to my attention :)
anshwad10 commentedon Sep 2, 2025
Well it looks like ncf has already fixed this by implemented the macro. So is there any reason this issue is still open?
ncfavier commentedon Sep 2, 2025
The macro still needs changes to Agda in order to work (a way to remove entries from the
TCcontext, and I think #6569 had to be reverted so more work might be necessary there).I lost interest in implementing this since I think in most cases this issue can be avoided by using an eliminator instead of pattern matching (which is easy to do using my induction macro).
anuyts commentedon Sep 15, 2025
While that is really nice, I would still considering a workaround rather than a solution.
Pattern matching in Agda is widely considered to be a useful feature.