Repository navigation
Open
Labels
cubicalCubical Agda paraphernalia: Paths, Glue, partial elements, hcomp, transpperformanceSlow type checking, interaction, compilation or execution of Agda programstype: enhancementIssues and pull requests about possible improvements
Milestone
Description
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:
- Attempting to give any of the postulates into any of the holes.
- 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
- Agda 2.6.1,
- the latest version of the
cubicallibrary: e862e8bea9c5e7cc58e101f9bdd89bbb0d4ca9fd, and - standard library 1.3.
Metadata
Metadata
Assignees
Labels
cubicalCubical Agda paraphernalia: Paths, Glue, partial elements, hcomp, transpperformanceSlow type checking, interaction, compilation or execution of Agda programstype: enhancementIssues and pull requests about possible improvements
Activity
nad commentedon Apr 11, 2020
Do you think you could create a smaller test case?
How do you know that Agda does not terminate (given enough time and space)?
ayberkt commentedon Apr 11, 2020
@nad
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.
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.
ayberkt commentedon Apr 11, 2020
@nad I can try making things with long proofs postulates maybe?
ayberkt commentedon Apr 11, 2020
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-posetare converted into postulates.ayberkt commentedon Apr 11, 2020
I managed to solve the problem by marking
<<-refland<<-antisymin the definition ofdownward-subset-posetas abstract:https://gist.github.com/ayberkt/3c8c5e3d4555f7723bbccd7a24702997#file-problem-agda-L518-L528
[-]Typechecking does not terminate in cubical[/-][+]Slow typechecking unless using abstract in cubical[/+]WorldSEnder commentedon Apr 19, 2020
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 20just hangs at some point with the last few lines being:Hope it helps.
WorldSEnder commentedon Apr 23, 2020
In my file, when I make this definition abstract, everything type checks "instantly". I believe this is connected to
rigidVarsNotContainedInfully reducing a complicated term that appears as an argument to the meta, when trying to prune the metaayberkt commentedon Apr 23, 2020
@WorldSEnder that sounds really similar to the problem I had.
WorldSEnder commentedon Apr 23, 2020
I found an alternative for my file, using no abstracts:
On line 290 giving an explicit argument to
Σ≡also solves the problem:WorldSEnder commentedon Apr 23, 2020
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:10the 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.Without giving an explicit
B, agda will try to solve this and when pruning arguments from the generated meta variable, the definition ofp≡qis getting fully reduced, since it shows up in the argument list.Note the additional argument
i. Is agda trying to solve something like{B = λ y i → _B_145}and hence considersp≡qrelevant?Saizan commentedon Apr 24, 2020
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 coursep≡qshows up in that unification problem.And indeed both
hasBadRigidandrigidVarsNotContainedInare 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?
nad commentedon Apr 24, 2020
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.
11 remaining items
Saizan commentedon May 6, 2022
With
--experimental-lossy-unificationsome 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.
mortberg commentedon May 6, 2022
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.
anuyts commentedon May 6, 2022
So it never has the effect of refuting a well-typed definition, i.e. it applies non-lossy unification if lossy unification fails?
Saizan commentedon May 6, 2022
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)
nad commentedon May 6, 2022
When things get too slow I usually try to rewrite the code. (See for instance my presentation from AIM XXXII.)
nad commentedon May 6, 2022
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.
nad commentedon May 18, 2022
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 withsuc (suc (suc (n + m + k))). I suspect thatKn→Ω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 toKn→ΩKn+1). Perhaps similar comments apply to_⌣ₖ_as well.mortberg commentedon May 19, 2022
I know @aljungstrom experimented a lot with locking things to speed up typechecking already? But maybe there are more definitions that should be locked?
aljungstrom commentedon May 19, 2022
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).
nad commentedon May 19, 2022
The first module that I encountered problems in was
Cubical.ZCohomology.RingStructure.RingLaws(see the local definitionfinalTransp).Here is the type signature given for
finalTransp:The first occurrence of
Kn→ΩKn+1 _stands forKn→ΩKn+1 (suc (suc (n + suc (m + k)))), and the second occurrence stands forKn→ΩKn+1 (suc (suc (suc (n + m + k)))).The normal form of
Kn→ΩKn+1 nis small. The pretty-printed normal form ofKn→ΩKn+1 (suc n)is 1219 lines long. I can imagine that the normal form ofKn→ΩKn+1 (suc (suc (suc (n + m + k))))is quite a bit larger.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.)
UlfNorell commentedon May 20, 2022
From https://agda.readthedocs.io/en/latest/tools/emacs-mode.html#keybindings (emphasis added):
nad commentedon May 20, 2022
Thanks! The weak head normal forms of the terms I mentioned are small. I noticed that I could give the body of
finalTransppiece by piece (in a certain order), but reloading takes a long time. (I don't have time to investigate this further right now.)