author | wenzelm |
Sat, 29 Sep 2012 20:13:50 +0200 | |
changeset 49662 | de6be6922c19 |
parent 49660 | de49d9b4d7bc |
child 49674 | dbadb4d03cbc |
permissions | -rw-r--r-- |
42243 | 1 |
(* Title: Pure/Syntax/syntax_phases.ML |
42241
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
2 |
Author: Makarius |
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
3 |
|
42243 | 4 |
Main phases of inner syntax processing, with standard implementations |
5 |
of parse/unparse operations. |
|
42241
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
6 |
*) |
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
7 |
|
42243 | 8 |
signature SYNTAX_PHASES = |
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
9 |
sig |
45427
fca432074fb2
sort assignment before simultaneous term_check, not isolated parse_term;
wenzelm
parents:
45423
diff
changeset
|
10 |
val decode_sort: term -> sort |
fca432074fb2
sort assignment before simultaneous term_check, not isolated parse_term;
wenzelm
parents:
45423
diff
changeset
|
11 |
val decode_typ: term -> typ |
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
12 |
val decode_term: Proof.context -> |
44736 | 13 |
Position.report list * term Exn.result -> Position.report list * term Exn.result |
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
14 |
val parse_ast_pattern: Proof.context -> string * string -> Ast.ast |
42247
12fe41a92cd5
typed_print_translation: discontinued show_sorts argument;
wenzelm
parents:
42245
diff
changeset
|
15 |
val term_of_typ: Proof.context -> typ -> term |
45429 | 16 |
val print_checks: Proof.context -> unit |
17 |
val typ_check: int -> string -> (Proof.context -> typ list -> typ list) -> |
|
18 |
Context.generic -> Context.generic |
|
19 |
val term_check: int -> string -> (Proof.context -> term list -> term list) -> |
|
20 |
Context.generic -> Context.generic |
|
21 |
val typ_uncheck: int -> string -> (Proof.context -> typ list -> typ list) -> |
|
22 |
Context.generic -> Context.generic |
|
23 |
val term_uncheck: int -> string -> (Proof.context -> term list -> term list) -> |
|
24 |
Context.generic -> Context.generic |
|
45444 | 25 |
val typ_check': int -> string -> |
26 |
(typ list -> Proof.context -> (typ list * Proof.context) option) -> |
|
27 |
Context.generic -> Context.generic |
|
28 |
val term_check': int -> string -> |
|
29 |
(term list -> Proof.context -> (term list * Proof.context) option) -> |
|
30 |
Context.generic -> Context.generic |
|
31 |
val typ_uncheck': int -> string -> |
|
32 |
(typ list -> Proof.context -> (typ list * Proof.context) option) -> |
|
33 |
Context.generic -> Context.generic |
|
34 |
val term_uncheck': int -> string -> |
|
35 |
(term list -> Proof.context -> (term list * Proof.context) option) -> |
|
36 |
Context.generic -> Context.generic |
|
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
37 |
end |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
38 |
|
42243 | 39 |
structure Syntax_Phases: SYNTAX_PHASES = |
42241
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
40 |
struct |
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
41 |
|
42298
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents:
42296
diff
changeset
|
42 |
(** markup logical entities **) |
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents:
42296
diff
changeset
|
43 |
|
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents:
42296
diff
changeset
|
44 |
fun markup_class ctxt c = |
42379 | 45 |
[Name_Space.markup (Type.class_space (Proof_Context.tsig_of ctxt)) c]; |
42298
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents:
42296
diff
changeset
|
46 |
|
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents:
42296
diff
changeset
|
47 |
fun markup_type ctxt c = |
42379 | 48 |
[Name_Space.markup (Type.type_space (Proof_Context.tsig_of ctxt)) c]; |
42298
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents:
42296
diff
changeset
|
49 |
|
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents:
42296
diff
changeset
|
50 |
fun markup_const ctxt c = |
42379 | 51 |
[Name_Space.markup (Consts.space_of (Proof_Context.consts_of ctxt)) c]; |
42298
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents:
42296
diff
changeset
|
52 |
|
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents:
42296
diff
changeset
|
53 |
fun markup_free ctxt x = |
45666 | 54 |
[if can Name.dest_skolem x then Isabelle_Markup.skolem else Isabelle_Markup.free] @ |
42493
01430341fc79
more informative markup for fixed variables (via name space entry);
wenzelm
parents:
42488
diff
changeset
|
55 |
(if Variable.is_body ctxt orelse Variable.is_fixed ctxt x |
01430341fc79
more informative markup for fixed variables (via name space entry);
wenzelm
parents:
42488
diff
changeset
|
56 |
then [Variable.markup_fixed ctxt x] |
01430341fc79
more informative markup for fixed variables (via name space entry);
wenzelm
parents:
42488
diff
changeset
|
57 |
else []); |
42298
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents:
42296
diff
changeset
|
58 |
|
45666 | 59 |
fun markup_var xi = [Markup.name (Term.string_of_vname xi) Isabelle_Markup.var]; |
42298
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents:
42296
diff
changeset
|
60 |
|
45412 | 61 |
fun markup_bound def ps (name, id) = |
45666 | 62 |
let val entity = Isabelle_Markup.entity Isabelle_Markup.boundN name in |
63 |
Isabelle_Markup.bound :: |
|
45412 | 64 |
map (fn pos => Markup.properties (Position.entity_properties_of def id pos) entity) ps |
65 |
end; |
|
42298
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents:
42296
diff
changeset
|
66 |
|
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents:
42296
diff
changeset
|
67 |
fun markup_entity ctxt c = |
42360 | 68 |
(case Syntax.lookup_const (Proof_Context.syn_of ctxt) c of |
42298
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents:
42296
diff
changeset
|
69 |
SOME "" => [] |
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents:
42296
diff
changeset
|
70 |
| SOME b => markup_entity ctxt b |
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents:
42296
diff
changeset
|
71 |
| NONE => c |> Lexicon.unmark |
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents:
42296
diff
changeset
|
72 |
{case_class = markup_class ctxt, |
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents:
42296
diff
changeset
|
73 |
case_type = markup_type ctxt, |
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents:
42296
diff
changeset
|
74 |
case_const = markup_const ctxt, |
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents:
42296
diff
changeset
|
75 |
case_fixed = markup_free ctxt, |
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents:
42296
diff
changeset
|
76 |
case_default = K []}); |
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents:
42296
diff
changeset
|
77 |
|
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents:
42296
diff
changeset
|
78 |
|
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents:
42296
diff
changeset
|
79 |
|
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
80 |
(** decode parse trees **) |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
81 |
|
45427
fca432074fb2
sort assignment before simultaneous term_check, not isolated parse_term;
wenzelm
parents:
45423
diff
changeset
|
82 |
(* decode_sort *) |
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
83 |
|
45427
fca432074fb2
sort assignment before simultaneous term_check, not isolated parse_term;
wenzelm
parents:
45423
diff
changeset
|
84 |
fun decode_sort tm = |
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
85 |
let |
45427
fca432074fb2
sort assignment before simultaneous term_check, not isolated parse_term;
wenzelm
parents:
45423
diff
changeset
|
86 |
fun err () = raise TERM ("decode_sort: bad encoding of classes", [tm]); |
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
87 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
88 |
fun class s = Lexicon.unmark_class s handle Fail _ => err (); |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
89 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
90 |
fun classes (Const (s, _)) = [class s] |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
91 |
| classes (Const ("_classes", _) $ Const (s, _) $ cs) = class s :: classes cs |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
92 |
| classes _ = err (); |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
93 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
94 |
fun sort (Const ("_topsort", _)) = [] |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
95 |
| sort (Const (s, _)) = [class s] |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
96 |
| sort (Const ("_sort", _) $ cs) = classes cs |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
97 |
| sort _ = err (); |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
98 |
in sort tm end; |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
99 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
100 |
|
45427
fca432074fb2
sort assignment before simultaneous term_check, not isolated parse_term;
wenzelm
parents:
45423
diff
changeset
|
101 |
(* decode_typ *) |
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
102 |
|
45427
fca432074fb2
sort assignment before simultaneous term_check, not isolated parse_term;
wenzelm
parents:
45423
diff
changeset
|
103 |
fun decode_typ tm = |
fca432074fb2
sort assignment before simultaneous term_check, not isolated parse_term;
wenzelm
parents:
45423
diff
changeset
|
104 |
let |
fca432074fb2
sort assignment before simultaneous term_check, not isolated parse_term;
wenzelm
parents:
45423
diff
changeset
|
105 |
fun err () = raise TERM ("decode_typ: bad encoding of type", [tm]); |
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
106 |
|
45427
fca432074fb2
sort assignment before simultaneous term_check, not isolated parse_term;
wenzelm
parents:
45423
diff
changeset
|
107 |
fun typ (Free (x, _)) = TFree (x, dummyS) |
fca432074fb2
sort assignment before simultaneous term_check, not isolated parse_term;
wenzelm
parents:
45423
diff
changeset
|
108 |
| typ (Var (xi, _)) = TVar (xi, dummyS) |
fca432074fb2
sort assignment before simultaneous term_check, not isolated parse_term;
wenzelm
parents:
45423
diff
changeset
|
109 |
| typ (Const ("_tfree",_) $ (t as Free _)) = typ t |
fca432074fb2
sort assignment before simultaneous term_check, not isolated parse_term;
wenzelm
parents:
45423
diff
changeset
|
110 |
| typ (Const ("_tvar",_) $ (t as Var _)) = typ t |
fca432074fb2
sort assignment before simultaneous term_check, not isolated parse_term;
wenzelm
parents:
45423
diff
changeset
|
111 |
| typ (Const ("_ofsort", _) $ Free (x, _) $ s) = TFree (x, decode_sort s) |
fca432074fb2
sort assignment before simultaneous term_check, not isolated parse_term;
wenzelm
parents:
45423
diff
changeset
|
112 |
| typ (Const ("_ofsort", _) $ (Const ("_tfree",_) $ Free (x, _)) $ s) = |
fca432074fb2
sort assignment before simultaneous term_check, not isolated parse_term;
wenzelm
parents:
45423
diff
changeset
|
113 |
TFree (x, decode_sort s) |
fca432074fb2
sort assignment before simultaneous term_check, not isolated parse_term;
wenzelm
parents:
45423
diff
changeset
|
114 |
| typ (Const ("_ofsort", _) $ Var (xi, _) $ s) = TVar (xi, decode_sort s) |
fca432074fb2
sort assignment before simultaneous term_check, not isolated parse_term;
wenzelm
parents:
45423
diff
changeset
|
115 |
| typ (Const ("_ofsort", _) $ (Const ("_tvar",_) $ Var (xi, _)) $ s) = |
fca432074fb2
sort assignment before simultaneous term_check, not isolated parse_term;
wenzelm
parents:
45423
diff
changeset
|
116 |
TVar (xi, decode_sort s) |
fca432074fb2
sort assignment before simultaneous term_check, not isolated parse_term;
wenzelm
parents:
45423
diff
changeset
|
117 |
| typ (Const ("_dummy_ofsort", _) $ s) = TFree ("'_dummy_", decode_sort s) |
fca432074fb2
sort assignment before simultaneous term_check, not isolated parse_term;
wenzelm
parents:
45423
diff
changeset
|
118 |
| typ t = |
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
119 |
let |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
120 |
val (head, args) = Term.strip_comb t; |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
121 |
val a = |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
122 |
(case head of |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
123 |
Const (c, _) => (Lexicon.unmark_type c handle Fail _ => err ()) |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
124 |
| _ => err ()); |
45427
fca432074fb2
sort assignment before simultaneous term_check, not isolated parse_term;
wenzelm
parents:
45423
diff
changeset
|
125 |
in Type (a, map typ args) end; |
fca432074fb2
sort assignment before simultaneous term_check, not isolated parse_term;
wenzelm
parents:
45423
diff
changeset
|
126 |
in typ tm end; |
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
127 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
128 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
129 |
(* parsetree_to_ast *) |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
130 |
|
46126
bab00660539d
discontinued Syntax.positions -- atomic parse trees are always annotated;
wenzelm
parents:
45666
diff
changeset
|
131 |
fun parsetree_to_ast ctxt raw trf parsetree = |
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
132 |
let |
44736 | 133 |
val reports = Unsynchronized.ref ([]: Position.report list); |
44735 | 134 |
fun report pos = Position.store_reports reports [pos]; |
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
135 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
136 |
fun trans a args = |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
137 |
(case trf a of |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
138 |
NONE => Ast.mk_appl (Ast.Constant a) args |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
139 |
| SOME f => f ctxt args); |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
140 |
|
42282
4fa41e068ff0
report literal tokens according to parsetree head;
wenzelm
parents:
42281
diff
changeset
|
141 |
fun asts_of (Parser.Node ("_class_name", [Parser.Tip tok])) = |
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
142 |
let |
42470 | 143 |
val pos = Lexicon.pos_of_token tok; |
144 |
val c = Proof_Context.read_class ctxt (Lexicon.str_of_token tok) |
|
48992 | 145 |
handle ERROR msg => error (msg ^ Position.here pos); |
42470 | 146 |
val _ = report pos (markup_class ctxt) c; |
42282
4fa41e068ff0
report literal tokens according to parsetree head;
wenzelm
parents:
42281
diff
changeset
|
147 |
in [Ast.Constant (Lexicon.mark_class c)] end |
4fa41e068ff0
report literal tokens according to parsetree head;
wenzelm
parents:
42281
diff
changeset
|
148 |
| asts_of (Parser.Node ("_type_name", [Parser.Tip tok])) = |
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
149 |
let |
42470 | 150 |
val pos = Lexicon.pos_of_token tok; |
151 |
val Type (c, _) = |
|
152 |
Proof_Context.read_type_name_proper ctxt false (Lexicon.str_of_token tok) |
|
48992 | 153 |
handle ERROR msg => error (msg ^ Position.here pos); |
42470 | 154 |
val _ = report pos (markup_type ctxt) c; |
42282
4fa41e068ff0
report literal tokens according to parsetree head;
wenzelm
parents:
42281
diff
changeset
|
155 |
in [Ast.Constant (Lexicon.mark_type c)] end |
42410
16bc5564535f
less bulky "_position", for improved readability of parse trees;
wenzelm
parents:
42408
diff
changeset
|
156 |
| asts_of (Parser.Node ("_position", [pt as Parser.Tip tok])) = |
46126
bab00660539d
discontinued Syntax.positions -- atomic parse trees are always annotated;
wenzelm
parents:
45666
diff
changeset
|
157 |
if raw then [ast_of pt] |
bab00660539d
discontinued Syntax.positions -- atomic parse trees are always annotated;
wenzelm
parents:
45666
diff
changeset
|
158 |
else |
45453 | 159 |
[Ast.Appl [Ast.Constant "_constrain", ast_of pt, |
160 |
Ast.Variable (Term_Position.encode (Lexicon.pos_of_token tok))]] |
|
42282
4fa41e068ff0
report literal tokens according to parsetree head;
wenzelm
parents:
42281
diff
changeset
|
161 |
| asts_of (Parser.Node (a, pts)) = |
4fa41e068ff0
report literal tokens according to parsetree head;
wenzelm
parents:
42281
diff
changeset
|
162 |
let |
4fa41e068ff0
report literal tokens according to parsetree head;
wenzelm
parents:
42281
diff
changeset
|
163 |
val _ = pts |> List.app |
4fa41e068ff0
report literal tokens according to parsetree head;
wenzelm
parents:
42281
diff
changeset
|
164 |
(fn Parser.Node _ => () | Parser.Tip tok => |
4fa41e068ff0
report literal tokens according to parsetree head;
wenzelm
parents:
42281
diff
changeset
|
165 |
if Lexicon.valued_token tok then () |
42298
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents:
42296
diff
changeset
|
166 |
else report (Lexicon.pos_of_token tok) (markup_entity ctxt) a); |
42282
4fa41e068ff0
report literal tokens according to parsetree head;
wenzelm
parents:
42281
diff
changeset
|
167 |
in [trans a (maps asts_of pts)] end |
4fa41e068ff0
report literal tokens according to parsetree head;
wenzelm
parents:
42281
diff
changeset
|
168 |
| asts_of (Parser.Tip tok) = |
4fa41e068ff0
report literal tokens according to parsetree head;
wenzelm
parents:
42281
diff
changeset
|
169 |
if Lexicon.valued_token tok |
4fa41e068ff0
report literal tokens according to parsetree head;
wenzelm
parents:
42281
diff
changeset
|
170 |
then [Ast.Variable (Lexicon.str_of_token tok)] |
4fa41e068ff0
report literal tokens according to parsetree head;
wenzelm
parents:
42281
diff
changeset
|
171 |
else [] |
4fa41e068ff0
report literal tokens according to parsetree head;
wenzelm
parents:
42281
diff
changeset
|
172 |
|
4fa41e068ff0
report literal tokens according to parsetree head;
wenzelm
parents:
42281
diff
changeset
|
173 |
and ast_of pt = |
4fa41e068ff0
report literal tokens according to parsetree head;
wenzelm
parents:
42281
diff
changeset
|
174 |
(case asts_of pt of |
4fa41e068ff0
report literal tokens according to parsetree head;
wenzelm
parents:
42281
diff
changeset
|
175 |
[ast] => ast |
4fa41e068ff0
report literal tokens according to parsetree head;
wenzelm
parents:
42281
diff
changeset
|
176 |
| asts => raise Ast.AST ("parsetree_to_ast: malformed parsetree", asts)); |
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
177 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
178 |
val ast = Exn.interruptible_capture ast_of parsetree; |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
179 |
in (! reports, ast) end; |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
180 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
181 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
182 |
(* ast_to_term *) |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
183 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
184 |
fun ast_to_term ctxt trf = |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
185 |
let |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
186 |
fun trans a args = |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
187 |
(case trf a of |
42476 | 188 |
NONE => Term.list_comb (Syntax.const a, args) |
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
189 |
| SOME f => f ctxt args); |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
190 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
191 |
fun term_of (Ast.Constant a) = trans a [] |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
192 |
| term_of (Ast.Variable x) = Lexicon.read_var x |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
193 |
| term_of (Ast.Appl (Ast.Constant a :: (asts as _ :: _))) = |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
194 |
trans a (map term_of asts) |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
195 |
| term_of (Ast.Appl (ast :: (asts as _ :: _))) = |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
196 |
Term.list_comb (term_of ast, map term_of asts) |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
197 |
| term_of (ast as Ast.Appl _) = raise Ast.AST ("ast_to_term: malformed ast", [ast]); |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
198 |
in term_of end; |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
199 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
200 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
201 |
(* decode_term -- transform parse tree into raw term *) |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
202 |
|
44736 | 203 |
fun decode_term _ (result as (_: Position.report list, Exn.Exn _)) = result |
43761
e72ba84ae58f
tuned signature -- corresponding to Scala version;
wenzelm
parents:
43731
diff
changeset
|
204 |
| decode_term ctxt (reports0, Exn.Res tm) = |
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
205 |
let |
42250
cc5ac538f991
eliminated odd object-oriented type_context/term_context;
wenzelm
parents:
42249
diff
changeset
|
206 |
fun get_const a = |
42360 | 207 |
((true, #1 (Term.dest_Const (Proof_Context.read_const_proper ctxt false a))) |
208 |
handle ERROR _ => (false, Consts.intern (Proof_Context.consts_of ctxt) a)); |
|
209 |
val get_free = Proof_Context.intern_skolem ctxt; |
|
42250
cc5ac538f991
eliminated odd object-oriented type_context/term_context;
wenzelm
parents:
42249
diff
changeset
|
210 |
|
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
211 |
val reports = Unsynchronized.ref reports0; |
44735 | 212 |
fun report ps = Position.store_reports reports ps; |
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
213 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
214 |
fun decode ps qs bs (Const ("_constrain", _) $ t $ typ) = |
42264 | 215 |
(case Term_Position.decode_position typ of |
45445
41e641a870de
pass term positions into check phase, where resulting types are reported accordingly, and eventually shown as tooltips;
wenzelm
parents:
45444
diff
changeset
|
216 |
SOME (p, T) => Type.constraint T (decode (p :: ps) qs bs t) |
45427
fca432074fb2
sort assignment before simultaneous term_check, not isolated parse_term;
wenzelm
parents:
45423
diff
changeset
|
217 |
| NONE => Type.constraint (decode_typ typ) (decode ps qs bs t)) |
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
218 |
| decode ps qs bs (Const ("_constrainAbs", _) $ t $ typ) = |
42264 | 219 |
(case Term_Position.decode_position typ of |
45445
41e641a870de
pass term positions into check phase, where resulting types are reported accordingly, and eventually shown as tooltips;
wenzelm
parents:
45444
diff
changeset
|
220 |
SOME (q, T) => Type.constraint (T --> dummyT) (decode ps (q :: qs) bs t) |
45427
fca432074fb2
sort assignment before simultaneous term_check, not isolated parse_term;
wenzelm
parents:
45423
diff
changeset
|
221 |
| NONE => Type.constraint (decode_typ typ --> dummyT) (decode ps qs bs t)) |
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
222 |
| decode _ qs bs (Abs (x, T, t)) = |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
223 |
let |
45412 | 224 |
val id = serial (); |
225 |
val _ = report qs (markup_bound true qs) (x, id); |
|
226 |
in Abs (x, T, decode [] [] ((qs, (x, id)) :: bs) t) end |
|
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
227 |
| decode _ _ bs (t $ u) = decode [] [] bs t $ decode [] [] bs u |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
228 |
| decode ps _ _ (Const (a, T)) = |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
229 |
(case try Lexicon.unmark_fixed a of |
42282
4fa41e068ff0
report literal tokens according to parsetree head;
wenzelm
parents:
42281
diff
changeset
|
230 |
SOME x => (report ps (markup_free ctxt) x; Free (x, T)) |
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
231 |
| NONE => |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
232 |
let |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
233 |
val c = |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
234 |
(case try Lexicon.unmark_const a of |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
235 |
SOME c => c |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
236 |
| NONE => snd (get_const a)); |
42282
4fa41e068ff0
report literal tokens according to parsetree head;
wenzelm
parents:
42281
diff
changeset
|
237 |
val _ = report ps (markup_const ctxt) c; |
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
238 |
in Const (c, T) end) |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
239 |
| decode ps _ _ (Free (a, T)) = |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
240 |
(case (get_free a, get_const a) of |
42282
4fa41e068ff0
report literal tokens according to parsetree head;
wenzelm
parents:
42281
diff
changeset
|
241 |
(SOME x, _) => (report ps (markup_free ctxt) x; Free (x, T)) |
4fa41e068ff0
report literal tokens according to parsetree head;
wenzelm
parents:
42281
diff
changeset
|
242 |
| (_, (true, c)) => (report ps (markup_const ctxt) c; Const (c, T)) |
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
243 |
| (_, (false, c)) => |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
244 |
if Long_Name.is_qualified c |
42282
4fa41e068ff0
report literal tokens according to parsetree head;
wenzelm
parents:
42281
diff
changeset
|
245 |
then (report ps (markup_const ctxt) c; Const (c, T)) |
4fa41e068ff0
report literal tokens according to parsetree head;
wenzelm
parents:
42281
diff
changeset
|
246 |
else (report ps (markup_free ctxt) c; Free (c, T))) |
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
247 |
| decode ps _ _ (Var (xi, T)) = (report ps markup_var xi; Var (xi, T)) |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
248 |
| decode ps _ bs (t as Bound i) = |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
249 |
(case try (nth bs) i of |
45412 | 250 |
SOME (qs, (x, id)) => (report ps (markup_bound false qs) (x, id); t) |
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
251 |
| NONE => t); |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
252 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
253 |
val tm' = Exn.interruptible_capture (fn () => decode [] [] [] tm) (); |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
254 |
in (! reports, tm') end; |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
255 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
256 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
257 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
258 |
(** parse **) |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
259 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
260 |
(* results *) |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
261 |
|
43761
e72ba84ae58f
tuned signature -- corresponding to Scala version;
wenzelm
parents:
43731
diff
changeset
|
262 |
fun proper_results results = map_filter (fn (y, Exn.Res x) => SOME (y, x) | _ => NONE) results; |
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
263 |
fun failed_results results = map_filter (fn (y, Exn.Exn e) => SOME (y, e) | _ => NONE) results; |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
264 |
|
46512
4f9f61f9b535
simplified configuration options for syntax ambiguity;
wenzelm
parents:
46506
diff
changeset
|
265 |
fun report_result ctxt pos ambig_msgs results = |
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
266 |
(case (proper_results results, failed_results results) of |
44736 | 267 |
([], (reports, exn) :: _) => (Context_Position.reports ctxt reports; reraise exn) |
268 |
| ([(reports, x)], _) => (Context_Position.reports ctxt reports; x) |
|
46512
4f9f61f9b535
simplified configuration options for syntax ambiguity;
wenzelm
parents:
46506
diff
changeset
|
269 |
| _ => |
4f9f61f9b535
simplified configuration options for syntax ambiguity;
wenzelm
parents:
46506
diff
changeset
|
270 |
if null ambig_msgs then |
48992 | 271 |
error ("Parse error: ambiguous syntax" ^ Position.here pos) |
46512
4f9f61f9b535
simplified configuration options for syntax ambiguity;
wenzelm
parents:
46506
diff
changeset
|
272 |
else error (cat_lines ambig_msgs)); |
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
273 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
274 |
|
42249 | 275 |
(* parse raw asts *) |
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
276 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
277 |
fun parse_asts ctxt raw root (syms, pos) = |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
278 |
let |
42360 | 279 |
val syn = Proof_Context.syn_of ctxt; |
42253 | 280 |
val ast_tr = Syntax.parse_ast_translation syn; |
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
281 |
|
42251 | 282 |
val toks = Syntax.tokenize syn raw syms; |
44736 | 283 |
val _ = Context_Position.reports ctxt (map Lexicon.report_of_token toks); |
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
284 |
|
45641 | 285 |
val pts = Syntax.parse syn root (filter Lexicon.is_proper toks) |
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
286 |
handle ERROR msg => |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
287 |
error (msg ^ |
45666 | 288 |
implode |
289 |
(map (Markup.markup Isabelle_Markup.report o Lexicon.reported_token_range ctxt) toks)); |
|
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
290 |
val len = length pts; |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
291 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
292 |
val limit = Config.get ctxt Syntax.ambiguity_limit; |
46512
4f9f61f9b535
simplified configuration options for syntax ambiguity;
wenzelm
parents:
46506
diff
changeset
|
293 |
val ambig_msgs = |
4f9f61f9b535
simplified configuration options for syntax ambiguity;
wenzelm
parents:
46506
diff
changeset
|
294 |
if len <= 1 then [] |
46506
c7faa011bfa7
simplified configuration options for syntax ambiguity;
wenzelm
parents:
46126
diff
changeset
|
295 |
else |
46512
4f9f61f9b535
simplified configuration options for syntax ambiguity;
wenzelm
parents:
46506
diff
changeset
|
296 |
[cat_lines |
48992 | 297 |
(("Ambiguous input" ^ Position.here (Position.reset_range pos) ^ |
46512
4f9f61f9b535
simplified configuration options for syntax ambiguity;
wenzelm
parents:
46506
diff
changeset
|
298 |
"\nproduces " ^ string_of_int len ^ " parse trees" ^ |
4f9f61f9b535
simplified configuration options for syntax ambiguity;
wenzelm
parents:
46506
diff
changeset
|
299 |
(if len <= limit then "" else " (" ^ string_of_int limit ^ " displayed)") ^ ":") :: |
4f9f61f9b535
simplified configuration options for syntax ambiguity;
wenzelm
parents:
46506
diff
changeset
|
300 |
map (Pretty.string_of o Parser.pretty_parsetree) (take limit pts))]; |
4f9f61f9b535
simplified configuration options for syntax ambiguity;
wenzelm
parents:
46506
diff
changeset
|
301 |
|
4f9f61f9b535
simplified configuration options for syntax ambiguity;
wenzelm
parents:
46506
diff
changeset
|
302 |
in (ambig_msgs, map (parsetree_to_ast ctxt raw ast_tr) pts) end; |
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
303 |
|
46506
c7faa011bfa7
simplified configuration options for syntax ambiguity;
wenzelm
parents:
46126
diff
changeset
|
304 |
fun parse_tree ctxt root input = |
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
305 |
let |
42360 | 306 |
val syn = Proof_Context.syn_of ctxt; |
42253 | 307 |
val tr = Syntax.parse_translation syn; |
42255 | 308 |
val parse_rules = Syntax.parse_rules syn; |
46512
4f9f61f9b535
simplified configuration options for syntax ambiguity;
wenzelm
parents:
46506
diff
changeset
|
309 |
val (ambig_msgs, asts) = parse_asts ctxt false root input; |
4f9f61f9b535
simplified configuration options for syntax ambiguity;
wenzelm
parents:
46506
diff
changeset
|
310 |
val results = |
4f9f61f9b535
simplified configuration options for syntax ambiguity;
wenzelm
parents:
46506
diff
changeset
|
311 |
(map o apsnd o Exn.maps_result) |
4f9f61f9b535
simplified configuration options for syntax ambiguity;
wenzelm
parents:
46506
diff
changeset
|
312 |
(Ast.normalize ctxt parse_rules #> Exn.interruptible_capture (ast_to_term ctxt tr)) asts; |
4f9f61f9b535
simplified configuration options for syntax ambiguity;
wenzelm
parents:
46506
diff
changeset
|
313 |
in (ambig_msgs, results) end; |
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
314 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
315 |
|
42249 | 316 |
(* parse logical entities *) |
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
317 |
|
42241
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
318 |
fun parse_failed ctxt pos msg kind = |
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
319 |
cat_error msg ("Failed to parse " ^ kind ^ |
45666 | 320 |
Markup.markup Isabelle_Markup.report |
48768
abc45de5bb22
clarified "bad" markup: proper body text, invent missing serial on Scala side (counting backwards);
wenzelm
parents:
48756
diff
changeset
|
321 |
(Context_Position.reported_text ctxt pos Isabelle_Markup.bad "")); |
42241
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
322 |
|
43731
70072780e095
inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
wenzelm
parents:
43552
diff
changeset
|
323 |
fun parse_sort ctxt = |
45666 | 324 |
Syntax.parse_token ctxt Term_XML.Decode.sort Isabelle_Markup.sort |
43731
70072780e095
inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
wenzelm
parents:
43552
diff
changeset
|
325 |
(fn (syms, pos) => |
46506
c7faa011bfa7
simplified configuration options for syntax ambiguity;
wenzelm
parents:
46126
diff
changeset
|
326 |
parse_tree ctxt "sort" (syms, pos) |
46512
4f9f61f9b535
simplified configuration options for syntax ambiguity;
wenzelm
parents:
46506
diff
changeset
|
327 |
|> uncurry (report_result ctxt pos) |
45427
fca432074fb2
sort assignment before simultaneous term_check, not isolated parse_term;
wenzelm
parents:
45423
diff
changeset
|
328 |
|> decode_sort |
43731
70072780e095
inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
wenzelm
parents:
43552
diff
changeset
|
329 |
|> Type.minimize_sort (Proof_Context.tsig_of ctxt) |
70072780e095
inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
wenzelm
parents:
43552
diff
changeset
|
330 |
handle ERROR msg => parse_failed ctxt pos msg "sort"); |
42241
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
331 |
|
43731
70072780e095
inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
wenzelm
parents:
43552
diff
changeset
|
332 |
fun parse_typ ctxt = |
45666 | 333 |
Syntax.parse_token ctxt Term_XML.Decode.typ Isabelle_Markup.typ |
43731
70072780e095
inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
wenzelm
parents:
43552
diff
changeset
|
334 |
(fn (syms, pos) => |
46506
c7faa011bfa7
simplified configuration options for syntax ambiguity;
wenzelm
parents:
46126
diff
changeset
|
335 |
parse_tree ctxt "type" (syms, pos) |
46512
4f9f61f9b535
simplified configuration options for syntax ambiguity;
wenzelm
parents:
46506
diff
changeset
|
336 |
|> uncurry (report_result ctxt pos) |
45427
fca432074fb2
sort assignment before simultaneous term_check, not isolated parse_term;
wenzelm
parents:
45423
diff
changeset
|
337 |
|> decode_typ |
43731
70072780e095
inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
wenzelm
parents:
43552
diff
changeset
|
338 |
handle ERROR msg => parse_failed ctxt pos msg "type"); |
42241
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
339 |
|
43731
70072780e095
inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
wenzelm
parents:
43552
diff
changeset
|
340 |
fun parse_term is_prop ctxt = |
42241
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
341 |
let |
42281 | 342 |
val (markup, kind, root, constrain) = |
343 |
if is_prop |
|
45666 | 344 |
then (Isabelle_Markup.prop, "proposition", "prop", Type.constraint propT) |
345 |
else (Isabelle_Markup.term, "term", Config.get ctxt Syntax.root, I); |
|
43731
70072780e095
inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
wenzelm
parents:
43552
diff
changeset
|
346 |
val decode = constrain o Term_XML.Decode.term; |
42249 | 347 |
in |
43731
70072780e095
inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
wenzelm
parents:
43552
diff
changeset
|
348 |
Syntax.parse_token ctxt decode markup |
70072780e095
inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
wenzelm
parents:
43552
diff
changeset
|
349 |
(fn (syms, pos) => |
70072780e095
inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
wenzelm
parents:
43552
diff
changeset
|
350 |
let |
46512
4f9f61f9b535
simplified configuration options for syntax ambiguity;
wenzelm
parents:
46506
diff
changeset
|
351 |
val (ambig_msgs, results) = parse_tree ctxt root (syms, pos) ||> map (decode_term ctxt); |
46506
c7faa011bfa7
simplified configuration options for syntax ambiguity;
wenzelm
parents:
46126
diff
changeset
|
352 |
val parsed_len = length (proper_results results); |
42249 | 353 |
|
46512
4f9f61f9b535
simplified configuration options for syntax ambiguity;
wenzelm
parents:
46506
diff
changeset
|
354 |
val ambiguity_warning = Config.get ctxt Syntax.ambiguity_warning; |
43731
70072780e095
inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
wenzelm
parents:
43552
diff
changeset
|
355 |
val limit = Config.get ctxt Syntax.ambiguity_limit; |
42249 | 356 |
|
43731
70072780e095
inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
wenzelm
parents:
43552
diff
changeset
|
357 |
(*brute-force disambiguation via type-inference*) |
43761
e72ba84ae58f
tuned signature -- corresponding to Scala version;
wenzelm
parents:
43731
diff
changeset
|
358 |
fun check t = (Syntax.check_term ctxt (constrain t); Exn.Res t) |
43731
70072780e095
inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
wenzelm
parents:
43552
diff
changeset
|
359 |
handle exn as ERROR _ => Exn.Exn exn; |
42249 | 360 |
|
43731
70072780e095
inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
wenzelm
parents:
43552
diff
changeset
|
361 |
val results' = |
46506
c7faa011bfa7
simplified configuration options for syntax ambiguity;
wenzelm
parents:
46126
diff
changeset
|
362 |
if parsed_len > 1 then |
46989 | 363 |
(grouped 10 (Par_List.map_name "Syntax_Phases.parse_term") o apsnd o Exn.maps_result) |
43731
70072780e095
inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
wenzelm
parents:
43552
diff
changeset
|
364 |
check results |
70072780e095
inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
wenzelm
parents:
43552
diff
changeset
|
365 |
else results; |
70072780e095
inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
wenzelm
parents:
43552
diff
changeset
|
366 |
val reports' = fst (hd results'); |
42249 | 367 |
|
43731
70072780e095
inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
wenzelm
parents:
43552
diff
changeset
|
368 |
val errs = map snd (failed_results results'); |
70072780e095
inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
wenzelm
parents:
43552
diff
changeset
|
369 |
val checked = map snd (proper_results results'); |
46506
c7faa011bfa7
simplified configuration options for syntax ambiguity;
wenzelm
parents:
46126
diff
changeset
|
370 |
val checked_len = length checked; |
42249 | 371 |
|
43731
70072780e095
inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
wenzelm
parents:
43552
diff
changeset
|
372 |
val show_term = Syntax.string_of_term (Config.put Printer.show_brackets true ctxt); |
70072780e095
inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
wenzelm
parents:
43552
diff
changeset
|
373 |
in |
46506
c7faa011bfa7
simplified configuration options for syntax ambiguity;
wenzelm
parents:
46126
diff
changeset
|
374 |
if checked_len = 0 then |
46512
4f9f61f9b535
simplified configuration options for syntax ambiguity;
wenzelm
parents:
46506
diff
changeset
|
375 |
report_result ctxt pos [] |
4f9f61f9b535
simplified configuration options for syntax ambiguity;
wenzelm
parents:
46506
diff
changeset
|
376 |
[(reports', Exn.Exn (Exn.EXCEPTIONS (map ERROR ambig_msgs @ errs)))] |
46506
c7faa011bfa7
simplified configuration options for syntax ambiguity;
wenzelm
parents:
46126
diff
changeset
|
377 |
else if checked_len = 1 then |
46512
4f9f61f9b535
simplified configuration options for syntax ambiguity;
wenzelm
parents:
46506
diff
changeset
|
378 |
(if parsed_len > 1 andalso ambiguity_warning then |
43731
70072780e095
inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
wenzelm
parents:
43552
diff
changeset
|
379 |
Context_Position.if_visible ctxt warning |
46512
4f9f61f9b535
simplified configuration options for syntax ambiguity;
wenzelm
parents:
46506
diff
changeset
|
380 |
(cat_lines (ambig_msgs @ |
4f9f61f9b535
simplified configuration options for syntax ambiguity;
wenzelm
parents:
46506
diff
changeset
|
381 |
["Fortunately, only one parse tree is type correct" ^ |
48992 | 382 |
Position.here (Position.reset_range pos) ^ |
46512
4f9f61f9b535
simplified configuration options for syntax ambiguity;
wenzelm
parents:
46506
diff
changeset
|
383 |
",\nbut you may still want to disambiguate your grammar or your input."])) |
4f9f61f9b535
simplified configuration options for syntax ambiguity;
wenzelm
parents:
46506
diff
changeset
|
384 |
else (); report_result ctxt pos [] results') |
43731
70072780e095
inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
wenzelm
parents:
43552
diff
changeset
|
385 |
else |
46512
4f9f61f9b535
simplified configuration options for syntax ambiguity;
wenzelm
parents:
46506
diff
changeset
|
386 |
report_result ctxt pos [] |
4f9f61f9b535
simplified configuration options for syntax ambiguity;
wenzelm
parents:
46506
diff
changeset
|
387 |
[(reports', Exn.Exn (ERROR (cat_lines (ambig_msgs @ |
4f9f61f9b535
simplified configuration options for syntax ambiguity;
wenzelm
parents:
46506
diff
changeset
|
388 |
(("Ambiguous input\n" ^ string_of_int checked_len ^ " terms are type correct" ^ |
46506
c7faa011bfa7
simplified configuration options for syntax ambiguity;
wenzelm
parents:
46126
diff
changeset
|
389 |
(if checked_len <= limit then "" |
c7faa011bfa7
simplified configuration options for syntax ambiguity;
wenzelm
parents:
46126
diff
changeset
|
390 |
else " (" ^ string_of_int limit ^ " displayed)") ^ ":") :: |
43731
70072780e095
inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
wenzelm
parents:
43552
diff
changeset
|
391 |
map show_term (take limit checked))))))] |
70072780e095
inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
wenzelm
parents:
43552
diff
changeset
|
392 |
end handle ERROR msg => parse_failed ctxt pos msg kind) |
42249 | 393 |
end; |
42241
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
394 |
|
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
395 |
|
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
396 |
(* parse_ast_pattern *) |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
397 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
398 |
fun parse_ast_pattern ctxt (root, str) = |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
399 |
let |
42360 | 400 |
val syn = Proof_Context.syn_of ctxt; |
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
401 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
402 |
fun constify (ast as Ast.Constant _) = ast |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
403 |
| constify (ast as Ast.Variable x) = |
42298
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents:
42296
diff
changeset
|
404 |
if is_some (Syntax.lookup_const syn x) orelse Long_Name.is_qualified x |
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents:
42296
diff
changeset
|
405 |
then Ast.Constant x |
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
406 |
else ast |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
407 |
| constify (Ast.Appl asts) = Ast.Appl (map constify asts); |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
408 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
409 |
val (syms, pos) = Syntax.read_token str; |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
410 |
in |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
411 |
parse_asts ctxt true root (syms, pos) |
46512
4f9f61f9b535
simplified configuration options for syntax ambiguity;
wenzelm
parents:
46506
diff
changeset
|
412 |
|> uncurry (report_result ctxt pos) |
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
413 |
|> constify |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
414 |
end; |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
415 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
416 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
417 |
|
42245 | 418 |
(** encode parse trees **) |
419 |
||
420 |
(* term_of_sort *) |
|
421 |
||
422 |
fun term_of_sort S = |
|
423 |
let |
|
42476 | 424 |
val class = Syntax.const o Lexicon.mark_class; |
42245 | 425 |
|
426 |
fun classes [c] = class c |
|
42476 | 427 |
| classes (c :: cs) = Syntax.const "_classes" $ class c $ classes cs; |
42245 | 428 |
in |
429 |
(case S of |
|
42476 | 430 |
[] => Syntax.const "_topsort" |
42245 | 431 |
| [c] => class c |
42476 | 432 |
| cs => Syntax.const "_sort" $ classes cs) |
42245 | 433 |
end; |
434 |
||
435 |
||
436 |
(* term_of_typ *) |
|
437 |
||
42247
12fe41a92cd5
typed_print_translation: discontinued show_sorts argument;
wenzelm
parents:
42245
diff
changeset
|
438 |
fun term_of_typ ctxt ty = |
42245 | 439 |
let |
42247
12fe41a92cd5
typed_print_translation: discontinued show_sorts argument;
wenzelm
parents:
42245
diff
changeset
|
440 |
val show_sorts = Config.get ctxt show_sorts; |
12fe41a92cd5
typed_print_translation: discontinued show_sorts argument;
wenzelm
parents:
42245
diff
changeset
|
441 |
|
42245 | 442 |
fun of_sort t S = |
42476 | 443 |
if show_sorts then Syntax.const "_ofsort" $ t $ term_of_sort S |
42245 | 444 |
else t; |
445 |
||
446 |
fun term_of (Type (a, Ts)) = |
|
42476 | 447 |
Term.list_comb (Syntax.const (Lexicon.mark_type a), map term_of Ts) |
42245 | 448 |
| term_of (TFree (x, S)) = |
42290
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42289
diff
changeset
|
449 |
if is_some (Term_Position.decode x) then Syntax.free x |
42476 | 450 |
else of_sort (Syntax.const "_tfree" $ Syntax.free x) S |
451 |
| term_of (TVar (xi, S)) = of_sort (Syntax.const "_tvar" $ Syntax.var xi) S; |
|
42245 | 452 |
in term_of ty end; |
453 |
||
454 |
||
455 |
(* simple_ast_of *) |
|
456 |
||
457 |
fun simple_ast_of ctxt = |
|
458 |
let |
|
459 |
val tune_var = if Config.get ctxt show_question_marks then I else unprefix "?"; |
|
460 |
fun ast_of (Const (c, _)) = Ast.Constant c |
|
461 |
| ast_of (Free (x, _)) = Ast.Variable x |
|
462 |
| ast_of (Var (xi, _)) = Ast.Variable (tune_var (Term.string_of_vname xi)) |
|
463 |
| ast_of (t as _ $ _) = |
|
464 |
let val (f, args) = strip_comb t |
|
465 |
in Ast.mk_appl (ast_of f) (map ast_of args) end |
|
42408 | 466 |
| ast_of (Bound i) = Ast.Appl [Ast.Constant "_loose", Ast.Variable ("B." ^ string_of_int i)] |
42245 | 467 |
| ast_of (Abs _) = raise Fail "simple_ast_of: Abs"; |
468 |
in ast_of end; |
|
469 |
||
470 |
||
471 |
(* sort_to_ast and typ_to_ast *) |
|
472 |
||
473 |
fun ast_of_termT ctxt trf tm = |
|
474 |
let |
|
475 |
val ctxt' = Config.put show_sorts false ctxt; |
|
476 |
fun ast_of (t as Const ("_tfree", _) $ Free _) = simple_ast_of ctxt t |
|
477 |
| ast_of (t as Const ("_tvar", _) $ Var _) = simple_ast_of ctxt t |
|
478 |
| ast_of (Const (a, _)) = trans a [] |
|
479 |
| ast_of (t as _ $ _) = |
|
480 |
(case strip_comb t of |
|
481 |
(Const (a, _), args) => trans a args |
|
482 |
| (f, args) => Ast.Appl (map ast_of (f :: args))) |
|
483 |
| ast_of t = simple_ast_of ctxt t |
|
42254 | 484 |
and trans a args = ast_of (trf a ctxt' dummyT args) |
485 |
handle Match => Ast.mk_appl (Ast.Constant a) (map ast_of args); |
|
42245 | 486 |
in ast_of tm end; |
487 |
||
488 |
fun sort_to_ast ctxt trf S = ast_of_termT ctxt trf (term_of_sort S); |
|
42247
12fe41a92cd5
typed_print_translation: discontinued show_sorts argument;
wenzelm
parents:
42245
diff
changeset
|
489 |
fun typ_to_ast ctxt trf T = ast_of_termT ctxt trf (term_of_typ ctxt T); |
42245 | 490 |
|
491 |
||
492 |
(* term_to_ast *) |
|
493 |
||
42252 | 494 |
fun term_to_ast idents is_syntax_const ctxt trf tm = |
42245 | 495 |
let |
496 |
val show_types = |
|
497 |
Config.get ctxt show_types orelse Config.get ctxt show_sorts orelse |
|
498 |
Config.get ctxt show_all_types; |
|
499 |
val show_structs = Config.get ctxt show_structs; |
|
500 |
val show_free_types = Config.get ctxt show_free_types; |
|
501 |
val show_all_types = Config.get ctxt show_all_types; |
|
49657
40e4feac2921
turn constraints into Isabelle_Markup.typing, depending on show_markup options;
wenzelm
parents:
49655
diff
changeset
|
502 |
val show_markup = Config.get ctxt show_markup; |
42245 | 503 |
|
504 |
val {structs, fixes} = idents; |
|
505 |
||
42249 | 506 |
fun mark_atoms ((t as Const (c, _)) $ u) = |
42294 | 507 |
if member (op =) Syntax.token_markers c |
42245 | 508 |
then t $ u else mark_atoms t $ mark_atoms u |
509 |
| mark_atoms (t $ u) = mark_atoms t $ mark_atoms u |
|
510 |
| mark_atoms (Abs (x, T, t)) = Abs (x, T, mark_atoms t) |
|
511 |
| mark_atoms (t as Const (c, T)) = |
|
42252 | 512 |
if is_syntax_const c then t |
42245 | 513 |
else Const (Lexicon.mark_const c, T) |
514 |
| mark_atoms (t as Free (x, T)) = |
|
515 |
let val i = find_index (fn s => s = x) structs + 1 in |
|
516 |
if i = 0 andalso member (op =) fixes x then |
|
517 |
Const (Lexicon.mark_fixed x, T) |
|
518 |
else if i = 1 andalso not show_structs then |
|
42476 | 519 |
Syntax.const "_struct" $ Syntax.const "_indexdefault" |
520 |
else Syntax.const "_free" $ t |
|
42245 | 521 |
end |
522 |
| mark_atoms (t as Var (xi, T)) = |
|
42288
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42284
diff
changeset
|
523 |
if xi = Syntax_Ext.dddot_indexname then Const ("_DDDOT", T) |
42476 | 524 |
else Syntax.const "_var" $ t |
42245 | 525 |
| mark_atoms a = a; |
526 |
||
527 |
fun prune_typs (t_seen as (Const _, _)) = t_seen |
|
528 |
| prune_typs (t as Free (x, ty), seen) = |
|
529 |
if ty = dummyT then (t, seen) |
|
42290
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42289
diff
changeset
|
530 |
else if not show_free_types orelse member (op aconv) seen t then (Syntax.free x, seen) |
42245 | 531 |
else (t, t :: seen) |
532 |
| prune_typs (t as Var (xi, ty), seen) = |
|
533 |
if ty = dummyT then (t, seen) |
|
42290
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42289
diff
changeset
|
534 |
else if not show_free_types orelse member (op aconv) seen t then (Syntax.var xi, seen) |
42245 | 535 |
else (t, t :: seen) |
536 |
| prune_typs (t_seen as (Bound _, _)) = t_seen |
|
537 |
| prune_typs (Abs (x, ty, t), seen) = |
|
538 |
let val (t', seen') = prune_typs (t, seen); |
|
539 |
in (Abs (x, ty, t'), seen') end |
|
540 |
| prune_typs (t1 $ t2, seen) = |
|
541 |
let |
|
542 |
val (t1', seen') = prune_typs (t1, seen); |
|
543 |
val (t2', seen'') = prune_typs (t2, seen'); |
|
544 |
in (t1' $ t2', seen'') end; |
|
545 |
||
546 |
fun ast_of tm = |
|
547 |
(case strip_comb tm of |
|
42284 | 548 |
(t as Abs _, ts) => Ast.mk_appl (ast_of (Syntax_Trans.abs_tr' ctxt t)) (map ast_of ts) |
42245 | 549 |
| ((c as Const ("_free", _)), Free (x, T) :: ts) => |
42290
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42289
diff
changeset
|
550 |
Ast.mk_appl (constrain (c $ Syntax.free x) T) (map ast_of ts) |
42245 | 551 |
| ((c as Const ("_var", _)), Var (xi, T) :: ts) => |
42290
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42289
diff
changeset
|
552 |
Ast.mk_appl (constrain (c $ Syntax.var xi) T) (map ast_of ts) |
49660
de49d9b4d7bc
more explicit Syntax_Trans.mark_bound_abs/mark_bound_body: preserve type information for show_markup;
wenzelm
parents:
49659
diff
changeset
|
553 |
| ((c as Const ("_bound", B)), Free (x, T) :: ts) => |
de49d9b4d7bc
more explicit Syntax_Trans.mark_bound_abs/mark_bound_body: preserve type information for show_markup;
wenzelm
parents:
49659
diff
changeset
|
554 |
let |
de49d9b4d7bc
more explicit Syntax_Trans.mark_bound_abs/mark_bound_body: preserve type information for show_markup;
wenzelm
parents:
49659
diff
changeset
|
555 |
val X = |
de49d9b4d7bc
more explicit Syntax_Trans.mark_bound_abs/mark_bound_body: preserve type information for show_markup;
wenzelm
parents:
49659
diff
changeset
|
556 |
if show_markup andalso not show_types orelse B <> dummyT then T |
de49d9b4d7bc
more explicit Syntax_Trans.mark_bound_abs/mark_bound_body: preserve type information for show_markup;
wenzelm
parents:
49659
diff
changeset
|
557 |
else dummyT; |
de49d9b4d7bc
more explicit Syntax_Trans.mark_bound_abs/mark_bound_body: preserve type information for show_markup;
wenzelm
parents:
49659
diff
changeset
|
558 |
in Ast.mk_appl (constrain (c $ Syntax.free x) X) (map ast_of ts) end |
42245 | 559 |
| (Const ("_idtdummy", T), ts) => |
42476 | 560 |
Ast.mk_appl (constrain (Syntax.const "_idtdummy") T) (map ast_of ts) |
42245 | 561 |
| (const as Const (c, T), ts) => |
562 |
if show_all_types |
|
563 |
then Ast.mk_appl (constrain const T) (map ast_of ts) |
|
564 |
else trans c T ts |
|
565 |
| (t, ts) => Ast.mk_appl (simple_ast_of ctxt t) (map ast_of ts)) |
|
566 |
||
42254 | 567 |
and trans a T args = ast_of (trf a ctxt T args) |
568 |
handle Match => Ast.mk_appl (Ast.Constant a) (map ast_of args) |
|
42245 | 569 |
|
570 |
and constrain t T = |
|
49657
40e4feac2921
turn constraints into Isabelle_Markup.typing, depending on show_markup options;
wenzelm
parents:
49655
diff
changeset
|
571 |
if (show_types orelse show_markup) andalso T <> dummyT then |
42248 | 572 |
Ast.Appl [Ast.Constant "_constrain", simple_ast_of ctxt t, |
42247
12fe41a92cd5
typed_print_translation: discontinued show_sorts argument;
wenzelm
parents:
42245
diff
changeset
|
573 |
ast_of_termT ctxt trf (term_of_typ ctxt T)] |
42245 | 574 |
else simple_ast_of ctxt t; |
575 |
in |
|
576 |
tm |
|
42284 | 577 |
|> Syntax_Trans.prop_tr' |
49659
251342b60a82
explicit show_types takes preferenced over show_markup;
wenzelm
parents:
49657
diff
changeset
|
578 |
|> show_types ? (#1 o prune_typs o rpair []) |
42245 | 579 |
|> mark_atoms |
580 |
|> ast_of |
|
581 |
end; |
|
582 |
||
583 |
||
584 |
||
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
585 |
(** unparse **) |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
586 |
|
42245 | 587 |
local |
588 |
||
42267
9566078ad905
simplified printer context: uniform externing and static token translations;
wenzelm
parents:
42264
diff
changeset
|
589 |
fun free_or_skolem ctxt x = |
9566078ad905
simplified printer context: uniform externing and static token translations;
wenzelm
parents:
42264
diff
changeset
|
590 |
let |
9566078ad905
simplified printer context: uniform externing and static token translations;
wenzelm
parents:
42264
diff
changeset
|
591 |
val m = |
9566078ad905
simplified printer context: uniform externing and static token translations;
wenzelm
parents:
42264
diff
changeset
|
592 |
if Variable.is_fixed ctxt x orelse Syntax.is_pretty_global ctxt |
45666 | 593 |
then Isabelle_Markup.fixed x |
49358 | 594 |
else Isabelle_Markup.intensify; |
42267
9566078ad905
simplified printer context: uniform externing and static token translations;
wenzelm
parents:
42264
diff
changeset
|
595 |
in |
9566078ad905
simplified printer context: uniform externing and static token translations;
wenzelm
parents:
42264
diff
changeset
|
596 |
if can Name.dest_skolem x |
45666 | 597 |
then ([m, Isabelle_Markup.skolem], Variable.revert_fixed ctxt x) |
598 |
else ([m, Isabelle_Markup.free], x) |
|
42267
9566078ad905
simplified printer context: uniform externing and static token translations;
wenzelm
parents:
42264
diff
changeset
|
599 |
end; |
9566078ad905
simplified printer context: uniform externing and static token translations;
wenzelm
parents:
42264
diff
changeset
|
600 |
|
9566078ad905
simplified printer context: uniform externing and static token translations;
wenzelm
parents:
42264
diff
changeset
|
601 |
fun var_or_skolem s = |
9566078ad905
simplified printer context: uniform externing and static token translations;
wenzelm
parents:
42264
diff
changeset
|
602 |
(case Lexicon.read_variable s of |
9566078ad905
simplified printer context: uniform externing and static token translations;
wenzelm
parents:
42264
diff
changeset
|
603 |
SOME (x, i) => |
9566078ad905
simplified printer context: uniform externing and static token translations;
wenzelm
parents:
42264
diff
changeset
|
604 |
(case try Name.dest_skolem x of |
45666 | 605 |
NONE => (Isabelle_Markup.var, s) |
606 |
| SOME x' => (Isabelle_Markup.skolem, Term.string_of_vname (x', i))) |
|
607 |
| NONE => (Isabelle_Markup.var, s)); |
|
42267
9566078ad905
simplified printer context: uniform externing and static token translations;
wenzelm
parents:
42264
diff
changeset
|
608 |
|
49657
40e4feac2921
turn constraints into Isabelle_Markup.typing, depending on show_markup options;
wenzelm
parents:
49655
diff
changeset
|
609 |
val typing_elem = YXML.output_markup_elem Isabelle_Markup.typing; |
40e4feac2921
turn constraints into Isabelle_Markup.typing, depending on show_markup options;
wenzelm
parents:
49655
diff
changeset
|
610 |
|
42267
9566078ad905
simplified printer context: uniform externing and static token translations;
wenzelm
parents:
42264
diff
changeset
|
611 |
fun unparse_t t_to_ast prt_t markup ctxt t = |
42245 | 612 |
let |
49659
251342b60a82
explicit show_types takes preferenced over show_markup;
wenzelm
parents:
49657
diff
changeset
|
613 |
val show_types = |
251342b60a82
explicit show_types takes preferenced over show_markup;
wenzelm
parents:
49657
diff
changeset
|
614 |
Config.get ctxt show_types orelse Config.get ctxt show_sorts orelse |
251342b60a82
explicit show_types takes preferenced over show_markup;
wenzelm
parents:
49657
diff
changeset
|
615 |
Config.get ctxt show_all_types; |
49657
40e4feac2921
turn constraints into Isabelle_Markup.typing, depending on show_markup options;
wenzelm
parents:
49655
diff
changeset
|
616 |
val show_markup = Config.get ctxt show_markup; |
40e4feac2921
turn constraints into Isabelle_Markup.typing, depending on show_markup options;
wenzelm
parents:
49655
diff
changeset
|
617 |
|
42360 | 618 |
val syn = Proof_Context.syn_of ctxt; |
49657
40e4feac2921
turn constraints into Isabelle_Markup.typing, depending on show_markup options;
wenzelm
parents:
49655
diff
changeset
|
619 |
val prtabs = Syntax.prtabs syn; |
40e4feac2921
turn constraints into Isabelle_Markup.typing, depending on show_markup options;
wenzelm
parents:
49655
diff
changeset
|
620 |
val trf = Syntax.print_ast_translation syn; |
40e4feac2921
turn constraints into Isabelle_Markup.typing, depending on show_markup options;
wenzelm
parents:
49655
diff
changeset
|
621 |
|
40e4feac2921
turn constraints into Isabelle_Markup.typing, depending on show_markup options;
wenzelm
parents:
49655
diff
changeset
|
622 |
fun markup_extern c = |
40e4feac2921
turn constraints into Isabelle_Markup.typing, depending on show_markup options;
wenzelm
parents:
49655
diff
changeset
|
623 |
(case Syntax.lookup_const syn c of |
40e4feac2921
turn constraints into Isabelle_Markup.typing, depending on show_markup options;
wenzelm
parents:
49655
diff
changeset
|
624 |
SOME "" => ([], c) |
40e4feac2921
turn constraints into Isabelle_Markup.typing, depending on show_markup options;
wenzelm
parents:
49655
diff
changeset
|
625 |
| SOME b => markup_extern b |
40e4feac2921
turn constraints into Isabelle_Markup.typing, depending on show_markup options;
wenzelm
parents:
49655
diff
changeset
|
626 |
| NONE => c |> Lexicon.unmark |
40e4feac2921
turn constraints into Isabelle_Markup.typing, depending on show_markup options;
wenzelm
parents:
49655
diff
changeset
|
627 |
{case_class = fn x => (markup_class ctxt x, Proof_Context.extern_class ctxt x), |
40e4feac2921
turn constraints into Isabelle_Markup.typing, depending on show_markup options;
wenzelm
parents:
49655
diff
changeset
|
628 |
case_type = fn x => (markup_type ctxt x, Proof_Context.extern_type ctxt x), |
40e4feac2921
turn constraints into Isabelle_Markup.typing, depending on show_markup options;
wenzelm
parents:
49655
diff
changeset
|
629 |
case_const = fn x => (markup_const ctxt x, Proof_Context.extern_const ctxt x), |
40e4feac2921
turn constraints into Isabelle_Markup.typing, depending on show_markup options;
wenzelm
parents:
49655
diff
changeset
|
630 |
case_fixed = fn x => free_or_skolem ctxt x, |
40e4feac2921
turn constraints into Isabelle_Markup.typing, depending on show_markup options;
wenzelm
parents:
49655
diff
changeset
|
631 |
case_default = fn x => ([], x)}); |
42267
9566078ad905
simplified printer context: uniform externing and static token translations;
wenzelm
parents:
42264
diff
changeset
|
632 |
|
45666 | 633 |
fun token_trans "_tfree" x = SOME (Pretty.mark_str (Isabelle_Markup.tfree, x)) |
634 |
| token_trans "_tvar" x = SOME (Pretty.mark_str (Isabelle_Markup.tvar, x)) |
|
42267
9566078ad905
simplified printer context: uniform externing and static token translations;
wenzelm
parents:
42264
diff
changeset
|
635 |
| token_trans "_free" x = SOME (Pretty.marks_str (free_or_skolem ctxt x)) |
45666 | 636 |
| token_trans "_bound" x = SOME (Pretty.mark_str (Isabelle_Markup.bound, x)) |
48768
abc45de5bb22
clarified "bad" markup: proper body text, invent missing serial on Scala side (counting backwards);
wenzelm
parents:
48756
diff
changeset
|
637 |
| token_trans "_loose" x = SOME (Pretty.mark_str (Isabelle_Markup.bad, x)) |
42267
9566078ad905
simplified printer context: uniform externing and static token translations;
wenzelm
parents:
42264
diff
changeset
|
638 |
| token_trans "_var" x = SOME (Pretty.mark_str (var_or_skolem x)) |
45666 | 639 |
| token_trans "_numeral" x = SOME (Pretty.mark_str (Isabelle_Markup.numeral, x)) |
640 |
| token_trans "_inner_string" x = SOME (Pretty.mark_str (Isabelle_Markup.inner_string, x)) |
|
42267
9566078ad905
simplified printer context: uniform externing and static token translations;
wenzelm
parents:
42264
diff
changeset
|
641 |
| token_trans _ _ = NONE; |
9566078ad905
simplified printer context: uniform externing and static token translations;
wenzelm
parents:
42264
diff
changeset
|
642 |
|
49655 | 643 |
fun markup_trans a [Ast.Variable x] = token_trans a x |
49662
de6be6922c19
proper handling of constraints stemming from idtyp_ast_tr';
wenzelm
parents:
49660
diff
changeset
|
644 |
| markup_trans "_constrain" [t, ty] = constrain_trans t ty |
de6be6922c19
proper handling of constraints stemming from idtyp_ast_tr';
wenzelm
parents:
49660
diff
changeset
|
645 |
| markup_trans "_idtyp" [t, ty] = constrain_trans t ty |
49657
40e4feac2921
turn constraints into Isabelle_Markup.typing, depending on show_markup options;
wenzelm
parents:
49655
diff
changeset
|
646 |
| markup_trans _ _ = NONE |
49655 | 647 |
|
49662
de6be6922c19
proper handling of constraints stemming from idtyp_ast_tr';
wenzelm
parents:
49660
diff
changeset
|
648 |
and constrain_trans t ty = |
de6be6922c19
proper handling of constraints stemming from idtyp_ast_tr';
wenzelm
parents:
49660
diff
changeset
|
649 |
if not show_types andalso show_markup then |
de6be6922c19
proper handling of constraints stemming from idtyp_ast_tr';
wenzelm
parents:
49660
diff
changeset
|
650 |
let |
de6be6922c19
proper handling of constraints stemming from idtyp_ast_tr';
wenzelm
parents:
49660
diff
changeset
|
651 |
val ((bg1, bg2), en) = typing_elem; |
de6be6922c19
proper handling of constraints stemming from idtyp_ast_tr';
wenzelm
parents:
49660
diff
changeset
|
652 |
val bg = bg1 ^ Pretty.symbolic_output (pretty_typ_ast Markup.empty ty) ^ bg2; |
de6be6922c19
proper handling of constraints stemming from idtyp_ast_tr';
wenzelm
parents:
49660
diff
changeset
|
653 |
in SOME (Pretty.raw_markup (bg, en) (0, [pretty_ast Markup.empty t])) end |
de6be6922c19
proper handling of constraints stemming from idtyp_ast_tr';
wenzelm
parents:
49660
diff
changeset
|
654 |
else NONE |
de6be6922c19
proper handling of constraints stemming from idtyp_ast_tr';
wenzelm
parents:
49660
diff
changeset
|
655 |
|
49657
40e4feac2921
turn constraints into Isabelle_Markup.typing, depending on show_markup options;
wenzelm
parents:
49655
diff
changeset
|
656 |
and pretty_typ_ast m ast = ast |
40e4feac2921
turn constraints into Isabelle_Markup.typing, depending on show_markup options;
wenzelm
parents:
49655
diff
changeset
|
657 |
|> Printer.pretty_typ_ast ctxt prtabs trf markup_trans markup_extern |
40e4feac2921
turn constraints into Isabelle_Markup.typing, depending on show_markup options;
wenzelm
parents:
49655
diff
changeset
|
658 |
|> Pretty.markup m |
40e4feac2921
turn constraints into Isabelle_Markup.typing, depending on show_markup options;
wenzelm
parents:
49655
diff
changeset
|
659 |
|
40e4feac2921
turn constraints into Isabelle_Markup.typing, depending on show_markup options;
wenzelm
parents:
49655
diff
changeset
|
660 |
and pretty_ast m ast = ast |
40e4feac2921
turn constraints into Isabelle_Markup.typing, depending on show_markup options;
wenzelm
parents:
49655
diff
changeset
|
661 |
|> prt_t ctxt prtabs trf markup_trans markup_extern |
40e4feac2921
turn constraints into Isabelle_Markup.typing, depending on show_markup options;
wenzelm
parents:
49655
diff
changeset
|
662 |
|> Pretty.markup m; |
42245 | 663 |
in |
42255 | 664 |
t_to_ast ctxt (Syntax.print_translation syn) t |
665 |
|> Ast.normalize ctxt (Syntax.print_rules syn) |
|
49657
40e4feac2921
turn constraints into Isabelle_Markup.typing, depending on show_markup options;
wenzelm
parents:
49655
diff
changeset
|
666 |
|> pretty_ast markup |
42245 | 667 |
end; |
668 |
||
669 |
in |
|
670 |
||
45666 | 671 |
val unparse_sort = unparse_t sort_to_ast Printer.pretty_typ_ast Isabelle_Markup.sort; |
672 |
val unparse_typ = unparse_t typ_to_ast Printer.pretty_typ_ast Isabelle_Markup.typ; |
|
42241
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
673 |
|
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
674 |
fun unparse_term ctxt = |
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
675 |
let |
42360 | 676 |
val thy = Proof_Context.theory_of ctxt; |
677 |
val syn = Proof_Context.syn_of ctxt; |
|
678 |
val idents = Local_Syntax.idents_of (Proof_Context.syntax_of ctxt); |
|
42241
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
679 |
in |
42298
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
wenzelm
parents:
42296
diff
changeset
|
680 |
unparse_t (term_to_ast idents (is_some o Syntax.lookup_const syn)) |
42267
9566078ad905
simplified printer context: uniform externing and static token translations;
wenzelm
parents:
42264
diff
changeset
|
681 |
(Printer.pretty_term_ast (not (Pure_Thy.old_appl_syntax thy))) |
45666 | 682 |
Isabelle_Markup.term ctxt |
42241
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
683 |
end; |
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
684 |
|
42249 | 685 |
end; |
686 |
||
42241
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
687 |
|
42245 | 688 |
|
689 |
(** translations **) |
|
690 |
||
691 |
(* type propositions *) |
|
692 |
||
42247
12fe41a92cd5
typed_print_translation: discontinued show_sorts argument;
wenzelm
parents:
42245
diff
changeset
|
693 |
fun type_prop_tr' ctxt T [Const ("\\<^const>Pure.sort_constraint", _)] = |
42476 | 694 |
Syntax.const "_sort_constraint" $ term_of_typ (Config.put show_sorts true ctxt) T |
42247
12fe41a92cd5
typed_print_translation: discontinued show_sorts argument;
wenzelm
parents:
42245
diff
changeset
|
695 |
| type_prop_tr' ctxt T [t] = |
42476 | 696 |
Syntax.const "_ofclass" $ term_of_typ ctxt T $ t |
42245 | 697 |
| type_prop_tr' _ T ts = raise TYPE ("type_prop_tr'", [T], ts); |
698 |
||
699 |
||
700 |
(* type reflection *) |
|
701 |
||
42247
12fe41a92cd5
typed_print_translation: discontinued show_sorts argument;
wenzelm
parents:
42245
diff
changeset
|
702 |
fun type_tr' ctxt (Type ("itself", [T])) ts = |
42476 | 703 |
Term.list_comb (Syntax.const "_TYPE" $ term_of_typ ctxt T, ts) |
42245 | 704 |
| type_tr' _ _ _ = raise Match; |
705 |
||
706 |
||
707 |
(* type constraints *) |
|
708 |
||
42247
12fe41a92cd5
typed_print_translation: discontinued show_sorts argument;
wenzelm
parents:
42245
diff
changeset
|
709 |
fun type_constraint_tr' ctxt (Type ("fun", [T, _])) (t :: ts) = |
42476 | 710 |
Term.list_comb (Syntax.const "_constrain" $ t $ term_of_typ ctxt T, ts) |
42245 | 711 |
| type_constraint_tr' _ _ _ = raise Match; |
712 |
||
713 |
||
42295
8fdbb3b10beb
moved CONST syntax/translations to their proper place;
wenzelm
parents:
42294
diff
changeset
|
714 |
(* authentic syntax *) |
8fdbb3b10beb
moved CONST syntax/translations to their proper place;
wenzelm
parents:
42294
diff
changeset
|
715 |
|
8fdbb3b10beb
moved CONST syntax/translations to their proper place;
wenzelm
parents:
42294
diff
changeset
|
716 |
fun const_ast_tr intern ctxt [Ast.Variable c] = |
8fdbb3b10beb
moved CONST syntax/translations to their proper place;
wenzelm
parents:
42294
diff
changeset
|
717 |
let |
42360 | 718 |
val Const (c', _) = Proof_Context.read_const_proper ctxt false c; |
42295
8fdbb3b10beb
moved CONST syntax/translations to their proper place;
wenzelm
parents:
42294
diff
changeset
|
719 |
val d = if intern then Lexicon.mark_const c' else c; |
8fdbb3b10beb
moved CONST syntax/translations to their proper place;
wenzelm
parents:
42294
diff
changeset
|
720 |
in Ast.Constant d end |
42470 | 721 |
| const_ast_tr intern ctxt [Ast.Appl [Ast.Constant "_constrain", x, T as Ast.Variable pos]] = |
722 |
(Ast.Appl [Ast.Constant "_constrain", const_ast_tr intern ctxt [x], T] |
|
723 |
handle ERROR msg => |
|
48992 | 724 |
error (msg ^ Position.here (the_default Position.none (Term_Position.decode pos)))) |
42295
8fdbb3b10beb
moved CONST syntax/translations to their proper place;
wenzelm
parents:
42294
diff
changeset
|
725 |
| const_ast_tr _ _ asts = raise Ast.AST ("const_ast_tr", asts); |
8fdbb3b10beb
moved CONST syntax/translations to their proper place;
wenzelm
parents:
42294
diff
changeset
|
726 |
|
8fdbb3b10beb
moved CONST syntax/translations to their proper place;
wenzelm
parents:
42294
diff
changeset
|
727 |
|
42245 | 728 |
(* setup translations *) |
729 |
||
730 |
val _ = Context.>> (Context.map_theory |
|
42295
8fdbb3b10beb
moved CONST syntax/translations to their proper place;
wenzelm
parents:
42294
diff
changeset
|
731 |
(Sign.add_advanced_trfuns |
8fdbb3b10beb
moved CONST syntax/translations to their proper place;
wenzelm
parents:
42294
diff
changeset
|
732 |
([("_context_const", const_ast_tr true), |
8fdbb3b10beb
moved CONST syntax/translations to their proper place;
wenzelm
parents:
42294
diff
changeset
|
733 |
("_context_xconst", const_ast_tr false)], [], [], []) #> |
8fdbb3b10beb
moved CONST syntax/translations to their proper place;
wenzelm
parents:
42294
diff
changeset
|
734 |
Sign.add_advanced_trfunsT |
42245 | 735 |
[("_type_prop", type_prop_tr'), |
736 |
("\\<^const>TYPE", type_tr'), |
|
737 |
("_type_constraint_", type_constraint_tr')])); |
|
738 |
||
739 |
||
740 |
||
42382
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents:
42379
diff
changeset
|
741 |
(** check/uncheck **) |
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents:
42379
diff
changeset
|
742 |
|
45429 | 743 |
(* context-sensitive (un)checking *) |
744 |
||
745 |
type key = int * bool; |
|
746 |
||
747 |
structure Checks = Generic_Data |
|
748 |
( |
|
749 |
type 'a check = 'a list -> Proof.context -> ('a list * Proof.context) option; |
|
750 |
type T = |
|
751 |
((key * ((string * typ check) * stamp) list) list * |
|
752 |
(key * ((string * term check) * stamp) list) list); |
|
753 |
val empty = ([], []); |
|
754 |
val extend = I; |
|
755 |
fun merge ((typ_checks1, term_checks1), (typ_checks2, term_checks2)) : T = |
|
756 |
(AList.join (op =) (K (Library.merge (eq_snd (op =)))) (typ_checks1, typ_checks2), |
|
757 |
AList.join (op =) (K (Library.merge (eq_snd (op =)))) (term_checks1, term_checks2)); |
|
758 |
); |
|
759 |
||
760 |
fun print_checks ctxt = |
|
761 |
let |
|
762 |
fun split_checks checks = |
|
763 |
List.partition (fn ((_, un), _) => not un) checks |
|
764 |
|> pairself (map (fn ((i, _), fs) => (i, map (fst o fst) fs)) |
|
765 |
#> sort (int_ord o pairself fst)); |
|
766 |
fun pretty_checks kind checks = |
|
767 |
checks |> map (fn (i, names) => Pretty.block |
|
768 |
[Pretty.str (kind ^ " (stage " ^ signed_string_of_int i ^ "):"), |
|
769 |
Pretty.brk 1, Pretty.strs names]); |
|
770 |
||
771 |
val (typs, terms) = Checks.get (Context.Proof ctxt); |
|
772 |
val (typ_checks, typ_unchecks) = split_checks typs; |
|
773 |
val (term_checks, term_unchecks) = split_checks terms; |
|
774 |
in |
|
775 |
pretty_checks "typ_checks" typ_checks @ |
|
776 |
pretty_checks "term_checks" term_checks @ |
|
777 |
pretty_checks "typ_unchecks" typ_unchecks @ |
|
778 |
pretty_checks "term_unchecks" term_unchecks |
|
779 |
end |> Pretty.chunks |> Pretty.writeln; |
|
780 |
||
781 |
||
782 |
local |
|
783 |
||
784 |
fun context_check which (key: key) name f = |
|
785 |
Checks.map (which (AList.map_default op = (key, []) (cons ((name, f), stamp ())))); |
|
786 |
||
787 |
fun simple_check eq f xs ctxt = |
|
788 |
let val xs' = f ctxt xs |
|
789 |
in if eq_list eq (xs, xs') then NONE else SOME (xs', ctxt) end; |
|
790 |
||
791 |
in |
|
792 |
||
45444 | 793 |
fun typ_check' stage = context_check apfst (stage, false); |
794 |
fun term_check' stage = context_check apsnd (stage, false); |
|
795 |
fun typ_uncheck' stage = context_check apfst (stage, true); |
|
796 |
fun term_uncheck' stage = context_check apsnd (stage, true); |
|
45429 | 797 |
|
45444 | 798 |
fun typ_check key name f = typ_check' key name (simple_check (op =) f); |
799 |
fun term_check key name f = term_check' key name (simple_check (op aconv) f); |
|
800 |
fun typ_uncheck key name f = typ_uncheck' key name (simple_check (op =) f); |
|
801 |
fun term_uncheck key name f = term_uncheck' key name (simple_check (op aconv) f); |
|
45429 | 802 |
|
803 |
end; |
|
804 |
||
805 |
||
806 |
local |
|
807 |
||
808 |
fun check_stage fs = perhaps_loop (perhaps_apply (map uncurry fs)); |
|
809 |
fun check_all fs = perhaps_apply (map check_stage fs); |
|
810 |
||
811 |
fun check which uncheck ctxt0 xs0 = |
|
812 |
let |
|
813 |
val funs = which (Checks.get (Context.Proof ctxt0)) |
|
814 |
|> map_filter (fn ((i, u), fs) => if uncheck = u then SOME (i, map (snd o fst) fs) else NONE) |
|
815 |
|> Library.sort (int_ord o pairself fst) |> map snd |
|
816 |
|> not uncheck ? map rev; |
|
817 |
in #1 (perhaps (check_all funs) (xs0, ctxt0)) end; |
|
818 |
||
819 |
val apply_typ_check = check fst false; |
|
820 |
val apply_term_check = check snd false; |
|
821 |
val apply_typ_uncheck = check fst true; |
|
822 |
val apply_term_uncheck = check snd true; |
|
823 |
||
824 |
in |
|
825 |
||
45423
92f91f990165
tuned signature -- emphasize internal role of these operations;
wenzelm
parents:
45412
diff
changeset
|
826 |
fun check_typs ctxt = |
45453 | 827 |
Proof_Context.prepare_sorts ctxt #> |
45429 | 828 |
apply_typ_check ctxt #> |
45427
fca432074fb2
sort assignment before simultaneous term_check, not isolated parse_term;
wenzelm
parents:
45423
diff
changeset
|
829 |
Term_Sharing.typs (Proof_Context.theory_of ctxt); |
45423
92f91f990165
tuned signature -- emphasize internal role of these operations;
wenzelm
parents:
45412
diff
changeset
|
830 |
|
45445
41e641a870de
pass term positions into check phase, where resulting types are reported accordingly, and eventually shown as tooltips;
wenzelm
parents:
45444
diff
changeset
|
831 |
fun check_terms ctxt raw_ts = |
41e641a870de
pass term positions into check phase, where resulting types are reported accordingly, and eventually shown as tooltips;
wenzelm
parents:
45444
diff
changeset
|
832 |
let |
45448
018f8959c7a6
more efficient prepare_sorts -- bypass encoded positions;
wenzelm
parents:
45447
diff
changeset
|
833 |
val (ts, ps) = raw_ts |
45453 | 834 |
|> Term.burrow_types (Proof_Context.prepare_sorts ctxt) |
45448
018f8959c7a6
more efficient prepare_sorts -- bypass encoded positions;
wenzelm
parents:
45447
diff
changeset
|
835 |
|> Type_Infer_Context.prepare_positions ctxt; |
45445
41e641a870de
pass term positions into check phase, where resulting types are reported accordingly, and eventually shown as tooltips;
wenzelm
parents:
45444
diff
changeset
|
836 |
val tys = map (Logic.mk_type o snd) ps; |
45448
018f8959c7a6
more efficient prepare_sorts -- bypass encoded positions;
wenzelm
parents:
45447
diff
changeset
|
837 |
val (ts', tys') = ts @ tys |
45445
41e641a870de
pass term positions into check phase, where resulting types are reported accordingly, and eventually shown as tooltips;
wenzelm
parents:
45444
diff
changeset
|
838 |
|> apply_term_check ctxt |
41e641a870de
pass term positions into check phase, where resulting types are reported accordingly, and eventually shown as tooltips;
wenzelm
parents:
45444
diff
changeset
|
839 |
|> chop (length ts); |
41e641a870de
pass term positions into check phase, where resulting types are reported accordingly, and eventually shown as tooltips;
wenzelm
parents:
45444
diff
changeset
|
840 |
val _ = |
41e641a870de
pass term positions into check phase, where resulting types are reported accordingly, and eventually shown as tooltips;
wenzelm
parents:
45444
diff
changeset
|
841 |
map2 (fn (pos, _) => fn ty => |
41e641a870de
pass term positions into check phase, where resulting types are reported accordingly, and eventually shown as tooltips;
wenzelm
parents:
45444
diff
changeset
|
842 |
if Position.is_reported pos then |
45666 | 843 |
Markup.markup (Position.markup pos Isabelle_Markup.typing) |
45445
41e641a870de
pass term positions into check phase, where resulting types are reported accordingly, and eventually shown as tooltips;
wenzelm
parents:
45444
diff
changeset
|
844 |
(Syntax.string_of_typ ctxt (Logic.dest_type ty)) |
41e641a870de
pass term positions into check phase, where resulting types are reported accordingly, and eventually shown as tooltips;
wenzelm
parents:
45444
diff
changeset
|
845 |
else "") ps tys' |
41e641a870de
pass term positions into check phase, where resulting types are reported accordingly, and eventually shown as tooltips;
wenzelm
parents:
45444
diff
changeset
|
846 |
|> implode |> Output.report |
41e641a870de
pass term positions into check phase, where resulting types are reported accordingly, and eventually shown as tooltips;
wenzelm
parents:
45444
diff
changeset
|
847 |
in Term_Sharing.terms (Proof_Context.theory_of ctxt) ts' end; |
45423
92f91f990165
tuned signature -- emphasize internal role of these operations;
wenzelm
parents:
45412
diff
changeset
|
848 |
|
42382
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents:
42379
diff
changeset
|
849 |
fun check_props ctxt = map (Type.constraint propT) #> check_terms ctxt; |
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents:
42379
diff
changeset
|
850 |
|
45429 | 851 |
val uncheck_typs = apply_typ_uncheck; |
852 |
val uncheck_terms = apply_term_uncheck; |
|
853 |
||
854 |
end; |
|
855 |
||
856 |
||
857 |
(* standard phases *) |
|
858 |
||
859 |
val _ = Context.>> |
|
860 |
(typ_check 0 "standard" Proof_Context.standard_typ_check #> |
|
861 |
term_check 0 "standard" |
|
862 |
(fn ctxt => Type_Infer_Context.infer_types ctxt #> map (Proof_Context.expand_abbrevs ctxt)) #> |
|
863 |
term_check 100 "standard_finish" Proof_Context.standard_term_check_finish #> |
|
864 |
term_uncheck 0 "standard" Proof_Context.standard_term_uncheck); |
|
42382
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents:
42379
diff
changeset
|
865 |
|
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents:
42379
diff
changeset
|
866 |
|
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents:
42379
diff
changeset
|
867 |
|
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
868 |
(** install operations **) |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
869 |
|
42241
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
870 |
val _ = Syntax.install_operations |
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
871 |
{parse_sort = parse_sort, |
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
872 |
parse_typ = parse_typ, |
42281 | 873 |
parse_term = parse_term false, |
874 |
parse_prop = parse_term true, |
|
42241
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
875 |
unparse_sort = unparse_sort, |
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
876 |
unparse_typ = unparse_typ, |
42382
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents:
42379
diff
changeset
|
877 |
unparse_term = unparse_term, |
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents:
42379
diff
changeset
|
878 |
check_typs = check_typs, |
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents:
42379
diff
changeset
|
879 |
check_terms = check_terms, |
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents:
42379
diff
changeset
|
880 |
check_props = check_props, |
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents:
42379
diff
changeset
|
881 |
uncheck_typs = uncheck_typs, |
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
wenzelm
parents:
42379
diff
changeset
|
882 |
uncheck_terms = uncheck_terms}; |
42241
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
883 |
|
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
884 |
end; |