| author | bulwahn | 
| Thu, 18 Aug 2011 12:06:17 +0200 | |
| changeset 44272 | 360fcbb1aa01 | 
| parent 41493 | f05976d69141 | 
| child 51930 | 52fd62618631 | 
| permissions | -rw-r--r-- | 
| 21394 | 1 | (* Title: Pure/General/basics.ML | 
| 2 | Author: Florian Haftmann and Makarius, TU Muenchen | |
| 3 | ||
| 4 | Fundamental concepts. | |
| 5 | *) | |
| 6 | ||
| 7 | infix 1 |> |-> |>> ||> ||>> | |
| 8 | infix 1 #> #-> #>> ##> ##>> | |
| 9 | ||
| 10 | signature BASICS = | |
| 11 | sig | |
| 12 | (*functions*) | |
| 13 |   val |> : 'a * ('a -> 'b) -> 'b
 | |
| 14 |   val |-> : ('c * 'a) * ('c -> 'a -> 'b) -> 'b
 | |
| 15 |   val |>> : ('a * 'c) * ('a -> 'b) -> 'b * 'c
 | |
| 16 |   val ||> : ('c * 'a) * ('a -> 'b) -> 'c * 'b
 | |
| 17 |   val ||>> : ('c * 'a) * ('a -> 'd * 'b) -> ('c * 'd) * 'b
 | |
| 18 |   val #> : ('a -> 'b) * ('b -> 'c) -> 'a -> 'c
 | |
| 19 |   val #-> : ('a -> 'c * 'b) * ('c -> 'b -> 'd) -> 'a -> 'd
 | |
| 20 |   val #>> : ('a -> 'c * 'b) * ('c -> 'd) -> 'a -> 'd * 'b
 | |
| 21 |   val ##> : ('a -> 'c * 'b) * ('b -> 'd) -> 'a -> 'c * 'd
 | |
| 22 |   val ##>> : ('a -> 'c * 'b) * ('b -> 'e * 'd) -> 'a -> ('c * 'e) * 'd
 | |
| 23 |   val ` : ('b -> 'a) -> 'b -> 'a * 'b
 | |
| 24 |   val tap: ('b -> 'a) -> 'b -> 'b
 | |
| 25 | ||
| 26 | (*options*) | |
| 27 | val is_some: 'a option -> bool | |
| 28 | val is_none: 'a option -> bool | |
| 29 | val the: 'a option -> 'a | |
| 30 | val these: 'a list option -> 'a list | |
| 31 | val the_list: 'a option -> 'a list | |
| 32 | val the_default: 'a -> 'a option -> 'a | |
| 33 |   val perhaps: ('a -> 'a option) -> 'a -> 'a
 | |
| 41493 | 34 | val merge_options: 'a option * 'a option -> 'a option | 
| 21394 | 35 | |
| 36 | (*partiality*) | |
| 37 |   val try: ('a -> 'b) -> 'a -> 'b option
 | |
| 38 |   val can: ('a -> 'b) -> 'a -> bool
 | |
| 39 | ||
| 40 | (*lists*) | |
| 41 | val cons: 'a -> 'a list -> 'a list | |
| 42 | val append: 'a list -> 'a list -> 'a list | |
| 43 |   val fold: ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b
 | |
| 44 |   val fold_rev: ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b
 | |
| 45 |   val fold_map: ('a -> 'b -> 'c * 'b) -> 'a list -> 'b -> 'c list * 'b
 | |
| 46 | end; | |
| 47 | ||
| 48 | structure Basics: BASICS = | |
| 49 | struct | |
| 50 | ||
| 51 | (* functions *) | |
| 52 | ||
| 53 | (*application and structured results*) | |
| 54 | fun x |> f = f x; | |
| 55 | fun (x, y) |-> f = f x y; | |
| 56 | fun (x, y) |>> f = (f x, y); | |
| 57 | fun (x, y) ||> f = (x, f y); | |
| 58 | fun (x, y) ||>> f = let val (z, y') = f y in ((x, z), y') end; | |
| 59 | ||
| 60 | (*composition and structured results*) | |
| 61 | fun (f #> g) x = x |> f |> g; | |
| 62 | fun (f #-> g) x = x |> f |-> g; | |
| 63 | fun (f #>> g) x = x |> f |>> g; | |
| 64 | fun (f ##> g) x = x |> f ||> g; | |
| 65 | fun (f ##>> g) x = x |> f ||>> g; | |
| 66 | ||
| 67 | (*result views*) | |
| 68 | fun `f = fn x => (f x, x); | |
| 69 | fun tap f = fn x => (f x; x); | |
| 70 | ||
| 71 | ||
| 72 | (* options *) | |
| 73 | ||
| 74 | fun is_some (SOME _) = true | |
| 75 | | is_some NONE = false; | |
| 76 | ||
| 77 | fun is_none (SOME _) = false | |
| 78 | | is_none NONE = true; | |
| 79 | ||
| 80 | fun the (SOME x) = x | |
| 81 | | the NONE = raise Option; | |
| 82 | ||
| 83 | fun these (SOME x) = x | |
| 23559 | 84 | | these NONE = []; | 
| 21394 | 85 | |
| 86 | fun the_list (SOME x) = [x] | |
| 23559 | 87 | | the_list NONE = [] | 
| 21394 | 88 | |
| 89 | fun the_default x (SOME y) = y | |
| 23559 | 90 | | the_default x NONE = x; | 
| 21394 | 91 | |
| 92 | fun perhaps f x = the_default x (f x); | |
| 93 | ||
| 41493 | 94 | fun merge_options (x, y) = if is_some x then x else y; | 
| 95 | ||
| 21394 | 96 | |
| 97 | (* partiality *) | |
| 98 | ||
| 99 | fun try f x = SOME (f x) | |
| 39232 
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
 wenzelm parents: 
31480diff
changeset | 100 | handle exn => if Exn.is_interrupt exn then reraise exn else NONE; | 
| 21394 | 101 | |
| 102 | fun can f x = is_some (try f x); | |
| 103 | ||
| 104 | ||
| 105 | (* lists *) | |
| 106 | ||
| 107 | fun cons x xs = x :: xs; | |
| 108 | ||
| 109 | fun append xs ys = xs @ ys; | |
| 110 | ||
| 23225 | 111 | fun fold _ [] y = y | 
| 112 | | fold f (x :: xs) y = fold f xs (f x y); | |
| 21394 | 113 | |
| 23225 | 114 | fun fold_rev _ [] y = y | 
| 115 | | fold_rev f (x :: xs) y = f x (fold_rev f xs y); | |
| 21394 | 116 | |
| 23225 | 117 | fun fold_map _ [] y = ([], y) | 
| 118 | | fold_map f (x :: xs) y = | |
| 119 | let | |
| 120 | val (x', y') = f x y; | |
| 121 | val (xs', y'') = fold_map f xs y'; | |
| 122 | in (x' :: xs', y'') end; | |
| 21394 | 123 | |
| 124 | end; | |
| 125 | ||
| 126 | open Basics; |