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