src/Pure/General/basics.ML
author wenzelm
Thu Sep 09 17:20:27 2010 +0200 (2010-09-09 ago)
changeset 39232 69c6d3e87660
parent 31480 05937d6aafb5
child 41493 f05976d69141
permissions -rw-r--r--
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
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@21394
    34
wenzelm@21394
    35
  (*partiality*)
wenzelm@21394
    36
  val try: ('a -> 'b) -> 'a -> 'b option
wenzelm@21394
    37
  val can: ('a -> 'b) -> 'a -> bool
wenzelm@21394
    38
wenzelm@21394
    39
  (*lists*)
wenzelm@21394
    40
  val cons: 'a -> 'a list -> 'a list
wenzelm@21394
    41
  val append: 'a list -> 'a list -> 'a list
wenzelm@21394
    42
  val fold: ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b
wenzelm@21394
    43
  val fold_rev: ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b
wenzelm@21394
    44
  val fold_map: ('a -> 'b -> 'c * 'b) -> 'a list -> 'b -> 'c list * 'b
wenzelm@21394
    45
end;
wenzelm@21394
    46
wenzelm@21394
    47
structure Basics: BASICS =
wenzelm@21394
    48
struct
wenzelm@21394
    49
wenzelm@21394
    50
(* functions *)
wenzelm@21394
    51
wenzelm@21394
    52
(*application and structured results*)
wenzelm@21394
    53
fun x |> f = f x;
wenzelm@21394
    54
fun (x, y) |-> f = f x y;
wenzelm@21394
    55
fun (x, y) |>> f = (f x, y);
wenzelm@21394
    56
fun (x, y) ||> f = (x, f y);
wenzelm@21394
    57
fun (x, y) ||>> f = let val (z, y') = f y in ((x, z), y') end;
wenzelm@21394
    58
wenzelm@21394
    59
(*composition and structured results*)
wenzelm@21394
    60
fun (f #> g) x   = x |> f |> g;
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
wenzelm@21394
    66
(*result views*)
wenzelm@21394
    67
fun `f = fn x => (f x, x);
wenzelm@21394
    68
fun tap f = fn x => (f x; x);
wenzelm@21394
    69
wenzelm@21394
    70
wenzelm@21394
    71
(* options *)
wenzelm@21394
    72
wenzelm@21394
    73
fun is_some (SOME _) = true
wenzelm@21394
    74
  | is_some NONE = false;
wenzelm@21394
    75
wenzelm@21394
    76
fun is_none (SOME _) = false
wenzelm@21394
    77
  | is_none NONE = true;
wenzelm@21394
    78
wenzelm@21394
    79
fun the (SOME x) = x
wenzelm@21394
    80
  | the NONE = raise Option;
wenzelm@21394
    81
wenzelm@21394
    82
fun these (SOME x) = x
wenzelm@23559
    83
  | these NONE = [];
wenzelm@21394
    84
wenzelm@21394
    85
fun the_list (SOME x) = [x]
wenzelm@23559
    86
  | the_list NONE = []
wenzelm@21394
    87
wenzelm@21394
    88
fun the_default x (SOME y) = y
wenzelm@23559
    89
  | the_default x NONE = x;
wenzelm@21394
    90
wenzelm@21394
    91
fun perhaps f x = the_default x (f x);
wenzelm@21394
    92
wenzelm@21394
    93
wenzelm@21394
    94
(* partiality *)
wenzelm@21394
    95
wenzelm@21394
    96
fun try f x = SOME (f x)
wenzelm@39232
    97
  handle exn => if Exn.is_interrupt exn then reraise exn else NONE;
wenzelm@21394
    98
wenzelm@21394
    99
fun can f x = is_some (try f x);
wenzelm@21394
   100
wenzelm@21394
   101
wenzelm@21394
   102
(* lists *)
wenzelm@21394
   103
wenzelm@21394
   104
fun cons x xs = x :: xs;
wenzelm@21394
   105
wenzelm@21394
   106
fun append xs ys = xs @ ys;
wenzelm@21394
   107
wenzelm@23225
   108
fun fold _ [] y = y
wenzelm@23225
   109
  | fold f (x :: xs) y = fold f xs (f x y);
wenzelm@21394
   110
wenzelm@23225
   111
fun fold_rev _ [] y = y
wenzelm@23225
   112
  | fold_rev f (x :: xs) y = f x (fold_rev f xs y);
wenzelm@21394
   113
wenzelm@23225
   114
fun fold_map _ [] y = ([], y)
wenzelm@23225
   115
  | fold_map f (x :: xs) y =
wenzelm@23225
   116
      let
wenzelm@23225
   117
        val (x', y') = f x y;
wenzelm@23225
   118
        val (xs', y'') = fold_map f xs y';
wenzelm@23225
   119
      in (x' :: xs', y'') end;
wenzelm@21394
   120
wenzelm@21394
   121
end;
wenzelm@21394
   122
wenzelm@21394
   123
open Basics;