Skip to content

Slow typechecking unless using abstract in cubical #4573

Open

Description

@ayberkt

I have encountered a problem in the last step of a development I have been carrying out in Cubical Agda. I have three remaining holes in an equational proof, and the typechecker does not terminate when trying to give proofs for these holes.

I merged all my files into a single one to illustrate the problem: https://gist.github.com/ayberkt/3c8c5e3d4555f7723bbccd7a24702997.

In lines 1220-1228

        g~g′ : (𝔘 : ∣ L ∣F) → g 𝔘 ≡ g′ 𝔘
        g~g′ 𝔘 =
          g 𝔘                           ≡⟨ cong g (main-lemma 𝔘)                      ⟩
          g (⋃[ L ] (η ⊚ ⟪ ⦅ 𝔘 ⦆ ⟫))    ≡⟨ π₁ (π₁ g-frame-homo) (η ⊚ ⟪ ⦅ 𝔘 ⦆ ⟫)       ⟩
          ⋃[ R ] ((g  ∘ η) ⊚ ⟪ ⦅ 𝔘 ⦆ ⟫) ≡⟨ cong (λ - → ⋃[ R ] (- ⊚ ⟪ ⦅ 𝔘 ⦆ ⟫)) g∘η=f′ ⟩
          ⋃[ R ] (f ⊚ ⟪ ⦅ 𝔘 ⦆ ⟫)        ≡⟨ {! eq₀ !} ⟩
          ⋃[ R ] ((g′ ∘ η) ⊚ ⟪ ⦅ 𝔘 ⦆ ⟫) ≡⟨ {! eq₁ !} ⟩
          g′ (⋃[ L ] (η ⊚ ⟪ ⦅ 𝔘 ⦆ ⟫))   ≡⟨ {! eq₂ !} ⟩
          g′ 𝔘 ∎
          where
            postulate eq₀ : ⋃[ R ] (f ⊚ ⟪ ⦅ 𝔘 ⦆ ⟫) ≡ ⋃[ R ] ((g′ ∘ η) ⊚ ⟪ ⦅ 𝔘 ⦆ ⟫)
            -- eq₀ : ⋃[ R ] (f ⊚ ⟪ ⦅ 𝔘 ⦆ ⟫) ≡ ⋃[ R ] ((g′ ∘ η) ⊚ ⟪ ⦅ 𝔘 ⦆ ⟫)
            -- eq₀ = cong (λ - → ⋃[ R ] (- ⊚ ⟪ ⦅ 𝔘 ⦆ ⟫)) f=g′∘η
            postulate eq₁ : ⋃[ R ] ((g′ ∘ η) ⊚ ⟪ ⦅ 𝔘 ⦆ ⟫) ≡ g′ (⋃[ L ] (η ⊚ ⟪ ⦅ 𝔘 ⦆ ⟫))
            -- eq₁ : ⋃[ R ] ((g′ ∘ η) ⊚ ⟪ ⦅ 𝔘 ⦆ ⟫) ≡ g′ (⋃[ L ] (η ⊚ ⟪ ⦅ 𝔘 ⦆ ⟫))
            -- eq₁ = sym (π₁ (π₁ g′-frame-homo) (η ⊚ ⟪ ⦅ 𝔘 ⦆ ⟫))
            postulate eq₂ : g′ (⋃[ L ] ((η ⊚ ⟪ ⦅ 𝔘 ⦆ ⟫))) ≡ g′ 𝔘
            -- eq₂ : g′ (⋃[ L ] ((η ⊚ ⟪ ⦅ 𝔘 ⦆ ⟫))) ≡ g′ 𝔘
            -- eq₂ = cong g′ (sym (main-lemma 𝔘))

eq₀, eq₁, and eq₂ should go into holes 0, 1, and 2 (lines 1225-1227). Both of the following result in nonterminating typechecking:

  1. Attempting to give any of the postulates into any of the holes.
  2. Trying to actually write down proofs for these. You can see that the commented lines contain the required proofs of the postulates.

I am using

