| author | huffman | 
| Thu, 06 May 2010 11:08:19 -0700 | |
| changeset 36696 | 1b69f78be286 | 
| parent 36502 | 586af36cf3cc | 
| child 37216 | 3165bc303f66 | 
| 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: 
31542diff
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: 
31542diff
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: 
32786diff
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: | |
| 35429 
afa8cf9e63d8
authentic syntax for classes and type constructors;
 wenzelm parents: 
35255diff
changeset | 37 | (string * (Ast.ast list -> Ast.ast)) list * | 
| 
afa8cf9e63d8
authentic syntax for classes and type constructors;
 wenzelm parents: 
35255diff
changeset | 38 | (string * (term list -> term)) list * | 
| 
afa8cf9e63d8
authentic syntax for classes and type constructors;
 wenzelm parents: 
35255diff
changeset | 39 | (string * (term list -> term)) list * | 
| 
afa8cf9e63d8
authentic syntax for classes and type constructors;
 wenzelm parents: 
35255diff
changeset | 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 -> | 
| 35429 
afa8cf9e63d8
authentic syntax for classes and type constructors;
 wenzelm parents: 
35255diff
changeset | 43 | (string * (Ast.ast list -> Ast.ast)) list * | 
| 
afa8cf9e63d8
authentic syntax for classes and type constructors;
 wenzelm parents: 
35255diff
changeset | 44 | (string * (term list -> term)) list * | 
| 
afa8cf9e63d8
authentic syntax for classes and type constructors;
 wenzelm parents: 
35255diff
changeset | 45 | (string * (bool -> typ -> term list -> term)) list * | 
| 
afa8cf9e63d8
authentic syntax for classes and type constructors;
 wenzelm parents: 
35255diff
changeset | 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: 
15570diff
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: 
639diff
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: 
2698diff
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: 
18212diff
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" $ | |
| 35429 
afa8cf9e63d8
authentic syntax for classes and type constructors;
 wenzelm parents: 
35255diff
changeset | 134 | Lexicon.const "\\<^const>TYPE" $ (Lexicon.const "\\<^type>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 | ||
| 35429 
afa8cf9e63d8
authentic syntax for classes and type constructors;
 wenzelm parents: 
35255diff
changeset | 146 | fun aprop_tr (*"_aprop"*) [t] = Lexicon.const "_constrain" $ t $ Lexicon.const "\\<^type>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: 
15570diff
changeset | 152 | fun bigimpl_ast_tr (*"_bigimpl"*) (asts as [asms, concl]) = | 
| 
48be8ef738df
transform_failure in translation functions: TRANSLATION_FAIL;
 wenzelm parents: 
15570diff
changeset | 153 | let val prems = | 
| 
48be8ef738df
transform_failure in translation functions: TRANSLATION_FAIL;
 wenzelm parents: 
15570diff
changeset | 154 | (case Ast.unfold_ast_p "_asms" asms of | 
| 
48be8ef738df
transform_failure in translation functions: TRANSLATION_FAIL;
 wenzelm parents: 
15570diff
changeset | 155 | (asms', Ast.Appl [Ast.Constant "_asm", asm']) => asms' @ [asm'] | 
| 
48be8ef738df
transform_failure in translation functions: TRANSLATION_FAIL;
 wenzelm parents: 
15570diff
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: 
14981diff
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: 
14981diff
changeset | 159 | |
| 548 | 160 | |
| 23824 
8ad7131dbfcf
moved print_translations from Pure.thy to Syntax/syn_trans.ML;
 wenzelm parents: 
21773diff
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: 
32786diff
changeset | 193 | (* corresponding updates *) | 
| 
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
 wenzelm parents: 
32786diff
changeset | 194 | |
| 
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
 wenzelm parents: 
32786diff
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: 
32786diff
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: 
32786diff
changeset | 197 |   | update_name_tr (((c as Const ("_constrain", _)) $ t $ ty) :: ts) =
 | 
| 35429 
afa8cf9e63d8
authentic syntax for classes and type constructors;
 wenzelm parents: 
35255diff
changeset | 198 | list_comb (c $ update_name_tr [t] $ | 
| 
afa8cf9e63d8
authentic syntax for classes and type constructors;
 wenzelm parents: 
35255diff
changeset | 199 | (Lexicon.const "\\<^type>fun" $ ty $ Lexicon.const "\\<^type>dummy"), ts) | 
| 35145 
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
 wenzelm parents: 
