src/Pure/ML-Systems/smlnj.ML
author haftmann
Tue Oct 20 16:13:01 2009 +0200 (2009-10-20)
changeset 33037 b22e44496dc2
parent 32776 1504f9c2d060
child 33060 e66b41782cb5
permissions -rw-r--r--
replaced old_style infixes eq_set, subset, union, inter and variants by generic versions
wenzelm@4403
     1
(*  Title:      Pure/ML-Systems/smlnj.ML
wenzelm@4403
     2
wenzelm@5708
     3
Compatibility file for Standard ML of New Jersey 110 or later.
wenzelm@4403
     4
*)
wenzelm@4403
     5
wenzelm@28443
     6
exception Interrupt;
wenzelm@31427
     7
fun reraise exn = raise exn;
wenzelm@28443
     8
wenzelm@24599
     9
use "ML-Systems/proper_int.ML";
wenzelm@32776
    10
use "ML-Systems/unsynchronized.ML";
wenzelm@24599
    11
use "ML-Systems/overloading_smlnj.ML";
wenzelm@23965
    12
use "ML-Systems/exn.ML";
wenzelm@25732
    13
use "ML-Systems/universal.ML";
wenzelm@28151
    14
use "ML-Systems/thread_dummy.ML";
wenzelm@24688
    15
use "ML-Systems/multithreading.ML";
wenzelm@31686
    16
use "ML-Systems/timing.ML";
wenzelm@26220
    17
use "ML-Systems/system_shell.ML";
wenzelm@28268
    18
use "ML-Systems/ml_name_space.ML";
wenzelm@30619
    19
use "ML-Systems/ml_pretty.ML";
wenzelm@30672
    20
use "ML-Systems/use_context.ML";
wenzelm@23921
    21
wenzelm@16542
    22
wenzelm@16542
    23
(*low-level pointer equality*)
wenzelm@16502
    24
wenzelm@21298
    25
CM.autoload "$smlnj/init/init.cmi";
wenzelm@21298
    26
val pointer_eq = InlineT.ptreql;
paulson@16528
    27
wenzelm@16502
    28
wenzelm@4403
    29
(* restore old-style character / string functions *)
wenzelm@4403
    30
wenzelm@24145
    31
val ord     = mk_int o SML90.ord;
wenzelm@24145
    32
val chr     = SML90.chr o dest_int;
paulson@10725
    33
val explode = SML90.explode;
paulson@10725
    34
val implode = SML90.implode;
wenzelm@4403
    35
wenzelm@4403
    36
wenzelm@4403
    37
(* New Jersey ML parameters *)
wenzelm@4403
    38
wenzelm@4403
    39
val _ =
wenzelm@21298
    40
 (Control.Print.printLength := 1000;
wenzelm@21298
    41
  Control.Print.printDepth := 350;
wenzelm@21298
    42
  Control.Print.stringDepth := 250;
wenzelm@21298
    43
  Control.Print.signatures := 2;
wenzelm@21298
    44
  Control.MC.matchRedundantError := false);
wenzelm@4403
    45
wenzelm@4403
    46
wenzelm@4403
    47
(* Poly/ML emulation *)
wenzelm@4403
    48
wenzelm@24599
    49
val exit = exit o dest_int;
wenzelm@4403
    50
fun quit () = exit 0;
wenzelm@4403
    51
wenzelm@4403
    52
(*limit the printing depth -- divided by 2 for comparibility with Poly/ML*)
wenzelm@24329
    53
local
wenzelm@24599
    54
  val depth = ref (10: int);
wenzelm@24329
    55
in
wenzelm@24329
    56
  fun get_print_depth () = ! depth;
wenzelm@24329
    57
  fun print_depth n =
wenzelm@24329
    58
   (depth := n;
wenzelm@24329
    59
    Control.Print.printDepth := dest_int n div 2;
wenzelm@24329
    60
    Control.Print.printLength := dest_int n);
wenzelm@24329
    61
end;
wenzelm@4403
    62
wenzelm@26474
    63
