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