Debian Codegraph @

doc/guide/src/VariableArityPolymorphism.adoc

raw
1 = VariableArityPolymorphism
2
3 <<StandardML#,Standard ML>> programmers often face the problem of how to
4 provide a variable-arity polymorphic function. For example, suppose
5 one is defining a combinator library, e.g. for parsing or pickling.
6 The signature for such a library might look something like the
7 following.
8
9 [source,sml]
10 ----
11 signature COMBINATOR =
12 sig
13 type 'a t
14
15 val int: int t
16 val real: real t
17 val string: string t
18 val unit: unit t
19 val tuple2: 'a1 t * 'a2 t -> ('a1 * 'a2) t
20 val tuple3: 'a1 t * 'a2 t * 'a3 t -> ('a1 * 'a2 * 'a3) t
21 val tuple4: 'a1 t * 'a2 t * 'a3 t * 'a4 t
22 -> ('a1 * 'a2 * 'a3 * 'a4) t
23 ...
24 end
25 ----
26
27 The question is how to define a variable-arity tuple combinator.
28 Traditionally, the only way to take a variable number of arguments in
29 SML is to put the arguments in a list (or vector) and pass that. So,
30 one might define a tuple combinator with the following signature.
31 [source,sml]
32 ----
33 val tupleN: 'a list -> 'a list t
34 ----
35
36 The problem with this approach is that as soon as one places values in
37 a list, they must all have the same type. So, programmers often take
38 an alternative approach, and define a family of `tuple<N>` functions,
39 as we see in the `COMBINATOR` signature above.
40
41 The family-of-functions approach is ugly for many reasons. First, it
42 clutters the signature with a number of functions when there should
43 really only be one. Second, it is _closed_, in that there are a fixed
44 number of tuple combinators in the interface, and should a client need
45 a combinator for a large tuple, he is out of luck. Third, this
46 approach often requires a lot of duplicate code in the implementation
47 of the combinators.
48
49 Fortunately, using <<Fold01N#>> and <<ProductType#,products>>, one can
50 provide an interface and implementation that solves all these
51 problems. Here is a simple pickling module that converts values to
52 strings.
53 [source,sml]
54 ----
55 structure Pickler =
56 struct
57 type 'a t = 'a -> string
58
59 val unit = fn () => ""
60
61 val int = Int.toString
62
63 val real = Real.toString
64
65 val string = id
66
67 type 'a accum = 'a * string list -> string list
68
69 val tuple =
70 fn z =>
71 Fold01N.fold
72 {finish = fn ps => fn x => concat (rev (ps (x, []))),
73 start = fn p => fn (x, l) => p x :: l,
74 zero = unit}
75 z
76
77 val ` =
78 fn z =>
79 Fold01N.step1
80 {combine = (fn (p, p') => fn (x & x', l) => p' x' :: "," :: p (x, l))}
81 z
82 end
83 ----
84
85 If one has `n` picklers of types
86 [source,sml]
87 ----
88 val p1: a1 Pickler.t
89 val p2: a2 Pickler.t
90 ...
91 val pn: an Pickler.t
92 ----
93 then one can construct a pickler for n-ary products as follows.
94 [source,sml]
95 ----
96 tuple `p1 `p2 ... `pn $ : (a1 & a2 & ... & an) Pickler.t
97 ----
98
99 For example, with `Pickler` in scope, one can prove the following
100 equations.
101 [source,sml]
102 ----
103 "" = tuple $ ()
104 "1" = tuple `int $ 1
105 "1,2.0" = tuple `int `real $ (1 & 2.0)
106 "1,2.0,three" = tuple `int `real `string $ (1 & 2.0 & "three")
107 ----
108
109 Here is the signature for `Pickler`. It shows why the `accum` type is
110 useful.
111 [source,sml]
112 ----
113 signature PICKLER =
114 sig
115 type 'a t
116
117 val int: int t
118 val real: real t
119 val string: string t
120 val unit: unit t
121
122 type 'a accum
123 val ` : ('a accum, 'b t, ('a, 'b) prod accum,
124 'z1, 'z2, 'z3, 'z4, 'z5, 'z6, 'z7) Fold01N.step1
125 val tuple: ('a t, 'a accum, 'b accum, 'b t, unit t,
126 'z1, 'z2, 'z3, 'z4, 'z5) Fold01N.t
127 end
128
129 structure Pickler: PICKLER = Pickler
130 ----