src/Pure/basis.ML
author wenzelm
Wed Jun 10 11:57:01 1998 +0200 (1998-06-10)
changeset 5021 235f8508d440
parent 3244 71b760618f30
child 5208 cea0adbc7276
permissions -rw-r--r--
added exnMessage;
wenzelm@2402
     1
(*  Title:      Pure/basis.ML
paulson@2217
     2
    ID:         $Id$
paulson@2217
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
paulson@2217
     4
    Copyright   1993  University of Cambridge
paulson@2217
     5
wenzelm@5021
     6
Basis Library emulation.  Needed for Poly/ML and Standard ML of New
wenzelm@5021
     7
Jersey version 0.93 to 1.08.  Full compatibility cannot be obtained
wenzelm@5021
     8
using a file: what about char constants?
paulson@2217
     9
*)
paulson@2217
    10
paulson@2265
    11
exception Subscript;
paulson@2265
    12
paulson@2230
    13
structure Bool =
paulson@2230
    14
  struct
paulson@2230
    15
  fun toString true  = "true"
paulson@2230
    16
    | toString false = "false"
paulson@2230
    17
  end;
paulson@2230
    18
paulson@3244
    19
paulson@3244
    20
structure Option =
paulson@2862
    21
  struct
paulson@2862
    22
  exception Option
paulson@2862
    23
paulson@2862
    24
  datatype 'a option = NONE | SOME of 'a
paulson@2862
    25
paulson@2862
    26
  fun getOpt (SOME v, _) = v
paulson@2862
    27
    | getOpt (NONE,   a) = a
paulson@2862
    28
paulson@2862
    29
  fun isSome (SOME _) = true 
paulson@2862
    30
    | isSome NONE     = false
paulson@2862
    31
paulson@2862
    32
  fun valOf (SOME v) = v
paulson@2862
    33
    | valOf NONE     = raise Option
paulson@2862
    34
  end;
paulson@2862
    35
paulson@2862
    36
paulson@2217
    37
structure Int =
paulson@2217
    38
  struct
paulson@2230
    39
  fun toString (i: int) = makestring i;
paulson@2217
    40
  fun max (x, y) = if x < y then y else x : int;
paulson@2217
    41
  fun min (x, y) = if x < y then x else y : int;
paulson@2217
    42
  end;
paulson@2217
    43
paulson@2265
    44
paulson@2265
    45
structure List =
paulson@2265
    46
  struct
paulson@2265
    47
  exception Empty
paulson@2265
    48
paulson@2265
    49
  fun last []      = raise Empty
paulson@2265
    50
    | last [x]     = x
paulson@2265
    51
    | last (x::xs) = last xs;
paulson@2265
    52
paulson@2265
    53
  fun nth (xs, n) =
paulson@2265
    54
      let fun h []      _ = raise Subscript
paulson@2265
    55
	    | h (x::xs) n = if n=0 then x else h xs (n-1)
paulson@2265
    56
      in if n<0 then raise Subscript else h xs n end;
paulson@2265
    57
paulson@2265
    58
  fun drop (xs, n) =
paulson@2265
    59
      let fun h xs      0 = xs
paulson@2265
    60
	    | h []      n = raise Subscript
paulson@2265
    61
	    | h (x::xs) n = h xs (n-1)
paulson@2265
    62
      in if n<0 then raise Subscript else h xs n end;
paulson@2265
    63
paulson@2265
    64
  fun take (xs, n) =
paulson@2265
    65
      let fun h xs      0 = []
paulson@2265
    66
	    | h []      n = raise Subscript
paulson@2265
    67
	    | h (x::xs) n = x :: h xs (n-1)
paulson@2265
    68
      in if n<0 then raise Subscript else h xs n end;
paulson@2265
    69
paulson@2265
    70
  fun concat []      = []
paulson@2265
    71
    | concat (l::ls) = l @ concat ls;
paulson@2862
    72
paulson@2862
    73
  fun mapPartial f []      = []
paulson@2862
    74
    | mapPartial f (x::xs) = 
paulson@3244
    75
         (case f x of Option.NONE   => mapPartial f xs
paulson@3244
    76
                    | Option.SOME y => y :: mapPartial f xs);
paulson@2884
    77
paulson@3244
    78
  fun find _ []        = Option.NONE
paulson@3244
    79
    | find p (x :: xs) = if p x then Option.SOME x else find p xs;
paulson@2884
    80
paulson@2884
    81
paulson@2884
    82
  (*copy the list preserving elements that satisfy the predicate*)
paulson@2884
    83
  fun filter p [] = []
paulson@2884
    84
    | filter p (x :: xs) = if p x then x :: filter p xs else filter p xs;
paulson@2884
    85
paulson@2884
    86
  (*Partition list into elements that satisfy predicate and those that don't.
paulson@2884
    87
    Preserves order of elements in both lists.*)
paulson@2884
    88
  fun partition (p: 'a->bool) (ys: 'a list) : ('a list * 'a list) =
paulson@2884
    89
      let fun part ([], answer) = answer
paulson@2884
    90
	    | part (x::xs, (ys, ns)) = if p(x)
paulson@2884
    91
	      then  part (xs, (x::ys, ns))
paulson@2884
    92
	      else  part (xs, (ys, x::ns))
