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