src/Pure/ML-Systems/polyml.ML
author webertj
Tue, 01 Jun 2004 00:18:01 +0200
changeset 14850 393a7be73160
parent 14519 4ca3608fdf4f
child 14870 c5cf7c001313
permissions -rw-r--r--
including polyml-time-limit.ML
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2341
e154da40ef00 Compatibility file for Poly/ML (versions 2.x, 3.x).
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/ML-Systems/polyml.ML
e154da40ef00 Compatibility file for Poly/ML (versions 2.x, 3.x).
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
e154da40ef00 Compatibility file for Poly/ML (versions 2.x, 3.x).
wenzelm
parents:
diff changeset
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
e154da40ef00 Compatibility file for Poly/ML (versions 2.x, 3.x).
wenzelm
parents:
diff changeset
     4
    Copyright   1991  University of Cambridge
e154da40ef00 Compatibility file for Poly/ML (versions 2.x, 3.x).
wenzelm
parents:
diff changeset
     5
11078
c1b32e7124b4 4.0 version;
wenzelm
parents: 10914
diff changeset
     6
Compatibility file for Poly/ML (version 4.0).
2341
e154da40ef00 Compatibility file for Poly/ML (versions 2.x, 3.x).
wenzelm
parents:
diff changeset
     7
*)
e154da40ef00 Compatibility file for Poly/ML (versions 2.x, 3.x).
wenzelm
parents:
diff changeset
     8
11078
c1b32e7124b4 4.0 version;
wenzelm
parents: 10914
diff changeset
     9
(** ML system related **)
2341
e154da40ef00 Compatibility file for Poly/ML (versions 2.x, 3.x).
wenzelm
parents:
diff changeset
    10
11078
c1b32e7124b4 4.0 version;
wenzelm
parents: 10914
diff changeset
    11
(* old Poly/ML emulation *)
2341
e154da40ef00 Compatibility file for Poly/ML (versions 2.x, 3.x).
wenzelm
parents:
diff changeset
    12
11078
c1b32e7124b4 4.0 version;
wenzelm
parents: 10914
diff changeset
    13
local
c1b32e7124b4 4.0 version;
wenzelm
parents: 10914
diff changeset
    14
  val orig_exit = exit;
c1b32e7124b4 4.0 version;
wenzelm
parents: 10914
diff changeset
    15
in
c1b32e7124b4 4.0 version;
wenzelm
parents: 10914
diff changeset
    16
  open PolyML;
c1b32e7124b4 4.0 version;
wenzelm
parents: 10914
diff changeset
    17
  val exit = orig_exit;
c1b32e7124b4 4.0 version;
wenzelm
parents: 10914
diff changeset
    18
  fun quit () = exit 0;
7148
e9c253036b45 removed obsolete concat;
wenzelm
parents: 7147
diff changeset
    19
end;
6042
0ccde2f25dd6 Basis Library compatible substring oeration
paulson
parents: 5813
diff changeset
    20
2341
e154da40ef00 Compatibility file for Poly/ML (versions 2.x, 3.x).
wenzelm
parents:
diff changeset
    21
11078
c1b32e7124b4 4.0 version;
wenzelm
parents: 10914
diff changeset
    22
(* restore old-style character / string functions *)
3588
e487bf0ed6c4 cleaned up;
wenzelm
parents: 2408
diff changeset
    23
11078
c1b32e7124b4 4.0 version;
wenzelm
parents: 10914
diff changeset
    24
val ord = SML90.ord;
c1b32e7124b4 4.0 version;
wenzelm
parents: 10914
diff changeset
    25
val chr = SML90.chr;
c1b32e7124b4 4.0 version;
wenzelm
parents: 10914
diff changeset
    26
val explode = SML90.explode;
c1b32e7124b4 4.0 version;
wenzelm
parents: 10914
diff changeset
    27
val implode = SML90.implode;
c1b32e7124b4 4.0 version;
wenzelm
parents: 10914
diff changeset
    28
14519
4ca3608fdf4f Added support for the newer versions of SML/NJ, which break several of the
skalberg
parents: 12989
diff changeset
    29
