| author | haftmann | 
| Thu, 14 Jul 2011 00:21:56 +0200 | |
| changeset 43819 | 89082fd9e32d | 
| parent 42407 | 5b9dd52f5dca | 
| child 44057 | fda143b5c2f5 | 
| permissions | -rw-r--r-- | 
| 13402 | 1 | (* Title: Pure/Proof/extraction.ML | 
| 2 | Author: Stefan Berghofer, TU Muenchen | |
| 3 | ||
| 4 | Extraction of programs from proofs. | |
| 5 | *) | |
| 6 | ||
| 7 | signature EXTRACTION = | |
| 8 | sig | |
| 16458 | 9 | val set_preprocessor : (theory -> Proofterm.proof -> Proofterm.proof) -> theory -> theory | 
| 13402 | 10 | val add_realizes_eqns_i : ((term * term) list * (term * term)) list -> theory -> theory | 
| 11 | val add_realizes_eqns : string list -> theory -> theory | |
| 12 | val add_typeof_eqns_i : ((term * term) list * (term * term)) list -> theory -> theory | |
| 13 | val add_typeof_eqns : string list -> theory -> theory | |
| 14 | val add_realizers_i : (string * (string list * term * Proofterm.proof)) list | |
| 15 | -> theory -> theory | |
| 16 | val add_realizers : (thm * (string list * string * string)) list | |
| 17 | -> theory -> theory | |
| 33704 
6aeb8454efc1
add_expand_thm: explicit indication of is_def instead of fragile heuristic, tuned signature;
 wenzelm parents: 
33522diff
changeset | 18 | val add_expand_thm : bool -> thm -> theory -> theory | 
| 13732 | 19 | val add_types : (xstring * ((term -> term option) list * | 
| 20 | (term -> typ -> term -> typ -> term) option)) list -> theory -> theory | |
| 21 | val extract : (thm * string list) list -> theory -> theory | |
| 13402 | 22 | val nullT : typ | 
| 23 | val nullt : term | |
| 13714 | 24 | val mk_typ : typ -> term | 
| 25 | val etype_of : theory -> string list -> typ list -> term -> typ | |
| 26 | val realizes_of: theory -> string list -> term -> term -> term | |
| 37233 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 27 | val abs_corr_shyps: theory -> thm -> string list -> term list -> Proofterm.proof -> Proofterm.proof | 
| 13402 | 28 | end; | 
| 29 | ||
| 30 | structure Extraction : EXTRACTION = | |
| 31 | struct | |
| 32 | ||
| 33 | (**** tools ****) | |
| 34 | ||
| 42407 
5b9dd52f5dca
prefer internal types, via Simple_Syntax.read_typ;
 wenzelm parents: 
42406diff
changeset | 35 | val typ = Simple_Syntax.read_typ; | 
| 
5b9dd52f5dca
prefer internal types, via Simple_Syntax.read_typ;
 wenzelm parents: 
42406diff
changeset | 36 | |
| 
5b9dd52f5dca
prefer internal types, via Simple_Syntax.read_typ;
 wenzelm parents: 
42406diff
changeset | 37 | val add_syntax = | 
| 
5b9dd52f5dca
prefer internal types, via Simple_Syntax.read_typ;
 wenzelm parents: 
42406diff
changeset | 38 | Theory.copy | 
| 
5b9dd52f5dca
prefer internal types, via Simple_Syntax.read_typ;
 wenzelm parents: 
42406diff
changeset | 39 | #> Sign.root_path | 
| 
5b9dd52f5dca
prefer internal types, via Simple_Syntax.read_typ;
 wenzelm parents: 
42406diff
changeset | 40 | #> Sign.add_types_global [(Binding.name "Type", 0, NoSyn), (Binding.name "Null", 0, NoSyn)] | 
| 
5b9dd52f5dca
prefer internal types, via Simple_Syntax.read_typ;
 wenzelm parents: 
42406diff
changeset | 41 | #> Sign.add_consts_i | 
| 
5b9dd52f5dca
prefer internal types, via Simple_Syntax.read_typ;
 wenzelm parents: 
42406diff
changeset | 42 | [(Binding.name "typeof", typ "'b => Type", NoSyn), | 
| 
5b9dd52f5dca
prefer internal types, via Simple_Syntax.read_typ;
 wenzelm parents: 
42406diff
changeset | 43 | (Binding.name "Type", typ "'a itself => Type", NoSyn), | 
| 
5b9dd52f5dca
prefer internal types, via Simple_Syntax.read_typ;
 wenzelm parents: 
42406diff
changeset | 44 | (Binding.name "Null", typ "Null", NoSyn), | 
| 
5b9dd52f5dca
prefer internal types, via Simple_Syntax.read_typ;
 wenzelm parents: 
42406diff
changeset | 45 | (Binding.name "realizes", typ "'a => 'b => 'b", NoSyn)]; | 
| 13402 | 46 | |
| 47 | val nullT = Type ("Null", []);
 | |
| 48 | val nullt = Const ("Null", nullT);
 | |
| 49 | ||
| 50 | fun mk_typ T = | |
| 19391 | 51 |   Const ("Type", Term.itselfT T --> Type ("Type", [])) $ Logic.mk_type T;
 | 
| 13402 | 52 | |
| 53 | fun typeof_proc defaultS vs (Const ("typeof", _) $ u) =
 | |
| 15531 | 54 | SOME (mk_typ (case strip_comb u of | 
| 13402 | 55 | (Var ((a, i), _), _) => | 
| 20664 | 56 |             if member (op =) vs a then TFree ("'" ^ a ^ ":" ^ string_of_int i, defaultS)
 | 
| 13402 | 57 | else nullT | 
| 58 | | (Free (a, _), _) => | |
| 20664 | 59 |             if member (op =) vs a then TFree ("'" ^ a, defaultS) else nullT
 | 
| 13402 | 60 | | _ => nullT)) | 
| 15531 | 61 | | typeof_proc _ _ _ = NONE; | 
| 13402 | 62 | |
| 15531 | 63 | fun rlz_proc (Const ("realizes", Type (_, [Type ("Null", []), _])) $ r $ t) = SOME t
 | 
| 13732 | 64 |   | rlz_proc (Const ("realizes", Type (_, [T, _])) $ r $ t) =
 | 
| 65 | (case strip_comb t of | |
| 15531 | 66 | (Var (ixn, U), ts) => SOME (list_comb (Var (ixn, T --> U), r :: ts)) | 
| 67 | | (Free (s, U), ts) => SOME (list_comb (Free (s, T --> U), r :: ts)) | |
| 68 | | _ => NONE) | |
| 69 | | rlz_proc _ = NONE; | |
| 13402 | 70 | |
| 71 | val unpack_ixn = apfst implode o apsnd (fst o read_int o tl) o | |
| 40627 
becf5d5187cc
renamed raw "explode" function to "raw_explode" to emphasize its meaning;
 wenzelm parents: 
40132diff
changeset | 72 | take_prefix (fn s => s <> ":") o raw_explode; | 
| 13402 | 73 | |
| 74 | type rules = | |
| 75 |   {next: int, rs: ((term * term) list * (term * term)) list,
 | |
| 76 | net: (int * ((term * term) list * (term * term))) Net.net}; | |
| 77 | ||
| 78 | val empty_rules : rules = {next = 0, rs = [], net = Net.empty};
 | |
| 79 | ||
| 33337 | 80 | fun add_rule (r as (_, (lhs, _))) ({next, rs, net} : rules) =
 | 
| 16800 | 81 |   {next = next - 1, rs = r :: rs, net = Net.insert_term (K false)
 | 
| 18956 | 82 | (Envir.eta_contract lhs, (next, r)) net}; | 
| 13402 | 83 | |
| 32784 | 84 | fun merge_rules ({next, rs = rs1, net} : rules) ({rs = rs2, ...} : rules) =
 | 
| 33337 | 85 |   fold_rev add_rule (subtract (op =) rs1 rs2) {next = next, rs = rs1, net = net};
 | 
