Skip to content

Commit 041424e

Browse files
committed
many fixes
- removed warnings from deprecated functions - make main get the file as argument - make `makeTree` over a alpha Type
1 parent 606109a commit 041424e

File tree

9 files changed

+68
-45
lines changed

9 files changed

+68
-45
lines changed

Fad/Assignment01.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def product (ns : List Nat) : Nat :=
116116
| [] => 1
117117
| n :: ns => n * product ns
118118

119-
#eval product [2, 3, 4] -- should be 24
119+
-- #eval product [2, 3, 4] -- should be 24
120120

121121

122122
end Assignment01

Fad/Chapter3.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ theorem length_sl_eq_length (xs : List a)
277277

278278

279279
theorem length_tail_lt_length (sl : SymList a) (h : sl ≠ nil)
280-
: lengthSL sl lengthSL (tailSL sl) := by
280+
: lengthSL sl > lengthSL (tailSL sl) := by
281281
have ⟨lsl, rsl, ok⟩ := sl
282282
unfold lengthSL tailSL
283283
simp

Fad/Chapter4.lean

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,24 @@ namespace Chapter4
88
namespace D1
99

1010
def search₀ (f : Nat → Nat) (t : Nat) : List Nat :=
11-
List.foldl (fun xs x => if t = f x then x :: xs else xs) []
12-
(List.range <| t + 1)
11+
(List.range $ t + 1).filter (t = f ·)
1312

13+
-- #eval search₀ (fun _ => 3) 3
1414

1515
def search₁ (f : Nat → Nat) (t : Nat) : List Nat :=
1616
seek (0, t)
1717
where
18-
acc xs x := if t = f x then x :: xs else xs
1918
seek : (Nat × Nat) → List Nat
20-
| (a, b) => List.foldl acc [] <| List.range' a (b - a + 1)
19+
| (a, b) => List.range' a (b - a + 1) |>.filter (t = f ·)
2120

2221

2322
def search₂ (f : Nat → Nat) (t : Nat) : List Nat :=
2423
let rec seek (a b : Nat) : List Nat :=
2524
let m := (a + b) / 2
2625
let v := f m
27-
if h₁ : a = b then
26+
if h₁ : a = b then
2827
if t = v then [m] else []
29-
else if h₉ : a > b then []
28+
else if h₉ : a > b then []
3029
else if h₂ : t = v then [m]
3130
else if h₃ : t < v then
3231
seek a (m - 1)
@@ -35,15 +34,15 @@ def search₂ (f : Nat → Nat) (t : Nat) : List Nat :=
3534
termination_by (b - a) -- see https://tinyurl.com/57szywn5
3635
seek 0 t
3736

38-
-- #eval search₂ (λ a => dbg_trace "f {a}"; a * a) 100000000
37+
-- #eval search₂ (λ a => dbg_trace "f {a}"; a * a) 1024
3938

4039
def bound (f : Nat → Nat) (t : Nat) : (Nat × Nat) :=
4140
if t ≤ f 0 then (0, 0) else (b / 2, b)
4241
where
4342
b := Chapter1.until' done (· * 2) 1
4443
done b := t ≤ f b
4544

46-
-- #eval bound (fun x => dbg_trace "fun {x}"; x + 10) 20
45+
-- #eval bound (fun x => dbg_trace "fun {x}"; x + 10) 10
4746

4847
def smallest (f : Nat → Nat) (t : Nat) (p : Nat × Nat) : Nat :=
4948
match p with
@@ -54,7 +53,7 @@ def smallest (f : Nat → Nat) (t : Nat) (p : Nat × Nat) : Nat :=
5453
else if h₁ : a > b then b
5554
else if h₃ : t = v then m
5655
else if h₂ : t < v then
57-
smallest f t (a, m - 1)
56+
smallest f t (a, m)
5857
else
5958
smallest f t (m + 1, b)
6059
termination_by (p.2 - p.1)
@@ -111,6 +110,8 @@ where
111110
-- #eval search₁ (λ (x, y) => x^2 + 3^y) 20259
112111

113112

113+
-- BUG #eval helper 12 (λ (x, y) => x^2 + 3^y) (0, 12) (12,0)
114+
114115
partial def helper (t : Nat) (f : Nat × Nat → Nat)
115116
: (Nat × Nat) → (Nat × Nat) → List (Nat × Nat)
116117
| (x₁, y₁), (x₂, y₂) =>
@@ -139,14 +140,11 @@ partial def helper (t : Nat) (f : Nat × Nat → Nat)
139140
helper t f (x₁, y₁) (c - 1, y) ++ helper t f (c + 1, y - 1) (x₂, y₂)
140141

141142
partial def search₂ (f : Nat × Nat → Nat) (t : Nat) : List (Nat × Nat) :=
142-
let p := D1.smallest (λ y => f (0, y)) t (-1, t)
143-
let q := D1.smallest (λ x => f (x, 0)) t (-1, t)
143+
let p := D1.smallest (λ y => f (0, y)) t (0, t)
144+
let q := D1.smallest (λ x => f (x, 0)) t (0, t)
144145
helper t f (0, p) (q, 0)
145146

146147

147-
-- BUG #eval helper 12 (λ (x, y) => x^2 + 3^y) (0, 12) (12,0)
148-
149-
150148
/- https://kmill.github.io/informalization/ucsc_cse_talk.pdf -/
151149

152150
def scale (a : Array Int) (c : Int) : Array Int := Id.run do
@@ -174,6 +172,8 @@ end D2
174172

175173
namespace BST1
176174

175+
variable {α : Type}
176+
177177
inductive Tree (α : Type) : Type
178178
| null : Tree α
179179
| node : (Tree α) → α → (Tree α) → Tree α
@@ -187,20 +187,19 @@ def Tree.toFormat [ToString α] : (t : Tree α) → Std.Format
187187
bracket "(" (f!"{x}" ++
188188
line ++ nest 2 t₁.toFormat ++ line ++ nest 2 t₂.toFormat) ")"
189189

190-
instance [ToString a] : Repr (Tree a) where
191-
reprPrec e _ := Tree.toFormat e
190+
instance [ToString α] : Repr (Tree α) where
191+
reprPrec e _ := e.toFormat
192192

193-
def Tree.size : Tree a → Nat
193+
def Tree.size : Tree α → Nat
194194
| null => 0
195195
| node t₁ _ t₂ => 1 + t₁.size + t₂.size
196196

197-
def Tree.flatten : Tree a → List a
197+
def Tree.flatten : Tree α → List α
198198
| null => []
199199
| node l x r => l.flatten ++ [x] ++ r.flatten
200200

201-
202201
def search (f : Nat → Nat) : Nat → Tree Nat → Option Nat
203-
| _, Tree.null => none
202+
| _, Tree.null => none
204203
| k, Tree.node l x r =>
205204
if f x < k then
206205
search f k r
@@ -210,12 +209,13 @@ def search (f : Nat → Nat) : Nat → Tree Nat → Option Nat
210209
else
211210
search f k l
212211

213-
def Tree.height : Tree a → Nat
212+
def Tree.height : Tree α → Nat
214213
| null => 0
215214
| node l _ r => 1 + (max l.height r.height)
216215

217216

218-
def mkTree : List Nat → Tree Nat
217+
def mkTree [LT α] [DecidableRel (α := α) (· < ·)]
218+
: List α → Tree α
219219
| [] => Tree.null
220220
| x :: xs =>
221221
let p := xs.partition (· < x)
@@ -224,12 +224,17 @@ def mkTree : List Nat → Tree Nat
224224
decreasing_by
225225
all_goals
226226
simp [List.partition_eq_filter_filter,
227-
List.length_filter_le, Nat.lt_add_one_of_le]
227+
List.length_filter_le,
228+
Nat.lt_add_one_of_le]
229+
228230

229231
end BST1
230232

233+
231234
namespace BST2
232235

236+
variable {α : Type}
237+
233238
inductive Tree (α : Type) : Type
234239
| null : Tree α
235240
| node : Nat → (Tree α) → α → (Tree α) → Tree α
@@ -243,14 +248,14 @@ def Tree.toFormat [ToString α] : (t : Tree α) → Std.Format
243248
bracket "(" (f!"{x}" ++
244249
line ++ nest 2 t₁.toFormat ++ line ++ nest 2 t₂.toFormat) ")"
245250

246-
instance [ToString a] : Repr (Tree a) where
247-
reprPrec e _ := Tree.toFormat e
251+
instance [ToString α] : Repr (Tree α) where
252+
reprPrec e _ := e.toFormat
248253

249-
def Tree.height : (a : Tree α) -> Nat
250-
| Tree.null => 0
251-
| Tree.node x _ _ _ => x
254+
def Tree.height : Tree α Nat
255+
| .null => 0
256+
| .node x _ _ _ => x
252257

253-
def Tree.flatten : Tree a → List a
258+
def Tree.flatten : Tree α → List α
254259
| null => []
255260
| node _ l x r => l.flatten ++ [x] ++ r.flatten
256261

Fad/Chapter5-Ex.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ open Function (uncurry) in
333333

334334
def csort (m : Nat) (xs : List Nat) : List Nat :=
335335
let a := Chapter3.accumArray Nat.add 0 m (xs.map (·, 1))
336-
a.zipWithIndex.toList.flatMap (uncurry replicate)
336+
a.zipIdx.toList.flatMap (uncurry replicate)
337337

338338

339339
/- # Exercicio 5.19 -/

Fad/Chapter6.lean

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def minmax [Inhabited a] [LE a] [DecidableRel (α := a) (· ≤ ·)]
9999

100100
def select₀ [Inhabited a] [LT a] [DecidableRel (α := a) (· < ·)]
101101
(k : Nat) (xs : List a) : a :=
102-
(qsort₁ xs).get! (k - 1)
102+
(qsort₁ xs)[k - 1]!
103103

104104

105105
def median [Inhabited a] [LT a] [DecidableRel (α := a) (· < ·)]
@@ -120,7 +120,7 @@ partial def group [Inhabited a] (n : Nat) (xs : List a) : List (List a) :=
120120
def medians [Inhabited a] [LT a]
121121
[DecidableRel (α := a) (· < ·)] [DecidableRel (α := a) (· = ·)]
122122
: List a → List a :=
123-
let middle (xs : List a) := xs.get! (((xs.length + 1) / 2) - 1)
123+
let middle (xs : List a) := xs[((xs.length + 1) / 2) - 1]!
124124
List.map (middle ∘ qsort₁) ∘ group 5
125125

126126

@@ -152,7 +152,7 @@ partial def select [Inhabited a] [LT a]
152152
let m := us.length
153153
let n := vs.length
154154
if k ≤ m then select k us
155-
else if k ≤ m + n then vs.get! (k - m - 1)
155+
else if k ≤ m + n then vs[k - m - 1]!
156156
else if k > m + n then select (k - m - n) ws
157157
else panic! "unreachable code"
158158

Fad/Chapter7-Ex.lean

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,12 @@ namespace S73
109109
return the other `minimum`. -/
110110

111111
def mktuples' : List Denom → Nat → List Tuple
112-
| [1] , n => [[n]]
113-
| [] , _ => panic! "mktuples: invalid empty list"
112+
| [1] , n => [[n]]
113+
| [] , _ => panic! "mktuples: invalid empty list"
114114
| d :: ds, n =>
115-
let rs := List.iota (n / d + 1) |>.map (· - 1)
116-
rs.flatMap (λ c => mktuples₀ ds (n - c * d) |>.map (λ cs => c :: cs))
115+
let rs := List.range (n / d + 1)
116+
rs.flatMap (λ c => mktuples₀ ds (n - c * d) |>.map
117+
(λ cs => c :: cs))
117118

118119
def mkchange' (ds : List Denom) : Nat → Tuple :=
119120
minWith List.sum ∘ mktuples' ds

Fad/Chapter9-Ex.lean

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ def toAdj (g : Graph) : AdjArray :=
99

1010
def toGraph (adj : AdjArray) : Graph :=
1111
let vs := List.range adj.size
12-
let es : List Edge := adj.toList.enum |>.flatMap
12+
let es : List Edge := adj.toList.zipIdx.map (λ (a,b) => (b,a))
13+
|>.flatMap
1314
(λ (v, ps) => ps.map (λ p => (v, p.1, p.2)))
1415
(vs, es)
1516

1617
def g : Graph :=
1718
([0, 1, 2, 3],
1819
[(0, 1, 1), (1, 2, 5), (1, 3, 10), (2, 3, 2), (3, 0, 4)])
1920

20-
21+
-- #eval toAdj g |> toGraph
2122

2223
end Chapter9

Fad/Chapter9.lean

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,4 @@ def weight (e : Edge) : Weight := e.2.2
1717
abbrev AdjArray := Array (List (Vertex × Weight))
1818

1919

20-
21-
2220
end Chapter9

Main.lean

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,22 @@
11
import Fad
22

3-
def main : IO Unit :=
4-
IO.println Chapter0.hello₁
3+
4+
def readFile (filename : String) : IO (List Nat) := do
5+
let contents ← IO.FS.readFile filename
6+
let lines := contents.splitOn "\n"
7+
let mut result := []
8+
for line in lines do
9+
match line.trim.toNat? with
10+
| some n => result := n :: result
11+
| none => result := result
12+
return result
13+
14+
15+
def main (args : List String) : IO Unit := do
16+
match args with
17+
| [] => println! "Usage: lake fad FILE"
18+
| a :: _ =>
19+
let r ← readFile a
20+
let t := Chapter4.BST1.mkTree r
21+
println! t.height
22+
println! Chapter4.BST1.search id 678 t

0 commit comments

Comments
 (0)