Import Upstream version 20180207
[hcoop/debian/mlton.git] / doc / guide / src / Utilities.adoc
CommitLineData
7f918cf1
CE
1Utilities
2=========
3
4This page is a collection of basic utilities used in the examples on
5various pages. See
6
7 * <:InfixingOperators:>, and
8 * <:ProductType:>
9
10for longer discussions on some of these utilities.
11
12[source,sml]
13----
14(* Operator precedence table *)
15infix 8 * / div mod (* +1 from Basis Library *)
16infix 7 + - ^ (* +1 from Basis Library *)
17infixr 6 :: @ (* +1 from Basis Library *)
18infix 5 = <> > >= < <= (* +1 from Basis Library *)
19infix 4 <\ \>
20infixr 4 </ />
21infix 3 o
22infix 2 >|
23infixr 2 |<
24infix 1 := (* -2 from Basis Library *)
25infix 0 before &
26
27(* Some basic combinators *)
28fun const x _ = x
29fun cross (f, g) (x, y) = (f x, g y)
30fun curry f x y = f (x, y)
31fun fail e _ = raise e
32fun id x = x
33
34(* Product type *)
35datatype ('a, 'b) product = & of 'a * 'b
36
37(* Sum type *)
38datatype ('a, 'b) sum = INL of 'a | INR of 'b
39
40(* Some type shorthands *)
41type 'a uop = 'a -> 'a
42type 'a fix = 'a uop -> 'a
43type 'a thunk = unit -> 'a
44type 'a effect = 'a -> unit
45type ('a, 'b) emb = ('a -> 'b) * ('b -> 'a)
46
47(* Infixing, sectioning, and application operators *)
48fun x <\ f = fn y => f (x, y)
49fun f \> y = f y
50fun f /> y = fn x => f (x, y)
51fun x </ f = f x
52
53(* Piping operators *)
54val op>| = op</
55val op|< = op\>
56----