| author | wenzelm | 
| Sun, 17 Aug 2008 21:11:06 +0200 | |
| changeset 27930 | 2b44df907cc2 | 
| parent 27003 | aae9b369b338 | 
| child 28268 | ac8431ecd57e | 
| permissions | -rw-r--r-- | 
| 9254 | 1 | (* Title: Pure/ML-Systems/mosml.ML | 
| 2 | ID: $Id$ | |
| 3 | Author: Lawrence C Paulson, Cambridge University Computer Laboratory | |
| 4 | Copyright 1999 University of Cambridge | |
| 5 | ||
| 17491 | 6 | Compatibility file for Moscow ML 2.01 | 
| 9254 | 7 | |
| 17491 | 8 | NOTE: saving images does not work; may run it interactively as follows: | 
| 9254 | 9 | |
| 23826 
463903573934
moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
 wenzelm parents: 
22252diff
changeset | 10 | $ cd Isabelle/src/Pure | 
| 9254 | 11 | $ isabelle RAW_ML_SYSTEM | 
| 12 | > val ml_system = "mosml"; | |
| 13 | > use "ML-Systems/mosml.ML"; | |
| 14 | > use "ROOT.ML"; | |
| 16470 | 15 | > Session.finish (); | 
| 23836 | 16 | > cd "../FOL"; | 
| 16470 | 17 | > use "ROOT.ML"; | 
| 24808 | 18 | > Session.finish (); | 
| 19 | > cd "../ZF"; | |
| 20 | > use "ROOT.ML"; | |
| 9254 | 21 | *) | 
| 22 | ||
| 23 | (** ML system related **) | |
| 24 | ||
| 24597 | 25 | val ml_system_fix_ints = false; | 
| 26 | ||
| 26474 | 27 | fun forget_structure _ = (); | 
| 28 | ||
| 16517 | 29 | load "Obj"; | 
| 9254 | 30 | load "Bool"; | 
| 31 | load "Int"; | |
| 32 | load "Real"; | |
| 33 | load "ListPair"; | |
| 34 | load "OS"; | |
| 35 | load "Process"; | |
| 36 | load "FileSys"; | |
| 21298 
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
 wenzelm parents: 
21280diff
changeset | 37 | load "IO"; | 
| 9254 | 38 | |
| 23965 
f93e509659c1
ML-Systems/exn.ML, ML-Systems/multithreading_dummy.ML;
 wenzelm parents: 
23921diff
changeset | 39 | use "ML-Systems/exn.ML"; | 
| 25732 | 40 | use "ML-Systems/universal.ML"; | 
| 24688 
a5754ca5c510
replaced interrupt_timeout by TimeLimit.timeLimit (available on SML/NJ and Poly/ML 5.1);
 wenzelm parents: 
24597diff
changeset | 41 | use "ML-Systems/multithreading.ML"; | 
| 
a5754ca5c510
replaced interrupt_timeout by TimeLimit.timeLimit (available on SML/NJ and Poly/ML 5.1);
 wenzelm parents: 
24597diff
changeset | 42 | use "ML-Systems/time_limit.ML"; | 
| 23921 
947152add153
added compatibility file for ML systems without multithreading;
 wenzelm parents: 
23836diff
changeset | 43 | |
| 
947152add153
added compatibility file for ML systems without multithreading;
 wenzelm parents: 
23836diff
changeset | 44 | |
| 16517 | 45 | (*low-level pointer equality*) | 
| 46 | local val cast : 'a -> int = Obj.magic | |
| 47 | in fun pointer_eq (x:'a, y:'a) = (cast x = cast y) end; | |
| 48 | ||
| 16469 | 49 | (*proper implementation available?*) | 
| 50 | structure IntInf = | |
| 51 | struct | |
| 24808 | 52 | fun divMod (x, y) = (x div y, x mod y); | 
| 16469 | 53 | open Int; | 
| 54 | end; | |
| 55 | ||
| 27003 | 56 | structure Substring = | 
| 57 | struct | |
| 58 | open Substring; | |
| 59 | val full = all; | |
| 60 | end; | |
| 61 | ||
| 16469 | 62 | structure Real = | 
| 63 | struct | |
| 64 | open Real; | |
| 65 | val realFloor = real o floor; | |
| 66 | end; | |
| 67 | ||
| 17491 | 68 | structure String = | 
| 69 | struct | |
| 70 | fun isSuffix s1 s2 = | |
| 71 | let val n1 = size s1 and n2 = size s2 | |
| 72 | in if n1 = n2 then s1 = s2 else n1 <= n2 andalso String.substring (s2, n2 - n1, n1) = s1 end; | |
| 73 | open String; | |
| 74 | end; | |
| 75 | ||
| 16469 | 76 | structure Time = | 
| 77 | struct | |
| 78 | open Time; | |
| 79 | fun toString t = Time.toString t | |
| 80 | handle Overflow => Real.toString (Time.toReal t); (*workaround Y2004 bug*) | |
| 81 | end; | |
| 82 | ||
| 9254 | 83 | structure OS = | 
| 16469 | 84 | struct | 
| 9254 | 85 | open OS | 
| 86 | structure Process = Process | |
| 87 | structure FileSys = FileSys | |
| 16469 | 88 | end; | 
| 16993 | 89 | |
| 22252 | 90 | exception Option = Option.Option; | 
| 91 | ||
| 92 | ||
| 9254 | 93 | (*limit the printing depth*) | 
| 24329 | 94 | local | 
| 95 | val depth = ref 10; | |
| 96 | in | |
| 97 | fun get_print_depth () = ! depth; | |
| 98 | fun print_depth n = | |
| 99 | (depth := n; | |
| 100 | Meta.printDepth := n div 2; | |
| 101 | Meta.printLength := n); | |
| 102 | end; | |
| 9254 | 103 | |
| 24290 | 104 | (*interface for toplevel pretty printers, see also Pure/pure_setup.ML*) | 
| 9254 | 105 | (*the documentation refers to an installPP but I couldn't fine it!*) | 
| 106 | fun make_pp path pprint = (); | |
| 107 | fun install_pp _ = (); | |
| 108 | ||
| 16681 | 109 | (*dummy implementation*) | 
| 110 | fun ml_prompts p1 p2 = (); | |
| 111 | ||
| 112 | (*dummy implementation*) | |
| 16660 | 113 | fun profile (n: int) f x = f x; | 
| 114 | ||
| 18384 | 115 | (*dummy implementation*) | 
| 16681 | 116 | fun exception_trace f = f (); | 
| 9254 | 117 | |
| 18384 | 118 | (*dummy implementation*) | 
| 119 | fun print x = x; | |
| 120 | ||
| 9254 | 121 | |
| 122 | (** Compiler-independent timing functions **) | |
| 123 | ||
| 124 | load "Timer"; | |
| 125 | ||
| 21298 
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
 wenzelm parents: 
21280diff
changeset | 126 | fun start_timing () = | 
| 
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
 wenzelm parents: 
21280diff
changeset | 127 | let val CPUtimer = Timer.startCPUTimer(); | 
| 
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
 wenzelm parents: 
21280diff
changeset | 128 | val time = Timer.checkCPUTimer(CPUtimer) | 
| 
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
 wenzelm parents: 
21280diff
changeset | 129 | in (CPUtimer,time) end; | 
| 
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
 wenzelm parents: 
21280diff
changeset | 130 | |
| 
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
 wenzelm parents: 
21280diff
changeset | 131 | fun end_timing (CPUtimer, {gc,sys,usr}) =
 | 
| 
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
 wenzelm parents: 
21280diff
changeset | 132 | let open Time (*...for Time.toString, Time.+ and Time.- *) | 
| 
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
 wenzelm parents: 
21280diff
changeset | 133 |       val {gc=gc2,sys=sys2,usr=usr2} = Timer.checkCPUTimer(CPUtimer)
 | 
| 
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
 wenzelm parents: 
21280diff
changeset | 134 | in "User " ^ toString (usr2-usr) ^ | 
| 
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
 wenzelm parents: 
21280diff
changeset | 135 | " GC " ^ toString (gc2-gc) ^ | 
| 
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
 wenzelm parents: 
21280diff
changeset | 136 | " All "^ toString (sys2-sys + usr2-usr + gc2-gc) ^ | 
| 
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
 wenzelm parents: 
21280diff
changeset | 137 | " secs" | 
| 
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
 wenzelm parents: 
21280diff
changeset | 138 | handle Time => "" | 
| 
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
 wenzelm parents: 
21280diff
changeset | 139 | end; | 
| 
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
 wenzelm parents: 
21280diff
changeset | 140 | |
| 
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
 wenzelm parents: 
21280diff
changeset | 141 | fun check_timer timer = | 
| 
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
 wenzelm parents: 
21280diff
changeset | 142 |   let val {sys, usr, gc} = Timer.checkCPUTimer timer
 | 
| 
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
 wenzelm parents: 
21280diff
changeset | 143 | in (sys, usr, gc) end; | 
| 
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
 wenzelm parents: 
21280diff
changeset | 144 | |
| 
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
 wenzelm parents: 
21280diff
changeset | 145 | |
| 
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
 wenzelm parents: 
21280diff
changeset | 146 | (* SML basis library fixes *) | 
| 
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
 wenzelm parents: 
21280diff
changeset | 147 | |
| 
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
 wenzelm parents: 
21280diff
changeset | 148 | structure TextIO = | 
| 
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
 wenzelm parents: 
21280diff
changeset | 149 | struct | 
| 
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
 wenzelm parents: 
21280diff
changeset | 150 |   fun canInput _ = raise IO.Io {cause = Fail "undefined", function = "canInput", name = ""};
 | 
| 
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
 wenzelm parents: 
21280diff
changeset | 151 | open TextIO; | 
| 23836 | 152 | fun inputLine is = | 
| 153 | let val s = TextIO.inputLine is | |
| 154 | in if s = "" then NONE else SOME s end; | |
| 21298 
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
 wenzelm parents: 
21280diff
changeset | 155 | end; | 
| 
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
 wenzelm parents: 
21280diff
changeset | 156 | |
| 9254 | 157 | |
| 158 | (* ML command execution *) | |
| 159 | ||
| 160 | (*Can one redirect 'use' directly to an instream?*) | |
| 26884 | 161 | fun use_text (tune: string -> string) _ _ _ _ txt = | 
| 9254 | 162 | let | 
| 163 | val tmp_name = FileSys.tmpName (); | |
| 164 | val tmp_file = TextIO.openOut tmp_name; | |
| 165 | in | |
| 24808 | 166 | TextIO.output (tmp_file, tune txt); | 
| 9254 | 167 | TextIO.closeOut tmp_file; | 
| 168 | use tmp_name; | |
| 169 | FileSys.remove tmp_name | |
| 170 | end; | |
| 171 | ||
| 26884 | 172 | fun use_file _ _ _ _ name = use name; | 
| 21770 | 173 | |
| 9254 | 174 | |
| 175 | ||
| 16993 | 176 | (** interrupts **) (*Note: may get into race conditions*) | 
| 9254 | 177 | |
| 178 | (* FIXME proper implementation available? *) | |
| 179 | ||
| 180 | exception Interrupt; | |
| 181 | ||
| 26084 
a7475459c740
replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
 wenzelm parents: 
25732diff
changeset | 182 | fun interruptible f x = f x; | 
| 
a7475459c740
replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
 wenzelm parents: 
25732diff
changeset | 183 | fun uninterruptible f x = f (fn (g: 'c -> 'd) => g) x; | 
| 9254 | 184 | |
| 185 | ||
| 186 | ||
| 187 | (** OS related **) | |
| 188 | ||
| 26223 
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
 wenzelm parents: 
26220diff
changeset | 189 | (*dummy implementation*) | 
| 
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
 wenzelm parents: 
26220diff
changeset | 190 | structure Posix = | 
| 
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
 wenzelm parents: 
26220diff
changeset | 191 | struct | 
| 
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
 wenzelm parents: 
26220diff
changeset | 192 | structure ProcEnv = | 
| 
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
 wenzelm parents: 
26220diff
changeset | 193 | struct | 
| 
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
 wenzelm parents: 
26220diff
changeset | 194 | fun getpid () = 0; | 
| 
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
 wenzelm parents: 
26220diff
changeset | 195 | end; | 
| 
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
 wenzelm parents: 
26220diff
changeset | 196 | end; | 
| 
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
 wenzelm parents: 
26220diff
changeset | 197 | |
| 
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
 wenzelm parents: 
26220diff
changeset | 198 | local | 
| 
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
 wenzelm parents: 
26220diff
changeset | 199 | |
| 
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
 wenzelm parents: 
26220diff
changeset | 200 | fun read_file name = | 
| 
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
 wenzelm parents: 
26220diff
changeset | 201 | let val is = TextIO.openIn name | 
| 26504 | 202 | in Exn.release (Exn.capture TextIO.inputAll is before TextIO.closeIn is) end; | 
| 26223 
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
 wenzelm parents: 
26220diff
changeset | 203 | |
| 
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
 wenzelm parents: 
26220diff
changeset | 204 | fun write_file name txt = | 
| 
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
 wenzelm parents: 
26220diff
changeset | 205 | let val os = TextIO.openOut name | 
| 26504 | 206 | in Exn.release (Exn.capture TextIO.output (os, txt) before TextIO.closeOut os) end; | 
| 26223 
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
 wenzelm parents: 
26220diff
changeset | 207 | |
| 
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
 wenzelm parents: 
26220diff
changeset | 208 | in | 
| 
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
 wenzelm parents: 
26220diff
changeset | 209 | |
| 
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
 wenzelm parents: 
26220diff
changeset | 210 | fun system_out script = | 
| 
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
 wenzelm parents: 
26220diff
changeset | 211 | let | 
| 
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
 wenzelm parents: 
26220diff
changeset | 212 | val script_name = OS.FileSys.tmpName (); | 
| 
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
 wenzelm parents: 
26220diff
changeset | 213 | val _ = write_file script_name script; | 
| 
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
 wenzelm parents: 
26220diff
changeset | 214 | |
| 
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
 wenzelm parents: 
26220diff
changeset | 215 | val output_name = OS.FileSys.tmpName (); | 
| 
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
 wenzelm parents: 
26220diff
changeset | 216 | |
| 
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
 wenzelm parents: 
26220diff
changeset | 217 | val status = | 
| 
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
 wenzelm parents: 
26220diff
changeset | 218 |       OS.Process.system ("perl -w \"$ISABELLE_HOME/lib/scripts/system.pl\" nogroup " ^
 | 
| 
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
 wenzelm parents: 
26220diff
changeset | 219 | script_name ^ " /dev/null " ^ output_name); | 
| 
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
 wenzelm parents: 
26220diff
changeset | 220 | val rc = if status = OS.Process.success then 0 else 1; | 
| 
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
 wenzelm parents: 
26220diff
changeset | 221 | |
| 
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
 wenzelm parents: 
26220diff
changeset | 222 | val output = read_file output_name handle IO.Io _ => ""; | 
| 
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
 wenzelm parents: 
26220diff
changeset | 223 | val _ = OS.FileSys.remove script_name handle OS.SysErr _ => (); | 
| 
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
 wenzelm parents: 
26220diff
changeset | 224 | val _ = OS.FileSys.remove output_name handle OS.SysErr _ => (); | 
| 
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
 wenzelm parents: 
26220diff
changeset | 225 | in (output, rc) end; | 
| 
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
 wenzelm parents: 
26220diff
changeset | 226 | |
| 
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
 wenzelm parents: 
26220diff
changeset | 227 | end; | 
| 
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
 wenzelm parents: 
26220diff
changeset | 228 | |
| 23826 
463903573934
moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
 wenzelm parents: 
22252diff
changeset | 229 | val cd = OS.FileSys.chDir; | 
| 
463903573934
moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
 wenzelm parents: 
22252diff
changeset | 230 | val pwd = OS.FileSys.getDir; | 
| 
463903573934
moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
 wenzelm parents: 
22252diff
changeset | 231 | |
| 17824 | 232 | val string_of_pid = Int.toString; | 
| 233 | ||
| 9254 | 234 | fun getenv var = | 
| 235 | (case Process.getEnv var of | |
| 236 | NONE => "" | |
| 237 | | SOME txt => txt); |