@@ -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)
1515open Chapter1 (wrap unwrap single until ' concatMap)
1616open Chapter6 (foldl1)
1717open Chapter7 (minWith)
@@ -176,32 +176,33 @@ end S1
176176/- 8.2 Huffman coding trees -/
177177
178178namespace S2
179+
179180open S1 (Tree Forest)
180181open Chapter1 (concatMap wrap unwrap unwrap! single until ' single picks apply)
181182open 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
190191abbrev Weight := Nat
191192abbrev Elem := Char × Weight
192193abbrev 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
206207def 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
221222def 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 :=
251252abbrev Queue (α : Type ) := Chapter3.SymList α
252253abbrev Stack (α : Type ) := List α
253254abbrev SQ (α : Type ) := Stack α × Queue α
254- abbrev Pair := Tree Elem × Weight
255+ abbrev Pair := S1. Tree Elem × Weight
255256
256257def leaf : Elem → Pair
257258 | (c, w) => (Tree.leaf (c, w), w)
258259
259260def 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
262263def makeSQ (xs : List Pair) : SQ Pair :=
263264 (xs, nil)
264265
265266def 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