32786diff
changeset | 200 |   | 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: 
32786diff
changeset | 201 | |
| 
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
 wenzelm parents: 
32786diff
changeset | 202 | |
| 14697 | 203 | (* indexed syntax *) | 
| 204 | ||
| 205 | fun struct_ast_tr (*"_struct"*) [Ast.Appl [Ast.Constant "_index", ast]] = ast | |
| 206 | | struct_ast_tr (*"_struct"*) asts = Ast.mk_appl (Ast.Constant "_struct") asts; | |
| 207 | ||
| 208 | fun index_ast_tr ast = | |
| 209 | Ast.mk_appl (Ast.Constant "_index") [Ast.mk_appl (Ast.Constant "_struct") [ast]]; | |
| 210 | ||
| 211 | fun indexdefault_ast_tr (*"_indexdefault"*) [] = | |
| 212 | index_ast_tr (Ast.Constant "_indexdefault") | |
| 213 | | indexdefault_ast_tr (*"_indexdefault"*) asts = | |
| 214 |       raise Ast.AST ("indexdefault_ast_tr", asts);
 | |
| 215 | ||
| 216 | fun indexnum_ast_tr (*"_indexnum"*) [ast] = | |
| 217 | index_ast_tr (Ast.mk_appl (Ast.Constant "_indexnum") [ast]) | |
| 218 |   | indexnum_ast_tr (*"_indexnum"*) asts = raise Ast.AST ("indexnum_ast_tr", asts);
 | |
| 12122 | 219 | |
| 14697 | 220 | fun indexvar_ast_tr (*"_indexvar"*) [] = | 
| 221 | Ast.mk_appl (Ast.Constant "_index") [Ast.Variable "some_index"] | |
| 222 |   | indexvar_ast_tr (*"_indexvar"*) asts = raise Ast.AST ("indexvar_ast_tr", asts);
 | |
| 223 | ||
| 224 | fun index_tr (*"_index"*) [t] = t | |
| 225 |   | index_tr (*"_index"*) ts = raise TERM ("index_tr", ts);
 | |
| 226 | ||
| 227 | ||
| 228 | (* implicit structures *) | |
| 229 | ||
| 230 | fun the_struct structs i = | |
| 30146 | 231 | if 1 <= i andalso i <= length structs then nth structs (i - 1) | 
| 31542 
3371a3c19bb1
reraise exceptions to preserve position information;
 wenzelm parents: 
30146diff
changeset | 232 |   else error ("Illegal reference to implicit structure #" ^ string_of_int i);
 | 
| 14697 | 233 | |
| 234 | fun struct_tr structs (*"_struct"*) [Const ("_indexdefault", _)] =
 | |
| 235 | Lexicon.free (the_struct structs 1) | |
| 236 |   | struct_tr structs (*"_struct"*) [t as (Const ("_indexnum", _) $ Const (s, _))] =
 | |
| 237 | Lexicon.free (the_struct structs | |
| 15531 | 238 |         (case Lexicon.read_nat s of SOME n => n | NONE => raise TERM ("struct_tr", [t])))
 | 
| 14697 | 239 |   | struct_tr _ (*"_struct"*) ts = raise TERM ("struct_tr", ts);
 | 
| 12122 | 240 | |
| 241 | ||
| 5084 | 242 | |
| 548 | 243 | (** print (ast) translations **) | 
| 244 | ||
| 14647 | 245 | (* types *) | 
| 246 | ||
| 247 | fun non_typed_tr' f _ _ ts = f ts; | |
| 248 | fun non_typed_tr'' f x _ _ ts = f x ts; | |
| 249 | ||
| 250 | ||
| 548 | 251 | (* application *) | 
| 252 | ||
| 5690 | 253 | fun appl_ast_tr' (f, []) = raise Ast.AST ("appl_ast_tr'", [f])
 | 
| 254 | | appl_ast_tr' (f, args) = Ast.Appl [Ast.Constant "_appl", f, Ast.fold_ast "_args" args]; | |
| 548 | 255 | |
| 5690 | 256 | fun applC_ast_tr' (f, []) = raise Ast.AST ("applC_ast_tr'", [f])
 | 
| 257 | | 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: 
639diff
changeset | 258 | |
| 548 | 259 | |
| 260 | (* abstraction *) | |
| 261 | ||
| 19311 | 262 | fun mark_boundT (x, T) = Const ("_bound", T --> T) $ Free (x, T);
 | 