wenzelm@16660
    64
(*prompts*)
wenzelm@4977
    65
fun ml_prompts p1 p2 =
wenzelm@21298
    66
  (Control.primaryPrompt := p1; Control.secondaryPrompt := p2);
wenzelm@4977
    67
wenzelm@17511
    68
(*dummy implementation*)
wenzelm@16681
    69
fun profile (n: int) f x = f x;
wenzelm@16681
    70
wenzelm@17511
    71
(*dummy implementation*)
wenzelm@16681
    72
fun exception_trace f = f ();
wenzelm@4977
    73
wenzelm@21298
    74
wenzelm@4403
    75
(* ML command execution *)
wenzelm@4403
    76
wenzelm@30672
    77
fun use_text ({tune_source, print, error, ...}: use_context) (line, name) verbose txt =
wenzelm@5090
    78
  let
wenzelm@21298
    79
    val ref out_orig = Control.Print.out;
wenzelm@5090
    80
wenzelm@5090
    81
    val out_buffer = ref ([]: string list);
wenzelm@5090
    82
    val out = {say = (fn s => out_buffer := s :: ! out_buffer), flush = (fn () => ())};
wenzelm@10914
    83
    fun output () =
wenzelm@7890
    84
      let val str = implode (rev (! out_buffer))
wenzelm@10914
    85
      in String.substring (str, 0, Int.max (0, size str - 1)) end;
wenzelm@5090
    86
  in
wenzelm@21298
    87
    Control.Print.out := out;
wenzelm@30672
    88
    Backend.Interact.useStream (TextIO.openString (tune_source txt)) handle exn =>
wenzelm@22144
    89
      (Control.Print.out := out_orig;
wenzelm@30672
    90
        error ((if name = "" then "" else "Error in " ^ name ^ "\n") ^ output ()); raise exn);
wenzelm@21298
    91
    Control.Print.out := out_orig;
wenzelm@10914
    92
    if verbose then print (output ()) else ()
wenzelm@5090
    93
  end;
wenzelm@4403
    94
wenzelm@30672
    95
fun use_file context verbose name =
wenzelm@24599
    96
  let
wenzelm@24599
    97
    val instream = TextIO.openIn name;
wenzelm@26504
    98
    val txt = Exn.release (Exn.capture TextIO.inputAll instream before TextIO.closeIn instream);
wenzelm@30672
    99
  in use_text context (1, name) verbose txt end;
wenzelm@21770
   100
wenzelm@30672
   101
fun forget_structure _ = ();
wenzelm@26474
   102
wenzelm@21770
   103
wenzelm@30626
   104
(* toplevel pretty printing *)
wenzelm@30626
   105
wenzelm@30626
   106
fun ml_pprint pps =
wenzelm@30626
   107
  let
wenzelm@30626
   108
    fun str "" = ()
wenzelm@30626
   109
      | str s = PrettyPrint.string pps s;
wenzelm@30626
   110
    fun pprint (ML_Pretty.Block ((bg, en), prts, ind)) =
wenzelm@30626
   111
          (str bg; PrettyPrint.openHOVBox pps (PrettyPrint.Rel (dest_int ind));
wenzelm@30626
   112
            List.app pprint prts; PrettyPrint.closeBox pps; str en)
wenzelm@30626
   113
      | pprint (ML_Pretty.String (s, _)) = str s
wenzelm@30626
   114
      | pprint (ML_Pretty.Break (false, wd)) = PrettyPrint.break pps {nsp = dest_int wd, offset = 0}
wenzelm@30626
   115
      | pprint (ML_Pretty.Break (true, _)) = PrettyPrint.newline pps;
wenzelm@30626
   116
  in pprint end;
wenzelm@30626
   117
wenzelm@30672
   118
fun toplevel_pp context path pp =
wenzelm@30672
   119
  use_text context (1, "pp") false
wenzelm@30626
   120
    ("CompilerPPTable.install_pp [" ^ String.concatWith "," (map (fn s => "\"" ^ s ^ "\"")  path) ^
wenzelm@30626
   121
      "] (fn pps => ml_pprint pps o Pretty.to_ML o (" ^ pp ^ "))");
