src/Pure/ML-Systems/polyml.ML
author wenzelm
Tue Mar 25 13:18:10 2014 +0100 (2014-03-25 ago)
changeset 56275 600f432ab556
parent 54723 124432e77ecf
child 56281 03c3d1a7c3b8
permissions -rw-r--r--
added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
     1 (*  Title:      Pure/ML-Systems/polyml.ML
     2     Author:     Makarius
     3 
     4 Compatibility wrapper for Poly/ML.
     5 *)
     6 
     7 (* ML name space *)
     8 
     9 structure ML_Name_Space =
    10 struct
    11   open PolyML.NameSpace;
    12   type T = PolyML.NameSpace.nameSpace;
    13   val global = PolyML.globalNameSpace;
    14   val initial_val =
    15     List.filter (fn (a, _) => a <> "use" andalso a <> "exit" andalso a <> "commit")
    16       (#allVal global ());
    17   val initial_type = #allType global ();
    18   val initial_fixity = #allFix global ();
    19   val initial_structure = #allStruct global ();
    20   val initial_signature = #allSig global ();
    21   val initial_functor = #allFunct global ();
    22 end;
    23 
    24 
    25 (* ML system operations *)
    26 
    27 use "ML-Systems/ml_system.ML";
    28 
    29 if ML_System.name = "polyml-5.3.0"
    30 then use "ML-Systems/share_common_data_polyml-5.3.0.ML"
    31 else ();
    32 
    33 structure ML_System =
    34 struct
    35 
    36 open ML_System;
    37 
    38 fun share_common_data () = PolyML.shareCommonData PolyML.rootFunction;
    39 val save_state = PolyML.SaveState.saveState;
    40 
    41 end;
    42 
    43 
    44 (* exceptions *)
    45 
    46 fun reraise exn =
    47   (case PolyML.exceptionLocation exn of
    48     NONE => raise exn
    49   | SOME location => PolyML.raiseWithLocation (exn, location));
    50 
    51 exception Interrupt = SML90.Interrupt;
    52 
    53 use "General/exn.ML";
    54 
    55 
    56 (* multithreading *)
    57 
    58 val seconds = Time.fromReal;
    59 
    60 if List.exists (fn s => s = "SingleAssignment") (PolyML.Compiler.structureNames ())
    61 then ()
    62 else use "ML-Systems/single_assignment_polyml.ML";
    63 
    64 open Thread;
    65 use "ML-Systems/multithreading.ML";
    66 use "ML-Systems/multithreading_polyml.ML";
    67 
    68 use "ML-Systems/unsynchronized.ML";
    69 val _ = PolyML.Compiler.forgetValue "ref";
    70 val _ = PolyML.Compiler.forgetType "ref";
    71 
    72 
    73 (* pervasive environment *)
    74 
    75 fun op before (a, _: unit) = a;
    76 
    77 val _ = PolyML.Compiler.forgetValue "isSome";
    78 val _ = PolyML.Compiler.forgetValue "getOpt";
    79 val _ = PolyML.Compiler.forgetValue "valOf";
    80 val _ = PolyML.Compiler.forgetValue "foldl";
    81 val _ = PolyML.Compiler.forgetValue "foldr";
    82 val _ = PolyML.Compiler.forgetValue "print";
    83 val _ = PolyML.Compiler.forgetValue "explode";
    84 val _ = PolyML.Compiler.forgetValue "concat";
    85 
    86 val ord = SML90.ord;
    87 val chr = SML90.chr;
    88 val raw_explode = SML90.explode;
    89 val implode = SML90.implode;
    90 
    91 val io_buffer_size = 4096;
    92 
    93 fun quit () = exit 0;
    94 
    95 
    96 (* ML runtime system *)
    97 
    98 fun print_exception_trace (_: exn -> string) = PolyML.exception_trace;
    99 val timing = PolyML.timing;
   100 val profiling = PolyML.profiling;
   101 
   102 fun profile 0 f x = f x
   103   | profile n f x =
   104       let
   105         val _ = RunCall.run_call1 RuntimeCalls.POLY_SYS_profiler n;
   106         val res = Exn.capture f x;
   107         val _ = RunCall.run_call1 RuntimeCalls.POLY_SYS_profiler 0;
   108       in Exn.release res end;
   109 
   110 val pointer_eq = PolyML.pointerEq;
   111 
   112 
   113 (* ML compiler *)
   114 
   115 use "ML-Systems/use_context.ML";
   116 use "ML-Systems/compiler_polyml.ML";
   117 
   118 PolyML.Compiler.reportUnreferencedIds := true;
   119 PolyML.Compiler.printInAlphabeticalOrder := false;
   120 PolyML.Compiler.maxInlineSize := 80;
   121 (*PolyML.Compiler.reportExhaustiveHandlers := true;*)
   122 
   123 fun ml_prompts p1 p2 = (PolyML.Compiler.prompt1 := p1; PolyML.Compiler.prompt2 := p2);
   124 
   125 
   126 (* ML toplevel pretty printing *)
   127 
   128 use "ML-Systems/ml_pretty.ML";
   129 
   130 local
   131   val depth = Unsynchronized.ref 10;
   132 in
   133   fun get_print_depth () = ! depth;
   134   fun print_depth n = (depth := n; PolyML.print_depth n);
   135 end;
   136 
   137 val error_depth = PolyML.error_depth;
   138 
   139 val pretty_ml =
   140   let
   141     fun convert _ (PolyML.PrettyBreak (wd, _)) = ML_Pretty.Break (false, wd)
   142       | convert _ (PolyML.PrettyBlock (_, _,
   143             [PolyML.ContextProperty ("fbrk", _)], [PolyML.PrettyString " "])) =
   144           ML_Pretty.Break (true, 1)
   145       | convert len (PolyML.PrettyBlock (ind, _, context, prts)) =
   146           let
   147             fun property name default =
   148               (case List.find (fn PolyML.ContextProperty (a, _) => name = a | _ => false) context of
   149                 SOME (PolyML.ContextProperty (_, b)) => b
   150               | _ => default);
   151             val bg = property "begin" "";
   152             val en = property "end" "";
   153             val len' = property "length" len;
   154           in ML_Pretty.Block ((bg, en), map (convert len') prts, ind) end
   155       | convert len (PolyML.PrettyString s) =
   156           ML_Pretty.String (s, case Int.fromString len of SOME i => i | NONE => size s)
   157   in convert "" end;
   158 
   159 fun ml_pretty (ML_Pretty.Break (false, wd)) = PolyML.PrettyBreak (wd, 0)
   160   | ml_pretty (ML_Pretty.Break (true, _)) =
   161       PolyML.PrettyBlock (0, false, [PolyML.ContextProperty ("fbrk", "")],
   162         [PolyML.PrettyString " "])
   163   | ml_pretty (ML_Pretty.Block ((bg, en), prts, ind)) =
   164       let val context =
   165         (if bg = "" then [] else [PolyML.ContextProperty ("begin", bg)]) @
   166         (if en = "" then [] else [PolyML.ContextProperty ("end", en)])
   167       in PolyML.PrettyBlock (ind, false, context, map ml_pretty prts) end
   168   | ml_pretty (ML_Pretty.String (s, len)) =
   169       if len = size s then PolyML.PrettyString s
   170       else PolyML.PrettyBlock
   171         (0, false, [PolyML.ContextProperty ("length", Int.toString len)], [PolyML.PrettyString s]);
   172 
   173 fun toplevel_pp context (_: string list) pp =
   174   use_text context (1, "pp") false
   175     ("PolyML.addPrettyPrinter (fn _ => fn _ => ml_pretty o Pretty.to_ML o (" ^ pp ^ "))");
   176 
   177 val ml_make_string =
   178   "(fn x => Pretty.string_of (Pretty.from_ML (pretty_ml (PolyML.prettyRepresentation (x, get_print_depth ())))))";
   179