-
Notifications
You must be signed in to change notification settings - Fork 0
/
nats.v
132 lines (109 loc) · 2.14 KB
/
nats.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
Inductive nat : Type :=
| O : nat
| S : nat -> nat.
Definition pred (n : nat) : nat :=
match n with
| O => O
| S n' => n'
end.
Definition succ (n:nat) :nat :=
match n with
| O => (S O)
| S n' => S (S n')
end.
Definition minustwo (n: nat) : nat :=
match n with
| O => O
| S O => O
| S (S n') => n'
end.
Fixpoint evenb (n:nat) : bool :=
match n with
| O => true
| S O => false
| S n' => negb (evenb n')
end.
Definition oddb (n:nat) : bool := negb (evenb n).
Fixpoint plus (n:nat) (m:nat) : nat :=
match n with
| O => m
| S n' => S (plus n' m)
end.
Fixpoint mult (n m :nat) : nat :=
match n with
| O => O
| S O => m
| S n' => plus m (mult n' m)
end.
Fixpoint minus (n m : nat) : nat :=
match n, m with
| O, _ => O
| S n', O => n
| S n, S m => minus n m
end.
Fixpoint exp (base power :nat) : nat :=
match power with
| O => S O
| S n => mult base (exp base n)
end.
Fixpoint factorial (n:nat) : nat :=
match n with
| O => S O
| S O => S O
| S n' => mult n (factorial n')
end.
Notation "x + y" := (plus x y)
(at level 50, left associativity)
: nat_scope.
Notation "x - y" := (minus x y)
(at level 50,left associativity)
: nat_scope.
Notation "x * y" := (mult x y)
(at level 40,left associativity)
: nat_scope.
Fixpoint beq_nat (n m : nat) : bool :=
match n with
| O => match m with
| O => true
| S m' => false
end
| S n' => match m with
| O => false
| S m' => (beq_nat n' m')
end
end.
Fixpoint blt_nat (n m : nat) : bool :=
match n with
| O => match m with
| O => false
| S _ => true
end
| S n' => match m with
| O => false
| S m' => blt_nat n' m'
end
end.
Inductive bin : Type :=
| Z : bin
| T : bin -> bin
| T' : bin -> bin.
Fixpoint incr (n : bin) : bin :=
match n with
| Z => T' Z
| T n' => T' n'
| T' n' => T (incr n')
end.
Fixpoint bin_pred (n: bin) : bin :=
match n with
| Z => Z
| T Z => Z
| T' Z => Z
| T' n' => T n'
| T n' => T' (bin_pred n')
end.
Fixpoint bin_to_nat (n: bin) : nat :=
match n with
| Z => O
| T' n' => succ (bin_to_nat (T n'))
| T n' => succ (bin_to_nat (T' (bin_pred n')))
end.