src/Pure/ML-Systems/mlworks.ML
author wenzelm
Wed, 13 Oct 1999 19:42:46 +0200
changeset 7855 092a6435afad
parent 6227 3198f547f8af
child 10730 bbaa0c6ef59f
permissions -rw-r--r--
system; use_text: pass print function;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4424
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/ML-Systems/mlworks.ML
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
     4
    Copyright   1996  University of Cambridge
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
     5
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
     6
Compatibility file for MLWorks version 1.0r2 or later.
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
     7
*)
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
     8
4430
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
     9
(** ML system related **)
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
    10
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
    11
(* restore old-style character / string functions *)
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
    12
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
    13
fun ord s = Char.ord (String.sub (s, 0));
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
    14
val chr = str o Char.chr;
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
    15
val explode = (map str) o String.explode;
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
    16
val implode = String.concat;
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
    17
4424
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    18
4430
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
    19
(* MLWorks parameters *)
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
    20
5660
f2c5354cd32f tuned MLWorks options;
wenzelm
parents: 5090
diff changeset
    21
val _ =
5750
7ab9dd4a8ba6 Warns when stack is extended; better decl of print_depth
paulson
parents: 5703
diff changeset
    22
 (MLWorks.Internal.Runtime.Event.stack_overflow_handler 
7ab9dd4a8ba6 Warns when stack is extended; better decl of print_depth
paulson
parents: 5703
diff changeset
    23
  (fn () =>
5703
53b00681c63b tuned stack_overflow_handler;
wenzelm
parents: 5660
diff changeset
    24
    let val max_stack = MLWorks.Internal.Runtime.Memory.max_stack_blocks
5750
7ab9dd4a8ba6 Warns when stack is extended; better decl of print_depth
paulson
parents: 5703
diff changeset
    25
    in max_stack := (!max_stack * 3) div 2 + 5;
7ab9dd4a8ba6 Warns when stack is extended; better decl of print_depth
paulson
parents: 5703
diff changeset
    26
       print ("#### Increasing stack to " ^ Int.toString (64 * !max_stack) ^
7ab9dd4a8ba6 Warns when stack is extended; better decl of print_depth
paulson
parents: 5703
diff changeset
    27
	      "KB\n")
7ab9dd4a8ba6 Warns when stack is extended; better decl of print_depth
paulson
parents: 5703
diff changeset
    28
    end);
5660
f2c5354cd32f tuned MLWorks options;
wenzelm
parents: 5090
diff changeset
    29
  MLWorks.Internal.Runtime.Memory.gc_message_level := 10;
f2c5354cd32f tuned MLWorks options;
wenzelm
parents: 5090
diff changeset
    30
  (*Is this of any use at all?*)
f2c5354cd32f tuned MLWorks options;
wenzelm
parents: 5090
diff changeset
    31
  Shell.Options.set (Shell.Options.ValuePrinter.showExnDetails, true));
4430
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
    32
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
    33
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
    34
(* Poly/ML emulation *)
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
    35
4424
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    36
(*To exit the system with an exit code -- an alternative to ^D *)
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    37
fun exit 0 = (OS.Process.exit OS.Process.success): unit
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    38
  | exit _ = OS.Process.exit OS.Process.failure;
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    39
fun quit () = exit 0;
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    40
5660
f2c5354cd32f tuned MLWorks options;
wenzelm
parents: 5090
diff changeset
    41
(*limit the printing depth*)
5750
7ab9dd4a8ba6 Warns when stack is extended; better decl of print_depth
paulson
parents: 5703
diff changeset
    42
fun print_depth n = 
7ab9dd4a8ba6 Warns when stack is extended; better decl of print_depth
paulson
parents: 5703
diff changeset
    43
    let open Shell.Options
7ab9dd4a8ba6 Warns when stack is extended; better decl of print_depth
paulson
parents: 5703
diff changeset
    44
    in set (ValuePrinter.maximumDepth, n div 2);
7ab9dd4a8ba6 Warns when stack is extended; better decl of print_depth
paulson
parents: 5703
diff changeset
    45
       set (ValuePrinter.maximumSeqSize, n)
7ab9dd4a8ba6 Warns when stack is extended; better decl of print_depth
paulson
parents: 5703
diff changeset
    46
    end;
4424
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    47
4430
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
    48
(*interface for toplevel pretty printers, see also Pure/install_pp.ML*)
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
    49
(*n.a.*)
4424
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    50
fun make_pp path pprint = ();
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    51
fun install_pp _ = ();
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    52
4977
6cec2c0ffdbf added ml_prompts;
wenzelm
parents: 4973
diff changeset
    53
(*prompts*)
6cec2c0ffdbf added ml_prompts;
wenzelm
parents: 4973
diff changeset
    54
(*n.a.??*)
6cec2c0ffdbf added ml_prompts;
wenzelm
parents: 4973
diff changeset
    55
fun ml_prompts p1 p2 = ();
6cec2c0ffdbf added ml_prompts;
wenzelm
parents: 4973
diff changeset
    56
