Skip to content

#6238: Allow flat matching on inductive families - #6239

Merged
plt-amy merged 8 commits into
agda:masterfrom
felixwellen:flat-matching
Oct 28, 2022
Merged

plt-amy merged 8 commits into
agda:masterfrom
felixwellen:flat-matching

Conversation

@felixwellen

Copy link
Copy Markdown
Contributor

As argued in #6238, it should be possible to match on flat variables of inductive families.
As suggested by @nad , this can be fixed analogous to how it was fixed for Irrelevance here.

@felixwellen felixwellen added cubical Cubical Agda paraphernalia: Paths, Glue, partial elements, hcomp, transp flat The @flat/@♭ modality labels Oct 26, 2022
@plt-amy

plt-amy commented Oct 26, 2022 •

Copy link
Copy Markdown
Contributor

As far as --cubical is concerned, I'm not sure that silencing the check is the correct fix here: I'm not even sure what the correct fix should be (note that with only --without-K, the code is accepted).

Let's try rewriting the definition to use paths. Initially, only the domain: this is a simplification of the cause translation that --cubical will perform when extending CommFlatId to cover transp (λ i → Id x (p i) φ x, so it makes sense IMO that it would type check:

module _ {@♭ A : Set} {@♭ u v : A} where
  p : @♭ (u ≡ v) → Id (flat u) (flat v)
  p path = transp (λ i → Id (flat u) (flat (path i))) i0 reflId

This doesn't check because in that inner term we need to have @⊤ i : I ⊢ path i : A. We could try making it land in paths, too:

  q : @♭ (u ≡ v) → flat u ≡ flat v
  q path i = flat (path i)

But of course we hit the same problem, since now the i binder in PathP isn't marked @♭ (I'm not sure that'd be enough to get the i argument to get the correct modality here but let's just pretend that it would be 🙂). We'd have to go and make essentially every cubical primitive bind their interval variables in @i, since I'm ~half sure that one of the generated clauses will try to hcomp under @♭, too.
Edit: we'd have to make primTransp, primHComp and primComp be @♭ variables, too (this escaped me because I really quickly built an Agda that doesn't check the cohesion of any primitives, to play around with this).

@felixwellen

Copy link
Copy Markdown
Contributor Author

Can you explain why silencing the check could be bad?
I can't really follow all the cubical things you wrote, but I have the feeling that it would be fitting, if all interval variables are '@♭' somehow.

@plt-amy

plt-amy commented Oct 26, 2022

Copy link
Copy Markdown
Contributor

I guess the core of the point I'm making is: in Cubical Agda, indexed types are implemented as types which store paths, so to commute "type indexing" and Flat, we also need to commute paths and Flat. You can't do this today, because the cubical primitives are all using the default modalities for their binders (and live in the default modality, but that's an easy thing to fix).

So a "better" fix for this, IMO, would be to keep the check as-is but make the generated code modality-correct, and that starts by making p and q go through. (Note that you can implement p/q by first smuggling it into an identification, applying CommFlatId, then turning the identification back into a path!)

@nad

nad commented Oct 26, 2022

Copy link
Copy Markdown
Collaborator

Is the cohesive stuff supposed to work together with Cubical Agda? One option might be to hide the cohesive stuff behind a flag, and make this flag incompatible with --cubical-compatible.

@felixwellen

Copy link
Copy Markdown
Contributor Author

@nad: I don't want to stall the release with this - when I need it, I'll just use not put it in a library and use a patched version of agda.

@nad

nad commented Oct 26, 2022

Copy link
Copy Markdown
Collaborator

Others might also use this feature.

An alternative fix is perhaps to turn off the check only when --cubical-compatible is not active.

@felixwellen

Copy link
Copy Markdown
Contributor Author

I guess the core of the point I'm making is: in Cubical Agda, indexed types are implemented as types which store paths, so to commute "type indexing" and Flat, we also need to commute paths and Flat. You can't do this today, because the cubical primitives are all using the default modalities for their binders (and live in the default modality, but that's an easy thing to fix).

So a "better" fix for this, IMO, would be to keep the check as-is but make the generated code modality-correct, and that starts by making p and q go through. (Note that you can implement p/q by first smuggling it into an identification, applying CommFlatId, then turning the identification back into a path!)

Yes! That is actually what I did here.

@felixwellen

Copy link
Copy Markdown
Contributor Author

Others might also use this feature.

An alternative fix is perhaps to turn off the check only when --cubical-compatible is not active.

I'm a bit confused about the status of this. Last time I tried, this worked on master, but isn't there a PR that stops that?

@felixwellen

Copy link
Copy Markdown
Contributor Author

Yes! That is actually what I did here.

I mean, I wrote user level code doing that - what you suggest seems to be on the level of generated code?

@plt-amy

plt-amy commented Oct 26, 2022 •

Copy link
Copy Markdown
Contributor

My understanding of the situation is that we have the following two independent-ish problems:

  • The --cubical-compatible (née --without-K, post-2.6.2.2 but pre-#6212) test case fails, because it internally generates (then checks) some modality-incorrect code. This internal generated code is, modulo noise, my function p from this comment. Agda is correctly rejecting this code: primTransp isn't flat, PathP isn't flat, transp takes an (i : I) → ... rather than (@♭ i : I) → ... (and the φ argument should be flat too!)

  • The modality check in my open PR does not reject this code. It doesn't try very hard to approximate the transport which Cubical Agda will generate, since approximating that transport is essentially generating the that internal code, which we want to avoid, because as @nad found out in this comment on #6049, it's super expensive).

And there are three-ish possible fixes, as I see it:

  1. I correct my PR so it rejects the code above, rather than it only getting rejected with --cubical-compatible. That's effectively closing Allow flat matching on inductive families #6238 WONTFIX. Pros: At least no internal cubical code will get thrown on the users' faces? (This option sucks.) Cons: I defer to your expertise when saying that the code above is fine, so breaking it would be a shame!

  2. We merge this, which causes the clause generated by Cubical Agda to not be rejected. Pro: Your test case works, and the fix doesn't break anything, other than my heart. Cons: Agda is internally accepting code that, were the user to write it, it would reject; we also stay in the status quo, where paths are only kinda-sorta integrated with @♭.

  3. We make it so that p (hence the internal code, hence your example) are accepted, somehow. E.g.:

    • One obvious approach would be to make every interval binder in the Cubical primitives @♭-modal, but that would probably break far too much code (... and might get in the way of my idea for fixing Finite equality for empty domains breaks subject reduction #6016, which I'll try to write up soon, but that's tangential).
    • Most of the cubical primitives already have lies for types (try defining a wrapper around primTransp using its stated type - you can't); It might be possible to magic them up even more, so they lie about the modalities of their binders depending on the modality of their context. I'm not a fan, but it would work.

Having put it like this, even though I objected initially, I think approach (2), with the goal of eventually moving to (3). But then we should have a separate issue for integrating @♭ and cubical.

@felixwellen

felixwellen commented Oct 26, 2022 •

Copy link
Copy Markdown
Contributor Author

I see - thanks for putting everything together so clearly.
I didn't think about the Con in (2) . I feel a bit reluctant to propose the change then, but (1) seems worse and (3) is out of reach for me - I don't know how feasible the first and how evil the second option of (3) is...

@nad

nad commented Oct 27, 2022

Copy link
Copy Markdown
Collaborator

I don't think we should do any large changes just before the release, but I also don't think we should break code that used to work without having a good reason for doing so. Is there any "real" code that uses both the flat modality and Cubical Agda? If not, then I think we should hide the flat modality behind a flag (if that is easy) and disallow the combination of this modality and --cubical-compatible. The combination of the flat modality and Cubical Agda could be addressed later, if anyone is interested in this.

@plt-amy

plt-amy commented Oct 27, 2022

Copy link
Copy Markdown
Contributor

Decision: We'll add a new --flat flag, which is not --safe when used together with --cubical-compatible; When --flat is used, generating the trX clauses will be skipped (and so --flat --cubical will have the same computational behaviour for inductive types as in 2.6.2.2)

@andreasabel andreasabel added this to the 2.6.3 milestone Oct 27, 2022
@plt-amy

plt-amy commented Oct 27, 2022

Copy link
Copy Markdown
Contributor

... and turns out we already have --flat-split, so I'll use that.

@nad

nad commented Oct 27, 2022

Copy link
Copy Markdown
Collaborator

Is the new plan to not have a flag that enables the flat modality? I think it would make sense to have such a flag.

@felixwellen

Copy link
Copy Markdown
Contributor Author

... and turns out we already have --flat-split, so I'll use that.

I thought that was about something else. I think it was supposed to disallow some pattern matching and Andrea put it in after Colin Zwanziger asked about it.

@plt-amy

plt-amy commented Oct 27, 2022 •

Copy link
Copy Markdown
Contributor

My idea was to make --without-K (and descendants) imply --no-flat-split, and to make --flat-split infective (edit: not co!) when used together with --without-K.
Then if --cubical-compatible and --flat-split are used together (which would not be --safe), generating the transpX&co clauses for splitting on @♭ would be skipped.
That way we don't have to settle on --flat (and whether it should be by default) right now.

@felixwellen

Copy link
Copy Markdown
Contributor Author

Ah - I see. Makes sense to me.

felixwellen and others added 4 commits October 27, 2022 12:43
When --flat-split and --cubical(-compatible) are used (which is not
--safe), --flat-split behaves as an injective flag. Furthermore, in that
case, the transpX clauses will not be generated for the function. (We
skip this by failing the LeftInv computation, so that a
NoEquivWhenSplitting warning is printed indicating the defn. won't
compute)
@plt-amy
plt-amy marked this pull request as ready for review October 27, 2022 16:57
Comment thread test/Succeed/exec-tc/executables Resolved
@andreasabel

andreasabel commented Oct 28, 2022 •

Copy link
Copy Markdown
Member

I did a formal review:

  • mention --without-K also in the changelog
  • remove absolute exec path
  • if you have, an example with --without-K and (by default) disabled --split-flat would be nice (triggering an error in the LHS check). So far, there is one for --cubical-compatible if I remember correctly.

@andreasabel

Copy link
Copy Markdown
Member

(Meta comment concerning branch protection: We did not explicitly/consciously activate it, but now merging seems to be blocked when someone requests changes. Ok, let's run with it. Also, it seems one can still directly push to master, which I expected to be forbidden. Maybe because I am admin?)

@plt-amy plt-amy self-assigned this Oct 28, 2022

@andreasabel andreasabel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fixing!
If you want to add one more test (--without-K) that would be nice.

@plt-amy

plt-amy commented Oct 28, 2022

Copy link
Copy Markdown
Contributor

Maybe because I am admin?

I think admins do bypass branch protection rules, yeah.

but now merging seems to be blocked when someone requests changes

If there have been commits to the PR branch since the review, then admins and people with write access can dismiss them (quoting the GH docs):

If a pull request has changed since it was reviewed and the person who requested changes isn't available to give an approving review, repository administrators or people with write access can dismiss a review. This changes the status of the review to a review comment. When you dismiss a review, you must add a comment explaining why you dismissed it. Your comment will be added to the pull request conversation.

@andreasabel

Copy link
Copy Markdown
Member

Thanks for the added test!

(N.B. Concerning stray files: Since I use magit (emacs mode for git), I never add files by accident. Happened to me in the past when I used the command line. Now I sometimes forget to add files, but this is caught by the CI. Magit also encourages me to review my changes before committing, because it shows me the diff. I suppose any similar gui/tui tool would have the same benefits.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cubical Cubical Agda paraphernalia: Paths, Glue, partial elements, hcomp, transp flat The @flat/@♭ modality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants