src/Pure/General/basics.ML
author wenzelm
Thu Jul 19 23:18:48 2007 +0200 (2007-07-19)
changeset 23863 8f3099589cfa
parent 23559 0de527730294
child 28443 de653f1ad78b
permissions -rw-r--r--
tuned signature;
wenzelm@21394
     1
(*  Title:      Pure/General/basics.ML
wenzelm@21394
     2
    ID:         $Id$
wenzelm@21394
     3
    Author:     Florian Haftmann and Makarius, TU Muenchen
wenzelm@21394
     4
wenzelm@21394
     5
Fundamental concepts.
wenzelm@21394
     6
*)
wenzelm@21394
     7
wenzelm@21394
     8
infix 1 |> |-> |>> ||> ||>>
wenzelm@21394
     9
infix 1 #> #-> #>> ##> ##>>
wenzelm@21394
    10
wenzelm@21394
    11
signature BASICS =
wenzelm@21394
    12
sig
wenzelm@21394
    13
  (*functions*)
wenzelm@21394
    14
  val |> : 'a * ('a -> 'b) -> 'b
wenzelm@21394
    15
  val |-> : ('c * 'a) * ('c -> 'a -> 'b) -> 'b
wenzelm@21394
    16
  val |>> : ('a * 'c) * ('a -> 'b) -> 'b * 'c
wenzelm@21394
    17
  val ||> : ('c * 'a) * ('a -> 'b) -> 'c * 'b
wenzelm@21394
    18
  val ||>> : ('c * 'a) * ('a -> 'd * 'b) -> ('c * 'd) * 'b
wenzelm@21394
    19
  val #> : ('a -> 'b) * ('b -> 'c) -> 'a -> 'c
wenzelm@21394
    20
  val #-> : ('a -> 'c * 'b) * ('c -> 'b -> 'd) -> 'a -> 'd
wenzelm@21394
    21
  val #>> : ('a -> 'c * 'b) * ('c -> 'd) -> 'a -> 'd * 'b
wenzelm@21394
    22
  val ##> : ('a -> 'c * 'b) * ('b -> 'd) -> 'a -> 'c * 'd
wenzelm@21394
    23
  val ##>> : ('a -> 'c * 'b) * ('b -> 'e * 'd) -> 'a -> ('c * 'e) * 'd
wenzelm@21394
    24
  val ` : ('b -> 'a) -> 'b -> 'a * 'b
wenzelm@21394
    25
  val tap: ('b -> 'a) -> 'b -> 'b
wenzelm@21394
    26
wenzelm@21394
    27
  (*options*)
wenzelm@21394
    28
  val is_some: 'a option -> bool
wenzelm@21394
    29
  val is_none: 'a option -> bool
wenzelm@21394
    30
  val the: 'a option -> 'a
wenzelm@21394
    31
  val these: 'a list option -> 'a list
wenzelm@21394
    32
  val the_list: 'a option -> 'a list
wenzelm@21394
    33
  val the_default: 'a -> 'a option -> 'a
wenzelm@21394
    34
  val perhaps: ('a -> 'a option) -> 'a -> 'a
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@21394
    94
wenzelm@21394
    95
(* partiality *)
wenzelm@21394
    96
wenzelm@21394
    97
fun try f x = SOME (f x)
wenzelm@21394
    98
  handle Interrupt => raise Interrupt | _ => NONE;
wenzelm@21394
    99
wenzelm@21394
   100
fun can f x = is_some (try f x);
wenzelm@21394
   101
wenzelm@21394
   102
wenzelm@21394
   103
(* lists *)
wenzelm@21394
   104
wenzelm@21394
   105
fun cons x xs = x :: xs;
wenzelm@21394
   106
wenzelm@21394
   107
fun append xs ys = xs @ ys;
wenzelm@21394
   108
wenzelm@23225
   109
fun fold _ [] y = y
wenzelm@23225
   110
  | fold f (x :: xs) y = fold f xs (f x y);
wenzelm@21394
   111
wenzelm@23225
   112
fun fold_rev _ [] y = y
wenzelm@23225
   113
  | fold_rev f (x :: xs) y = f x (fold_rev f xs y);
wenzelm@21394
   114
wenzelm@23225
   115
fun fold_map _ [] y = ([], y)
wenzelm@23225
   116
  | fold_map f (x :: xs) y =
wenzelm@23225
   117
      let
wenzelm@23225
   118
        val (x', y') = f x y;
wenzelm@23225
   119
        val (xs', y'') = fold_map f xs y';
wenzelm@23225
   120
      in (x' :: xs', y'') end;
wenzelm@21394
   121
wenzelm@21394
   122
end;
wenzelm@21394
   123
wenzelm@21394
   124
open Basics;