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