src/Pure/ML-Systems/smlnj.ML
author paulson
Wed Jun 22 11:20:45 2005 +0200 (2005-06-22)
changeset 16528 25a7459d4d4a
parent 16502 5a56e59526a5
child 16542 1259088dc448
permissions -rw-r--r--
pointer equality for sml/nj
wenzelm@4403
     1
(*  Title:      Pure/ML-Systems/smlnj.ML
wenzelm@4403
     2
    ID:         $Id$
wenzelm@4403
     3
    Author:     Carsten Clasohm and Markus Wenzel, TU Muenchen
wenzelm@4403
     4
wenzelm@5708
     5
Compatibility file for Standard ML of New Jersey 110 or later.
wenzelm@4403
     6
*)
wenzelm@4403
     7
wenzelm@12874
     8
(case #version_id (Compiler.version) of
wenzelm@12874
     9
  [110, x] => if x >= 35 then use "ML-Systems/smlnj-compiler.ML" else ()
wenzelm@12874
    10
| _ => ());
wenzelm@12874
    11
wenzelm@4403
    12
(** ML system related **)
wenzelm@4403
    13
wenzelm@16502
    14
(* low-level pointer equality *)
wenzelm@16502
    15
paulson@16528
    16
(*proper implementation available? This declaration may get overridden below.*)
wenzelm@16502
    17
fun pointer_eq (x:''a, y) = false;
wenzelm@16502
    18
paulson@16528
    19
(case #version_id (Compiler.version) of
paulson@16528
    20
  [110, x] => if x >= 49 then use "ML-Systems/smlnj-ptreql.ML" else ()
paulson@16528
    21
| _ => ());
paulson@16528
    22
wenzelm@16502
    23
wenzelm@4403
    24
(* restore old-style character / string functions *)
wenzelm@4403
    25
paulson@10725
    26
val ord     = SML90.ord;
paulson@10725
    27
val chr     = SML90.chr;
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@4403
    35
 (Compiler.Control.Print.printLength := 1000;
wenzelm@4403
    36
  Compiler.Control.Print.printDepth := 350;
wenzelm@4403
    37
  Compiler.Control.Print.stringDepth := 250;
berghofe@12581
    38
  Compiler.Control.Print.signatures := 2;
berghofe@12581
    39
  Compiler.Control.MC.matchRedundantError := false);
wenzelm@4403
    40
wenzelm@4403
    41
wenzelm@4403
    42
(* Poly/ML emulation *)
wenzelm@4403
    43
wenzelm@4403
    44
fun quit () = exit 0;
wenzelm@4403
    45
wenzelm@4403
    46
(*limit the printing depth -- divided by 2 for comparibility with Poly/ML*)
wenzelm@4403
    47
fun print_depth n =
wenzelm@4403
    48
 (Compiler.Control.Print.printDepth := n div 2;
wenzelm@4403
    49
  Compiler.Control.Print.printLength := n);
wenzelm@4403
    50
wenzelm@4403
    51
wenzelm@5816
    52
(* compiler-independent timing functions *)
wenzelm@4403
    53
skalberg@14519
    54
(case #version_id (Compiler.version) of
paulson@14656
    55
  [110, x] => if x >= 44
skalberg@14519
    56
	      then use "ML-Systems/cpu-timer-basis.ML"
skalberg@14519
    57
	      else use "ML-Systems/cpu-timer-gc.ML"
skalberg@14520
    58
| _ => use "ML-Systems/cpu-timer-gc.ML");
wenzelm@4403
    59
wenzelm@4403
    60
wenzelm@4977
    61
(* prompts *)
wenzelm@4977
    62
wenzelm@4977
    63
fun ml_prompts p1 p2 =
wenzelm@4977
    64
  (Compiler.Control.primaryPrompt := p1; Compiler.Control.secondaryPrompt := p2);
wenzelm@4977
    65
wenzelm@4977
    66
skalberg@14519
    67
(case #version_id (Compiler.version) of
paulson@14656
    68
  [110, x] => if x >= 44
skalberg@14519
    69
	      then use "ML-Systems/smlnj-basis-compat.ML"
skalberg@14519
    70
	      else ()
skalberg@14519
    71
| _ => ());
skalberg@14519
    72
wenzelm@4403
    73
(* toplevel pretty printing (see also Pure/install_pp.ML) *)
wenzelm@4403
    74
skalberg@14519
    75
(case #version_id (Compiler.version) of
paulson@14656
    76
  [110, x] => if x >= 44
skalberg@14519
    77
	      then use "ML-Systems/smlnj-pp-new.ML"
skalberg@14519
    78
	      else use "ML-Systems/smlnj-pp-old.ML"
skalberg@14520
    79
| _ => use "ML-Systems/smlnj-pp-old.ML");
wenzelm@4403
    80
wenzelm@4403
    81
fun install_pp (path, pp) = Compiler.PPTable.install_pp path pp;
wenzelm@4403
    82
wenzelm@4403
    83
wenzelm@4403
    84
(* ML command execution *)
wenzelm@4403
    85
wenzelm@10914
    86
fun use_text (print, err) verbose txt =
wenzelm@5090
    87
  let
wenzelm@5090
    88
    val ref out_orig = Compiler.Control.Print.out;
wenzelm@5090
    89
wenzelm@5090
    90
    val out_buffer = ref ([]: string list);
wenzelm@5090
    91
    val out = {say = (fn s => out_buffer := s :: ! out_buffer), flush = (fn () => ())};
wenzelm@10914
    92
    fun output () =
wenzelm@7890
    93
      let val str = implode (rev (! out_buffer))
wenzelm@10914
    94
      in String.substring (str, 0, Int.max (0, size str - 1)) end;
wenzelm@5090
    95
  in
wenzelm@5090
    96
    Compiler.Control.Print.out := out;
wenzelm@5090
    97
    Compiler.Interact.useStream (TextIO.openString txt) handle exn =>
wenzelm@10914
    98
      (Compiler.Control.Print.out := out_orig; err (output ()); raise exn);
wenzelm@5090
    99
    Compiler.Control.Print.out := out_orig;
wenzelm@10914
   100
    if verbose then print (output ()) else ()
wenzelm@5090
   101
  end;
wenzelm@4403
   102
wenzelm@4403
   103
wenzelm@4403
   104
wenzelm@5816
   105
(** interrupts **)
wenzelm@5816
   106
wenzelm@12990
   107
exception Interrupt;
wenzelm@12990
   108
wenzelm@5816
   109
local
wenzelm@5816
   110
wenzelm@12990
   111
fun capture f x = ((f x): unit; NONE) handle exn => SOME exn;
wenzelm@5816
   112
wenzelm@12990
   113
fun release NONE = ()
wenzelm@12990
   114
  | release (SOME exn) = raise exn;
wenzelm@5816
   115
wenzelm@5816
   116
in
wenzelm@5816
   117
wenzelm@12990
   118
fun ignore_interrupt f x =
wenzelm@5816
   119
  let
wenzelm@12990
   120
    val old_handler = Signals.setHandler (Signals.sigINT, Signals.IGNORE);
wenzelm@12990
   121
    val result = capture f x;
wenzelm@12990
   122
    val _ = Signals.setHandler (Signals.sigINT, old_handler);
wenzelm@12990
   123
  in release result end;
wenzelm@5816
   124
wenzelm@12990
   125
fun raise_interrupt f x =
wenzelm@12990
   126
  let
wenzelm@5816
   127
    val interrupted = ref false;
wenzelm@12990
   128
    val result = ref NONE;
wenzelm@12990
   129
    val old_handler = Signals.inqHandler Signals.sigINT;
wenzelm@5816
   130
  in
wenzelm@12990
   131
    SMLofNJ.Cont.callcc (fn cont =>
wenzelm@12990
   132
      (Signals.setHandler (Signals.sigINT, Signals.HANDLER (fn _ => (interrupted := true; cont)));
wenzelm@12990
   133
      result := capture f x));
wenzelm@12990
   134
    Signals.setHandler (Signals.sigINT, old_handler);
