author | wenzelm |
Fri, 19 Dec 2014 17:23:56 +0100 | |
changeset 59152 | 66e6c539a36d |
parent 56438 | 7f6b2634d853 |
child 62529 | 8b7bdfc09f3b |
permissions | -rw-r--r-- |
42284 | 1 |
(* Title: Pure/Syntax/syntax_trans.ML |
548 | 2 |
Author: Tobias Nipkow and Markus Wenzel, TU Muenchen |
3 |
||
4 |
Syntax translation functions. |
|
5 |
*) |
|
6 |
||
42284 | 7 |
signature BASIC_SYNTAX_TRANS = |
8 |
sig |
|
9 |
val eta_contract: bool Config.T |
|
10 |
end |
|
11 |
||
12 |
signature SYNTAX_TRANS = |
|
2698 | 13 |
sig |
42284 | 14 |
include BASIC_SYNTAX_TRANS |
45057
86c9b73158a8
default print mode for Isabelle/Scala, not just Isabelle/jEdit;
wenzelm
parents:
44433
diff
changeset
|
15 |
val bracketsN: string |
86c9b73158a8
default print mode for Isabelle/Scala, not just Isabelle/jEdit;
wenzelm
parents:
44433
diff
changeset
|
16 |
val no_bracketsN: string |
42284 | 17 |
val no_brackets: unit -> bool |
45057
86c9b73158a8
default print mode for Isabelle/Scala, not just Isabelle/jEdit;
wenzelm
parents:
44433
diff
changeset
|
18 |
val type_bracketsN: string |
86c9b73158a8
default print mode for Isabelle/Scala, not just Isabelle/jEdit;
wenzelm
parents:
44433
diff
changeset
|
19 |
val no_type_bracketsN: string |
42284 | 20 |
val no_type_brackets: unit -> bool |
21 |
val abs_tr: term list -> term |
|
52143 | 22 |
val mk_binder_tr: string * string -> string * (Proof.context -> term list -> term) |
42284 | 23 |
val antiquote_tr: string -> term -> term |
24 |
val quote_tr: string -> term -> term |
|
52143 | 25 |
val quote_antiquote_tr: string -> string -> string -> |
26 |
string * (Proof.context -> term list -> term) |
|
27 |
val non_typed_tr': (Proof.context -> term list -> term) -> |
|
28 |
Proof.context -> typ -> term list -> term |
|
42284 | 29 |
val tappl_ast_tr': Ast.ast * Ast.ast list -> Ast.ast |
30 |
val appl_ast_tr': Ast.ast * Ast.ast list -> Ast.ast |
|
31 |
val applC_ast_tr': Ast.ast * Ast.ast list -> Ast.ast |
|
39163
4d701c0388c3
more explicit indication of Config.raw options, which are only needed for bootstrapping Pure;
wenzelm
parents:
39128
diff
changeset
|
32 |
val eta_contract_raw: Config.raw |
49660
de49d9b4d7bc
more explicit Syntax_Trans.mark_bound_abs/mark_bound_body: preserve type information for show_markup;
wenzelm
parents:
46236
diff
changeset
|
33 |
val mark_bound_abs: string * typ -> term |
de49d9b4d7bc
more explicit Syntax_Trans.mark_bound_abs/mark_bound_body: preserve type information for show_markup;
wenzelm
parents:
46236
diff
changeset
|
34 |
val mark_bound_body: string * typ -> term |
42284 | 35 |
val bound_vars: (string * typ) list -> term -> term |
36 |
val abs_tr': Proof.context -> term -> term |
|
13762 | 37 |
val atomic_abs_tr': string * typ * term -> term * term |
42086
74bf78db0d87
added Syntax.const_abs_tr' with proper eta_abs and Term.is_dependent;
wenzelm
parents:
42085
diff
changeset
|
38 |
val const_abs_tr': term -> term |
52143 | 39 |
val mk_binder_tr': string * string -> string * (Proof.context -> term list -> term) |
40 |
val preserve_binder_abs_tr': string -> string -> string * (Proof.context -> term list -> term) |
|
41 |
val preserve_binder_abs2_tr': string -> string -> string * (Proof.context -> term list -> term) |
|
42284 | 42 |
val variant_abs: string * typ * term -> string * term |
43 |
val variant_abs': string * typ * term -> string * term |
|
548 | 44 |
val dependent_tr': string * string -> term list -> term |
8577 | 45 |
val antiquote_tr': string -> term -> term |
46 |
val quote_tr': string -> term -> term |
|
52143 | 47 |
val quote_antiquote_tr': string -> string -> string -> |
48 |
string * (Proof.context -> term list -> term) |
|
35145
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
wenzelm
parents:
32786
diff
changeset
|
49 |
val update_name_tr': term -> term |
59152
66e6c539a36d
more frugal Local_Syntax.init -- maintain idents within context;
wenzelm
parents:
56438
diff
changeset
|
50 |
val get_idents: Proof.context -> {structs: string list, fixes: string list} |
66e6c539a36d
more frugal Local_Syntax.init -- maintain idents within context;
wenzelm
parents:
56438
diff
changeset
|
51 |
val put_idents: {structs: string list, fixes: string list} -> Proof.context -> Proof.context |
52143 | 52 |
val pure_parse_ast_translation: (string * (Proof.context -> Ast.ast list -> Ast.ast)) list |
53 |
val pure_parse_translation: (string * (Proof.context -> term list -> term)) list |
|
54 |
val pure_print_ast_translation: (string * (Proof.context -> Ast.ast list -> Ast.ast)) list |
|
2698 | 55 |
end; |
548 | 56 |
|
42284 | 57 |
structure Syntax_Trans: SYNTAX_TRANS = |
548 | 58 |
struct |
2698 | 59 |
|
42476 | 60 |
structure Syntax = Lexicon.Syntax; |
61 |
||
62 |
||
42262 | 63 |
(* print mode *) |
64 |
||
65 |
val bracketsN = "brackets"; |
|
66 |
val no_bracketsN = "no_brackets"; |
|
67 |
||
68 |
fun no_brackets () = |
|
69 |
find_first (fn mode => mode = bracketsN orelse mode = no_bracketsN) |
|
70 |
(print_mode_value ()) = SOME no_bracketsN; |
|
71 |
||
72 |
val type_bracketsN = "type_brackets"; |
|
73 |
val no_type_bracketsN = "no_type_brackets"; |
|
74 |
||
75 |
fun no_type_brackets () = |
|
76 |
find_first (fn mode => mode = type_bracketsN orelse mode = no_type_bracketsN) |
|
77 |
(print_mode_value ()) <> SOME type_bracketsN; |
|
78 |
||
79 |
||
2698 | 80 |
|
548 | 81 |
(** parse (ast) translations **) |
82 |
||
42057
3eba96ff3d3e
more selective strip_positions in case patterns -- reactivate translations based on "case _ of _" in HOL and special patterns in HOLCF;
wenzelm
parents:
42055
diff
changeset
|
83 |
(* strip_positions *) |
3eba96ff3d3e
more selective strip_positions in case patterns -- reactivate translations based on "case _ of _" in HOL and special patterns in HOLCF;
wenzelm
parents:
42055
diff
changeset
|
84 |
|
42264 | 85 |
fun strip_positions_ast_tr [ast] = Ast.strip_positions ast |
42057
3eba96ff3d3e
more selective strip_positions in case patterns -- reactivate translations based on "case _ of _" in HOL and special patterns in HOLCF;
wenzelm
parents:
42055
diff
changeset
|
86 |
| strip_positions_ast_tr asts = raise Ast.AST ("strip_positions_ast_tr", asts); |
3eba96ff3d3e
more selective strip_positions in case patterns -- reactivate translations based on "case _ of _" in HOL and special patterns in HOLCF;
wenzelm
parents:
42055
diff
changeset
|
87 |
|
3eba96ff3d3e
more selective strip_positions in case patterns -- reactivate translations based on "case _ of _" in HOL and special patterns in HOLCF;
wenzelm
parents:
42055
diff
changeset
|
88 |
|
11491 | 89 |
(* constify *) |
90 |
||
46236
ae79f2978a67
position constraints for numerals enable PIDE markup;
wenzelm
parents:
45389
diff
changeset
|
91 |
fun constify_ast_tr [Ast.Appl [c as Ast.Constant "_constrain", ast1, ast2]] = |
ae79f2978a67
position constraints for numerals enable PIDE markup;
wenzelm
parents:
45389
diff
changeset
|
92 |
Ast.Appl [c, constify_ast_tr [ast1], ast2] |
ae79f2978a67
position constraints for numerals enable PIDE markup;
wenzelm
parents:
45389
diff
changeset
|
93 |
| constify_ast_tr [Ast.Variable c] = Ast.Constant c |
11491 | 94 |
| constify_ast_tr asts = raise Ast.AST ("constify_ast_tr", asts); |
95 |
||
96 |
||
42262 | 97 |
(* type syntax *) |
98 |
||
99 |
fun tapp_ast_tr [ty, c] = Ast.Appl [c, ty] |
|
100 |
| tapp_ast_tr asts = raise Ast.AST ("tapp_ast_tr", asts); |
|
101 |
||
102 |
fun tappl_ast_tr [ty, tys, c] = Ast.mk_appl c (ty :: Ast.unfold_ast "_types" tys) |
|
103 |
| tappl_ast_tr asts = raise Ast.AST ("tappl_ast_tr", asts); |
|
104 |
||
105 |
fun bracket_ast_tr [dom, cod] = Ast.fold_ast_p "\\<^type>fun" (Ast.unfold_ast "_types" dom, cod) |
|
106 |
| bracket_ast_tr asts = raise Ast.AST ("bracket_ast_tr", asts); |
|
107 |
||
108 |
||
548 | 109 |
(* application *) |
110 |
||
5690 | 111 |
fun appl_ast_tr [f, args] = Ast.Appl (f :: Ast.unfold_ast "_args" args) |
112 |
| appl_ast_tr asts = raise Ast.AST ("appl_ast_tr", asts); |
|
922
196ca0973a6d
added CPure (curried functions) and ProtoPure (ancestor of Pure and CPure)
clasohm
parents:
639
diff
changeset
|
113 |
|
5690 | 114 |
fun applC_ast_tr [f, args] = Ast.Appl (f :: Ast.unfold_ast "_cargs" args) |
115 |
| applC_ast_tr asts = raise Ast.AST ("applC_ast_tr", asts); |
|
548 | 116 |
|
117 |
||
118 |
(* abstraction *) |
|
119 |
||
42278 | 120 |
fun idtyp_ast_tr [x, ty] = Ast.Appl [Ast.Constant "_constrain", x, ty] |
121 |
| idtyp_ast_tr asts = raise Ast.AST ("idtyp_ast_tr", asts); |
|
548 | 122 |
|
42278 | 123 |
fun lambda_ast_tr [pats, body] = Ast.fold_ast_p "_abs" (Ast.unfold_ast "_pttrns" pats, body) |
124 |
| lambda_ast_tr asts = raise Ast.AST ("lambda_ast_tr", asts); |
|
548 | 125 |
|
44241 | 126 |
fun absfree_proper (x, T) t = |
55948 | 127 |
if Name.is_internal x |
42048
afd11ca8e018
support for encoded positions (for id_position, longid_position) as pseudo type-constraints -- still inactive;
wenzelm
parents:
42045
diff
changeset
|
128 |
then error ("Illegal internal variable in abstraction: " ^ quote x) |
44241 | 129 |
else absfree (x, T) t; |
21773 | 130 |
|
44241 | 131 |
fun abs_tr [Free x, t] = absfree_proper x t |
132 |
| abs_tr [Const ("_idtdummy", T), t] = absdummy T t |
|
42284 | 133 |
| abs_tr [Const ("_constrain", _) $ x $ tT, t] = |
42476 | 134 |
Syntax.const "_constrainAbs" $ abs_tr [x, t] $ tT |
42048
afd11ca8e018
support for encoded positions (for id_position, longid_position) as pseudo type-constraints -- still inactive;
wenzelm
parents:
42045
diff
changeset
|
135 |
| abs_tr ts = raise TERM ("abs_tr", ts); |
548 | 136 |
|
137 |
||
138 |
(* binder *) |
|
139 |
||
21535 | 140 |
fun mk_binder_tr (syn, name) = |
548 | 141 |
let |
42055 | 142 |
fun err ts = raise TERM ("binder_tr: " ^ syn, ts) |
42048
afd11ca8e018
support for encoded positions (for id_position, longid_position) as pseudo type-constraints -- still inactive;
wenzelm
parents:
42045
diff
changeset
|
143 |
fun binder_tr [Const ("_idts", _) $ idt $ idts, t] = binder_tr [idt, binder_tr [idts, t]] |
afd11ca8e018
support for encoded positions (for id_position, longid_position) as pseudo type-constraints -- still inactive;
wenzelm
parents:
42045
diff
changeset
|
144 |
| binder_tr [x, t] = |
42055 | 145 |
let val abs = abs_tr [x, t] handle TERM _ => err [x, t] |
42476 | 146 |
in Syntax.const name $ abs end |
42055 | 147 |
| binder_tr ts = err ts; |
52143 | 148 |
in (syn, fn _ => binder_tr) end; |
548 | 149 |
|
150 |
||
28628 | 151 |
(* type propositions *) |
152 |
||
35255 | 153 |
fun mk_type ty = |
42476 | 154 |
Syntax.const "_constrain" $ |
56243 | 155 |
Syntax.const "\\<^const>Pure.type" $ (Syntax.const "\\<^type>itself" $ ty); |
28628 | 156 |
|
42278 | 157 |
fun ofclass_tr [ty, cls] = cls $ mk_type ty |
158 |
| ofclass_tr ts = raise TERM ("ofclass_tr", ts); |
|
28628 | 159 |
|
42476 | 160 |
fun sort_constraint_tr [ty] = Syntax.const "\\<^const>Pure.sort_constraint" $ mk_type ty |
42278 | 161 |
| sort_constraint_tr ts = raise TERM ("sort_constraint_tr", ts); |
28628 | 162 |
|
163 |
||
548 | 164 |
(* meta propositions *) |
165 |
||
42476 | 166 |
fun aprop_tr [t] = Syntax.const "_constrain" $ t $ Syntax.const "\\<^type>prop" |
42278 | 167 |
| aprop_tr ts = raise TERM ("aprop_tr", ts); |
548 | 168 |
|
169 |
||
170 |
(* meta implication *) |
|
171 |
||
42278 | 172 |
fun bigimpl_ast_tr (asts as [asms, concl]) = |
16612
48be8ef738df
transform_failure in translation functions: TRANSLATION_FAIL;
wenzelm
parents:
15570
diff
changeset
|
173 |
let val prems = |
48be8ef738df
transform_failure in translation functions: TRANSLATION_FAIL;
wenzelm
parents:
15570
diff
changeset
|
174 |
(case Ast.unfold_ast_p "_asms" asms of |
48be8ef738df
transform_failure in translation functions: TRANSLATION_FAIL;
wenzelm
parents:
15570
diff
changeset
|
175 |
(asms', Ast.Appl [Ast.Constant "_asm", asm']) => asms' @ [asm'] |
48be8ef738df
transform_failure in translation functions: TRANSLATION_FAIL;
wenzelm
parents:
15570
diff
changeset
|
176 |
| _ => raise Ast.AST ("bigimpl_ast_tr", asts)) |
56245 | 177 |
in Ast.fold_ast_p "\\<^const>Pure.imp" (prems, concl) end |
42278 | 178 |
| bigimpl_ast_tr asts = raise Ast.AST ("bigimpl_ast_tr", asts); |
15421
fcf747c0b6b8
Syntax: last premise of "_bigimpl" is wrapped with "_asm", to have a hook for
schirmer
parents:
14981
diff
changeset
|
179 |
|
548 | 180 |
|
23824
8ad7131dbfcf
moved print_translations from Pure.thy to Syntax/syn_trans.ML;
wenzelm
parents:
21773
diff
changeset
|
181 |
(* type/term reflection *) |
4148 | 182 |
|
42278 | 183 |
fun type_tr [ty] = mk_type ty |
184 |
| type_tr ts = raise TERM ("type_tr", ts); |
|
4148 | 185 |
|
548 | 186 |
|
6761 | 187 |
(* dddot *) |
188 |
||
42476 | 189 |
fun dddot_tr ts = Term.list_comb (Syntax.var Syntax_Ext.dddot_indexname, ts); |
6761 | 190 |
|
191 |
||
5084 | 192 |
(* quote / antiquote *) |
193 |
||
8577 | 194 |
fun antiquote_tr name = |
195 |
let |
|
196 |
fun tr i ((t as Const (c, _)) $ u) = |
|
197 |
if c = name then tr i u $ Bound i |
|
198 |
else tr i t $ tr i u |
|
199 |
| tr i (t $ u) = tr i t $ tr i u |
|
200 |
| tr i (Abs (x, T, t)) = Abs (x, T, tr (i + 1) t) |
|
201 |
| tr _ a = a; |
|
202 |
in tr 0 end; |
|
203 |
||
204 |
fun quote_tr name t = Abs ("s", dummyT, antiquote_tr name (Term.incr_boundvars 1 t)); |
|
205 |
||
5084 | 206 |
fun quote_antiquote_tr quoteN antiquoteN name = |
207 |
let |
|
42476 | 208 |
fun tr [t] = Syntax.const name $ quote_tr antiquoteN t |
8577 | 209 |
| tr ts = raise TERM ("quote_tr", ts); |
52143 | 210 |
in (quoteN, fn _ => tr) end; |
5084 | 211 |
|
212 |
||
35145
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
wenzelm
parents:
32786
diff
changeset
|
213 |
(* corresponding updates *) |
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
wenzelm
parents:
32786
diff
changeset
|
214 |
|
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
wenzelm
parents:
32786
diff
changeset
|
215 |
fun update_name_tr (Free (x, T) :: ts) = list_comb (Free (suffix "_update" x, T), ts) |
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
wenzelm
parents:
32786
diff
changeset
|
216 |
| update_name_tr (Const (x, T) :: ts) = list_comb (Const (suffix "_update" x, T), ts) |
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
wenzelm
parents:
32786
diff
changeset
|
217 |
| update_name_tr (((c as Const ("_constrain", _)) $ t $ ty) :: ts) = |
42264 | 218 |
if Term_Position.is_position ty then list_comb (c $ update_name_tr [t] $ ty, ts) |
42053
006095137a81
update_name_tr: more precise handling of explicit constraints, including positions;
wenzelm
parents:
42048
diff
changeset
|
219 |
else |
006095137a81
update_name_tr: more precise handling of explicit constraints, including positions;
wenzelm
parents:
42048
diff
changeset
|
220 |
list_comb (c $ update_name_tr [t] $ |
42080
58b465952287
update_name: more uniform treatment of type constraints (NB: type equality is hard to establish in parse trees);
wenzelm
parents:
42057
diff
changeset
|
221 |
(Lexicon.fun_type $ |
58b465952287
update_name: more uniform treatment of type constraints (NB: type equality is hard to establish in parse trees);
wenzelm
parents:
42057
diff
changeset
|
222 |
(Lexicon.fun_type $ Lexicon.dummy_type $ ty) $ Lexicon.dummy_type), ts) |
35145
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
wenzelm
parents:
32786
diff
changeset
|
223 |
| update_name_tr ts = raise TERM ("update_name_tr", ts); |
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
wenzelm
parents:
32786
diff
changeset
|
224 |
|
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
wenzelm
parents:
32786
diff
changeset
|
225 |
|
14697 | 226 |
(* indexed syntax *) |
227 |
||
59152
66e6c539a36d
more frugal Local_Syntax.init -- maintain idents within context;
wenzelm
parents:
56438
diff
changeset
|
228 |
structure Idents = Proof_Data |
66e6c539a36d
more frugal Local_Syntax.init -- maintain idents within context;
wenzelm
parents:
56438
diff
changeset
|
229 |
( |
66e6c539a36d
more frugal Local_Syntax.init -- maintain idents within context;
wenzelm
parents:
56438
diff
changeset
|
230 |
type T = {structs: string list, fixes: string list}; |
66e6c539a36d
more frugal Local_Syntax.init -- maintain idents within context;
wenzelm
parents:
56438
diff
changeset
|
231 |
fun init _ : T = {structs = [], fixes = []}; |
66e6c539a36d
more frugal Local_Syntax.init -- maintain idents within context;
wenzelm
parents:
56438
diff
changeset
|
232 |
); |
66e6c539a36d
more frugal Local_Syntax.init -- maintain idents within context;
wenzelm
parents:
56438
diff
changeset
|
233 |
|
66e6c539a36d
more frugal Local_Syntax.init -- maintain idents within context;
wenzelm
parents:
56438
diff
changeset
|
234 |
val get_idents = Idents.get; |
66e6c539a36d
more frugal Local_Syntax.init -- maintain idents within context;
wenzelm
parents:
56438
diff
changeset
|
235 |
val put_idents = Idents.put; |
66e6c539a36d
more frugal Local_Syntax.init -- maintain idents within context;
wenzelm
parents:
56438
diff
changeset
|
236 |
|
45389
bc0d50f8ae19
discontinued numbered structure indexes (legacy feature);
wenzelm
parents:
45057
diff
changeset
|
237 |
fun indexdefault_ast_tr [] = |
bc0d50f8ae19
discontinued numbered structure indexes (legacy feature);
wenzelm
parents:
45057
diff
changeset
|
238 |
Ast.Appl [Ast.Constant "_index", |
bc0d50f8ae19
discontinued numbered structure indexes (legacy feature);
wenzelm
parents:
45057
diff
changeset
|
239 |
Ast.Appl [Ast.Constant "_struct", Ast.Constant "_indexdefault"]] |
bc0d50f8ae19
discontinued numbered structure indexes (legacy feature);
wenzelm
parents:
45057
diff
changeset
|
240 |
| indexdefault_ast_tr asts = raise Ast.AST ("indexdefault_ast_tr", asts); |
bc0d50f8ae19
discontinued numbered structure indexes (legacy feature);
wenzelm
parents:
45057
diff
changeset
|
241 |
|
bc0d50f8ae19
discontinued numbered structure indexes (legacy feature);
wenzelm
parents:
45057
diff
changeset
|
242 |
fun indexvar_ast_tr [] = Ast.Appl [Ast.Constant "_index", Ast.Variable "some_index"] |
bc0d50f8ae19
discontinued numbered structure indexes (legacy feature);
wenzelm
parents:
45057
diff
changeset
|
243 |
| indexvar_ast_tr asts = raise Ast.AST ("indexvar_ast_tr", asts); |
bc0d50f8ae19
discontinued numbered structure indexes (legacy feature);
wenzelm
parents:
45057
diff
changeset
|
244 |
|
42278 | 245 |
fun struct_ast_tr [Ast.Appl [Ast.Constant "_index", ast]] = ast |
246 |
| struct_ast_tr asts = Ast.mk_appl (Ast.Constant "_struct") asts; |
|
14697 | 247 |
|
42278 | 248 |
fun index_tr [t] = t |
249 |
| index_tr ts = raise TERM ("index_tr", ts); |
|
14697 | 250 |
|
59152
66e6c539a36d
more frugal Local_Syntax.init -- maintain idents within context;
wenzelm
parents:
56438
diff
changeset
|
251 |
fun struct_tr ctxt [Const ("_indexdefault", _)] = |
66e6c539a36d
more frugal Local_Syntax.init -- maintain idents within context;
wenzelm
parents:
56438
diff
changeset
|
252 |
(case #structs (get_idents ctxt) of |
66e6c539a36d
more frugal Local_Syntax.init -- maintain idents within context;
wenzelm
parents:
56438
diff
changeset
|
253 |
x :: _ => Syntax.const (Lexicon.mark_fixed x) |
66e6c539a36d
more frugal Local_Syntax.init -- maintain idents within context;
wenzelm
parents:
56438
diff
changeset
|
254 |
| _ => error "Illegal reference to implicit structure") |
66e6c539a36d
more frugal Local_Syntax.init -- maintain idents within context;
wenzelm
parents:
56438
diff
changeset
|
255 |
| struct_tr _ ts = raise TERM ("struct_tr", ts); |
12122 | 256 |
|
257 |
||
5084 | 258 |
|
548 | 259 |
(** print (ast) translations **) |
260 |
||
14647 | 261 |
(* types *) |
262 |
||
52143 | 263 |
fun non_typed_tr' f ctxt _ ts = f ctxt ts; |
14647 | 264 |
|
265 |
||
42262 | 266 |
(* type syntax *) |
267 |
||
268 |
fun tappl_ast_tr' (f, []) = raise Ast.AST ("tappl_ast_tr'", [f]) |
|
269 |
| tappl_ast_tr' (f, [ty]) = Ast.Appl [Ast.Constant "_tapp", ty, f] |
|
270 |
| tappl_ast_tr' (f, ty :: tys) = |
|
271 |
Ast.Appl [Ast.Constant "_tappl", ty, Ast.fold_ast "_types" tys, f]; |
|
272 |
||
273 |
fun fun_ast_tr' asts = |
|
274 |
if no_brackets () orelse no_type_brackets () then raise Match |
|
275 |
else |
|
276 |
(case Ast.unfold_ast_p "\\<^type>fun" (Ast.Appl (Ast.Constant "\\<^type>fun" :: asts)) of |
|
277 |
(dom as _ :: _ :: _, cod) |
|
278 |
=> Ast.Appl [Ast.Constant "_bracket", Ast.fold_ast "_types" dom, cod] |
|
279 |
| _ => raise Match); |
|
280 |
||
281 |
||
548 | 282 |
(* application *) |
283 |
||
5690 | 284 |
fun appl_ast_tr' (f, []) = raise Ast.AST ("appl_ast_tr'", [f]) |
285 |
| appl_ast_tr' (f, args) = Ast.Appl [Ast.Constant "_appl", f, Ast.fold_ast "_args" args]; |
|
548 | 286 |
|
5690 | 287 |
fun applC_ast_tr' (f, []) = raise Ast.AST ("applC_ast_tr'", [f]) |
288 |
| applC_ast_tr' (f, args) = Ast.Appl [Ast.Constant "_applC", f, Ast.fold_ast "_cargs" args]; |
|
922
196ca0973a6d
added CPure (curried functions) and ProtoPure (ancestor of Pure and CPure)
clasohm
parents:
639
diff
changeset
|
289 |
|
548 | 290 |
|
42085 | 291 |
(* partial eta-contraction before printing *) |
292 |
||
293 |
fun eta_abs (Abs (a, T, t)) = |
|
294 |
(case eta_abs t of |
|
295 |
t' as Const ("_aprop", _) $ _ => Abs (a, T, t') |
|
296 |
| t' as f $ u => |
|
297 |
(case eta_abs u of |
|
298 |
Bound 0 => |
|
299 |
if Term.is_dependent f then Abs (a, T, t') |
|
300 |
else incr_boundvars ~1 f |
|
301 |
| _ => Abs (a, T, t')) |
|
302 |
| t' => Abs (a, T, t')) |
|
303 |
| eta_abs t = t; |
|
304 |
||
56438 | 305 |
val eta_contract_raw = Config.declare_option ("eta_contract", @{here}); |
42085 | 306 |
val eta_contract = Config.bool eta_contract_raw; |
307 |
||
308 |
fun eta_contr ctxt tm = |
|
309 |
if Config.get ctxt eta_contract then eta_abs tm else tm; |
|
310 |
||
311 |
||
548 | 312 |
(* abstraction *) |
313 |
||
49660
de49d9b4d7bc
more explicit Syntax_Trans.mark_bound_abs/mark_bound_body: preserve type information for show_markup;
wenzelm
parents:
46236
diff
changeset
|
314 |
fun mark_bound_abs (x, T) = Const ("_bound", T --> T) $ Free (x, T); |
de49d9b4d7bc
more explicit Syntax_Trans.mark_bound_abs/mark_bound_body: preserve type information for show_markup;
wenzelm
parents:
46236
diff
changeset
|
315 |
fun mark_bound_body (x, T) = Const ("_bound", dummyT) $ Free (x, T); |
2698 | 316 |
|
18958 | 317 |
fun bound_vars vars body = |
49660
de49d9b4d7bc
more explicit Syntax_Trans.mark_bound_abs/mark_bound_body: preserve type information for show_markup;
wenzelm
parents:
46236
diff
changeset
|
318 |
subst_bounds (map mark_bound_abs (Term.rename_wrt_term body vars), body); |
18958 | 319 |
|
548 | 320 |
fun strip_abss vars_of body_of tm = |
321 |
let |
|
322 |
val vars = vars_of tm; |
|
323 |
val body = body_of tm; |
|
29276 | 324 |
val rev_new_vars = Term.rename_wrt_term body vars; |
21750 | 325 |
fun subst (x, T) b = |
55948 | 326 |
if Name.is_internal x andalso not (Term.is_dependent b) |
21750 | 327 |
then (Const ("_idtdummy", T), incr_boundvars ~1 b) |
49660
de49d9b4d7bc
more explicit Syntax_Trans.mark_bound_abs/mark_bound_body: preserve type information for show_markup;
wenzelm
parents:
46236
diff
changeset
|
328 |
else (mark_bound_abs (x, T), Term.subst_bound (mark_bound_body (x, T), b)); |
21750 | 329 |
val (rev_vars', body') = fold_map subst rev_new_vars body; |
330 |
in (rev rev_vars', body') end; |
|
548 | 331 |
|
3928
787d2659ce4a
no longer tries bogus eta-contract involving aprops;
wenzelm
parents:
3777
diff
changeset
|
332 |
|
39128
93a7365fb4ee
turned eta_contract into proper configuration option;
wenzelm
parents:
37216
diff
changeset
|
333 |
fun abs_tr' ctxt tm = |
42476 | 334 |
uncurry (fold_rev (fn x => fn t => Syntax.const "_abs" $ x $ t)) |
39128
93a7365fb4ee
turned eta_contract into proper configuration option;
wenzelm
parents:
37216
diff
changeset
|
335 |
(strip_abss strip_abs_vars strip_abs_body (eta_contr ctxt tm)); |
548 | 336 |
|
14697 | 337 |
fun atomic_abs_tr' (x, T, t) = |
29276 | 338 |
let val [xT] = Term.rename_wrt_term t [(x, T)] |
49660
de49d9b4d7bc
more explicit Syntax_Trans.mark_bound_abs/mark_bound_body: preserve type information for show_markup;
wenzelm
parents:
46236
diff
changeset
|
339 |
in (mark_bound_abs xT, subst_bound (mark_bound_body xT, t)) end; |
13762 | 340 |
|
42085 | 341 |
fun abs_ast_tr' asts = |
5690 | 342 |
(case Ast.unfold_ast_p "_abs" (Ast.Appl (Ast.Constant "_abs" :: asts)) of |
343 |
([], _) => raise Ast.AST ("abs_ast_tr'", asts) |
|
344 |
| (xs, body) => Ast.Appl [Ast.Constant "_lambda", Ast.fold_ast "_pttrns" xs, body]); |
|
548 | 345 |
|
42086
74bf78db0d87
added Syntax.const_abs_tr' with proper eta_abs and Term.is_dependent;
wenzelm
parents:
42085
diff
changeset
|
346 |
fun const_abs_tr' t = |
74bf78db0d87
added Syntax.const_abs_tr' with proper eta_abs and Term.is_dependent;
wenzelm
parents:
42085
diff
changeset
|
347 |
(case eta_abs t of |
74bf78db0d87
added Syntax.const_abs_tr' with proper eta_abs and Term.is_dependent;
wenzelm
parents:
42085
diff
changeset
|
348 |
Abs (_, _, t') => |
74bf78db0d87
added Syntax.const_abs_tr' with proper eta_abs and Term.is_dependent;
wenzelm
parents:
42085
diff
changeset
|
349 |
if Term.is_dependent t' then raise Match |
74bf78db0d87
added Syntax.const_abs_tr' with proper eta_abs and Term.is_dependent;
wenzelm
parents:
42085
diff
changeset
|
350 |
else incr_boundvars ~1 t' |
74bf78db0d87
added Syntax.const_abs_tr' with proper eta_abs and Term.is_dependent;
wenzelm
parents:
42085
diff
changeset
|
351 |
| _ => raise Match); |
74bf78db0d87
added Syntax.const_abs_tr' with proper eta_abs and Term.is_dependent;
wenzelm
parents:
42085
diff
changeset
|
352 |
|
32120
53a21a5e6889
attempt for more concise setup of non-etacontracting binders
haftmann
parents:
31542
diff
changeset
|
353 |
|
42085 | 354 |
(* binders *) |
548 | 355 |
|
21535 | 356 |
fun mk_binder_tr' (name, syn) = |
548 | 357 |
let |
358 |
fun mk_idts [] = raise Match (*abort translation*) |
|
359 |
| mk_idts [idt] = idt |
|
42476 | 360 |
| mk_idts (idt :: idts) = Syntax.const "_idts" $ idt $ mk_idts idts; |
548 | 361 |
|
362 |
fun tr' t = |
|
363 |
let |
|
364 |
val (xs, bd) = strip_abss (strip_qnt_vars name) (strip_qnt_body name) t; |
|
42476 | 365 |
in Syntax.const syn $ mk_idts xs $ bd end; |
548 | 366 |
|
42476 | 367 |
fun binder_tr' (t :: ts) = Term.list_comb (tr' (Syntax.const name $ t), ts) |
21535 | 368 |
| binder_tr' [] = raise Match; |
52143 | 369 |
in (name, fn _ => binder_tr') end; |
548 | 370 |
|
52143 | 371 |
fun preserve_binder_abs_tr' name syn = (name, fn _ => fn Abs abs :: ts => |
42085 | 372 |
let val (x, t) = atomic_abs_tr' abs |
42476 | 373 |
in list_comb (Syntax.const syn $ x $ t, ts) end); |
42085 | 374 |
|
52143 | 375 |
fun preserve_binder_abs2_tr' name syn = (name, fn _ => fn A :: Abs abs :: ts => |
42085 | 376 |
let val (x, t) = atomic_abs_tr' abs |
42476 | 377 |
in list_comb (Syntax.const syn $ x $ A $ t, ts) end); |
42085 | 378 |
|
548 | 379 |
|
3691
f0396ac63e12
tuned lambda_ast_tr, idtyp_ast_tr' to accomodate fix of idt/idts
wenzelm
parents:
2698
diff
changeset
|
380 |
(* idtyp constraints *) |
548 | 381 |
|
42045 | 382 |
fun idtyp_ast_tr' a [Ast.Appl [Ast.Constant "_constrain", x, ty], xs] = |
383 |
Ast.Appl [Ast.Constant a, Ast.Appl [Ast.Constant "_idtyp", x, ty], xs] |
|
3691
f0396ac63e12
tuned lambda_ast_tr, idtyp_ast_tr' to accomodate fix of idt/idts
wenzelm
parents:
2698
diff
changeset
|
384 |
| idtyp_ast_tr' _ _ = raise Match; |
548 | 385 |
|
386 |
||
387 |
(* meta implication *) |
|
388 |
||
42278 | 389 |
fun impl_ast_tr' asts = |
42262 | 390 |
if no_brackets () then raise Match |
10572 | 391 |
else |
56245 | 392 |
(case Ast.unfold_ast_p "\\<^const>Pure.imp" |
393 |
(Ast.Appl (Ast.Constant "\\<^const>Pure.imp" :: asts)) of |
|
16612
48be8ef738df
transform_failure in translation functions: TRANSLATION_FAIL;
wenzelm
parents:
15570
diff
changeset
|
394 |
(prems as _ :: _ :: _, concl) => |
48be8ef738df
transform_failure in translation functions: TRANSLATION_FAIL;
wenzelm
parents:
15570
diff
changeset
|
395 |
let |
48be8ef738df
transform_failure in translation functions: TRANSLATION_FAIL;
wenzelm
parents:
15570
diff
changeset
|
396 |
val (asms, asm) = split_last prems; |
48be8ef738df
transform_failure in translation functions: TRANSLATION_FAIL;
wenzelm
parents:
15570
diff
changeset
|
397 |
val asms' = Ast.fold_ast_p "_asms" (asms, Ast.Appl [Ast.Constant "_asm", asm]); |
48be8ef738df
transform_failure in translation functions: TRANSLATION_FAIL;
wenzelm
parents:
15570
diff
changeset
|
398 |
in Ast.Appl [Ast.Constant "_bigimpl", asms', concl] end |
15421
fcf747c0b6b8
Syntax: last premise of "_bigimpl" is wrapped with "_asm", to have a hook for
schirmer
parents:
14981
diff
changeset
|
399 |
| _ => raise Match); |
fcf747c0b6b8
Syntax: last premise of "_bigimpl" is wrapped with "_asm", to have a hook for
schirmer
parents:
14981
diff
changeset
|
400 |
|
548 | 401 |
|
402 |
(* dependent / nondependent quantifiers *) |
|
403 |
||
20202 | 404 |
fun var_abs mark (x, T, b) = |
43326
47cf4bc789aa
simplified Name.variant -- discontinued builtin fold_map;
wenzelm
parents:
42476
diff
changeset
|
405 |
let val (x', _) = Name.variant x (Term.declare_term_names b Name.context) |
20202 | 406 |
in (x', subst_bound (mark (x', T), b)) end; |
407 |
||
408 |
val variant_abs = var_abs Free; |
|
49660
de49d9b4d7bc
more explicit Syntax_Trans.mark_bound_abs/mark_bound_body: preserve type information for show_markup;
wenzelm
parents:
46236
diff
changeset
|
409 |
val variant_abs' = var_abs mark_bound_abs; |
2698 | 410 |
|
548 | 411 |
fun dependent_tr' (q, r) (A :: Abs (x, T, B) :: ts) = |
42083
e1209fc7ecdc
added Term.is_open and Term.is_dependent convenience, to cover common situations of loose bounds;
wenzelm
parents:
42080
diff
changeset
|
412 |
if Term.is_dependent B then |
2698 | 413 |
let val (x', B') = variant_abs' (x, dummyT, B); |
49660
de49d9b4d7bc
more explicit Syntax_Trans.mark_bound_abs/mark_bound_body: preserve type information for show_markup;
wenzelm
parents:
46236
diff
changeset
|
414 |
in Term.list_comb (Syntax.const q $ mark_bound_abs (x', T) $ A $ B', ts) end |
42476 | 415 |
else Term.list_comb (Syntax.const r $ A $ incr_boundvars ~1 B, ts) |
548 | 416 |
| dependent_tr' _ _ = raise Match; |
417 |
||
418 |
||
5084 | 419 |
(* quote / antiquote *) |
420 |
||
8577 | 421 |
fun antiquote_tr' name = |
422 |
let |
|
423 |
fun tr' i (t $ u) = |
|
42476 | 424 |
if u aconv Bound i then Syntax.const name $ tr' i t |
42084
532b3a76103f
dependent_tr': formal treatment of bounds after stripping Abs, although it should only happen for malformed terms, since print_translations work top-down;
wenzelm
parents:
42083
diff
changeset
|
425 |
else tr' i t $ tr' i u |
8577 | 426 |
| tr' i (Abs (x, T, t)) = Abs (x, T, tr' (i + 1) t) |
18139 | 427 |
| tr' i a = if a aconv Bound i then raise Match else a; |
8577 | 428 |
in tr' 0 end; |
429 |
||
430 |
fun quote_tr' name (Abs (_, _, t)) = Term.incr_boundvars ~1 (antiquote_tr' name t) |
|
431 |
| quote_tr' _ _ = raise Match; |
|
432 |
||
5084 | 433 |
fun quote_antiquote_tr' quoteN antiquoteN name = |
434 |
let |
|
42476 | 435 |
fun tr' (t :: ts) = Term.list_comb (Syntax.const quoteN $ quote_tr' antiquoteN t, ts) |
8577 | 436 |
| tr' _ = raise Match; |
52143 | 437 |
in (name, fn _ => tr') end; |
5084 | 438 |
|
439 |
||
35145
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
wenzelm
parents:
32786
diff
changeset
|
440 |
(* corresponding updates *) |
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
wenzelm
parents:
32786
diff
changeset
|
441 |
|
42080
58b465952287
update_name: more uniform treatment of type constraints (NB: type equality is hard to establish in parse trees);
wenzelm
parents:
42057
diff
changeset
|
442 |
local |
58b465952287
update_name: more uniform treatment of type constraints (NB: type equality is hard to establish in parse trees);
wenzelm
parents:
42057
diff
changeset
|
443 |
|
58b465952287
update_name: more uniform treatment of type constraints (NB: type equality is hard to establish in parse trees);
wenzelm
parents:
42057
diff
changeset
|
444 |
fun upd_type (Type ("fun", [Type ("fun", [_, T]), _])) = T |
58b465952287
update_name: more uniform treatment of type constraints (NB: type equality is hard to establish in parse trees);
wenzelm
parents:
42057
diff
changeset
|
445 |
| upd_type _ = dummyT; |
58b465952287
update_name: more uniform treatment of type constraints (NB: type equality is hard to establish in parse trees);
wenzelm
parents:
42057
diff
changeset
|
446 |
|
35145
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
wenzelm
parents:
32786
diff
changeset
|
447 |
fun upd_tr' (x_upd, T) = |
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
wenzelm
parents:
32786
diff
changeset
|
448 |
(case try (unsuffix "_update") x_upd of |
42080
58b465952287
update_name: more uniform treatment of type constraints (NB: type equality is hard to establish in parse trees);
wenzelm
parents:
42057
diff
changeset
|
449 |
SOME x => (x, upd_type T) |
35145
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
wenzelm
parents:
32786
diff
changeset
|
450 |
| NONE => raise Match); |
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
wenzelm
parents:
32786
diff
changeset
|
451 |
|
42080
58b465952287
update_name: more uniform treatment of type constraints (NB: type equality is hard to establish in parse trees);
wenzelm
parents:
42057
diff
changeset
|
452 |
in |
58b465952287
update_name: more uniform treatment of type constraints (NB: type equality is hard to establish in parse trees);
wenzelm
parents:
42057
diff
changeset
|
453 |
|
35145
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
wenzelm
parents:
32786
diff
changeset
|
454 |
fun update_name_tr' (Free x) = Free (upd_tr' x) |
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
wenzelm
parents:
32786
diff
changeset
|
455 |
| update_name_tr' ((c as Const ("_free", _)) $ Free x) = c $ Free (upd_tr' x) |
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
wenzelm
parents:
32786
diff
changeset
|
456 |
| update_name_tr' (Const x) = Const (upd_tr' x) |
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
wenzelm
parents:
32786
diff
changeset
|
457 |
| update_name_tr' _ = raise Match; |
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
wenzelm
parents:
32786
diff
changeset
|
458 |
|
42080
58b465952287
update_name: more uniform treatment of type constraints (NB: type equality is hard to establish in parse trees);
wenzelm
parents:
42057
diff
changeset
|
459 |
end; |
58b465952287
update_name: more uniform treatment of type constraints (NB: type equality is hard to establish in parse trees);
wenzelm
parents:
42057
diff
changeset
|
460 |
|
35145
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
wenzelm
parents:
32786
diff
changeset
|
461 |
|
14697 | 462 |
(* indexed syntax *) |
548 | 463 |
|
42278 | 464 |
fun index_ast_tr' [Ast.Appl [Ast.Constant "_struct", ast]] = ast |
14697 | 465 |
| index_ast_tr' _ = raise Match; |
466 |
||
59152
66e6c539a36d
more frugal Local_Syntax.init -- maintain idents within context;
wenzelm
parents:
56438
diff
changeset
|
467 |
fun struct_ast_tr' ctxt [Ast.Constant "_indexdefault"] = |
66e6c539a36d
more frugal Local_Syntax.init -- maintain idents within context;
wenzelm
parents:
56438
diff
changeset
|
468 |
(case #structs (get_idents ctxt) of |
66e6c539a36d
more frugal Local_Syntax.init -- maintain idents within context;
wenzelm
parents:
56438
diff
changeset
|
469 |
x :: _ => Ast.Appl [Ast.Constant "_free", Ast.Variable x] |
66e6c539a36d
more frugal Local_Syntax.init -- maintain idents within context;
wenzelm
parents:
56438
diff
changeset
|
470 |
| _ => raise Match) |
66e6c539a36d
more frugal Local_Syntax.init -- maintain idents within context;
wenzelm
parents:
56438
diff
changeset
|
471 |
| struct_ast_tr' _ _ = raise Match; |
14697 | 472 |
|
473 |
||
474 |
||
475 |
(** Pure translations **) |
|
548 | 476 |
|
52143 | 477 |
val pure_parse_ast_translation = |
478 |
[("_strip_positions", fn _ => strip_positions_ast_tr), |
|
479 |
("_constify", fn _ => constify_ast_tr), |
|
480 |
("_tapp", fn _ => tapp_ast_tr), |
|
481 |
("_tappl", fn _ => tappl_ast_tr), |
|
482 |
("_bracket", fn _ => bracket_ast_tr), |
|
483 |
("_appl", fn _ => appl_ast_tr), |
|
484 |
("_applC", fn _ => applC_ast_tr), |
|
485 |
("_lambda", fn _ => lambda_ast_tr), |
|
486 |
("_idtyp", fn _ => idtyp_ast_tr), |
|
487 |
("_bigimpl", fn _ => bigimpl_ast_tr), |
|
488 |
("_indexdefault", fn _ => indexdefault_ast_tr), |
|
489 |
("_indexvar", fn _ => indexvar_ast_tr), |
|
490 |
("_struct", fn _ => struct_ast_tr)]; |
|
548 | 491 |
|
52143 | 492 |
val pure_parse_translation = |
493 |
[("_abs", fn _ => abs_tr), |
|
494 |
("_aprop", fn _ => aprop_tr), |
|
495 |
("_ofclass", fn _ => ofclass_tr), |
|
496 |
("_sort_constraint", fn _ => sort_constraint_tr), |
|
497 |
("_TYPE", fn _ => type_tr), |
|
498 |
("_DDDOT", fn _ => dddot_tr), |
|
499 |
("_update_name", fn _ => update_name_tr), |
|
59152
66e6c539a36d
more frugal Local_Syntax.init -- maintain idents within context;
wenzelm
parents:
56438
diff
changeset
|
500 |
("_index", fn _ => index_tr), |
66e6c539a36d
more frugal Local_Syntax.init -- maintain idents within context;
wenzelm
parents:
56438
diff
changeset
|
501 |
("_struct", struct_tr)]; |
52143 | 502 |
|
503 |
val pure_print_ast_translation = |
|
504 |
[("\\<^type>fun", fn _ => fun_ast_tr'), |
|
505 |
("_abs", fn _ => abs_ast_tr'), |
|
506 |
("_idts", fn _ => idtyp_ast_tr' "_idts"), |
|
507 |
("_pttrns", fn _ => idtyp_ast_tr' "_pttrns"), |
|
56245 | 508 |
("\\<^const>Pure.imp", fn _ => impl_ast_tr'), |
59152
66e6c539a36d
more frugal Local_Syntax.init -- maintain idents within context;
wenzelm
parents:
56438
diff
changeset
|
509 |
("_index", fn _ => index_ast_tr'), |
66e6c539a36d
more frugal Local_Syntax.init -- maintain idents within context;
wenzelm
parents:
56438
diff
changeset
|
510 |
("_struct", struct_ast_tr')]; |
52143 | 511 |
|
548 | 512 |
end; |
42284 | 513 |
|
514 |
structure Basic_Syntax_Trans: BASIC_SYNTAX_TRANS = Syntax_Trans; |
|
515 |
open Basic_Syntax_Trans; |