-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinductive_types.v
95 lines (55 loc) · 1.69 KB
/
inductive_types.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
Inductive I (A : Type) (x : A) : A -> Type :=
refl : I A x x.
Arguments refl {_} _.
Notation "x = y" := (I _ x y): type_scope.
Definition subst (A:Type) (t:A) (P : forall y:A, t = y -> Type)
(u : A) (p : t = u) (v:P t (refl t)) : P u p :=
match p with
| refl _ => v (* : P t (refl t) *)
end.
Section subst_computation.
Variable A : Type.
Variable t : A.
Variable P : forall y:A, t = y -> Type.
Variable v : P t (refl t).
Eval cbn in
subst A t P t (refl t) v.
End subst_computation.
Inductive nat : Set :=
O : nat
| S : nat -> nat.
Fixpoint rec (P : nat -> Type) (P0 : P O) (PS: forall n : nat, P n -> P (S n)) (n : nat) : P n :=
match n with
| O => P0
| S n' => PS n' (rec P P0 PS n')
end.
Section nat_computation.
Variable n : nat.
Variable P : nat -> Type.
Variable P0 : P O.
Variable PS : forall n : nat, P n -> P (S n).
Eval cbn in
rec P P0 PS O.
Eval cbn in
rec P P0 PS (S n).
Eval cbn in
rec P P0 PS (S (S (S O))).
End nat_computation.
Fixpoint plus (n : nat) : nat -> nat :=
fun m =>
match n with
| O => m
| S n' => S (plus n' m)
end.
Check (fun m => refl m) : forall m:nat, plus O m = m.
Fail Check (fun n => refl n) : forall n:nat, plus n O = n.
Definition ap {A B:Type} (f:A -> B) {x y:A} (p:x = y) : f x = f y
:= match p with refl _ => refl (f x) end.
Fixpoint plus_O_r (n : nat) : plus n O = n :=
match n with
| O => refl O (* : plus O O = O *)
| S n' => ap S (plus_O_r n') (* S (plus n' 0) = S n' *)
end.
(* Alternative definition of dependent pairs *)
Inductive sigma (A: Type) (B: A -> Type) : Type :=
| exist : forall (a:A), B a -> sigma A B.