Skip to content

Commit ee837f2

Browse files
committed
namespace change
1 parent aad5264 commit ee837f2

File tree

5 files changed

+31
-29
lines changed

5 files changed

+31
-29
lines changed

Fad.lean

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
-- This module serves as the root of the `Fad` library.
22
-- Import modules here that should be built as part of the library.
3+
34
import Fad.Basic
45
import Fad.Chapter0
56

Fad/Chapter4.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ def mktree : List a → Tree a :=
326326
def sort : List a → List a :=
327327
Tree.flatten ∘ mkTree
328328

329-
#eval sort [3, 2, 1, 4, 5, 5] -- bug with duplicated elements!
329+
-- #eval sort [3, 2, 1, 4, 5, 5] -- bug with duplicated elements!
330330

331331
end BST2
332332

Fad/Chapter5-Ex.lean

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ example (a b : Nat) : T a b ≤ a + b := by
142142

143143
/- # Exercicio 5.9 -/
144144

145-
namespace S52
145+
namespace Mergesort
146146

147147
example (xs : List Nat) : msort₂ xs = msort₃ xs := by
148148
induction xs with
@@ -151,7 +151,7 @@ example (xs : List Nat) : msort₂ xs = msort₃ xs := by
151151
simp [msort₂, msort₃]
152152
sorry
153153

154-
end S52
154+
end Mergesort
155155

156156
/- # Exercicio 5.10 -/
157157

@@ -217,7 +217,7 @@ def merge₁ (f : a → a → Ordering) : List a → List a → List a
217217
y :: merge₁ f (x :: xs) ys
218218

