src/Pure/ML-Systems/alice.ML
author wenzelm
Sat Aug 18 21:27:52 2007 +0200 (2007-08-18)
changeset 24329 f31594168d27
parent 24290 5607b8b752bb
child 24597 cbf2c5cf335e
permissions -rw-r--r--
ML system provides get_print_depth;
     1 (*  Title:      Pure/ML-Systems/alice.ML
     2     ID:         $Id$
     3 
     4 Compatibility file for Alice 1.4.
     5 
     6 NOTE: there is no wrapper script; may run it interactively as follows:
     7 
     8 $ cd Isabelle/src/Pure
     9 $ env ALICE_JIT_MODE=0 alice
    10 - val ml_system = "alice";
    11 - use "ML-Systems/alice.ML";
    12 - use "ROOT.ML";
    13 - Session.finish ();
    14 *)
    15 
    16 use "ML-Systems/exn.ML";
    17 use "ML-Systems/multithreading_dummy.ML";
    18 
    19 
    20 fun exit 0 = (OS.Process.exit OS.Process.success): unit
    21   | exit _ = OS.Process.exit OS.Process.failure;
    22 
    23 
    24 (** ML system related **)
    25 
    26 (*low-level pointer equality*)
    27 fun pointer_eq (_: 'a, _: 'a) = false;
    28 
    29 
    30 (* restore old-style character / string functions *)
    31 
    32 exception Ord;
    33 fun ord "" = raise Ord
    34   | ord s = Char.ord (String.sub (s, 0));
    35 
    36 val chr = String.str o chr;
    37 val explode = map String.str o String.explode;
    38 val implode = String.concat;
    39 
    40 
    41 (* Poly/ML emulation *)
    42 
    43 fun quit () = exit 0;
    44 
    45 fun get_print_depth () = ! Print.depth;
    46 fun print_depth n = Print.depth := n;
    47 
    48 
    49 (* compiler-independent timing functions *)
    50 
    51 structure Timer =
    52 struct
    53   open Timer;
    54   type cpu_timer = unit;
    55   fun startCPUTimer () = ();
    56   fun checkCPUTimer () = {sys = Time.zeroTime, usr = Time.zeroTime};
    57   fun checkGCTime () = Time.zeroTime;
    58 end;
    59 
    60 fun start_timing () =
    61   let val CPUtimer = Timer.startCPUTimer();
    62       val time = Timer.checkCPUTimer(CPUtimer)
    63   in  (CPUtimer,time)  end;
    64 
    65 fun end_timing (CPUtimer, {sys,usr}) =
    66   let open Time  (*...for Time.toString, Time.+ and Time.- *)
    67       val {sys=sys2,usr=usr2} = Timer.checkCPUTimer(CPUtimer)
    68   in  "User " ^ toString (usr2-usr) ^
    69       "  All "^ toString (sys2-sys + usr2-usr) ^
    70       " secs"
    71       handle Time => ""
    72   end;
    73 
    74 fun check_timer timer =
    75   let
    76     val {sys, usr} = Timer.checkCPUTimer timer;
    77     val gc = Timer.checkGCTime timer;    (* FIXME already included in usr? *)
    78   in (sys, usr, gc) end;
    79 
    80 
    81 (*prompts*)
    82 fun ml_prompts p1 p2 = ();
    83 
    84 (*dummy implementation*)
    85 fun profile (n: int) f x = f x;
    86 
    87 (*dummy implementation*)
    88 fun exception_trace f = f ();
    89 
    90 (*dummy implementation*)
    91 fun print x = x;
    92 
    93 
    94 (* toplevel pretty printing (see also Pure/pure_setup.ML) *)
    95 
    96 fun make_pp path pprint = (path, pprint);
    97 fun install_pp (path, pp) = ();
    98 
    99 
   100 (* ML command execution *)
   101 
   102 fun use_text name (print, err) verbose txt = (Compiler.eval txt; ());
   103 
   104 fun use_file _ _ name = use name;
   105 
   106 
   107 
   108 (** interrupts **)
   109 
   110 exception Interrupt;
   111 
   112 fun ignore_interrupt f x = f x;
   113 fun raise_interrupt f x = f x;
   114 
   115 
   116 (* basis library fixes *)
   117 
   118 structure TextIO =
   119 struct
   120   open TextIO;
   121   fun inputLine is = TextIO.inputLine is
   122     handle IO.Io _ => raise Interrupt;
   123 end;
   124 
   125 
   126 (* bounded time execution *)
   127 
   128 (*dummy implementation*)
   129 fun interrupt_timeout time f x =
   130   f x;
   131 
   132 
   133 
   134 (** OS related **)
   135 
   136 (* current directory *)
   137 
   138 val cd = OS.FileSys.chDir;
   139 val pwd = OS.FileSys.getDir;
   140 
   141 
   142 (* system command execution *)
   143 
   144 (*execute Unix command which doesn't take any input from stdin and
   145   sends its output to stdout; could be done more easily by Unix.execute,
   146   but that function doesn't use the PATH*)
   147 fun execute command =
   148   let
   149     val tmp_name = OS.FileSys.tmpName ();
   150     val is = (OS.Process.system (command ^ " > " ^ tmp_name); TextIO.openIn tmp_name);
   151     val result = TextIO.inputAll is;
   152   in
   153     TextIO.closeIn is;
   154     OS.FileSys.remove tmp_name;
   155     result
   156   end;
   157 
   158 (*plain version; with return code*)
   159 val system = OS.Process.system: string -> int;
   160 
   161 structure OS =
   162 struct
   163   open OS;
   164   structure FileSys =
   165   struct
   166     fun fileId name =
   167       (case execute ("perl -e '@_ = stat(q:" ^ name ^ ":); print $_[1]'") of
   168         "" => raise Fail "OS.FileSys.fileId"   (* FIXME IO.Io!? *)
   169       | s => (case Int.fromString s of NONE => raise Fail "OS.FileSys.fileId" | SOME i => i));
   170     val compare = Int.compare;
   171     open FileSys;
   172   end;
   173 end;
   174 
   175 
   176 (* getenv *)
   177 
   178 fun getenv var =
   179   (case OS.Process.getEnv var of
   180     NONE => ""
   181   | SOME txt => txt);