src/Pure/basis.ML
author paulson
Fri May 30 15:14:59 1997 +0200 (1997-05-30)
changeset 3365 86c0d1988622
parent 3244 71b760618f30
child 5021 235f8508d440
permissions -rw-r--r--
flushOut ensures that no recent error message are lost (not certain this is
necessary)
     1 (*  Title:      Pure/basis.ML
     2     ID:         $Id$
     3     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
     4     Copyright   1993  University of Cambridge
     5 
     6 Basis Library emulation.
     7 
     8 Needed for Poly/ML and Standard ML of New Jersey version 0.93 to 1.08.
     9 
    10 Full compatibility cannot be obtained using a file: what about char constants?
    11 *)
    12 
    13 exception Subscript;
    14 
    15 structure Bool =
    16   struct
    17   fun toString true  = "true"
    18     | toString false = "false"
    19   end;
    20 
    21 
    22 structure Option =
    23   struct
    24   exception Option
    25 
    26   datatype 'a option = NONE | SOME of 'a
    27 
    28   fun getOpt (SOME v, _) = v
    29     | getOpt (NONE,   a) = a
    30 
    31   fun isSome (SOME _) = true 
    32     | isSome NONE     = false
    33 
    34   fun valOf (SOME v) = v
    35     | valOf NONE     = raise Option
    36   end;
    37 
    38 
    39 structure Int =
    40   struct
    41   fun toString (i: int) = makestring i;
    42   fun max (x, y) = if x < y then y else x : int;
    43   fun min (x, y) = if x < y then x else y : int;
    44   end;
    45 
    46 
    47 structure List =
    48   struct
    49   exception Empty
    50 
    51   fun last []      = raise Empty
    52     | last [x]     = x
    53     | last (x::xs) = last xs;
    54 
    55   fun nth (xs, n) =
    56       let fun h []      _ = raise Subscript
    57 	    | h (x::xs) n = if n=0 then x else h xs (n-1)
    58       in if n<0 then raise Subscript else h xs n end;
    59 
    60   fun drop (xs, n) =
    61       let fun h xs      0 = xs
    62 	    | h []      n = raise Subscript
    63 	    | h (x::xs) n = h xs (n-1)
    64       in if n<0 then raise Subscript else h xs n end;
    65 
    66   fun take (xs, n) =
    67       let fun h xs      0 = []
    68 	    | h []      n = raise Subscript
    69 	    | h (x::xs) n = x :: h xs (n-1)
    70       in if n<0 then raise Subscript else h xs n end;
    71 
    72   fun concat []      = []
    73     | concat (l::ls) = l @ concat ls;
    74 
    75   fun mapPartial f []      = []
    76     | mapPartial f (x::xs) = 
    77          (case f x of Option.NONE   => mapPartial f xs
    78                     | Option.SOME y => y :: mapPartial f xs);
    79 
    80   fun find _ []        = Option.NONE
    81     | find p (x :: xs) = if p x then Option.SOME x else find p xs;
    82 
    83 
    84   (*copy the list preserving elements that satisfy the predicate*)
    85   fun filter p [] = []
    86     | filter p (x :: xs) = if p x then x :: filter p xs else filter p xs;
    87 
    88   (*Partition list into elements that satisfy predicate and those that don't.
    89     Preserves order of elements in both lists.*)
    90   fun partition (p: 'a->bool) (ys: 'a list) : ('a list * 'a list) =
    91       let fun part ([], answer) = answer
    92 	    | part (x::xs, (ys, ns)) = if p(x)
    93 	      then  part (xs, (x::ys, ns))
    94 	      else  part (xs, (ys, x::ns))
    95       in  part (rev ys, ([], []))  end;
    96 
    97   end;
    98 
    99 
   100 structure ListPair =
   101   struct
   102   fun zip ([], [])      = []
   103     | zip (x::xs,y::ys) = (x,y) :: zip(xs,ys);
   104 
   105   fun unzip [] = ([],[])
   106     | unzip((x,y)::pairs) =
   107 	  let val (xs,ys) = unzip pairs
   108 	  in  (x::xs, y::ys)  end;
   109 
   110   fun map f ([], [])      = []
   111     | map f (x::xs,y::ys) = f(x,y) :: map f (xs,ys);
   112 
   113   fun exists p =
   114     let fun boolf ([], [])      = false
   115 	  | boolf (x::xs,y::ys) = p(x,y) orelse boolf (xs,ys)
   116     in boolf end;
   117 
   118   fun all p =
   119     let fun boolf ([], [])      = true
   120 	  | boolf (x::xs,y::ys) = p(x,y) andalso boolf (xs,ys)
   121     in boolf end;
   122   end;
   123 
   124 
   125 structure TextIO =
   126   struct
   127   type instream = instream
   128   and  outstream = outstream
   129   exception Io of {name: string, function: string, cause: exn}
   130   val stdIn 	= std_in
   131   val stdOut 	= std_out
   132   val openIn 	= open_in
   133   val openAppend = open_append
   134   val openOut 	= open_out
   135   val closeIn 	= close_in
   136   val closeOut 	= close_out
   137   val inputN 	= input
   138   val inputAll  = fn is => inputN (is, 999999)
   139   val inputLine = input_line
   140   val endOfStream = end_of_stream
   141   val output 	= output
   142   val flushOut 	= flush_out
   143   end;
   144 
   145 
   146 fun print s = (output (std_out, s); flush_out std_out);