| 2698 | 263 | fun mark_bound x = mark_boundT (x, dummyT); | 
| 264 | ||
| 18958 | 265 | fun bound_vars vars body = | 
| 266 | subst_bounds (map mark_boundT (Term.rename_wrt_term body vars), body); | |
| 267 | ||
| 548 | 268 | fun strip_abss vars_of body_of tm = | 
| 269 | let | |
| 270 | val vars = vars_of tm; | |
| 271 | val body = body_of tm; | |
| 29276 | 272 | val rev_new_vars = Term.rename_wrt_term body vars; | 
| 21750 | 273 | fun subst (x, T) b = | 
| 274 | if can Name.dest_internal x andalso not (Term.loose_bvar1 (b, 0)) | |
| 275 |       then (Const ("_idtdummy", T), incr_boundvars ~1 b)
 | |
| 276 | else (mark_boundT (x, T), Term.subst_bound (mark_bound x, b)); | |
| 277 | val (rev_vars', body') = fold_map subst rev_new_vars body; | |
| 278 | in (rev rev_vars', body') end; | |
| 548 | 279 | |
| 3928 
787d2659ce4a
no longer tries bogus eta-contract involving aprops;
 wenzelm parents: 
3777diff
changeset | 280 | |
| 548 | 281 | (*do (partial) eta-contraction before printing*) | 
| 282 | ||
| 32738 | 283 | val eta_contract = Unsynchronized.ref true; | 
| 548 | 284 | |
| 285 | fun eta_contr tm = | |
| 286 | let | |
| 3928 
787d2659ce4a
no longer tries bogus eta-contract involving aprops;
 wenzelm parents: 
3777diff
changeset | 287 |     fun is_aprop (Const ("_aprop", _)) = true
 | 
| 
787d2659ce4a
no longer tries bogus eta-contract involving aprops;
 wenzelm parents: 
3777diff
changeset | 288 | | is_aprop _ = false; | 
| 
787d2659ce4a
no longer tries bogus eta-contract involving aprops;
 wenzelm parents: 
3777diff
changeset | 289 | |
| 548 | 290 | fun eta_abs (Abs (a, T, t)) = | 
| 291 | (case eta_abs t of | |
| 292 | t' as f $ u => | |
| 293 | (case eta_abs u of | |
| 294 | Bound 0 => | |
| 5084 | 295 | 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: 
3777diff
changeset | 296 | else incr_boundvars ~1 f | 
| 548 | 297 | | _ => Abs (a, T, t')) | 
| 298 | | t' => Abs (a, T, t')) | |
| 299 | | eta_abs t = t; | |
| 300 | in | |
| 301 | if ! eta_contract then eta_abs tm else tm | |
| 302 | end; | |
| 303 | ||
| 304 | ||
| 305 | fun abs_tr' tm = | |
| 19473 | 306 | uncurry (fold_rev (fn x => fn t => Lexicon.const "_abs" $ x $ t)) | 
| 548 | 307 | (strip_abss strip_abs_vars strip_abs_body (eta_contr tm)); | 
| 308 | ||
| 14697 | 309 | fun atomic_abs_tr' (x, T, t) = | 
| 29276 | 310 | let val [xT] = Term.rename_wrt_term t [(x, T)] | 
| 14697 | 311 | in (mark_boundT xT, subst_bound (mark_bound (fst xT), t)) end; | 
| 13762 | 312 | |
| 548 | 313 | fun abs_ast_tr' (*"_abs"*) asts = | 
| 5690 | 314 | (case Ast.unfold_ast_p "_abs" (Ast.Appl (Ast.Constant "_abs" :: asts)) of | 
| 315 |     ([], _) => raise Ast.AST ("abs_ast_tr'", asts)
 | |
| 316 | | (xs, body) => Ast.Appl [Ast.Constant "_lambda", Ast.fold_ast "_pttrns" xs, body]); | |
| 548 | 317 | |
| 32120 
53a21a5e6889
attempt for more concise setup of non-etacontracting binders
 haftmann parents: 
31542diff
changeset | 318 | fun preserve_binder_abs_tr' name syn = (name, fn (Abs abs :: ts) => | 
| 
53a21a5e6889
attempt for more concise setup of non-etacontracting binders
 haftmann parents: 
31542diff
changeset | 319 | let val (x, t) = atomic_abs_tr' abs | 
| 
53a21a5e6889
attempt for more concise setup of non-etacontracting binders
 haftmann parents: 
31542diff
changeset | 320 | in list_comb (Lexicon.const syn $ x $ t, ts) end); | 
| 
53a21a5e6889
attempt for more concise setup of non-etacontracting binders
 haftmann parents: 
