| author | wenzelm | 
| Tue, 21 Feb 2012 20:22:23 +0100 | |
| changeset 46579 | fa035a015ea8 | 
| parent 44249 | 64620f1d6f87 | 
| child 48416 | 5787e1c911d0 | 
| permissions | -rw-r--r-- | 
| 4403 | 1 | (* Title: Pure/ML-Systems/smlnj.ML | 
| 2 | ||
| 5708 | 3 | Compatibility file for Standard ML of New Jersey 110 or later. | 
| 4403 | 4 | *) | 
| 5 | ||
| 44249 
64620f1d6f87
identify parallel exceptions where they emerge first -- to achieve unique results within evaluation graph;
 wenzelm parents: 
43948diff
changeset | 6 | use "ML-Systems/proper_int.ML"; | 
| 
64620f1d6f87
identify parallel exceptions where they emerge first -- to achieve unique results within evaluation graph;
 wenzelm parents: 
43948diff
changeset | 7 | |
| 28443 | 8 | exception Interrupt; | 
| 31427 
5a07cc86675d
reraise exceptions to preserve original position (ML system specific);
 wenzelm parents: 
31321diff
changeset | 9 | fun reraise exn = raise exn; | 
| 44249 
64620f1d6f87
identify parallel exceptions where they emerge first -- to achieve unique results within evaluation graph;
 wenzelm parents: 
43948diff
changeset | 10 | fun set_exn_serial (_: int) (exn: exn) = exn; | 
| 
64620f1d6f87
identify parallel exceptions where they emerge first -- to achieve unique results within evaluation graph;
 wenzelm parents: 
43948diff
changeset | 11 | fun get_exn_serial (exn: exn) : int option = NONE; | 
| 28443 | 12 | |
| 24599 
7b0ecf9a9055
use_text/file: tune text (cf. ML_Parse.fix_ints);
 wenzelm parents: 
24329diff
changeset | 13 | use "ML-Systems/overloading_smlnj.ML"; | 
| 34136 | 14 | use "General/exn.ML"; | 
| 35014 
a725ff6ead26
explicit representation of single-assignment variables;
 wenzelm parents: 
35010diff
changeset | 15 | use "ML-Systems/single_assignment.ML"; | 
| 25732 | 16 | use "ML-Systems/universal.ML"; | 
| 28151 
61f9c918b410
explicit use of universal.ML and dummy_thread.ML;
 wenzelm parents: 
26885diff
changeset | 17 | use "ML-Systems/thread_dummy.ML"; | 
| 24688 
a5754ca5c510
replaced interrupt_timeout by TimeLimit.timeLimit (available on SML/NJ and Poly/ML 5.1);
 wenzelm parents: 
24599diff
changeset | 18 | use "ML-Systems/multithreading.ML"; | 
| 28268 
ac8431ecd57e
use_text/use_file now depend on explicit ML name space;
 wenzelm parents: 
28151diff
changeset | 19 | use "ML-Systems/ml_name_space.ML"; | 
| 30619 | 20 | use "ML-Systems/ml_pretty.ML"; | 
| 38635 
f76ad0771f67
added ML toplevel pretty-printing for tables, using dummy for anything other than Poly/ML 5.3.0 (or later);
 wenzelm parents: 
36162diff
changeset | 21 | structure PolyML = struct end; | 
| 
f76ad0771f67
added ML toplevel pretty-printing for tables, using dummy for anything other than Poly/ML 5.3.0 (or later);
 wenzelm parents: 
36162diff
changeset | 22 | use "ML-Systems/pp_dummy.ML"; | 
| 30672 
beaadd5af500
more systematic type use_context, with particular values ML_Parse.global_context and ML_Context.local_context;
 wenzelm parents: 
30626diff
changeset | 23 | use "ML-Systems/use_context.ML"; | 
| 23921 
947152add153
added compatibility file for ML systems without multithreading;
 wenzelm parents: 
23826diff
changeset | 24 | |
| 16542 | 25 | |
| 42012 
2c3fe3cbebae
structure Timing: covers former start_timing/end_timing and Output.timeit etc;
 wenzelm parents: 
41411diff
changeset | 26 | val seconds = Time.fromReal; | 
| 
2c3fe3cbebae
structure Timing: covers former start_timing/end_timing and Output.timeit etc;
 wenzelm parents: 
