You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Could the termination checker be less restrictive when the K rule is turned off? #4527
cubicalCubical Agda paraphernalia: Paths, Glue, partial elements, hcomp, transpterminationIssues relating to the termination checkertype: enhancementIssues and pull requests about possible improvementswithout-KK-related restrictions to pattern matching, termination checking, indices, erasure
The termination checker is currently more conservative when the K rule is turned off (#1023). I think that all the problematic examples that are listed in Issue #1023 rely on pattern matching on refl. If inductive families were disallowed (under some flag), would it be safe to loosen the restrictions?
@Saizan is/was working on proper support for inductive families in Cubical Agda (#3733). Is this done in such a way that the restrictions become unnecessary?
The issue with termination checking --without-K seems to be that the checker does not get to see the transports that actually happen when given a non-refl proof.
When implementing inductive families for Cubical I do generate a clause that inserts the necessary transports, so termination checking that clause should catch the examples from the other thread.
If your types are refining just because you are matching on some plain datatype then I don't think it should be rejected, as that wouldn't need extra desugaring. Someone should do a proof though?
e.g., the following is easy to translate to eliminators/see terminating, but in general?
open importAgda.Builtin.NatdataUnit:Setwhereunit : Unit
F : Unit ->Set
F unit = Nat
test :∀ (x : Unit) → F x → Nat
test unit zero = zero
test unit (suc n) = suc (test unit n)
When implementing inductive families for Cubical I do generate a clause that inserts the necessary transports, so termination checking that clause should catch the examples from the other thread.
The idea is that --without-K should be compatible both with --with-K and with --cubical. Would it make sense to generate these clauses whenever the K rule is turned off, or could this create problems for code that relies on K? (It might make sense for compiler backends to remove the extra clauses.) In that case we could perhaps remove the restriction and rely only on regular termination checking.
A way to make the termination checker less restrictive without involving (#3733) would be to only do the masking from the solution to #1023 when there's a match on an inductive family in one of the clauses.
I tried to implement a variant of one of the universes from The Gentle Art of Levitation by Chapman, Dagand and McBride (the one from Section 5.1). The following code is rejected by Agda:
{-# OPTIONS --safe --without-K --erasure --hidden-argument-puns #-}open importAgda.Builtin.Unitopen importAgda.Primitiveprivatevariable
@0 a i l p : Level
recordLift (@0 l) (A :Set a) :Set (a ⊔ l) whereconstructorliftfield lower : A
recordΣ (A :Set a) (P : A →Set p) :Set (a ⊔ p) whereconstructor_,_fieldfst : A
snd : P fst
infixr2 _⊗_
dataIDesc (@0 l) (I :Set i) :Set (lsuc l ⊔ i) wherevar : I → IDesc l I
const :Set l → IDesc l I
_⊗_ : IDesc l I → IDesc l I → IDesc l I
σ π : (A :Set l) → (A → IDesc l I) → IDesc l I
⟦_⟧ : {@0 I :Set i} → IDesc l I → (I →Set p) →Set (l ⊔ p)
⟦_⟧ {l} (var i) P = Lift l (P i)
⟦_⟧ {p} (const A) _ = Lift p A
⟦_⟧ (D₁ ⊗ D₂) P = Σ (⟦ D₁ ⟧ P) λ _ → ⟦ D₂ ⟧ P
⟦_⟧ (σ A D) P = Σ A λ x → ⟦ D x ⟧ P
⟦_⟧ (π A D) P = (x : A) → ⟦ D x ⟧ P
dataμ {@0 I :Set i} (F : I → IDesc l I) (i : I) :Set l wherecon : ⟦ F i ⟧ (μ F) → μ F i
All :
{@0 I :Set i} {@0 P : I →Set p}
(D : IDesc l I) → ⟦ D ⟧ P → IDesc l (Σ I P)
All (var i) (lift x) = var (i , x)
All (const _) _ = const (Lift _ ⊤)
All (D₁ ⊗ D₂) (xs , ys) = All D₁ xs ⊗ All D₂ ys
All (σ _ D) (x , xs) = All (D x) xs
All (π A D) f = π A λ x → All (D x) (f x)
ind :
{@0 I :Set i}
(F : I → IDesc l I) (@0 P : Σ I (μ F) →Set p) →
(∀ {i} (xs : ⟦ F i ⟧ (μ F)) → ⟦ All (F i) xs ⟧ P → P (i , con xs)) →∀ {i} (xs : μ F i) → P (i , xs)
ind {l} {I} F P f (con xs) = f xs (ind-All (F _) xs)
whereind-All : (D : IDesc l I) (xs : ⟦ D ⟧ (μ F)) → ⟦ All D xs ⟧ P
ind-All (var i) (lift xs) = lift (ind F P f xs)
ind-All (const _) _ = lift (lift tt)
ind-All (D₁ ⊗ D₂) (xs , ys) = ind-All D₁ xs , ind-All D₂ ys
ind-All (σ _ D) (x , xs) = ind-All (D x) xs
ind-All (π _ D) f =λ x → ind-All (D x) (f x)
The termination checker rejects the final definition, where the recursion involves the type ⟦ d ⟧ (μ F): the code is accepted if --with-K is used instead of --without-K.
One can replace ⟦_⟧ with a data type, but the following implementation is large:
dataSem {@0 l} {I :Set i} (P : I →Set p) :
(@0 d : IDesc l I) →Set (lsuc l ⊔ p ⊔ i) wherevar : {x : I} → P x → Sem P (var x)
const : {A :Set l} → A → Sem P (const A)
_⊗_ :∀ {@0 D₁ D₂ : IDesc l I} →
Sem P D₁ → Sem P D₂ → Sem P (D₁ ⊗ D₂)
σ : {A :Set l} {@0 F : A → IDesc l I} →
(x : A) → Sem P (F x) → Sem P (σ A F)
π : {A :Set l} {@0 F : A → IDesc l I} →
((x : A) → Sem P (F x)) → Sem P (π A F)
Furthermore I is no longer erased. If I use this definition, then I can implement variants of μ and ind with the following types:
μ : {I :Set i} → (I → IDesc l I) → I →Set (i ⊔ lsuc l)
ind :
{I :Set i}
(F : I → IDesc l I) (P : Σ I (μ F) →Set p) →
(∀ {i} (xs : ⟦ F i ⟧ (μ F)) → ⟦ All (F i) xs ⟧ P → P (i , con xs)) →∀ {i} (xs : μ F i) → P (i , xs)
Note that this variant of μ is large, and that I and P are not erased. Is there a workaround without these problems?
cubicalCubical Agda paraphernalia: Paths, Glue, partial elements, hcomp, transpterminationIssues relating to the termination checkertype: enhancementIssues and pull requests about possible improvementswithout-KK-related restrictions to pattern matching, termination checking, indices, erasure
Activity
andreasabel commentedon Mar 20, 2020
Well, if you do not have equality in the first place, there should be no difference in K or without-K.
nad commentedon Mar 20, 2020
In Cubical Agda you would still have paths.
Saizan commentedon Mar 24, 2020
The issue with termination checking
--without-Kseems to be that the checker does not get to see the transports that actually happen when given a non-refl proof.When implementing inductive families for Cubical I do generate a clause that inserts the necessary transports, so termination checking that clause should catch the examples from the other thread.
If your types are refining just because you are matching on some plain datatype then I don't think it should be rejected, as that wouldn't need extra desugaring. Someone should do a proof though?
e.g., the following is easy to translate to eliminators/see terminating, but in general?
nad commentedon Mar 24, 2020
The idea is that
--without-Kshould be compatible both with--with-Kand with--cubical. Would it make sense to generate these clauses whenever the K rule is turned off, or could this create problems for code that relies on K? (It might make sense for compiler backends to remove the extra clauses.) In that case we could perhaps remove the restriction and rely only on regular termination checking.Saizan commentedon Nov 10, 2020
A way to make the termination checker less restrictive without involving (#3733) would be to only do the masking from the solution to #1023 when there's a match on an inductive family in one of the clauses.
Fix agda#4527: reliberalise termination checking when --without-K and…
Fix agda#4527: document --without-K + termination checker
nad commentedon Jan 17, 2025
I tried to implement a variant of one of the universes from The Gentle Art of Levitation by Chapman, Dagand and McBride (the one from Section 5.1). The following code is rejected by Agda:
The termination checker rejects the final definition, where the recursion involves the type
⟦ d ⟧ (μ F): the code is accepted if--with-Kis used instead of--without-K.One can replace
⟦_⟧with a data type, but the following implementation is large:Furthermore
Iis no longer erased. If I use this definition, then I can implement variants ofμandindwith the following types:Note that this variant of
μis large, and thatIandPare not erased. Is there a workaround without these problems?