src/Pure/basis.ML
author paulson
Wed Apr 02 11:32:48 1997 +0200 (1997-04-02)
changeset 2862 3f38cbd57d47
parent 2470 273580d5c040
child 2884 4f2a4c27f9b5
permissions -rw-r--r--
Now declares Basis Library version of type option
Also function mapPartial
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@2402
     6
Basis Library emulation.
paulson@2217
     7
wenzelm@2402
     8
Needed for Poly/ML and Standard ML of New Jersey version 0.93 to 1.08.
paulson@2217
     9
paulson@2217
    10
Full compatibility cannot be obtained using a file: what about char constants?
paulson@2217
    11
*)
paulson@2217
    12
paulson@2265
    13
exception Subscript;
paulson@2265
    14
paulson@2230
    15
structure Bool =
paulson@2230
    16
  struct
paulson@2230
    17
  fun toString true  = "true"
paulson@2230
    18
    | toString false = "false"
paulson@2230
    19
  end;
paulson@2230
    20
paulson@2862
    21
structure General =
paulson@2862
    22
  struct
paulson@2862
    23
  exception Option
paulson@2862
    24
paulson@2862
    25
  datatype 'a option = NONE | SOME of 'a
paulson@2862
    26
paulson@2862
    27
  fun getOpt (SOME v, _) = v
paulson@2862
    28
    | getOpt (NONE,   a) = a
paulson@2862
    29
paulson@2862
    30
  fun isSome (SOME _) = true 
paulson@2862
    31
    | isSome NONE     = false
paulson@2862
    32
paulson@2862
    33
  fun valOf (SOME v) = v
paulson@2862
    34
    | valOf NONE     = raise Option
paulson@2862
    35
  end;
paulson@2862
    36
paulson@2862
    37
paulson@2217
    38
structure Int =
paulson@2217
    39
  struct
paulson@2230
    40
  fun toString (i: int) = makestring i;
paulson@2217
    41
  fun max (x, y) = if x < y then y else x : int;
paulson@2217
    42
  fun min (x, y) = if x < y then x else y : int;
paulson@2217
    43
  end;
paulson@2217
    44
paulson@2265
    45
paulson@2265
    46
structure List =
paulson@2265
    47
  struct
paulson@2265
    48
  exception Empty
paulson@2265
    49
paulson@2265
    50
  fun last []      = raise Empty
paulson@2265
    51
    | last [x]     = x
paulson@2265
    52
    | last (x::xs) = last xs;
paulson@2265
    53
paulson@2265
    54
  fun nth (xs, n) =
paulson@2265
    55
      let fun h []      _ = raise Subscript
paulson@2265
    56
	    | h (x::xs) n = if n=0 then x else h xs (n-1)
paulson@2265
    57
      in if n<0 then raise Subscript else h xs n end;
paulson@2265
    58
paulson@2265
    59
  fun drop (xs, n) =
paulson@2265
    60
      let fun h xs      0 = xs
paulson@2265
    61
	    | h []      n = raise Subscript
paulson@2265
    62
	    | h (x::xs) n = h xs (n-1)
paulson@2265
    63
      in if n<0 then raise Subscript else h xs n end;
paulson@2265
    64
paulson@2265
    65
  fun take (xs, n) =
paulson@2265
    66
      let fun h xs      0 = []
paulson@2265
    67
	    | h []      n = raise Subscript
paulson@2265
    68
	    | h (x::xs) n = x :: h xs (n-1)
paulson@2265
    69
      in if n<0 then raise Subscript else h xs n end;
paulson@2265
    70
paulson@2265
    71
  fun concat []      = []
paulson@2265
    72
    | concat (l::ls) = l @ concat ls;
paulson@2862
    73
paulson@2862
    74
  fun mapPartial f []      = []
paulson@2862
    75
    | mapPartial f (x::xs) = 
paulson@2862
    76
         (case f x of General.NONE   => mapPartial f xs
paulson@2862
    77
                    | General.SOME y => y :: mapPartial f xs);
paulson@2265
    78
  end;
paulson@2265
    79
paulson@2265
    80
paulson@2265
    81
structure ListPair =
paulson@2265
    82
  struct
paulson@2265
    83
  fun zip ([], [])      = []
paulson@2265
    84
    | zip (x::xs,y::ys) = (x,y) :: zip(xs,ys);
paulson@2265
    85
paulson@2265
    86
  fun unzip [] = ([],[])
paulson@2265
    87
    | unzip((x,y)::pairs) =
paulson@2265
    88
	  let val (xs,ys) = unzip pairs
paulson@2265
    89
	  in  (x::xs, y::ys)  end;
paulson@2265
    90
paulson@2265
    91
  fun map f ([], [])      = []
paulson@2265
    92
    | map f (x::xs,y::ys) = f(x,y) :: map f (xs,ys);
paulson@2265
    93
paulson@2265
    94
  fun exists pred =
paulson@2265
    95
    let fun boolf ([], [])      = false
paulson@2265
    96
	  | boolf (x::xs,y::ys) = pred(x,y) orelse boolf (xs,ys)
paulson@2265
    97
    in boolf end;
paulson@2265
    98
paulson@2265
    99
  fun all pred =
paulson@2265
   100
    let fun boolf ([], [])      = true
paulson@2265
   101
	  | boolf (x::xs,y::ys) = pred(x,y) andalso boolf (xs,ys)
paulson@2265
   102
    in boolf end;
paulson@2265
   103
  end;
paulson@2265
   104
paulson@2265
   105
paulson@2217
   106
structure TextIO =
paulson@2217
   107
  struct
paulson@2217
   108
  type instream = instream
paulson@2217
   109
  and  outstream = outstream
paulson@2217
   110
  exception Io of {name: string, function: string, cause: exn}
paulson@2217
   111
  val stdIn 	= std_in
paulson@2217
   112
  val stdOut 	= std_out
paulson@2217
   113
  val openIn 	= open_in
paulson@2217
   114
  val openAppend = open_append
paulson@2217
   115
  val openOut 	= open_out
paulson@2217
   116
  val closeIn 	= close_in
paulson@2217
   117
  val closeOut 	= close_out
paulson@2217
   118
  val inputN 	= input
paulson@2217
   119
  val inputAll  = fn is => inputN (is, 999999)
paulson@2217
   120
  val inputLine = input_line
paulson@2217
   121
  val endOfStream = end_of_stream
paulson@2217
   122
  val output 	= output
paulson@2217
   123
  val flushOut 	= flush_out
paulson@2217
   124
  end;
paulson@2470
   125
paulson@2470
   126
paulson@2470
   127
fun print s = (output (std_out, s); flush_out std_out);