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