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