| 1 |
(** |
| 2 |
This example shows how to use derive |
| 3 |
*) |
| 4 |
|
| 5 |
From elpi.apps Require Import derive.std. |
| 6 |
Set Uniform Inductive Parameters. |
| 7 |
|
| 8 |
(** The best way to call derive is to prefix an Inductive declaration. *) |
| 9 |
#[module] derive |
| 10 |
Inductive tickle A := stop | more : A -> tickle -> tickle. |
| 11 |
|
| 12 |
(** The command is elaborated to something like: |
| 13 |
|
| 14 |
Module tickle. |
| 15 |
Inductive tickle A := stop | more : A -> tickle -> tickle. |
| 16 |
derive tickle. |
| 17 |
End tickle. |
| 18 |
Notation tickle := tickle.tickle. |
| 19 |
Notation stop := tickle.stop. |
| 20 |
Notation more := tickle.more. |
| 21 |
|
| 22 |
*) |
| 23 |
Check more : |
| 24 |
forall A, A -> tickle A -> tickle A. |
| 25 |
|
| 26 |
(** Some goodies *) |
| 27 |
Check tickle.eqb : (* eq test *) |
| 28 |
forall A, (A -> A -> bool) -> tickle A -> tickle A -> bool. |
| 29 |
|
| 30 |
Check tickle.eqb_OK : (* eq test correctness proof *) |
| 31 |
forall A f, (forall x y, reflect (x = y) (f x y)) -> forall x y, reflect (x = y) (tickle.eqb A f x y). |
| 32 |
|
| 33 |
Check tickle.map : (* map the container *) |
| 34 |
forall A B, (A -> B) -> tickle A -> tickle B. |
| 35 |
|
| 36 |
Check tickle.tickle_R : (* relator (binary parametricity translation) *) |
| 37 |
forall A B, (A -> B -> Type) -> tickle A -> tickle B -> Type. |
| 38 |
|
| 39 |
(** This is a tricky case, since you need a good induction principle for the |
| 40 |
nested occurrence of tickle. #[verbose] prints all the derivations being |
| 41 |
run *) |
| 42 |
#[verbose,module] derive |
| 43 |
Inductive rtree A := Leaf (a : A) | Node (l : tickle rtree). |
| 44 |
|
| 45 |
Check rtree.induction : (* this is the key *) |
| 46 |
forall A PA P, |
| 47 |
(forall a, PA a -> P (Leaf A a)) -> |
| 48 |
(forall l, tickle.is_tickle (rtree A) P l -> P (Node A l)) -> |
| 49 |
forall x, rtree.is_rtree A PA x -> P x. |
| 50 |
|
| 51 |
(** You can also select which derivations you like *) |
| 52 |
#[verbose, only(lens_laws, eqb), module] derive |
| 53 |
Record Box A := { contents : A; tag : nat }. |
| 54 |
|
| 55 |
Check Box.eqb : |
| 56 |
forall A, (A -> A -> bool) -> Box A -> Box A -> bool. |
| 57 |
|
| 58 |
Import lens. |
| 59 |
|
| 60 |
Check @Box._tag : (* the Lens for the second field (A is implicit) *) |
| 61 |
forall A, Lens (Box A) (Box A) nat nat. |
| 62 |
|
| 63 |
Check Box._tag_set_set : (* a Lens law *) |
| 64 |
forall A (r : Box A) y x, set Box._tag x (set Box._tag y r) = set Box._tag x r. |
| 65 |
|
| 66 |
Check Box._tag_contents_exchange : (* another one *) |
| 67 |
forall A (r : Box A) x y, set Box._tag x (set Box._contents y r) = |
| 68 |
set Box._contents y (set Box._tag x r). |
| 69 |
|
| 70 |
(** Finally, one can derive an existing inductive type. |
| 71 |
Generated constants are |
| 72 |
prefixed with nat_ but won't be in the right |
| 73 |
place, which is where the type is defined. This means that two users |
| 74 |
may run derive for the same type in different files, leading to |
| 75 |
duplication. *) |
| 76 |
|
| 77 |
derive nat. |
| 78 |
|
| 79 |
Check nat_eqb_OK : |
| 80 |
forall x y, reflect (x = y) (nat_eqb x y). |
| 81 |
|
| 82 |
(** Once can also run derive recursively, but this has the same bad effect, |
| 83 |
all generated concepts will be out of place *) |
| 84 |
|
| 85 |
Inductive a := A. |
| 86 |
Inductive b := B : a -> b. |
| 87 |
|
| 88 |
#[recursive, only(eqbOK)] derive b. |
| 89 |
|
| 90 |
Check a_eqb. |
| 91 |
Check b_eqb. |