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