219219
open Chapter1 (wrap unwrap single until') in
220-
open S52 in
220+
open Mergesort in
221221

222222
def sortBy (f : a → a → Ordering) : List a → List a
223223
| [] => []

Fad/Chapter6.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import Fad.«Chapter4-Ex»
66

77
namespace Chapter6
88
open Chapter1 (unwrap until' single)
9-
open Chapter5.S52 (halve length_halve_fst length_halve_snd)
9+
open Chapter5.Mergesort (halve length_halve_fst length_halve_snd)
1010
open Chapter5.Quicksort (qsort₁)
1111
open Chapter4.BST2 (partition3)
1212

Fad/Chapter8.lean

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace S1
1111

1212
/- 8.1 Minimum-height trees -/
1313

14-
open Chapter5.S52 (halve length_halve_fst length_halve_snd pairWith)
14+
open Chapter5.Mergesort (halve length_halve_fst length_halve_snd pairWith)
1515
open Chapter1 (wrap unwrap single until' concatMap)
1616
open Chapter6 (foldl1)
1717
open Chapter7 (minWith)
@@ -176,32 +176,33 @@ end S1
176176
/- 8.2 Huffman coding trees -/
177177

178178
namespace S2
179+
179180
open S1 (Tree Forest)
180181
open Chapter1 (concatMap wrap unwrap unwrap! single until' single picks apply)
181182
open Chapter3.SymList (nil singleSL headSL headSL! snocSL isEmpty tailSL)
182183

183184

184-
def depths : Tree a → List Nat :=
185-
let rec frm (n : Nat) : Tree a → List Nat
186-
| Tree.leaf _ => [n]
187-
| Tree.node u v => frm (n + 1) u ++ frm (n + 1) v
185+
def depths : S1.Tree a → List Nat :=
186+
let rec frm (n : Nat) : S1.Tree a → List Nat
187+
| .leaf _ => [n]
188+
| .node u v => frm (n + 1) u ++ frm (n + 1) v
188189
frm 0
189190

190191
abbrev Weight := Nat
191192
abbrev Elem := Char × Weight
192193
abbrev Cost := Nat
193194

194-
def cost (t : Tree Elem) : Cost :=
195+
def cost (t : S1.Tree Elem) : Cost :=
195196
let t := t.fringe.zip (depths t)
196197
List.sum $ t.map (λ (c, d) => d * c.snd)
197198

198-
def weight : Tree Elem → Nat
199-
| Tree.leaf (_, w) => w
200-
| Tree.node u v => weight u + weight v
199+
def weight : S1.Tree Elem → Nat
200+
| .leaf (_, w) => w
201+
| .node u v => weight u + weight v
201202

202-
def cost₁ : Tree Elem → Cost
203-
| Tree.leaf _ => 0
204-
| Tree.node u v => cost u + cost v + weight u + weight v
203+
def cost₁ : S1.Tree Elem → Cost
204+
| .leaf _ => 0
205+
| .node u v => cost u + cost v + weight u + weight v
205206

206207
def pairs (xs : List α) : List ((α × α) × List α) :=
207208
(picks xs).flatMap
@@ -210,7 +211,7 @@ def pairs (xs : List α) : List ((α × α) × List α) :=
210211
fun (y, zs) => ((x, y), zs)
211212

212213
/- Exercise 8.11 -/
213-
def insert (t₁ : Tree Elem) : Forest Elem → Forest Elem
214+
def insert (t₁ : S1.Tree Elem) : Forest Elem → Forest Elem
214215
| [] => [t₁]
215216
| t₂ :: ts =>
216217
if weight t₁ ≤ weight t₂ then
@@ -220,26 +221,26 @@ def insert (t₁ : Tree Elem) : Forest Elem → Forest Elem
220221

221222
def combine (ts : Forest Elem) : List (Forest Elem) :=
222223
(pairs ts).map fun (p, us) =>
223-
insert (Tree.node p.1 p.2) us
224+
insert (S1.Tree.node p.1 p.2) us
224225

225-
def mkForests : List (Tree Elem) → List (Forest Elem) :=
226+
def mkForests : List (S1.Tree Elem) → List (Forest Elem) :=
226227
until' (flip List.all single) (concatMap combine) ∘ wrap
227228

228-
def mkTrees : List Elem → List (Tree Elem) :=
229+
def mkTrees : List Elem → List (S1.Tree Elem) :=
229230
List.map unwrap! ∘ mkForests ∘ List.map Tree.leaf
230231

231-
def mkForests₁ (ts : List (Tree Elem)) : List (Forest Elem) :=
232+
def mkForests₁ (ts : List (S1.Tree Elem)) : List (Forest Elem) :=
232233
apply (ts.length - 1) (concatMap combine) [ts]
233234

234-
def mkTrees₁ : List Elem → List (Tree Elem) :=
235+
def mkTrees₁ : List Elem → List (S1.Tree Elem) :=
235236
List.map unwrap! ∘ mkForests₁ ∘ List.map Tree.leaf
236237

237238

238239
/- quadractic version -/
239240

240-
def huffman₁ (es : List Elem) : Tree Elem :=
241+
def huffman₁ (es : List Elem) : S1.Tree Elem :=
241242
let gstep : Forest Elem → Forest Elem
242-
| t₁ :: t₂ :: ts => insert (Tree.node t₁ t₂) ts
243+
| t₁ :: t₂ :: ts => insert (S1.Tree.node t₁ t₂) ts
243244
| [] => dbg_trace "error"; [] -- not used
244245
| t₁ :: ts => dbg_trace "error"; t₁ :: ts -- not used
245246
unwrap! (until' single gstep (List.map Tree.leaf es))
@@ -251,21 +252,21 @@ def huffman₁ (es : List Elem) : Tree Elem :=
251252
abbrev Queue (α : Type) := Chapter3.SymList α
252253
abbrev Stack (α : Type) := List α
253254
abbrev SQ (α : Type) := Stack α × Queue α
254-
abbrev Pair := Tree Elem × Weight
255+
abbrev Pair := S1.Tree Elem × Weight
255256

256257
def leaf : Elem → Pair
257258
| (c, w) => (Tree.leaf (c, w), w)
258259

259260
def node : Pair → Pair → Pair
260-
| (t₁, w₁), (t₂, w₂) => (Tree.node t₁ t₂, w₁ + w₂)
261+
| (t₁, w₁), (t₂, w₂) => (S1.Tree.node t₁ t₂, w₁ + w₂)
261262

262263
def makeSQ (xs : List Pair) : SQ Pair :=
263264
(xs, nil)
264265

265266
def singleSQ (sq : SQ a) : Bool :=
266267
sq.1.isEmpty ∧ singleSL sq.2
267268

268-
def extractSQ (sq : SQ Pair) : Tree Elem :=
269+
def extractSQ (sq : SQ Pair) : S1.Tree Elem :=
269270
(headSL! sq.2).1
270271

271272

@@ -290,7 +291,7 @@ def gstep (ps : SQ Pair) : SQ Pair :=
290291
let (p₂, rs) := extractMin qs
291292
add (node p₁ p₂) rs
292293

293-
def huffman : List Elem → Tree Elem :=
294+
def huffman : List Elem → S1.Tree Elem :=
294295
extractSQ ∘ until' singleSQ gstep ∘ makeSQ ∘ List.map leaf
295296

296297
-- #eval huffman [('a', 2), ('b', 3), ('c', 1), ('d', 20)]

0 commit comments

Comments
 (0)