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