Debian Codegraph @

doc/guide/src/OptionalArguments.adoc

raw
1 = OptionalArguments
2
3 <<StandardML#,Standard ML>> does not have built-in support for optional
4 arguments. Nevertheless, using <<Fold#>>, it is easy to define
5 functions that take optional arguments.
6
7 For example, suppose that we have the following definition of a
8 function `f`.
9
10 [source,sml]
11 ----
12 fun f (i, r, s) =
13 concat [Int.toString i, ", ", Real.toString r, ", ", s]
14 ----
15
16 Using the `OptionalArg` structure described below, we can define a
17 function `f'`, an optionalized version of `f`, that takes 0, 1, 2, or
18 3 arguments. Embedded within `f'` will be default values for `i`,
19 `r`, and `s`. If `f'` gets no arguments, then all the defaults are
20 used. If `f'` gets one argument, then that will be used for `i`. Two
21 arguments will be used for `i` and `r` respectively. Three arguments
22 will override all default values. Calls to `f'` will look like the
23 following.
24
25 [source,sml]
26 ----
27 f' $
28 f' `2 $
29 f' `2 `3.0 $
30 f' `2 `3.0 `"four" $
31 ----
32
33 The optional argument indicator, `{backtick}`, is not special syntax ---
34 it is a normal SML value, defined in the `OptionalArg` structure
35 below.
36
37 Here is the definition of `f'` using the `OptionalArg` structure, in
38 particular, `OptionalArg.make` and `OptionalArg.D`.
39
40 [source,sml]
41 ----
42 val f' =
43 fn z =>
44 let open OptionalArg in
45 make (D 1) (D 2.0) (D "three") $
46 end (fn i & r & s => f (i, r, s))
47 z
48 ----
49
50 The definition of `f'` is eta expanded as with all uses of fold. A
51 call to `OptionalArg.make` is supplied with a variable number of
52 defaults (in this case, three), the end-of-arguments terminator, `$`,
53 and the function to run, taking its arguments as an n-ary
54 <<ProductType#,product>>. In this case, the function simply converts
55 the product to an ordinary tuple and calls `f`. Often, the function
56 body will simply be written directly.
57
58 In general, the definition of an optional-argument function looks like
59 the following.
60
61 [source,sml]
62 ----
63 val f =
64 fn z =>
65 let open OptionalArg in
66 make (D <default1>) (D <default2>) ... (D <defaultn>) $
67 end (fn x1 & x2 & ... & xn =>
68 <function code goes here>)
69 z
70 ----
71
72 Here is the definition of `OptionalArg`.
73
74 [source,sml]
75 ----
76 structure OptionalArg =
77 struct
78 val make =
79 fn z =>
80 Fold.fold
81 ((id, fn (f, x) => f x),
82 fn (d, r) => fn func =>
83 Fold.fold ((id, d ()), fn (f, d) =>
84 let
85 val d & () = r (id, f d)
86 in
87 func d
88 end))
89 z
90
91 fun D d = Fold.step0 (fn (f, r) =>
92 (fn ds => f (d & ds),
93 fn (f, a & b) => r (fn x => f a & x, b)))
94
95 val ` =
96 fn z =>
97 Fold.step1 (fn (x, (f, _ & d)) => (fn d => f (x & d), d))
98 z
99 end
100 ----
101
102 `OptionalArg.make` uses a nested fold. The first `fold` accumulates
103 the default values in a product, associated to the right, and a
104 reversal function that converts a product (of the same arity as the
105 number of defaults) from right associativity to left associativity.
106 The accumulated defaults are used by the second fold, which recurs
107 over the product, replacing the appropriate component as it encounters
108 optional arguments. The second fold also constructs a "fill"
109 function, `f`, that is used to reconstruct the product once the
110 end-of-arguments is reached. Finally, the finisher reconstructs the
111 product and uses the reversal function to convert the product from
112 right associative to left associative, at which point it is passed to
113 the user-supplied function.
114
115 Much of the complexity comes from the fact that while recurring over a
116 product from left to right, one wants it to be right-associative,
117 e.g., look like
118
119 [source,sml]
120 ----
121 a & (b & (c & d))
122 ----
123
124 but the user function in the end wants the product to be left
125 associative, so that the product argument pattern can be written
126 without parentheses (since `&` is left associative).
127
128
129 == Labelled optional arguments
130
131 In addition to the positional optional arguments described above, it
132 is sometimes useful to have labelled optional arguments. These allow
133 one to define a function, `f`, with defaults, say `a` and `b`. Then,
134 a caller of `f` can supply values for `a` and `b` by name. If no
135 value is supplied then the default is used.
136
137 Labelled optional arguments are a simple extension of
138 <<FunctionalRecordUpdate#>> using post composition. Suppose, for
139 example, that one wants a function `f` with labelled optional
140 arguments `a` and `b` with default values `0` and `0.0` respectively.
141 If one has a functional-record-update function `updateAB` for records
142 with `a` and `b` fields, then one can define `f` in the following way.
143
144 [source,sml]
145 ----
146 val f =
147 fn z =>
148 Fold.post
149 (updateAB {a = 0, b = 0.0},
150 fn {a, b} => print (concat [Int.toString a, " ",
151 Real.toString b, "\n"]))
152 z
153 ----
154
155 The idea is that `f` is the post composition (using `Fold.post`) of
156 the actual code for the function with a functional-record updater that
157 starts with the defaults.
158
159 Here are some example calls to `f`.
160 [source,sml]
161 ----
162 val () = f $
163 val () = f (U#a 13) $
164 val () = f (U#a 13) (U#b 17.5) $
165 val () = f (U#b 17.5) (U#a 13) $
166 ----
167
168 Notice that a caller can supply neither of the arguments, either of
169 the arguments, or both of the arguments, and in either order. All
170 that matter is that the arguments be labelled correctly (and of the
171 right type, of course).
172
173 Here is another example.
174
175 [source,sml]
176 ----
177 val f =
178 fn z =>
179 Fold.post
180 (updateBCD {b = 0, c = 0.0, d = "<>"},
181 fn {b, c, d} =>
182 print (concat [Int.toString b, " ",
183 Real.toString c, " ",
184 d, "\n"]))
185 z
186 ----
187
188 Here are some example calls.
189
190 [source,sml]
191 ----
192 val () = f $
193 val () = f (U#d "goodbye") $
194 val () = f (U#d "hello") (U#b 17) (U#c 19.3) $
195 ----