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