forked from PrincetonUniversity/VST
-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic.v
147 lines (107 loc) · 3.39 KB
/
basic.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
Load loadpath.
Require Import Coq.Lists.List Coq.Arith.EqNat Coq.Arith.Compare_dec
Coq.ZArith.ZArith.
Require Import veristar.tactics.
Set Implicit Arguments.
Unset Strict Implicit.
Section option.
Variables (A B : Type) (h : A -> B) (f : A -> option B) (o : option A).
Definition omap := match o with Some a => Some (h a) | None => None end.
Definition obnd := match o with Some a => f a | None => None end.
End option.
Implicit Arguments omap [A B].
Implicit Arguments obnd [A B].
Definition isSome {A : Type} (o : option A) :=
match o with Some _ => true | _ => false end.
Section comp.
Variables (A B C : Type) (g : B -> C) (f : A -> B) (a : A).
Definition compose := g (f a).
End comp.
Implicit Arguments compose [A B C].
(*new notation*)
(*Notation "f \o g" := (compose f g) (at level 54, right associativity).*)
(*for backwards compatibility*)
Infix "oo" := compose (at level 54, right associativity).
(*why does List appear to not export this notation?*)
Notation "[ ]" := nil.
Notation "[ x , .. , y ]" := (cons x .. (cons y []) ..).
Fixpoint zip_with_acc {A B : Type} acc (l1 : list A) (l2 : list B) :=
match l1, l2 with
| a :: l1', b :: l2' => (a, b) :: zip_with_acc acc l1' l2'
| _, _ => acc
end.
Definition zip {A B : Type} := @zip_with_acc A B [].
Section iter.
Variables (A : Type) (f : nat -> A -> A).
Fixpoint iter (n : nat) (a : A) :=
match n with
| O => a
| S n' => iter n' (f n' a)
end.
End iter.
Implicit Arguments iter [A].
Section tryfind.
Variables (A E B : Type) (f : A -> E + B).
Fixpoint tryfind (err : E) (l : list A) :=
match l with
| nil => inl _ err
| a :: l' => match f a with
| inl err' => tryfind err' l'
| inr success as r => r
end
end.
End tryfind.
Definition max (n m : nat) := if leb n m then m else n.
Definition maxs (l : list nat) := fold_left (fun m n => max m n) l 0.
Definition elemb (n : nat) := existsb (fun m => beq_nat n m).
(*positive lemmas*)
Require Import ZArith.
Lemma Ppred_decrease n : (n<>1)%positive -> (nat_of_P (Ppred n)<nat_of_P n)%nat.
Proof.
intros; destruct (Psucc_pred n) as [Hpred | Hpred]; try contradiction;
pattern n at 2; rewrite <- Hpred; rewrite nat_of_P_succ_morphism; omega.
Defined.
Ltac Ppred_tac n :=
apply Ppred_decrease; destruct n;
let H := fresh "H" in intro H; try (inversion H||inversion H1); congruence.
Definition Pleb (x y : positive) :=
match Pcompare x y Eq with
| Lt => true
| Eq => true
| Gt => false
end.
Lemma Pleb_Ple (x y : positive) : Pleb x y = true <-> Ple x y.
Proof.
unfold Pleb, Ple; split; intro H1.
intro H2; done(rewrite H2 in H1).
done(remember ((x ?= y)%positive Eq) as b; destruct b).
Qed.
(* N lemmas *)
Require Import NArith NOrderedType.
Definition Nleb (x y : N) :=
match Ncompare x y with
| Lt => true
| Eq => true
| Gt => false
end.
Lemma Nleb_Nle (x y : N) : Nleb x y = true <-> Nle x y.
Proof.
unfold Nleb, Nle; split; intro H1.
intro H2; done(rewrite H2 in H1).
done(remember ((x ?= y)%N) as b; destruct b).
Qed.
(*reverse comparison*)
Section revc.
Variables (A : Type) (c : A -> A -> comparison).
Definition revc a1 a2 :=
match c a1 a2 with
| Gt => Lt
| Eq => Eq
| Lt => Gt
end.
End revc.
Inductive ret_kind (val : Type) : Type :=
| Success : val -> ret_kind val
| Failure : ret_kind val.
Implicit Arguments Success [val].
Implicit Arguments Failure [val].