Activity

  1. nad commented on Apr 11, 2020

    @nad
    Collaborator

    Do you think you could create a smaller test case?

    How do you know that Agda does not terminate (given enough time and space)?

  2. ayberkt commented on Apr 11, 2020

    @ayberkt
    Author

    @nad

    How do you know that Agda does not terminate (given enough time and space)?

    I should have maybe said "Agda takes an unreasonable amount of time to typecheck" as I don't know for sure that it won't eventually terminate. I did try waiting for 3 hours 40 minutes with the heap size increased and it did not terminate. Other very similar goals in the same part of the proof take no more than a couple of seconds which is what made me think that this is looping, not just blowing up exponentially.

    Do you think you could create a smaller test case?

    I might be able to reduce the size of the test case to some extent, but I don't think it will be possible to reduce it substantially as the problem is occurring in the theorem that every other construction is building up towards.

  3. ayberkt commented on Apr 11, 2020

    @ayberkt
    Author

    I might be able to reduce the size of the test case to some extent, but I don't think it will be possible to reduce it substantially

    @nad I can try making things with long proofs postulates maybe?

  4. ayberkt commented on Apr 11, 2020

    @ayberkt
    Author

    Tried converting things to postulates one by one, and it seems that the problem is fixed when all the lemmas defined in the where clause in the definition of downwnard-subset-poset are converted into postulates.

  5. ayberkt commented on Apr 11, 2020

    @ayberkt
    Author

    I managed to solve the problem by marking <<-refl and <<-antisym in the definition of downward-subset-poset as abstract:

    https://gist.github.com/ayberkt/3c8c5e3d4555f7723bbccd7a24702997#file-problem-agda-L518-L528

  6. added
    cubical
    on Apr 12, 2020
  7. added
    performance
    on Apr 14, 2020
  8. changed the title [-]Typechecking does not terminate in cubical[/-] [+]Slow typechecking unless using abstract in cubical[/+] on Apr 17, 2020
  9. WorldSEnder commented on Apr 19, 2020

    @WorldSEnder

    I have a very similar problem with my code here. Not sure if related. Typechecking seems to go on forever when the definition on line 298 is given, when instead replaced by a hole, everything works fine.

    I did not yet figure out which definitions I need to make abstract to solve the issue in the way @ayberkt did, but the file is a little bit smaller.

    Running with -v 20 just hangs at some point with the last few lines being:

    initOccursCheck: we look into the following definitions:
    sett-repr
    Kernel
    Rep
    ixRep
    isEmbIxRep
    propFibers
    solution
    propP₁
    propP₂
    base
    ℓ
    sets.GeneralizeTel
    sets.mkGeneralizeTel
    passed occursCheck
    fvars rhs: 0 1 2 3 4 5 6 7 8
    

    Hope it helps.

  10. WorldSEnder commented on Apr 23, 2020

    @WorldSEnder

    In my file, when I make this definition abstract, everything type checks "instantly". I believe this is connected to rigidVarsNotContainedIn fully reducing a complicated term that appears as an argument to the meta, when trying to prune the meta

  11. ayberkt commented on Apr 23, 2020

    @ayberkt
    Author

    In my file, when I make this definition abstract, everything type checks "instantly". I believe this is connected to rigidVarsNotContainedIn fully reducing a complicated term that appears as an argument to the meta, when trying to prune the meta

    @WorldSEnder that sounds really similar to the problem I had.

  12. WorldSEnder commented on Apr 23, 2020

    @WorldSEnder

    I found an alternative for my file, using no abstracts:

    On line 290 giving an explicit argument to Σ≡ also solves the problem:

        propFibers y (p₁ , m) (p₂ , n) = Σ≡ {B = λ p → ixRep p ≡ y} .fst (base , isProp→PathP (λ _ → setIsSet _ _) _ _) where
    
  13. WorldSEnder commented on Apr 23, 2020

    @WorldSEnder

    I came up with a smaller test file. It's not exhibiting the problem since I couldn't come up with a complicated enough term, but with -v tc.meta.kill:10 the difference in generated metas (specifically one close to those not getting solved in the above files) can be observed.

    Toggle between the two definitions of slow.

    {-# OPTIONS --cubical #-}
    
    module testcase where
    
    open import Cubical.Foundations.Prelude
    open import Cubical.Foundations.HLevels
    open import Cubical.Foundations.Equiv
    open import Cubical.Data.Nat
    open import Cubical.Data.Sigma
    open import Cubical.HITs.SetQuotients as S
    
    data SetTruncatedComplicatedType : Type₀ where
      ByNat : ℕ → SetTruncatedComplicatedType
      eq : ∀ n → ByNat (suc n) ≡ ByNat (suc (suc (suc n)))
      setTruncC : isSet SetTruncatedComplicatedType
    
    Equality : (a b : SetTruncatedComplicatedType) → Type₀
    Equality a b = a ≡ b
    eq→≡ : (a b : SetTruncatedComplicatedType) → (Equality a b) ≃ (a ≡ b)
    eq→≡ a b = idEquiv _
    
    module _ (x : SetTruncatedComplicatedType) where
      Kernel : (a b : SetTruncatedComplicatedType) → Type₀
      Kernel = Equality
      Rep : Type₀
      Rep = SetTruncatedComplicatedType / Kernel
      ixRep : Rep → SetTruncatedComplicatedType
      ixRep = invEquiv (S.setQuotUniversal setTruncC) .fst ((λ x → x) , λ a b → eq→≡ a b .fst)
      slow : (p q : Σ[ y ∈ Rep ] (ixRep y ≡ x)) → p ≡ q
      slow (p , m) (q , n) = Σ≡ .fst (p≡q , isProp→PathP {B = λ i → ixRep (p≡q i) ≡ x} (λ i → setTruncC _ _) m n) where
      -- slow (p , m) (q , n) = Σ≡ {B = λ y → ixRep y ≡ x} .fst (p≡q , isProp→PathP {B = λ i → ixRep (p≡q i) ≡ x} (λ i → setTruncC _ _) m n) where
        prop1 : ∀ p → isProp ((ixRep p ≡ x) → p ≡ q)
        prop1 p = isPropΠ λ _ → squash/ p q
        prop2 : ∀ p q → isProp ((ixRep q ≡ x) → S.[ p ] ≡ q)
        prop2 p q = isPropΠ λ _ → squash/ S.[ p ] q
        p≡q : p ≡ q
        p≡q = S.elimProp prop1 (λ p m → S.elimProp (prop2 p) (λ q n → eq/ p q (invEquiv (eq→≡ p q) .fst (m ∙ sym n))) q n)  p m
    
    

    Without giving an explicit B, agda will try to solve this and when pruning arguments from the generated meta variable, the definition of p≡q is getting fully reduced, since it shows up in the argument list.

    attempting kills
    m' = _145
    vs = [x, p, m, q, n,
    elimProp prop1
    (λ p₂ m₂ →
    elimProp (prop2 p₂)
    (λ q₂ n₂ →
    eq/ p₂ q₂ (invEquiv (eq→≡ p₂ q₂) .fst (m₂ ∙ (λ i₁ → n₂ (~ i₁)))))
    q n)
    p m i]
    kills = [False,False,False,False,False,False]

    Note the additional argument i. Is agda trying to solve something like {B = λ y i → _B_145} and hence considers p≡q relevant?

  14. Saizan commented on Apr 24, 2020

    @Saizan
    Collaborator

    It's quite possible that agda is working on unifying the type of the second element of the pair with the type PathP (λ i → B (p i)) (snd x) (snd y) from the type of Σ≡. And of course p≡q shows up in that unification problem.

    And indeed both hasBadRigid and rigidVarsNotContainedIn are fairly eager to reduce stuff, that's very good to spot!

    Are there implicits we could give in @ayberkt example that would make typechecking fast without using abstract then?

  15. nad commented on Apr 24, 2020

    @nad
    Collaborator

    Apparently the problem causing #1386 was that Agda treated things as rigid even if they were not, and this was fixed by making the analysis more precise. An alternative might be to have a less costly analysis that answers "don't know" more often (rather than incorrectly answering "yes"). However, it seems to me as if such a change would lead to (more) situations in which the code breaks when one replaces one piece of code with a definitionally equal piece of code.

    Perhaps it would be better to focus attention on #4607.

  16. 11 remaining items

  17. Saizan commented on May 6, 2022

    @Saizan
    Collaborator

    With --experimental-lossy-unification some unification variables will be solved even if they might have more than one correct solution, which means that you might have expected them to be solved some other way.

    I would expect that in most cases if a variable gets solved with an unexpected term you'll realize when you try to prove something about the affected definition.

  18. mortberg commented on May 6, 2022

    @mortberg
    Contributor

    With --experimental-lossy-unification some unification variables will be solved even if they might have more than one correct solution, which means that you might have expected them to be solved some other way.

    I would expect that in most cases if a variable gets solved with an unexpected term you'll realize when you try to prove something about the affected definition.

    Ok, this seems like it's pretty much never a problem in practice for us and having type checking be fast (seconds instead of minutes) is much more important than Agda very rarely being unpredictable.

  19. anuyts commented on May 6, 2022

    @anuyts
    Contributor

    With --experimental-lossy-unification some unification variables will be solved even if they might have more than one correct solution, which means that you might have expected them to be solved some other way.

    So it never has the effect of refuting a well-typed definition, i.e. it applies non-lossy unification if lossy unification fails?

  20. Saizan commented on May 6, 2022

    @Saizan
    Collaborator

    It does also have that drawback: picking the "wrong" solution can lead to type errors in some other part of the code.

    (I was going to add there is no backtracking in Agda's unification, but actually there is a tiny bit within the solution of a given constraint, but it's not enough to prevent this problem, and we would not want to add more backtracking)

  21. nad commented on May 6, 2022

    @nad
    Collaborator

    Right, but what we noticed (especially when working on the Z-cohomology stuff) is that it's pretty much impossible to do anything without the --experimental-lossy-unification flag as Agda gets so slow without it activated....

    When things get too slow I usually try to rewrite the code. (See for instance my presentation from AIM XXXII.)

  22. nad commented on May 6, 2022

    @nad
    Collaborator

    I would expect that in most cases if a variable gets solved with an unexpected term you'll realize when you try to prove something about the affected definition.

    Perhaps it is possible for some malicious actor to come up with a theorem statement that looks like it says one thing, but actually means something else.

  23. nad commented on May 18, 2022

    @nad
    Collaborator

    Right, but what we noticed (especially when working on the Z-cohomology stuff) is that it's pretty much impossible to do anything without the --experimental-lossy-unification flag as Agda gets so slow without it activated....

    I'm investigating whether @vlopezj's work on heterogeneous unification (#5234) can be merged. I tried to get the cubical library to type-check with the new implementation of unification, and noticed that some code related to ℤ-cohomology was very slow. If the code relies on artefacts of the unification algorithm, then the code can break if unification changes.

    I briefly tried to optimise the code, before deciding to comment it out. I noticed that some goal contained Kn→ΩKn+1 _, where the underscore was unified with suc (suc (suc (n + m + k))). I suspect that Kn→ΩKn+1 _ might unfold to something fairly large. If you want to improve performance, then it might be worthwhile to ensure that this term does not unfold too much (perhaps by adding an extra "locking" argument to Kn→ΩKn+1). Perhaps similar comments apply to _⌣ₖ_ as well.

  24. mortberg commented on May 19, 2022

    @mortberg
    Contributor

    Right, but what we noticed (especially when working on the Z-cohomology stuff) is that it's pretty much impossible to do anything without the --experimental-lossy-unification flag as Agda gets so slow without it activated....

    I'm investigating whether @vlopezj's work on heterogeneous unification (#5234) can be merged. I tried to get the cubical library to type-check with the new implementation of unification, and noticed that some code related to ℤ-cohomology was very slow. If the code relies on artefacts of the unification algorithm, then the code can break if unification changes.

    I briefly tried to optimise the code, before deciding to comment it out. I noticed that some goal contained Kn→ΩKn+1 _, where the underscore was unified with suc (suc (suc (n + m + k))). I suspect that Kn→ΩKn+1 _ might unfold to something fairly large. If you want to improve performance, then it might be worthwhile to ensure that this term does not unfold too much (perhaps by adding an extra "locking" argument to Kn→ΩKn+1). Perhaps similar comments apply to _⌣ₖ_ as well.

    I know @aljungstrom experimented a lot with locking things to speed up typechecking already? But maybe there are more definitions that should be locked?

  25. aljungstrom commented on May 19, 2022

    @aljungstrom

    I actually don't think Kn→ΩKn+1 _ or _⌣ₖ_ should be problematic. Its definition is very simple and it's defined via truncation elimination anyway, so I don't think it should unfold very often. Its inverse is probably more problematic though. Regarding _⌣ₖ_, it's defined via pattern-matching (and N-induction), so that guy should rarely unfold to anything.

    My guess is that the slow type checking mainly is due to all of the path algebra. The problem there is, of course, that it's harder to lock definitions - often one wants to get into the nitty-gritty details of the paths concerned.

    Was there any file that was particularly problematic? I know the torus one has been problematic in the past (and should probably be removed soon anyway since we now have more general results in another file).

  26. nad commented on May 19, 2022

    @nad
    Collaborator

    Was there any file that was particularly problematic?

    The first module that I encountered problems in was Cubical.ZCohomology.RingStructure.RingLaws (see the local definition finalTransp).

    I actually don't think Kn→ΩKn+1 _ or _⌣ₖ_ should be problematic. Its definition is very simple and it's defined via truncation elimination anyway, so I don't think it should unfold very often. Its inverse is probably more problematic though.

    Here is the type signature given for finalTransp:

    finalTransp : (n : ℕ) (a : _) → Kn→ΩKn+1 _ (subst coHomK (λ i₁ → +'-assoc (suc n) (suc m) (suc k) (~ i₁)) ((∣ a ∣ ⌣ₖ b) ⌣ₖ c))
                ≡ cong (subst coHomK (λ i₁ → +'-assoc (2 + n) (suc m) (suc k) (~ i₁))) (Kn→ΩKn+1 _ ((∣ a ∣ ⌣ₖ b) ⌣ₖ c))

    The first occurrence of Kn→ΩKn+1 _ stands for Kn→ΩKn+1 (suc (suc (n + suc (m + k)))), and the second occurrence stands for Kn→ΩKn+1 (suc (suc (suc (n + m + k)))).

    The normal form of Kn→ΩKn+1 n is small. The pretty-printed normal form of Kn→ΩKn+1 (suc n) is 1219 lines long. I can imagine that the normal form of Kn→ΩKn+1 (suc (suc (suc (n + m + k)))) is quite a bit larger.

    Regarding _⌣ₖ_, it's defined via pattern-matching (and N-induction), so that guy should rarely unfold to anything.

    Apparently it takes a long time for Agda to normalise "((∣ a ∣ ⌣ₖ b) ⌣ₖ c)" (I did not wait for the computation to finish).

    (In this case it would perhaps be more reasonable to ask what the weak head normal forms are, rather than the normal forms. I think it was suggested recently to add a command for this, but I did not find a ticket for it.)

  27. UlfNorell commented on May 20, 2022

    @UlfNorell
    Member

    (In this case it would perhaps be more reasonable to ask what the weak head normal forms are, rather than the normal forms. I think it was suggested recently to add a command for this, but I did not find a ticket for it.)

    From https://agda.readthedocs.io/en/latest/tools/emacs-mode.html#keybindings (emphasis added):

    Commands working with terms or types can be prefixed with C-u to compute without further normalisation, with C-u C-u to compute normal forms, and C-u C-u C-u to compute weak-head normal forms.

  28. nad commented on May 20, 2022

    @nad
    Collaborator

    Thanks! The weak head normal forms of the terms I mentioned are small. I noticed that I could give the body of finalTransp piece by piece (in a certain order), but reloading takes a long time. (I don't have time to investigate this further right now.)

  29. added
    type: enhancement
    on Aug 26, 2022
  30. modified the milestones: 2.6.3, Later on Aug 26, 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

    cubicalperformancetype: enhancement

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions