src/Pure/Syntax/term_position.ML
author wenzelm
Sat, 07 Dec 2024 15:00:43 +0100
changeset 81552 4717d3bf5752
parent 81251 89ea66c2045b
child 81558 b57996a0688c
permissions -rw-r--r--
clarified signature and caching;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
42264
b6c1b0c4c511 separate structure Term_Position;
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/Syntax/term_position.ML
b6c1b0c4c511 separate structure Term_Position;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
b6c1b0c4c511 separate structure Term_Position;
wenzelm
parents:
diff changeset
     3
81251
89ea66c2045b clarified position constraints: omit redundant information, e.g. for implicit object-logic judgment;
wenzelm
parents: 81232
diff changeset
     4
Reported positions within term syntax trees (non-empty list).
42264
b6c1b0c4c511 separate structure Term_Position;
wenzelm
parents:
diff changeset
     5
*)
b6c1b0c4c511 separate structure Term_Position;
wenzelm
parents:
diff changeset
     6
b6c1b0c4c511 separate structure Term_Position;
wenzelm
parents:
diff changeset
     7
signature TERM_POSITION =
b6c1b0c4c511 separate structure Term_Position;
wenzelm
parents:
diff changeset
     8
sig
81218
94bace5078ba support multiple positions (non-empty list);
wenzelm
parents: 80875
diff changeset
     9
  val pretty: Position.T list -> Pretty.T
94bace5078ba support multiple positions (non-empty list);
wenzelm
parents: 80875
diff changeset
    10
  val encode: Position.T list -> string
81232
9867b5cf3f7a more concise representation of term positions;
wenzelm
parents: 81218
diff changeset
    11
  val detect: string -> bool
81218
94bace5078ba support multiple positions (non-empty list);
wenzelm
parents: 80875
diff changeset
    12
  val decode: string -> Position.T list
81232
9867b5cf3f7a more concise representation of term positions;
wenzelm
parents: 81218
diff changeset
    13
  val detect_position: term -> bool
81218
94bace5078ba support multiple positions (non-empty list);
wenzelm
parents: 80875
diff changeset
    14
  val decode_position: term -> (Position.T list * typ) option
94bace5078ba support multiple positions (non-empty list);
wenzelm
parents: 80875
diff changeset
    15
  val decode_position1: term -> Position.T option
81232
9867b5cf3f7a more concise representation of term positions;
wenzelm
parents: 81218
diff changeset
    16
  val detect_positionT: typ -> bool
81218
94bace5078ba support multiple positions (non-empty list);
wenzelm
parents: 80875
diff changeset
    17
  val decode_positionT: typ -> Position.T list
49685
4341e4d86872 allow position constraints to coexist with 0 or 1 sort constraints;
wenzelm
parents: 49674
diff changeset
    18
  val decode_positionS: sort -> Position.T list * sort
52175
626a757d3c2d uniform Term_Position.markers (cf. dbadb4d03cbc);
wenzelm
parents: 50201
diff changeset
    19
  val markers: string list
42264
b6c1b0c4c511 separate structure Term_Position;
wenzelm
parents:
diff changeset
    20
  val strip_positions: term -> term
b6c1b0c4c511 separate structure Term_Position;
wenzelm
parents:
diff changeset
    21
end;
b6c1b0c4c511 separate structure Term_Position;
wenzelm
parents:
diff changeset
    22
b6c1b0c4c511 separate structure Term_Position;
wenzelm
parents:
diff changeset
    23
structure Term_Position: TERM_POSITION =
b6c1b0c4c511 separate structure Term_Position;
wenzelm
parents:
diff changeset
    24
struct
b6c1b0c4c511 separate structure Term_Position;
wenzelm
parents:
diff changeset
    25
b6c1b0c4c511 separate structure Term_Position;
wenzelm
parents:
diff changeset
    26
(* markup *)
b6c1b0c4c511 separate structure Term_Position;
wenzelm
parents:
diff changeset
    27
b6c1b0c4c511 separate structure Term_Position;
wenzelm
parents:
diff changeset
    28
val position_dummy = "<position>";
b6c1b0c4c511 separate structure Term_Position;
wenzelm
parents:
diff changeset
    29
val position_text = XML.Text position_dummy;
b6c1b0c4c511 separate structure Term_Position;
wenzelm
parents:
diff changeset
    30
81218
94bace5078ba support multiple positions (non-empty list);
wenzelm
parents: 80875
diff changeset
    31
fun pretty ps = Pretty.marks_str (map Position.markup ps, position_dummy);
42264
b6c1b0c4c511 separate structure Term_Position;
wenzelm
parents:
diff changeset
    32
81218
94bace5078ba support multiple positions (non-empty list);
wenzelm
parents: 80875
diff changeset
    33
fun encode_pos pos = XML.Elem (Position.markup pos, [position_text]);
42264
b6c1b0c4c511 separate structure Term_Position;
wenzelm
parents:
diff changeset
    34
81218
94bace5078ba support multiple positions (non-empty list);
wenzelm
parents: 80875
diff changeset
    35
fun decode_pos (XML.Elem ((name, props), [arg])) =
50201
c26369c9eda6 Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents: 49685
diff changeset
    36
      if name = Markup.positionN andalso arg = position_text
42264
b6c1b0c4c511 separate structure Term_Position;
wenzelm
parents:
diff changeset
    37
      then SOME (Position.of_properties props)
b6c1b0c4c511 separate structure Term_Position;
wenzelm
parents:
diff changeset
    38
      else NONE
81218
94bace5078ba support multiple positions (non-empty list);
wenzelm
parents: 80875
diff changeset
    39
  | decode_pos _ = NONE;
94bace5078ba support multiple positions (non-empty list);
wenzelm
parents: 80875
diff changeset
    40
81232
9867b5cf3f7a more concise representation of term positions;
wenzelm
parents: 81218
diff changeset
    41
9867b5cf3f7a more concise representation of term positions;
wenzelm
parents: 81218
diff changeset
    42
(* encode *)
9867b5cf3f7a more concise representation of term positions;
wenzelm
parents: 81218
diff changeset
    43
9867b5cf3f7a more concise representation of term positions;
wenzelm
parents: 81218
diff changeset
    44
val encode_none = YXML.string_of (encode_pos Position.none);
9867b5cf3f7a more concise representation of term positions;
wenzelm
parents: 81218
diff changeset
    45
81218
94bace5078ba support multiple positions (non-empty list);
wenzelm
parents: 80875
diff changeset
    46
fun encode ps =
81251
89ea66c2045b clarified position constraints: omit redundant information, e.g. for implicit object-logic judgment;
wenzelm
parents: 81232
diff changeset
    47
  (case filter Position.is_reported ps of
81232
9867b5cf3f7a more concise representation of term positions;
wenzelm
parents: 81218
diff changeset
    48
    [] => encode_none
81251
89ea66c2045b clarified position constraints: omit redundant information, e.g. for implicit object-logic judgment;
wenzelm
parents: 81232
diff changeset
    49
  | ps' => YXML.string_of_body (map encode_pos ps'));
81232
9867b5cf3f7a more concise representation of term positions;
wenzelm
parents: 81218
diff changeset
    50
9867b5cf3f7a more concise representation of term positions;
wenzelm
parents: 81218
diff changeset
    51
val encode_prefix = YXML.XY ^ Markup.positionN;
9867b5cf3f7a more concise representation of term positions;
wenzelm
parents: 81218
diff changeset
    52
9867b5cf3f7a more concise representation of term positions;
wenzelm
parents: 81218
diff changeset
    53
9867b5cf3f7a more concise representation of term positions;
wenzelm
parents: 81218
diff changeset
    54
(* decode *)
9867b5cf3f7a more concise representation of term positions;
wenzelm
parents: 81218
diff changeset
    55
9867b5cf3f7a more concise representation of term positions;
wenzelm
parents: 81218
diff changeset
    56
val detect = String.isPrefix encode_prefix;
81218
94bace5078ba support multiple positions (non-empty list);
wenzelm
parents: 80875
diff changeset
    57
94bace5078ba support multiple positions (non-empty list);
wenzelm
parents: 80875
diff changeset
    58
fun decode str =
81232
9867b5cf3f7a more concise representation of term positions;
wenzelm
parents: 81218
diff changeset
    59
  if str = encode_none then [Position.none]
9867b5cf3f7a more concise representation of term positions;
wenzelm
parents: 81218
diff changeset
    60
  else if detect str then
9867b5cf3f7a more concise representation of term positions;
wenzelm
parents: 81218
diff changeset
    61
    let
9867b5cf3f7a more concise representation of term positions;
wenzelm
parents: 81218
diff changeset
    62
      val xml = YXML.parse_body str handle Fail msg => error msg;
9867b5cf3f7a more concise representation of term positions;
wenzelm
parents: 81218
diff changeset
    63
      val ps = map decode_pos xml;
9867b5cf3f7a more concise representation of term positions;
wenzelm
parents: 81218
diff changeset
    64
    in
9867b5cf3f7a more concise representation of term positions;
wenzelm
parents: 81218
diff changeset
    65
      if not (null ps) andalso forall is_some ps then map the ps
9867b5cf3f7a more concise representation of term positions;
wenzelm
parents: 81218
diff changeset
    66
      else if forall is_none ps then []
9867b5cf3f7a more concise representation of term positions;
wenzelm
parents: 81218
diff changeset
    67
      else error ("Bad encoded positions: " ^ quote str)
9867b5cf3f7a more concise representation of term positions;
wenzelm
parents: 81218
diff changeset
    68
    end
9867b5cf3f7a more concise representation of term positions;
wenzelm
parents: 81218
diff changeset
    69
  else [];
42264
b6c1b0c4c511 separate structure Term_Position;
wenzelm
parents:
diff changeset
    70
b6c1b0c4c511 separate structure Term_Position;
wenzelm
parents:
diff changeset
    71
b6c1b0c4c511 separate structure Term_Position;
wenzelm
parents:
diff changeset
    72
(* positions within parse trees *)
b6c1b0c4c511 separate structure Term_Position;
wenzelm
parents:
diff changeset
    73
81232
9867b5cf3f7a more concise representation of term positions;
wenzelm
parents: 81218
diff changeset
    74
fun detect_position (Free (x, _)) = detect x
9867b5cf3f7a more concise representation of term positions;
wenzelm
parents: 81218
diff changeset
    75
  | detect_position _ = false;
9867b5cf3f7a more concise representation of term positions;
wenzelm
parents: 81218
diff changeset
    76
45445
41e641a870de pass term positions into check phase, where resulting types are reported accordingly, and eventually shown as tooltips;
wenzelm
parents: 42264
diff changeset
    77
fun decode_position (Free (x, _)) =
41e641a870de pass term positions into check phase, where resulting types are reported accordingly, and eventually shown as tooltips;
wenzelm
parents: 42264
diff changeset
    78
      (case decode x of
81218
94bace5078ba support multiple positions (non-empty list);
wenzelm
parents: 80875
diff changeset
    79
        [] => NONE
94bace5078ba support multiple positions (non-empty list);
wenzelm
parents: 80875
diff changeset
    80
      | ps => SOME (ps, TFree (x, dummyS)))
42264
b6c1b0c4c511 separate structure Term_Position;
wenzelm
parents:
diff changeset
    81
  | decode_position _ = NONE;
b6c1b0c4c511 separate structure Term_Position;
wenzelm
parents:
diff changeset
    82
81218
94bace5078ba support multiple positions (non-empty list);
wenzelm
parents: 80875
diff changeset
    83
fun decode_position1 (Free (x, _)) =
94bace5078ba support multiple positions (non-empty list);
wenzelm
parents: 80875
diff changeset
    84
      (case decode x of
94bace5078ba support multiple positions (non-empty list);
wenzelm
parents: 80875
diff changeset
    85
        [] => NONE
94bace5078ba support multiple positions (non-empty list);
wenzelm
parents: 80875
diff changeset
    86
      | pos :: _ => SOME pos)