41411diff
changeset | 27 | |
| 16542 | 28 | (*low-level pointer equality*) | 
| 21298 
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
 wenzelm parents: 
18790diff
changeset | 29 | CM.autoload "$smlnj/init/init.cmi"; | 
| 
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
 wenzelm parents: 
18790diff
changeset | 30 | val pointer_eq = InlineT.ptreql; | 
| 16528 | 31 | |
| 41411 | 32 | fun share_common_data () = (); | 
| 33 | ||
| 16502 | 34 | |
| 4403 | 35 | (* restore old-style character / string functions *) | 
| 36 | ||
| 40627 
becf5d5187cc
renamed raw "explode" function to "raw_explode" to emphasize its meaning;
 wenzelm parents: 
40393diff
changeset | 37 | val ord = mk_int o SML90.ord; | 
| 
becf5d5187cc
renamed raw "explode" function to "raw_explode" to emphasize its meaning;
 wenzelm parents: 
40393diff
changeset | 38 | val chr = SML90.chr o dest_int; | 
| 
becf5d5187cc
renamed raw "explode" function to "raw_explode" to emphasize its meaning;
 wenzelm parents: 
40393diff
changeset | 39 | val raw_explode = SML90.explode; | 
| 10725 | 40 | val implode = SML90.implode; | 
| 4403 | 41 | |
| 42 | ||
| 43 | (* New Jersey ML parameters *) | |
| 44 | ||
| 45 | val _ = | |
| 21298 
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
 wenzelm parents: 
18790diff
changeset | 46 | (Control.Print.printLength := 1000; | 
| 
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
 wenzelm parents: 
18790diff
changeset | 47 | Control.Print.printDepth := 350; | 
| 
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
 wenzelm parents: 
18790diff
changeset | 48 | Control.Print.stringDepth := 250; | 
| 
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
 wenzelm parents: 
18790diff
changeset | 49 | Control.Print.signatures := 2; | 
| 
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
 wenzelm parents: 
18790diff
changeset | 50 | Control.MC.matchRedundantError := false); | 
| 4403 | 51 | |
| 52 | ||
| 53 | (* Poly/ML emulation *) | |
| 54 | ||
| 24599 
7b0ecf9a9055
use_text/file: tune text (cf. ML_Parse.fix_ints);
 wenzelm parents: 
24329diff
changeset | 55 | val exit = exit o dest_int; | 
| 4403 | 56 | fun quit () = exit 0; | 
| 57 | ||
| 58 | (*limit the printing depth -- divided by 2 for comparibility with Poly/ML*) | |
| 24329 | 59 | local | 
| 24599 
7b0ecf9a9055
use_text/file: tune text (cf. ML_Parse.fix_ints);
 wenzelm parents: 
24329diff
changeset | 60 | val depth = ref (10: int); | 
| 24329 | 61 | in | 
| 62 | fun get_print_depth () = ! depth; | |
| 63 | fun print_depth n = | |
| 64 | (depth := n; | |
| 65 | Control.Print.printDepth := dest_int n div 2; | |
| 66 | Control.Print.printLength := dest_int n); | |
| 67 | end; | |
| 4403 | 68 | |
| 36162 
0bd034a80a9a
added ML antiquotation @{make_string}, which produces proper pretty printed version in Poly/ML 5.3.0 or later;
 wenzelm parents: 
35014diff
changeset | 69 | val ml_make_string = "(fn _ => \"?\")"; | 
| 
0bd034a80a9a
added ML antiquotation @{make_string}, which produces proper pretty printed version in Poly/ML 5.3.0 or later;
 wenzelm parents: 
35014diff
changeset | 70 | |
| 26474 | 71 | |
| 16660 | 72 | (*prompts*) | 
| 4977 | 73 | fun ml_prompts p1 p2 = | 
| 21298 
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
 wenzelm parents: 
18790diff
changeset | 74 | (Control.primaryPrompt := p1; Control.secondaryPrompt := p2); | 
| 4977 | 75 | |
| 17511 | 76 | (*dummy implementation*) | 
| 16681 | 77 | fun profile (n: int) f x = f x; | 
| 78 | ||
| 17511 | 79 | (*dummy implementation*) | 
| 16681 | 80 | fun exception_trace f = f (); | 
| 4977 | 81 | |
| 21298 
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
 wenzelm parents: 
