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