src/Pure/ML-Systems/mosml.ML
author wenzelm
Tue, 11 Oct 2005 13:28:08 +0200
changeset 17824 36b2978d339a
parent 17491 2bd5a6e26e1e
child 18384 fa38cca42913
permissions -rw-r--r--
added string_of_pid;
     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 ~~/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 "../HOL";
    17 > use "ROOT.ML";
    18 *)
    19 
    20 (** ML system related **)
    21 
    22 load "Obj";
    23 load "Bool";
    24 load "Int";
    25 load "Real";
    26 load "ListPair";
    27 load "OS";
    28 load "Process";
    29 load "FileSys";
    30 
    31 (*low-level pointer equality*)
    32 local val cast : 'a -> int = Obj.magic
    33 in fun pointer_eq (x:'a, y:'a) = (cast x = cast y) end;
    34 
    35 (*proper implementation available?*)
    36 structure IntInf =
    37 struct
    38   open Int;
    39 end;
    40 
    41 structure Real =
    42 struct
    43   open Real;
    44   val realFloor = real o floor;
    45 end;
    46 
    47 structure String =
    48 struct
    49   fun isSuffix s1 s2 =
    50     let val n1 = size s1 and n2 = size s2
    51     in if n1 = n2 then s1 = s2 else n1 <= n2 andalso String.substring (s2, n2 - n1, n1) = s1 end;
    52   open String;
    53 end;
    54 
    55 structure Time =
    56 struct
    57   open Time;
    58   fun toString t = Time.toString t
    59     handle Overflow => Real.toString (Time.toReal t);   (*workaround Y2004 bug*)
    60 end;
    61 
    62 structure OS =
    63 struct
    64   open OS
    65   structure Process = Process
    66   structure FileSys = FileSys
    67 end;
    68 
    69 (*limit the printing depth*)
    70 fun print_depth n =
    71  (Meta.printDepth := n div 2;
    72   Meta.printLength := n);
    73 
    74 (*interface for toplevel pretty printers, see also Pure/install_pp.ML*)
    75 (*the documentation refers to an installPP but I couldn't fine it!*)
    76 fun make_pp path pprint = ();
    77 fun install_pp _ = ();
    78 
    79 (*dummy implementation*)
    80 fun ml_prompts p1 p2 = ();
    81 
    82 (*dummy implementation*)
    83 fun profile (n: int) f x = f x;
    84 
    85 (*dummy impelemtation*)
    86 fun exception_trace f = f ();
    87 
    88 
    89 (** Compiler-independent timing functions **)
    90 
    91 load "Timer";
    92 
    93 use "ML-Systems/cpu-timer-gc.ML";
    94 
    95 (* bounded time execution *)
    96 
    97 (* FIXME proper implementation available? *)
    98 
    99 structure TimeLimit : sig
   100    exception TimeOut
   101    val timeLimit : Time.time -> ('a -> 'b) -> 'a -> 'b
   102 end = struct
   103    exception TimeOut
   104    fun timeLimit t f x =
   105       f x;
   106 end;
   107 
   108 
   109 (* ML command execution *)
   110 
   111 (*Can one redirect 'use' directly to an instream?*)
   112 fun use_text _ _ txt =
   113   let
   114     val tmp_name = FileSys.tmpName ();
   115     val tmp_file = TextIO.openOut tmp_name;
   116   in
   117     TextIO.output (tmp_file, txt);
   118     TextIO.closeOut tmp_file;
   119     use tmp_name;
   120     FileSys.remove tmp_name
   121   end;
   122 
   123 
   124 
   125 (** interrupts **)      (*Note: may get into race conditions*)
   126 
   127 (* FIXME proper implementation available? *)
   128 
   129 exception Interrupt;
   130 
   131 fun ignore_interrupt f x = f x;
   132 fun raise_interrupt f x = f x;
   133 
   134 
   135 
   136 (** OS related **)
   137 
   138 (* system command execution *)
   139 
   140 (*execute Unix command which doesn't take any input from stdin and
   141   sends its output to stdout; could be done more easily by Unix.execute,
   142   but that function doesn't use the PATH*)
   143 fun execute command =
   144   let
   145     val tmp_name = FileSys.tmpName ();
   146     val is = (Process.system (command ^ " > " ^ tmp_name); TextIO.openIn tmp_name);
   147     val result = TextIO.inputAll is;
   148   in
   149     TextIO.closeIn is;
   150     FileSys.remove tmp_name;
   151     result
   152   end;
   153 
   154 (*plain version; with return code*)
   155 fun system cmd =
   156   if Process.system cmd = Process.success then 0 else 1;
   157 
   158 
   159 val string_of_pid = Int.toString;
   160 
   161 
   162 (* getenv *)
   163 
   164 fun getenv var =
   165   (case Process.getEnv var of
   166     NONE => ""
   167   | SOME txt => txt);