-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDemo.v
99 lines (82 loc) · 2.14 KB
/
Demo.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
93
94
95
96
97
From Tuto1 Require Import Loader.
(*** Defining terms ***)
(*
* MyDefine works a lot like Definition.
* Some examples:
*)
MyDefine n := 1.
Print n.
MyDefine f := (fun (x : Type) => x).
Print f.
MyDefine definition := 5.
Print definition.
MyDefine foo := (fun (T : Type) => forall (P : T -> Type) (t : T), P t).
Print foo.
(*** Reasoning about terms ***)
(*
* The Count command counts the occurrences
* definitionally equal to the first term
* inside of the second term. This makes some
* simplifying assumptions about the format of terms.
*)
Count nat in (foo nat). (* 1 *)
Count nat in (fun (n : nat) => n). (* 1 *)
(*
* Since it's definitional equality, we don't need
* to rely on syntax. We can define our own constant
* wrapping nat, for example, and get the same behavior:
*)
Definition my_nat := nat.
Count my_nat in (foo nat). (* 1 *)
(*
* Similarly, since 8 is sugar for S (S (S ... 0)),
* we can count the occurrences of S in 8 to get 8.
* Or, we can pass in (fun (n : nat) => 1 + n),
* which is definitionally equal to S.
*)
Count S in 8. (* 8 *)
Count (fun (n : nat) => 1 + n) in 8. (* 8 *)
(*** Both together ***)
(*
* The fun part! The substitution command you will implement.
*)
(*
* Turn 4 (sugar for (S (S (S (S 0))))) into
* [1; 1; 1; 1] (sugar for (cons 1 (cons 1 ... nil))).
* Do this by substituting the constructors of nat
* with the constructors of lists.
*)
Sub O S with (@nil nat) (cons 1) in 4 as list_4.
Lemma test1:
list_4 = (cons 1 (cons 1 (cons 1 (cons 1 nil)))).
Proof.
reflexivity.
Qed.
(*
* Convert unary natural numbers to binary numbers!
*)
Require Import Coq.NArith.BinNatDef Coq.NArith.BinNat.
Sub O S with N.zero N.succ in 256 as two_fifty_six_binary.
Lemma test2:
two_fifty_six_binary = 256%N.
Proof.
reflexivity.
Qed.
(*
* Convert the addition function of unary into an addition
* function defined over binary!
*)
Sub nat nat_rec O S with N.t N.peano_rec N.zero N.succ in
(fun (n m : nat) =>
nat_rec
(fun _ => nat -> nat)
(fun m => m)
(fun p add_p m => S (add_p m))
n
m)
as add_bin.
Lemma test3:
add_bin 128%N 12%N = 140%N.
Proof.
reflexivity.
Qed.