Skip to content

Commit aad5264

Browse files
committed
aula 2025-05-21
1 parent b0c12e2 commit aad5264

File tree

3 files changed

+54
-36
lines changed

3 files changed

+54
-36
lines changed

Fad/Chapter4.lean

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
import Fad.Chapter1
32

43
namespace Chapter4
@@ -227,63 +226,64 @@ def mkTree [LT α] [DecidableRel (α := α) (· < ·)]
227226
List.length_filter_le,
228227
Nat.lt_add_one_of_le]
229228

230-
231229
end BST1
232230

233231

234232
namespace BST2
235233

236-
variable {α : Type}
234+
variable {a : Type} [LT a] [DecidableRel (α := a) (· < ·)]
237235

238-
inductive Tree (α : Type) : Type
239-
| null : Tree α
240-
| node : Nat → (Tree α) → α → (Tree α) → Tree α
241-
deriving Nonempty
236+
inductive Tree (a : Type) : Type
237+
| null : Tree a
238+
| node : Nat → (Tree a) → a → (Tree a) → Tree a
239+
deriving Nonempty, Inhabited
242240

243241
open Std.Format in
244242

245-
def Tree.toFormat [ToString α] : (t : Tree α) → Std.Format
243+
def Tree.toFormat [ToString a] : (t : Tree a) → Std.Format
246244
| .null => Std.Format.text "."
247245
| .node _ t₁ x t₂ =>
248246
bracket "(" (f!"{x}" ++
249247
line ++ nest 2 t₁.toFormat ++ line ++ nest 2 t₂.toFormat) ")"
250248

251-
instance [ToString α] : Repr (Tree α) where
249+
instance [ToString a] : Repr (Tree a) where
252250
reprPrec e _ := e.toFormat
253251

254-
def Tree.height : Tree α → Nat
252+
def Tree.height : Tree a → Nat
255253
| .null => 0
256254
| .node x _ _ _ => x
257255

258-
def Tree.flatten : Tree α → List α
256+
def Tree.flatten : Tree a → List a
259257
| null => []
260258
| node _ l x r => l.flatten ++ [x] ++ r.flatten
261259

262-
def node (l : Tree α) (x : α) (r : Tree α): Tree α :=
260+
def node (l : Tree a) (x : a) (r : Tree a): Tree a :=
263261
Tree.node h l x r
264262
where h := 1 + (max l.height r.height)
265263

266-
def bias : Tree α → Int
264+
def bias : Tree a → Int
267265
| .null => 0
268266
| .node _ l _ r => l.height - r.height
269267

270-
def rotr : Tree α → Tree α
268+
def rotr : Tree a → Tree a
271269
| .null => .null
272270
| .node _ (.node _ ll y rl) x r => node ll y (node rl x r)
273271
| .node _ .null _ _ => .null
274272

275-
def rotl : Tree α → Tree α
273+
def rotl : Tree a → Tree a
276274
| .null => .null
277275
| .node _ ll y (.node _ lrl z rrl) => node (node ll y lrl) z rrl
278276
| .node _ _ _ .null => .null
279277

280-
def balance (t1 : Tree α) (x : α) (t2 : Tree α) : Tree α :=
278+
def balance (t1 : Tree a) (x : a) (t2 : Tree a) : Tree a :=
281279
if Int.natAbs (h1 - h2) ≤ 1 then
282280
node t1 x t2
283-
else if h1 == h2 + 2 then
281+
else if h1 = h2 + 2 then
284282
rotateR t1 x t2
285-
else
283+
else if h2 = h1 + 2 then
286284
rotateL t1 x t2
285+
else
286+
panic! "balance: impossible case"
287287
where
288288
h1 := t1.height
289289
h2 := t2.height
@@ -297,33 +297,45 @@ def balance (t1 : Tree α) (x : α) (t2 : Tree α) : Tree α :=
297297
else rotl (node t1 x (rotr t2))
298298

299299

300-
def insert {α : Type} [LT α] [DecidableRel (α := α) (· < ·)]
301-
: (x : α) -> Tree α -> Tree α
300+
def insert : (x : a) -> Tree a -> Tree a
302301
| x, .null => node .null x .null
303302
| x, .node h l y r =>
304303
if x < y then balance (insert x l) y r else
305304
if x > y then balance l y (insert x r) else .node h l y r
306305

307306

308-
def mkTree [LT α] [DecidableRel (α := α) (· < ·)] : (xs : List α) → Tree α :=
309-
Chapter1.foldr insert (.null : Tree α)
307+
def mkTree : (xs : List a) → Tree a :=
308+
List.foldr insert (.null : Tree a)
310309

310+
-- #eval balance (mkTree [1,2,3,4]) 4 (mkTree [])
311311

312-
def balanceR (t₁ : Tree α) (x : α) (t₂ : Tree α) : Tree α :=
312+
def balanceR (t₁ : Tree a) (x : a) (t₂ : Tree a) : Tree a :=
313313
match t₁ with
314314
| Tree.null => Tree.null
315315
| Tree.node _ l y r =>
316316
if r.height ≥ t₂.height + 2
317317
then balance l y (balanceR r x t₂)
318318
else balance l y (node r x t₂)
319319

320+
-- def balanceL ...
321+
-- def gbalance ...
322+
323+
def mktree : List a → Tree a :=
324+
List.foldr insert Tree.null
325+
326+
def sort : List a → List a :=
327+
Tree.flatten ∘ mkTree
328+
329+
#eval sort [3, 2, 1, 4, 5, 5] -- bug with duplicated elements!
330+
320331
end BST2
321332

322333
namespace DSet
323334
open BST2 (insert node)
324335

325336
abbrev Set a := BST2.Tree a
326337

338+
327339
def member {a : Type} [LT a] [DecidableRel (α := a) (· < ·)] (x : a) : Set a → Bool
328340
| .null => false
329341
| .node _ l y r =>

Fad/Chapter5-Ex.lean

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@ namespace Chapter5
1111
section
1212
open Quicksort
1313

14-
theorem qsort₀_eq_qsort₁ [h₁ : LT β] [h₂ : DecidableRel (α := β) (· < ·)]
15-
(xs : List β) : qsort₀ xs = qsort₁ xs := by
14+
variable {a : Type} [h₁ : LT a] [h₂ : DecidableRel (α := a) (· < ·)]
15+
16+
-- trocar cases para induction?
17+
18+
theorem qsort₀_eq_qsort₁ (xs : List a) : qsort₀ xs = qsort₁ xs := by
1619
cases xs with
1720
| nil =>
1821
rw [qsort₀, Function.comp, mkTree, Tree.flatten]
@@ -29,10 +32,10 @@ theorem qsort₀_eq_qsort₁ [h₁ : LT β] [h₂ : DecidableRel (α := β) (·
2932
have h₂ := qsort₀_eq_qsort₁ (List.filter (not ∘ fun x => decide (x < a)) as)
3033
rw [h₁, h₂]
3134
termination_by xs.length
32-
decreasing_by
33-
all_goals simp
34-
all_goals rw [Nat.lt_add_one_iff]
35-
all_goals simp [List.length_filter_le]
35+
decreasing_by
36+
all_goals simp
37+
all_goals rw [Nat.lt_add_one_iff]
38+
all_goals simp [List.length_filter_le]
3639

3740
end
3841

Fad/Chapter5.lean

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ namespace Chapter5
55

66
namespace Quicksort
77

8+
variable {a : Type} [h₁ : LT a] [h₂ : DecidableRel (α := a) (· < ·)]
9+
810
inductive Tree a where
911
| null : Tree a
1012
| node : (Tree a) → a → (Tree a) → Tree a
1113

12-
def mkTree [LT a] [DecidableRel (α := a) (· < ·)] : List a → Tree a
14+
def mkTree: List a → Tree a
1315
| [] => Tree.null
1416
| x :: xs =>
1517
let p := xs.partition (. < x)
@@ -25,10 +27,10 @@ def Tree.flatten : Tree a → List a
2527
| null => []
2628
| node l x r => l.flatten ++ [x] ++ r.flatten
2729

28-
def qsort₀ [LT a] [DecidableRel (α := a) (· < ·)] : List a → List a :=
30+
def qsort₀ : List a → List a :=
2931
Tree.flatten ∘ mkTree
3032

31-
def qsort₁ [h₁ : LT a] [h₂ : DecidableRel (α := a) (· < ·)] : List a → List a
33+
def qsort₁ : List a → List a
3234
| [] => []
3335
| (x :: xs) =>
3436
let p := xs.partition (· < x)
@@ -39,6 +41,7 @@ def qsort₁ [h₁ : LT a] [h₂ : DecidableRel (α := a) (· < ·)] : List a
3941
[List.partition_eq_filter_filter,
4042
List.length_filter_le, Nat.lt_add_one_of_le]
4143

44+
4245
def qsort₂ [Ord a] (f : a → a → Ordering) : List a → List a
4346
| [] => []
4447
| (x :: xs) =>
@@ -78,6 +81,7 @@ instance (a b : Person) : Decidable (a < b) :=
7881
def people := [
7982
Person.mk "Alice" 23,
8083
Person.mk "Bob" 25,
84+
Person.mk "Bob" 25,
8185
Person.mk "Eve" 22]
8286

8387
/-
@@ -88,7 +92,7 @@ def people := [
8892
end Quicksort
8993

9094

91-
namespace S52 -- Mergesort
95+
namespace Mergesort
9296

9397
inductive Tree (α : Type) : Type where
9498
| null : Tree α
@@ -235,7 +239,7 @@ def msort₃ [LE a] [DecidableRel (α := a) (· ≤ ·)] : List a → List a
235239
#eval msort₁ ['a','b','a']
236240
-/
237241

238-
end S52
242+
end Mergesort
239243

240244
namespace Heapsort
241245

@@ -247,7 +251,7 @@ inductive Tree (α : Type) : Type
247251

248252
def flatten [LE a] [DecidableRel (α := a) (· ≤ ·)] : Tree a → List a
249253
| Tree.null => []
250-
| Tree.node x u v => x :: S52.merge (flatten u) (flatten v)
254+
| Tree.node x u v => x :: Mergesort.merge (flatten u) (flatten v)
251255

252256

253257
open Std.Format in
@@ -264,5 +268,4 @@ instance [ToString a] : Repr (Tree a) where
264268

265269
end Heapsort
266270

267-
268271
end Chapter5

0 commit comments

Comments
 (0)