src/Pure/ML-Systems/mosml.ML
author wenzelm
Fri, 28 Mar 2008 22:39:47 +0100
changeset 26474 94735cff132c
parent 26223 f4a1a96cc07c
child 26504 6e87c0a60104
permissions -rw-r--r--
added forget_structure;
     1 (*  Title:      Pure/ML-Systems/mosml.ML
     2     ID:         $Id$
     3     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
     4     Copyright   1999  University of Cambridge
     5 
     6 Compatibility file for Moscow ML 2.01
     7 
     8 NOTE: saving images does not work; may run it interactively as follows:
     9 
    10 $ cd Isabelle/src/Pure
    11 $ isabelle RAW_ML_SYSTEM
    12 > val ml_system = "mosml";
    13 > use "ML-Systems/mosml.ML";
    14 > use "ROOT.ML";
    15 > Session.finish ();
    16 > cd "../FOL";
    17 > use "ROOT.ML";
    18 > Session.finish ();
    19 > cd "../ZF";
    20 > use "ROOT.ML";
    21 *)
    22 
    23 (** ML system related **)
    24 
    25 val ml_system_fix_ints = false;
    26 
    27 fun forget_structure _ = ();
    28 
    29 load "Obj";
    30 load "Bool";
    31 load "Int";
    32 load "Real";
    33 load "ListPair";
    34 load "OS";
    35 load "Process";
    36 load "FileSys";
    37 load "IO";
    38 
    39 use "ML-Systems/exn.ML";
    40 use "ML-Systems/universal.ML";
    41 use "ML-Systems/multithreading.ML";
    42 use "ML-Systems/time_limit.ML";
    43 
    44 
    45 (*low-level pointer equality*)
    46 local val cast : 'a -> int = Obj.magic
    47 in fun pointer_eq (x:'a, y:'a) = (cast x = cast y) end;
    48 
    49 (*proper implementation available?*)
    50 structure IntInf =
    51 struct
    52   fun divMod (x, y) = (x div y, x mod y);
    53   open Int;
    54 end;
    55 
    56 structure Real =
    57 struct
    58   open Real;
    59   val realFloor = real o floor;
    60 end;
    61 
    62 structure String =
    63 struct
    64   fun isSuffix s1 s2 =
    65     let val n1 = size s1 and n2 = size s2
    66     in if n1 = n2 then s1 = s2 else n1 <= n2 andalso String.substring (s2, n2 - n1, n1) = s1 end;
    67   open String;
    68 end;
    69 
    70 structure Time =
    71 struct
    72   open Time;
    73   fun toString t = Time.toString t
    74     handle Overflow => Real.toString (Time.toReal t);   (*workaround Y2004 bug*)
    75 end;
    76 
    77 structure OS =
    78 struct
    79   open OS
    80   structure Process = Process
    81   structure FileSys = FileSys
    82 end;
    83 
    84 exception Option = Option.Option;
    85 
    86 
    87 (*limit the printing depth*)
    88 local
    89   val depth = ref 10;
    90 in
    91   fun get_print_depth () = ! depth;
    92   fun print_depth n =
    93    (depth := n;
    94     Meta.printDepth := n div 2;
    95     Meta.printLength := n);
    96 end;
    97 
    98 (*interface for toplevel pretty printers, see also Pure/pure_setup.ML*)
    99 (*the documentation refers to an installPP but I couldn't fine it!*)
   100 fun make_pp path pprint = ();
   101 fun install_pp _ = ();
   102 
   103 (*dummy implementation*)
   104 fun ml_prompts p1 p2 = ();
   105 
   106 (*dummy implementation*)
   107 fun profile (n: int) f x = f x;
   108 
   109 (*dummy implementation*)
   110 fun exception_trace f = f ();
   111 
   112 (*dummy implementation*)
   113 fun print x = x;
   114 
   115 
   116 (** Compiler-independent timing functions **)
   117 
   118 load "Timer";
   119 
   120 fun start_timing () =
   121   let val CPUtimer = Timer.startCPUTimer();
   122       val time = Timer.checkCPUTimer(CPUtimer)
   123   in  (CPUtimer,time)  end;
   124 
   125 fun end_timing (CPUtimer, {gc,sys,usr}) =
   126   let open Time  (*...for Time.toString, Time.+ and Time.- *)
   127       val {gc=gc2,sys=sys2,usr=usr2} = Timer.checkCPUTimer(CPUtimer)
   128   in  "User " ^ toString (usr2-usr) ^
   129       "  GC " ^ toString (gc2-gc) ^
   130       "  All "^ toString (sys2-sys + usr2-usr + gc2-gc) ^
   131       " secs"
   132       handle Time => ""
   133   end;
   134 
   135 fun check_timer timer =
   136   let val {sys, usr, gc} = Timer.checkCPUTimer timer
   137   in (sys, usr, gc) end;
   138 
   139 
   140 (* SML basis library fixes *)
   141 
   142 structure TextIO =
   143 struct
   144   fun canInput _ = raise IO.Io {cause = Fail "undefined", function = "canInput", name = ""};
   145   open TextIO;
   146   fun inputLine is =
   147     let val s = TextIO.inputLine is
   148     in if s = "" then NONE else SOME s end;
   149 end;
   150 
   151 
   152 (* ML command execution *)
   153 
   154 (*Can one redirect 'use' directly to an instream?*)
   155 fun use_text (tune: string -> string) _ _ _ txt =
   156   let
   157     val tmp_name = FileSys.tmpName ();
   158     val tmp_file = TextIO.openOut tmp_name;
   159   in
   160     TextIO.output (tmp_file, tune txt);
   161     TextIO.closeOut tmp_file;
   162     use tmp_name;
   163     FileSys.remove tmp_name
   164   end;
   165 
   166 fun use_file _ _ _ name = use name;
   167 
   168 
   169 
   170 (** interrupts **)      (*Note: may get into race conditions*)
   171 
   172 (* FIXME proper implementation available? *)
   173 
   174 exception Interrupt;
   175 
   176 fun interruptible f x = f x;
   177 fun uninterruptible f x = f (fn (g: 'c -> 'd) => g) x;
   178 
   179 
   180 
   181 (** OS related **)
   182 
   183 (*dummy implementation*)
   184 structure Posix =
   185 struct
   186   structure ProcEnv =
   187   struct
   188     fun getpid () = 0;
   189   end;  
   190 end;
   191 
   192 local
   193 
   194 fun read_file name =
   195   let val is = TextIO.openIn name
   196   in TextIO.inputAll is before TextIO.closeIn is end;
   197 
   198 fun write_file name txt =
   199   let val os = TextIO.openOut name
   200   in TextIO.output (os, txt) before TextIO.closeOut os end;
   201 
   202 in
   203 
   204 fun system_out script =
   205   let
   206     val script_name = OS.FileSys.tmpName ();
   207     val _ = write_file script_name script;
   208 
   209     val output_name = OS.FileSys.tmpName ();
   210 
   211     val status =
   212       OS.Process.system ("perl -w \"$ISABELLE_HOME/lib/scripts/system.pl\" nogroup " ^
   213         script_name ^ " /dev/null " ^ output_name);
   214     val rc = if status = OS.Process.success then 0 else 1;
   215 
   216     val output = read_file output_name handle IO.Io _ => "";
   217     val _ = OS.FileSys.remove script_name handle OS.SysErr _ => ();
   218     val _ = OS.FileSys.remove output_name handle OS.SysErr _ => ();
   219   in (output, rc) end;
   220 
   221 end;
   222 
   223 val cd = OS.FileSys.chDir;
   224 val pwd = OS.FileSys.getDir;
   225 
   226 val string_of_pid = Int.toString;
   227 
   228 fun getenv var =
   229   (case Process.getEnv var of
   230     NONE => ""
   231   | SOME txt => txt);