Import Upstream version 20180207
[hcoop/debian/mlton.git] / doc / guide / src / StaticSum.adoc
CommitLineData
7f918cf1
CE
1StaticSum
2=========
3
4While SML makes it impossible to write functions whose types would
5depend on the values of their arguments, or so called dependently
6typed functions, it is possible, and arguably commonplace, to write
7functions whose types depend on the types of their arguments. Indeed,
8the types of parametrically polymorphic functions like `map` and
9`foldl` can be said to depend on the types of their arguments. What
10is less commonplace, however, is to write functions whose behavior
11would depend on the types of their arguments. Nevertheless, there are
12several techniques for writing such functions.
13<:TypeIndexedValues:Type-indexed values> and <:Fold:fold> are two such
14techniques. This page presents another such technique dubbed static
15sums.
16
17
18== Ordinary Sums ==
19
20Consider the sum type as defined below:
21[source,sml]
22----
23structure Sum = struct
24 datatype ('a, 'b) t = INL of 'a | INR of 'b
25end
26----
27
28While a generic sum type such as defined above is very useful, it has
29a number of limitations. As an example, we could write the function
30`out` to extract the value from a sum as follows:
31[source,sml]
32----
33fun out (s : ('a, 'a) Sum.t) : 'a =
34 case s
35 of Sum.INL a => a
36 | Sum.INR a => a
37----
38
39As can be seen from the type of `out`, it is limited in the sense that
40it requires both variants of the sum to have the same type. So, `out`
41cannot be used to extract the value of a sum of two different types,
42such as the type `(int, real) Sum.t`. As another example of a
43limitation, consider the following attempt at a `succ` function:
44[source,sml]
45----
46fun succ (s : (int, real) Sum.t) : ??? =
47 case s
48 of Sum.INL i => i + 1
49 | Sum.INR r => Real.nextAfter (r, Real.posInf)
50----
51
52The above definition of `succ` cannot be typed, because there is no
53type for the codomain within SML.
54
55
56== Static Sums ==
57
58Interestingly, it is possible to define values `inL`, `inR`, and
59`match` that satisfy the laws
60----
61match (inL x) (f, g) = f x
62match (inR x) (f, g) = g x
63----
64and do not suffer from the same limitions. The definitions are
65actually quite trivial:
66[source,sml]
67----
68structure StaticSum = struct
69 fun inL x (f, _) = f x
70 fun inR x (_, g) = g x
71 fun match x = x
72end
73----
74
75Now, given the `succ` function defined as
76[source,sml]
77----
78fun succ s =
79 StaticSum.match s
80 (fn i => i + 1,
81 fn r => Real.nextAfter (r, Real.posInf))
82----
83we get
84[source,sml]
85----
86succ (StaticSum.inL 1) = 2
87succ (StaticSum.inR Real.maxFinite) = Real.posInf
88----
89
90To better understand how this works, consider the following signature
91for static sums:
92[source,sml]
93----
94structure StaticSum :> sig
95 type ('dL, 'cL, 'dR, 'cR, 'c) t
96 val inL : 'dL -> ('dL, 'cL, 'dR, 'cR, 'cL) t
97 val inR : 'dR -> ('dL, 'cL, 'dR, 'cR, 'cR) t
98 val match : ('dL, 'cL, 'dR, 'cR, 'c) t -> ('dL -> 'cL) * ('dR -> 'cR) -> 'c
99end = struct
100 type ('dL, 'cL, 'dR, 'cR, 'c) t = ('dL -> 'cL) * ('dR -> 'cR) -> 'c
101 open StaticSum
102end
103----
104
105Above, `'d` stands for domain and `'c` for codomain. The key
106difference between an ordinary sum type, like `(int, real) Sum.t`, and
107a static sum type, like `(int, real, real, int, real) StaticSum.t`, is
108that the ordinary sum type says nothing about the type of the result
109of deconstructing a sum while the static sum type specifies the type.
110
111With the sealed static sum module, we get the type
112[source,sml]
113----
114val succ : (int, int, real, real, 'a) StaticSum.t -> 'a
115----
116for the previously defined `succ` function. The type specifies that
117`succ` maps a left `int` to an `int` and a right `real` to a `real`.
118For example, the type of `StaticSum.inL 1` is
119`(int, 'cL, 'dR, 'cR, 'cL) StaticSum.t`. Unifying this with the
120argument type of `succ` gives the type `(int, int, real, real, int)
121StaticSum.t -> int`.
122
123The `out` function is quite useful on its own. Here is how it can be
124defined:
125[source,sml]
126----
127structure StaticSum = struct
128 open StaticSum
129 val out : ('a, 'a, 'b, 'b, 'c) t -> 'c =
130 fn s => match s (fn x => x, fn x => x)
131end
132----
133
134Due to the value restriction, lack of first class polymorphism and
135polymorphic recursion, the usefulness and convenience of static sums
136is somewhat limited in SML. So, don't throw away the ordinary sum
137type just yet. Static sums can nevertheless be quite useful.
138
139
140=== Example: Send and Receive with Argument Type Dependent Result Types ===
141
142In some situations it would seem useful to define functions whose
143result type would depend on some of the arguments. Traditionally such
144functions have been thought to be impossible in SML and the solution
145has been to define multiple functions. For example, the
146http://www.standardml.org/Basis/socket.html[`Socket` structure] of the
147Basis library defines 16 `send` and 16 `recv` functions. In contrast,
148the Net structure
149(<!ViewGitFile(mltonlib,master,com/sweeks/basic/unstable/net.sig)>) of the
150Basic library designed by Stephen Weeks defines only a single `send`
151and a single `receive` and the result types of the functions depend on
152their arguments. The implementation
153(<!ViewGitFile(mltonlib,master,com/sweeks/basic/unstable/net.sml)>) uses
154static sums (with a slighly different signature:
155<!ViewGitFile(mltonlib,master,com/sweeks/basic/unstable/static-sum.sig)>).
156
157
158=== Example: Picking Monad Results ===
159
160Suppose that we need to write a parser that accepts a pair of integers
161and returns their sum given a monadic parsing combinator library. A
162part of the signature of such library could look like this
163[source,sml]
164----
165signature PARSING = sig
166 include MONAD
167 val int : int t
168 val lparen : unit t
169 val rparen : unit t
170 val comma : unit t
171 (* ... *)
172end
173----
174where the `MONAD` signature could be defined as
175[source,sml]
176----
177signature MONAD = sig
178 type 'a t
179 val return : 'a -> 'a t
180 val >>= : 'a t * ('a -> 'b t) -> 'b t
181end
182infix >>=
183----
184
185The straightforward, but tedious, way to write the desired parser is:
186[source,sml]
187----
188val p = lparen >>= (fn _ =>
189 int >>= (fn x =>
190 comma >>= (fn _ =>
191 int >>= (fn y =>
192 rparen >>= (fn _ =>
193 return (x + y))))))
194----
195
196In Haskell, the parser could be written using the `do` notation
197considerably less verbosely as:
198[source,haskell]
199----
200p = do { lparen ; x <- int ; comma ; y <- int ; rparen ; return $ x + y }
201----
202
203SML doesn't provide a `do` notation, so we need another solution.
204
205Suppose we would have a "pick" notation for monads that would allows
206us to write the parser as
207[source,sml]
208----
209val p = `lparen ^ \int ^ `comma ^ \int ^ `rparen @ (fn x & y => x + y)
210----
211using four auxiliary combinators: +&grave;+, `\`, `^`, and `@`.
212
213Roughly speaking
214
215* +&grave;p+ means that the result of `p` is dropped,
216* `\p` means that the result of `p` is taken,
217* `p ^ q` means that results of `p` and `q` are taken as a product, and
218* `p @ a` means that the results of `p` are passed to the function `a` and that result is returned.
219
220The difficulty is in implementing the concatenation combinator `^`.
221The type of the result of the concatenation depends on the types of
222the arguments.
223
224Using static sums and the <:ProductType:product type>, the pick
225notation for monads can be implemented as follows:
226[source,sml]
227----
228functor MkMonadPick (include MONAD) = let
229 open StaticSum
230in
231 struct
232 fun `a = inL (a >>= (fn _ => return ()))
233 val \ = inR
234 fun a @ f = out a >>= (return o f)
235 fun a ^ b =
236 (match b o match a)
237 (fn a =>
238 (fn b => inL (a >>= (fn _ => b)),
239 fn b => inR (a >>= (fn _ => b))),
240 fn a =>
241 (fn b => inR (a >>= (fn a => b >>= (fn _ => return a))),
242 fn b => inR (a >>= (fn a => b >>= (fn b => return (a & b))))))
243 end
244end
245----
246
247The above implementation is inefficient, however. It uses many more
248bind operations, `>>=`, than necessary. That can be solved with an
249additional level of abstraction:
250[source,sml]
251----
252functor MkMonadPick (include MONAD) = let
253 open StaticSum
254in
255 struct
256 fun `a = inL (fn b => a >>= (fn _ => b ()))
257 fun \a = inR (fn b => a >>= b)
258 fun a @ f = out a (return o f)
259 fun a ^ b =
260 (match b o match a)
261 (fn a => (fn b => inL (fn c => a (fn () => b c)),
262 fn b => inR (fn c => a (fn () => b c))),
263 fn a => (fn b => inR (fn c => a (fn a => b (fn () => c a))),
264 fn b => inR (fn c => a (fn a => b (fn b => c (a & b))))))
265 end
266end
267----
268
269After instantiating and opening either of the above monad pick
270implementations, the previously given definition of `p` can be
271compiled and results in a parser whose result is of type `int`. Here
272is a functor to test the theory:
273[source,sml]
274----
275functor Test (Arg : PARSING) = struct
276 local
277 structure Pick = MkMonadPick (Arg)
278 open Pick Arg
279 in
280 val p : int t =
281 `lparen ^ \int ^ `comma ^ \int ^ `rparen @ (fn x & y => x + y)
282 end
283end
284----
285
286
287== Also see ==
288
289There are a number of related techniques. Here are some of them.
290
291* <:Fold:>
292* <:TypeIndexedValues:>