(* compiler-independent timing functions *)
2341
e154da40ef00 Compatibility file for Poly/ML (versions 2.x, 3.x).
wenzelm
parents:
diff changeset
    30
14519
4ca3608fdf4f Added support for the newer versions of SML/NJ, which break several of the
skalberg
parents: 12989
diff changeset
    31
use "ML-Systems/cpu-timer-basis.ML";
2341
e154da40ef00 Compatibility file for Poly/ML (versions 2.x, 3.x).
wenzelm
parents:
diff changeset
    32
14850
393a7be73160 including polyml-time-limit.ML
webertj
parents: 14519
diff changeset
    33
(* bounded time execution *)
393a7be73160 including polyml-time-limit.ML
webertj
parents: 14519
diff changeset
    34
393a7be73160 including polyml-time-limit.ML
webertj
parents: 14519
diff changeset
    35
use "ML-Systems/polyml-time-limit.ML";
393a7be73160 including polyml-time-limit.ML
webertj
parents: 14519
diff changeset
    36
4977
6cec2c0ffdbf added ml_prompts;
wenzelm
parents: 4428
diff changeset
    37
(* prompts *)
6cec2c0ffdbf added ml_prompts;
wenzelm
parents: 4428
diff changeset
    38
4984
d17004d4c13b fixed ml_prompts;
wenzelm
parents: 4977
diff changeset
    39
fun ml_prompts p1 p2 = (PolyML.Compiler.prompt1 := p1; PolyML.Compiler.prompt2 := p2);
4977
6cec2c0ffdbf added ml_prompts;
wenzelm
parents: 4428
diff changeset
    40
6cec2c0ffdbf added ml_prompts;
wenzelm
parents: 4428
diff changeset
    41
3588
e487bf0ed6c4 cleaned up;
wenzelm
parents: 2408
diff changeset
    42
(* toplevel pretty printing (see also Pure/install_pp.ML) *)
2341
e154da40ef00 Compatibility file for Poly/ML (versions 2.x, 3.x).
wenzelm
parents:
diff changeset
    43
11078
c1b32e7124b4 4.0 version;
wenzelm
parents: 10914
diff changeset
    44
fun make_pp _ pprint (str, blk, brk, en) _ _ obj =
2341
e154da40ef00 Compatibility file for Poly/ML (versions 2.x, 3.x).
wenzelm
parents:
diff changeset
    45
  pprint obj (str, fn ind => blk (ind, false), fn wd => brk (wd, 0),
e154da40ef00 Compatibility file for Poly/ML (versions 2.x, 3.x).
wenzelm
parents:
diff changeset
    46
    fn () => brk (99999, 0), en);
e154da40ef00 Compatibility file for Poly/ML (versions 2.x, 3.x).
wenzelm
parents:
diff changeset
    47
e154da40ef00 Compatibility file for Poly/ML (versions 2.x, 3.x).
wenzelm
parents:
diff changeset
    48
3588
e487bf0ed6c4 cleaned up;
wenzelm
parents: 2408
diff changeset
    49
(* ML command execution -- 'eval' *)
e487bf0ed6c4 cleaned up;
wenzelm
parents: 2408
diff changeset
    50
7890
0aa16bc2abdb use_text: remove last char from output;
wenzelm
parents: 7855
diff changeset
    51
local
0aa16bc2abdb use_text: remove last char from output;
wenzelm
parents: 7855
diff changeset
    52
0aa16bc2abdb use_text: remove last char from output;
wenzelm
parents: 7855
diff changeset
    53
fun drop_last [] = []
0aa16bc2abdb use_text: remove last char from output;
wenzelm
parents: 7855
diff changeset
    54
  | drop_last [x] = []
0aa16bc2abdb use_text: remove last char from output;
wenzelm
parents: 7855
diff changeset
    55
  | drop_last (x :: xs) = x :: drop_last xs;
0aa16bc2abdb use_text: remove last char from output;
wenzelm
parents: 7855
diff changeset
    56
0aa16bc2abdb use_text: remove last char from output;
wenzelm
parents: 7855
diff changeset
    57
val drop_last_char = implode o drop_last o explode;
0aa16bc2abdb use_text: remove last char from output;
wenzelm
parents: 7855
diff changeset
    58
