| author | paulson | 
| Fri, 18 Sep 1998 14:39:51 +0200 | |
| changeset 5501 | a63e0c326e6c | 
| parent 5208 | cea0adbc7276 | 
| child 5643 | 983ce1421211 | 
| permissions | -rw-r--r-- | 
| 2402 | 1 | (* Title: Pure/basis.ML | 
| 2217 | 2 | ID: $Id$ | 
| 3 | Author: Lawrence C Paulson, Cambridge University Computer Laboratory | |
| 4 | Copyright 1993 University of Cambridge | |
| 5 | ||
| 5021 | 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? | |
| 2217 | 9 | *) | 
| 10 | ||
| 2265 | 11 | exception Subscript; | 
| 12 | ||
| 2230 
275a5a699ff7
Structure Bool and value Int.toString needed to replace makestring calls
 paulson parents: 
2217diff
changeset | 13 | structure Bool = | 
| 
275a5a699ff7
Structure Bool and value Int.toString needed to replace makestring calls
 paulson parents: 
2217diff
changeset | 14 | struct | 
| 
275a5a699ff7
Structure Bool and value Int.toString needed to replace makestring calls
 paulson parents: 
2217diff
changeset | 15 | fun toString true = "true" | 
| 
275a5a699ff7
Structure Bool and value Int.toString needed to replace makestring calls
 paulson parents: 
2217diff
changeset | 16 | | toString false = "false" | 
| 
275a5a699ff7
Structure Bool and value Int.toString needed to replace makestring calls
 paulson parents: 
2217diff
changeset | 17 | end; | 
| 
275a5a699ff7
Structure Bool and value Int.toString needed to replace makestring calls
 paulson parents: 
2217diff
changeset | 18 | |
| 3244 
71b760618f30
Basis library version of type "option" now resides in its own structure Option
 paulson parents: 
3047diff
changeset | 19 | |
| 
71b760618f30
Basis library version of type "option" now resides in its own structure Option
 paulson parents: 
3047diff
changeset | 20 | structure Option = | 
| 2862 | 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 | ||
| 2217 | 37 | structure Int = | 
| 38 | struct | |
| 2230 
275a5a699ff7
Structure Bool and value Int.toString needed to replace makestring calls
 paulson parents: 
2217diff
changeset | 39 | fun toString (i: int) = makestring i; | 
| 2217 | 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 | ||
| 2265 | 44 | |
| 5208 
cea0adbc7276
added Real structure (taken from SML/NJ basis lib);
 wenzelm parents: 
5021diff
changeset | 45 | structure Real = | 
| 
cea0adbc7276
added Real structure (taken from SML/NJ basis lib);
 wenzelm parents: 
5021diff
changeset | 46 | struct | 
| 
cea0adbc7276
added Real structure (taken from SML/NJ basis lib);
 wenzelm parents: 
5021diff
changeset | 47 | fun toString (x: real) = makestring x; | 
| 
cea0adbc7276
added Real structure (taken from SML/NJ basis lib);
 wenzelm parents: 
5021diff
changeset | 48 | fun max (x, y) = if x < y then y else x : real; | 
| 
cea0adbc7276
added Real structure (taken from SML/NJ basis lib);
 wenzelm parents: 
5021diff
changeset | 49 | fun min (x, y) = if x < y then x else y : real; | 
| 
cea0adbc7276
added Real structure (taken from SML/NJ basis lib);
 wenzelm parents: 
5021diff
changeset | 50 | val real = real; | 
| 
cea0adbc7276
added Real structure (taken from SML/NJ basis lib);
 wenzelm parents: 
5021diff
changeset | 51 | val floor = floor; | 
| 
cea0adbc7276
added Real structure (taken from SML/NJ basis lib);
 wenzelm parents: 
5021diff
changeset | 52 | fun ceil x = ~1 - floor (~ (x + 1.0)); | 
| 
cea0adbc7276
added Real structure (taken from SML/NJ basis lib);
 wenzelm parents: 
5021diff
changeset | 53 | fun round x = floor (x + 0.5); (*does not do round-to-nearest*) | 
| 
cea0adbc7276
added Real structure (taken from SML/NJ basis lib);
 wenzelm parents: 
5021diff
changeset | 54 | fun trunc x = if x < 0.0 then ceil x else floor x; | 
| 
cea0adbc7276
added Real structure (taken from SML/NJ basis lib);
 wenzelm parents: 
5021diff
changeset | 55 | end; | 
| 
cea0adbc7276
added Real structure (taken from SML/NJ basis lib);
 wenzelm parents: 
5021diff
changeset | 56 | |
| 
cea0adbc7276
added Real structure (taken from SML/NJ basis lib);
 wenzelm parents: 
5021diff
changeset | 57 | |
| 2265 | 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; | |
| 2862 | 85 | |
| 86 | fun mapPartial f [] = [] | |
| 87 | | mapPartial f (x::xs) = | |
| 3244 
71b760618f30
Basis library version of type "option" now resides in its own structure Option
 paulson parents: 
3047diff
changeset | 88 | (case f x of Option.NONE => mapPartial f xs | 
| 
71b760618f30
Basis library version of type "option" now resides in its own structure Option
 paulson parents: 
3047diff
changeset | 89 | | Option.SOME y => y :: mapPartial f xs); | 
| 2884 | 90 | |
| 3244 
71b760618f30
Basis library version of type "option" now resides in its own structure Option
 paulson parents: 
3047diff
changeset | 91 | fun find _ [] = Option.NONE | 
| 
71b760618f30
Basis library version of type "option" now resides in its own structure Option
 paulson parents: 
3047diff
changeset | 92 | | find p (x :: xs) = if p x then Option.SOME x else find p xs; | 
| 2884 | 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 | ||
| 2265 | 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 | ||
| 2884 | 124 | fun exists p = | 
| 2265 | 125 | let fun boolf ([], []) = false | 
| 2884 | 126 | | boolf (x::xs,y::ys) = p(x,y) orelse boolf (xs,ys) | 
| 2265 | 127 | in boolf end; | 
| 128 | ||
| 2884 | 129 | fun all p = | 
| 2265 | 130 | let fun boolf ([], []) = true | 
| 2884 | 131 | | boolf (x::xs,y::ys) = p(x,y) andalso boolf (xs,ys) | 
| 2265 | 132 | in boolf end; | 
| 133 | end; | |
| 134 | ||
| 135 | ||
| 2217 | 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; | |
| 2470 
273580d5c040
A definition of "print", unfortunately overridden by each "open PolyML"
 paulson parents: 
2402diff
changeset | 155 | |
| 
273580d5c040
A definition of "print", unfortunately overridden by each "open PolyML"
 paulson parents: 
2402diff
changeset | 156 | |
| 
273580d5c040
A definition of "print", unfortunately overridden by each "open PolyML"
 paulson parents: 
2402diff
changeset | 157 | fun print s = (output (std_out, s); flush_out std_out); | 
| 5021 | 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; |