wenzelm@30626
   122
wenzelm@30626
   123
wenzelm@4403
   124
wenzelm@5816
   125
(** interrupts **)
wenzelm@5816
   126
wenzelm@26084
   127
local
wenzelm@26084
   128
wenzelm@26084
   129
fun change_signal new_handler f x =
wenzelm@5816
   130
  let
wenzelm@26084
   131
    val old_handler = Signals.setHandler (Signals.sigINT, new_handler);
wenzelm@26084
   132
    val result = Exn.capture (f old_handler) x;
wenzelm@12990
   133
    val _ = Signals.setHandler (Signals.sigINT, old_handler);
wenzelm@23965
   134
  in Exn.release result end;
wenzelm@5816
   135
wenzelm@26084
   136
in
wenzelm@26084
   137
wenzelm@26084
   138
fun interruptible (f: 'a -> 'b) x =
wenzelm@12990
   139
  let
wenzelm@26084
   140
    val result = ref (Exn.Exn Interrupt: 'b Exn.result);
wenzelm@12990
   141
    val old_handler = Signals.inqHandler Signals.sigINT;
wenzelm@5816
   142
  in
wenzelm@12990
   143
    SMLofNJ.Cont.callcc (fn cont =>
wenzelm@26084
   144
      (Signals.setHandler (Signals.sigINT, Signals.HANDLER (fn _ => cont));
wenzelm@26084
   145
        result := Exn.capture f x));
wenzelm@12990
   146
    Signals.setHandler (Signals.sigINT, old_handler);
wenzelm@26084
   147
    Exn.release (! result)
wenzelm@12990
   148
  end;
wenzelm@5816
   149
wenzelm@26084
   150
fun uninterruptible f =
wenzelm@26084
   151
  change_signal Signals.IGNORE
wenzelm@26084
   152
    (fn old_handler => f (fn g => change_signal old_handler (fn _ => g)));
wenzelm@26084
   153
wenzelm@26084
   154
end;
wenzelm@26084
   155
wenzelm@21298
   156
wenzelm@21298
   157
(* basis library fixes *)
wenzelm@21298
   158
wenzelm@21298
   159
structure TextIO =
wenzelm@21298
   160
struct
wenzelm@21298
   161
  open TextIO;
wenzelm@23139
   162
  fun inputLine is = TextIO.inputLine is
wenzelm@21298
   163
    handle IO.Io _ => raise Interrupt;
wenzelm@21298
   164
end;
wenzelm@17511
   165
webertj@18790
   166
wenzelm@5816
   167
wenzelm@4403
   168
(** OS related **)
wenzelm@4403
   169
wenzelm@23826
   170
(* current directory *)
wenzelm@23826
   171
wenzelm@23826
   172
val cd = OS.FileSys.chDir;
wenzelm@23826
   173
val pwd = OS.FileSys.getDir;
wenzelm@23826
   174
wenzelm@23826
   175
wenzelm@4403
   176
(* system command execution *)
wenzelm@4403
   177
wenzelm@26220
   178
val system_out = (fn (output, rc) => (output, mk_int rc)) o system_out;
wenzelm@7855
   179
wenzelm@4403
   180
wenzelm@17824
   181
(*Convert a process ID to a decimal string (chiefly for tracing)*)
wenzelm@28488
   182
fun process_id pid =
wenzelm@28488
   183
  Word.fmt StringCvt.DEC (Word.fromLargeWord (Posix.Process.pidToWord (Posix.ProcEnv.getpid ())));
wenzelm@17824
   184
wenzelm@17824
   185
wenzelm@4403
   186
(* getenv *)
wenzelm@4403
   187
wenzelm@4403
   188
fun getenv var =
wenzelm@4403
   189
  (case OS.Process.getEnv var of
wenzelm@4403
   190
    NONE => ""
wenzelm@4403
   191
  | SOME txt => txt);