0aa16bc2abdb use_text: remove last char from output;
wenzelm
parents: 7855
diff changeset
    59
in
0aa16bc2abdb use_text: remove last char from output;
wenzelm
parents: 7855
diff changeset
    60
10914
aded4ba99b88 use_text etc.: proper output of error messages;
wenzelm
parents: 8830
diff changeset
    61
fun use_text (print, err) verbose txt =
3588
e487bf0ed6c4 cleaned up;
wenzelm
parents: 2408
diff changeset
    62
  let
5090
75ac9b451ae0 use_text: verbose flag;
wenzelm
parents: 5038
diff changeset
    63
    val in_buffer = ref (explode txt);
75ac9b451ae0 use_text: verbose flag;
wenzelm
parents: 5038
diff changeset
    64
    val out_buffer = ref ([]: string list);
10914
aded4ba99b88 use_text etc.: proper output of error messages;
wenzelm
parents: 8830
diff changeset
    65
    fun output () = drop_last_char (implode (rev (! out_buffer)));
5090
75ac9b451ae0 use_text: verbose flag;
wenzelm
parents: 5038
diff changeset
    66
5038
301c37df931d use_text replaces use_strings;
wenzelm
parents: 4984
diff changeset
    67
    fun get () =
5090
75ac9b451ae0 use_text: verbose flag;
wenzelm
parents: 5038
diff changeset
    68
      (case ! in_buffer of
5038
301c37df931d use_text replaces use_strings;
wenzelm
parents: 4984
diff changeset
    69
        [] => ""
5090
75ac9b451ae0 use_text: verbose flag;
wenzelm
parents: 5038
diff changeset
    70
      | c :: cs => (in_buffer := cs; c));
75ac9b451ae0 use_text: verbose flag;
wenzelm
parents: 5038
diff changeset
    71
    fun put s = out_buffer := s :: ! out_buffer;
75ac9b451ae0 use_text: verbose flag;
wenzelm
parents: 5038
diff changeset
    72
5038
301c37df931d use_text replaces use_strings;
wenzelm
parents: 4984
diff changeset
    73
    fun exec () =
5090
75ac9b451ae0 use_text: verbose flag;
wenzelm
parents: 5038
diff changeset
    74
      (case ! in_buffer of
5038
301c37df931d use_text replaces use_strings;
wenzelm
parents: 4984
diff changeset
    75
        [] => ()
5090
75ac9b451ae0 use_text: verbose flag;
wenzelm
parents: 5038
diff changeset
    76
      | _ => (PolyML.compiler (get, put) (); exec ()));
75ac9b451ae0 use_text: verbose flag;
wenzelm
parents: 5038
diff changeset
    77
  in
10914
aded4ba99b88 use_text etc.: proper output of error messages;
wenzelm
parents: 8830
diff changeset
    78
    exec () handle exn => (err (output ()); raise exn);
aded4ba99b88 use_text etc.: proper output of error messages;
wenzelm
parents: 8830
diff changeset
    79
    if verbose then print (output ()) else ()
5090
75ac9b451ae0 use_text: verbose flag;
wenzelm
parents: 5038
diff changeset
    80
  end;
3588
e487bf0ed6c4 cleaned up;
wenzelm
parents: 2408
diff changeset
    81
11078
c1b32e7124b4 4.0 version;
wenzelm
parents: 10914
diff changeset
    82
end;
c1b32e7124b4 4.0 version;
wenzelm
parents: 10914
diff changeset
    83
c1b32e7124b4 4.0 version;
wenzelm
parents: 10914
diff changeset
    84
c1b32e7124b4 4.0 version;
wenzelm
parents: 10914
diff changeset
    85
c1b32e7124b4 4.0 version;
wenzelm
parents: 10914
diff changeset
    86
(** interrupts **)
c1b32e7124b4 4.0 version;
wenzelm
parents: 10914
diff changeset
    87
c1b32e7124b4 4.0 version;
wenzelm
parents: 10914
diff changeset
    88
exception Interrupt = SML90.Interrupt;
c1b32e7124b4 4.0 version;
wenzelm
parents: 10914
diff changeset
    89