31542diff
changeset | 321 | |
| 
53a21a5e6889
attempt for more concise setup of non-etacontracting binders
 haftmann parents: 
31542diff
changeset | 322 | 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: 
31542diff
changeset | 323 | let val (x, t) = atomic_abs_tr' abs | 
| 
53a21a5e6889
attempt for more concise setup of non-etacontracting binders
 haftmann parents: 
31542diff
changeset | 324 | in list_comb (Lexicon.const syn $ x $ A $ t, ts) end); | 
| 
53a21a5e6889
attempt for more concise setup of non-etacontracting binders
 haftmann parents: 
31542diff
changeset | 325 | |
| 548 | 326 | |
| 327 | (* binder *) | |
| 328 | ||
| 21535 | 329 | fun mk_binder_tr' (name, syn) = | 
| 548 | 330 | let | 
| 331 | fun mk_idts [] = raise Match (*abort translation*) | |
| 332 | | mk_idts [idt] = idt | |
| 5690 | 333 | | mk_idts (idt :: idts) = Lexicon.const "_idts" $ idt $ mk_idts idts; | 
| 548 | 334 | |
| 335 | fun tr' t = | |
| 336 | let | |
| 337 | val (xs, bd) = strip_abss (strip_qnt_vars name) (strip_qnt_body name) t; | |
| 21535 | 338 | in Lexicon.const syn $ mk_idts xs $ bd end; | 
| 548 | 339 | |
| 21535 | 340 | fun binder_tr' (t :: ts) = Term.list_comb (tr' (Lexicon.const name $ t), ts) | 
| 341 | | binder_tr' [] = raise Match; | |
| 342 | in (name, binder_tr') end; | |
| 548 | 343 | |
| 344 | ||
| 3691 
f0396ac63e12
tuned lambda_ast_tr, idtyp_ast_tr' to accomodate fix of idt/idts
 wenzelm parents: 
2698diff
changeset | 345 | (* idtyp constraints *) | 
| 548 | 346 | |
| 5690 | 347 | fun idtyp_ast_tr' a [Ast.Appl [Ast.Constant c, x, ty], xs] = | 
| 17788 | 348 | if c = "_constrain" then | 
| 28628 | 349 | Ast.Appl [Ast.Constant a, Ast.Appl [Ast.Constant "_idtyp", x, ty], xs] | 
| 548 | 350 | else raise Match | 
| 3691 
f0396ac63e12
tuned lambda_ast_tr, idtyp_ast_tr' to accomodate fix of idt/idts
 wenzelm parents: 
2698diff
changeset | 351 | | idtyp_ast_tr' _ _ = raise Match; | 
| 548 | 352 | |
| 353 | ||
| 28628 | 354 | (* type propositions *) | 
| 355 | ||
| 35255 | 356 | fun type_prop_tr' _ (*"_type_prop"*) T [Const ("\\<^const>Pure.sort_constraint", _)] =
 | 
| 28628 | 357 | Lexicon.const "_sort_constraint" $ TypeExt.term_of_typ true T | 
| 358 | | type_prop_tr' show_sorts (*"_type_prop"*) T [t] = | |
| 359 | Lexicon.const "_ofclass" $ TypeExt.term_of_typ show_sorts T $ t | |
| 360 |   | type_prop_tr' _ (*"_type_prop"*) T ts = raise TYPE ("type_prop_tr'", [T], ts);
 | |
| 361 | ||
| 362 | ||
| 548 | 363 | (* meta propositions *) | 
| 364 | ||
| 4148 | 365 | fun prop_tr' tm = | 
| 548 | 366 | let | 
| 5690 | 367 | fun aprop t = Lexicon.const "_aprop" $ t; | 
| 548 | 368 | |
| 2698 | 369 | fun is_prop Ts t = | 
| 370 | fastype_of1 (Ts, t) = propT handle TERM _ => false; | |
| 548 | 371 | |
| 35429 
afa8cf9e63d8
authentic syntax for classes and type constructors;
 wenzelm parents: 
35255diff
changeset | 372 |     fun is_term (Const ("Pure.term", _) $ _) = true
 | 
| 19848 | 373 | | is_term _ = false; | 
| 374 | ||
| 548 | 375 | fun tr' _ (t as Const _) = t | 
| 18478 
29a5070b517c
prop_tr': proper handling of aprop marked as bound;
 wenzelm parents: 
18342diff
changeset | 376 |       | tr' Ts (t as Const ("_bound", _) $ u) =
 | 
| 
29a5070b517c
prop_tr': proper handling of aprop marked as bound;
 wenzelm parents: 
18342diff
changeset | 377 | if is_prop Ts u then aprop t else t | 
| 2698 | 378 | | tr' _ (t as Free (x, T)) = | 
| 5690 | 379 | if T = propT then aprop (Lexicon.free x) else t | 
| 2698 | 380 | | tr' _ (t as Var (xi, T)) = | 
| 5690 | 381 | if T = propT then aprop (Lexicon.var xi) else t | 
| 2698 | 382 | | tr' Ts (t as Bound _) = | 
| 383 | if is_prop Ts t then aprop t else t | |
| 384 | | tr' Ts (Abs (x, T, t)) = Abs (x, T, tr' (T :: Ts) t) | |
| 35429 
afa8cf9e63d8
authentic syntax for classes and type constructors;
 wenzelm parents: 
