Skip to content

Could the termination checker be less restrictive when the K rule is turned off? #4527

Open

Description

@nad

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?

Activity

  1. added
    type: enhancement
    termination
    without-K
    cubical
    on Mar 20, 2020
  2. andreasabel commented on Mar 20, 2020

    @andreasabel
    Member

    If inductive families were disallowed (under some flag), would it be safe to loosen the restrictions?

    Well, if you do not have equality in the first place, there should be no difference in K or without-K.

  3. nad commented on Mar 20, 2020

    @nad
    CollaboratorAuthor

    In Cubical Agda you would still have paths.

  4. Saizan commented on Mar 24, 2020

    @Saizan
    Collaborator

    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 import Agda.Builtin.Nat
    
    data Unit : Set where
      unit : 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)
  5. nad commented on Mar 24, 2020

    @nad
    CollaboratorAuthor

    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.

  6. added this to the icebox milestone on May 22, 2020
  7. Saizan commented on Nov 10, 2020

    @Saizan
    Collaborator

    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.

  8. added 2 commits that reference this issue on Nov 3, 2022
    11e5f0b
    08e97b3
  9. nad commented on Jan 17, 2025

    @nad
    CollaboratorAuthor

    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 import Agda.Builtin.Unit
    open import Agda.Primitive
    
    private variable
      @0 a i l p : Level
    
    record Lift (@0 l) (A : Set a) : Set (a ⊔ l) where
      constructor lift
      field lower : A
    
    record Σ (A : Set a) (P : A → Set p) : Set (a ⊔ p) where
      constructor _,_
      field
        fst : A
        snd : P fst
    
    infixr 2 _⊗_
    
    data IDesc (@0 l) (I : Set i) : Set (lsuc l ⊔ i) where
      var   : 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 where
      con : ⟦ 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)
      where
      ind-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:

    data Sem {@0 l} {I : Set i} (P : I → Set p) :
           (@0 d : IDesc l I) → Set (lsuc l ⊔ p ⊔ i) where
      var   : {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?

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

    cubicalterminationtype: enhancementwithout-K

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions