src/Pure/ML/exn_properties.ML
author wenzelm
Thu, 23 Jan 2025 22:19:30 +0100
changeset 81960 326ecfc079a6
parent 78764 a3dcae9a2ebe
permissions -rw-r--r--
support for @{instantiate (no_beta) ...};
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
62355
00f7618a9f2b clarified file names;
wenzelm
parents: 61138
diff changeset
     1
(*  Title:      Pure/ML/exn_properties.ML
50911
ee7fe4230642 more explicit treatment of (optional) exception properties, notably for "serial" -- avoid conflict with startPosition = offset;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
ee7fe4230642 more explicit treatment of (optional) exception properties, notably for "serial" -- avoid conflict with startPosition = offset;
wenzelm
parents:
diff changeset
     3
62355
00f7618a9f2b clarified file names;
wenzelm
parents: 61138
diff changeset
     4
Exception properties.
50911
ee7fe4230642 more explicit treatment of (optional) exception properties, notably for "serial" -- avoid conflict with startPosition = offset;
wenzelm
parents:
diff changeset
     5
*)
ee7fe4230642 more explicit treatment of (optional) exception properties, notably for "serial" -- avoid conflict with startPosition = offset;
wenzelm
parents:
diff changeset
     6
ee7fe4230642 more explicit treatment of (optional) exception properties, notably for "serial" -- avoid conflict with startPosition = offset;
wenzelm
parents:
diff changeset
     7
signature EXN_PROPERTIES =
ee7fe4230642 more explicit treatment of (optional) exception properties, notably for "serial" -- avoid conflict with startPosition = offset;
wenzelm
parents:
diff changeset
     8
sig
62821
48c24d0b6d85 tuned signature;
wenzelm
parents: 62518
diff changeset
     9
  val position_of_polyml_location: ML_Compiler0.polyml_location -> Position.T
62516
5732f1c31566 tuned signature -- clarified modules;
wenzelm
parents: 62468
diff changeset
    10
  val position: exn -> Position.T
50911
ee7fe4230642 more explicit treatment of (optional) exception properties, notably for "serial" -- avoid conflict with startPosition = offset;
wenzelm
parents:
diff changeset
    11
  val get: exn -> Properties.T
78679
dc7455787a8e clarified modules;
wenzelm
parents: 77776
diff changeset
    12
  val identify: Properties.T -> exn -> exn
dc7455787a8e clarified modules;
wenzelm
parents: 77776
diff changeset
    13
  val the_serial: exn -> int
dc7455787a8e clarified modules;
wenzelm
parents: 77776
diff changeset
    14
  val ord: exn ord
50911
ee7fe4230642 more explicit treatment of (optional) exception properties, notably for "serial" -- avoid conflict with startPosition = offset;
wenzelm
parents:
diff changeset
    15
end;
ee7fe4230642 more explicit treatment of (optional) exception properties, notably for "serial" -- avoid conflict with startPosition = offset;
wenzelm
parents:
diff changeset
    16
ee7fe4230642 more explicit treatment of (optional) exception properties, notably for "serial" -- avoid conflict with startPosition = offset;
wenzelm
parents:
diff changeset
    17
structure Exn_Properties: EXN_PROPERTIES =
ee7fe4230642 more explicit treatment of (optional) exception properties, notably for "serial" -- avoid conflict with startPosition = offset;
wenzelm
parents:
diff changeset
    18
struct
ee7fe4230642 more explicit treatment of (optional) exception properties, notably for "serial" -- avoid conflict with startPosition = offset;
wenzelm
parents:
diff changeset
    19
56303
4cc3f4db3447 clarified Isabelle/ML bootstrap, such that Execution does not require ML_Compiler;
wenzelm
parents: 50911
diff changeset
    20
(* source locations *)
4cc3f4db3447 clarified Isabelle/ML bootstrap, such that Execution does not require ML_Compiler;
wenzelm
parents: 50911
diff changeset
    21
62821
48c24d0b6d85 tuned signature;
wenzelm
parents: 62518
diff changeset
    22
fun props_of_polyml_location (loc: ML_Compiler0.polyml_location) =
50911
ee7fe4230642 more explicit treatment of (optional) exception properties, notably for "serial" -- avoid conflict with startPosition = offset;
wenzelm
parents:
diff changeset
    23
  (case YXML.parse_body (#file loc) of
ee7fe4230642 more explicit treatment of (optional) exception properties, notably for "serial" -- avoid conflict with startPosition = offset;
wenzelm
parents:
diff changeset
    24
    [] => []
58714
ca0b7d7cc9a3 suppress special Poly/ML location inBasis (see $ML_SOURCES/mlsource/MLCompiler/STRUCT_VALS.ML);
wenzelm
parents: 56303
diff changeset
    25
  | [XML.Text file] =>
ca0b7d7cc9a3 suppress special Poly/ML location inBasis (see $ML_SOURCES/mlsource/MLCompiler/STRUCT_VALS.ML);
wenzelm
parents: 56303
diff changeset
    26
      if file = "Standard Basis" then []
62468
d97e13e5ea5b clarified modules;
wenzelm
parents: 62387
diff changeset
    27
      else [(Markup.fileN, ML_System.standard_path file)]
50911
ee7fe4230642 more explicit treatment of (optional) exception properties, notably for "serial" -- avoid conflict with startPosition = offset;
wenzelm
parents:
diff changeset
    28
  | body => XML.Decode.properties body);
ee7fe4230642 more explicit treatment of (optional) exception properties, notably for "serial" -- avoid conflict with startPosition = offset;
wenzelm
parents:
diff changeset
    29
62821
48c24d0b6d85 tuned signature;
wenzelm
parents: 62518
diff changeset
    30
fun position_of_polyml_location loc =
77776
58e53c61f15f more compact data;
wenzelm
parents: 71250
diff changeset
    31
  Position.of_props
62387
ad3eb2889f9a support for polyml-git ec49a49972c5 (branch FixedPrecisionInt);
wenzelm
parents: 62355
diff changeset
    32
   {line = FixedInt.toInt (#startLine loc),
ad3eb2889f9a support for polyml-git ec49a49972c5 (branch FixedPrecisionInt);
wenzelm
parents: 62355
diff changeset
    33
    offset = FixedInt.toInt (#startPosition loc),
ad3eb2889f9a support for polyml-git ec49a49972c5 (branch FixedPrecisionInt);
wenzelm
parents: 62355
diff changeset
    34
    end_offset = FixedInt.toInt (#endPosition loc),
62821
48c24d0b6d85 tuned signature;
wenzelm
parents: 62518
diff changeset
    35
    props = props_of_polyml_location loc};
62516
5732f1c31566 tuned signature -- clarified modules;
wenzelm
parents: 62468
diff changeset
    36
5732f1c31566 tuned signature -- clarified modules;
wenzelm
parents: 62468
diff changeset
    37
fun position exn =
62821
48c24d0b6d85 tuned signature;
wenzelm
parents: 62518
diff changeset
    38
  (case Exn.polyml_location exn of
62516
5732f1c31566 tuned signature -- clarified modules;
wenzelm
parents: 62468
diff changeset
    39
    NONE => Position.none
62821
48c24d0b6d85 tuned signature;
wenzelm
parents: 62518
diff changeset
    40
  | SOME loc => position_of_polyml_location loc);
56303
4cc3f4db3447 clarified Isabelle/ML bootstrap, such that Execution does not require ML_Compiler;
wenzelm
parents: 50911
diff changeset
    41
4cc3f4db3447 clarified Isabelle/ML bootstrap, such that Execution does not require ML_Compiler;
wenzelm
parents: 50911
diff changeset
    42
4cc3f4db3447 clarified Isabelle/ML bootstrap, such that Execution does not require ML_Compiler;
wenzelm
parents: 50911
diff changeset
    43
(* exception properties *)
4cc3f4db3447 clarified Isabelle/ML bootstrap, such that Execution does not require ML_Compiler;
wenzelm
parents: 50911
diff changeset
    44
50911
ee7fe4230642 more explicit treatment of (optional) exception properties, notably for "serial" -- avoid conflict with startPosition = offset;
wenzelm
parents:
diff changeset
    45
fun get exn =
62821
48c24d0b6d85 tuned signature;
wenzelm
parents: 62518
diff changeset
    46
  (case Exn.polyml_location exn of
50911
ee7fe4230642 more explicit treatment of (optional) exception properties, notably for "serial" -- avoid conflict with startPosition = offset;
wenzelm
parents:
diff changeset
    47
    NONE => []
62821
48c24d0b6d85 tuned signature;
wenzelm
parents: 62518
diff changeset
    48
  | SOME loc => props_of_polyml_location loc);
50911
ee7fe4230642 more explicit treatment of (optional) exception properties, notably for "serial" -- avoid conflict with startPosition = offset;
wenzelm
parents:
diff changeset
    49
ee7fe4230642 more explicit treatment of (optional) exception properties, notably for "serial" -- avoid conflict with startPosition = offset;
wenzelm
parents:
diff changeset
    50
fun update entries exn =
78680
61a6b4b81d6e clarified signature;
wenzelm
parents: 78679
diff changeset
    51
  let
61a6b4b81d6e clarified signature;
wenzelm
parents: 78679
diff changeset
    52
    val loc =
61a6b4b81d6e clarified signature;
wenzelm
parents: 78679
diff changeset
    53
      the_default {file = "", startLine = 0, endLine = 0, startPosition = 0, endPosition = 0}
61a6b4b81d6e clarified signature;
wenzelm
parents: 78679
diff changeset
    54
        (Exn.polyml_location exn);
61a6b4b81d6e clarified signature;
wenzelm
parents: 78679
diff changeset
    55
    val props = props_of_polyml_location loc;
61a6b4b81d6e clarified signature;
wenzelm
parents: 78679
diff changeset
    56
    val props' = fold Properties.put entries props;
61a6b4b81d6e clarified signature;
wenzelm
parents: 78679
diff changeset
    57
  in
61a6b4b81d6e clarified signature;
wenzelm
parents: 78679
diff changeset
    58
    if props = props' then exn
61a6b4b81d6e clarified signature;
wenzelm
parents: 78679
diff changeset
    59
    else
61a6b4b81d6e clarified signature;
wenzelm
parents: 78679
diff changeset
    60
      let
61a6b4b81d6e clarified signature;
wenzelm
parents: 78679
diff changeset
    61
        val loc' =
61a6b4b81d6e clarified signature;
wenzelm
parents: 78679
diff changeset
    62
          {file = YXML.string_of_body (XML.Encode.properties props'),
61a6b4b81d6e clarified signature;
wenzelm
parents: 78679
diff changeset
    63
            startLine = #startLine loc, endLine = #endLine loc,
61a6b4b81d6e clarified signature;
wenzelm
parents: 78679
diff changeset
    64
            startPosition = #startPosition loc, endPosition = #endPosition loc};
61a6b4b81d6e clarified signature;
wenzelm
parents: 78679
diff changeset
    65
      in
78720
909dc00766a0 clarified signature;
wenzelm
parents: 78680
diff changeset
    66
        Thread_Attributes.uninterruptible_body (fn _ =>
909dc00766a0 clarified signature;
wenzelm
parents: 78680
diff changeset
    67
          PolyML.raiseWithLocation (exn, loc') handle exn' => exn')
78680
61a6b4b81d6e clarified signature;
wenzelm
parents: 78679
diff changeset
    68
      end
61a6b4b81d6e clarified signature;
wenzelm
parents: 78679
diff changeset
    69
  end;
50911
ee7fe4230642 more explicit treatment of (optional) exception properties, notably for "serial" -- avoid conflict with startPosition = offset;
wenzelm
parents:
diff changeset
    70
78679
dc7455787a8e clarified modules;
wenzelm
parents: 77776
diff changeset
    71
dc7455787a8e clarified modules;
wenzelm
parents: 77776
diff changeset
    72
(* identification via serial numbers *)
dc7455787a8e clarified modules;
wenzelm
parents: 77776
diff changeset
    73
dc7455787a8e clarified modules;
wenzelm
parents: 77776
diff changeset
    74
fun identify default_props exn =
78764
a3dcae9a2ebe distinguish proper interrupts from Poly/ML RTS breakdown;
wenzelm
parents: 78720
diff changeset
    75
  if Exn.is_interrupt_proper exn then exn
78680
61a6b4b81d6e clarified signature;
wenzelm
parents: 78679
diff changeset
    76
  else
61a6b4b81d6e clarified signature;
wenzelm
parents: 78679
diff changeset
    77
    let
61a6b4b81d6e clarified signature;
wenzelm
parents: 78679
diff changeset
    78
      val props = get exn;
61a6b4b81d6e clarified signature;
wenzelm
parents: 78679
diff changeset
    79
      val update_serial =
61a6b4b81d6e clarified signature;
wenzelm
parents: 78679
diff changeset
    80
        if Properties.defined props Markup.serialN then []
61a6b4b81d6e clarified signature;
wenzelm
parents: 78679
diff changeset
    81
        else [(Markup.serialN, serial_string ())];
61a6b4b81d6e clarified signature;
wenzelm
parents: 78679
diff changeset
    82
      val update_props = filter_out (Properties.defined props o #1) default_props;
61a6b4b81d6e clarified signature;
wenzelm
parents: 78679
diff changeset
    83
    in update (update_serial @ update_props) exn end;
78679
dc7455787a8e clarified modules;
wenzelm
parents: 77776
diff changeset
    84
dc7455787a8e clarified modules;
wenzelm
parents: 77776
diff changeset
    85
fun the_serial exn =
dc7455787a8e clarified modules;
wenzelm
parents: 77776
diff changeset
    86
  Value.parse_int (the (Properties.get (get exn) Markup.serialN));
dc7455787a8e clarified modules;
wenzelm
parents: 77776
diff changeset
    87
dc7455787a8e clarified modules;
wenzelm
parents: 77776
diff changeset
    88
val ord = rev_order o int_ord o apply2 the_serial;
dc7455787a8e clarified modules;
wenzelm
parents: 77776
diff changeset
    89
50911
ee7fe4230642 more explicit treatment of (optional) exception properties, notably for "serial" -- avoid conflict with startPosition = offset;
wenzelm
parents:
diff changeset
    90
end;