-
Notifications
You must be signed in to change notification settings - Fork 4
/
Basic.lean
391 lines (301 loc) · 10.8 KB
/
Basic.lean
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/-
Copyright (c) 2016 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Leonardo de Moura
-/
prelude
import Leanbin.Init.Logic
import Leanbin.Init.Data.Nat.Basic
import Leanbin.Init.Data.Bool.Basic
import Leanbin.Init.Propext
open Decidable List
universe u v w
instance (α : Type u) : Inhabited (List α) :=
⟨List.nil⟩
variable {α : Type u} {β : Type v} {γ : Type w}
namespace List
protected def hasDecEqₓ [s : DecidableEq α] : DecidableEq (List α)
| [], [] => isTrue rfl
| a :: as, [] => isFalse fun h => List.noConfusion h
| [], b :: bs => isFalse fun h => List.noConfusion h
| a :: as, b :: bs =>
match s a b with
| is_true hab =>
match has_dec_eq as bs with
| is_true habs => isTrue (Eq.subst hab (Eq.subst habs rfl))
| is_false nabs => isFalse fun h => List.noConfusion h fun _ habs => absurd habs nabs
| is_false nab => isFalse fun h => List.noConfusion h fun hab _ => absurd hab nab
instance [DecidableEq α] : DecidableEq (List α) :=
List.hasDecEqₓ
@[simp]
protected def append : List α → List α → List α
| [], l => l
| h :: s, t => h :: append s t
instance : Append (List α) :=
⟨List.append⟩
protected def Memₓ : α → List α → Prop
| a, [] => False
| a, b :: l => a = b ∨ mem a l
instance : Membership α (List α) :=
⟨List.Memₓ⟩
instance decidableMem [DecidableEq α] (a : α) : ∀ l : List α, Decidable (a ∈ l)
| [] => isFalse not_false
| b :: l =>
if h₁ : a = b then isTrue (Or.inl h₁)
else
match decidable_mem l with
| is_true h₂ => isTrue (Or.inr h₂)
| is_false h₂ => isFalse (not_orₓ h₁ h₂)
instance : EmptyCollection (List α) :=
⟨List.nil⟩
protected def eraseₓ {α} [DecidableEq α] : List α → α → List α
| [], b => []
| a :: l, b => if a = b then l else a :: erase l b
protected def bagInterₓ {α} [DecidableEq α] : List α → List α → List α
| [], _ => []
| _, [] => []
| a :: l₁, l₂ => if a ∈ l₂ then a :: bag_inter l₁ (l₂.erase a) else bag_inter l₁ l₂
protected def diffₓ {α} [DecidableEq α] : List α → List α → List α
| l, [] => l
| l₁, a :: l₂ => if a ∈ l₁ then diff (l₁.erase a) l₂ else diff l₁ l₂
@[simp]
def length : List α → Nat
| [] => 0
| a :: l => length l + 1
def empty : List α → Bool
| [] => true
| _ :: _ => false
open Option Nat
@[simp]
def nth : List α → Nat → Option α
| [], n => none
| a :: l, 0 => some a
| a :: l, n + 1 => nth l n
@[simp]
def nthLe : ∀ (l : List α) (n), n < l.length → α
| [], n, h => absurd h n.not_lt_zero
| a :: l, 0, h => a
| a :: l, n + 1, h => nth_le l n (le_of_succ_le_succₓ h)
@[simp]
def headₓ [Inhabited α] : List α → α
| [] => default
| a :: l => a
@[simp]
def tail : List α → List α
| [] => []
| a :: l => l
def reverseCore : List α → List α → List α
| [], r => r
| a :: l, r => reverse_core l (a :: r)
def reverse : List α → List α := fun l => reverseCore l []
@[simp]
def map (f : α → β) : List α → List β
| [] => []
| a :: l => f a :: map l
@[simp]
def map₂ₓ (f : α → β → γ) : List α → List β → List γ
| [], _ => []
| _, [] => []
| x :: xs, y :: ys => f x y :: map₂ xs ys
def mapWithIndexCore (f : ℕ → α → β) : ℕ → List α → List β
| k, [] => []
| k, a :: as => f k a :: map_with_index_core (k + 1) as
/-- Given a function `f : ℕ → α → β` and `as : list α`, `as = [a₀, a₁, ...]`, returns the list
`[f 0 a₀, f 1 a₁, ...]`. -/
def mapWithIndex (f : ℕ → α → β) (as : List α) : List β :=
mapWithIndexCore f 0 as
def join : List (List α) → List α
| [] => []
| l :: ls => l ++ join ls
def filterMap (f : α → Option β) : List α → List β
| [] => []
| a :: l =>
match f a with
| none => filter_map l
| some b => b :: filter_map l
def filterₓ (p : α → Prop) [DecidablePred p] : List α → List α
| [] => []
| a :: l => if p a then a :: filter l else filter l
def partitionₓ (p : α → Prop) [DecidablePred p] : List α → List α × List α
| [] => ([], [])
| a :: l =>
let (l₁, l₂) := partition l
if p a then (a :: l₁, l₂) else (l₁, a :: l₂)
def dropWhileₓ (p : α → Prop) [DecidablePred p] : List α → List α
| [] => []
| a :: l => if p a then drop_while l else a :: l
/-- `after p xs` is the suffix of `xs` after the first element that satisfies
`p`, not including that element.
```lean
after (eq 1) [0, 1, 2, 3] = [2, 3]
drop_while (not ∘ eq 1) [0, 1, 2, 3] = [1, 2, 3]
```
-/
def after (p : α → Prop) [DecidablePred p] : List α → List α
| [] => []
| x :: xs => if p x then xs else after xs
def spanₓ (p : α → Prop) [DecidablePred p] : List α → List α × List α
| [] => ([], [])
| a :: xs =>
if p a then
let (l, r) := span xs
(a :: l, r)
else ([], a :: xs)
def findIndex (p : α → Prop) [DecidablePred p] : List α → Nat
| [] => 0
| a :: l => if p a then 0 else succ (find_index l)
def indexOfₓ [DecidableEq α] (a : α) : List α → Nat :=
findIndex (Eq a)
def removeAllₓ [DecidableEq α] (xs ys : List α) : List α :=
filterₓ (· ∉ ys) xs
def updateNth : List α → ℕ → α → List α
| x :: xs, 0, a => a :: xs
| x :: xs, i + 1, a => x :: update_nth xs i a
| [], _, _ => []
def removeNthₓ : List α → ℕ → List α
| [], _ => []
| x :: xs, 0 => xs
| x :: xs, i + 1 => x :: remove_nth xs i
@[simp]
def dropₓ : ℕ → List α → List α
| 0, a => a
| succ n, [] => []
| succ n, x :: r => drop n r
@[simp]
def takeₓ : ℕ → List α → List α
| 0, a => []
| succ n, [] => []
| succ n, x :: r => x :: take n r
@[simp]
def foldlₓ (f : α → β → α) : α → List β → α
| a, [] => a
| a, b :: l => foldl (f a b) l
@[simp]
def foldr (f : α → β → β) (b : β) : List α → β
| [] => b
| a :: l => f a (foldr l)
def any (l : List α) (p : α → Bool) : Bool :=
foldr (fun a r => p a || r) false l
def all (l : List α) (p : α → Bool) : Bool :=
foldr (fun a r => p a && r) true l
def bor (l : List Bool) : Bool :=
any l id
def band (l : List Bool) : Bool :=
all l id
def zipWithₓ (f : α → β → γ) : List α → List β → List γ
| x :: xs, y :: ys => f x y :: zip_with xs ys
| _, _ => []
def zipₓ : List α → List β → List (Prod α β) :=
zipWithₓ Prod.mk
def unzip : List (α × β) → List α × List β
| [] => ([], [])
| (a, b) :: t =>
match unzip t with
| (al, bl) => (a :: al, b :: bl)
protected def insertₓ [DecidableEq α] (a : α) (l : List α) : List α :=
if a ∈ l then l else a :: l
instance [DecidableEq α] : Insert α (List α) :=
⟨List.insertₓ⟩
instance : Singleton α (List α) :=
⟨fun x => [x]⟩
instance [DecidableEq α] : IsLawfulSingleton α (List α) :=
⟨fun x => show (if x ∈ ([] : List α) then [] else [x]) = [x] from if_neg not_false⟩
protected def unionₓ [DecidableEq α] (l₁ l₂ : List α) : List α :=
foldr insert l₂ l₁
instance [DecidableEq α] : Union (List α) :=
⟨List.unionₓ⟩
protected def interₓ [DecidableEq α] (l₁ l₂ : List α) : List α :=
filterₓ (· ∈ l₂) l₁
instance [DecidableEq α] : Inter (List α) :=
⟨List.interₓ⟩
@[simp]
def repeat (a : α) : ℕ → List α
| 0 => []
| succ n => a :: repeat n
def rangeCore : ℕ → List ℕ → List ℕ
| 0, l => l
| succ n, l => range_core n (n :: l)
def range (n : ℕ) : List ℕ :=
rangeCore n []
def iota : ℕ → List ℕ
| 0 => []
| succ n => succ n :: iota n
def enumFrom : ℕ → List α → List (ℕ × α)
| n, [] => nil
| n, x :: xs => (n, x) :: enum_from (n + 1) xs
def enum : List α → List (ℕ × α) :=
enumFrom 0
@[simp]
def last : ∀ l : List α, l ≠ [] → α
| [], h => absurd rfl h
| [a], h => a
| a :: b :: l, h => last (b :: l) fun h => List.noConfusion h
def ilast [Inhabited α] : List α → α
| [] => arbitrary α
| [a] => a
| [a, b] => b
| a :: b :: l => ilast l
def init : List α → List α
| [] => []
| [a] => []
| a :: l => a :: init l
def intersperse (sep : α) : List α → List α
| [] => []
| [x] => [x]
| x :: xs => x :: sep :: intersperse xs
def intercalate (sep : List α) (xs : List (List α)) : List α :=
join (intersperse sep xs)
@[inline]
protected def bind {α : Type u} {β : Type v} (a : List α) (b : α → List β) : List β :=
join (map b a)
@[inline]
protected def ret {α : Type u} (a : α) : List α :=
[a]
protected def Lt [LT α] : List α → List α → Prop
| [], [] => False
| [], b :: bs => True
| a :: as, [] => False
| a :: as, b :: bs => a < b ∨ ¬b < a ∧ lt as bs
instance [LT α] : LT (List α) :=
⟨List.Lt⟩
instance hasDecidableLtₓ [LT α] [h : DecidableRel ((· < ·) : α → α → Prop)] : ∀ l₁ l₂ : List α, Decidable (l₁ < l₂)
| [], [] => isFalse not_false
| [], b :: bs => isTrue trivialₓ
| a :: as, [] => isFalse not_false
| a :: as, b :: bs =>
match h a b with
| is_true h₁ => isTrue (Or.inl h₁)
| is_false h₁ =>
match h b a with
| is_true h₂ => isFalse fun h => Or.elim h (fun h => absurd h h₁) fun ⟨h, _⟩ => absurd h₂ h
| is_false h₂ =>
match has_decidable_lt as bs with
| is_true h₃ => isTrue (Or.inr ⟨h₂, h₃⟩)
| is_false h₃ => isFalse fun h => Or.elim h (fun h => absurd h h₁) fun ⟨_, h⟩ => absurd h h₃
@[reducible]
protected def Le [LT α] (a b : List α) : Prop :=
¬b < a
instance [LT α] : LE (List α) :=
⟨List.Le⟩
instance hasDecidableLe [LT α] [h : DecidableRel ((· < ·) : α → α → Prop)] : ∀ l₁ l₂ : List α, Decidable (l₁ ≤ l₂) :=
fun a b => Not.decidable
theorem le_eq_not_gt [LT α] : ∀ l₁ l₂ : List α, (l₁ ≤ l₂) = ¬l₂ < l₁ := fun l₁ l₂ => rfl
theorem lt_eq_not_ge [LT α] [DecidableRel ((· < ·) : α → α → Prop)] : ∀ l₁ l₂ : List α, (l₁ < l₂) = ¬l₂ ≤ l₁ :=
fun l₁ l₂ => show (l₁ < l₂) = ¬¬l₁ < l₂ from Eq.subst (propext (not_not_iff (l₁ < l₂))).symm rfl
/-- `is_prefix_of l₁ l₂` returns `tt` iff `l₁` is a prefix of `l₂`. -/
def isPrefixOfₓ [DecidableEq α] : List α → List α → Bool
| [], _ => true
| _, [] => false
| a :: as, b :: bs => toBool (a = b) && is_prefix_of as bs
/-- `is_suffix_of l₁ l₂` returns `tt` iff `l₁` is a suffix of `l₂`. -/
def isSuffixOfₓ [DecidableEq α] (l₁ l₂ : List α) : Bool :=
isPrefixOfₓ l₁.reverse l₂.reverse
end List
namespace BinTree
private def to_list_aux : BinTree α → List α → List α
| Empty, as => as
| leaf a, as => a :: as
| node l r, as => to_list_aux l (to_list_aux r as)
def toList (t : BinTree α) : List α :=
toListAux t []
end BinTree