src/Pure/General/basics.ML
author wenzelm
Sat, 06 Jun 2009 21:47:02 +0200
changeset 31480 05937d6aafb5
parent 29606 fedb8be05f24
child 39232 69c6d3e87660
permissions -rw-r--r--
reraise exceptions to preserve position information;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
21394
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/General/basics.ML
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
     2
    Author:     Florian Haftmann and Makarius, TU Muenchen
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
     3
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
     4
Fundamental concepts.
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
     5
*)
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
     6
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
     7
infix 1 |> |-> |>> ||> ||>>
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
     8
infix 1 #> #-> #>> ##> ##>>
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
     9
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    10
signature BASICS =
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    11
sig
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    12
  (*functions*)
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    13
  val |> : 'a * ('a -> 'b) -> 'b
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    14
  val |-> : ('c * 'a) * ('c -> 'a -> 'b) -> 'b
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    15
  val |>> : ('a * 'c) * ('a -> 'b) -> 'b * 'c
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    16
  val ||> : ('c * 'a) * ('a -> 'b) -> 'c * 'b
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    17
  val ||>> : ('c * 'a) * ('a -> 'd * 'b) -> ('c * 'd) * 'b
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    18
  val #> : ('a -> 'b) * ('b -> 'c) -> 'a -> 'c
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    19
  val #-> : ('a -> 'c * 'b) * ('c -> 'b -> 'd) -> 'a -> 'd
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    20
  val #>> : ('a -> 'c * 'b) * ('c -> 'd) -> 'a -> 'd * 'b
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    21
  val ##> : ('a -> 'c * 'b) * ('b -> 'd) -> 'a -> 'c * 'd
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    22
  val ##>> : ('a -> 'c * 'b) * ('b -> 'e * 'd) -> 'a -> ('c * 'e) * 'd
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    23
  val ` : ('b -> 'a) -> 'b -> 'a * 'b
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    24
  val tap: ('b -> 'a) -> 'b -> 'b
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    25
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    26
  (*options*)
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    27
  val is_some: 'a option -> bool
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    28
  val is_none: 'a option -> bool
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    29
  val the: 'a option -> 'a
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    30
  val these: 'a list option -> 'a list
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    31
  val the_list: 'a option -> 'a list
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    32
  val the_default: 'a -> 'a option -> 'a
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    33
  val perhaps: ('a -> 'a option) -> 'a -> 'a
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    34
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    35
  (*partiality*)
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    36
  val try: ('a -> 'b) -> 'a -> 'b option
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    37
  val can: ('a -> 'b) -> 'a -> bool
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    38
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    39
  (*lists*)
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    40
  val cons: 'a -> 'a list -> 'a list
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    41
  val append: 'a list -> 'a list -> 'a list
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    42
  val fold: ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    43
  val fold_rev: ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    44
  val fold_map: ('a -> 'b -> 'c * 'b) -> 'a list -> 'b -> 'c list * 'b
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    45
end;
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    46
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    47
structure Basics: BASICS =
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    48
struct
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    49
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    50
(* functions *)
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    51
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    52
(*application and structured results*)
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    53
fun x |> f = f x;
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    54
fun (x, y) |-> f = f x y;
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    55
fun (x, y) |>> f = (f x, y);
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    56
fun (x, y) ||> f = (x, f y);
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    57
fun (x, y) ||>> f = let val (z, y') = f y in ((x, z), y') end;
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    58
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    59
(*composition and structured results*)
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    60
fun (f #> g) x   = x |> f |> g;
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    61
fun (f #-> g) x  = x |> f |-> g;
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    62
fun (f #>> g) x  = x |> f |>> g;
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    63
fun (f ##> g) x  = x |> f ||> g;
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    64
fun (f ##>> g) x = x |> f ||>> g;
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    65
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    66
(*result views*)
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    67
fun `f = fn x => (f x, x);
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    68
fun tap f = fn x => (f x; x);
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    69
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    70
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    71
(* options *)
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    72
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    73
fun is_some (SOME _) = true
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    74
  | is_some NONE = false;
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    75
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    76
fun is_none (SOME _) = false
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    77
  | is_none NONE = true;
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    78
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    79
fun the (SOME x) = x
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    80
  | the NONE = raise Option;
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    81
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    82
fun these (SOME x) = x
23559
wenzelm
parents: 23358
diff changeset
    83
  | these NONE = [];
21394
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    84
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    85
fun the_list (SOME x) = [x]
23559
wenzelm
parents: 23358
diff changeset
    86
  | the_list NONE = []
21394
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    87
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    88
fun the_default x (SOME y) = y
23559
wenzelm
parents: 23358
diff changeset
    89
  | the_default x NONE = x;
21394
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    90
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    91
fun perhaps f x = the_default x (f x);
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    92
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    93
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    94
(* partiality *)
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    95
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    96
fun try f x = SOME (f x)
31480
05937d6aafb5 reraise exceptions to preserve position information;
wenzelm
parents: 29606
diff changeset
    97
  handle (exn as Exn.Interrupt) => reraise exn | _ => NONE;
21394
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    98
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
    99
fun can f x = is_some (try f x);
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
   100
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
   101
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
   102
(* lists *)
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
   103
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
   104
fun cons x xs = x :: xs;
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
   105
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
   106
fun append xs ys = xs @ ys;
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
   107
23225
591af6a2f09e moved flip to library.ML;
wenzelm
parents: 22936
diff changeset
   108
fun fold _ [] y = y
591af6a2f09e moved flip to library.ML;
wenzelm
parents: 22936
diff changeset
   109
  | fold f (x :: xs) y = fold f xs (f x y);
21394
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
   110
23225
591af6a2f09e moved flip to library.ML;
wenzelm
parents: 22936
diff changeset
   111
fun fold_rev _ [] y = y
591af6a2f09e moved flip to library.ML;
wenzelm
parents: 22936
diff changeset
   112
  | fold_rev f (x :: xs) y = f x (fold_rev f xs y);
21394
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
   113
23225
591af6a2f09e moved flip to library.ML;
wenzelm
parents: 22936
diff changeset
   114
fun fold_map _ [] y = ([], y)
591af6a2f09e moved flip to library.ML;
wenzelm
parents: 22936
diff changeset
   115
  | fold_map f (x :: xs) y =
591af6a2f09e moved flip to library.ML;
wenzelm
parents: 22936
diff changeset
   116
      let
591af6a2f09e moved flip to library.ML;
wenzelm
parents: 22936
diff changeset
   117
        val (x', y') = f x y;
591af6a2f09e moved flip to library.ML;
wenzelm
parents: 22936
diff changeset
   118
        val (xs', y'') = fold_map f xs y';
591af6a2f09e moved flip to library.ML;
wenzelm
parents: 22936
diff changeset
   119
      in (x' :: xs', y'') end;
21394
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
   120
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
   121
end;
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
   122
9f20604d2b5e Fundamental concepts.
wenzelm
parents:
diff changeset
   123
open Basics;