Skip to content

Keeping track of constraints in cubical via non-primitive path types? #5884

Closed

Description

@anuyts

What I'm going to write here is something between a feature request and a research proposal.

One of the great advantages of cubical type theory is that the path type behaves somewhat like a function type, making results like funExt, refl, sym, cong and flipSquare trivial to implement and giving them nice computational behaviour.
However, when as a user of Agda cubical you dare to make use of this function-like nature of paths and actually abstract over an interval variable, then the endpoint constraints move from the type to Agda's constraint system and become rather difficult to keep track of. This is a theoretical issue, not one of Agda. Indeed, we could keep constraints in the judgement (as I think Agda actually does as long as possible) but there is no way to infer constraints e.g. for the following subterms:

Γ |- f : A -> B [ φ -> ? ]
Γ |- a : A [ φ -> ? ]
------------------
Γ |- f a : B [ φ -> b ]

I think that instead of having libraries full of operations like the ones listed above that involve trivial manipulations of interval variables while not really helping us out with the constraints, we should instead leave interval variable manipulations to the user and have libraries that help out with the constraints. This is possible if we view the path type not as a primitive type but as built up of two parts:

  1. Functions (i : I) -> A i ranging over the interval. I think these should be Kan, if not by the usual proof for Π-types, then at least by swapping i with the dimensions of the Kan operation, which is possible since box inclusions are closed under products with I.
  2. Extension types, not in the sense of Riehl and Shulman (what they call extension types is a generalization of path types) but in the sense of §6.2.2 of my phd thesis (I do not claim to have invented these).
    I'll quickly state the rules here:
Γ |- A type
Γ |- φ : Prop
Γ, φ |- a : A
-----------------
Γ |- Ext A [φ -> a] type

Γ |- a : A
Γ |- φ : Prop
-----------------
Γ |- cut φ a : Ext A [φ -> a]

Γ |- e : Ext A [φ -> a]
-----------------------
Γ |- paste φ e : A

paste φ (cut φ a) = a
cut φ (paste φ e) = e
paste \top e = a   -- (the index from the type)

I guess these are Kan by design of the Kan operations.

Now if we define PathP (λ i -> A i) a0 a1 as (i : I) -> Ext (A i) [i = i0 -> a0 | i = i1 -> a1], then it is sufficient to support library operations for working with extension types. For example (omitting implicit arguments), we could have

xCong : (f : (x : A) -> B x) -> (ex : Ext A [φ -> a]) -> Ext (B (paste ex)) [φ -> f a]

so that xCong f ? infers the hole's constraints at least if f is injective and keeps them in the type.

Instead of function extensionality and its inverse, we would have

xFunExt : ((x : A) -> Ext (B x) [φ -> b x]) -> Ext ((x : A) -> B x) [φ -> λ x -> b x]
xFunExtI : ((x : I) -> Ext (B x) [φ -> b x]) -> Ext ((x : I) -> B x) [φ -> λ x -> b x]
xFunExt- : Ext ((x : A) -> B x) [φ -> λ x -> b x] -> ((x : A) -> Ext (B x) [φ -> b x])
xFunExtI- : Ext ((x : I) -> B x) [φ -> λ x -> b x] -> ((x : I) -> Ext (B x) [φ -> b x])

(which should be implementable).
If there is also an (implementable) merging and unmerging operation

xMerge : Ext (Ext A [φ -> a]) [ψ -> eb] -> Ext A [φ -> a | ψ -> paste eb]
xUnmerge : Ext A [φ -> a | ψ -> paste eb] -> Ext (Ext A [φ -> a]) [ψ -> eb]

then, when in need of a square, we could simply do

square i = xFunExtI λ j -> xUnmerge ?

and we get a hole with a clear constraint of the form i = 0 /\ i = 1 /\ j = 0 /\ j = 1.

As a first step towards evaluating this proposal, the Agda developers could consider adding experimental pragmas to be used in combination with --cubical that:

  • allow level-preserving interval quantification,
  • allow extension types.
    Then we can have an experimental library that builds path types from these two (which should be easily proven isomorphic to the primitive path types) and provides operations to work with extension types.

