author | wenzelm |
Sun, 21 Feb 2010 21:08:25 +0100 | |
changeset 35255 | 2cb27605301f |
parent 35198 | f95c6440c1c7 |
child 35429 | afa8cf9e63d8 |
permissions | -rw-r--r-- |
548 | 1 |
(* Title: Pure/Syntax/syn_trans.ML |
2 |
Author: Tobias Nipkow and Markus Wenzel, TU Muenchen |
|
3 |
||
4 |
Syntax translation functions. |
|
5 |
*) |
|
6 |
||
7 |
signature SYN_TRANS0 = |
|
2698 | 8 |
sig |
32738 | 9 |
val eta_contract: bool Unsynchronized.ref |
13762 | 10 |
val atomic_abs_tr': string * typ * term -> term * term |
32120
53a21a5e6889
attempt for more concise setup of non-etacontracting binders
haftmann
parents:
31542
diff
changeset
|
11 |
val preserve_binder_abs_tr': string -> string -> string * (term list -> term) |
53a21a5e6889
attempt for more concise setup of non-etacontracting binders
haftmann
parents:
31542
diff
changeset
|
12 |
val preserve_binder_abs2_tr': string -> string -> string * (term list -> term) |
548 | 13 |
val mk_binder_tr: string * string -> string * (term list -> term) |
14 |
val mk_binder_tr': string * string -> string * (term list -> term) |
|
15 |
val dependent_tr': string * string -> term list -> term |
|
8577 | 16 |
val antiquote_tr: string -> term -> term |
17 |
val quote_tr: string -> term -> term |
|
5084 | 18 |
val quote_antiquote_tr: string -> string -> string -> string * (term list -> term) |
8577 | 19 |
val antiquote_tr': string -> term -> term |
20 |
val quote_tr': string -> term -> term |
|
5084 | 21 |
val quote_antiquote_tr': string -> string -> string -> string * (term list -> term) |
35145
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
wenzelm
parents:
32786
diff
changeset
|
22 |
val update_name_tr': term -> term |
2698 | 23 |
val mark_bound: string -> term |
24 |
val mark_boundT: string * typ -> term |
|
18958 | 25 |
val bound_vars: (string * typ) list -> term -> term |
20202 | 26 |
val variant_abs: string * typ * term -> string * term |
2698 | 27 |
val variant_abs': string * typ * term -> string * term |
28 |
end; |
|
548 | 29 |
|
30 |
signature SYN_TRANS1 = |
|
2698 | 31 |
sig |
548 | 32 |
include SYN_TRANS0 |
14647 | 33 |
val non_typed_tr': (term list -> term) -> bool -> typ -> term list -> term |
34 |
val non_typed_tr'': ('a -> term list -> term) -> 'a -> bool -> typ -> term list -> term |
|
1511 | 35 |
val constrainAbsC: string |
36 |
val pure_trfuns: |
|
37 |
(string * (Ast.ast list -> Ast.ast)) list * |
|
548 | 38 |
(string * (term list -> term)) list * |
39 |
(string * (term list -> term)) list * |
|
1511 | 40 |
(string * (Ast.ast list -> Ast.ast)) list |
4148 | 41 |
val pure_trfunsT: (string * (bool -> typ -> term list -> term)) list |
14697 | 42 |
val struct_trfuns: string list -> |
43 |
(string * (Ast.ast list -> Ast.ast)) list * |
|
44 |
(string * (term list -> term)) list * |
|
45 |
(string * (bool -> typ -> term list -> term)) list * |
|
46 |
(string * (Ast.ast list -> Ast.ast)) list |
|
2698 | 47 |
end; |
548 | 48 |
|
49 |
signature SYN_TRANS = |
|
2698 | 50 |
sig |
548 | 51 |
include SYN_TRANS1 |
1511 | 52 |
val abs_tr': term -> term |
4148 | 53 |
val prop_tr': term -> term |
1511 | 54 |
val appl_ast_tr': Ast.ast * Ast.ast list -> Ast.ast |
55 |
val applC_ast_tr': Ast.ast * Ast.ast list -> Ast.ast |
|
21773 | 56 |
val pts_to_asts: Proof.context -> |
57 |
(string -> (Proof.context -> Ast.ast list -> Ast.ast) option) -> |
|
16612
48be8ef738df
transform_failure in translation functions: TRANSLATION_FAIL;
wenzelm
parents:
15570
diff
changeset
|
58 |
Parser.parsetree list -> Ast.ast list |
21773 | 59 |
val asts_to_terms: Proof.context -> |
60 |
(string -> (Proof.context -> term list -> term) option) -> Ast.ast list -> term list |
|
2698 | 61 |
end; |
548 | 62 |
|
2698 | 63 |
structure SynTrans: SYN_TRANS = |
548 | 64 |
struct |
2698 | 65 |
|
66 |
||
548 | 67 |
(** parse (ast) translations **) |
68 |
||
11491 | 69 |
(* constify *) |
70 |
||
71 |
fun constify_ast_tr [Ast.Variable c] = Ast.Constant c |
|
72 |
| constify_ast_tr asts = raise Ast.AST ("constify_ast_tr", asts); |
|
73 |
||
74 |
||
548 | 75 |
(* application *) |
76 |
||
5690 | 77 |
fun appl_ast_tr [f, args] = Ast.Appl (f :: Ast.unfold_ast "_args" args) |
78 |
| 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
|
79 |
|
5690 | 80 |
fun applC_ast_tr [f, args] = Ast.Appl (f :: Ast.unfold_ast "_cargs" args) |
81 |
| applC_ast_tr asts = raise Ast.AST ("applC_ast_tr", asts); |
|
548 | 82 |
|
83 |
||
84 |
(* abstraction *) |
|
85 |
||
17788 | 86 |
fun idtyp_ast_tr (*"_idtyp"*) [x, ty] = Ast.Appl [Ast.Constant "_constrain", x, ty] |
5690 | 87 |
| idtyp_ast_tr (*"_idtyp"*) asts = raise Ast.AST ("idtyp_ast_tr", asts); |
548 | 88 |
|
17788 | 89 |
fun idtypdummy_ast_tr (*"_idtypdummy"*) [ty] = |
90 |
Ast.Appl [Ast.Constant "_constrain", Ast.Constant "_idtdummy", ty] |
|
91 |
| idtypdummy_ast_tr (*"_idtypdummy"*) asts = raise Ast.AST ("idtyp_ast_tr", asts); |
|
92 |
||
3691
f0396ac63e12
tuned lambda_ast_tr, idtyp_ast_tr' to accomodate fix of idt/idts
wenzelm
parents:
2698
diff
changeset
|
93 |
fun lambda_ast_tr (*"_lambda"*) [pats, body] = |
5690 | 94 |
Ast.fold_ast_p "_abs" (Ast.unfold_ast "_pttrns" pats, body) |
95 |
| lambda_ast_tr (*"_lambda"*) asts = raise Ast.AST ("lambda_ast_tr", asts); |
|
548 | 96 |
|
97 |
val constrainAbsC = "_constrainAbs"; |
|
98 |
||
21773 | 99 |
fun absfree_proper (x, T, t) = |
100 |
if can Name.dest_internal x then error ("Illegal internal variable in abstraction: " ^ quote x) |
|
101 |
else Term.absfree (x, T, t); |
|
102 |
||
103 |
fun abs_tr (*"_abs"*) [Free (x, T), t] = absfree_proper (x, T, t) |
|
17788 | 104 |
| abs_tr (*"_abs"*) [Const ("_idtdummy", T), t] = Term.absdummy (T, t) |
105 |
| abs_tr (*"_abs"*) [Const ("_constrain", _) $ Free (x, T) $ tT, t] = |
|
21773 | 106 |
Lexicon.const constrainAbsC $ absfree_proper (x, T, t) $ tT |
17788 | 107 |
| abs_tr (*"_abs"*) [Const ("_constrain", _) $ Const ("_idtdummy", T) $ tT, t] = |
108 |
Lexicon.const constrainAbsC $ Term.absdummy (T, t) $ tT |
|
3777 | 109 |
| abs_tr (*"_abs"*) ts = raise TERM ("abs_tr", ts); |
548 | 110 |
|
111 |
||
112 |
(* binder *) |
|
113 |
||
21535 | 114 |
fun mk_binder_tr (syn, name) = |
548 | 115 |
let |
21773 | 116 |
fun tr (Free (x, T), t) = Lexicon.const name $ absfree_proper (x, T, t) |
17788 | 117 |
| tr (Const ("_idtdummy", T), t) = Lexicon.const name $ Term.absdummy (T, t) |
118 |
| tr (Const ("_constrain", _) $ Free (x, T) $ tT, t) = |
|
21773 | 119 |
Lexicon.const name $ (Lexicon.const constrainAbsC $ absfree_proper (x, T, t) $ tT) |
17788 | 120 |
| tr (Const ("_constrain", _) $ Const ("_idtdummy", T) $ tT, t) = |
18342
1b7109c10b7b
asts_to_terms: builtin free_fixed translation makes local binders work properly;
wenzelm
parents:
18212
diff
changeset
|
121 |
Lexicon.const name $ (Lexicon.const constrainAbsC $ Term.absdummy (T, t) $ tT) |
548 | 122 |
| tr (Const ("_idts", _) $ idt $ idts, t) = tr (idt, tr (idts, t)) |
3777 | 123 |
| tr (t1, t2) = raise TERM ("binder_tr", [t1, t2]); |
548 | 124 |
|
21535 | 125 |
fun binder_tr [idts, body] = tr (idts, body) |
126 |
| binder_tr ts = raise TERM ("binder_tr", ts); |
|
127 |
in (syn, binder_tr) end; |
|
548 | 128 |
|
129 |
||
28628 | 130 |
(* type propositions *) |
131 |
||
35255 | 132 |
fun mk_type ty = |
133 |
Lexicon.const "_constrain" $ |
|
134 |
Lexicon.const "\\<^const>TYPE" $ (Lexicon.const "itself" $ ty); |
|
28628 | 135 |
|
136 |
fun ofclass_tr (*"_ofclass"*) [ty, cls] = cls $ mk_type ty |
|
137 |
| ofclass_tr (*"_ofclass"*) ts = raise TERM ("ofclass_tr", ts); |
|
138 |
||
139 |
fun sort_constraint_tr (*"_sort_constraint"*) [ty] = |
|
35255 | 140 |
Lexicon.const "\\<^const>Pure.sort_constraint" $ mk_type ty |
28628 | 141 |
| sort_constraint_tr (*"_sort_constraint"*) ts = raise TERM ("sort_constraint_tr", ts); |
142 |
||
143 |
||
548 | 144 |
(* meta propositions *) |
145 |
||
17788 | 146 |
fun aprop_tr (*"_aprop"*) [t] = Lexicon.const "_constrain" $ t $ Lexicon.const "prop" |
3777 | 147 |
| aprop_tr (*"_aprop"*) ts = raise TERM ("aprop_tr", ts); |
548 | 148 |
|
149 |
||
150 |
(* meta implication *) |
|
151 |
||
16612
48be8ef738df
transform_failure in translation functions: TRANSLATION_FAIL;
wenzelm
parents:
15570
diff
changeset
|
152 |
fun bigimpl_ast_tr (*"_bigimpl"*) (asts as [asms, concl]) = |
48be8ef738df
transform_failure in translation functions: TRANSLATION_FAIL;
wenzelm
parents:
15570
diff
changeset
|
153 |
let val prems = |
48be8ef738df
transform_failure in translation functions: TRANSLATION_FAIL;
wenzelm
parents:
15570
diff
changeset
|
154 |
(case Ast.unfold_ast_p "_asms" asms of |
48be8ef738df
transform_failure in translation functions: TRANSLATION_FAIL;
wenzelm
parents:
15570
diff
changeset
|
155 |
(asms', Ast.Appl [Ast.Constant "_asm", asm']) => asms' @ [asm'] |
48be8ef738df
transform_failure in translation functions: TRANSLATION_FAIL;
wenzelm
parents:
15570
diff
changeset
|
156 |
| _ => raise Ast.AST ("bigimpl_ast_tr", asts)) |
35255 | 157 |
in Ast.fold_ast_p "\\<^const>==>" (prems, concl) end |
15421
fcf747c0b6b8
Syntax: last premise of "_bigimpl" is wrapped with "_asm", to have a hook for
schirmer
parents:
14981
diff
changeset
|
158 |
| bigimpl_ast_tr (*"_bigimpl"*) asts = raise Ast.AST ("bigimpl_ast_tr", asts); |
fcf747c0b6b8
Syntax: last premise of "_bigimpl" is wrapped with "_asm", to have a hook for
schirmer
parents:
14981
diff
changeset
|
159 |
|
548 | 160 |
|
23824
8ad7131dbfcf
moved print_translations from Pure.thy to Syntax/syn_trans.ML;
wenzelm
parents:
21773
diff
changeset
|
161 |
(* type/term reflection *) |
4148 | 162 |
|
28628 | 163 |
fun type_tr (*"_TYPE"*) [ty] = mk_type ty |
4148 | 164 |
| type_tr (*"_TYPE"*) ts = raise TERM ("type_tr", ts); |
165 |
||
548 | 166 |
|
6761 | 167 |
(* dddot *) |
168 |
||
8595 | 169 |
fun dddot_tr (*"_DDDOT"*) ts = Term.list_comb (Lexicon.var SynExt.dddot_indexname, ts); |
6761 | 170 |
|
171 |
||
5084 | 172 |
(* quote / antiquote *) |
173 |
||
8577 | 174 |
fun antiquote_tr name = |
175 |
let |
|
176 |
fun tr i ((t as Const (c, _)) $ u) = |
|
177 |
if c = name then tr i u $ Bound i |
|
178 |
else tr i t $ tr i u |
|
179 |
| tr i (t $ u) = tr i t $ tr i u |
|
180 |
| tr i (Abs (x, T, t)) = Abs (x, T, tr (i + 1) t) |
|
181 |
| tr _ a = a; |
|
182 |
in tr 0 end; |
|
183 |
||
184 |
fun quote_tr name t = Abs ("s", dummyT, antiquote_tr name (Term.incr_boundvars 1 t)); |
|
185 |
||
5084 | 186 |
fun quote_antiquote_tr quoteN antiquoteN name = |
187 |
let |
|
8577 | 188 |
fun tr [t] = Lexicon.const name $ quote_tr antiquoteN t |
189 |
| tr ts = raise TERM ("quote_tr", ts); |
|
190 |
in (quoteN, tr) end; |
|
5084 | 191 |
|
192 |
||
35145
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
wenzelm
parents:
32786
diff
changeset
|
193 |
(* corresponding updates *) |
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
wenzelm
parents:
32786
diff
changeset
|
194 |
|
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
wenzelm
parents:
32786
diff
changeset
|
195 |
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
|
196 |
| 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
|
197 |
| update_name_tr (((c as Const ("_constrain", _)) $ t $ ty) :: ts) = |
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
wenzelm
parents:
32786
diff
changeset
|
198 |
list_comb (c $ update_name_tr [t] $ (Lexicon.const "fun" $ ty $ Lexicon.const "dummy"), ts) |
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
wenzelm
parents:
32786
diff
changeset
|
199 |
| 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
|
200 |
|
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
wenzelm
parents:
32786
diff
changeset
|
201 |
|
14697 | 202 |
(* indexed syntax *) |
203 |
||
204 |
fun struct_ast_tr (*"_struct"*) [Ast.Appl [Ast.Constant "_index", ast]] = ast |
|
205 |
| struct_ast_tr (*"_struct"*) asts = Ast.mk_appl (Ast.Constant "_struct") asts; |
|
206 |
||
207 |
fun index_ast_tr ast = |
|
208 |
Ast.mk_appl (Ast.Constant "_index") [Ast.mk_appl (Ast.Constant "_struct") [ast]]; |
|
209 |
||
210 |
fun indexdefault_ast_tr (*"_indexdefault"*) [] = |
|
211 |
index_ast_tr (Ast.Constant "_indexdefault") |
|
212 |
| indexdefault_ast_tr (*"_indexdefault"*) asts = |
|
213 |
raise Ast.AST ("indexdefault_ast_tr", asts); |
|
214 |
||
215 |
fun indexnum_ast_tr (*"_indexnum"*) [ast] = |
|
216 |
index_ast_tr (Ast.mk_appl (Ast.Constant "_indexnum") [ast]) |
|
217 |
| indexnum_ast_tr (*"_indexnum"*) asts = raise Ast.AST ("indexnum_ast_tr", asts); |
|
12122 | 218 |
|
14697 | 219 |
fun indexvar_ast_tr (*"_indexvar"*) [] = |
220 |
Ast.mk_appl (Ast.Constant "_index") [Ast.Variable "some_index"] |
|
221 |
| indexvar_ast_tr (*"_indexvar"*) asts = raise Ast.AST ("indexvar_ast_tr", asts); |
|
222 |
||
223 |
fun index_tr (*"_index"*) [t] = t |
|
224 |
| index_tr (*"_index"*) ts = raise TERM ("index_tr", ts); |
|
225 |
||
226 |
||
227 |
(* implicit structures *) |
|
228 |
||
229 |
fun the_struct structs i = |
|
30146 | 230 |
if 1 <= i andalso i <= length structs then nth structs (i - 1) |
31542
3371a3c19bb1
reraise exceptions to preserve position information;
wenzelm
parents:
30146
diff
changeset
|
231 |
else error ("Illegal reference to implicit structure #" ^ string_of_int i); |
14697 | 232 |
|
233 |
fun struct_tr structs (*"_struct"*) [Const ("_indexdefault", _)] = |
|
234 |
Lexicon.free (the_struct structs 1) |
|
235 |
| struct_tr structs (*"_struct"*) [t as (Const ("_indexnum", _) $ Const (s, _))] = |
|
236 |
Lexicon.free (the_struct structs |
|
15531 | 237 |
(case Lexicon.read_nat s of SOME n => n | NONE => raise TERM ("struct_tr", [t]))) |
14697 | 238 |
| struct_tr _ (*"_struct"*) ts = raise TERM ("struct_tr", ts); |
12122 | 239 |
|
240 |
||
5084 | 241 |
|
548 | 242 |
(** print (ast) translations **) |
243 |
||
14647 | 244 |
(* types *) |
245 |
||
246 |
fun non_typed_tr' f _ _ ts = f ts; |
|
247 |
fun non_typed_tr'' f x _ _ ts = f x ts; |
|
248 |
||
249 |
||
548 | 250 |
(* application *) |
251 |
||
5690 | 252 |
fun appl_ast_tr' (f, []) = raise Ast.AST ("appl_ast_tr'", [f]) |
253 |
| appl_ast_tr' (f, args) = Ast.Appl [Ast.Constant "_appl", f, Ast.fold_ast "_args" args]; |
|
548 | 254 |
|
5690 | 255 |
fun applC_ast_tr' (f, []) = raise Ast.AST ("applC_ast_tr'", [f]) |
256 |
| 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
|
257 |
|
548 | 258 |
|
259 |
(* abstraction *) |
|
260 |
||
19311 | 261 |
fun mark_boundT (x, T) = Const ("_bound", T --> T) $ Free (x, T); |
2698 | 262 |
fun mark_bound x = mark_boundT (x, dummyT); |
263 |
||
18958 | 264 |
fun bound_vars vars body = |
265 |
subst_bounds (map mark_boundT (Term.rename_wrt_term body vars), body); |
|
266 |
||
548 | 267 |
fun strip_abss vars_of body_of tm = |
268 |
let |
|
269 |
val vars = vars_of tm; |
|
270 |
val body = body_of tm; |
|
29276 | 271 |
val rev_new_vars = Term.rename_wrt_term body vars; |
21750 | 272 |
fun subst (x, T) b = |
273 |
if can Name.dest_internal x andalso not (Term.loose_bvar1 (b, 0)) |
|
274 |
then (Const ("_idtdummy", T), incr_boundvars ~1 b) |
|
275 |
else (mark_boundT (x, T), Term.subst_bound (mark_bound x, b)); |
|
276 |
val (rev_vars', body') = fold_map subst rev_new_vars body; |
|
277 |
in (rev rev_vars', body') end; |
|
548 | 278 |
|
3928
787d2659ce4a
no longer tries bogus eta-contract involving aprops;
wenzelm
parents:
3777
diff
changeset
|
279 |
|
548 | 280 |
(*do (partial) eta-contraction before printing*) |
281 |
||
32738 | 282 |
val eta_contract = Unsynchronized.ref true; |
548 | 283 |
|
284 |
fun eta_contr tm = |
|
285 |
let |
|
3928
787d2659ce4a
no longer tries bogus eta-contract involving aprops;
wenzelm
parents:
3777
diff
changeset
|
286 |
fun is_aprop (Const ("_aprop", _)) = true |
787d2659ce4a
no longer tries bogus eta-contract involving aprops;
wenzelm
parents:
3777
diff
changeset
|
287 |
| is_aprop _ = false; |
787d2659ce4a
no longer tries bogus eta-contract involving aprops;
wenzelm
parents:
3777
diff
changeset
|
288 |
|
548 | 289 |
fun eta_abs (Abs (a, T, t)) = |
290 |
(case eta_abs t of |
|
291 |
t' as f $ u => |
|
292 |
(case eta_abs u of |
|
293 |
Bound 0 => |
|
5084 | 294 |
if Term.loose_bvar1 (f, 0) orelse is_aprop f then Abs (a, T, t') |
3928
787d2659ce4a
no longer tries bogus eta-contract involving aprops;
wenzelm
parents:
3777
diff
changeset
|
295 |
else incr_boundvars ~1 f |
548 | 296 |
| _ => Abs (a, T, t')) |
297 |
| t' => Abs (a, T, t')) |
|
298 |
| eta_abs t = t; |
|
299 |
in |
|
300 |
if ! eta_contract then eta_abs tm else tm |
|
301 |
end; |
|
302 |
||
303 |
||
304 |
fun abs_tr' tm = |
|
19473 | 305 |
uncurry (fold_rev (fn x => fn t => Lexicon.const "_abs" $ x $ t)) |
548 | 306 |
(strip_abss strip_abs_vars strip_abs_body (eta_contr tm)); |
307 |
||
14697 | 308 |
fun atomic_abs_tr' (x, T, t) = |
29276 | 309 |
let val [xT] = Term.rename_wrt_term t [(x, T)] |
14697 | 310 |
in (mark_boundT xT, subst_bound (mark_bound (fst xT), t)) end; |
13762 | 311 |
|
548 | 312 |
fun abs_ast_tr' (*"_abs"*) asts = |
5690 | 313 |
(case Ast.unfold_ast_p "_abs" (Ast.Appl (Ast.Constant "_abs" :: asts)) of |
314 |
([], _) => raise Ast.AST ("abs_ast_tr'", asts) |
|
315 |
| (xs, body) => Ast.Appl [Ast.Constant "_lambda", Ast.fold_ast "_pttrns" xs, body]); |
|
548 | 316 |
|
32120
53a21a5e6889
attempt for more concise setup of non-etacontracting binders
haftmann
parents:
31542
diff
changeset
|
317 |
fun preserve_binder_abs_tr' name syn = (name, fn (Abs abs :: ts) => |
53a21a5e6889
attempt for more concise setup of non-etacontracting binders
haftmann
parents:
31542
diff
changeset
|
318 |
let val (x, t) = atomic_abs_tr' abs |
53a21a5e6889
attempt for more concise setup of non-etacontracting binders
haftmann
parents:
31542
diff
changeset
|
319 |
in list_comb (Lexicon.const syn $ x $ t, ts) end); |
53a21a5e6889
attempt for more concise setup of non-etacontracting binders
haftmann
parents:
31542
diff
changeset
|
320 |
|
53a21a5e6889
attempt for more concise setup of non-etacontracting binders
haftmann
parents:
31542
diff
changeset
|
321 |
fun preserve_binder_abs2_tr' name syn = (name, fn (A :: Abs abs :: ts) => |
53a21a5e6889
attempt for more concise setup of non-etacontracting binders
haftmann
parents:
31542
diff
changeset
|
322 |
let val (x, t) = atomic_abs_tr' abs |
53a21a5e6889
attempt for more concise setup of non-etacontracting binders
haftmann
parents:
31542
diff
changeset
|
323 |
in list_comb (Lexicon.const syn $ x $ A $ t, ts) end); |
53a21a5e6889
attempt for more concise setup of non-etacontracting binders
haftmann
parents:
31542
diff
changeset
|
324 |
|
548 | 325 |
|
326 |
(* binder *) |
|
327 |
||
21535 | 328 |
fun mk_binder_tr' (name, syn) = |
548 | 329 |
let |
330 |
fun mk_idts [] = raise Match (*abort translation*) |
|
331 |
| mk_idts [idt] = idt |
|
5690 | 332 |
| mk_idts (idt :: idts) = Lexicon.const "_idts" $ idt $ mk_idts idts; |
548 | 333 |
|
334 |
fun tr' t = |
|
335 |
let |
|
336 |
val (xs, bd) = strip_abss (strip_qnt_vars name) (strip_qnt_body name) t; |
|
21535 | 337 |
in Lexicon.const syn $ mk_idts xs $ bd end; |
548 | 338 |
|
21535 | 339 |
fun binder_tr' (t :: ts) = Term.list_comb (tr' (Lexicon.const name $ t), ts) |
340 |
| binder_tr' [] = raise Match; |
|
341 |
in (name, binder_tr') end; |
|
548 | 342 |
|
343 |
||
3691
f0396ac63e12
tuned lambda_ast_tr, idtyp_ast_tr' to accomodate fix of idt/idts
wenzelm
parents:
2698
diff
changeset
|
344 |
(* idtyp constraints *) |
548 | 345 |
|
5690 | 346 |
fun idtyp_ast_tr' a [Ast.Appl [Ast.Constant c, x, ty], xs] = |
17788 | 347 |
if c = "_constrain" then |
28628 | 348 |
Ast.Appl [Ast.Constant a, Ast.Appl [Ast.Constant "_idtyp", x, ty], xs] |
548 | 349 |
else raise Match |
3691
f0396ac63e12
tuned lambda_ast_tr, idtyp_ast_tr' to accomodate fix of idt/idts
wenzelm
parents:
2698
diff
changeset
|
350 |
| idtyp_ast_tr' _ _ = raise Match; |
548 | 351 |
|
352 |
||
28628 | 353 |
(* type propositions *) |
354 |
||
35255 | 355 |
fun type_prop_tr' _ (*"_type_prop"*) T [Const ("\\<^const>Pure.sort_constraint", _)] = |
28628 | 356 |
Lexicon.const "_sort_constraint" $ TypeExt.term_of_typ true T |
357 |
| type_prop_tr' show_sorts (*"_type_prop"*) T [t] = |
|
358 |
Lexicon.const "_ofclass" $ TypeExt.term_of_typ show_sorts T $ t |
|
359 |
| type_prop_tr' _ (*"_type_prop"*) T ts = raise TYPE ("type_prop_tr'", [T], ts); |
|
360 |
||
361 |
||
548 | 362 |
(* meta propositions *) |
363 |
||
4148 | 364 |
fun prop_tr' tm = |
548 | 365 |
let |
5690 | 366 |
fun aprop t = Lexicon.const "_aprop" $ t; |
548 | 367 |
|
2698 | 368 |
fun is_prop Ts t = |
369 |
fastype_of1 (Ts, t) = propT handle TERM _ => false; |
|
548 | 370 |
|
35255 | 371 |
fun is_term (Const ("\\<^const>Pure.term", _) $ _) = true |
19848 | 372 |
| is_term _ = false; |
373 |
||
548 | 374 |
fun tr' _ (t as Const _) = t |
18478
29a5070b517c
prop_tr': proper handling of aprop marked as bound;
wenzelm
parents:
18342
diff
changeset
|
375 |
| tr' Ts (t as Const ("_bound", _) $ u) = |
29a5070b517c
prop_tr': proper handling of aprop marked as bound;
wenzelm
parents:
18342
diff
changeset
|
376 |
if is_prop Ts u then aprop t else t |
2698 | 377 |
| tr' _ (t as Free (x, T)) = |
5690 | 378 |
if T = propT then aprop (Lexicon.free x) else t |
2698 | 379 |
| tr' _ (t as Var (xi, T)) = |
5690 | 380 |
if T = propT then aprop (Lexicon.var xi) else t |
2698 | 381 |
| tr' Ts (t as Bound _) = |
382 |
if is_prop Ts t then aprop t else t |
|
383 |
| tr' Ts (Abs (x, T, t)) = Abs (x, T, tr' (T :: Ts) t) |
|
35255 | 384 |
| tr' Ts (t as t1 $ (t2 as Const ("\\<^const>TYPE", Type ("itself", [T])))) = |
28628 | 385 |
if is_prop Ts t andalso not (is_term t) then Const ("_type_prop", T) $ tr' Ts t1 |
2698 | 386 |
else tr' Ts t1 $ tr' Ts t2 |
387 |
| tr' Ts (t as t1 $ t2) = |
|
5690 | 388 |
(if is_Const (Term.head_of t) orelse not (is_prop Ts t) |
2698 | 389 |
then I else aprop) (tr' Ts t1 $ tr' Ts t2); |
16612
48be8ef738df
transform_failure in translation functions: TRANSLATION_FAIL;
wenzelm
parents:
15570
diff
changeset
|
390 |
in tr' [] tm end; |
548 | 391 |
|
2698 | 392 |
|
548 | 393 |
(* meta implication *) |
394 |
||
395 |
fun impl_ast_tr' (*"==>"*) asts = |
|
10572 | 396 |
if TypeExt.no_brackets () then raise Match |
397 |
else |
|
35255 | 398 |
(case Ast.unfold_ast_p "\\<^const>==>" (Ast.Appl (Ast.Constant "\\<^const>==>" :: asts)) of |
16612
48be8ef738df
transform_failure in translation functions: TRANSLATION_FAIL;
wenzelm
parents:
15570
diff
changeset
|
399 |
(prems as _ :: _ :: _, concl) => |
48be8ef738df
transform_failure in translation functions: TRANSLATION_FAIL;
wenzelm
parents:
15570
diff
changeset
|
400 |
let |
48be8ef738df
transform_failure in translation functions: TRANSLATION_FAIL;
wenzelm
parents:
15570
diff
changeset
|
401 |
val (asms, asm) = split_last prems; |
48be8ef738df
transform_failure in translation functions: TRANSLATION_FAIL;
wenzelm
parents:
15570
diff
changeset
|
402 |
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
|
403 |
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
|
404 |
| _ => raise Match); |
fcf747c0b6b8
Syntax: last premise of "_bigimpl" is wrapped with "_asm", to have a hook for
schirmer
parents:
14981
diff
changeset
|
405 |
|
548 | 406 |
|
4148 | 407 |
(* type reflection *) |
408 |
||
409 |
fun type_tr' show_sorts (*"TYPE"*) (Type ("itself", [T])) ts = |
|
5690 | 410 |
Term.list_comb (Lexicon.const "_TYPE" $ TypeExt.term_of_typ show_sorts T, ts) |
4148 | 411 |
| type_tr' _ _ _ = raise Match; |
412 |
||
413 |
||
19577 | 414 |
(* type constraints *) |
415 |
||
416 |
fun type_constraint_tr' show_sorts (*"_type_constraint_"*) (Type ("fun", [T, _])) (t :: ts) = |
|
417 |
Term.list_comb (Lexicon.const SynExt.constrainC $ t $ TypeExt.term_of_typ show_sorts T, ts) |
|
418 |
| type_constraint_tr' _ _ _ = raise Match; |
|
419 |
||
420 |
||
548 | 421 |
(* dependent / nondependent quantifiers *) |
422 |
||
20202 | 423 |
fun var_abs mark (x, T, b) = |
424 |
let val ([x'], _) = Name.variants [x] (Term.declare_term_names b Name.context) |
|
425 |
in (x', subst_bound (mark (x', T), b)) end; |
|
426 |
||
427 |
val variant_abs = var_abs Free; |
|
428 |
val variant_abs' = var_abs mark_boundT; |
|
2698 | 429 |
|
548 | 430 |
fun dependent_tr' (q, r) (A :: Abs (x, T, B) :: ts) = |
5084 | 431 |
if Term.loose_bvar1 (B, 0) then |
2698 | 432 |
let val (x', B') = variant_abs' (x, dummyT, B); |
5690 | 433 |
in Term.list_comb (Lexicon.const q $ mark_boundT (x', T) $ A $ B', ts) end |
434 |
else Term.list_comb (Lexicon.const r $ A $ B, ts) |
|
548 | 435 |
| dependent_tr' _ _ = raise Match; |
436 |
||
437 |
||
5084 | 438 |
(* quote / antiquote *) |
439 |
||
8577 | 440 |
fun antiquote_tr' name = |
441 |
let |
|
442 |
fun tr' i (t $ u) = |
|
18139 | 443 |
if u aconv Bound i then Lexicon.const name $ tr' i t |
8577 | 444 |
else tr' i t $ tr' i u |
445 |
| tr' i (Abs (x, T, t)) = Abs (x, T, tr' (i + 1) t) |
|
18139 | 446 |
| tr' i a = if a aconv Bound i then raise Match else a; |
8577 | 447 |
in tr' 0 end; |
448 |
||
449 |
fun quote_tr' name (Abs (_, _, t)) = Term.incr_boundvars ~1 (antiquote_tr' name t) |
|
450 |
| quote_tr' _ _ = raise Match; |
|
451 |
||
5084 | 452 |
fun quote_antiquote_tr' quoteN antiquoteN name = |
453 |
let |
|
8577 | 454 |
fun tr' (t :: ts) = Term.list_comb (Lexicon.const quoteN $ quote_tr' antiquoteN t, ts) |
455 |
| tr' _ = raise Match; |
|
456 |
in (name, tr') end; |
|
5084 | 457 |
|
458 |
||
35145
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
wenzelm
parents:
32786
diff
changeset
|
459 |
(* corresponding updates *) |
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
wenzelm
parents:
32786
diff
changeset
|
460 |
|
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
wenzelm
parents:
32786
diff
changeset
|
461 |
fun upd_tr' (x_upd, T) = |
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
wenzelm
parents:
32786
diff
changeset
|
462 |
(case try (unsuffix "_update") x_upd of |
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
wenzelm
parents:
32786
diff
changeset
|
463 |
SOME x => (x, if T = dummyT then T else Term.domain_type T) |
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
wenzelm
parents:
32786
diff
changeset
|
464 |
| NONE => raise Match); |
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
wenzelm
parents:
32786
diff
changeset
|
465 |
|
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
wenzelm
parents:
32786
diff
changeset
|
466 |
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
|
467 |
| 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
|
468 |
| 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
|
469 |
| update_name_tr' _ = raise Match; |
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
wenzelm
parents:
32786
diff
changeset
|
470 |
|
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
wenzelm
parents:
32786
diff
changeset
|
471 |
|
14697 | 472 |
(* indexed syntax *) |
548 | 473 |
|
14697 | 474 |
fun index_ast_tr' (*"_index"*) [Ast.Appl [Ast.Constant "_struct", ast]] = ast |
475 |
| index_ast_tr' _ = raise Match; |
|
476 |
||
477 |
||
478 |
(* implicit structures *) |
|
479 |
||
480 |
fun the_struct' structs s = |
|
481 |
[(case Lexicon.read_nat s of |
|
18678 | 482 |
SOME i => Ast.Variable (the_struct structs i handle ERROR _ => raise Match) |
15531 | 483 |
| NONE => raise Match)] |> Ast.mk_appl (Ast.Constant "_free"); |
14697 | 484 |
|
485 |
fun struct_ast_tr' structs (*"_struct"*) [Ast.Constant "_indexdefault"] = |
|
486 |
the_struct' structs "1" |
|
487 |
| struct_ast_tr' structs (*"_struct"*) [Ast.Appl [Ast.Constant "_indexnum", Ast.Constant s]] = |
|
488 |
the_struct' structs s |
|
489 |
| struct_ast_tr' _ _ = raise Match; |
|
490 |
||
491 |
||
492 |
||
493 |
(** Pure translations **) |
|
548 | 494 |
|
495 |
val pure_trfuns = |
|
35145
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
wenzelm
parents:
32786
diff
changeset
|
496 |
([("_constify", constify_ast_tr), |
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
wenzelm
parents:
32786
diff
changeset
|
497 |
("_appl", appl_ast_tr), |
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
wenzelm
parents:
32786
diff
changeset
|
498 |
("_applC", applC_ast_tr), |
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
wenzelm
parents:
32786
diff
changeset
|
499 |
("_lambda", lambda_ast_tr), |
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
wenzelm
parents:
32786
diff
changeset
|
500 |
("_idtyp", idtyp_ast_tr), |
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
wenzelm
parents:
32786
diff
changeset
|
501 |
("_idtypdummy", idtypdummy_ast_tr), |
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
wenzelm
parents:
32786
diff
changeset
|
502 |
("_bigimpl", bigimpl_ast_tr), |
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
wenzelm
parents:
32786
diff
changeset
|
503 |
("_indexdefault", indexdefault_ast_tr), |
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
wenzelm
parents:
32786
diff
changeset
|
504 |
("_indexnum", indexnum_ast_tr), |
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
wenzelm
parents:
32786
diff
changeset
|
505 |
("_indexvar", indexvar_ast_tr), |
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
wenzelm
parents:
32786
diff
changeset
|
506 |
("_struct", struct_ast_tr)], |
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
wenzelm
parents:
32786
diff
changeset
|
507 |
[("_abs", abs_tr), |
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
wenzelm
parents:
32786
diff
changeset
|
508 |
("_aprop", aprop_tr), |
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
wenzelm
parents:
32786
diff
changeset
|
509 |
("_ofclass", ofclass_tr), |
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
wenzelm
parents:
32786
diff
changeset
|
510 |
("_sort_constraint", sort_constraint_tr), |
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
wenzelm
parents:
32786
diff
changeset
|
511 |
("_TYPE", type_tr), |
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
wenzelm
parents:
32786
diff
changeset
|
512 |
("_DDDOT", dddot_tr), |
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
wenzelm
parents:
32786
diff
changeset
|
513 |
("_update_name", update_name_tr), |
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
wenzelm
parents:
32786
diff
changeset
|
514 |
("_index", index_tr)], |
35198 | 515 |
([]: (string * (term list -> term)) list), |
35145
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
wenzelm
parents:
32786
diff
changeset
|
516 |
[("_abs", abs_ast_tr'), |
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
wenzelm
parents:
32786
diff
changeset
|
517 |
("_idts", idtyp_ast_tr' "_idts"), |
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
wenzelm
parents:
32786
diff
changeset
|
518 |
("_pttrns", idtyp_ast_tr' "_pttrns"), |
35255 | 519 |
("\\<^const>==>", impl_ast_tr'), |
35145
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
wenzelm
parents:
32786
diff
changeset
|
520 |
("_index", index_ast_tr')]); |
548 | 521 |
|
2698 | 522 |
val pure_trfunsT = |
35255 | 523 |
[("_type_prop", type_prop_tr'), |
524 |
("\\<^const>TYPE", type_tr'), |
|
525 |
("_type_constraint_", type_constraint_tr')]; |
|
2698 | 526 |
|
14697 | 527 |
fun struct_trfuns structs = |
528 |
([], [("_struct", struct_tr structs)], [], [("_struct", struct_ast_tr' structs)]); |
|
529 |
||
548 | 530 |
|
531 |
||
14868 | 532 |
(** pts_to_asts **) |
548 | 533 |
|
21773 | 534 |
fun pts_to_asts ctxt trf pts = |
548 | 535 |
let |
536 |
fun trans a args = |
|
537 |
(case trf a of |
|
15531 | 538 |
NONE => Ast.mk_appl (Ast.Constant a) args |
23937
66e1f24d655d
eliminated transform_failure (to avoid critical section for main transactions);
wenzelm
parents:
23824
diff
changeset
|
539 |
| SOME f => f ctxt args); |
548 | 540 |
|
987 | 541 |
(*translate pt bottom-up*) |
5690 | 542 |
fun ast_of (Parser.Node (a, pts)) = trans a (map ast_of pts) |
543 |
| ast_of (Parser.Tip tok) = Ast.Variable (Lexicon.str_of_token tok); |
|
14798
702cb4859cab
Modified functions pt_to_ast and ast_to_term to improve handling
berghofe
parents:
14697
diff
changeset
|
544 |
|
23963
c2ee97a963db
moved exception capture/release to structure Exn;
wenzelm
parents:
23937
diff
changeset
|
545 |
val exn_results = map (Exn.capture ast_of) pts; |
c2ee97a963db
moved exception capture/release to structure Exn;
wenzelm
parents:
23937
diff
changeset
|
546 |
val exns = map_filter Exn.get_exn exn_results; |
c2ee97a963db
moved exception capture/release to structure Exn;
wenzelm
parents:
23937
diff
changeset
|
547 |
val results = map_filter Exn.get_result exn_results |
31542
3371a3c19bb1
reraise exceptions to preserve position information;
wenzelm
parents:
30146
diff
changeset
|
548 |
in (case (results, exns) of ([], exn :: _) => reraise exn | _ => results) end; |
548 | 549 |
|
550 |
||
551 |
||
14798
702cb4859cab
Modified functions pt_to_ast and ast_to_term to improve handling
berghofe
parents:
14697
diff
changeset
|
552 |
(** asts_to_terms **) |
548 | 553 |
|
21773 | 554 |
fun asts_to_terms ctxt trf asts = |
548 | 555 |
let |
556 |
fun trans a args = |
|
557 |
(case trf a of |
|
15531 | 558 |
NONE => Term.list_comb (Lexicon.const a, args) |
23937
66e1f24d655d
eliminated transform_failure (to avoid critical section for main transactions);
wenzelm
parents:
23824
diff
changeset
|
559 |
| SOME f => f ctxt args); |
548 | 560 |
|
5690 | 561 |
fun term_of (Ast.Constant a) = trans a [] |
562 |
| term_of (Ast.Variable x) = Lexicon.read_var x |
|
563 |
| term_of (Ast.Appl (Ast.Constant a :: (asts as _ :: _))) = |
|
548 | 564 |
trans a (map term_of asts) |
5690 | 565 |
| term_of (Ast.Appl (ast :: (asts as _ :: _))) = |
566 |
Term.list_comb (term_of ast, map term_of asts) |
|
567 |
| term_of (ast as Ast.Appl _) = raise Ast.AST ("ast_to_term: malformed ast", [ast]); |
|
14798
702cb4859cab
Modified functions pt_to_ast and ast_to_term to improve handling
berghofe
parents:
14697
diff
changeset
|
568 |
|
19005 | 569 |
val free_fixed = Term.map_aterms |
570 |
(fn t as Const (c, T) => |
|
571 |
(case try (unprefix Lexicon.fixedN) c of |
|
572 |
NONE => t |
|
18342
1b7109c10b7b
asts_to_terms: builtin free_fixed translation makes local binders work properly;
wenzelm
parents:
18212
diff
changeset
|
573 |
| SOME x => Free (x, T)) |
19005 | 574 |
| t => t); |
18342
1b7109c10b7b
asts_to_terms: builtin free_fixed translation makes local binders work properly;
wenzelm
parents:
18212
diff
changeset
|
575 |
|
23963
c2ee97a963db
moved exception capture/release to structure Exn;
wenzelm
parents:
23937
diff
changeset
|
576 |
val exn_results = map (Exn.capture (term_of #> free_fixed)) asts; |
c2ee97a963db
moved exception capture/release to structure Exn;
wenzelm
parents:
23937
diff
changeset
|
577 |
val exns = map_filter Exn.get_exn exn_results; |
c2ee97a963db
moved exception capture/release to structure Exn;
wenzelm
parents:
23937
diff
changeset
|
578 |
val results = map_filter Exn.get_result exn_results |
31542
3371a3c19bb1
reraise exceptions to preserve position information;
wenzelm
parents:
30146
diff
changeset
|
579 |
in (case (results, exns) of ([], exn :: _) => reraise exn | _ => results) end; |
548 | 580 |
|
581 |
end; |