c1b32e7124b4 4.0 version;
wenzelm
parents: 10914
diff changeset
    90
local
c1b32e7124b4 4.0 version;
wenzelm
parents: 10914
diff changeset
    91
12989
42ac77552dbf renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12108
diff changeset
    92
fun capture f x = ((f x): unit; NONE) handle exn => SOME exn;
11078
c1b32e7124b4 4.0 version;
wenzelm
parents: 10914
diff changeset
    93
12989
42ac77552dbf renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12108
diff changeset
    94
fun release NONE = ()
42ac77552dbf renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12108
diff changeset
    95
  | release (SOME exn) = raise exn;
3588
e487bf0ed6c4 cleaned up;
wenzelm
parents: 2408
diff changeset
    96
11078
c1b32e7124b4 4.0 version;
wenzelm
parents: 10914
diff changeset
    97
val sig_int = 2;
c1b32e7124b4 4.0 version;
wenzelm
parents: 10914
diff changeset
    98
c1b32e7124b4 4.0 version;
wenzelm
parents: 10914
diff changeset
    99
fun change_signal new_handler f x =
c1b32e7124b4 4.0 version;
wenzelm
parents: 10914
diff changeset
   100
  let
c1b32e7124b4 4.0 version;
wenzelm
parents: 10914
diff changeset
   101
    (*RACE wrt. other signals*)
c1b32e7124b4 4.0 version;
wenzelm
parents: 10914
diff changeset
   102
    val old_handler = Signal.signal (sig_int, new_handler);
c1b32e7124b4 4.0 version;
wenzelm
parents: 10914
diff changeset
   103
    val result = capture f x;
c1b32e7124b4 4.0 version;
wenzelm
parents: 10914
diff changeset
   104
    val _ = Signal.signal (sig_int, old_handler);
c1b32e7124b4 4.0 version;
wenzelm
parents: 10914
diff changeset
   105
  in release result end;
5813
4eea84a9427d fake interrupt handler;
wenzelm
parents: 5090
diff changeset
   106
11078
c1b32e7124b4 4.0 version;
wenzelm
parents: 10914
diff changeset
   107
val default_handler = Signal.SIG_HANDLE (fn _ => Process.interruptConsoleProcesses ());
c1b32e7124b4 4.0 version;
wenzelm
parents: 10914
diff changeset
   108
c1b32e7124b4 4.0 version;
wenzelm
parents: 10914
diff changeset
   109
in
c1b32e7124b4 4.0 version;
wenzelm
parents: 10914
diff changeset
   110
c1b32e7124b4 4.0 version;
wenzelm
parents: 10914
diff changeset
   111
val _ = Signal.signal (sig_int, default_handler);
c1b32e7124b4 4.0 version;
wenzelm
parents: 10914
diff changeset
   112
12989
42ac77552dbf renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12108
diff changeset
   113
fun ignore_interrupt f = change_signal Signal.SIG_IGN f;
42ac77552dbf renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12108
diff changeset
   114
fun raise_interrupt f = change_signal default_handler f;
11078
c1b32e7124b4 4.0 version;
wenzelm
parents: 10914
diff changeset
   115
c1b32e7124b4 4.0 version;
wenzelm
parents: 10914
diff changeset
   116
end;
5813
4eea84a9427d fake interrupt handler;
wenzelm
parents: 5090
diff changeset
   117
4eea84a9427d fake interrupt handler;
wenzelm
parents: 5090
diff changeset
   118
4eea84a9427d fake interrupt handler;
wenzelm
parents: 5090
diff changeset
   119
3588
e487bf0ed6c4 cleaned up;
wenzelm
parents: 2408
diff changeset
   120
(** OS related **)
2341
e154da40ef00 Compatibility file for Poly/ML (versions 2.x, 3.x).
wenzelm
parents:
diff changeset
   121
3588
e487bf0ed6c4 cleaned up;
wenzelm
parents: 2408
diff changeset
   122
(* system command execution *)
2341
e154da40ef00 Compatibility file for Poly/ML (versions 2.x, 3.x).
wenzelm
parents:
diff changeset
   123
