| 1 |
= Fold01N |
| 2 |
|
| 3 |
A common use pattern of <<Fold#>> is to define a variable-arity |
| 4 |
function that combines multiple arguments together using a binary |
| 5 |
function. It is slightly tricky to do this directly using fold, |
| 6 |
because of the special treatment required for the case of zero or one |
| 7 |
argument. Here is a structure, `Fold01N`, that solves the problem |
| 8 |
once and for all, and eases the definition of such functions. |
| 9 |
|
| 10 |
[source,sml] |
| 11 |
---- |
| 12 |
structure Fold01N = |
| 13 |
struct |
| 14 |
fun fold {finish, start, zero} = |
| 15 |
Fold.fold ((id, finish, fn () => zero, start), |
| 16 |
fn (finish, _, p, _) => finish (p ())) |
| 17 |
|
| 18 |
fun step0 {combine, input} = |
| 19 |
Fold.step0 (fn (_, finish, _, f) => |
| 20 |
(finish, |
| 21 |
finish, |
| 22 |
fn () => f input, |
| 23 |
fn x' => combine (f input, x'))) |
| 24 |
|
| 25 |
fun step1 {combine} z input = |
| 26 |
step0 {combine = combine, input = input} z |
| 27 |
end |
| 28 |
---- |
| 29 |
|
| 30 |
If one has a value `zero`, and functions `start`, `c`, and `finish`, |
| 31 |
then one can define a variable-arity function `f` and stepper |
| 32 |
`{backtick}` as follows. |
| 33 |
[source,sml] |
| 34 |
---- |
| 35 |
val f = fn z => Fold01N.fold {finish = finish, start = start, zero = zero} z |
| 36 |
val ` = fn z => Fold01N.step1 {combine = c} z |
| 37 |
---- |
| 38 |
|
| 39 |
One can then use the fold equation to prove the following equations. |
| 40 |
[source,sml] |
| 41 |
---- |
| 42 |
f $ = zero |
| 43 |
f `a1 $ = finish (start a1) |
| 44 |
f `a1 `a2 $ = finish (c (start a1, a2)) |
| 45 |
f `a1 `a2 `a3 $ = finish (c (c (start a1, a2), a3)) |
| 46 |
... |
| 47 |
---- |
| 48 |
|
| 49 |
For an example of `Fold01N`, see <<VariableArityPolymorphism#>>. |
| 50 |
|
| 51 |
|
| 52 |
== Typing Fold01N |
| 53 |
|
| 54 |
Here is the signature for `Fold01N`. We use a trick to avoid having |
| 55 |
to duplicate the definition of some rather complex types in both the |
| 56 |
signature and the structure. We first define the types in a |
| 57 |
structure. Then, we define them via type re-definitions in the |
| 58 |
signature, and via `open` in the full structure. |
| 59 |
[source,sml] |
| 60 |
---- |
| 61 |
structure Fold01N = |
| 62 |
struct |
| 63 |
type ('input, 'accum1, 'accum2, 'answer, 'zero, |
| 64 |
'a, 'b, 'c, 'd, 'e) t = |
| 65 |
(('zero -> 'zero) |
| 66 |
* ('accum2 -> 'answer) |
| 67 |
* (unit -> 'zero) |
| 68 |
* ('input -> 'accum1), |
| 69 |
('a -> 'b) * 'c * (unit -> 'a) * 'd, |
| 70 |
'b, |
| 71 |
'e) Fold.t |
| 72 |
|
| 73 |
type ('input1, 'accum1, 'input2, 'accum2, |
| 74 |
'a, 'b, 'c, 'd, 'e, 'f) step0 = |
| 75 |
('a * 'b * 'c * ('input1 -> 'accum1), |
| 76 |
'b * 'b * (unit -> 'accum1) * ('input2 -> 'accum2), |
| 77 |
'd, 'e, 'f) Fold.step0 |
| 78 |
|
| 79 |
type ('accum1, 'input, 'accum2, |
| 80 |
'a, 'b, 'c, 'd, 'e, 'f, 'g) step1 = |
| 81 |
('a, |
| 82 |
'b * 'c * 'd * ('a -> 'accum1), |
| 83 |
'c * 'c * (unit -> 'accum1) * ('input -> 'accum2), |
| 84 |
'e, 'f, 'g) Fold.step1 |
| 85 |
end |
| 86 |
|
| 87 |
signature FOLD_01N = |
| 88 |
sig |
| 89 |
type ('a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j) t = |
| 90 |
('a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j) Fold01N.t |
| 91 |
type ('a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j) step0 = |
| 92 |
('a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j) Fold01N.step0 |
| 93 |
type ('a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j) step1 = |
| 94 |
('a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j) Fold01N.step1 |
| 95 |
|
| 96 |
val fold: |
| 97 |
{finish: 'accum2 -> 'answer, |
| 98 |
start: 'input -> 'accum1, |
| 99 |
zero: 'zero} |
| 100 |
-> ('input, 'accum1, 'accum2, 'answer, 'zero, |
| 101 |
'a, 'b, 'c, 'd, 'e) t |
| 102 |
|
| 103 |
val step0: |
| 104 |
{combine: 'accum1 * 'input2 -> 'accum2, |
| 105 |
input: 'input1} |
| 106 |
-> ('input1, 'accum1, 'input2, 'accum2, |
| 107 |
'a, 'b, 'c, 'd, 'e, 'f) step0 |
| 108 |
|
| 109 |
val step1: |
| 110 |
{combine: 'accum1 * 'input -> 'accum2} |
| 111 |
-> ('accum1, 'input, 'accum2, |
| 112 |
'a, 'b, 'c, 'd, 'e, 'f, 'g) step1 |
| 113 |
end |
| 114 |
|
| 115 |
structure Fold01N: FOLD_01N = |
| 116 |
struct |
| 117 |
open Fold01N |
| 118 |
|
| 119 |
fun fold {finish, start, zero} = |
| 120 |
Fold.fold ((id, finish, fn () => zero, start), |
| 121 |
fn (finish, _, p, _) => finish (p ())) |
| 122 |
|
| 123 |
fun step0 {combine, input} = |
| 124 |
Fold.step0 (fn (_, finish, _, f) => |
| 125 |
(finish, |
| 126 |
finish, |
| 127 |
fn () => f input, |
| 128 |
fn x' => combine (f input, x'))) |
| 129 |
|
| 130 |
fun step1 {combine} z input = |
| 131 |
step0 {combine = combine, input = input} z |
| 132 |
end |
| 133 |
---- |