35255diff
changeset | 385 |       | tr' Ts (t as t1 $ (t2 as Const ("TYPE", Type ("itself", [T])))) =
 | 
| 28628 | 386 |           if is_prop Ts t andalso not (is_term t) then Const ("_type_prop", T) $ tr' Ts t1
 | 
| 2698 | 387 | else tr' Ts t1 $ tr' Ts t2 | 
| 388 | | tr' Ts (t as t1 $ t2) = | |
| 5690 | 389 | (if is_Const (Term.head_of t) orelse not (is_prop Ts t) | 
| 2698 | 390 | then I else aprop) (tr' Ts t1 $ tr' Ts t2); | 
| 16612 
48be8ef738df
transform_failure in translation functions: TRANSLATION_FAIL;
 wenzelm parents: 
15570diff
changeset | 391 | in tr' [] tm end; | 
| 548 | 392 | |
| 2698 | 393 | |
| 548 | 394 | (* meta implication *) | 
| 395 | ||
| 396 | fun impl_ast_tr' (*"==>"*) asts = | |
| 10572 | 397 | if TypeExt.no_brackets () then raise Match | 
| 398 | else | |
| 35255 | 399 | (case Ast.unfold_ast_p "\\<^const>==>" (Ast.Appl (Ast.Constant "\\<^const>==>" :: asts)) of | 
| 16612 
48be8ef738df
transform_failure in translation functions: TRANSLATION_FAIL;
 wenzelm parents: 
15570diff
changeset | 400 | (prems as _ :: _ :: _, concl) => | 
| 
48be8ef738df
transform_failure in translation functions: TRANSLATION_FAIL;
 wenzelm parents: 
15570diff
changeset | 401 | let | 
| 
48be8ef738df
transform_failure in translation functions: TRANSLATION_FAIL;
 wenzelm parents: 
15570diff
changeset | 402 | val (asms, asm) = split_last prems; | 
| 
48be8ef738df
transform_failure in translation functions: TRANSLATION_FAIL;
 wenzelm parents: 
15570diff
changeset | 403 | val asms' = Ast.fold_ast_p "_asms" (asms, Ast.Appl [Ast.Constant "_asm", asm]); | 
| 
48be8ef738df
transform_failure in translation functions: TRANSLATION_FAIL;
 wenzelm parents: 
15570diff
changeset | 404 | 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: 
14981diff
changeset | 405 | | _ => raise Match); | 
| 
fcf747c0b6b8
Syntax: last premise of "_bigimpl" is wrapped with "_asm", to have a hook for
 schirmer parents: 
14981diff
changeset | 406 | |
| 548 | 407 | |
| 4148 | 408 | (* type reflection *) | 
| 409 | ||
| 410 | fun type_tr' show_sorts (*"TYPE"*) (Type ("itself", [T])) ts =
 | |
| 5690 | 411 | Term.list_comb (Lexicon.const "_TYPE" $ TypeExt.term_of_typ show_sorts T, ts) | 
| 4148 | 412 | | type_tr' _ _ _ = raise Match; | 
| 413 | ||
| 414 | ||
| 19577 | 415 | (* type constraints *) | 
| 416 | ||
| 417 | fun type_constraint_tr' show_sorts (*"_type_constraint_"*) (Type ("fun", [T, _])) (t :: ts) =
 | |
