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