Chapter Info #
In this chapter $ 2 ∈ 3$ we will confine ourselves to just two examples. The first is a simple scheduling problem, while the second involves breaking paragraphs into lines.
A partition of a list of type [A] has type [[A]], so a list of partitions has type [[[A]]].
Equations
- Chapter12.Segment a = List a
Instances For
Equations
Instances For
Equations
- Chapter12.cons x p = [x] :: p
Instances For
Equations
- Chapter12.glue x (s :: p) = (x :: s) :: p
- Chapter12.glue x [] = panicWithPosWithDecl "Fad.Chapter12" "Chapter12.glue" 64 13 "glue: empty partition"
Instances For
Equations
- Chapter12.extendl x [] = [Chapter12.cons x []]
- Chapter12.extendl x x✝ = [Chapter12.cons x x✝, Chapter12.glue x x✝]
Instances For
Equations
Instances For
Equations
- Chapter12.last [x_1] = x_1
- Chapter12.last [] = panicWithPosWithDecl "Fad.Chapter12" "Chapter12.last" 76 12 "last: empty list"
- Chapter12.last (head :: xs) = Chapter12.last xs
Instances For
Equations
- Chapter12.init [x_1] = []
- Chapter12.init [] = panicWithPosWithDecl "Fad.Chapter12" "Chapter12.init" 81 14 "init: empty list"
- Chapter12.init (head :: xs) = head :: Chapter12.init xs
Instances For
Equations
- Chapter12.bind x p = Chapter12.init p ++ [Chapter12.last p ++ [x]]
Instances For
Equations
- Chapter12.extendr x [] = [Chapter12.snoc x []]
- Chapter12.extendr x x✝ = [Chapter12.snoc x x✝, Chapter12.bind x x✝]
Instances For
Equations
Instances For
The major constraint on paragraphs is that all lines have to fit into a specified width. For simplicity, we assume a single globally defined value maxWidth that gives the maximum width a line can possess.
Equations
Instances For
The definition depends on another globally defined constant optWidth, whose value is at most maxWidth and which specifies the optimum width of each line of a paragraph.
Equations
Instances For
It is assumed that a text consists of a nonempty sequence of words, each word being a nonempty sequence of non-space characters. A paragraph therefore consists of at least one line.
Equations
Instances For
Equations
Instances For
Equations
Instances For
Equations
Instances For
Equations
- Chapter12.width line = Chapter8.S1.foldrn (fun (w : Chapter12.Word) (n : ℕ) => List.length w + 1 + n) (fun (w : Chapter12.Word) => List.length w) line
Instances For
Equations
- Chapter12.aux_add w n = List.length w + 1 + n
Instances For
Instances For
Equations
- Chapter12.fits line = decide (Chapter12.width line ≤ Chapter12.maxWidth)
Instances For
Equations
- Chapter12.waste₂ line = Chapter12.maxWidth - Chapter12.width line
Instances For
Equations
Instances For
Equations
- Chapter12.waste₃ line = (Chapter12.optWidth - Chapter12.width line) * (Chapter12.optWidth - Chapter12.width line)
Instances For
Equations
Instances For
Equations
Instances For
Equations
Instances For
There is an obvious greedy algorithm for the paragraph problem:
Equations
- Chapter12.add [] w = Chapter12.snoc w []
- Chapter12.add p w = (List.filter (fun (para : List Chapter12.Line) => Chapter12.fits (Chapter12.last para)) [Chapter12.bind w p, Chapter12.snoc w p]).headD []
Instances For
Equations
Instances For
Adding cost to the algorithm
Equations
- Chapter12.tstep [[]] w cost = [[[w]]]
- Chapter12.tstep ps w cost = Chapter7.minWith cost (List.map (Chapter12.snoc w) ps) :: List.filter (Chapter12.fits ∘ Chapter12.last) (List.map (Chapter12.bind w) ps)
Instances For
Equations
- Chapter12.para ws cost = Chapter7.minWith cost (List.foldl (fun (ps : List Chapter12.Para) (w : Chapter12.Word) => Chapter12.tstep ps w cost) [[]] ws)