src/Pure/General/same.ML
author wenzelm
Sun, 17 Dec 2023 15:09:55 +0100
changeset 79269 2c65d3356ef9
parent 79131 cd17a90523d4
child 79272 899f37f6d218
permissions -rw-r--r--
proper scope of cache (amending 61af3e917597);
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
e8fbfa84c23f added same;
wenzelm
parents: 32022
diff changeset
    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
2933e71f4e09 clarified signature;
wenzelm
parents: 78712
diff changeset
    15
  val commit_id: 'a operation -> 'a -> 'a * bool
79131
cd17a90523d4 more operations;
wenzelm
parents: 79129
diff changeset
    16
  val catch: ('a, 'b) function -> 'a -> 'b option
32015
7101feb5247e Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff changeset
    17
  val function: ('a -> 'b option) -> ('a, 'b) function
32017
e91a3acf8383 added map;
wenzelm
parents: 32015
diff changeset
    18
  val map: 'a operation -> 'a list operation
32022
c2f4ee07647f added map_option;
wenzelm
parents: 32017
diff changeset
    19
  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
    20
end;
7101feb5247e Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff changeset
    21
7101feb5247e Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff changeset
    22
structure Same: SAME =
7101feb5247e Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff changeset
    23
struct
7101feb5247e Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff changeset
    24
7101feb5247e Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff changeset
    25
exception SAME;
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
type ('a, 'b) function = 'a -> 'b;
7101feb5247e Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff changeset
    28
type 'a operation = ('a, 'a) function;
7101feb5247e Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff changeset
    29
32025
e8fbfa84c23f added same;
wenzelm
parents: 32022
diff changeset
    30
fun same _ = raise SAME;
32015
7101feb5247e Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff changeset
    31
fun commit f x = f x handle SAME => x;
79129
2933e71f4e09 clarified signature;
wenzelm
parents: 78712
diff changeset
    32
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
    33
79131
cd17a90523d4 more operations;
wenzelm
parents: 79129
diff changeset
    34
fun catch f x = SOME (f x) handle SAME => NONE;
cd17a90523d4 more operations;
wenzelm
parents: 79129
diff changeset
    35
32015
7101feb5247e Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff changeset
    36
fun function f x =
7101feb5247e Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff changeset
    37
  (case f x of
7101feb5247e Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff changeset
    38
    NONE => raise SAME
7101feb5247e Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff changeset
    39
  | SOME y => y);
7101feb5247e Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff changeset
    40
32017
e91a3acf8383 added map;
wenzelm
parents: 32015
diff changeset
    41
fun map f [] = raise SAME
e91a3acf8383 added map;
wenzelm
parents: 32015
diff changeset
    42
  | 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
    43
32022
c2f4ee07647f added map_option;
wenzelm
parents: 32017
diff changeset
    44
fun map_option f NONE = raise SAME
c2f4ee07647f added map_option;
wenzelm
parents: 32017
diff changeset
    45
  | map_option f (SOME x) = SOME (f x);
c2f4ee07647f added map_option;
wenzelm
parents: 32017
diff changeset
    46
32015
7101feb5247e Support for copy-avoiding functions on pure values, at the cost of readability.
wenzelm
parents:
diff changeset
    47
end;