author | wenzelm |
Sat, 07 Dec 2024 15:00:43 +0100 | |
changeset 81552 | 4717d3bf5752 |
parent 81251 | 89ea66c2045b |
child 81558 | b57996a0688c |
permissions | -rw-r--r-- |
42264 | 1 |
(* Title: Pure/Syntax/term_position.ML |
2 |
Author: Makarius |
|
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 | 5 |
*) |
6 |
||
7 |
signature TERM_POSITION = |
|
8 |
sig |
|
81218 | 9 |
val pretty: Position.T list -> Pretty.T |
10 |
val encode: Position.T list -> string |
|
81232 | 11 |
val detect: string -> bool |
81218 | 12 |
val decode: string -> Position.T list |
81232 | 13 |
val detect_position: term -> bool |
81218 | 14 |
val decode_position: term -> (Position.T list * typ) option |
15 |
val decode_position1: term -> Position.T option |
|
81232 | 16 |
val detect_positionT: typ -> bool |
81218 | 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 | 20 |
val strip_positions: term -> term |
21 |
end; |
|
22 |
||
23 |
structure Term_Position: TERM_POSITION = |
|
24 |
struct |
|
25 |
||
26 |
(* markup *) |
|
27 |
||
28 |
val position_dummy = "<position>"; |
|
29 |
val position_text = XML.Text position_dummy; |
|
30 |
||
81218 | 31 |
fun pretty ps = Pretty.marks_str (map Position.markup ps, position_dummy); |
42264 | 32 |
|
81218 | 33 |
fun encode_pos pos = XML.Elem (Position.markup pos, [position_text]); |
42264 | 34 |
|
81218 | 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 | 37 |
then SOME (Position.of_properties props) |
38 |
else NONE |
|
81218 | 39 |
| decode_pos _ = NONE; |
40 |
||
81232 | 41 |
|
42 |
(* encode *) |
|
43 |
||
44 |
val encode_none = YXML.string_of (encode_pos Position.none); |
|
45 |
||
81218 | 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 | 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 | 50 |
|
51 |
val encode_prefix = YXML.XY ^ Markup.positionN; |
|
52 |
||
53 |
||
54 |
(* decode *) |
|
55 |
||
56 |
val detect = String.isPrefix encode_prefix; |
|
81218 | 57 |
|
58 |
fun decode str = |
|
81232 | 59 |
if str = encode_none then [Position.none] |
60 |
else if detect str then |
|
61 |
let |
|
62 |
val xml = YXML.parse_body str handle Fail msg => error msg; |
|
63 |
val ps = map decode_pos xml; |
|
64 |
in |
|
65 |
if not (null ps) andalso forall is_some ps then map the ps |
|
66 |
else if forall is_none ps then [] |
|
67 |
else error ("Bad encoded positions: " ^ quote str) |
|
68 |
end |
|
69 |
else []; |
|
42264 | 70 |
|
71 |
||
72 |
(* positions within parse trees *) |
|
73 |
||
81232 | 74 |
fun detect_position (Free (x, _)) = detect x |
75 |
| detect_position _ = false; |
|
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 | 79 |
[] => NONE |
80 |
| ps => SOME (ps, TFree (x, dummyS))) |
|
42264 | 81 |
| decode_position _ = NONE; |
82 |
||
81218 | 83 |
fun decode_position1 (Free (x, _)) = |
84 |
(case decode x of |
|
85 |
[] => NONE |
|
86 |
| pos :: _ => SOME pos) |
|
87 |
| decode_position1 _ = NONE; |
|
88 |
||
81232 | 89 |
fun detect_positionT (TFree (x, _)) = detect x |
90 |
| detect_positionT _ = false; |
|
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 | 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 | 96 |
let val (ps, sort) = List.partition detect cs |
81218 | 97 |
in (maps decode ps, sort) end; |
49674
dbadb4d03cbc
report sort assignment of visible type variables;
wenzelm
parents:
45666
diff
changeset
|
98 |
|
81232 | 99 |
|
100 |
(* strip_positions *) |
|
42264 | 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 | 104 |
fun strip_positions ((t as Const (c, _)) $ u $ v) = |
81232 | 105 |
if member (op =) markers c andalso detect_position v |
42264 | 106 |
then strip_positions u |
107 |
else t $ strip_positions u $ strip_positions v |
|
108 |
| strip_positions (t $ u) = strip_positions t $ strip_positions u |
|
109 |
| strip_positions (Abs (x, T, t)) = Abs (x, T, strip_positions t) |
|
110 |
| strip_positions t = t; |
|
111 |
||
112 |
end; |