1 | include "basics/types.ma". |
---|
2 | include "arithmetics/nat.ma". |
---|
3 | include "utilities/option.ma". |
---|
4 | |
---|
5 | (* JHM: here, for definiteness; used in ASM/ASM.ma *) |
---|
6 | let rec nat_bound_opt (N:nat) (n:nat) : option (n < N) ≝ |
---|
7 | match N return λy. option (n < y) with |
---|
8 | [ O ⇒ None ? |
---|
9 | | S N' ⇒ |
---|
10 | match n return λx. option (x < S N') with |
---|
11 | [ O ⇒ (return (lt_O_S ?)) |
---|
12 | | S n' ⇒ (! prf ← nat_bound_opt N' n' ; return (le_S_S ?? prf)) |
---|
13 | ] |
---|
14 | ]. |
---|
15 | |
---|
16 | inductive nat_compared : nat → nat → Type[0] ≝ |
---|
17 | | nat_lt : ∀n,m:nat. nat_compared n (n+S m) |
---|
18 | | nat_eq : ∀n:nat. nat_compared n n |
---|
19 | | nat_gt : ∀n,m:nat. nat_compared (m+S n) m. |
---|
20 | |
---|
21 | let rec nat_compare (n:nat) (m:nat) : nat_compared n m ≝ |
---|
22 | match n return λx. nat_compared x m with |
---|
23 | [ O ⇒ match m return λy. nat_compared O y with [ O ⇒ nat_eq ? | S m' ⇒ nat_lt ?? ] |
---|
24 | | S n' ⇒ |
---|
25 | match m return λy. nat_compared (S n') y with |
---|
26 | [ O ⇒ nat_gt n' O |
---|
27 | | S m' ⇒ match nat_compare n' m' return λx,y.λ_. nat_compared (S x) (S y) with |
---|
28 | [ nat_lt x y ⇒ nat_lt ?? |
---|
29 | | nat_eq x ⇒ nat_eq ? |
---|
30 | | nat_gt x y ⇒ nat_gt ? (S y) |
---|
31 | ] |
---|
32 | ] |
---|
33 | ]. |
---|
34 | |
---|
35 | lemma nat_compare_eq : ∀n. nat_compare n n = nat_eq n. |
---|
36 | #n elim n |
---|
37 | [ @refl |
---|
38 | | #m #IH whd in ⊢ (??%?); >IH @refl |
---|
39 | ] qed. |
---|
40 | |
---|
41 | lemma nat_compare_lt : ∀n,m. nat_compare n (n+S m) = nat_lt n m. |
---|
42 | #n #m elim n |
---|
43 | [ // |
---|
44 | | #n' #IH whd in ⊢ (??%?); >IH @refl |
---|
45 | ] qed. |
---|
46 | |
---|
47 | lemma nat_compare_gt : ∀n,m. nat_compare (m+S n) m = nat_gt n m. |
---|
48 | #n #m elim m |
---|
49 | [ // |
---|
50 | | #m' #IH whd in ⊢ (??%?); >IH @refl |
---|
51 | ] qed. |
---|
52 | |
---|
53 | |
---|
54 | let rec eq_nat_dec (n:nat) (m:nat) : Sum (n=m) (n≠m) ≝ |
---|
55 | match n return λn.Sum (n=m) (n≠m) with |
---|
56 | [ O ⇒ match m return λm.Sum (O=m) (O≠m) with [O ⇒ inl ?? (refl ??) | S m' ⇒ inr ??? ] |
---|
57 | | S n' ⇒ match m return λm.Sum (S n'=m) (S n'≠m) with [O ⇒ inr ??? | S m' ⇒ |
---|
58 | match eq_nat_dec n' m' with [ inl E ⇒ inl ??? | inr NE ⇒ inr ??? ] ] |
---|
59 | ]. |
---|
60 | [ 1,2: % #E destruct |
---|
61 | | >E @refl |
---|
62 | | % #E destruct cases NE /2/ |
---|
63 | ] qed. |
---|
64 | |
---|
65 | lemma max_l : ∀m,n,o:nat. o ≤ m → o ≤ max m n. |
---|
66 | #m #n #o #H whd in ⊢ (??%); @leb_elim #H' |
---|
67 | [ @(transitive_le ? m ? H H') |
---|
68 | | @H |
---|
69 | ] qed. |
---|
70 | |
---|
71 | lemma max_r : ∀m,n,o:nat. o ≤ n → o ≤ max m n. |
---|
72 | #m #n #o #H whd in ⊢ (??%); @leb_elim #H' |
---|
73 | [ @H |
---|
74 | | @(transitive_le … H) @(transitive_le … (not_le_to_lt … H')) // |
---|
75 | ] qed. |
---|
76 | |
---|
77 | lemma max_O_n : ∀n. max O n = n. |
---|
78 | * // |
---|
79 | qed. |
---|
80 | |
---|
81 | lemma max_n_O : ∀n. max n O = n. |
---|
82 | * // |
---|
83 | qed. |
---|
84 | |
---|
85 | lemma associative_max : associative nat max. |
---|
86 | #n #m #o normalize |
---|
87 | @(leb_elim n m) |
---|
88 | [ normalize @(leb_elim m o) normalize #H1 #H2 |
---|
89 | [ >(le_to_leb_true n o) /2/ |
---|
90 | | >(le_to_leb_true n m) // |
---|
91 | ] |
---|
92 | | normalize @(leb_elim m o) normalize #H1 #H2 |
---|
93 | [ % |
---|
94 | | >(not_le_to_leb_false … H2) |
---|
95 | >(not_le_to_leb_false n o) // @lt_to_not_le @(transitive_lt … m) /2/ |
---|
96 | ] |
---|
97 | ] qed. |
---|
98 | |
---|
99 | lemma le_S_to_le: ∀n,m:ℕ.S n ≤ m → n ≤ m. |
---|
100 | /2/ qed. |
---|
101 | |
---|
102 | lemma le_plus_k: |
---|
103 | ∀n,m:ℕ.n ≤ m → ∃k:ℕ.m = n + k. |
---|
104 | #n #m elim m -m; |
---|
105 | [ #Hn % [ @O | <(le_n_O_to_eq n Hn) // ] |
---|
106 | | #m #Hind #Hn cases (le_to_or_lt_eq … Hn) -Hn; #Hn |
---|
107 | [ elim (Hind (le_S_S_to_le … Hn)) #k #Hk % [ @(S k) | >Hk // ] |
---|
108 | | % [ @O | <Hn // ] |
---|
109 | ] |
---|
110 | ] |
---|
111 | qed. |
---|
112 | |
---|
113 | lemma eq_plus_S_to_lt: |
---|
114 | ∀n,m,p:ℕ.n = m + (S p) → m < n. |
---|
115 | #n #m #p /2 by lt_plus_to_lt_l/ |
---|
116 | qed. |
---|
117 | |
---|
118 | (* "Fast" proofs: some proofs get reduced during normalization (in particular, |
---|
119 | some functions which use a proof for rewriting are applied to constants and |
---|
120 | get reduced during a proof or while matita is searching for a term; |
---|
121 | they may also be normalized during testing), and so here are some more |
---|
122 | efficient versions. Perhaps they could be replaced using some kind of proof |
---|
123 | irrelevance? *) |
---|
124 | |
---|
125 | let rec plus_n_Sm_fast (n:nat) on n : ∀m:nat. S (n+m) = n+S m ≝ |
---|
126 | match n return λn'.∀m.S(n'+m) = n'+S m with |
---|
127 | [ O ⇒ λm.refl ?? |
---|
128 | | S n' ⇒ λm. ? |
---|
129 | ]. normalize @(match plus_n_Sm_fast n' m with [ refl ⇒ ? ]) @refl qed. |
---|
130 | |
---|
131 | let rec plus_n_O_faster (n:nat) : n = n + O ≝ |
---|
132 | match n return λn.n=n+O with |
---|
133 | [ O ⇒ refl ?? |
---|
134 | | S n' ⇒ match plus_n_O_faster n' return λx.λ_.S n'=S x with [ refl ⇒ refl ?? ] |
---|
135 | ]. |
---|
136 | |
---|
137 | let rec commutative_plus_faster (n,m:nat) : n+m = m+n ≝ |
---|
138 | match n return λn.n+m = m+n with |
---|
139 | [ O ⇒ plus_n_O_faster ? |
---|
140 | | S n' ⇒ ? |
---|
141 | ]. @(match plus_n_Sm_fast m n' return λx.λ_. ? = x with [ refl ⇒ ? ]) |
---|
142 | @(match commutative_plus_faster n' m return λx.λ_.? = S x with [refl ⇒ ?]) @refl qed. |
---|
143 | |
---|
144 | lemma distributive_times_plus_fast : distributive ? times plus. |
---|
145 | #n elim n [ #m #p % ] |
---|
146 | #n' #IH #m #p normalize |
---|
147 | >IH |
---|
148 | >associative_plus in ⊢ (???%); |
---|
149 | <(associative_plus ? p) in ⊢ (???%); |
---|
150 | >(commutative_plus_faster ? p) in ⊢ (???%); |
---|
151 | >(associative_plus p) |
---|
152 | @associative_plus |
---|
153 | qed. |
---|
154 | |
---|
155 | lemma times_n_Sm_fast : ∀n,m.n + n * m = n * S m. |
---|
156 | #n elim n -n |
---|
157 | [ #m % ] |
---|
158 | #n #IH #m normalize <IH |
---|
159 | <associative_plus >(commutative_plus_faster n) |
---|
160 | >associative_plus >IH % |
---|
161 | qed. |
---|
162 | |
---|
163 | lemma commutative_times_fast : commutative ? times. |
---|
164 | #n elim n -n |
---|
165 | [ #m <times_n_O % ] |
---|
166 | #n #IH #m normalize <times_n_Sm_fast >IH % |
---|
167 | qed. |
---|
168 | |
---|
169 | (* notation for sum *) |
---|
170 | notation > "Σ_{ ident i ∈ l } f" |
---|
171 | with precedence 20 |
---|
172 | for @{'fold plus 0 (λ${ident i}.true) (λ${ident i}. $f) $l}. |
---|
173 | notation < "hvbox(Σ_{ ident i break ∈ l } break f)" |
---|
174 | with precedence 20 |
---|
175 | for @{'fold plus 0 (λ${ident i}:$X.true) (λ${ident i}:$Y. $f) $l}. |
---|
176 | |
---|