94bace5078ba support multiple positions (non-empty list);
wenzelm
parents: 80875
diff changeset
    87
  | decode_position1 _ = NONE;
94bace5078ba support multiple positions (non-empty list);
wenzelm
parents: 80875
diff changeset
    88
81232
9867b5cf3f7a more concise representation of term positions;
wenzelm
parents: 81218
diff changeset
    89
fun detect_positionT (TFree (x, _)) = detect x
9867b5cf3f7a more concise representation of term positions;
wenzelm
parents: 81218
diff changeset
    90
  | detect_positionT _ = false;
9867b5cf3f7a more concise representation of term positions;
wenzelm
parents: 81218
diff changeset
    91
45445
41e641a870de pass term positions into check phase, where resulting types are reported accordingly, and eventually shown as tooltips;
wenzelm
parents: 42264
diff changeset
    92
fun decode_positionT (TFree (x, _)) = decode x
81218
94bace5078ba support multiple positions (non-empty list);
wenzelm
parents: 80875
diff changeset
    93
  | decode_positionT _ = [];
45445
41e641a870de pass term positions into check phase, where resulting types are reported accordingly, and eventually shown as tooltips;
wenzelm
parents: 42264
diff changeset
    94
49685
4341e4d86872 allow position constraints to coexist with 0 or 1 sort constraints;
wenzelm
parents: 49674
diff changeset
    95
fun decode_positionS cs =
81232
9867b5cf3f7a more concise representation of term positions;
wenzelm
parents: 81218
diff changeset
    96
  let val (ps, sort) = List.partition detect cs
81218
94bace5078ba support multiple positions (non-empty list);
wenzelm
parents: 80875
diff changeset
    97
  in (maps decode ps, sort) end;
49674
dbadb4d03cbc report sort assignment of visible type variables;
wenzelm
parents: 45666
diff changeset
    98
81232
9867b5cf3f7a more concise representation of term positions;
wenzelm
parents: 81218
diff changeset
    99
9867b5cf3f7a more concise representation of term positions;
wenzelm
parents: 81218
diff changeset
   100
(* strip_positions *)
42264
b6c1b0c4c511 separate structure Term_Position;
wenzelm
parents:
diff changeset
   101
52175
626a757d3c2d uniform Term_Position.markers (cf. dbadb4d03cbc);
wenzelm
parents: 50201
diff changeset
   102
val markers = ["_constrain", "_constrainAbs", "_ofsort"];
626a757d3c2d uniform Term_Position.markers (cf. dbadb4d03cbc);
wenzelm
parents: 50201
diff changeset
   103
42264
b6c1b0c4c511 separate structure Term_Position;
wenzelm
parents:
diff changeset
   104
fun strip_positions ((t as Const (c, _)) $ u $ v) =
81232
9867b5cf3f7a more concise representation of term positions;
wenzelm
parents: 81218
diff changeset
   105
      if member (op =) markers c andalso detect_position v
42264
b6c1b0c4c511 separate structure Term_Position;
wenzelm
parents:
diff changeset
   106
      then strip_positions u
b6c1b0c4c511 separate structure Term_Position;
wenzelm
parents:
diff changeset
   107
      else t $ strip_positions u $ strip_positions v
b6c1b0c4c511 separate structure Term_Position;
wenzelm
parents:
diff changeset
   108
  | strip_positions (t $ u) = strip_positions t $ strip_positions u
b6c1b0c4c511 separate structure Term_Position;
wenzelm
parents:
diff changeset
   109
  | strip_positions (Abs (x, T, t)) = Abs (x, T, strip_positions t)
b6c1b0c4c511 separate structure Term_Position;
wenzelm
parents:
diff changeset
   110
  | strip_positions t = t;
b6c1b0c4c511 separate structure Term_Position;
wenzelm
parents:
diff changeset
   111
b6c1b0c4c511 separate structure Term_Position;
wenzelm
parents:
diff changeset
   112
end;