| 418 | Term.list_comb (Lexicon.const SynExt.constrainC $ t $ TypeExt.term_of_typ show_sorts T, ts) | |
| 419 | | type_constraint_tr' _ _ _ = raise Match; | |
| 420 | ||
| 421 | ||
| 548 | 422 | (* dependent / nondependent quantifiers *) | 
| 423 | ||
| 20202 | 424 | fun var_abs mark (x, T, b) = | 
| 425 | let val ([x'], _) = Name.variants [x] (Term.declare_term_names b Name.context) | |
| 426 | in (x', subst_bound (mark (x', T), b)) end; | |
| 427 | ||
| 428 | val variant_abs = var_abs Free; | |
| 429 | val variant_abs' = var_abs mark_boundT; | |
| 2698 | 430 | |
| 548 | 431 | fun dependent_tr' (q, r) (A :: Abs (x, T, B) :: ts) = | 
| 5084 | 432 | if Term.loose_bvar1 (B, 0) then | 
| 2698 | 433 | let val (x', B') = variant_abs' (x, dummyT, B); | 
| 5690 | 434 | in Term.list_comb (Lexicon.const q $ mark_boundT (x', T) $ A $ B', ts) end | 
| 435 | else Term.list_comb (Lexicon.const r $ A $ B, ts) | |
| 548 | 436 | | dependent_tr' _ _ = raise Match; | 
| 437 | ||
| 438 | ||
| 5084 | 439 | (* quote / antiquote *) | 
| 440 | ||
| 8577 | 441 | fun antiquote_tr' name = | 
| 442 | let | |
| 443 | fun tr' i (t $ u) = | |
| 18139 | 444 | if u aconv Bound i then Lexicon.const name $ tr' i t | 
| 8577 | 445 | else tr' i t $ tr' i u | 
| 446 | | tr' i (Abs (x, T, t)) = Abs (x, T, tr' (i + 1) t) | |
| 18139 | 447 | | tr' i a = if a aconv Bound i then raise Match else a; | 
| 8577 | 448 | in tr' 0 end; | 
| 449 | ||
| 450 | fun quote_tr' name (Abs (_, _, t)) = Term.incr_boundvars ~1 (antiquote_tr' name t) | |
| 451 | | quote_tr' _ _ = raise Match; | |
| 452 | ||
| 5084 | 453 | fun quote_antiquote_tr' quoteN antiquoteN name = | 
| 454 | let | |
| 8577 | 455 | fun tr' (t :: ts) = Term.list_comb (Lexicon.const quoteN $ quote_tr' antiquoteN t, ts) | 
| 456 | | tr' _ = raise Match; | |
| 457 | in (name, tr') end; | |
| 5084 | 458 | |
| 459 | ||
| 35145 
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
 wenzelm parents: 
32786diff
changeset | 460 | (* corresponding updates *) | 
| 
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
 wenzelm parents: 
32786diff
changeset | 461 | |
| 
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
 wenzelm parents: 
32786diff
changeset | 462 | fun upd_tr' (x_upd, T) = | 
| 
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
 wenzelm parents: 
32786diff
changeset | 463 | (case try (unsuffix "_update") x_upd of | 
| 
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
 wenzelm parents: 
32786diff
changeset | 464 | 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: 
32786diff
changeset | 465 | | NONE => raise Match); | 
| 
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
 wenzelm parents: 
32786diff
changeset | 466 | |
| 
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
 wenzelm parents: 
32786diff
changeset | 467 | 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: 
32786diff
changeset | 468 |   | 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: 
32786diff
changeset | 469 | | update_name_tr' (Const x) = Const (upd_tr' x) | 
| 
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
 wenzelm parents: 
32786diff
changeset | 470 | | update_name_tr' _ = raise Match; | 
| 
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
 wenzelm parents: 
32786diff
changeset | 471 | |
| 
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
 wenzelm parents: 
