src/Pure/ML-Systems/mlworks.ML
author paulson
Wed, 27 May 1998 12:25:56 +0200
changeset 4973 7420178bd2d9
parent 4430 b2c1cf960c53
child 4977 6cec2c0ffdbf
permissions -rw-r--r--
Structure Option now declared in MLWorks
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
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
    21
MLWorks.Internal.Runtime.Event.stack_overflow_handler
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
    22
  (fn () => MLWorks.Internal.Runtime.Memory.max_stack_blocks :=
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
    23
    ! MLWorks.Internal.Runtime.Memory.max_stack_blocks + 20);
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
    24
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
    25
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
    26
(* Poly/ML emulation *)
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
    27
4424
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    28
(*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
    29
fun exit 0 = (OS.Process.exit OS.Process.success): unit
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    30
  | exit _ = OS.Process.exit OS.Process.failure;
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    31
fun quit () = exit 0;
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    32
4430
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
    33
(*n.a.*)
4424
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    34
fun print_depth n = ();
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    35
4430
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
    36
(*interface for toplevel pretty printers, see also Pure/install_pp.ML*)
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
    37
(*n.a.*)
4424
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    38
fun make_pp path pprint = ();
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    39
fun install_pp _ = ();
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    40
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    41
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    42
(** Compiler-independent timing functions **)
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    43
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    44
(*Note start point for timing*)
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    45
fun startTiming() = 
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    46
  let val CPUtimer = Timer.startCPUTimer();
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    47
      val time = Timer.checkCPUTimer(CPUtimer)
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    48
  in  (CPUtimer,time)  end;
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    49
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    50
(*Finish timing and return string*)
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    51
fun endTiming (CPUtimer, {gc,sys,usr}) =
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    52
  let open Time  (*...for Time.toString, Time.+ and Time.- *)
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    53
      val {gc=gc2,sys=sys2,usr=usr2} = Timer.checkCPUTimer(CPUtimer)
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    54
  in  "User " ^ toString (usr2-usr) ^
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    55
      "  GC " ^ toString (gc2-gc) ^
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    56
      "  All "^ toString (sys2-sys + usr2-usr + gc2-gc) ^
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    57
      " secs"
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    58
      handle Time => ""
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    59
  end;
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    60
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    61
4430
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
    62
(* ML command execution *)
4424
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    63
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    64
(*Can one redirect 'use' directly to an instream?*)
4430
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
    65
fun use_strings strs =
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
    66
  let
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
    67
    val tmp_name = OS.FileSys.tmpName ();
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
    68
    val tmp_file = TextIO.openOut tmp_name;
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
    69
  in app (fn s => TextIO.output (tmp_file, s)) strs;
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
    70
     TextIO.closeOut tmp_file;
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
    71
     use tmp_name;
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
    72
     OS.FileSys.remove tmp_name
4424
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    73
  end;
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    74
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    75
4430
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
    76
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
    77
(** OS related **)
4424
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    78
4430
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
    79
(* system command execution *)
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
    80
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
    81
(*execute Unix command which doesn't take any input from stdin and
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
    82
  sends its output to stdout; could be done more easily by Unix.execute,
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
    83
  but that function doesn't use the PATH*)
4424
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    84
fun execute command =
4430
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
    85
  let
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
    86
    val tmp_name = OS.FileSys.tmpName ();
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
    87
    val is = (OS.Process.system (command ^ " > " ^ tmp_name); TextIO.openIn tmp_name);
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
    88
    val result = TextIO.inputAll is;
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
    89
  in
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
    90
    TextIO.closeIn is;
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
    91
    OS.FileSys.remove tmp_name;
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
    92
    result
4424
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    93
  end;
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    94
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
    95
4430
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
    96
(* file handling *)
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
    97
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
    98
(*get time of last modification; if file doesn't exist return an empty string*)
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
    99
fun file_info "" = ""		(* FIXME !? *)
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
   100
  | file_info name = Time.toString (OS.FileSys.modTime name) handle _ => "";
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
   101
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
   102
4424
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
   103
(* getenv *)
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
   104
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
   105
fun getenv var =
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
   106
  (case OS.Process.getEnv var of
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
   107
    NONE => ""
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
   108
  | SOME txt => txt);
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
   109
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
   110
4430
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
   111
(* non-ASCII input (see also Thy/use.ML) *)
4424
1f15655f7307 Compatibility file for MLWorks version 1.0r2 or later.
wenzelm
parents:
diff changeset
   112
4430
b2c1cf960c53 misc improvements;
wenzelm
parents: 4424
diff changeset
   113
val needs_filtered_use = false;