3588
e487bf0ed6c4 cleaned up;
wenzelm
parents: 2408
diff changeset
   124
(*execute Unix command which doesn't take any input from stdin and
11078
c1b32e7124b4 4.0 version;
wenzelm
parents: 10914
diff changeset
   125
  sends its output to stdout; could be done more easily by Unix.execute,
c1b32e7124b4 4.0 version;
wenzelm
parents: 10914
diff changeset
   126
  but that function doesn't use the PATH*)
3588
e487bf0ed6c4 cleaned up;
wenzelm
parents: 2408
diff changeset
   127
fun execute command =
e487bf0ed6c4 cleaned up;
wenzelm
parents: 2408
diff changeset
   128
  let
11078
c1b32e7124b4 4.0 version;
wenzelm
parents: 10914
diff changeset
   129
    val tmp_name = OS.FileSys.tmpName ();
c1b32e7124b4 4.0 version;
wenzelm
parents: 10914
diff changeset
   130
    val is = (OS.Process.system (command ^ " > " ^ tmp_name); TextIO.openIn tmp_name);
c1b32e7124b4 4.0 version;
wenzelm
parents: 10914
diff changeset
   131
    val result = TextIO.inputAll is;
c1b32e7124b4 4.0 version;
wenzelm
parents: 10914
diff changeset
   132
  in
c1b32e7124b4 4.0 version;
wenzelm
parents: 10914
diff changeset
   133
    TextIO.closeIn is;
c1b32e7124b4 4.0 version;
wenzelm
parents: 10914
diff changeset
   134
    OS.FileSys.remove tmp_name;
c1b32e7124b4 4.0 version;
wenzelm
parents: 10914
diff changeset
   135
    result
c1b32e7124b4 4.0 version;
wenzelm
parents: 10914
diff changeset
   136
  end;
7855
092a6435afad system;
wenzelm
parents: 7148
diff changeset
   137
11078
c1b32e7124b4 4.0 version;
wenzelm
parents: 10914
diff changeset
   138
(*plain version; with return code*)
c1b32e7124b4 4.0 version;
wenzelm
parents: 10914
diff changeset
   139
fun system cmd =
c1b32e7124b4 4.0 version;
wenzelm
parents: 10914
diff changeset
   140
  if OS.Process.isSuccess (OS.Process.system cmd) then 0 else 1;
2341
e154da40ef00 Compatibility file for Poly/ML (versions 2.x, 3.x).
wenzelm
parents:
diff changeset
   141
e154da40ef00 Compatibility file for Poly/ML (versions 2.x, 3.x).
wenzelm
parents:
diff changeset
   142
3588
e487bf0ed6c4 cleaned up;
wenzelm
parents: 2408
diff changeset
   143
(* file handling *)
2341
e154da40ef00 Compatibility file for Poly/ML (versions 2.x, 3.x).
wenzelm
parents:
diff changeset
   144
6227
3198f547f8af fixed file_info;
wenzelm
parents: 6042
diff changeset
   145
(*get time of last modification*)
11078
c1b32e7124b4 4.0 version;
wenzelm
parents: 10914
diff changeset
   146
fun file_info name = Time.toString (OS.FileSys.modTime name) handle _ => "";
3588
e487bf0ed6c4 cleaned up;
wenzelm
parents: 2408
diff changeset
   147
e487bf0ed6c4 cleaned up;
wenzelm
parents: 2408
diff changeset
   148
e487bf0ed6c4 cleaned up;
wenzelm
parents: 2408
diff changeset
   149
(* getenv *)
e487bf0ed6c4 cleaned up;
wenzelm
parents: 2408
diff changeset
   150
11078
c1b32e7124b4 4.0 version;
wenzelm
parents: 10914
diff changeset
   151
fun getenv var =
c1b32e7124b4 4.0 version;
wenzelm
parents: 10914
diff changeset
   152
  (case OS.Process.getEnv var of
c1b32e7124b4 4.0 version;
wenzelm
parents: 10914
diff changeset
   153
    NONE => ""
c1b32e7124b4 4.0 version;
wenzelm
parents: 10914
diff changeset
   154
  | SOME txt => txt);