paulson@2884
    93
      in  part (rev ys, ([], []))  end;
paulson@2884
    94
paulson@2265
    95
  end;
paulson@2265
    96
paulson@2265
    97
paulson@2265
    98
structure ListPair =
paulson@2265
    99
  struct
paulson@2265
   100
  fun zip ([], [])      = []
paulson@2265
   101
    | zip (x::xs,y::ys) = (x,y) :: zip(xs,ys);
paulson@2265
   102
paulson@2265
   103
  fun unzip [] = ([],[])
paulson@2265
   104
    | unzip((x,y)::pairs) =
paulson@2265
   105
	  let val (xs,ys) = unzip pairs
paulson@2265
   106
	  in  (x::xs, y::ys)  end;
paulson@2265
   107
paulson@2265
   108
  fun map f ([], [])      = []
paulson@2265
   109
    | map f (x::xs,y::ys) = f(x,y) :: map f (xs,ys);
paulson@2265
   110
paulson@2884
   111
  fun exists p =
paulson@2265
   112
    let fun boolf ([], [])      = false
paulson@2884
   113
	  | boolf (x::xs,y::ys) = p(x,y) orelse boolf (xs,ys)
paulson@2265
   114
    in boolf end;
paulson@2265
   115
paulson@2884
   116
  fun all p =
paulson@2265
   117
    let fun boolf ([], [])      = true
paulson@2884
   118
	  | boolf (x::xs,y::ys) = p(x,y) andalso boolf (xs,ys)
paulson@2265
   119
    in boolf end;
paulson@2265
   120
  end;
paulson@2265
   121
paulson@2265
   122
paulson@2217
   123
structure TextIO =
paulson@2217
   124
  struct
paulson@2217
   125
  type instream = instream
paulson@2217
   126
  and  outstream = outstream
paulson@2217
   127
  exception Io of {name: string, function: string, cause: exn}
paulson@2217
   128
  val stdIn 	= std_in
paulson@2217
   129
  val stdOut 	= std_out
paulson@2217
   130
  val openIn 	= open_in
paulson@2217
   131
  val openAppend = open_append
paulson@2217
   132
  val openOut 	= open_out
paulson@2217
   133
  val closeIn 	= close_in
paulson@2217
   134
  val closeOut 	= close_out
paulson@2217
   135
  val inputN 	= input
paulson@2217
   136
  val inputAll  = fn is => inputN (is, 999999)
paulson@2217
   137
  val inputLine = input_line
paulson@2217
   138
  val endOfStream = end_of_stream
paulson@2217
   139
  val output 	= output
paulson@2217
   140
  val flushOut 	= flush_out
paulson@2217
   141
  end;
paulson@2470
   142
paulson@2470
   143
paulson@2470
   144
fun print s = (output (std_out, s); flush_out std_out);
wenzelm@5021
   145
wenzelm@5021
   146
wenzelm@5021
   147
structure General =
wenzelm@5021
   148
struct
wenzelm@5021
   149
wenzelm@5021
   150
local
wenzelm@5021
   151
  fun raised name = "exception " ^ name ^ " raised";
wenzelm@5021
   152
  fun raised_msg name msg = raised name ^ ": " ^ msg;
wenzelm@5021
   153
in
wenzelm@5021
   154
  fun exnMessage Match = raised_msg "Match" "nonexhaustive match failure"
wenzelm@5021
   155
    | exnMessage Bind = raised_msg "Bind" "nonexhaustive binding failure"
wenzelm@5021
   156
    | exnMessage Interrupt = "Interrupt"
wenzelm@5021
   157
    | exnMessage (Io msg) = "I/O error: " ^ msg
wenzelm@5021
   158
    | exnMessage Neg = raised "Neg"
wenzelm@5021
   159
    | exnMessage Sum = raised "Sum"
wenzelm@5021
   160
    | exnMessage Diff = raised "Diff"
wenzelm@5021
   161
    | exnMessage Prod = raised "Prod"
wenzelm@5021
   162
    | exnMessage Quot = raised "Quot"
wenzelm@5021
   163
    | exnMessage Abs = raised "Abs"
wenzelm@5021
   164
    | exnMessage Div = raised "Div"
wenzelm@5021
   165
    | exnMessage Mod = raised "Mod"
wenzelm@5021
   166
    | exnMessage Floor = raised "Floor"
wenzelm@5021
   167
    | exnMessage Sqrt = raised "Sqrt"
wenzelm@5021
   168
    | exnMessage Exp = raised "Exp"
wenzelm@5021
   169
    | exnMessage Ln = raised "Ln"
wenzelm@5021
   170
    | exnMessage Ord = raised "Ord"
wenzelm@5021
   171
    | exnMessage Subscript = raised_msg "Subscript " "subscript out of bounds"
wenzelm@5021
   172
    | exnMessage Option.Option = raised "Option.Option"
wenzelm@5021
   173
    | exnMessage List.Empty = raised "List.Empty"
wenzelm@5021
   174
    | exnMessage (TextIO.Io {name, ...}) = raised_msg "TextIO.Io" name
wenzelm@5021
   175
    | exnMessage exn = raised "???";
wenzelm@5021
   176
end;
wenzelm@5021
   177
wenzelm@5021
   178
end;