author | wenzelm |
Wed, 06 Apr 2011 17:15:06 +0200 | |
changeset 42255 | 097c93dcd877 |
parent 42254 | f427c9890c46 |
child 42264 | b6c1b0c4c511 |
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 |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
10 |
val term_sorts: term -> (indexname * sort) list |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
11 |
val typ_of_term: (indexname -> sort) -> term -> typ |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
12 |
val decode_term: Proof.context -> |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
13 |
Position.reports * term Exn.result -> Position.reports * term Exn.result |
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 |
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
16 |
end |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
17 |
|
42243 | 18 |
structure Syntax_Phases: SYNTAX_PHASES = |
42241
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
19 |
struct |
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
20 |
|
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
21 |
(** decode parse trees **) |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
22 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
23 |
(* sort_of_term *) |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
24 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
25 |
fun sort_of_term tm = |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
26 |
let |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
27 |
fun err () = raise TERM ("sort_of_term: bad encoding of classes", [tm]); |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
28 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
29 |
fun class s = Lexicon.unmark_class s handle Fail _ => err (); |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
30 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
31 |
fun classes (Const (s, _)) = [class s] |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
32 |
| classes (Const ("_classes", _) $ Const (s, _) $ cs) = class s :: classes cs |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
33 |
| classes _ = err (); |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
34 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
35 |
fun sort (Const ("_topsort", _)) = [] |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
36 |
| sort (Const (s, _)) = [class s] |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
37 |
| sort (Const ("_sort", _) $ cs) = classes cs |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
38 |
| sort _ = err (); |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
39 |
in sort tm end; |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
40 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
41 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
42 |
(* term_sorts *) |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
43 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
44 |
fun term_sorts tm = |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
45 |
let |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
46 |
val sort_of = sort_of_term; |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
47 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
48 |
fun add_env (Const ("_ofsort", _) $ Free (x, _) $ cs) = |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
49 |
insert (op =) ((x, ~1), sort_of cs) |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
50 |
| add_env (Const ("_ofsort", _) $ (Const ("_tfree", _) $ Free (x, _)) $ cs) = |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
51 |
insert (op =) ((x, ~1), sort_of cs) |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
52 |
| add_env (Const ("_ofsort", _) $ Var (xi, _) $ cs) = |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
53 |
insert (op =) (xi, sort_of cs) |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
54 |
| add_env (Const ("_ofsort", _) $ (Const ("_tvar", _) $ Var (xi, _)) $ cs) = |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
55 |
insert (op =) (xi, sort_of cs) |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
56 |
| add_env (Abs (_, _, t)) = add_env t |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
57 |
| add_env (t1 $ t2) = add_env t1 #> add_env t2 |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
58 |
| add_env _ = I; |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
59 |
in add_env tm [] end; |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
60 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
61 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
62 |
(* typ_of_term *) |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
63 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
64 |
fun typ_of_term get_sort tm = |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
65 |
let |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
66 |
fun err () = raise TERM ("typ_of_term: bad encoding of type", [tm]); |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
67 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
68 |
fun typ_of (Free (x, _)) = TFree (x, get_sort (x, ~1)) |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
69 |
| typ_of (Var (xi, _)) = TVar (xi, get_sort xi) |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
70 |
| typ_of (Const ("_tfree",_) $ (t as Free _)) = typ_of t |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
71 |
| typ_of (Const ("_tvar",_) $ (t as Var _)) = typ_of t |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
72 |
| typ_of (Const ("_ofsort", _) $ Free (x, _) $ _) = TFree (x, get_sort (x, ~1)) |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
73 |
| typ_of (Const ("_ofsort", _) $ (Const ("_tfree",_) $ Free (x, _)) $ _) = |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
74 |
TFree (x, get_sort (x, ~1)) |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
75 |
| typ_of (Const ("_ofsort", _) $ Var (xi, _) $ _) = TVar (xi, get_sort xi) |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
76 |
| typ_of (Const ("_ofsort", _) $ (Const ("_tvar",_) $ Var (xi, _)) $ _) = |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
77 |
TVar (xi, get_sort xi) |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
78 |
| typ_of (Const ("_dummy_ofsort", _) $ t) = TFree ("'_dummy_", sort_of_term t) |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
79 |
| typ_of t = |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
80 |
let |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
81 |
val (head, args) = Term.strip_comb t; |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
82 |
val a = |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
83 |
(case head of |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
84 |
Const (c, _) => (Lexicon.unmark_type c handle Fail _ => err ()) |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
85 |
| _ => err ()); |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
86 |
in Type (a, map typ_of args) end; |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
87 |
in typ_of tm end; |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
88 |
|
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 |
(* parsetree_to_ast *) |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
91 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
92 |
fun parsetree_to_ast ctxt constrain_pos trf parsetree = |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
93 |
let |
42250
cc5ac538f991
eliminated odd object-oriented type_context/term_context;
wenzelm
parents:
42249
diff
changeset
|
94 |
val tsig = ProofContext.tsig_of ctxt; |
cc5ac538f991
eliminated odd object-oriented type_context/term_context;
wenzelm
parents:
42249
diff
changeset
|
95 |
|
cc5ac538f991
eliminated odd object-oriented type_context/term_context;
wenzelm
parents:
42249
diff
changeset
|
96 |
val get_class = ProofContext.read_class ctxt; |
cc5ac538f991
eliminated odd object-oriented type_context/term_context;
wenzelm
parents:
42249
diff
changeset
|
97 |
val get_type = #1 o dest_Type o ProofContext.read_type_name_proper ctxt false; |
cc5ac538f991
eliminated odd object-oriented type_context/term_context;
wenzelm
parents:
42249
diff
changeset
|
98 |
fun markup_class c = [Name_Space.markup_entry (Type.class_space tsig) c]; |
cc5ac538f991
eliminated odd object-oriented type_context/term_context;
wenzelm
parents:
42249
diff
changeset
|
99 |
fun markup_type c = [Name_Space.markup_entry (Type.type_space tsig) c]; |
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
100 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
101 |
val reports = Unsynchronized.ref ([]: Position.reports); |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
102 |
fun report pos = Position.reports reports [pos]; |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
103 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
104 |
fun trans a args = |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
105 |
(case trf a of |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
106 |
NONE => Ast.mk_appl (Ast.Constant a) args |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
107 |
| SOME f => f ctxt args); |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
108 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
109 |
fun ast_of (Parser.Node ("_class_name", [Parser.Tip tok])) = |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
110 |
let |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
111 |
val c = get_class (Lexicon.str_of_token tok); |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
112 |
val _ = report (Lexicon.pos_of_token tok) markup_class c; |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
113 |
in Ast.Constant (Lexicon.mark_class c) end |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
114 |
| ast_of (Parser.Node ("_type_name", [Parser.Tip tok])) = |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
115 |
let |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
116 |
val c = get_type (Lexicon.str_of_token tok); |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
117 |
val _ = report (Lexicon.pos_of_token tok) markup_type c; |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
118 |
in Ast.Constant (Lexicon.mark_type c) end |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
119 |
| ast_of (Parser.Node ("_constrain_position", [pt as Parser.Tip tok])) = |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
120 |
if constrain_pos then |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
121 |
Ast.Appl [Ast.Constant "_constrain", ast_of pt, |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
122 |
Ast.Variable (Lexicon.encode_position (Lexicon.pos_of_token tok))] |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
123 |
else ast_of pt |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
124 |
| ast_of (Parser.Node (a, pts)) = trans a (map ast_of pts) |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
125 |
| ast_of (Parser.Tip tok) = Ast.Variable (Lexicon.str_of_token tok); |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
126 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
127 |
val ast = Exn.interruptible_capture ast_of parsetree; |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
128 |
in (! reports, ast) end; |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
129 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
130 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
131 |
(* ast_to_term *) |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
132 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
133 |
fun ast_to_term ctxt trf = |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
134 |
let |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
135 |
fun trans a args = |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
136 |
(case trf a of |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
137 |
NONE => Term.list_comb (Lexicon.const a, args) |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
138 |
| SOME f => f ctxt args); |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
139 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
140 |
fun term_of (Ast.Constant a) = trans a [] |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
141 |
| term_of (Ast.Variable x) = Lexicon.read_var x |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
142 |
| term_of (Ast.Appl (Ast.Constant a :: (asts as _ :: _))) = |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
143 |
trans a (map term_of asts) |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
144 |
| term_of (Ast.Appl (ast :: (asts as _ :: _))) = |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
145 |
Term.list_comb (term_of ast, map term_of asts) |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
146 |
| 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
|
147 |
in term_of end; |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
148 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
149 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
150 |
(* decode_term -- transform parse tree into raw term *) |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
151 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
152 |
fun decode_term _ (result as (_: Position.reports, Exn.Exn _)) = result |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
153 |
| decode_term ctxt (reports0, Exn.Result tm) = |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
154 |
let |
42250
cc5ac538f991
eliminated odd object-oriented type_context/term_context;
wenzelm
parents:
42249
diff
changeset
|
155 |
val consts = ProofContext.consts_of ctxt; |
cc5ac538f991
eliminated odd object-oriented type_context/term_context;
wenzelm
parents:
42249
diff
changeset
|
156 |
fun get_const a = |
cc5ac538f991
eliminated odd object-oriented type_context/term_context;
wenzelm
parents:
42249
diff
changeset
|
157 |
((true, #1 (Term.dest_Const (ProofContext.read_const_proper ctxt false a))) |
cc5ac538f991
eliminated odd object-oriented type_context/term_context;
wenzelm
parents:
42249
diff
changeset
|
158 |
handle ERROR _ => (false, Consts.intern consts a)); |
cc5ac538f991
eliminated odd object-oriented type_context/term_context;
wenzelm
parents:
42249
diff
changeset
|
159 |
val get_free = ProofContext.intern_skolem ctxt; |
cc5ac538f991
eliminated odd object-oriented type_context/term_context;
wenzelm
parents:
42249
diff
changeset
|
160 |
fun markup_const c = [Name_Space.markup_entry (Consts.space_of consts) c]; |
cc5ac538f991
eliminated odd object-oriented type_context/term_context;
wenzelm
parents:
42249
diff
changeset
|
161 |
fun markup_free x = |
cc5ac538f991
eliminated odd object-oriented type_context/term_context;
wenzelm
parents:
42249
diff
changeset
|
162 |
[if can Name.dest_skolem x then Markup.skolem else Markup.free] @ |
cc5ac538f991
eliminated odd object-oriented type_context/term_context;
wenzelm
parents:
42249
diff
changeset
|
163 |
(if not (Variable.is_body ctxt) orelse Variable.is_fixed ctxt x then [] |
cc5ac538f991
eliminated odd object-oriented type_context/term_context;
wenzelm
parents:
42249
diff
changeset
|
164 |
else [Markup.hilite]); |
cc5ac538f991
eliminated odd object-oriented type_context/term_context;
wenzelm
parents:
42249
diff
changeset
|
165 |
fun markup_var xi = [Markup.name (Term.string_of_vname xi) Markup.var]; |
cc5ac538f991
eliminated odd object-oriented type_context/term_context;
wenzelm
parents:
42249
diff
changeset
|
166 |
fun markup_bound def id = |
cc5ac538f991
eliminated odd object-oriented type_context/term_context;
wenzelm
parents:
42249
diff
changeset
|
167 |
[Markup.properties [(if def then Markup.defN else Markup.refN, id)] Markup.bound]; |
cc5ac538f991
eliminated odd object-oriented type_context/term_context;
wenzelm
parents:
42249
diff
changeset
|
168 |
|
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
169 |
val decodeT = typ_of_term (ProofContext.get_sort ctxt (term_sorts tm)); |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
170 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
171 |
val reports = Unsynchronized.ref reports0; |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
172 |
fun report ps = Position.reports reports ps; |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
173 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
174 |
fun decode ps qs bs (Const ("_constrain", _) $ t $ typ) = |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
175 |
(case Syntax.decode_position_term typ of |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
176 |
SOME p => decode (p :: ps) qs bs t |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
177 |
| NONE => Type.constraint (decodeT typ) (decode ps qs bs t)) |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
178 |
| decode ps qs bs (Const ("_constrainAbs", _) $ t $ typ) = |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
179 |
(case Syntax.decode_position_term typ of |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
180 |
SOME q => decode ps (q :: qs) bs t |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
181 |
| NONE => Type.constraint (decodeT typ --> dummyT) (decode ps qs bs t)) |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
182 |
| decode _ qs bs (Abs (x, T, t)) = |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
183 |
let |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
184 |
val id = serial_string (); |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
185 |
val _ = report qs (markup_bound true) id; |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
186 |
in Abs (x, T, decode [] [] (id :: bs) t) end |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
187 |
| decode _ _ bs (t $ u) = decode [] [] bs t $ decode [] [] bs u |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
188 |
| decode ps _ _ (Const (a, T)) = |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
189 |
(case try Lexicon.unmark_fixed a of |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
190 |
SOME x => (report ps markup_free x; Free (x, T)) |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
191 |
| NONE => |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
192 |
let |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
193 |
val c = |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
194 |
(case try Lexicon.unmark_const a of |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
195 |
SOME c => c |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
196 |
| NONE => snd (get_const a)); |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
197 |
val _ = report ps markup_const c; |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
198 |
in Const (c, T) end) |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
199 |
| decode ps _ _ (Free (a, T)) = |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
200 |
(case (get_free a, get_const a) of |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
201 |
(SOME x, _) => (report ps markup_free x; Free (x, T)) |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
202 |
| (_, (true, c)) => (report ps markup_const c; Const (c, T)) |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
203 |
| (_, (false, c)) => |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
204 |
if Long_Name.is_qualified c |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
205 |
then (report ps markup_const c; Const (c, T)) |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
206 |
else (report ps markup_free c; Free (c, T))) |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
207 |
| 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
|
208 |
| decode ps _ bs (t as Bound i) = |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
209 |
(case try (nth bs) i of |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
210 |
SOME id => (report ps (markup_bound false) id; t) |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
211 |
| NONE => t); |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
212 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
213 |
val tm' = Exn.interruptible_capture (fn () => decode [] [] [] tm) (); |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
214 |
in (! reports, tm') end; |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
215 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
216 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
217 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
218 |
(** parse **) |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
219 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
220 |
(* results *) |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
221 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
222 |
fun ambiguity_msg pos = "Parse error: ambiguous syntax" ^ Position.str_of pos; |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
223 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
224 |
fun proper_results results = map_filter (fn (y, Exn.Result x) => SOME (y, x) | _ => NONE) results; |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
225 |
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
|
226 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
227 |
fun report ctxt = List.app (fn (pos, m) => Context_Position.report ctxt pos m); |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
228 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
229 |
fun report_result ctxt pos results = |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
230 |
(case (proper_results results, failed_results results) of |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
231 |
([], (reports, exn) :: _) => (report ctxt reports; reraise exn) |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
232 |
| ([(reports, x)], _) => (report ctxt reports; x) |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
233 |
| _ => error (ambiguity_msg pos)); |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
234 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
235 |
|
42249 | 236 |
(* parse raw asts *) |
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
237 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
238 |
fun parse_asts ctxt raw root (syms, pos) = |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
239 |
let |
42251 | 240 |
val syn = ProofContext.syn_of ctxt; |
42253 | 241 |
val ast_tr = Syntax.parse_ast_translation syn; |
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
242 |
|
42251 | 243 |
val toks = Syntax.tokenize syn raw syms; |
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
244 |
val _ = List.app (Lexicon.report_token ctxt) toks; |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
245 |
|
42251 | 246 |
val pts = Syntax.parse ctxt syn root (filter Lexicon.is_proper toks) |
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
247 |
handle ERROR msg => |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
248 |
error (msg ^ |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
249 |
implode (map (Markup.markup Markup.report o Lexicon.reported_token_range ctxt) toks)); |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
250 |
val len = length pts; |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
251 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
252 |
val limit = Config.get ctxt Syntax.ambiguity_limit; |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
253 |
val _ = |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
254 |
if len <= Config.get ctxt Syntax.ambiguity_level then () |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
255 |
else if not (Config.get ctxt Syntax.ambiguity_enabled) then error (ambiguity_msg pos) |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
256 |
else |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
257 |
(Context_Position.if_visible ctxt warning (cat_lines |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
258 |
(("Ambiguous input" ^ Position.str_of pos ^ |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
259 |
"\nproduces " ^ string_of_int len ^ " parse trees" ^ |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
260 |
(if len <= limit then "" else " (" ^ string_of_int limit ^ " displayed)") ^ ":") :: |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
261 |
map (Pretty.string_of o Parser.pretty_parsetree) (take limit pts)))); |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
262 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
263 |
val constrain_pos = not raw andalso Config.get ctxt Syntax.positions; |
42253 | 264 |
val parsetree_to_ast = parsetree_to_ast ctxt constrain_pos ast_tr; |
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
265 |
in map parsetree_to_ast pts end; |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
266 |
|
42249 | 267 |
fun parse_raw ctxt root input = |
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
268 |
let |
42253 | 269 |
val syn = ProofContext.syn_of ctxt; |
270 |
val tr = Syntax.parse_translation syn; |
|
42255 | 271 |
val parse_rules = Syntax.parse_rules syn; |
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
272 |
in |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
273 |
parse_asts ctxt false root input |
42255 | 274 |
|> (map o apsnd o Exn.maps_result) |
275 |
(Ast.normalize ctxt parse_rules #> Exn.interruptible_capture (ast_to_term ctxt tr)) |
|
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
276 |
end; |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
277 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
278 |
|
42249 | 279 |
(* parse logical entities *) |
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
280 |
|
42241
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
281 |
fun parse_failed ctxt pos msg kind = |
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
282 |
cat_error msg ("Failed to parse " ^ kind ^ |
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
283 |
Markup.markup Markup.report (Context_Position.reported_text ctxt pos Markup.bad "")); |
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
284 |
|
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
285 |
fun parse_sort ctxt text = |
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
286 |
let |
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
287 |
val (syms, pos) = Syntax.parse_token ctxt Markup.sort text; |
42249 | 288 |
val S = |
289 |
parse_raw ctxt "sort" (syms, pos) |
|
290 |
|> report_result ctxt pos |
|
291 |
|> sort_of_term |
|
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
292 |
handle ERROR msg => parse_failed ctxt pos msg "sort"; |
42241
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
293 |
in Type.minimize_sort (ProofContext.tsig_of ctxt) S end; |
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
294 |
|
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
295 |
fun parse_typ ctxt text = |
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
296 |
let |
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
297 |
val (syms, pos) = Syntax.parse_token ctxt Markup.typ text; |
42249 | 298 |
val T = |
299 |
parse_raw ctxt "type" (syms, pos) |
|
300 |
|> report_result ctxt pos |
|
301 |
|> (fn t => typ_of_term (ProofContext.get_sort ctxt (term_sorts t)) t) |
|
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
302 |
handle ERROR msg => parse_failed ctxt pos msg "type"; |
42241
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
303 |
in T end; |
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
304 |
|
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
305 |
fun parse_term T ctxt text = |
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
306 |
let |
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
307 |
val (T', _) = Type_Infer.paramify_dummies T 0; |
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
308 |
val (markup, kind) = |
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
309 |
if T' = propT then (Markup.prop, "proposition") else (Markup.term, "term"); |
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
310 |
val (syms, pos) = Syntax.parse_token ctxt markup text; |
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
311 |
|
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
312 |
val default_root = Config.get ctxt Syntax.default_root; |
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
313 |
val root = |
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
314 |
(case T' of |
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
315 |
Type (c, _) => |
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
316 |
if c <> "prop" andalso Type.is_logtype (ProofContext.tsig_of ctxt) c |
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
317 |
then default_root else c |
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
318 |
| _ => default_root); |
42249 | 319 |
in |
320 |
let |
|
321 |
val results = parse_raw ctxt root (syms, pos) |> map (decode_term ctxt); |
|
322 |
val ambiguity = length (proper_results results); |
|
42241
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
323 |
|
42249 | 324 |
val level = Config.get ctxt Syntax.ambiguity_level; |
325 |
val limit = Config.get ctxt Syntax.ambiguity_limit; |
|
326 |
||
327 |
fun ambig_msg () = |
|
328 |
if ambiguity > 1 andalso ambiguity <= level then |
|
329 |
"Got more than one parse tree.\n\ |
|
330 |
\Retry with smaller syntax_ambiguity_level for more information." |
|
331 |
else ""; |
|
332 |
||
333 |
(*brute-force disambiguation via type-inference*) |
|
334 |
fun check t = (Syntax.check_term ctxt (Type.constraint T' t); Exn.Result t) |
|
335 |
handle exn as ERROR _ => Exn.Exn exn; |
|
336 |
||
337 |
val results' = |
|
338 |
if ambiguity > 1 then |
|
339 |
(Par_List.map_name "Syntax_Phases.parse_term" o apsnd o Exn.maps_result) |
|
340 |
check results |
|
341 |
else results; |
|
342 |
val reports' = fst (hd results'); |
|
343 |
||
344 |
val errs = map snd (failed_results results'); |
|
345 |
val checked = map snd (proper_results results'); |
|
346 |
val len = length checked; |
|
347 |
||
348 |
val show_term = Syntax.string_of_term (Config.put Syntax.show_brackets true ctxt); |
|
349 |
in |
|
350 |
if len = 0 then |
|
351 |
report_result ctxt pos |
|
352 |
[(reports', Exn.Exn (Exn.EXCEPTIONS (ERROR (ambig_msg ()) :: errs)))] |
|
353 |
else if len = 1 then |
|
354 |
(if ambiguity > level then |
|
355 |
Context_Position.if_visible ctxt warning |
|
356 |
"Fortunately, only one parse tree is type correct.\n\ |
|
357 |
\You may still want to disambiguate your grammar or your input." |
|
358 |
else (); report_result ctxt pos results') |
|
359 |
else |
|
360 |
report_result ctxt pos |
|
361 |
[(reports', Exn.Exn (ERROR (cat_lines (ambig_msg () :: |
|
362 |
(("Ambiguous input, " ^ string_of_int len ^ " terms are type correct" ^ |
|
363 |
(if len <= limit then "" else " (" ^ string_of_int limit ^ " displayed)") ^ ":") :: |
|
364 |
map show_term (take limit checked))))))] |
|
365 |
end handle ERROR msg => parse_failed ctxt pos msg kind |
|
366 |
end; |
|
42241
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
367 |
|
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
368 |
|
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
369 |
(* parse_ast_pattern *) |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
370 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
371 |
fun parse_ast_pattern ctxt (root, str) = |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
372 |
let |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
373 |
val syn = ProofContext.syn_of ctxt; |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
374 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
375 |
fun constify (ast as Ast.Constant _) = ast |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
376 |
| constify (ast as Ast.Variable x) = |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
377 |
if Syntax.is_const syn x orelse Long_Name.is_qualified x then Ast.Constant x |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
378 |
else ast |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
379 |
| constify (Ast.Appl asts) = Ast.Appl (map constify asts); |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
380 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
381 |
val (syms, pos) = Syntax.read_token str; |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
382 |
in |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
383 |
parse_asts ctxt true root (syms, pos) |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
384 |
|> report_result ctxt pos |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
385 |
|> constify |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
386 |
end; |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
387 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
388 |
|
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
389 |
|
42245 | 390 |
(** encode parse trees **) |
391 |
||
392 |
(* term_of_sort *) |
|
393 |
||
394 |
fun term_of_sort S = |
|
395 |
let |
|
396 |
val class = Lexicon.const o Lexicon.mark_class; |
|
397 |
||
398 |
fun classes [c] = class c |
|
399 |
| classes (c :: cs) = Lexicon.const "_classes" $ class c $ classes cs; |
|
400 |
in |
|
401 |
(case S of |
|
402 |
[] => Lexicon.const "_topsort" |
|
403 |
| [c] => class c |
|
404 |
| cs => Lexicon.const "_sort" $ classes cs) |
|
405 |
end; |
|
406 |
||
407 |
||
408 |
(* term_of_typ *) |
|
409 |
||
42247
12fe41a92cd5
typed_print_translation: discontinued show_sorts argument;
wenzelm
parents:
42245
diff
changeset
|
410 |
fun term_of_typ ctxt ty = |
42245 | 411 |
let |
42247
12fe41a92cd5
typed_print_translation: discontinued show_sorts argument;
wenzelm
parents:
42245
diff
changeset
|
412 |
val show_sorts = Config.get ctxt show_sorts; |
12fe41a92cd5
typed_print_translation: discontinued show_sorts argument;
wenzelm
parents:
42245
diff
changeset
|
413 |
|
42245 | 414 |
fun of_sort t S = |
415 |
if show_sorts then Lexicon.const "_ofsort" $ t $ term_of_sort S |
|
416 |
else t; |
|
417 |
||
418 |
fun term_of (Type (a, Ts)) = |
|
419 |
Term.list_comb (Lexicon.const (Lexicon.mark_type a), map term_of Ts) |
|
420 |
| term_of (TFree (x, S)) = |
|
421 |
if is_some (Lexicon.decode_position x) then Lexicon.free x |
|
422 |
else of_sort (Lexicon.const "_tfree" $ Lexicon.free x) S |
|
423 |
| term_of (TVar (xi, S)) = of_sort (Lexicon.const "_tvar" $ Lexicon.var xi) S; |
|
424 |
in term_of ty end; |
|
425 |
||
426 |
||
427 |
(* simple_ast_of *) |
|
428 |
||
429 |
fun simple_ast_of ctxt = |
|
430 |
let |
|
431 |
val tune_var = if Config.get ctxt show_question_marks then I else unprefix "?"; |
|
432 |
fun ast_of (Const (c, _)) = Ast.Constant c |
|
433 |
| ast_of (Free (x, _)) = Ast.Variable x |
|
434 |
| ast_of (Var (xi, _)) = Ast.Variable (tune_var (Term.string_of_vname xi)) |
|
435 |
| ast_of (t as _ $ _) = |
|
436 |
let val (f, args) = strip_comb t |
|
437 |
in Ast.mk_appl (ast_of f) (map ast_of args) end |
|
438 |
| ast_of (Bound i) = Ast.Variable ("B." ^ string_of_int i) |
|
439 |
| ast_of (Abs _) = raise Fail "simple_ast_of: Abs"; |
|
440 |
in ast_of end; |
|
441 |
||
442 |
||
443 |
(* sort_to_ast and typ_to_ast *) |
|
444 |
||
445 |
fun ast_of_termT ctxt trf tm = |
|
446 |
let |
|
447 |
val ctxt' = Config.put show_sorts false ctxt; |
|
448 |
fun ast_of (t as Const ("_tfree", _) $ Free _) = simple_ast_of ctxt t |
|
449 |
| ast_of (t as Const ("_tvar", _) $ Var _) = simple_ast_of ctxt t |
|
450 |
| ast_of (Const (a, _)) = trans a [] |
|
451 |
| ast_of (t as _ $ _) = |
|
452 |
(case strip_comb t of |
|
453 |
(Const (a, _), args) => trans a args |
|
454 |
| (f, args) => Ast.Appl (map ast_of (f :: args))) |
|
455 |
| ast_of t = simple_ast_of ctxt t |
|
42254 | 456 |
and trans a args = ast_of (trf a ctxt' dummyT args) |
457 |
handle Match => Ast.mk_appl (Ast.Constant a) (map ast_of args); |
|
42245 | 458 |
in ast_of tm end; |
459 |
||
460 |
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
|
461 |
fun typ_to_ast ctxt trf T = ast_of_termT ctxt trf (term_of_typ ctxt T); |
42245 | 462 |
|
463 |
||
464 |
(* term_to_ast *) |
|
465 |
||
42252 | 466 |
fun term_to_ast idents is_syntax_const ctxt trf tm = |
42245 | 467 |
let |
468 |
val show_types = |
|
469 |
Config.get ctxt show_types orelse Config.get ctxt show_sorts orelse |
|
470 |
Config.get ctxt show_all_types; |
|
471 |
val show_structs = Config.get ctxt show_structs; |
|
472 |
val show_free_types = Config.get ctxt show_free_types; |
|
473 |
val show_all_types = Config.get ctxt show_all_types; |
|
474 |
||
475 |
val {structs, fixes} = idents; |
|
476 |
||
42249 | 477 |
fun mark_atoms ((t as Const (c, _)) $ u) = |
42245 | 478 |
if member (op =) Syntax.standard_token_markers c |
479 |
then t $ u else mark_atoms t $ mark_atoms u |
|
480 |
| mark_atoms (t $ u) = mark_atoms t $ mark_atoms u |
|
481 |
| mark_atoms (Abs (x, T, t)) = Abs (x, T, mark_atoms t) |
|
482 |
| mark_atoms (t as Const (c, T)) = |
|
42252 | 483 |
if is_syntax_const c then t |
42245 | 484 |
else Const (Lexicon.mark_const c, T) |
485 |
| mark_atoms (t as Free (x, T)) = |
|
486 |
let val i = find_index (fn s => s = x) structs + 1 in |
|
487 |
if i = 0 andalso member (op =) fixes x then |
|
488 |
Const (Lexicon.mark_fixed x, T) |
|
489 |
else if i = 1 andalso not show_structs then |
|
490 |
Lexicon.const "_struct" $ Lexicon.const "_indexdefault" |
|
491 |
else Lexicon.const "_free" $ t |
|
492 |
end |
|
493 |
| mark_atoms (t as Var (xi, T)) = |
|
494 |
if xi = Syntax.dddot_indexname then Const ("_DDDOT", T) |
|
495 |
else Lexicon.const "_var" $ t |
|
496 |
| mark_atoms a = a; |
|
497 |
||
498 |
fun prune_typs (t_seen as (Const _, _)) = t_seen |
|
499 |
| prune_typs (t as Free (x, ty), seen) = |
|
500 |
if ty = dummyT then (t, seen) |
|
501 |
else if not show_free_types orelse member (op aconv) seen t then (Lexicon.free x, seen) |
|
502 |
else (t, t :: seen) |
|
503 |
| prune_typs (t as Var (xi, ty), seen) = |
|
504 |
if ty = dummyT then (t, seen) |
|
505 |
else if not show_free_types orelse member (op aconv) seen t then (Lexicon.var xi, seen) |
|
506 |
else (t, t :: seen) |
|
507 |
| prune_typs (t_seen as (Bound _, _)) = t_seen |
|
508 |
| prune_typs (Abs (x, ty, t), seen) = |
|
509 |
let val (t', seen') = prune_typs (t, seen); |
|
510 |
in (Abs (x, ty, t'), seen') end |
|
511 |
| prune_typs (t1 $ t2, seen) = |
|
512 |
let |
|
513 |
val (t1', seen') = prune_typs (t1, seen); |
|
514 |
val (t2', seen'') = prune_typs (t2, seen'); |
|
515 |
in (t1' $ t2', seen'') end; |
|
516 |
||
517 |
fun ast_of tm = |
|
518 |
(case strip_comb tm of |
|
519 |
(t as Abs _, ts) => Ast.mk_appl (ast_of (Syn_Trans.abs_tr' ctxt t)) (map ast_of ts) |
|
520 |
| ((c as Const ("_free", _)), Free (x, T) :: ts) => |
|
521 |
Ast.mk_appl (constrain (c $ Lexicon.free x) T) (map ast_of ts) |
|
522 |
| ((c as Const ("_var", _)), Var (xi, T) :: ts) => |
|
523 |
Ast.mk_appl (constrain (c $ Lexicon.var xi) T) (map ast_of ts) |
|
524 |
| ((c as Const ("_bound", _)), Free (x, T) :: ts) => |
|
525 |
Ast.mk_appl (constrain (c $ Lexicon.free x) T) (map ast_of ts) |
|
526 |
| (Const ("_idtdummy", T), ts) => |
|
527 |
Ast.mk_appl (constrain (Lexicon.const "_idtdummy") T) (map ast_of ts) |
|
528 |
| (const as Const (c, T), ts) => |
|
529 |
if show_all_types |
|
530 |
then Ast.mk_appl (constrain const T) (map ast_of ts) |
|
531 |
else trans c T ts |
|
532 |
| (t, ts) => Ast.mk_appl (simple_ast_of ctxt t) (map ast_of ts)) |
|
533 |
||
42254 | 534 |
and trans a T args = ast_of (trf a ctxt T args) |
535 |
handle Match => Ast.mk_appl (Ast.Constant a) (map ast_of args) |
|
42245 | 536 |
|
537 |
and constrain t T = |
|
538 |
if show_types andalso T <> dummyT then |
|
42248 | 539 |
Ast.Appl [Ast.Constant "_constrain", simple_ast_of ctxt t, |
42247
12fe41a92cd5
typed_print_translation: discontinued show_sorts argument;
wenzelm
parents:
42245
diff
changeset
|
540 |
ast_of_termT ctxt trf (term_of_typ ctxt T)] |
42245 | 541 |
else simple_ast_of ctxt t; |
542 |
in |
|
543 |
tm |
|
544 |
|> Syn_Trans.prop_tr' |
|
545 |
|> show_types ? (#1 o prune_typs o rpair []) |
|
546 |
|> mark_atoms |
|
547 |
|> ast_of |
|
548 |
end; |
|
549 |
||
550 |
||
551 |
||
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
552 |
(** unparse **) |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
553 |
|
42245 | 554 |
local |
555 |
||
556 |
fun unparse_t t_to_ast prt_t markup ctxt curried t = |
|
557 |
let |
|
42253 | 558 |
val syn = ProofContext.syn_of ctxt; |
559 |
val tokentr = Syntax.token_translation syn (print_mode_value ()); |
|
42245 | 560 |
in |
42255 | 561 |
t_to_ast ctxt (Syntax.print_translation syn) t |
562 |
|> Ast.normalize ctxt (Syntax.print_rules syn) |
|
563 |
|> prt_t ctxt curried (Syntax.prtabs syn) (Syntax.print_ast_translation syn) tokentr |
|
42253 | 564 |
|> Pretty.markup markup |
42245 | 565 |
end; |
566 |
||
567 |
in |
|
568 |
||
42241
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
569 |
fun unparse_sort ctxt = |
42249 | 570 |
let |
571 |
val tsig = ProofContext.tsig_of ctxt; |
|
572 |
val extern = {extern_class = Type.extern_class tsig, extern_type = I}; |
|
42252 | 573 |
in unparse_t sort_to_ast (Printer.pretty_typ_ast extern) Markup.sort ctxt false end; |
42241
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
574 |
|
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
575 |
fun unparse_typ ctxt = |
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
576 |
let |
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
577 |
val tsig = ProofContext.tsig_of ctxt; |
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
578 |
val extern = {extern_class = Type.extern_class tsig, extern_type = Type.extern_type tsig}; |
42252 | 579 |
in unparse_t typ_to_ast (Printer.pretty_typ_ast extern) Markup.typ ctxt false end; |
42241
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
580 |
|
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
581 |
fun unparse_term ctxt = |
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
582 |
let |
42252 | 583 |
val thy = ProofContext.theory_of ctxt; |
584 |
val syn = ProofContext.syn_of ctxt; |
|
42241
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
585 |
val tsig = ProofContext.tsig_of ctxt; |
42252 | 586 |
val idents = Local_Syntax.idents_of (ProofContext.syntax_of ctxt); |
587 |
val is_syntax_const = Syntax.is_const syn; |
|
42241
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
588 |
val consts = ProofContext.consts_of ctxt; |
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
589 |
val extern = |
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
590 |
{extern_class = Type.extern_class tsig, |
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
591 |
extern_type = Type.extern_type tsig, |
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
592 |
extern_const = Consts.extern consts}; |
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
593 |
in |
42252 | 594 |
unparse_t (term_to_ast idents is_syntax_const) (Printer.pretty_term_ast extern) |
595 |
Markup.term ctxt (not (Pure_Thy.old_appl_syntax thy)) |
|
42241
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
596 |
end; |
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
597 |
|
42249 | 598 |
end; |
599 |
||
42241
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
600 |
|
42245 | 601 |
|
602 |
(** translations **) |
|
603 |
||
604 |
(* type propositions *) |
|
605 |
||
42247
12fe41a92cd5
typed_print_translation: discontinued show_sorts argument;
wenzelm
parents:
42245
diff
changeset
|
606 |
fun type_prop_tr' ctxt T [Const ("\\<^const>Pure.sort_constraint", _)] = |
12fe41a92cd5
typed_print_translation: discontinued show_sorts argument;
wenzelm
parents:
42245
diff
changeset
|
607 |
Lexicon.const "_sort_constraint" $ term_of_typ (Config.put show_sorts true ctxt) T |
12fe41a92cd5
typed_print_translation: discontinued show_sorts argument;
wenzelm
parents:
42245
diff
changeset
|
608 |
| type_prop_tr' ctxt T [t] = |
12fe41a92cd5
typed_print_translation: discontinued show_sorts argument;
wenzelm
parents:
42245
diff
changeset
|
609 |
Lexicon.const "_ofclass" $ term_of_typ ctxt T $ t |
42245 | 610 |
| type_prop_tr' _ T ts = raise TYPE ("type_prop_tr'", [T], ts); |
611 |
||
612 |
||
613 |
(* type reflection *) |
|
614 |
||
42247
12fe41a92cd5
typed_print_translation: discontinued show_sorts argument;
wenzelm
parents:
42245
diff
changeset
|
615 |
fun type_tr' ctxt (Type ("itself", [T])) ts = |
12fe41a92cd5
typed_print_translation: discontinued show_sorts argument;
wenzelm
parents:
42245
diff
changeset
|
616 |
Term.list_comb (Lexicon.const "_TYPE" $ term_of_typ ctxt T, ts) |
42245 | 617 |
| type_tr' _ _ _ = raise Match; |
618 |
||
619 |
||
620 |
(* type constraints *) |
|
621 |
||
42247
12fe41a92cd5
typed_print_translation: discontinued show_sorts argument;
wenzelm
parents:
42245
diff
changeset
|
622 |
fun type_constraint_tr' ctxt (Type ("fun", [T, _])) (t :: ts) = |
42248 | 623 |
Term.list_comb (Lexicon.const "_constrain" $ t $ term_of_typ ctxt T, ts) |
42245 | 624 |
| type_constraint_tr' _ _ _ = raise Match; |
625 |
||
626 |
||
627 |
(* setup translations *) |
|
628 |
||
629 |
val _ = Context.>> (Context.map_theory |
|
42247
12fe41a92cd5
typed_print_translation: discontinued show_sorts argument;
wenzelm
parents:
42245
diff
changeset
|
630 |
(Sign.add_advanced_trfunsT |
42245 | 631 |
[("_type_prop", type_prop_tr'), |
632 |
("\\<^const>TYPE", type_tr'), |
|
633 |
("_type_constraint_", type_constraint_tr')])); |
|
634 |
||
635 |
||
636 |
||
42242
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
637 |
(** install operations **) |
39261908e12f
moved decode/parse operations to standard_syntax.ML;
wenzelm
parents:
42241
diff
changeset
|
638 |
|
42241
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
639 |
val _ = Syntax.install_operations |
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
640 |
{parse_sort = parse_sort, |
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
641 |
parse_typ = parse_typ, |
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
642 |
parse_term = parse_term dummyT, |
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
643 |
parse_prop = parse_term propT, |
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
644 |
unparse_sort = unparse_sort, |
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
645 |
unparse_typ = unparse_typ, |
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
646 |
unparse_term = unparse_term}; |
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
647 |
|
dd8029f71e1c
separate module for standard implementation of inner syntax operations;
wenzelm
parents:
diff
changeset
|
648 |
end; |