18790diff
changeset | 82 | |
| 4403 | 83 | (* ML command execution *) | 
| 84 | ||
| 30672 
beaadd5af500
more systematic type use_context, with particular values ML_Parse.global_context and ML_Context.local_context;
 wenzelm parents: 
30626diff
changeset | 85 | fun use_text ({tune_source, print, error, ...}: use_context) (line, name) verbose txt =
 | 
| 5090 | 86 | let | 
| 21298 
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
 wenzelm parents: 
18790diff
changeset | 87 | val ref out_orig = Control.Print.out; | 
| 5090 | 88 | |
| 89 | val out_buffer = ref ([]: string list); | |
| 90 |     val out = {say = (fn s => out_buffer := s :: ! out_buffer), flush = (fn () => ())};
 | |
| 10914 | 91 | fun output () = | 
| 7890 | 92 | let val str = implode (rev (! out_buffer)) | 
| 10914 | 93 | in String.substring (str, 0, Int.max (0, size str - 1)) end; | 
| 5090 | 94 | in | 
| 21298 
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
 wenzelm parents: 
18790diff
changeset | 95 | Control.Print.out := out; | 
| 30672 
beaadd5af500
more systematic type use_context, with particular values ML_Parse.global_context and ML_Context.local_context;
 wenzelm parents: 
30626diff
changeset | 96 | Backend.Interact.useStream (TextIO.openString (tune_source txt)) handle exn => | 
| 22144 | 97 | (Control.Print.out := out_orig; | 
| 30672 
beaadd5af500
more systematic type use_context, with particular values ML_Parse.global_context and ML_Context.local_context;
 wenzelm parents: 
30626diff
changeset | 98 | error ((if name = "" then "" else "Error in " ^ name ^ "\n") ^ output ()); raise exn); | 
| 21298 
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
 wenzelm parents: 
18790diff
changeset | 99 | Control.Print.out := out_orig; | 
| 10914 | 100 | if verbose then print (output ()) else () | 
| 5090 | 101 | end; | 
| 4403 | 102 | |
| 30672 
beaadd5af500
more systematic type use_context, with particular values ML_Parse.global_context and ML_Context.local_context;
 wenzelm parents: 
30626diff
changeset | 103 | fun use_file context verbose name = | 
| 24599 
7b0ecf9a9055
use_text/file: tune text (cf. ML_Parse.fix_ints);
 wenzelm parents: 
24329diff
changeset | 104 | let | 
| 
7b0ecf9a9055
use_text/file: tune text (cf. ML_Parse.fix_ints);
 wenzelm parents: 
24329diff
changeset | 105 | val instream = TextIO.openIn name; | 
| 26504 | 106 | val txt = Exn.release (Exn.capture TextIO.inputAll instream before TextIO.closeIn instream); | 
| 30672 
beaadd5af500
more systematic type use_context, with particular values ML_Parse.global_context and ML_Context.local_context;
 wenzelm parents: 
30626diff
changeset | 107 | in use_text context (1, name) verbose txt end; | 
| 21770 | 108 | |
| 109 | ||
| 30626 | 110 | (* toplevel pretty printing *) | 
| 111 | ||
| 112 | fun ml_pprint pps = | |
| 113 | let | |
| 114 | fun str "" = () | |
| 115 | | str s = PrettyPrint.string pps s; | |
| 116 | fun pprint (ML_Pretty.Block ((bg, en), prts, ind)) = | |
| 117 | (str bg; PrettyPrint.openHOVBox pps (PrettyPrint.Rel (dest_int ind)); | |
| 118 | List.app pprint prts; PrettyPrint.closeBox pps; str en) | |
| 119 | | pprint (ML_Pretty.String (s, _)) = str s | |
| 120 |       | pprint (ML_Pretty.Break (false, wd)) = PrettyPrint.break pps {nsp = dest_int wd, offset = 0}
 | |
| 121 | | pprint (ML_Pretty.Break (true, _)) = PrettyPrint.newline pps; | |
| 122 | in pprint end; | |
| 123 | ||
| 30672 
beaadd5af500
more systematic type use_context, with particular values ML_Parse.global_context and ML_Context.local_context;
 wenzelm parents: 
30626diff
changeset | 124 | fun toplevel_pp context path pp = | 
| 
beaadd5af500
more systematic type use_context, with particular values ML_Parse.global_context and ML_Context.local_context;
 wenzelm parents: 
30626diff
changeset | 125 | use_text context (1, "pp") false | 
| 30626 | 126 |     ("CompilerPPTable.install_pp [" ^ String.concatWith "," (map (fn s => "\"" ^ s ^ "\"")  path) ^
 | 
| 127 |       "] (fn pps => ml_pprint pps o Pretty.to_ML o (" ^ pp ^ "))");
 | |
| 128 | ||
| 129 | ||
| 4403 | 130 | |
| 5816 | 131 | (** interrupts **) | 
| 132 | ||
| 26084 
a7475459c740
replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
 wenzelm parents: 
25732diff
changeset | 133 | local | 
| 
a7475459c740
replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
 wenzelm parents: 
25732diff
changeset | 134 | |
| 
a7475459c740
replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
 wenzelm parents: 
25732diff
changeset | 135 | fun change_signal new_handler f x = | 
| 5816 | 136 | let | 
| 26084 
a7475459c740
replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
 wenzelm parents: 
25732diff
changeset | 137 | val old_handler = Signals.setHandler (Signals.sigINT, new_handler); | 
| 
a7475459c740
replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
 wenzelm parents: 
25732diff
changeset | 138 | val result = Exn.capture (f old_handler) x; | 
| 12990 | 139 | val _ = Signals.setHandler (Signals.sigINT, old_handler); | 
| 23965 
f93e509659c1
ML-Systems/exn.ML, ML-Systems/multithreading_dummy.ML;
 wenzelm parents: 
23921diff
changeset | 140 | in Exn.release result end; | 
| 5816 | 141 | |
| 26084 
a7475459c740
replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
 wenzelm parents: 
25732diff
changeset | 142 | in | 
| 
a7475459c740
replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
 wenzelm parents: 
25732diff
changeset | 143 | |
| 
a7475459c740
replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
 wenzelm parents: 
25732diff
changeset | 144 | fun interruptible (f: 'a -> 'b) x = | 
| 12990 | 145 | let | 
| 39232 
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
 wenzelm parents: 
38635diff
changeset | 146 | val result = ref (Exn.interrupt_exn: 'b Exn.result); | 
| 12990 | 147 | val old_handler = Signals.inqHandler Signals.sigINT; | 
| 5816 | 148 | in | 
| 12990 | 149 | SMLofNJ.Cont.callcc (fn cont => | 
| 26084 
a7475459c740
replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
 wenzelm parents: 
25732diff
changeset | 150 | (Signals.setHandler (Signals.sigINT, Signals.HANDLER (fn _ => cont)); | 
| 
a7475459c740
replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
 wenzelm parents: 
25732diff
changeset | 151 | result := Exn.capture f x)); | 
| 12990 | 152 | Signals.setHandler (Signals.sigINT, old_handler); | 
| 26084 
a7475459c740
replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
 wenzelm parents: 
25732diff
changeset | 153 | Exn.release (! result) | 
| 12990 | 154 | end; | 
| 5816 | 155 | |
| 26084 
a7475459c740
replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
 wenzelm parents: 
25732diff
changeset | 156 | fun uninterruptible f = | 
| 
a7475459c740
replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
 wenzelm parents: 
25732diff
changeset | 157 | change_signal Signals.IGNORE | 
| 
a7475459c740
replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
 wenzelm parents: 
25732diff
changeset | 158 | (fn old_handler => f (fn g => change_signal old_handler (fn _ => g))); | 
| 
a7475459c740
replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
 wenzelm parents: 
25732diff
changeset | 159 | |
| 
a7475459c740
replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
 wenzelm parents: 
25732diff
changeset | 160 | end; | 
| 
a7475459c740
replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
 wenzelm parents: 
25732diff
changeset | 161 | |
| 21298 
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
 wenzelm parents: 
18790diff
changeset | 162 | |
| 39616 
8052101883c3
renamed setmp_noncritical to Unsynchronized.setmp to emphasize its meaning;
 wenzelm parents: 
39585diff
changeset | 163 | use "ML-Systems/unsynchronized.ML"; | 
| 
8052101883c3
renamed setmp_noncritical to Unsynchronized.setmp to emphasize its meaning;
 wenzelm parents: 
39585diff
changeset | 164 | |
| 43948 | 165 | use "ML-Systems/ml_system.ML"; | 
| 166 |