| author | wenzelm |
| Wed, 01 May 2024 20:26:20 +0200 | |
| changeset 80166 | 825f35bae74b |
| parent 79400 | 0824ca1f1da0 |
| child 80602 | 7aa14d4567fe |
| 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 |
| 79129 | 15 |
val commit_id: 'a operation -> 'a -> 'a * bool |
| 79131 | 16 |
val catch: ('a, 'b) function -> 'a -> 'b option
|
|
79272
899f37f6d218
proper beta_norm after instantiation (amending 90c5aadcc4b2);
wenzelm
parents:
79131
diff
changeset
|
17 |
val compose: 'a operation -> 'a operation -> 'a operation |
|
32015
7101feb5247e
Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff
changeset
|
18 |
val function: ('a -> 'b option) -> ('a, 'b) function
|
| 79400 | 19 |
val function_eq: ('a * 'b -> bool) -> ('a -> 'b) -> ('a, 'b) function
|
| 32017 | 20 |
val map: 'a operation -> 'a list operation |
| 32022 | 21 |
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
|
22 |
end; |
|
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 |
structure Same: SAME = |
|
7101feb5247e
Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff
changeset
|
25 |
struct |
|
7101feb5247e
Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff
changeset
|
26 |
|
|
7101feb5247e
Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff
changeset
|
27 |
exception SAME; |
|
7101feb5247e
Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff
changeset
|
28 |
|
|
7101feb5247e
Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff
changeset
|
29 |
type ('a, 'b) function = 'a -> 'b;
|
|
7101feb5247e
Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff
changeset
|
30 |
type 'a operation = ('a, 'a) function;
|
|
7101feb5247e
Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff
changeset
|
31 |
|
| 32025 | 32 |
fun same _ = raise SAME; |
|
32015
7101feb5247e
Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff
changeset
|
33 |
fun commit f x = f x handle SAME => x; |
| 79129 | 34 |
fun commit_id f x = (f x, false) handle SAME => (x, true); |
|
32015
7101feb5247e
Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff
changeset
|
35 |
|
| 79131 | 36 |
fun catch f x = SOME (f x) handle SAME => NONE; |
37 |
||
|
79272
899f37f6d218
proper beta_norm after instantiation (amending 90c5aadcc4b2);
wenzelm
parents:
79131
diff
changeset
|
38 |
fun compose g f x = |
|
899f37f6d218
proper beta_norm after instantiation (amending 90c5aadcc4b2);
wenzelm
parents:
79131
diff
changeset
|
39 |
(case catch f x of |
|
899f37f6d218
proper beta_norm after instantiation (amending 90c5aadcc4b2);
wenzelm
parents:
79131
diff
changeset
|
40 |
NONE => g x |
|
899f37f6d218
proper beta_norm after instantiation (amending 90c5aadcc4b2);
wenzelm
parents:
79131
diff
changeset
|
41 |
| SOME y => commit g y); |
|
899f37f6d218
proper beta_norm after instantiation (amending 90c5aadcc4b2);
wenzelm
parents:
79131
diff
changeset
|
42 |
|
|
32015
7101feb5247e
Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff
changeset
|
43 |
fun function f x = |
|
7101feb5247e
Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff
changeset
|
44 |
(case f x of |
|
7101feb5247e
Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff
changeset
|
45 |
NONE => raise SAME |
|
7101feb5247e
Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff
changeset
|
46 |
| SOME y => y); |
|
7101feb5247e
Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff
changeset
|
47 |
|
| 79400 | 48 |
fun function_eq eq f x = |
49 |
let val y = f x |
|
50 |
in if eq (x, y) then raise SAME else y end; |
|
51 |
||
| 32017 | 52 |
fun map f [] = raise SAME |
53 |
| 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
|
54 |
|
| 32022 | 55 |
fun map_option f NONE = raise SAME |
56 |
| map_option f (SOME x) = SOME (f x); |
|
57 |
||
|
32015
7101feb5247e
Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff
changeset
|
58 |
end; |