| 13402 | 86 | |
| 16458 | 87 | fun condrew thy rules procs = | 
| 13402 | 88 | let | 
| 89 | fun rew tm = | |
| 17203 | 90 | Pattern.rewrite_term thy [] (condrew' :: procs) tm | 
| 15399 
683d83051d6a
Added term cache to function condrew in order to speed up rewriting.
 berghofe parents: 
14981diff
changeset | 91 | and condrew' tm = | 
| 13402 | 92 | let | 
| 32738 | 93 | val cache = Unsynchronized.ref ([] : (term * term) list); | 
| 17232 | 94 | fun lookup f x = (case AList.lookup (op =) (!cache) x of | 
| 15531 | 95 | NONE => | 
| 15399 
683d83051d6a
Added term cache to function condrew in order to speed up rewriting.
 berghofe parents: 
14981diff
changeset | 96 | let val y = f x | 
| 
683d83051d6a
Added term cache to function condrew in order to speed up rewriting.
 berghofe parents: 
14981diff
changeset | 97 | in (cache := (x, y) :: !cache; y) end | 
| 15531 | 98 | | SOME y => y); | 
| 15399 
683d83051d6a
Added term cache to function condrew in order to speed up rewriting.
 berghofe parents: 
14981diff
changeset | 99 | in | 
| 
683d83051d6a
Added term cache to function condrew in order to speed up rewriting.
 berghofe parents: 
14981diff
changeset | 100 | get_first (fn (_, (prems, (tm1, tm2))) => | 
| 
683d83051d6a
Added term cache to function condrew in order to speed up rewriting.
 berghofe parents: 
14981diff
changeset | 101 | let | 
| 19466 | 102 | fun ren t = the_default t (Term.rename_abs tm1 tm t); | 
| 15399 
683d83051d6a
Added term cache to function condrew in order to speed up rewriting.
 berghofe parents: 
14981diff
changeset | 103 | val inc = Logic.incr_indexes ([], maxidx_of_term tm + 1); | 
| 18184 | 104 | val env as (Tenv, tenv) = Pattern.match thy (inc tm1, tm) (Vartab.empty, Vartab.empty); | 
| 32035 | 105 | val prems' = map (pairself (Envir.subst_term env o inc o ren)) prems; | 
| 15399 
683d83051d6a
Added term cache to function condrew in order to speed up rewriting.
 berghofe parents: 
14981diff
changeset | 106 | val env' = Envir.Envir | 
| 32032 | 107 |             {maxidx = fold (fn (t, u) => Term.maxidx_term t #> Term.maxidx_term u) prems' ~1,
 | 
| 108 | tenv = tenv, tyenv = Tenv}; | |
| 18184 | 109 | val env'' = fold (Pattern.unify thy o pairself (lookup rew)) prems' env'; | 
| 15531 | 110 | in SOME (Envir.norm_term env'' (inc (ren tm2))) | 
| 111 | end handle Pattern.MATCH => NONE | Pattern.Unif => NONE) | |
| 16486 | 112 | (sort (int_ord o pairself fst) | 
| 18956 | 113 | (Net.match_term rules (Envir.eta_contract tm))) | 
| 15399 
683d83051d6a
Added term cache to function condrew in order to speed up rewriting.
 berghofe parents: 
14981diff
changeset | 114 | end; | 
| 13402 | 115 | |
| 116 | in rew end; | |
| 117 | ||
| 37310 | 118 | val chtype = Proofterm.change_type o SOME; | 
| 13402 | 119 | |
| 30364 
577edc39b501
moved basic algebra of long names from structure NameSpace to Long_Name;
 wenzelm parents: 
30344diff
changeset | 120 | fun extr_name s vs = Long_Name.append "extr" (space_implode "_" (s :: vs)); | 
| 16195 | 121 | fun corr_name s vs = extr_name s vs ^ "_correctness"; | 
| 13402 | 122 | |
| 40132 
7ee65dbffa31
renamed Output.priority to Output.urgent_message to emphasize its special role more clearly;
 wenzelm parents: 
39557diff
changeset | 123 | fun msg d s = Output.urgent_message (Symbol.spaces d ^ s); | 
| 13402 | 124 | |
| 28812 
413695e07bd4
Frees in PThms are now quantified in the order of their appearance in the
 berghofe parents: 
28805diff
changeset | 125 | fun vars_of t = map Var (rev (Term.add_vars t [])); | 
| 
413695e07bd4
Frees in PThms are now quantified in the order of their appearance in the
 berghofe parents: 
28805diff
changeset | 126 | fun frees_of t = map Free (rev (Term.add_frees t [])); | 
| 
413695e07bd4
Frees in PThms are now quantified in the order of their appearance in the
 berghofe parents: 
28805diff
changeset | 127 | fun vfs_of t = vars_of t @ frees_of t; | 
| 13402 | 128 | |
| 37233 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 129 | val mkabs = fold_rev (fn v => fn t => Abs ("x", fastype_of v, abstract_over (v, t)));
 | 
| 13402 | 130 | |
| 37233 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 131 | val mkabsp = fold_rev (fn t => fn prf => AbsP ("H", SOME t, prf));
 | 
| 13402 | 132 | |
| 13732 | 133 | fun strip_abs 0 t = t | 
| 134 | | strip_abs n (Abs (_, _, t)) = strip_abs (n-1) t | |
| 135 | | strip_abs _ _ = error "strip_abs: not an abstraction"; | |
| 136 | ||
| 37310 | 137 | val prf_subst_TVars = Proofterm.map_proof_types o typ_subst_TVars; | 
| 13402 | 138 | |
| 40844 | 139 | fun relevant_vars types prop = | 
| 140 | List.foldr | |
| 141 | (fn (Var ((a, _), T), vs) => | |
| 142 | (case body_type T of | |
| 143 | Type (s, _) => if member (op =) types s then a :: vs else vs | |
| 144 | | _ => vs) | |
| 145 | | (_, vs) => vs) [] (vars_of prop); | |
| 13402 | 146 | |
| 13732 | 147 | fun tname_of (Type (s, _)) = s | 
| 148 | | tname_of _ = ""; | |
| 149 | ||
| 150 | fun get_var_type t = | |
| 151 | let | |
| 16865 | 152 | val vs = Term.add_vars t []; | 
| 153 | val fs = Term.add_frees t []; | |
| 42406 
05f2468d6b36
eliminated obsolete Proof_Syntax.strip_sorts_consttypes;
 wenzelm parents: 
42375diff
changeset | 154 | in | 
| 
05f2468d6b36
eliminated obsolete Proof_Syntax.strip_sorts_consttypes;
 wenzelm parents: 
42375diff
changeset | 155 | fn Var (ixn, _) => | 
| 
05f2468d6b36
eliminated obsolete Proof_Syntax.strip_sorts_consttypes;
 wenzelm parents: 
42375diff
changeset | 156 | (case AList.lookup (op =) vs ixn of | 
| 15531 | 157 | NONE => error "get_var_type: no such variable in term" | 
| 158 | | SOME T => Var (ixn, T)) | |
| 42406 
05f2468d6b36
eliminated obsolete Proof_Syntax.strip_sorts_consttypes;
 wenzelm parents: 
42375diff
changeset | 159 | | Free (s, _) => | 
| 
05f2468d6b36
eliminated obsolete Proof_Syntax.strip_sorts_consttypes;
 wenzelm parents: 
42375diff
changeset | 160 | (case AList.lookup (op =) fs s of | 
| 15531 | 161 | NONE => error "get_var_type: no such variable in term" | 
| 162 | | SOME T => Free (s, T)) | |
| 13732 | 163 | | _ => error "get_var_type: not a variable" | 
| 164 | end; | |
| 165 | ||
| 37233 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 166 | fun read_term thy T s = | 
| 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 167 | let | 
| 42360 | 168 | val ctxt = Proof_Context.init_global thy | 
| 42406 
05f2468d6b36
eliminated obsolete Proof_Syntax.strip_sorts_consttypes;
 wenzelm parents: 
42375diff
changeset | 169 | |> Config.put Type_Infer_Context.const_sorts false | 
| 42360 | 170 | |> Proof_Context.set_defsort []; | 
| 37233 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 171 | val parse = if T = propT then Syntax.parse_prop else Syntax.parse_term; | 
| 39288 | 172 | in parse ctxt s |> Type.constraint T |> Syntax.check_term ctxt end; | 
| 37233 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 173 | |
| 13402 | 174 | |
| 175 | (**** theory data ****) | |
| 176 | ||
| 22846 | 177 | (* theory data *) | 
| 13402 | 178 | |
| 33522 | 179 | structure ExtractionData = Theory_Data | 
| 22846 | 180 | ( | 
| 13402 | 181 | type T = | 
| 182 |     {realizes_eqns : rules,
 | |
| 183 | typeof_eqns : rules, | |
| 13732 | 184 | types : (string * ((term -> term option) list * | 
| 185 | (term -> typ -> term -> typ -> term) option)) list, | |
| 13402 | 186 | realizers : (string list * (term * proof)) list Symtab.table, | 
| 187 | defs : thm list, | |
| 37233 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 188 | expand : string list, | 
| 16458 | 189 | prep : (theory -> proof -> proof) option} | 
| 13402 | 190 | |
| 191 | val empty = | |
| 192 |     {realizes_eqns = empty_rules,
 | |
| 193 | typeof_eqns = empty_rules, | |
| 194 | types = [], | |
| 195 | realizers = Symtab.empty, | |
| 196 | defs = [], | |
| 197 | expand = [], | |
| 15531 | 198 | prep = NONE}; | 
| 16458 | 199 | val extend = I; | 
| 13402 | 200 | |
| 33522 | 201 | fun merge | 
| 202 |     ({realizes_eqns = realizes_eqns1, typeof_eqns = typeof_eqns1, types = types1,
 | |
| 13402 | 203 | realizers = realizers1, defs = defs1, expand = expand1, prep = prep1}, | 
| 204 |       {realizes_eqns = realizes_eqns2, typeof_eqns = typeof_eqns2, types = types2,
 | |
| 33522 | 205 | realizers = realizers2, defs = defs2, expand = expand2, prep = prep2}) : T = | 
| 13402 | 206 |     {realizes_eqns = merge_rules realizes_eqns1 realizes_eqns2,
 | 
| 207 | typeof_eqns = merge_rules typeof_eqns1 typeof_eqns2, | |
| 22717 | 208 | types = AList.merge (op =) (K true) (types1, types2), | 
| 33038 | 209 | realizers = Symtab.merge_list (eq_set (op =) o pairself #1) (realizers1, realizers2), | 
| 22662 | 210 | defs = Library.merge Thm.eq_thm (defs1, defs2), | 
| 37233 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 211 | expand = Library.merge (op =) (expand1, expand2), | 
| 38761 | 212 | prep = if is_some prep1 then prep1 else prep2}; | 
| 22846 | 213 | ); | 
| 13402 | 214 | |
| 215 | fun read_condeq thy = | |
| 16458 | 216 | let val thy' = add_syntax thy | 
| 13402 | 217 | in fn s => | 
| 37233 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 218 | let val t = Logic.varify_global (read_term thy' propT s) | 
| 35424 | 219 | in | 
| 220 | (map Logic.dest_equals (Logic.strip_imp_prems t), | |
| 221 | Logic.dest_equals (Logic.strip_imp_concl t)) | |
| 222 |       handle TERM _ => error ("Not a (conditional) meta equality:\n" ^ s)
 | |
| 223 | end | |
| 13402 | 224 | end; | 
| 225 | ||
| 226 | (** preprocessor **) | |
| 227 | ||
| 228 | fun set_preprocessor prep thy = | |
| 229 |   let val {realizes_eqns, typeof_eqns, types, realizers,
 | |
| 230 | defs, expand, ...} = ExtractionData.get thy | |
| 231 | in | |
| 232 | ExtractionData.put | |
| 233 |       {realizes_eqns = realizes_eqns, typeof_eqns = typeof_eqns, types = types,
 | |
| 15531 | 234 | realizers = realizers, defs = defs, expand = expand, prep = SOME prep} thy | 
| 13402 | 235 | end; | 
| 236 | ||
| 237 | (** equations characterizing realizability **) | |
| 238 | ||
| 239 | fun gen_add_realizes_eqns prep_eq eqns thy = | |
| 240 |   let val {realizes_eqns, typeof_eqns, types, realizers,
 | |
| 241 | defs, expand, prep} = ExtractionData.get thy; | |
| 242 | in | |
| 243 | ExtractionData.put | |
| 33337 | 244 |       {realizes_eqns = fold_rev add_rule (map (prep_eq thy) eqns) realizes_eqns,
 | 
| 13402 | 245 | typeof_eqns = typeof_eqns, types = types, realizers = realizers, | 
| 246 | defs = defs, expand = expand, prep = prep} thy | |
| 247 | end | |
| 248 | ||
| 249 | val add_realizes_eqns_i = gen_add_realizes_eqns (K I); | |
| 250 | val add_realizes_eqns = gen_add_realizes_eqns read_condeq; | |
| 251 | ||
| 252 | (** equations characterizing type of extracted program **) | |
| 253 | ||
| 254 | fun gen_add_typeof_eqns prep_eq eqns thy = | |
| 255 | let | |
| 256 |     val {realizes_eqns, typeof_eqns, types, realizers,
 | |
| 257 | defs, expand, prep} = ExtractionData.get thy; | |
| 13732 | 258 | val eqns' = map (prep_eq thy) eqns | 
| 13402 | 259 | in | 
| 260 | ExtractionData.put | |
| 261 |       {realizes_eqns = realizes_eqns, realizers = realizers,
 | |
| 33337 | 262 | typeof_eqns = fold_rev add_rule eqns' typeof_eqns, | 
| 13732 | 263 | types = types, defs = defs, expand = expand, prep = prep} thy | 
| 13402 | 264 | end | 
| 265 | ||
| 266 | val add_typeof_eqns_i = gen_add_typeof_eqns (K I); | |
| 267 | val add_typeof_eqns = gen_add_typeof_eqns read_condeq; | |
| 268 | ||
| 269 | fun thaw (T as TFree (a, S)) = | |
| 28375 | 270 | if exists_string (fn s => s = ":") a then TVar (unpack_ixn a, S) else T | 
| 13402 | 271 | | thaw (Type (a, Ts)) = Type (a, map thaw Ts) | 
| 272 | | thaw T = T; | |
| 273 | ||
| 274 | fun freeze (TVar ((a, i), S)) = TFree (a ^ ":" ^ string_of_int i, S) | |
| 275 | | freeze (Type (a, Ts)) = Type (a, map freeze Ts) | |
| 276 | | freeze T = T; | |
| 277 | ||
| 278 | fun freeze_thaw f x = | |
| 20548 
8ef25fe585a8
renamed Term.map_term_types to Term.map_types (cf. Term.fold_types);
 wenzelm parents: 
19482diff
changeset | 279 | map_types thaw (f (map_types freeze x)); | 
| 13402 | 280 | |
| 16458 | 281 | fun etype_of thy vs Ts t = | 
| 13402 | 282 | let | 
| 16458 | 283 |     val {typeof_eqns, ...} = ExtractionData.get thy;
 | 
| 13402 | 284 |     fun err () = error ("Unable to determine type of extracted program for\n" ^
 | 
| 26939 
1035c89b4c02
moved global pretty/string_of functions from Sign to Syntax;
 wenzelm parents: 
26653diff
changeset | 285 | Syntax.string_of_term_global thy t) | 
| 16458 | 286 | in case strip_abs_body (freeze_thaw (condrew thy (#net typeof_eqns) | 
| 37233 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 287 | [typeof_proc [] vs]) (list_abs (map (pair "x") (rev Ts), | 
| 13402 | 288 |       Const ("typeof", fastype_of1 (Ts, t) --> Type ("Type", [])) $ t))) of
 | 
| 289 |       Const ("Type", _) $ u => (Logic.dest_type u handle TERM _ => err ())
 | |
| 290 | | _ => err () | |
| 291 | end; | |
| 292 | ||
| 293 | (** realizers for axioms / theorems, together with correctness proofs **) | |
| 294 | ||
| 295 | fun gen_add_realizers prep_rlz rs thy = | |
| 296 |   let val {realizes_eqns, typeof_eqns, types, realizers,
 | |
| 297 | defs, expand, prep} = ExtractionData.get thy | |
| 298 | in | |
| 299 | ExtractionData.put | |
| 300 |       {realizes_eqns = realizes_eqns, typeof_eqns = typeof_eqns, types = types,
 | |
| 25389 | 301 | realizers = fold (Symtab.cons_list o prep_rlz thy) rs realizers, | 
| 13402 | 302 | defs = defs, expand = expand, prep = prep} thy | 
| 303 | end | |
| 304 | ||
| 305 | fun prep_realizer thy = | |
| 306 | let | |
| 13732 | 307 |     val {realizes_eqns, typeof_eqns, defs, types, ...} =
 | 
| 13402 | 308 | ExtractionData.get thy; | 
| 19482 
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
 wenzelm parents: 
19466diff
changeset | 309 | val procs = maps (fst o snd) types; | 
| 13732 | 310 | val rtypes = map fst types; | 
| 16800 | 311 | val eqns = Net.merge (K false) (#net realizes_eqns, #net typeof_eqns); | 
| 13402 | 312 | val thy' = add_syntax thy; | 
| 37233 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 313 | val rd = Proof_Syntax.read_proof thy' true false; | 
| 13402 | 314 | in fn (thm, (vs, s1, s2)) => | 
| 315 | let | |
| 36744 
6e1f3d609a68
renamed Thm.get_name -> Thm.derivation_name and Thm.put_name -> Thm.name_derivation, to emphasize the true nature of these operations;
 wenzelm parents: 
36620diff
changeset | 316 | val name = Thm.derivation_name thm; | 
| 21858 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 wenzelm parents: 
21646diff
changeset | 317 | val _ = name <> "" orelse error "add_realizers: unnamed theorem"; | 
| 37233 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 318 | val prop = Thm.unconstrainT thm |> prop_of |> | 
| 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 319 | Pattern.rewrite_term thy' (map (Logic.dest_equals o prop_of) defs) []; | 
| 13402 | 320 | val vars = vars_of prop; | 
| 13732 | 321 | val vars' = filter_out (fn v => | 
| 20664 | 322 | member (op =) rtypes (tname_of (body_type (fastype_of v)))) vars; | 
| 37233 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 323 | val shyps = maps (fn Var ((x, i), _) => | 
| 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 324 | if member (op =) vs x then Logic.mk_of_sort | 
| 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 325 |           (TVar (("'" ^ x, i), []), Sign.defaultS thy')
 | 
| 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 326 | else []) vars; | 
| 16458 | 327 | val T = etype_of thy' vs [] prop; | 
| 33832 | 328 | val (T', thw) = Type.legacy_freeze_thaw_type | 
| 13732 | 329 | (if T = nullT then nullT else map fastype_of vars' ---> T); | 
| 37233 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 330 | val t = map_types thw (read_term thy' T' s1); | 
| 16458 | 331 | val r' = freeze_thaw (condrew thy' eqns | 
| 37233 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 332 | (procs @ [typeof_proc [] vs, rlz_proc])) | 
| 13402 | 333 |           (Const ("realizes", T --> propT --> propT) $
 | 
| 13732 | 334 | (if T = nullT then t else list_comb (t, vars')) $ prop); | 
| 37233 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 335 | val r = Logic.list_implies (shyps, | 
| 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 336 | fold_rev Logic.all (map (get_var_type r') vars) r'); | 
| 16458 | 337 | val prf = Reconstruct.reconstruct_proof thy' r (rd s2); | 
| 13402 | 338 | in (name, (vs, (t, prf))) end | 
| 339 | end; | |
| 340 | ||
| 341 | val add_realizers_i = gen_add_realizers | |
| 342 | (fn _ => fn (name, (vs, t, prf)) => (name, (vs, (t, prf)))); | |
| 343 | val add_realizers = gen_add_realizers prep_realizer; | |
| 344 | ||
| 13714 | 345 | fun realizes_of thy vs t prop = | 
| 346 | let | |
| 347 | val thy' = add_syntax thy; | |
| 13732 | 348 |     val {realizes_eqns, typeof_eqns, defs, types, ...} =
 | 
| 13714 | 349 | ExtractionData.get thy'; | 
| 22717 | 350 | val procs = maps (rev o fst o snd) types; | 
| 16800 | 351 | val eqns = Net.merge (K false) (#net realizes_eqns, #net typeof_eqns); | 
| 17203 | 352 | val prop' = Pattern.rewrite_term thy' | 
| 13714 | 353 | (map (Logic.dest_equals o prop_of) defs) [] prop; | 
| 16458 | 354 | in freeze_thaw (condrew thy' eqns | 
| 37233 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 355 | (procs @ [typeof_proc [] vs, rlz_proc])) | 
| 13714 | 356 |       (Const ("realizes", fastype_of t --> propT --> propT) $ t $ prop')
 | 
| 357 | end; | |
| 358 | ||
| 37233 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 359 | fun abs_corr_shyps thy thm vs xs prf = | 
| 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 360 | let | 
| 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 361 | val S = Sign.defaultS thy; | 
| 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 362 | val ((atyp_map, constraints, _), prop') = | 
| 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 363 | Logic.unconstrainT (#shyps (rep_thm thm)) (prop_of thm); | 
| 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 364 | val atyps = fold_types (fold_atyps (insert (op =))) (prop_of thm) []; | 
| 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 365 | val Ts = map_filter (fn ((v, i), _) => if member (op =) vs v then | 
| 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 366 |         SOME (TVar (("'" ^ v, i), [])) else NONE)
 | 
| 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 367 | (rev (Term.add_vars prop' [])); | 
| 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 368 | val cs = maps (fn T => map (pair T) S) Ts; | 
| 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 369 | val constraints' = map Logic.mk_of_class cs; | 
| 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 370 | val cs' = rev (cs @ map (Logic.dest_of_class o snd) constraints); | 
| 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 371 | fun typ_map T = Type.strip_sorts | 
| 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 372 | (map_atyps (fn U => if member (op =) atyps U then atyp_map U else U) T); | 
| 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 373 | fun mk_hyp (T, c) = Hyp (Logic.mk_of_class (typ_map T, c)); | 
| 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 374 | val xs' = map (map_types typ_map) xs | 
| 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 375 | in | 
| 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 376 | prf |> | 
| 37310 | 377 | Same.commit (Proofterm.map_proof_same (map_types typ_map) typ_map mk_hyp) |> | 
| 378 | fold_rev Proofterm.implies_intr_proof' (map snd constraints) |> | |
| 379 | fold_rev Proofterm.forall_intr_proof' xs' |> | |
| 380 | fold_rev Proofterm.implies_intr_proof' constraints' | |
| 37233 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 381 | end; | 
| 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 382 | |
| 13402 | 383 | (** expanding theorems / definitions **) | 
| 384 | ||
| 33704 
6aeb8454efc1
add_expand_thm: explicit indication of is_def instead of fragile heuristic, tuned signature;
 wenzelm parents: 
33522diff
changeset | 385 | fun add_expand_thm is_def thm thy = | 
| 13402 | 386 | let | 
| 387 |     val {realizes_eqns, typeof_eqns, types, realizers,
 | |
| 388 | defs, expand, prep} = ExtractionData.get thy; | |
| 389 | ||
| 36744 
6e1f3d609a68
renamed Thm.get_name -> Thm.derivation_name and Thm.put_name -> Thm.name_derivation, to emphasize the true nature of these operations;
 wenzelm parents: 
36620diff
changeset | 390 | val name = Thm.derivation_name thm; | 
| 33704 
6aeb8454efc1
add_expand_thm: explicit indication of is_def instead of fragile heuristic, tuned signature;
 wenzelm parents: 
33522diff
changeset | 391 | val _ = name <> "" orelse error "add_expand_thm: unnamed theorem"; | 
| 13402 | 392 | in | 
| 33704 
6aeb8454efc1
add_expand_thm: explicit indication of is_def instead of fragile heuristic, tuned signature;
 wenzelm parents: 
33522diff
changeset | 393 | thy |> ExtractionData.put | 
| 
6aeb8454efc1
add_expand_thm: explicit indication of is_def instead of fragile heuristic, tuned signature;
 wenzelm parents: 
33522diff
changeset | 394 | (if is_def then | 
| 13402 | 395 |         {realizes_eqns = realizes_eqns,
 | 
| 37233 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 396 | typeof_eqns = add_rule ([], Logic.dest_equals (map_types | 
| 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 397 | Type.strip_sorts (prop_of (Drule.abs_def thm)))) typeof_eqns, | 
| 13402 | 398 | types = types, | 
| 22360 
26ead7ed4f4b
moved eq_thm etc. to structure Thm in Pure/more_thm.ML;
 wenzelm parents: 
21858diff
changeset | 399 | realizers = realizers, defs = insert Thm.eq_thm thm defs, | 
| 13402 | 400 | expand = expand, prep = prep} | 
| 401 | else | |
| 402 |         {realizes_eqns = realizes_eqns, typeof_eqns = typeof_eqns, types = types,
 | |
| 403 | realizers = realizers, defs = defs, | |
| 37233 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 404 | expand = insert (op =) name expand, prep = prep}) | 
| 13402 | 405 | end; | 
| 406 | ||
| 33704 
6aeb8454efc1
add_expand_thm: explicit indication of is_def instead of fragile heuristic, tuned signature;
 wenzelm parents: 
33522diff
changeset | 407 | fun extraction_expand is_def = | 
| 
6aeb8454efc1
add_expand_thm: explicit indication of is_def instead of fragile heuristic, tuned signature;
 wenzelm parents: 
33522diff
changeset | 408 | Thm.declaration_attribute (fn th => Context.mapping (add_expand_thm is_def th) I); | 
| 13402 | 409 | |
| 15801 | 410 | |
| 13732 | 411 | (** types with computational content **) | 
| 412 | ||
| 413 | fun add_types tys thy = | |
| 22717 | 414 | ExtractionData.map | 
| 415 |     (fn {realizes_eqns, typeof_eqns, types, realizers, defs, expand, prep} =>
 | |
| 13732 | 416 |       {realizes_eqns = realizes_eqns, typeof_eqns = typeof_eqns,
 | 
| 22717 | 417 | types = fold (AList.update (op =) o apfst (Sign.intern_type thy)) tys types, | 
| 418 | realizers = realizers, defs = defs, expand = expand, prep = prep}) | |
| 419 | thy; | |
| 13732 | 420 | |
| 13402 | 421 | |
| 15801 | 422 | (** Pure setup **) | 
| 423 | ||
| 26463 | 424 | val _ = Context.>> (Context.map_theory | 
| 18708 | 425 |   (add_types [("prop", ([], NONE))] #>
 | 
| 15801 | 426 | |
| 427 | add_typeof_eqns | |
| 428 | ["(typeof (PROP P)) == (Type (TYPE(Null))) ==> \ | |
| 429 |     \  (typeof (PROP Q)) == (Type (TYPE('Q))) ==>  \
 | |
| 430 |     \    (typeof (PROP P ==> PROP Q)) == (Type (TYPE('Q)))",
 | |
| 431 | ||
| 432 | "(typeof (PROP Q)) == (Type (TYPE(Null))) ==> \ | |
| 433 | \ (typeof (PROP P ==> PROP Q)) == (Type (TYPE(Null)))", | |
| 434 | ||
| 435 |       "(typeof (PROP P)) == (Type (TYPE('P))) ==>  \
 | |
| 436 |     \  (typeof (PROP Q)) == (Type (TYPE('Q))) ==>  \
 | |
| 437 |     \    (typeof (PROP P ==> PROP Q)) == (Type (TYPE('P => 'Q)))",
 | |
| 438 | ||
| 439 | "(%x. typeof (PROP P (x))) == (%x. Type (TYPE(Null))) ==> \ | |
| 440 | \ (typeof (!!x. PROP P (x))) == (Type (TYPE(Null)))", | |
| 441 | ||
| 442 |       "(%x. typeof (PROP P (x))) == (%x. Type (TYPE('P))) ==>  \
 | |
| 443 |     \    (typeof (!!x::'a. PROP P (x))) == (Type (TYPE('a => 'P)))",
 | |
| 444 | ||
| 445 |       "(%x. typeof (f (x))) == (%x. Type (TYPE('f))) ==>  \
 | |
| 18708 | 446 |     \    (typeof (f)) == (Type (TYPE('f)))"] #>
 | 
| 15801 | 447 | |
| 448 | add_realizes_eqns | |
| 449 | ["(typeof (PROP P)) == (Type (TYPE(Null))) ==> \ | |
| 450 | \ (realizes (r) (PROP P ==> PROP Q)) == \ | |
| 451 | \ (PROP realizes (Null) (PROP P) ==> PROP realizes (r) (PROP Q))", | |
| 452 | ||
| 453 |       "(typeof (PROP P)) == (Type (TYPE('P))) ==>  \
 | |
| 454 | \ (typeof (PROP Q)) == (Type (TYPE(Null))) ==> \ | |
| 455 | \ (realizes (r) (PROP P ==> PROP Q)) == \ | |
| 456 | \ (!!x::'P. PROP realizes (x) (PROP P) ==> PROP realizes (Null) (PROP Q))", | |
| 457 | ||
| 458 | "(realizes (r) (PROP P ==> PROP Q)) == \ | |
| 459 | \ (!!x. PROP realizes (x) (PROP P) ==> PROP realizes (r (x)) (PROP Q))", | |
| 460 | ||
| 461 | "(%x. typeof (PROP P (x))) == (%x. Type (TYPE(Null))) ==> \ | |
| 462 | \ (realizes (r) (!!x. PROP P (x))) == \ | |
| 463 | \ (!!x. PROP realizes (Null) (PROP P (x)))", | |
| 464 | ||
| 465 | "(realizes (r) (!!x. PROP P (x))) == \ | |
| 18708 | 466 | \ (!!x. PROP realizes (r (x)) (PROP P (x)))"] #> | 
| 15801 | 467 | |
| 33704 
6aeb8454efc1
add_expand_thm: explicit indication of is_def instead of fragile heuristic, tuned signature;
 wenzelm parents: 
33522diff
changeset | 468 | Attrib.setup (Binding.name "extraction_expand") (Scan.succeed (extraction_expand false)) | 
| 
6aeb8454efc1
add_expand_thm: explicit indication of is_def instead of fragile heuristic, tuned signature;
 wenzelm parents: 
33522diff
changeset | 469 | "specify theorems to be expanded during extraction" #> | 
| 
6aeb8454efc1
add_expand_thm: explicit indication of is_def instead of fragile heuristic, tuned signature;
 wenzelm parents: 
33522diff
changeset | 470 | Attrib.setup (Binding.name "extraction_expand_def") (Scan.succeed (extraction_expand true)) | 
| 
6aeb8454efc1
add_expand_thm: explicit indication of is_def instead of fragile heuristic, tuned signature;
 wenzelm parents: 
33522diff
changeset | 471 | "specify definitions to be expanded during extraction")); | 
| 15801 | 472 | |
| 473 | ||
| 13402 | 474 | (**** extract program ****) | 
| 475 | ||
| 476 | val dummyt = Const ("dummy", dummyT);
 | |
| 477 | ||
| 478 | fun extract thms thy = | |
| 479 | let | |
| 16458 | 480 | val thy' = add_syntax thy; | 
| 13402 | 481 |     val {realizes_eqns, typeof_eqns, types, realizers, defs, expand, prep} =
 | 
| 482 | ExtractionData.get thy; | |
| 22717 | 483 | val procs = maps (rev o fst o snd) types; | 
| 13732 | 484 | val rtypes = map fst types; | 
| 37233 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 485 | val typroc = typeof_proc []; | 
| 19466 | 486 | val prep = the_default (K I) prep thy' o ProofRewriteRules.elim_defs thy' false defs o | 
| 37233 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 487 |       Reconstruct.expand_proof thy' (map (rpair NONE) ("" :: expand));
 | 
| 16800 | 488 | val rrews = Net.merge (K false) (#net realizes_eqns, #net typeof_eqns); | 
| 13402 | 489 | |
| 490 | fun find_inst prop Ts ts vs = | |
| 491 | let | |
| 13732 | 492 | val rvs = relevant_vars rtypes prop; | 
| 13402 | 493 | val vars = vars_of prop; | 
| 494 | val n = Int.min (length vars, length ts); | |
| 495 | ||
| 33337 | 496 | fun add_args (Var ((a, i), _), t) (vs', tye) = | 
| 20664 | 497 | if member (op =) rvs a then | 
| 16458 | 498 | let val T = etype_of thy' vs Ts t | 
| 13402 | 499 | in if T = nullT then (vs', tye) | 
| 500 |                else (a :: vs', (("'" ^ a, i), T) :: tye)
 | |
| 501 | end | |
| 502 | else (vs', tye) | |
| 503 | ||
| 33957 | 504 | in fold_rev add_args (take n vars ~~ take n ts) ([], []) end; | 
| 13402 | 505 | |
| 37233 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 506 | fun mk_shyps tye = maps (fn (ixn, _) => | 
| 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 507 | Logic.mk_of_sort (TVar (ixn, []), Sign.defaultS thy)) tye; | 
| 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 508 | |
| 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 509 | fun mk_sprfs cs tye = maps (fn (_, T) => | 
| 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 510 | ProofRewriteRules.mk_of_sort_proof thy (map SOME cs) | 
| 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 511 | (T, Sign.defaultS thy)) tye; | 
| 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 512 | |
| 33038 | 513 | fun find (vs: string list) = Option.map snd o find_first (curry (eq_set (op =)) vs o fst); | 
| 28375 | 514 | fun find' (s: string) = map_filter (fn (s', x) => if s = s' then SOME x else NONE); | 
| 13402 | 515 | |
| 13732 | 516 | fun app_rlz_rews Ts vs t = strip_abs (length Ts) (freeze_thaw | 
| 16458 | 517 | (condrew thy' rrews (procs @ [typroc vs, rlz_proc])) (list_abs | 
| 13732 | 518 | (map (pair "x") (rev Ts), t))); | 
| 519 | ||
| 520 | fun realizes_null vs prop = app_rlz_rews [] vs | |
| 521 |       (Const ("realizes", nullT --> propT --> propT) $ nullt $ prop);
 | |
| 13402 | 522 | |
| 37233 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 523 | fun corr d defs vs ts Ts hs cs (PBound i) _ _ = (defs, PBound i) | 
| 13402 | 524 | |
| 37233 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 525 | | corr d defs vs ts Ts hs cs (Abst (s, SOME T, prf)) (Abst (_, _, prf')) t = | 
| 13402 | 526 | let val (defs', corr_prf) = corr d defs vs [] (T :: Ts) | 
| 37310 | 527 | (dummyt :: hs) cs prf (Proofterm.incr_pboundvars 1 0 prf') | 
| 15531 | 528 | (case t of SOME (Abs (_, _, u)) => SOME u | _ => NONE) | 
| 529 | in (defs', Abst (s, SOME T, corr_prf)) end | |
| 13402 | 530 | |
| 37233 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 531 | | corr d defs vs ts Ts hs cs (AbsP (s, SOME prop, prf)) (AbsP (_, _, prf')) t = | 
| 13402 | 532 | let | 
| 16458 | 533 | val T = etype_of thy' vs Ts prop; | 
| 42407 
5b9dd52f5dca
prefer internal types, via Simple_Syntax.read_typ;
 wenzelm parents: 
42406diff
changeset | 534 | val u = if T = nullT then | 
| 15531 | 535 | (case t of SOME u => SOME (incr_boundvars 1 u) | NONE => NONE) | 
| 536 | else (case t of SOME (Abs (_, _, u)) => SOME u | _ => NONE); | |
| 37310 | 537 | val (defs', corr_prf) = | 
| 538 | corr d defs vs [] (T :: Ts) (prop :: hs) | |
| 539 | (prop :: cs) (Proofterm.incr_pboundvars 0 1 prf) | |
| 540 | (Proofterm.incr_pboundvars 0 1 prf') u; | |
| 13402 | 541 |             val rlz = Const ("realizes", T --> propT --> propT)
 | 
| 542 | in (defs', | |
| 13732 | 543 |             if T = nullT then AbsP ("R",
 | 
| 15531 | 544 | SOME (app_rlz_rews Ts vs (rlz $ nullt $ prop)), | 
| 37310 | 545 | Proofterm.prf_subst_bounds [nullt] corr_prf) | 
| 15531 | 546 |             else Abst (s, SOME T, AbsP ("R",
 | 
| 547 | SOME (app_rlz_rews (T :: Ts) vs | |
| 13732 | 548 | (rlz $ Bound 0 $ incr_boundvars 1 prop)), corr_prf))) | 
| 13402 | 549 | end | 
| 550 | ||
| 37233 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 551 | | corr d defs vs ts Ts hs cs (prf % SOME t) (prf' % _) t' = | 
| 13732 | 552 | let | 
| 553 | val (Us, T) = strip_type (fastype_of1 (Ts, t)); | |
| 37233 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 554 | val (defs', corr_prf) = corr d defs vs (t :: ts) Ts hs cs prf prf' | 
| 20664 | 555 | (if member (op =) rtypes (tname_of T) then t' | 
| 15531 | 556 | else (case t' of SOME (u $ _) => SOME u | _ => NONE)); | 
| 20664 | 557 | val u = if not (member (op =) rtypes (tname_of T)) then t else | 
| 13732 | 558 | let | 
| 16458 | 559 | val eT = etype_of thy' vs Ts t; | 
| 13732 | 560 | val (r, Us') = if eT = nullT then (nullt, Us) else | 
| 561 | (Bound (length Us), eT :: Us); | |
| 562 | val u = list_comb (incr_boundvars (length Us') t, | |
| 563 | map Bound (length Us - 1 downto 0)); | |
| 17271 | 564 | val u' = (case AList.lookup (op =) types (tname_of T) of | 
| 15531 | 565 | SOME ((_, SOME f)) => f r eT u T | 
| 13732 | 566 |                   | _ => Const ("realizes", eT --> T --> T) $ r $ u)
 | 
| 567 | in app_rlz_rews Ts vs (list_abs (map (pair "x") Us', u')) end | |
| 15531 | 568 | in (defs', corr_prf % SOME u) end | 
| 13402 | 569 | |
| 37233 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 570 | | corr d defs vs ts Ts hs cs (prf1 %% prf2) (prf1' %% prf2') t = | 
| 13402 | 571 | let | 
| 572 | val prop = Reconstruct.prop_of' hs prf2'; | |
| 16458 | 573 | val T = etype_of thy' vs Ts prop; | 
| 15531 | 574 | val (defs1, f, u) = if T = nullT then (defs, t, NONE) else | 
| 13402 | 575 | (case t of | 
| 15531 | 576 | SOME (f $ u) => (defs, SOME f, SOME u) | 
| 13402 | 577 | | _ => | 
| 578 | let val (defs1, u) = extr d defs vs [] Ts hs prf2' | |
| 15531 | 579 | in (defs1, NONE, SOME u) end) | 
| 37233 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 580 | val (defs2, corr_prf1) = corr d defs1 vs [] Ts hs cs prf1 prf1' f; | 
| 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 581 | val (defs3, corr_prf2) = corr d defs2 vs [] Ts hs cs prf2 prf2' u; | 
| 13402 | 582 | in | 
| 583 | if T = nullT then (defs3, corr_prf1 %% corr_prf2) else | |
| 584 | (defs3, corr_prf1 % u %% corr_prf2) | |
| 585 | end | |
| 586 | ||
| 37233 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 587 | | corr d defs vs ts Ts hs cs (prf0 as PThm (_, ((name, prop, SOME Ts'), body))) _ _ = | 
| 13402 | 588 | let | 
| 37310 | 589 | val prf = Proofterm.join_proof body; | 
| 13402 | 590 | val (vs', tye) = find_inst prop Ts ts vs; | 
| 37233 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 591 | val shyps = mk_shyps tye; | 
| 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 592 | val sprfs = mk_sprfs cs tye; | 
| 36042 
85efdadee8ae
switched PThm/PAxm etc. to use canonical order of type variables (term variables unchanged)
 krauss parents: 
35985diff
changeset | 593 | val tye' = (map fst (Term.add_tvars prop [] |> rev) ~~ Ts') @ tye; | 
| 16458 | 594 | val T = etype_of thy' vs' [] prop; | 
| 13402 | 595 | val defs' = if T = nullT then defs | 
| 596 | else fst (extr d defs vs ts Ts hs prf0) | |
| 597 | in | |
| 13609 
73c3915553b4
Added check for axioms with "realizes Null A = A".
 berghofe parents: 
13417diff
changeset | 598 | if T = nullT andalso realizes_null vs' prop aconv prop then (defs, prf0) | 
| 17412 | 599 | else case Symtab.lookup realizers name of | 
| 15531 | 600 | NONE => (case find vs' (find' name defs') of | 
| 601 | NONE => | |
| 13402 | 602 | let | 
| 21858 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 wenzelm parents: 
21646diff
changeset | 603 | val _ = T = nullT orelse error "corr: internal error"; | 
| 13402 | 604 |                     val _ = msg d ("Building correctness proof for " ^ quote name ^
 | 
| 605 | (if null vs' then "" | |
| 606 | else " (relevant variables: " ^ commas_quote vs' ^ ")")); | |
| 16458 | 607 | val prf' = prep (Reconstruct.reconstruct_proof thy' prop prf); | 
| 37233 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 608 | val (defs'', corr_prf0) = corr (d + 1) defs' vs' [] [] [] | 
| 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 609 | (rev shyps) prf' prf' NONE; | 
| 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 610 | val corr_prf = mkabsp shyps corr_prf0; | 
| 13732 | 611 | val corr_prop = Reconstruct.prop_of corr_prf; | 
| 37233 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 612 | val corr_prf' = | 
| 37310 | 613 | Proofterm.proof_combP (Proofterm.proof_combt | 
| 28805 | 614 | (PThm (serial (), | 
| 36042 
85efdadee8ae
switched PThm/PAxm etc. to use canonical order of type variables (term variables unchanged)
 krauss parents: 
35985diff
changeset | 615 | ((corr_name name vs', corr_prop, SOME (map TVar (Term.add_tvars corr_prop [] |> rev))), | 
| 37310 | 616 | Future.value (Proofterm.approximate_proof_body corr_prf))), | 
| 617 | vfs_of corr_prop), | |
| 37233 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 618 | map PBound (length shyps - 1 downto 0)) |> | 
| 37310 | 619 | fold_rev Proofterm.forall_intr_proof' | 
| 620 | (map (get_var_type corr_prop) (vfs_of prop)) |> | |
| 37233 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 621 | mkabsp shyps | 
| 13402 | 622 | in | 
| 13732 | 623 | ((name, (vs', ((nullt, nullt), (corr_prf, corr_prf')))) :: defs'', | 
| 37310 | 624 | Proofterm.proof_combP (prf_subst_TVars tye' corr_prf', sprfs)) | 
| 13402 | 625 | end | 
| 37310 | 626 | | SOME (_, (_, prf')) => | 
| 627 | (defs', Proofterm.proof_combP (prf_subst_TVars tye' prf', sprfs))) | |
| 15531 | 628 | | SOME rs => (case find vs' rs of | 
| 37310 | 629 | SOME (_, prf') => (defs', Proofterm.proof_combP (prf_subst_TVars tye' prf', sprfs)) | 
| 15531 | 630 |               | NONE => error ("corr: no realizer for instance of theorem " ^
 | 
| 26939 
1035c89b4c02
moved global pretty/string_of functions from Sign to Syntax;
 wenzelm parents: 
26653diff
changeset | 631 | quote name ^ ":\n" ^ Syntax.string_of_term_global thy' (Envir.beta_norm | 
| 37310 | 632 | (Reconstruct.prop_of (Proofterm.proof_combt (prf0, ts)))))) | 
| 13402 | 633 | end | 
| 634 | ||
| 37233 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 635 | | corr d defs vs ts Ts hs cs (prf0 as PAxm (s, prop, SOME Ts')) _ _ = | 
| 13402 | 636 | let | 
| 637 | val (vs', tye) = find_inst prop Ts ts vs; | |
| 36042 
85efdadee8ae
switched PThm/PAxm etc. to use canonical order of type variables (term variables unchanged)
 krauss parents: 
35985diff
changeset | 638 | val tye' = (map fst (Term.add_tvars prop [] |> rev) ~~ Ts') @ tye | 
| 13402 | 639 | in | 
| 16458 | 640 | if etype_of thy' vs' [] prop = nullT andalso | 
| 13609 
73c3915553b4
Added check for axioms with "realizes Null A = A".
 berghofe parents: 
13417diff
changeset | 641 | realizes_null vs' prop aconv prop then (defs, prf0) | 
| 18956 | 642 | else case find vs' (Symtab.lookup_list realizers s) of | 
| 37233 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 643 | SOME (_, prf) => (defs, | 
| 37310 | 644 | Proofterm.proof_combP (prf_subst_TVars tye' prf, mk_sprfs cs tye)) | 
| 15531 | 645 |             | NONE => error ("corr: no realizer for instance of axiom " ^
 | 
| 26939 
1035c89b4c02
moved global pretty/string_of functions from Sign to Syntax;
 wenzelm parents: 
26653diff
changeset | 646 | quote s ^ ":\n" ^ Syntax.string_of_term_global thy' (Envir.beta_norm | 
| 37310 | 647 | (Reconstruct.prop_of (Proofterm.proof_combt (prf0, ts))))) | 
| 13402 | 648 | end | 
| 649 | ||
| 37233 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 650 | | corr d defs vs ts Ts hs _ _ _ _ = error "corr: bad proof" | 
| 13402 | 651 | |
| 652 | and extr d defs vs ts Ts hs (PBound i) = (defs, Bound i) | |
| 653 | ||
| 15531 | 654 | | extr d defs vs ts Ts hs (Abst (s, SOME T, prf)) = | 
| 13402 | 655 | let val (defs', t) = extr d defs vs [] | 
| 37310 | 656 | (T :: Ts) (dummyt :: hs) (Proofterm.incr_pboundvars 1 0 prf) | 
| 13402 | 657 | in (defs', Abs (s, T, t)) end | 
| 658 | ||
| 15531 | 659 | | extr d defs vs ts Ts hs (AbsP (s, SOME t, prf)) = | 
| 13402 | 660 | let | 
| 16458 | 661 | val T = etype_of thy' vs Ts t; | 
| 37310 | 662 | val (defs', t) = | 
| 663 | extr d defs vs [] (T :: Ts) (t :: hs) (Proofterm.incr_pboundvars 0 1 prf) | |
| 13402 | 664 | in (defs', | 
| 665 | if T = nullT then subst_bound (nullt, t) else Abs (s, T, t)) | |
| 666 | end | |
| 667 | ||
| 15531 | 668 | | extr d defs vs ts Ts hs (prf % SOME t) = | 
| 13402 | 669 | let val (defs', u) = extr d defs vs (t :: ts) Ts hs prf | 
| 13732 | 670 | in (defs', | 
| 20664 | 671 | if member (op =) rtypes (tname_of (body_type (fastype_of1 (Ts, t)))) then u | 
| 13732 | 672 | else u $ t) | 
| 673 | end | |
| 13402 | 674 | |
| 675 | | extr d defs vs ts Ts hs (prf1 %% prf2) = | |
| 676 | let | |
| 677 | val (defs', f) = extr d defs vs [] Ts hs prf1; | |
| 678 | val prop = Reconstruct.prop_of' hs prf2; | |
| 16458 | 679 | val T = etype_of thy' vs Ts prop | 
| 13402 | 680 | in | 
| 681 | if T = nullT then (defs', f) else | |
| 682 | let val (defs'', t) = extr d defs' vs [] Ts hs prf2 | |
| 683 | in (defs'', f $ t) end | |
| 684 | end | |
| 685 | ||
| 28805 | 686 | | extr d defs vs ts Ts hs (prf0 as PThm (_, ((s, prop, SOME Ts'), body))) = | 
| 13402 | 687 | let | 
| 37310 | 688 | val prf = Proofterm.join_proof body; | 
| 13402 | 689 | val (vs', tye) = find_inst prop Ts ts vs; | 
| 37233 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 690 | val shyps = mk_shyps tye; | 
| 36042 
85efdadee8ae
switched PThm/PAxm etc. to use canonical order of type variables (term variables unchanged)
 krauss parents: 
35985diff
changeset | 691 | val tye' = (map fst (Term.add_tvars prop [] |> rev) ~~ Ts') @ tye | 
| 13402 | 692 | in | 
| 17412 | 693 | case Symtab.lookup realizers s of | 
| 15531 | 694 | NONE => (case find vs' (find' s defs) of | 
| 695 | NONE => | |
| 13402 | 696 | let | 
| 697 |                     val _ = msg d ("Extracting " ^ quote s ^
 | |
| 698 | (if null vs' then "" | |
| 699 | else " (relevant variables: " ^ commas_quote vs' ^ ")")); | |
| 16458 | 700 | val prf' = prep (Reconstruct.reconstruct_proof thy' prop prf); | 
| 13402 | 701 | val (defs', t) = extr (d + 1) defs vs' [] [] [] prf'; | 
| 37233 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 702 | val (defs'', corr_prf) = corr (d + 1) defs' vs' [] [] [] | 
| 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 703 | (rev shyps) prf' prf' (SOME t); | 
| 13402 | 704 | |
| 705 | val nt = Envir.beta_norm t; | |
| 20664 | 706 | val args = filter_out (fn v => member (op =) rtypes | 
| 707 | (tname_of (body_type (fastype_of v)))) (vfs_of prop); | |
| 33317 | 708 | val args' = filter (fn v => Logic.occs (v, nt)) args; | 
| 37233 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 709 | val t' = mkabs args' nt; | 
| 13402 | 710 | val T = fastype_of t'; | 
| 13732 | 711 | val cname = extr_name s vs'; | 
| 13402 | 712 | val c = Const (cname, T); | 
| 37233 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 713 | val u = mkabs args (list_comb (c, args')); | 
| 13402 | 714 | val eqn = Logic.mk_equals (c, t'); | 
| 715 | val rlz = | |
| 716 |                       Const ("realizes", fastype_of nt --> propT --> propT);
 | |
| 13732 | 717 | val lhs = app_rlz_rews [] vs' (rlz $ nt $ prop); | 
| 718 | val rhs = app_rlz_rews [] vs' (rlz $ list_comb (c, args') $ prop); | |
| 719 | val f = app_rlz_rews [] vs' | |
| 720 |                       (Abs ("x", T, rlz $ list_comb (Bound 0, args') $ prop));
 | |
| 13402 | 721 | |
| 37233 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 722 | val corr_prf' = mkabsp shyps | 
| 37310 | 723 | (chtype [] Proofterm.equal_elim_axm %> lhs %> rhs %% | 
| 724 | (chtype [propT] Proofterm.symmetric_axm %> rhs %> lhs %% | |
| 725 | (chtype [T, propT] Proofterm.combination_axm %> f %> f %> c %> t' %% | |
| 726 | (chtype [T --> propT] Proofterm.reflexive_axm %> f) %% | |
| 13732 | 727 | PAxm (cname ^ "_def", eqn, | 
| 37233 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 728 | SOME (map TVar (Term.add_tvars eqn [] |> rev))))) %% corr_prf); | 
| 13732 | 729 | val corr_prop = Reconstruct.prop_of corr_prf'; | 
| 37233 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 730 | val corr_prf'' = | 
| 37310 | 731 | Proofterm.proof_combP (Proofterm.proof_combt | 
| 28805 | 732 | (PThm (serial (), | 
| 36042 
85efdadee8ae
switched PThm/PAxm etc. to use canonical order of type variables (term variables unchanged)
 krauss parents: 
35985diff
changeset | 733 | ((corr_name s vs', corr_prop, SOME (map TVar (Term.add_tvars corr_prop [] |> rev))), | 
| 37310 | 734 | Future.value (Proofterm.approximate_proof_body corr_prf'))), | 
| 735 | vfs_of corr_prop), | |
| 37233 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 736 | map PBound (length shyps - 1 downto 0)) |> | 
| 37310 | 737 | fold_rev Proofterm.forall_intr_proof' | 
| 738 | (map (get_var_type corr_prop) (vfs_of prop)) |> | |
| 37233 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 739 | mkabsp shyps | 
| 13402 | 740 | in | 
| 13732 | 741 | ((s, (vs', ((t', u), (corr_prf', corr_prf'')))) :: defs'', | 
| 13402 | 742 | subst_TVars tye' u) | 
| 743 | end | |
| 15531 | 744 | | SOME ((_, u), _) => (defs, subst_TVars tye' u)) | 
| 745 | | SOME rs => (case find vs' rs of | |
| 746 | SOME (t, _) => (defs, subst_TVars tye' t) | |
| 747 |               | NONE => error ("extr: no realizer for instance of theorem " ^
 | |
| 26939 
1035c89b4c02
moved global pretty/string_of functions from Sign to Syntax;
 wenzelm parents: 
26653diff
changeset | 748 | quote s ^ ":\n" ^ Syntax.string_of_term_global thy' (Envir.beta_norm | 
| 37310 | 749 | (Reconstruct.prop_of (Proofterm.proof_combt (prf0, ts)))))) | 
| 13402 | 750 | end | 
| 751 | ||
| 15531 | 752 | | extr d defs vs ts Ts hs (prf0 as PAxm (s, prop, SOME Ts')) = | 
| 13402 | 753 | let | 
| 754 | val (vs', tye) = find_inst prop Ts ts vs; | |
| 36042 
85efdadee8ae
switched PThm/PAxm etc. to use canonical order of type variables (term variables unchanged)
 krauss parents: 
35985diff
changeset | 755 | val tye' = (map fst (Term.add_tvars prop [] |> rev) ~~ Ts') @ tye | 
| 13402 | 756 | in | 
| 18956 | 757 | case find vs' (Symtab.lookup_list realizers s) of | 
| 15531 | 758 | SOME (t, _) => (defs, subst_TVars tye' t) | 
| 759 |             | NONE => error ("extr: no realizer for instance of axiom " ^
 | |
| 26939 
1035c89b4c02
moved global pretty/string_of functions from Sign to Syntax;
 wenzelm parents: 
26653diff
changeset | 760 | quote s ^ ":\n" ^ Syntax.string_of_term_global thy' (Envir.beta_norm | 
| 37310 | 761 | (Reconstruct.prop_of (Proofterm.proof_combt (prf0, ts))))) | 
| 13402 | 762 | end | 
| 763 | ||
| 764 | | extr d defs vs ts Ts hs _ = error "extr: bad proof"; | |
| 765 | ||
| 13732 | 766 | fun prep_thm (thm, vs) = | 
| 13402 | 767 | let | 
| 26626 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
26481diff
changeset | 768 | val thy = Thm.theory_of_thm thm; | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
26481diff
changeset | 769 | val prop = Thm.prop_of thm; | 
| 28814 | 770 | val prf = Thm.proof_of thm; | 
| 36744 
6e1f3d609a68
renamed Thm.get_name -> Thm.derivation_name and Thm.put_name -> Thm.name_derivation, to emphasize the true nature of these operations;
 wenzelm parents: 
36620diff
changeset | 771 | val name = Thm.derivation_name thm; | 
| 21858 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 wenzelm parents: 
21646diff
changeset | 772 | val _ = name <> "" orelse error "extraction: unnamed theorem"; | 
| 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 wenzelm parents: 
21646diff
changeset | 773 |         val _ = etype_of thy' vs [] prop <> nullT orelse error ("theorem " ^
 | 
| 13402 | 774 | quote name ^ " has no computational content") | 
| 22596 | 775 | in (Reconstruct.reconstruct_proof thy prop prf, vs) end; | 
| 13402 | 776 | |
| 33245 | 777 | val defs = | 
| 778 | fold (fn (prf, vs) => fn defs => fst (extr 0 defs vs [] [] [] prf)) | |
| 779 | (map prep_thm thms) []; | |
| 13402 | 780 | |
| 16149 | 781 | fun add_def (s, (vs, ((t, u), (prf, _)))) thy = | 
| 16458 | 782 | (case Sign.const_type thy (extr_name s vs) of | 
| 15531 | 783 | NONE => | 
| 13732 | 784 | let | 
| 785 | val corr_prop = Reconstruct.prop_of prf; | |
| 33832 | 786 | val ft = Type.legacy_freeze t; | 
| 787 | val fu = Type.legacy_freeze u; | |
| 22750 | 788 | val (def_thms, thy') = if t = nullt then ([], thy) else | 
| 789 | thy | |
| 30435 
e62d6ecab6ad
explicit Binding.qualified_name -- prevents implicitly qualified bstring;
 wenzelm parents: 
30364diff
changeset | 790 | |> Sign.add_consts_i [(Binding.qualified_name (extr_name s vs), fastype_of ft, NoSyn)] | 
| 39557 
fe5722fce758
renamed structure PureThy to Pure_Thy and moved most content to Global_Theory, to emphasize that this is global-only;
 wenzelm parents: 
39288diff
changeset | 791 | |> Global_Theory.add_defs false [((Binding.qualified_name (extr_name s vs ^ "_def"), | 
| 22750 | 792 | Logic.mk_equals (head_of (strip_abs_body fu), ft)), [])] | 
| 13732 | 793 | in | 
| 22750 | 794 | thy' | 
| 39557 
fe5722fce758
renamed structure PureThy to Pure_Thy and moved most content to Global_Theory, to emphasize that this is global-only;
 wenzelm parents: 
39288diff
changeset | 795 | |> Global_Theory.store_thm (Binding.qualified_name (corr_name s vs), | 
| 37233 
b78f31ca4675
Adapted to new format of proof terms containing explicit proofs of class membership.
 berghofe parents: 
36953diff
changeset | 796 | Thm.varifyT_global (funpow (length (vars_of corr_prop)) | 
| 35985 
0bbf0d2348f9
moved Drule.forall_intr_frees to Thm.forall_intr_frees (in more_thm.ML, which is loaded before pure_thy.ML);
 wenzelm parents: 
35845diff
changeset | 797 | (Thm.forall_elim_var 0) (Thm.forall_intr_frees | 
| 22750 | 798 | (ProofChecker.thm_of_proof thy' | 
| 26481 | 799 | (fst (Proofterm.freeze_thaw_prf prf)))))) | 
| 22750 | 800 | |> snd | 
| 28370 | 801 | |> fold Code.add_default_eqn def_thms | 
| 13732 | 802 | end | 
| 15531 | 803 | | SOME _ => thy); | 
| 13402 | 804 | |
| 16149 | 805 | in | 
| 806 | thy | |
| 30435 
e62d6ecab6ad
explicit Binding.qualified_name -- prevents implicitly qualified bstring;
 wenzelm parents: 
30364diff
changeset | 807 | |> Sign.root_path | 
| 16149 | 808 | |> fold_rev add_def defs | 
| 22796 | 809 | |> Sign.restore_naming thy | 
| 13402 | 810 | end; | 
| 811 | ||
| 812 | ||
| 813 | (**** interface ****) | |
| 814 | ||
| 36950 | 815 | val parse_vars = Scan.optional (Parse.$$$ "(" |-- Parse.list1 Parse.name --| Parse.$$$ ")") [];
 | 
| 13732 | 816 | |
| 24867 | 817 | val _ = | 
| 36953 
2af1ad9aa1a3
renamed structure OuterSyntax to Outer_Syntax, keeping the old name as alias for some time;
 wenzelm parents: 
36950diff
changeset | 818 | Outer_Syntax.command "realizers" | 
| 13402 | 819 | "specify realizers for primitive axioms / theorems, together with correctness proof" | 
| 36950 | 820 | Keyword.thy_decl | 
| 821 | (Scan.repeat1 (Parse.xname -- parse_vars --| Parse.$$$ ":" -- Parse.string -- Parse.string) >> | |
| 13402 | 822 | (fn xs => Toplevel.theory (fn thy => add_realizers | 
| 39557 
fe5722fce758
renamed structure PureThy to Pure_Thy and moved most content to Global_Theory, to emphasize that this is global-only;
 wenzelm parents: 
39288diff
changeset | 823 | (map (fn (((a, vs), s1), s2) => (Global_Theory.get_thm thy a, (vs, s1, s2))) xs) thy))); | 
| 13402 | 824 | |
| 24867 | 825 | val _ = | 
| 36953 
2af1ad9aa1a3
renamed structure OuterSyntax to Outer_Syntax, keeping the old name as alias for some time;
 wenzelm parents: 
36950diff
changeset | 826 | Outer_Syntax.command "realizability" | 
| 36950 | 827 | "add equations characterizing realizability" Keyword.thy_decl | 
| 828 | (Scan.repeat1 Parse.string >> (Toplevel.theory o add_realizes_eqns)); | |
| 13402 | 829 | |
| 24867 | 830 | val _ = | 
| 36953 
2af1ad9aa1a3
renamed structure OuterSyntax to Outer_Syntax, keeping the old name as alias for some time;
 wenzelm parents: 
36950diff
changeset | 831 | Outer_Syntax.command "extract_type" | 
| 36950 | 832 | "add equations characterizing type of extracted program" Keyword.thy_decl | 
| 833 | (Scan.repeat1 Parse.string >> (Toplevel.theory o add_typeof_eqns)); | |
| 13402 | 834 | |
| 24867 | 835 | val _ = | 
| 36953 
2af1ad9aa1a3
renamed structure OuterSyntax to Outer_Syntax, keeping the old name as alias for some time;
 wenzelm parents: 
36950diff
changeset | 836 | Outer_Syntax.command "extract" "extract terms from proofs" Keyword.thy_decl | 
| 36950 | 837 | (Scan.repeat1 (Parse.xname -- parse_vars) >> (fn xs => Toplevel.theory (fn thy => | 
| 39557 
fe5722fce758
renamed structure PureThy to Pure_Thy and moved most content to Global_Theory, to emphasize that this is global-only;
 wenzelm parents: 
39288diff
changeset | 838 | extract (map (apfst (Global_Theory.get_thm thy)) xs) thy))); | 
| 13402 | 839 | |
| 16458 | 840 | val etype_of = etype_of o add_syntax; | 
| 13714 | 841 | |
| 13402 | 842 | end; |