32786diff
changeset | 472 | |
| 14697 | 473 | (* indexed syntax *) | 
| 548 | 474 | |
| 14697 | 475 | fun index_ast_tr' (*"_index"*) [Ast.Appl [Ast.Constant "_struct", ast]] = ast | 
| 476 | | index_ast_tr' _ = raise Match; | |
| 477 | ||
| 478 | ||
| 479 | (* implicit structures *) | |
| 480 | ||
| 481 | fun the_struct' structs s = | |
| 482 | [(case Lexicon.read_nat s of | |
| 18678 | 483 | SOME i => Ast.Variable (the_struct structs i handle ERROR _ => raise Match) | 
| 15531 | 484 | | NONE => raise Match)] |> Ast.mk_appl (Ast.Constant "_free"); | 
| 14697 | 485 | |
| 486 | fun struct_ast_tr' structs (*"_struct"*) [Ast.Constant "_indexdefault"] = | |
| 487 | the_struct' structs "1" | |
| 488 | | struct_ast_tr' structs (*"_struct"*) [Ast.Appl [Ast.Constant "_indexnum", Ast.Constant s]] = | |
| 489 | the_struct' structs s | |
| 490 | | struct_ast_tr' _ _ = raise Match; | |
| 491 | ||
| 492 | ||
| 493 | ||
| 494 | (** Pure translations **) | |
| 548 | 495 | |
| 496 | val pure_trfuns = | |
| 35145 
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
 wenzelm parents: 
