| author | wenzelm | 
| Thu, 01 Dec 2022 11:36:45 +0100 | |
| changeset 76551 | c7996b073524 | 
| parent 62505 | 9e2a65912111 | 
| child 78062 | edb195122938 | 
| 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 | 
| 61845 | 35 |   val eq_option: ('a * 'b -> bool) -> 'a option * 'b option -> bool
 | 
| 21394 | 36 | |
| 37 | (*partiality*) | |
| 38 |   val try: ('a -> 'b) -> 'a -> 'b option
 | |
| 39 |   val can: ('a -> 'b) -> 'a -> bool
 | |
| 40 | ||
| 41 | (*lists*) | |
| 42 | val cons: 'a -> 'a list -> 'a list | |
| 43 | val append: 'a list -> 'a list -> 'a list | |
| 44 |   val fold: ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b
 | |
| 45 |   val fold_rev: ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b
 | |
| 46 |   val fold_map: ('a -> 'b -> 'c * 'b) -> 'a list -> 'b -> 'c list * 'b
 | |
| 47 | end; | |
| 48 | ||
| 49 | structure Basics: BASICS = | |
| 50 | struct | |
| 51 | ||
| 52 | (* functions *) | |
| 53 | ||
| 54 | (*application and structured results*) | |
| 55 | fun x |> f = f x; | |
| 56 | fun (x, y) |-> f = f x y; | |
| 57 | fun (x, y) |>> f = (f x, y); | |
| 58 | fun (x, y) ||> f = (x, f y); | |
| 59 | fun (x, y) ||>> f = let val (z, y') = f y in ((x, z), y') end; | |
| 60 | ||
| 61 | (*composition and structured results*) | |
| 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 | fun (f ##>> g) x = x |> f ||>> g; | |
| 67 | ||
| 68 | (*result views*) | |
| 69 | fun `f = fn x => (f x, x); | |
| 70 | fun tap f = fn x => (f x; x); | |
| 71 | ||
| 72 | ||
| 73 | (* options *) | |
| 74 | ||
| 75 | fun is_some (SOME _) = true | |
| 76 | | is_some NONE = false; | |
| 77 | ||
| 78 | fun is_none (SOME _) = false | |
| 79 | | is_none NONE = true; | |
| 80 | ||
| 81 | fun the (SOME x) = x | |
| 51930 
52fd62618631
prefer explicitly qualified exceptions, which is particular important for robust handlers;
 wenzelm parents: 
41493diff
changeset | 82 | | the NONE = raise Option.Option; | 
| 21394 | 83 | |
| 84 | fun these (SOME x) = x | |
| 23559 | 85 | | these NONE = []; | 
| 21394 | 86 | |
| 87 | fun the_list (SOME x) = [x] | |
| 23559 | 88 | | the_list NONE = [] | 
| 21394 | 89 | |
| 90 | fun the_default x (SOME y) = y | |
| 23559 | 91 | | the_default x NONE = x; | 
| 21394 | 92 | |
| 93 | fun perhaps f x = the_default x (f x); | |
| 94 | ||
| 41493 | 95 | fun merge_options (x, y) = if is_some x then x else y; | 
| 96 | ||
| 61845 | 97 | fun eq_option eq (SOME x, SOME y) = eq (x, y) | 
| 98 | | eq_option _ (NONE, NONE) = true | |
| 99 | | eq_option _ _ = false; | |
| 100 | ||
| 21394 | 101 | |
| 102 | (* partiality *) | |
| 103 | ||
| 104 | fun try f x = SOME (f x) | |
| 62505 | 105 | handle exn => if Exn.is_interrupt exn then Exn.reraise exn else NONE; | 
| 21394 | 106 | |
| 107 | fun can f x = is_some (try f x); | |
| 108 | ||
| 109 | ||
| 110 | (* lists *) | |
| 111 | ||
| 112 | fun cons x xs = x :: xs; | |
| 113 | ||
| 114 | fun append xs ys = xs @ ys; | |
| 115 | ||
| 23225 | 116 | fun fold _ [] y = y | 
| 117 | | fold f (x :: xs) y = fold f xs (f x y); | |
| 21394 | 118 | |
| 23225 | 119 | fun fold_rev _ [] y = y | 
| 120 | | fold_rev f (x :: xs) y = f x (fold_rev f xs y); | |
| 21394 | 121 | |
| 23225 | 122 | fun fold_map _ [] y = ([], y) | 
| 123 | | fold_map f (x :: xs) y = | |
| 124 | let | |
| 125 | val (x', y') = f x y; | |
| 126 | val (xs', y'') = fold_map f xs y'; | |
| 127 | in (x' :: xs', y'') end; | |
| 21394 | 128 | |
| 129 | end; | |
| 130 | ||
| 131 | open Basics; |