-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCommons.v
66 lines (50 loc) · 2.48 KB
/
Commons.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
From Coq Require Export ssreflect ssrfun ssrbool Omega Basics Equality.
Require Export mathcomp.ssreflect.eqtype.
Require Export mathcomp.ssreflect.seq.
Require Export mathcomp.ssreflect.ssrnat.
Require Export Ssromega.
Definition bind {X} {Y} (f : X -> option Y) (x : option X) : option Y :=
match x with Some x' => f x' | _ => None end.
Definition fmap {X Y} (f : X -> Y) : option X -> option Y :=
fun ox =>
match ox with
| Some x => Some (f x)
| None => None
end.
Definition maybe_eq {X} (eq : X -> X -> Prop) : option X -> option X -> Prop :=
fun a b =>
match a, b with
| Some a', Some b' => eq a' b'
| None, None => True
| _, _ => False
end.
Lemma fadd {X} {F : X -> nat} n l: foldl addn n [seq F i | i <- l] = n + foldl addn 0 [seq F i | i <- l].
elim: l n => [|l ls IHl] //= n. by rewrite addn0. by rewrite add0n (IHl (n + F l)) (IHl (F l)) addnA. Qed.
Lemma faddr {X} {F : X -> nat} n l: foldr addn n [seq F i | i <- l] = n + foldr addn 0 [seq F i | i <- l].
elim: l n => [|l ls IHl] //= n. by rewrite addn0. by rewrite addnA (addnC n) -addnA -(IHl n). Qed.
(* Some Numeric facts*)
Lemma le_ex : forall i k,
i <= k -> exists j, i + j = k.
Proof. move=> i k; elim: k i => [i|k IHk]. by rewrite leqn0 => /eqP ->; exists 0.
case => [|i]; first by exists k.+1.
by rewrite (leq_add2l 1) => /IHk [x] <-; exists x. Qed.
Lemma Nleq_ltnC: forall i k, i <= k -> k < i -> false.
Proof. move=> i k. by rewrite leqNgt => /negbTE ->. Qed.
Ltac maxapply H := move: H; repeat (let NH := fresh in move => NH /NH; move: NH => _).
Lemma ltn_addWr x y z: x + y < z -> x < z.
Proof. move=> /leq_trans A. move: (A _ (leq_addr y _)). by rewrite ltn_add2r. Qed.
Lemma leq_addWr x y z: x + y <= z -> x <= z.
Proof. move=> /leq_trans A. move: (A _ (leq_addr y _)). by rewrite leq_add2r. Qed.
Lemma ltn_addWl x y z: y + x < z -> x < z.
Proof. move=> /leq_trans A. move: (A _ (leq_addl y _)). by rewrite ltn_add2l. Qed.
Lemma leq_addWl x y z: y + x <= z -> x <= z.
Proof. move=> /leq_trans A. move: (A _ (leq_addl y _)). by rewrite leq_add2l. Qed.
Section MonadicNotation.
Context {A B : Type}.
Definition true_some (b : bool) (f : B) : option B :=
if b then Some f else None.
End MonadicNotation.
Notation "P --- Q" := (forall x, P x = false -> Q x = false) (at level 60).
Notation "ma >>= f" := (bind f ma) (at level 60).
Notation "[! b !] f" := (true_some b f) (at level 60).
Lemma compA {A B C D} (f3 : C -> D) (f2 : B -> C) (f1 : A -> B): (f3 \o f2) \o f1 =1 f3 \o (f2 \o f1). done. Qed.