32786diff
changeset | 497 |   ([("_constify", constify_ast_tr),
 | 
| 
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
 wenzelm parents: 
32786diff
changeset | 498 |     ("_appl", appl_ast_tr),
 | 
| 
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
 wenzelm parents: 
32786diff
changeset | 499 |     ("_applC", applC_ast_tr),
 | 
| 
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
 wenzelm parents: 
32786diff
changeset | 500 |     ("_lambda", lambda_ast_tr),
 | 
| 
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
 wenzelm parents: 
32786diff
changeset | 501 |     ("_idtyp", idtyp_ast_tr),
 | 
| 
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
 wenzelm parents: 
32786diff
changeset | 502 |     ("_idtypdummy", idtypdummy_ast_tr),
 | 
| 
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
 wenzelm parents: 
32786diff
changeset | 503 |     ("_bigimpl", bigimpl_ast_tr),
 | 
| 
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
 wenzelm parents: 
32786diff
changeset | 504 |     ("_indexdefault", indexdefault_ast_tr),
 | 
| 
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
 wenzelm parents: 
32786diff
changeset | 505 |     ("_indexnum", indexnum_ast_tr),
 | 
| 
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
 wenzelm parents: 
32786diff
changeset | 506 |     ("_indexvar", indexvar_ast_tr),
 | 
| 
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
 wenzelm parents: 
32786diff
changeset | 507 |     ("_struct", struct_ast_tr)],
 | 
| 
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
 wenzelm parents: 
32786diff
changeset | 508 |    [("_abs", abs_tr),
 | 
| 
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
 wenzelm parents: 
32786diff
changeset | 509 |     ("_aprop", aprop_tr),
 | 
| 
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
 wenzelm parents: 
32786diff
changeset | 510 |     ("_ofclass", ofclass_tr),
 | 
| 
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
 wenzelm parents: 
32786diff
changeset | 511 |     ("_sort_constraint", sort_constraint_tr),
 | 
| 
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
 wenzelm parents: 
32786diff
changeset | 512 |     ("_TYPE", type_tr),
 | 
| 
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
 wenzelm parents: 
32786diff
changeset | 513 |     ("_DDDOT", dddot_tr),
 | 
| 
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
 wenzelm parents: 
32786diff
changeset | 514 |     ("_update_name", update_name_tr),
 | 
| 
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
 wenzelm parents: 
32786diff
changeset | 515 |     ("_index", index_tr)],
 | 
| 35198 | 516 | ([]: (string * (term list -> term)) list), | 
| 35145 
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
 wenzelm parents: 
32786diff
changeset | 517 |    [("_abs", abs_ast_tr'),
 | 
| 
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
 wenzelm parents: 
32786diff
changeset | 518 |     ("_idts", idtyp_ast_tr' "_idts"),
 | 
| 
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
 wenzelm parents: 
32786diff
changeset | 519 |     ("_pttrns", idtyp_ast_tr' "_pttrns"),
 | 
| 35255 | 520 |     ("\\<^const>==>", impl_ast_tr'),
 | 
| 35145 
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
 wenzelm parents: 
32786diff
changeset | 521 |     ("_index", index_ast_tr')]);
 | 
| 548 | 522 | |
| 2698 | 523 | val pure_trfunsT = | 
| 35255 | 524 |  [("_type_prop", type_prop_tr'),
 | 
| 525 |   ("\\<^const>TYPE", type_tr'),
 | |
| 526 |   ("_type_constraint_", type_constraint_tr')];
 | |
| 2698 | 527 | |
| 14697 | 528 | fun struct_trfuns structs = | 
| 529 |   ([], [("_struct", struct_tr structs)], [], [("_struct", struct_ast_tr' structs)]);
 | |
| 530 | ||
| 548 | 531 | |
| 532 | ||
| 14868 | 533 | (** pts_to_asts **) | 
| 548 | 534 | |
| 21773 | 535 | fun pts_to_asts ctxt trf pts = | 
| 548 | 536 | let | 
| 537 | fun trans a args = | |
| 538 | (case trf a of | |
| 15531 | 539 | NONE => Ast.mk_appl (Ast.Constant a) args | 
| 23937 
66e1f24d655d
eliminated transform_failure (to avoid critical section for main transactions);
 wenzelm parents: 
23824diff
changeset | 540 | | SOME f => f ctxt args); | 
| 548 | 541 | |
| 987 | 542 | (*translate pt bottom-up*) | 
| 5690 | 543 | fun ast_of (Parser.Node (a, pts)) = trans a (map ast_of pts) | 
| 544 | | 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: 
14697diff
changeset | 545 | |
| 23963 
c2ee97a963db
moved exception capture/release to structure Exn;
 wenzelm parents: 
23937diff
changeset | 546 | val exn_results = map (Exn.capture ast_of) pts; | 
| 
c2ee97a963db
moved exception capture/release to structure Exn;
 wenzelm parents: 
23937diff
changeset | 547 | val exns = map_filter Exn.get_exn exn_results; | 
| 
c2ee97a963db
moved exception capture/release to structure Exn;
 wenzelm parents: 
23937diff
changeset | 548 | val results = map_filter Exn.get_result exn_results | 
| 31542 
3371a3c19bb1
reraise exceptions to preserve position information;
 wenzelm parents: 
30146diff
changeset | 549 | in (case (results, exns) of ([], exn :: _) => reraise exn | _ => results) end; | 
| 548 | 550 | |
| 551 | ||
| 552 | ||
| 14798 
702cb4859cab
Modified functions pt_to_ast and ast_to_term to improve handling
 berghofe parents: 
14697diff
changeset | 553 | (** asts_to_terms **) | 
| 548 | 554 | |
| 21773 | 555 | fun asts_to_terms ctxt trf asts = | 
| 548 | 556 | let | 
| 557 | fun trans a args = | |
| 558 | (case trf a of | |
| 15531 | 559 | NONE => Term.list_comb (Lexicon.const a, args) | 
| 23937 
66e1f24d655d
eliminated transform_failure (to avoid critical section for main transactions);
 wenzelm parents: 
23824diff
changeset | 560 | | SOME f => f ctxt args); | 
| 548 | 561 | |
| 5690 | 562 | fun term_of (Ast.Constant a) = trans a [] | 
| 563 | | term_of (Ast.Variable x) = Lexicon.read_var x | |
| 564 | | term_of (Ast.Appl (Ast.Constant a :: (asts as _ :: _))) = | |
| 548 | 565 | trans a (map term_of asts) | 
| 5690 | 566 | | term_of (Ast.Appl (ast :: (asts as _ :: _))) = | 
| 567 | Term.list_comb (term_of ast, map term_of asts) | |
| 568 |       | 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: 
14697diff
changeset | 569 | |
| 36502 
586af36cf3cc
uniform decoding of fixed/const syntax entities, allows to pass "\<^fixed>foo__" through the syntax layer (supersedes 1b7109c10b7b);
 wenzelm parents: 
35429diff
changeset | 570 | val exn_results = map (Exn.capture term_of) asts; | 
| 23963 
c2ee97a963db
moved exception capture/release to structure Exn;
 wenzelm parents: 
23937diff
changeset | 571 | val exns = map_filter Exn.get_exn exn_results; | 
| 
c2ee97a963db
moved exception capture/release to structure Exn;
 wenzelm parents: 
23937diff
changeset | 572 | val results = map_filter Exn.get_result exn_results | 
| 31542 
3371a3c19bb1
reraise exceptions to preserve position information;
 wenzelm parents: 
30146diff
changeset | 573 | in (case (results, exns) of ([], exn :: _) => reraise exn | _ => results) end; | 
| 548 | 574 | |
| 575 | end; |