Activity

  1. anuyts commented on May 2, 2022

    @anuyts
    ContributorAuthor

    Well we can probably only allow Kan operations in the extension type if φ is constant in the dimension of composition/transport. This could be a problem.

  2. jonsterling commented on Aug 2, 2022

    @jonsterling
    Contributor

    You may be interested in checking out what we did in cooltt, which is similar to your proposal i think.

  3. Saizan commented on Aug 3, 2022

    @Saizan
    Collaborator

    @anuyts your Ext is Sub from Agda.Builtin.Cubical.Sub and indeed it's not Kan in general.

  4. anuyts commented on Aug 4, 2022

    @anuyts
    ContributorAuthor

    @jonsterling Do you merely mean that cooltt also has Sub/Ext types, or also that you've used combinators as I described?

  5. jonsterling commented on Aug 4, 2022

    @jonsterling
    Contributor

    What I mean is that in addition to supporting the Sub/Ext (pre)types, both cooltt and redtt keep the boundary constraint in the judgment as long as possible; it's true that there are cases where one loses the boundary however. Your combinators are interesting and could be implemented there... But I would like to think about whether we can take some lesson from your combinators and integrate it into elaboration directly.

  6. anuyts commented on Aug 4, 2022

    @anuyts
    ContributorAuthor

    Well I guess we could have a typing rule

    Γ |- f : A -> B [ φ -> ∂f ]
    Γ |- a : A [ φ -> ∂a ]
    ------------------
    Γ |- f a : B [ φ -> ∂f ∂a ]
    

    Then when we give a hole {!f ?!} of type B [ φ -> ∂b ], I guess this could

    • immediately resolve ∂f to be f
    • issue a metavariable ∂a and equate f ∂a = ∂b. Then if f is judgementally an injective operation and ∂b = f a', we do get a subgoal A [ φ -> a' ] as desired.

    This is a type-theoretic formulation; I'm not sure (due to not being a seasoned proof assistant developer) if you mean something substantially different when regarding it as part of elaboration, but I guess the effect would be extremely close to what @plt-amy is proposing in #5997.

  7. anuyts commented on Aug 4, 2022

    @anuyts
    ContributorAuthor

    This was fun!
    I had success in a few places, but problems in most, and I do not understand all of them.
    On isProp→collapseBool, I seem to have run into an Agda bug, where reloading after giving changes the Agda state.

    {-# OPTIONS --cubical --cumulativity --type-in-type #-}
    
    open import Agda.Primitive
    open import Cubical.Foundations.Prelude renaming (inS to cut ; outS to paste)
    open import Cubical.Data.Nat
    --open import Agda.Builtin.Cubical.Sub
    
    variable
      φ : I
      A : Type
    
    Sub_[_??_] = Sub
    
    βSub : (a : A) → paste {φ = φ} (cut a) ≡ a
    βSub a = refl
    
    ∂Sub : {a : Partial φ A} → (xa : Sub A φ a) → PartialP φ (λ o → paste xa ≡ a o)
    ∂Sub {φ = φ} xa (φ = i1) = refl
    
    --cannot state equality in a nonKan type.
    --ηSub : {a : Partial φ A} → (xa : Sub A φ a) → cut {φ = φ} (paste xa) ≡ ?
    
    -----
    
    abs : {x y : A} → ((i : I) → Sub A (~ i ∨ i) λ {(i = i0) → x ; (i = i1) → y}) → x ≡ y
    abs p i = paste (p i)
    
    _$$_ : {p∂a : (i : I) → Partial (~ i ∨ i) A} → p∂a i0 1=1 ≡ p∂a i1 1=1 → (i : I) → Sub A (~ i ∨ i) (p∂a i)
    p $$ i = cut (p i)
    
    xCongS : {B : Type} {φ : I} {∂a : Partial φ A} (f : (x : A) -> B) → (xa : Sub A φ ∂a) → Sub B [ φ ?? (λ o → f (∂a o)) ]
    xCongS f xa = cut (f (paste xa))
    
    xCong : {B : A → Type} {φ : I} {∂a : Partial φ A} (f : (x : A) -> B x) → (xa : Sub A φ ∂a) → Sub (B (paste {_} {A} {φ} {∂a} xa)) [ φ ?? (λ where (φ = i1) → f (∂a _)) ]
    xCong f xa = cut (f (paste xa))
    
    --xFunExt : {B : A → Type} {φ : I} {∂fb : Partial φ ((x : A) -> B x)} → ((x : A) → Sub (B x) [ φ ?? (λ {(φ = i1) → ∂fb 1=1 x}) ]) → Sub ((x : A) -> B x) [ φ ?? ∂fb ]
    xFunExt : {B : A → Type} {φ : I} {∂fb : Partial φ ((x : A) -> B x)} → ((x : A) → Sub (B x) [ φ ?? (λ o → ∂fb o x) ]) → Sub ((x : A) -> B x) [ φ ?? ∂fb ]
    xFunExt fxb = cut λ a → paste (fxb a)
    
    --xFunExtI cannot be stated by lack of a Sub type for Ssets.
    
    _x$_ : {B : A → Type} {φ : I} {f∂b : (x : A) -> Partial φ (B x)} → Sub ((x : A) -> B x) [ φ ?? (λ o x → f∂b x o) ] → ((x : A) → Sub (B x) [ φ ?? f∂b x ])
    xfb x$ a = cut (paste xfb a)
    
    --xMerge and xUnmerge cannot be stated by lack of a Sub type for Ssets.
    
    -- trivial tests -----------------------
    
    abs' : {x y : A} → ((i : I) → Sub A (~ i ∨ i) λ {(i = i0) → x ; (i = i1) → y}) → x ≡ y
    abs' p = abs λ i → {!OK!!}
    
    -- Agda doesn't use η for IsOne when equating partials so we still lose our boundary.
    _$$'_ : {p∂a : (i : I) → Partial (~ i ∨ i) A} → p∂a i0 1=1 ≡ p∂a i1 1=1 → (i : I) → Sub A (~ i ∨ i) (p∂a i)
    p $$' i = {!Boundary is lost, oddly.!} $$' i
    
    xCongS' : {B : Type} {φ : I} {∂a : Partial φ A} (f : (x : A) -> B) → (xa : Sub A φ ∂a) → Sub B [ φ ?? (λ o → f (∂a o)) ]
    xCongS' f xa = xCongS f {!Boundary is lost.!}
    
    xCong' : {B : A → Type} {φ : I} {∂a : Partial φ A} (f : (x : A) -> B x) → (xa : Sub A φ ∂a) → Sub (B (paste {_} {A} {φ} {∂a} xa)) [ φ ?? (λ where (φ = i1) → f (∂a _)) ]
    xCong' f xa = xCong f {!Boundary is lost.!}
    
    --xFunExt : {B : A → Type} {φ : I} {∂fb : Partial φ ((x : A) -> B x)} → ((x : A) → Sub (B x) [ φ ?? (λ {(φ = i1) → ∂fb 1=1 x}) ]) → Sub ((x : A) -> B x) [ φ ?? ∂fb ]
    xFunExt' : {B : A → Type} {φ : I} {∂fb : Partial φ ((x : A) -> B x)} → ((x : A) → Sub (B x) [ φ ?? (λ o → ∂fb o x) ]) → Sub ((x : A) -> B x) [ φ ?? ∂fb ]
    xFunExt' fxb = xFunExt λ x → {!OK!!}
    
    _x$'_ : {B : A → Type} {φ : I} {f∂b : (x : A) -> Partial φ (B x)} → Sub ((x : A) -> B x) [ φ ?? (λ o x → f∂b x o) ] → ((x : A) → Sub (B x) [ φ ?? f∂b x ])
    xfb x$' a = {!Boundary is lost.!} x$ a
    
    
    -- practical tests -----------------------
    
    cong-suc : {m n : ℕ} → m ≡ n → suc m ≡ suc n
    cong-suc p = abs (λ i → xCong suc {!Boundary is lost.!})
    
    collapseBool : Type → Type
    collapseBool A = (λ (x : A) (y : A) → x) ≡ (λ x y → y)
    
    -- With either signature, the goal is not lost when giving the outer goal but then is lost when reloading (this is a bug!).
    isProp→collapseBool : isProp A → collapseBool A
    isProp→collapseBool isPropA = abs λ i → {!xFunExt {!λ x → {!xFunExt {!λ y → {!? $$ i!}!}!}!}!}
    
    -- I don't even know what I'm hoping for here,
    -- I don't think application to a given argument is ever injective on a function space (except if the domain is judgementally contractible).
    collapseBool→isProp : collapseBool A → isProp A
    collapseBool→isProp cbA x y = abs λ i → {!Boundary is lost.!} x$ y
  8. jespercockx commented on Aug 23, 2022

    @jespercockx
    Member

    I do not think the issue tracker is the best place to discuss novel research ideas. If you have a concrete proposal of what should be implemented, feel free to reopen this or create a new issue/PR!

  9. anuyts commented on Aug 23, 2022

    @anuyts
    ContributorAuthor

    Well it turned out that the proposed type was already there and is called Sub, so I think indeed this issue is completed.

  10. added
    type: discussion
    cubical
    not-in-changelog
    on Oct 24, 2022
  11. added this to the 2.6.3 milestone on Oct 24, 2022
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

    cubicalnot-in-changelogtype: discussion

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions