@@ -8,25 +8,24 @@ namespace Chapter4
88namespace D1
99
1010def 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
1515def 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
2322def 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
4039def 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
4847def 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+
114115partial 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
141142partial 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
152150def scale (a : Array Int) (c : Int) : Array Int := Id.run do
@@ -174,6 +172,8 @@ end D2
174172
175173namespace BST1
176174
175+ variable {α : Type }
176+
177177inductive 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-
202201def 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
229231end BST1
230232
233+
231234namespace BST2
232235
236+ variable {α : Type }
237+
233238inductive 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
0 commit comments