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