Skip to content

Cubical: User-friendly usage of HLevel proofs #6009

Open

Description

@anuyts

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 {!!} {!!} i

A 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) i

The 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 # _) i

but 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

  1. plt-amy commented on Aug 6, 2022

    @plt-amy
    Contributor

    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:

    def path (A : type) (x y : A) : type :=
      ext i => A with [i=0 => x | i=1 => y]
    
    def test (A : type) (p : (x y : A) → path A x y) (x y : A) : path A x y := 
      i => p ? ? i
    

    We see that for the first hole, our goal is (I've reformatted it for vertical space)

    test.cooltt:5.9-5.10 [Info]:
      Emitted hole:
        _ : [ ⊤ ]
        A : type
        p : (x : A) → (y : A) → ext {i => A} {i => i = 0 ∨ i = 1} {i _x => [ i = 0 => x | i = 1 => y ]}
        x : A
        y : A
        i : 𝕀
        |- ? : A
    

    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 [·] ~> N is 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:

    test.cooltt:5.7-5.14 [Error]:
      Expected p {#1 * A p x y i} {#2 * A p x y i} i =
               [ i = 0 => x | i = 1 => y ]
               : A
    

    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) ⊢ ? = x and the second of (i = i1) ⊢ ? = y. For the other endpoint, we're free to choose whatever inhabitant of A we want, even one that has no relation to x and y!

    test : ∀ {ℓ} {A : Type ℓ} (p : (x y : A) → x ≡ y) (x y z : A) → x ≡ y
    test p x y z i = p (p x z i) (p z y i) i
    
  2. anuyts commented on Aug 6, 2022

    @anuyts
    ContributorAuthor

    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 :)

  3. added
    type: discussion
    cubical
    on Aug 14, 2022
  4. ncfavier commented on Aug 18, 2022

    @ncfavier
    Contributor

    Maybe when multiple solutions exist, C-c C-s should 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)

  5. anuyts commented on Aug 18, 2022

    @anuyts
    ContributorAuthor

    Well I think the "promise" of C-c C-s is that it does not produce anything unless there is a unique solution, so I think it does behave correctly. There is an unmaintained shortcut C-c C-a for filling a hole with some term, it would not be inappropriate to tie a solution to that shortcut.

  6. plt-amy commented on Aug 18, 2022

    @plt-amy
    Contributor

    Right, so C-c C-s isn't a proof search procedure at all: It just reifies the solution to the metavariable associated with the hole, if any. This means that for C-c C-s to 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-a is not aware of unification constraints, so it can't use those to solve the goal. I think! I'll have a proper look later

  7. anuyts commented on Aug 18, 2022

    @anuyts
    ContributorAuthor

    From what I've heard, the implementation C-c C-a is 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.

  8. ice1000 commented on Aug 25, 2022

    @ice1000
    Member

    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.

    Does this look like a salvation?
    image

  9. anuyts commented on Aug 25, 2022

    @anuyts
    ContributorAuthor

    Hi @ice1000,

    • with markdown you can format your comments so that you don't have to use screenshots,
    • in the given code, what is the definition of fillCube?
  10. added this to the icebox milestone on Aug 25, 2022
  11. ice1000 commented on Aug 25, 2022

    @ice1000
    Member

    Hi @ice1000,

    • with markdown you can format your comments so that you don't have to use screenshots,
    • in the given code, what is the definition of fillCube?

    fillCube is a macro. The code comes from agda/cubical#910

  12. anuyts commented on Aug 26, 2022

    @anuyts
    ContributorAuthor

    That's certainly an interesting pull request but I don't think it solves the current problem:

    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.

    In the following hole:

    prop : (A : Type) → isProp A → isProp A
    prop A isPropA x y i = ?

    I could hope to use fillCube1:

    fillCube1 :
      (h : isOfHLevel 1 A)
      (u : (i : I) → Partial (i ∨ ~ i) A)
      (i : I) → A [ _ ↦ u i ]

    but since I have already abstracted over i, the argument u will have two interval variables in scope which will later be contracted. So if I write

    prop : (A : Type) → isProp A → isProp A
    prop A isPropA x y i = outS (fillCube1 isPropA {!u!} i)

    then I think even theoretically it is not possible to infer u, as the following two definitions are both valid:

    u j (j = i0) = x
    u j (j = i1) = y
    u j (j = i0) = isPropA x y i
    u j (j = i1) = isPropA x y i
  13. ncfavier commented on Apr 10, 2023

    @ncfavier
    Contributor

    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 getType ignoring module parameters #5858), inContext restores the original context in which we started the TC computation, instead of the empty context. I simply exposed inTopContext as a new reflection primitive, but it doesn't play well with the other primitives: I ran into __IMPOSSIBLE__ errors when trying to use declareDef/defineFun in 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 expose inTopContext and 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 checkIApplyConfluence runs. 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

      refl : ∀ {ℓ} {A : Type ℓ} (x : A) → _ ≡ _
      refl x i = x

      whereas previously it would complain about unsolved constraints.

    • (EDIT: outdated) The way the i # x macro works is that it first expands to something like 0 n# x, which has a type like {@(tactic unbind-tactic (0 th) x) y : A} → A (and the tactic then unifies x and y in a strengthened context). In order for this to work, I had to change the application rule for unquote to use coerce instead of checking that that type is equal to A.
      This is quite hacky and doesn't work properly with nesting (I'm not entirely sure why yet). Other paths I explored:

      • Getting rid of the macro, making n# take i : I instead of i : Nat and do the quoting in the tactic (this would require adding a quoteITC primitive as I lives in its own universe). The tactic would then only work with interval variables, which is probably okay.
      • Using only a macro, no tactic. The problem is as follows: given _0 x, if you only have control over _0, how to get x to be unified against a strengthened hole? I had no luck with this, all my attempts resulted in _0 being some flavour of the identity function, so the expression reduces to x before 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.

  14. jespercockx commented on Apr 10, 2023

    @jespercockx
    Member

    I simply exposed inTopContext as a new reflection primitive, but it doesn't play well with the other primitives

    Perhaps it would make sense to instead expose escapeContext as a new primitive?

    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 ?).

    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).

    In order for this to work, I had to change the application rule for unquote to use coerce instead of checking that that type is equal to A.

    I think using coerce here actually more sensible than equalType, 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?

  15. ncfavier commented on Apr 21, 2023

    @ncfavier
    Contributor

    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 :)

  16. anshwad10 commented on Sep 2, 2025

    @anshwad10

    Well it looks like ncf has already fixed this by implemented the macro. So is there any reason this issue is still open?

  17. ncfavier commented on Sep 2, 2025

    @ncfavier
    Contributor

    The macro still needs changes to Agda in order to work (a way to remove entries from the TC context, 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).

  18. anuyts commented on Sep 15, 2025

    @anuyts
    ContributorAuthor

    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.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    cubicaltype: discussion

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions