Giving access in match to the expansion of the term being matched via an alias in order to support more fixpoints#73
Open
herbelin wants to merge 6 commits intorocq-prover:masterfrom
Conversation
b4abbdb to
0bf5474
Compare
SkySkimmer
reviewed
Dec 4, 2023
Comment on lines
+11
to
+63
| # Motivation | ||
|
|
||
| When writing fixpoints in inductive types with indices, there is a standard conflict between referring to the expansion of a variable being matched (so that its type corresponds to the type in the branch) or referring to the variable so that it is compatible with the guard when the fixpoint is later used in another fixpoint. A typical example (even without indices) is `Nat.sub`: | ||
| ```coq | ||
| Fixpoint sub (n m : nat) {struct n} : nat := | ||
| match n, m with | ||
| | S k, S l => sub k l | ||
| | _, _ => n | ||
| end | ||
| ``` | ||
| where it is important that `n` is not expanded. | ||
|
|
||
| The proposal is to resolve this conflict by giving an explicit name to the expansion of the term being matched in a branch so that the guard condition knows that it is a decreasing argument and not a constructor disconnected from the term being matched. For instance, `Nat.sub` would be written: | ||
| ```coq | ||
| fix sub (n m : nat) {struct n} : nat := | ||
| match n, m with | ||
| | S k, S l => sub k l | ||
| | _ as n', _ => n' | ||
| end | ||
| ``` | ||
|
|
||
| In the case of an inductive type with no indices, this does not provide much, but the situation changes for type families. For instance, imagine we want to apply parametricity to `Nat.sub`. That would give: | ||
| ```coq | ||
| Fixpoint is_sub (n : nat) (Pn : is_nat n) (m : nat) (Pm : is_nat m) : is_nat (Nat.sub n m) := | ||
| match Pn in (is_nat n) return is_nat (Nat.sub n m) with | ||
| | is_O => is_O | ||
| | is_S x P_ => | ||
| match | ||
| Pm in (is_nat m0) | ||
| return is_nat (match m0 with 0 => S x | S l => Nat.sub x l end) | ||
| with | ||
| | is_O => is_S x P_ | ||
| | is_S l Pl => is_sub x P_ l Pl | ||
| end | ||
| end. | ||
| ``` | ||
| where, for typing, we have to expand `Pn` into a constructor in the right-hand side. | ||
|
|
||
| With the proposed extension, we would write: | ||
| ```coq | ||
| Fixpoint is_sub (n : nat) (Pn : is_nat n) (m : nat) (Pm : is_nat m) : is_nat (Nat.sub n m) := | ||
| match Pn in (is_nat n) return is_nat (Nat.sub n m) with | ||
| | is_O as p => p | ||
| | is_S x P_ as p => | ||
| match | ||
| Pm in (is_nat m0) | ||
| return is_nat (match m0 with 0 => S x | S l => Nat.sub x l end) | ||
| with | ||
| | is_O => p | ||
| | is_S l Pl => is_sub x P_ l Pl | ||
| end | ||
| end. | ||
| ``` |
Contributor
There was a problem hiding this comment.
Maybe we should be using some sort of convoy pattern?
Fixpoint sub (n m : nat) {struct n} : nat :=
match n with
| 0 => fun n => n
| S k => fun n =>
match m with
| 0 => n
| S l => sub k l
end
end n.
Inductive is_nat : nat -> Prop :=
| is_O : is_nat 0
| is_S x : is_nat x -> is_nat (S x).
Fixpoint is_sub (n : nat) (Pn : is_nat n) (m : nat) (Pm : is_nat m) : is_nat (Nat.sub n m) :=
match Pn in (is_nat n) return is_nat n -> is_nat (Nat.sub n m) with
| is_O => fun p => p
| is_S x P_ => fun p =>
match
Pm in (is_nat m0)
return is_nat (match m0 with 0 => S x | S l => Nat.sub x l end)
with
| is_O => p
| is_S l Pl => is_sub x P_ l Pl
end
end Pn.works (not sure if that's exactly what parametricity would produce)
Member
Author
|
@SkySkimmer: Nice! Your "commutative cut" indeed provides a simple encoding of For the record, I add the working parametricity of (a simplification of) Fixpoint G (a b : nat) {struct a} : nat :=
match a return nat with
| O => b
| S a' => G (Nat.sub a' b) (S a')
end.
Fixpoint is_G a (is_a : is_nat a) b (is_b : is_nat b) {struct is_a} : is_nat (G a b) :=
match is_a in is_nat a return is_nat (G a b) with
| is_O => is_b
| is_S a' P_ => is_G _ (is_sub a' P_ b is_b) (S a') (is_S a' P_)
(* failed with the variant of is_sub w/o generalization that returns the expansion of Pn *)
end. |
Member
Author
|
I guess this new justification still leads to two possible points of view:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Rendered here.