wenzelm@12990
   135
    if ! interrupted then raise Interrupt else release (! result)
wenzelm@12990
   136
  end;
wenzelm@5816
   137
wenzelm@5816
   138
end;
wenzelm@5816
   139
paulson@15702
   140
(** Signal handling: emulation of the Poly/ML Signal structure. Note that types 
paulson@15702
   141
    Posix.Signal.signal and Signals.signal differ **)
paulson@15702
   142
paulson@15702
   143
structure IsaSignal =
paulson@15702
   144
struct
paulson@15702
   145
paulson@15702
   146
datatype sig_handle = SIG_DFL | SIG_IGN | SIG_HANDLE of Signals.signal -> unit;
paulson@15702
   147
paulson@15702
   148
(*From the SML/NJ documentation:
paulson@15702
   149
"HANDLER(f) installs a handler for a signal. When signal is delivered to the process, the execution state of the current thread will be bundled up as a continuation k, then f(signal,n,k) will be called. The number n is the number of times signal has been signalled since the last time f was invoked for it."*)
paulson@15702
   150
paulson@15702
   151
fun toAction SIG_DFL = Signals.DEFAULT
paulson@15702
   152
  | toAction SIG_IGN = Signals.IGNORE
paulson@15702
   153
  | toAction (SIG_HANDLE iu) = 
paulson@15702
   154
      Signals.HANDLER (fn (signo,_,cont) => (iu signo; cont));
paulson@15702
   155
  
paulson@15702
   156
(*The types are correct, but I'm not sure about the semantics!*)
paulson@15702
   157
fun fromAction Signals.DEFAULT = SIG_DFL
paulson@15702
   158
  | fromAction Signals.IGNORE = SIG_IGN
paulson@15702
   159
  | fromAction (Signals.HANDLER f) = 
paulson@15702
   160
      SIG_HANDLE (fn signo => SMLofNJ.Cont.callcc (fn k => (f (signo,0,k); ())));
paulson@15702
   161
    
paulson@15702
   162
(*Poly/ML version has type  int * sig_handle -> sig_handle*)
paulson@15702
   163
fun signal (signo, sh) = fromAction (Signals.setHandler (signo, toAction sh));
paulson@15702
   164
paulson@15702
   165
val usr1 = UnixSignals.sigUSR1
paulson@15702
   166
val usr2 = UnixSignals.sigUSR2
paulson@15702
   167
paulson@15702
   168
end;
wenzelm@5816
   169
wenzelm@5816
   170
wenzelm@4403
   171
(** OS related **)
wenzelm@4403
   172
wenzelm@4403
   173
(* system command execution *)
wenzelm@4403
   174
wenzelm@4403
   175
(*execute Unix command which doesn't take any input from stdin and
wenzelm@4403
   176
  sends its output to stdout; could be done more easily by Unix.execute,
wenzelm@4403
   177
  but that function doesn't use the PATH*)
wenzelm@4403
   178
fun execute command =
wenzelm@4403
   179
  let
wenzelm@4403
   180
    val tmp_name = OS.FileSys.tmpName ();
wenzelm@4403
   181
    val is = (OS.Process.system (command ^ " > " ^ tmp_name); TextIO.openIn tmp_name);
wenzelm@4403
   182
    val result = TextIO.inputAll is;
wenzelm@4403
   183
  in
wenzelm@4403
   184
    TextIO.closeIn is;
wenzelm@4403
   185
    OS.FileSys.remove tmp_name;
wenzelm@4403
   186
    result
wenzelm@4403
   187
  end;
wenzelm@4403
   188
wenzelm@7855
   189
(*plain version; with return code*)
wenzelm@12990
   190
val system = OS.Process.system: string -> int;
wenzelm@7855
   191
wenzelm@4403
   192
wenzelm@4403
   193
(* getenv *)
wenzelm@4403
   194
wenzelm@4403
   195
fun getenv var =
wenzelm@4403
   196
  (case OS.Process.getEnv var of
wenzelm@4403
   197
    NONE => ""
wenzelm@4403
   198
  | SOME txt => txt);