author | wenzelm |
Tue, 21 Jun 2022 14:51:17 +0200 | |
changeset 75571 | ac5e633ad9b3 |
parent 32025 | e8fbfa84c23f |
child 78712 | c2c4d51b048b |
permissions | -rw-r--r-- |
32015
7101feb5247e
Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff
changeset
|
1 |
(* Title: Pure/General/same.ML |
7101feb5247e
Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff
changeset
|
2 |
Author: Makarius |
7101feb5247e
Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff
changeset
|
3 |
|
7101feb5247e
Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff
changeset
|
4 |
Support for copy-avoiding functions on pure values, at the cost of |
7101feb5247e
Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff
changeset
|
5 |
readability. |
7101feb5247e
Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff
changeset
|
6 |
*) |
7101feb5247e
Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff
changeset
|
7 |
|
7101feb5247e
Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff
changeset
|
8 |
signature SAME = |
7101feb5247e
Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff
changeset
|
9 |
sig |
7101feb5247e
Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff
changeset
|
10 |
exception SAME |
7101feb5247e
Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff
changeset
|
11 |
type ('a, 'b) function = 'a -> 'b (*exception SAME*) |
7101feb5247e
Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff
changeset
|
12 |
type 'a operation = ('a, 'a) function (*exception SAME*) |
32025 | 13 |
val same: ('a, 'b) function |
32015
7101feb5247e
Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff
changeset
|
14 |
val commit: 'a operation -> 'a -> 'a |
7101feb5247e
Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff
changeset
|
15 |
val function: ('a -> 'b option) -> ('a, 'b) function |
7101feb5247e
Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff
changeset
|
16 |
val capture: ('a, 'b) function -> 'a -> 'b option |
32017 | 17 |
val map: 'a operation -> 'a list operation |
32022 | 18 |
val map_option: ('a, 'b) function -> ('a option, 'b option) function |
32015
7101feb5247e
Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff
changeset
|
19 |
end; |
7101feb5247e
Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff
changeset
|
20 |
|
7101feb5247e
Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff
changeset
|
21 |
structure Same: SAME = |
7101feb5247e
Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff
changeset
|
22 |
struct |
7101feb5247e
Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff
changeset
|
23 |
|
7101feb5247e
Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff
changeset
|
24 |
exception SAME; |
7101feb5247e
Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff
changeset
|
25 |
|
7101feb5247e
Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff
changeset
|
26 |
type ('a, 'b) function = 'a -> 'b; |
7101feb5247e
Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff
changeset
|
27 |
type 'a operation = ('a, 'a) function; |
7101feb5247e
Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff
changeset
|
28 |
|
32025 | 29 |
fun same _ = raise SAME; |
32015
7101feb5247e
Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff
changeset
|
30 |
fun commit f x = f x handle SAME => x; |
7101feb5247e
Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff
changeset
|
31 |
|
7101feb5247e
Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff
changeset
|
32 |
fun capture f x = SOME (f x) handle SAME => NONE; |
7101feb5247e
Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff
changeset
|
33 |
|
7101feb5247e
Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff
changeset
|
34 |
fun function f x = |
7101feb5247e
Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff
changeset
|
35 |
(case f x of |
7101feb5247e
Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff
changeset
|
36 |
NONE => raise SAME |
7101feb5247e
Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff
changeset
|
37 |
| SOME y => y); |
7101feb5247e
Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff
changeset
|
38 |
|
32017 | 39 |
fun map f [] = raise SAME |
40 |
| map f (x :: xs) = (f x :: commit (map f) xs handle SAME => x :: map f xs); |
|
32015
7101feb5247e
Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff
changeset
|
41 |
|
32022 | 42 |
fun map_option f NONE = raise SAME |
43 |
| map_option f (SOME x) = SOME (f x); |
|
44 |
||
32015
7101feb5247e
Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff
changeset
|
45 |
end; |