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