4424
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    57
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    58
(** Compiler-independent timing functions **)
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    59
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    60
(*Note start point for timing*)
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    61
fun startTiming() = 
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    62
  let val CPUtimer = Timer.startCPUTimer();
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    63
      val time = Timer.checkCPUTimer(CPUtimer)
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    64
  in  (CPUtimer,time)  end;
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    65
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    66
(*Finish timing and return string*)
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    67
fun endTiming (CPUtimer, {gc,sys,usr}) =
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    68
  let open Time  (*...for Time.toString, Time.+ and Time.- *)
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    69
      val {gc=gc2,sys=sys2,usr=usr2} = Timer.checkCPUTimer(CPUtimer)
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    70
  in  "User " ^ toString (usr2-usr) ^
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    71
      "  GC " ^ toString (gc2-gc) ^
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    72
      "  All "^ toString (sys2-sys + usr2-usr + gc2-gc) ^
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    73
      " secs"
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    74
      handle Time => ""
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    75
  end;
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    76
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    77
4430
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
    78
(* ML command execution *)
4424
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    79
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    80
(*Can one redirect 'use' directly to an instream?*)
7855
092a6435afad system;
wenzelm
parents: 6227
diff changeset
    81
fun use_text _ _ txt =
4430
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
    82
  let
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
    83
    val tmp_name = OS.FileSys.tmpName ();
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
    84
    val tmp_file = TextIO.openOut tmp_name;
5038
301c37df931d use_text replaces use_strings;
wenzelm
parents: 4977
diff changeset
    85
  in
301c37df931d use_text replaces use_strings;
wenzelm
parents: 4977
diff changeset
    86
    TextIO.output (tmp_file, txt);
301c37df931d use_text replaces use_strings;
wenzelm
parents: 4977
diff changeset
    87
    TextIO.closeOut tmp_file;
301c37df931d use_text replaces use_strings;
wenzelm
parents: 4977
diff changeset
    88
    use tmp_name;
301c37df931d use_text replaces use_strings;
wenzelm
parents: 4977
diff changeset
    89
    OS.FileSys.remove tmp_name
4424
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    90
  end;
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    91
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    92
4430
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
    93
5812
cbc106ddcc83 simple interrupt_handler;
wenzelm
parents: 5750
diff changeset
    94
(** interrupts **)	(*Note: may get into race conditions*)
cbc106ddcc83 simple interrupt_handler;
wenzelm
parents: 5750
diff changeset
    95
cbc106ddcc83 simple interrupt_handler;
wenzelm
parents: 5750
diff changeset
    96
exception Interrupt;
cbc106ddcc83 simple interrupt_handler;
wenzelm
parents: 5750
diff changeset
    97
cbc106ddcc83 simple interrupt_handler;
wenzelm
parents: 5750
diff changeset
    98
MLWorks.Internal.Runtime.Event.interrupt_handler (fn () => raise Interrupt);
cbc106ddcc83 simple interrupt_handler;
wenzelm
parents: 5750
diff changeset
    99
cbc106ddcc83 simple interrupt_handler;
wenzelm
parents: 5750
diff changeset
   100
fun mask_interrupt f x = f x;           
cbc106ddcc83 simple interrupt_handler;
wenzelm
parents: 5750
diff changeset
   101
fun unmask_interrupt f x = f x;
cbc106ddcc83 simple interrupt_handler;
wenzelm
parents: 5750
diff changeset
   102
fun exhibit_interrupt f x = f x;
cbc106ddcc83 simple interrupt_handler;
wenzelm
parents: 5750
diff changeset
   103
cbc106ddcc83 simple interrupt_handler;
wenzelm
parents: 5750
diff changeset
   104
cbc106ddcc83 simple interrupt_handler;
wenzelm
parents: 5750
diff changeset
   105
4430
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
   106
(** OS related **)
4424
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
   107
4430
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
   108
(* system command execution *)
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
   109
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
   110
(*execute Unix command which doesn't take any input from stdin and
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
   111
  sends its output to stdout; could be done more easily by Unix.execute,
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
   112
  but that function doesn't use the PATH*)
4424
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
   113
fun execute command =
4430
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
   114
  let
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
   115
    val tmp_name = OS.FileSys.tmpName ();
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
   116
    val is = (OS.Process.system (command ^ " > " ^ tmp_name); TextIO.openIn tmp_name);
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
   117
    val result = TextIO.inputAll is;
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
   118
  in
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
   119
    TextIO.closeIn is;
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
   120
    OS.FileSys.remove tmp_name;
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
   121
    result
4424
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
   122
  end;
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
   123
7855
092a6435afad system;
wenzelm
parents: 6227
diff changeset
   124
(*plain version; with return code*)
092a6435afad system;
wenzelm
parents: 6227
diff changeset
   125
fun system cmd =
092a6435afad system;
wenzelm
parents: 6227
diff changeset
   126
  if OS.Process.system cmd = OS.Process.success then 0 else 1;
092a6435afad system;
wenzelm
parents: 6227
diff changeset
   127
4424
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
   128
4430
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
   129
(* file handling *)
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
   130
6227
3198f547f8af fixed file_info;
wenzelm
parents: 5812
diff changeset
   131
(*get time of last modification*)
3198f547f8af fixed file_info;
wenzelm
parents: 5812
diff changeset
   132
fun file_info name = Time.toString (OS.FileSys.modTime name) handle _ => "";
4430
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
   133
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
   134
4424
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
   135
(* getenv *)
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
   136
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
   137
fun getenv var =
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
   138
  (case OS.Process.getEnv var of
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
   139
    NONE => ""
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
   140
  | SOME txt => txt);
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
   141
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
   142
4430
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
   143
(* non-ASCII input (see also Thy/use.ML) *)
4424
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
   144
4430
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
   145
val needs_filtered_use = false;