src/Pure/General/basics.ML
author wenzelm
Fri Nov 25 18:37:14 2011 +0100 (2011-11-25 ago)
changeset 45633 2cb7e34f6096
parent 41493 f05976d69141
child 51930 52fd62618631
permissions -rw-r--r--
retain stderr and include it in syslog, which is buffered and thus increases the chance that users see remains from crashes etc.;
wenzelm@21394
     1
(*  Title:      Pure/General/basics.ML
wenzelm@21394
     2
    Author:     Florian Haftmann and Makarius, TU Muenchen
wenzelm@21394
     3
wenzelm@21394
     4
Fundamental concepts.
wenzelm@21394
     5
*)
wenzelm@21394
     6
wenzelm@21394
     7
infix 1 |> |-> |>> ||> ||>>
wenzelm@21394
     8
infix 1 #> #-> #>> ##> ##>>
wenzelm@21394
     9
wenzelm@21394
    10
signature BASICS =
wenzelm@21394
    11
sig
wenzelm@21394
    12
  (*functions*)
wenzelm@21394
    13
  val |> : 'a * ('a -> 'b) -> 'b
wenzelm@21394
    14
  val |-> : ('c * 'a) * ('c -> 'a -> 'b) -> 'b
wenzelm@21394
    15
  val |>> : ('a * 'c) * ('a -> 'b) -> 'b * 'c
wenzelm@21394
    16
  val ||> : ('c * 'a) * ('a -> 'b) -> 'c * 'b
wenzelm@21394
    17
  val ||>> : ('c * 'a) * ('a -> 'd * 'b) -> ('c * 'd) * 'b
wenzelm@21394
    18
  val #> : ('a -> 'b) * ('b -> 'c) -> 'a -> 'c
wenzelm@21394
    19
  val #-> : ('a -> 'c * 'b) * ('c -> 'b -> 'd) -> 'a -> 'd
wenzelm@21394
    20
  val #>> : ('a -> 'c * 'b) * ('c -> 'd) -> 'a -> 'd * 'b
wenzelm@21394
    21
  val ##> : ('a -> 'c * 'b) * ('b -> 'd) -> 'a -> 'c * 'd
wenzelm@21394
    22
  val ##>> : ('a -> 'c * 'b) * ('b -> 'e * 'd) -> 'a -> ('c * 'e) * 'd
wenzelm@21394
    23
  val ` : ('b -> 'a) -> 'b -> 'a * 'b
wenzelm@21394
    24
  val tap: ('b -> 'a) -> 'b -> 'b
wenzelm@21394
    25
wenzelm@21394
    26
  (*options*)
wenzelm@21394
    27
  val is_some: 'a option -> bool
wenzelm@21394
    28
  val is_none: 'a option -> bool
wenzelm@21394
    29
  val the: 'a option -> 'a
wenzelm@21394
    30
  val these: 'a list option -> 'a list
wenzelm@21394
    31
  val the_list: 'a option -> 'a list
wenzelm@21394
    32
  val the_default: 'a -> 'a option -> 'a
wenzelm@21394
    33
  val perhaps: ('a -> 'a option) -> 'a -> 'a
wenzelm@41493
    34
  val merge_options: 'a option * 'a option -> 'a option
wenzelm@21394
    35
wenzelm@21394
    36
  (*partiality*)
wenzelm@21394
    37
  val try: ('a -> 'b) -> 'a -> 'b option
wenzelm@21394
    38
  val can: ('a -> 'b) -> 'a -> bool
wenzelm@21394
    39
wenzelm@21394
    40
  (*lists*)
wenzelm@21394
    41
  val cons: 'a -> 'a list -> 'a list
wenzelm@21394
    42
  val append: 'a list -> 'a list -> 'a list
wenzelm@21394
    43
  val fold: ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b
wenzelm@21394
    44
  val fold_rev: ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b
wenzelm@21394
    45
  val fold_map: ('a -> 'b -> 'c * 'b) -> 'a list -> 'b -> 'c list * 'b
wenzelm@21394
    46
end;
wenzelm@21394
    47
wenzelm@21394
    48
structure Basics: BASICS =
wenzelm@21394
    49
struct
wenzelm@21394
    50
wenzelm@21394
    51
(* functions *)
wenzelm@21394
    52
wenzelm@21394
    53
(*application and structured results*)
wenzelm@21394
    54
fun x |> f = f x;
wenzelm@21394
    55
fun (x, y) |-> f = f x y;
wenzelm@21394
    56
fun (x, y) |>> f = (f x, y);
wenzelm@21394
    57
fun (x, y) ||> f = (x, f y);
wenzelm@21394
    58
fun (x, y) ||>> f = let val (z, y') = f y in ((x, z), y') end;
wenzelm@21394
    59
wenzelm@21394
    60
(*composition and structured results*)
wenzelm@21394
    61
fun (f #> g) x   = x |> f |> g;
wenzelm@21394
    62
fun (f #-> g) x  = x |> f |-> g;
wenzelm@21394
    63
fun (f #>> g) x  = x |> f |>> g;
wenzelm@21394
    64
fun (f ##> g) x  = x |> f ||> g;
wenzelm@21394
    65
fun (f ##>> g) x = x |> f ||>> g;
wenzelm@21394
    66
wenzelm@21394
    67
(*result views*)
wenzelm@21394
    68
fun `f = fn x => (f x, x);
wenzelm@21394
    69
fun tap f = fn x => (f x; x);
wenzelm@21394
    70
wenzelm@21394
    71
wenzelm@21394
    72
(* options *)
wenzelm@21394
    73
wenzelm@21394
    74
fun is_some (SOME _) = true
wenzelm@21394
    75
  | is_some NONE = false;
wenzelm@21394
    76
wenzelm@21394
    77
fun is_none (SOME _) = false
wenzelm@21394
    78
  | is_none NONE = true;
wenzelm@21394
    79
wenzelm@21394
    80
fun the (SOME x) = x
wenzelm@21394
    81
  | the NONE = raise Option;
wenzelm@21394
    82
wenzelm@21394
    83
fun these (SOME x) = x
wenzelm@23559
    84
  | these NONE = [];
wenzelm@21394
    85
wenzelm@21394
    86
fun the_list (SOME x) = [x]
wenzelm@23559
    87
  | the_list NONE = []
wenzelm@21394
    88
wenzelm@21394
    89
fun the_default x (SOME y) = y
wenzelm@23559
    90
  | the_default x NONE = x;
wenzelm@21394
    91
wenzelm@21394
    92
fun perhaps f x = the_default x (f x);
wenzelm@21394
    93
wenzelm@41493
    94
fun merge_options (x, y) = if is_some x then x else y;
wenzelm@41493
    95
wenzelm@21394
    96
wenzelm@21394
    97
(* partiality *)
wenzelm@21394
    98
wenzelm@21394
    99
fun try f x = SOME (f x)
wenzelm@39232
   100
  handle exn => if Exn.is_interrupt exn then reraise exn else NONE;
wenzelm@21394
   101
wenzelm@21394
   102
fun can f x = is_some (try f x);
wenzelm@21394
   103
wenzelm@21394
   104
wenzelm@21394
   105
(* lists *)
wenzelm@21394
   106
wenzelm@21394
   107
fun cons x xs = x :: xs;
wenzelm@21394
   108
wenzelm@21394
   109
fun append xs ys = xs @ ys;
wenzelm@21394
   110
wenzelm@23225
   111
fun fold _ [] y = y
wenzelm@23225
   112
  | fold f (x :: xs) y = fold f xs (f x y);
wenzelm@21394
   113
wenzelm@23225
   114
fun fold_rev _ [] y = y
wenzelm@23225
   115
  | fold_rev f (x :: xs) y = f x (fold_rev f xs y);
wenzelm@21394
   116
wenzelm@23225
   117
fun fold_map _ [] y = ([], y)
wenzelm@23225
   118
  | fold_map f (x :: xs) y =
wenzelm@23225
   119
      let
wenzelm@23225
   120
        val (x', y') = f x y;
wenzelm@23225
   121
        val (xs', y'') = fold_map f xs y';
wenzelm@23225
   122
      in (x' :: xs', y'') end;
wenzelm@21394
   123
wenzelm@21394
   124
end;
wenzelm@21394
   125
wenzelm@21394
   126
open Basics;