| author | wenzelm | 
| Thu, 27 Mar 2008 15:32:19 +0100 | |
| changeset 26436 | dfd6947ab5c2 | 
| parent 26435 | bdce320cd426 | 
| child 26463 | 9283b4185fdf | 
| permissions | -rw-r--r-- | 
| 13402 | 1 | (* Title: Pure/Proof/extraction.ML | 
| 2 | ID: $Id$ | |
| 3 | Author: Stefan Berghofer, TU Muenchen | |
| 4 | ||
| 5 | Extraction of programs from proofs. | |
| 6 | *) | |
| 7 | ||
| 8 | signature EXTRACTION = | |
| 9 | sig | |
| 16458 | 10 | val set_preprocessor : (theory -> Proofterm.proof -> Proofterm.proof) -> theory -> theory | 
| 13402 | 11 | val add_realizes_eqns_i : ((term * term) list * (term * term)) list -> theory -> theory | 
| 12 | val add_realizes_eqns : string list -> theory -> theory | |
| 13 | val add_typeof_eqns_i : ((term * term) list * (term * term)) list -> theory -> theory | |
| 14 | val add_typeof_eqns : string list -> theory -> theory | |
| 15 | val add_realizers_i : (string * (string list * term * Proofterm.proof)) list | |
| 16 | -> theory -> theory | |
| 17 | val add_realizers : (thm * (string list * string * string)) list | |
| 18 | -> theory -> theory | |
| 19 | val add_expand_thms : thm list -> theory -> theory | |
| 13732 | 20 | val add_types : (xstring * ((term -> term option) list * | 
| 21 | (term -> typ -> term -> typ -> term) option)) list -> theory -> theory | |
| 22 | val extract : (thm * string list) list -> theory -> theory | |
| 13402 | 23 | val nullT : typ | 
| 24 | val nullt : term | |
| 13714 | 25 | val mk_typ : typ -> term | 
| 26 | val etype_of : theory -> string list -> typ list -> term -> typ | |
| 27 | val realizes_of: theory -> string list -> term -> term -> term | |
| 13402 | 28 | end; | 
| 29 | ||
| 30 | structure Extraction : EXTRACTION = | |
| 31 | struct | |
| 32 | ||
| 33 | open Proofterm; | |
| 34 | ||
| 35 | (**** tools ****) | |
| 36 | ||
| 37 | fun add_syntax thy = | |
| 38 | thy | |
| 39 | |> Theory.copy | |
| 24712 
64ed05609568
proper Sign operations instead of Theory aliases;
 wenzelm parents: 
24707diff
changeset | 40 | |> Sign.root_path | 
| 22796 | 41 |   |> Sign.add_types [("Type", 0, NoSyn), ("Null", 0, NoSyn)]
 | 
| 42 | |> Sign.add_consts | |
| 14854 | 43 |       [("typeof", "'b::{} => Type", NoSyn),
 | 
| 44 |        ("Type", "'a::{} itself => Type", NoSyn),
 | |
| 13402 | 45 |        ("Null", "Null", NoSyn),
 | 
| 14854 | 46 |        ("realizes", "'a::{} => 'b::{} => 'b", NoSyn)];
 | 
| 13402 | 47 | |
| 48 | val nullT = Type ("Null", []);
 | |
| 49 | val nullt = Const ("Null", nullT);
 | |
| 50 | ||
| 51 | fun mk_typ T = | |
| 19391 | 52 |   Const ("Type", Term.itselfT T --> Type ("Type", [])) $ Logic.mk_type T;
 | 
| 13402 | 53 | |
| 54 | fun typeof_proc defaultS vs (Const ("typeof", _) $ u) =
 | |
| 15531 | 55 | SOME (mk_typ (case strip_comb u of | 
| 13402 | 56 | (Var ((a, i), _), _) => | 
| 20664 | 57 |             if member (op =) vs a then TFree ("'" ^ a ^ ":" ^ string_of_int i, defaultS)
 | 
| 13402 | 58 | else nullT | 
| 59 | | (Free (a, _), _) => | |
| 20664 | 60 |             if member (op =) vs a then TFree ("'" ^ a, defaultS) else nullT
 | 
| 13402 | 61 | | _ => nullT)) | 
| 15531 | 62 | | typeof_proc _ _ _ = NONE; | 
| 13402 | 63 | |
| 15531 | 64 | fun rlz_proc (Const ("realizes", Type (_, [Type ("Null", []), _])) $ r $ t) = SOME t
 | 
| 13732 | 65 |   | rlz_proc (Const ("realizes", Type (_, [T, _])) $ r $ t) =
 | 
| 66 | (case strip_comb t of | |
| 15531 | 67 | (Var (ixn, U), ts) => SOME (list_comb (Var (ixn, T --> U), r :: ts)) | 
| 68 | | (Free (s, U), ts) => SOME (list_comb (Free (s, T --> U), r :: ts)) | |
| 69 | | _ => NONE) | |
| 70 | | rlz_proc _ = NONE; | |
| 13402 | 71 | |
| 72 | val unpack_ixn = apfst implode o apsnd (fst o read_int o tl) o | |
| 73 | take_prefix (not o equal ":") o explode; | |
| 74 | ||
| 75 | type rules = | |
| 76 |   {next: int, rs: ((term * term) list * (term * term)) list,
 | |
| 77 | net: (int * ((term * term) list * (term * term))) Net.net}; | |
| 78 | ||
| 79 | val empty_rules : rules = {next = 0, rs = [], net = Net.empty};
 | |
| 80 | ||
| 81 | fun add_rule (r as (_, (lhs, _)), {next, rs, net} : rules) =
 | |
| 16800 | 82 |   {next = next - 1, rs = r :: rs, net = Net.insert_term (K false)
 | 
| 18956 | 83 | (Envir.eta_contract lhs, (next, r)) net}; | 
| 13402 | 84 | |
| 13417 
12cc77f90811
Tuned type constraint of function merge_rules to make smlnj happy.
 berghofe parents: 
13402diff
changeset | 85 | fun merge_rules | 
| 
12cc77f90811
Tuned type constraint of function merge_rules to make smlnj happy.
 berghofe parents: 
13402diff
changeset | 86 |   ({next, rs = rs1, net} : rules) ({next = next2, rs = rs2, ...} : rules) =
 | 
| 23178 | 87 |   List.foldr add_rule {next = next, rs = rs1, net = net} (subtract (op =) rs1 rs2);
 | 
| 13402 | 88 | |
| 16458 | 89 | fun condrew thy rules procs = | 
| 13402 | 90 | let | 
| 91 | fun rew tm = | |
| 17203 | 92 | 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 | 93 | and condrew' tm = | 
| 13402 | 94 | let | 
| 15399 
683d83051d6a
Added term cache to function condrew in order to speed up rewriting.
 berghofe parents: 
14981diff
changeset | 95 | val cache = ref ([] : (term * term) list); | 
| 17232 | 96 | fun lookup f x = (case AList.lookup (op =) (!cache) x of | 
| 15531 | 97 | NONE => | 
| 15399 
683d83051d6a
Added term cache to function condrew in order to speed up rewriting.
 berghofe parents: 
14981diff
changeset | 98 | let val y = f x | 
| 
683d83051d6a
Added term cache to function condrew in order to speed up rewriting.
 berghofe parents: 
14981diff
changeset | 99 | in (cache := (x, y) :: !cache; y) end | 
| 15531 | 100 | | SOME y => y); | 
| 15399 
683d83051d6a
Added term cache to function condrew in order to speed up rewriting.
 berghofe parents: 
14981diff
changeset | 101 | in | 
| 
683d83051d6a
Added term cache to function condrew in order to speed up rewriting.
 berghofe parents: 
14981diff
changeset | 102 | get_first (fn (_, (prems, (tm1, tm2))) => | 
| 
683d83051d6a
Added term cache to function condrew in order to speed up rewriting.
 berghofe parents: 
14981diff
changeset | 103 | let | 
| 19466 | 104 | 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 | 105 | val inc = Logic.incr_indexes ([], maxidx_of_term tm + 1); | 
| 18184 | 106 | val env as (Tenv, tenv) = Pattern.match thy (inc tm1, tm) (Vartab.empty, Vartab.empty); | 
| 15798 
016f3be5a5ec
Adapted to new interface of instantiation and unification / matching functions.
 berghofe parents: 
15574diff
changeset | 107 | val prems' = map (pairself (Envir.subst_vars env o inc o ren)) prems; | 
| 15399 
683d83051d6a
Added term cache to function condrew in order to speed up rewriting.
 berghofe parents: 
14981diff
changeset | 108 | val env' = Envir.Envir | 
| 15570 | 109 |             {maxidx = Library.foldl Int.max
 | 
| 15399 
683d83051d6a
Added term cache to function condrew in order to speed up rewriting.
 berghofe parents: 
14981diff
changeset | 110 | (~1, map (Int.max o pairself maxidx_of_term) prems'), | 
| 15798 
016f3be5a5ec
Adapted to new interface of instantiation and unification / matching functions.
 berghofe parents: 
15574diff
changeset | 111 | iTs = Tenv, asol = tenv}; | 
| 18184 | 112 | val env'' = fold (Pattern.unify thy o pairself (lookup rew)) prems' env'; | 
| 15531 | 113 | in SOME (Envir.norm_term env'' (inc (ren tm2))) | 
| 114 | end handle Pattern.MATCH => NONE | Pattern.Unif => NONE) | |
| 16486 | 115 | (sort (int_ord o pairself fst) | 
| 18956 | 116 | (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 | 117 | end; | 
| 13402 | 118 | |
| 119 | in rew end; | |
| 120 | ||
| 15531 | 121 | val chtype = change_type o SOME; | 
| 13402 | 122 | |
| 16195 | 123 | fun extr_name s vs = NameSpace.append "extr" (space_implode "_" (s :: vs)); | 
| 124 | fun corr_name s vs = extr_name s vs ^ "_correctness"; | |
| 13402 | 125 | |
| 16195 | 126 | fun msg d s = priority (Symbol.spaces d ^ s); | 
| 13402 | 127 | |
| 16865 | 128 | fun vars_of t = rev (fold_aterms (fn v as Var _ => insert (op =) v | _ => I) t []); | 
| 16983 | 129 | fun vfs_of t = vars_of t @ sort Term.term_ord (term_frees t); | 
| 13402 | 130 | |
| 131 | fun forall_intr (t, prop) = | |
| 132 | let val (a, T) = (case t of Var ((a, _), T) => (a, T) | Free p => p) | |
| 133 | in all T $ Abs (a, T, abstract_over (t, prop)) end; | |
| 134 | ||
| 135 | fun forall_intr_prf (t, prf) = | |
| 136 | let val (a, T) = (case t of Var ((a, _), T) => (a, T) | Free p => p) | |
| 15531 | 137 | in Abst (a, SOME T, prf_abstract_over t prf) end; | 
| 13402 | 138 | |
| 23178 | 139 | val mkabs = List.foldr (fn (v, t) => Abs ("x", fastype_of v, abstract_over (v, t)));
 | 
| 13402 | 140 | |
| 13732 | 141 | fun strip_abs 0 t = t | 
| 142 | | strip_abs n (Abs (_, _, t)) = strip_abs (n-1) t | |
| 143 | | strip_abs _ _ = error "strip_abs: not an abstraction"; | |
| 144 | ||
| 13402 | 145 | fun prf_subst_TVars tye = | 
| 146 | map_proof_terms (subst_TVars tye) (typ_subst_TVars tye); | |
| 147 | ||
| 23178 | 148 | fun relevant_vars types prop = List.foldr (fn | 
| 13402 | 149 | (Var ((a, i), T), vs) => (case strip_type T of | 
| 20664 | 150 | (_, Type (s, _)) => if member (op =) types s then a :: vs else vs | 
| 13402 | 151 | | _ => vs) | 
| 15574 
b1d1b5bfc464
Removed practically all references to Library.foldr.
 skalberg parents: 
15570diff
changeset | 152 | | (_, vs) => vs) [] (vars_of prop); | 
| 13402 | 153 | |
| 13732 | 154 | fun tname_of (Type (s, _)) = s | 
| 155 | | tname_of _ = ""; | |
| 156 | ||
| 157 | fun get_var_type t = | |
| 158 | let | |
| 16865 | 159 | val vs = Term.add_vars t []; | 
| 160 | val fs = Term.add_frees t []; | |
| 13732 | 161 | in fn | 
| 17232 | 162 | Var (ixn, _) => (case AList.lookup (op =) vs ixn of | 
| 15531 | 163 | NONE => error "get_var_type: no such variable in term" | 
| 164 | | SOME T => Var (ixn, T)) | |
| 17232 | 165 | | Free (s, _) => (case AList.lookup (op =) fs s of | 
| 15531 | 166 | NONE => error "get_var_type: no such variable in term" | 
| 167 | | SOME T => Free (s, T)) | |
| 13732 | 168 | | _ => error "get_var_type: not a variable" | 
| 169 | end; | |
| 170 | ||
| 13402 | 171 | |
| 172 | (**** theory data ****) | |
| 173 | ||
| 22846 | 174 | (* theory data *) | 
| 13402 | 175 | |
| 16458 | 176 | structure ExtractionData = TheoryDataFun | 
| 22846 | 177 | ( | 
| 13402 | 178 | type T = | 
| 179 |     {realizes_eqns : rules,
 | |
| 180 | typeof_eqns : rules, | |
| 13732 | 181 | types : (string * ((term -> term option) list * | 
| 182 | (term -> typ -> term -> typ -> term) option)) list, | |
| 13402 | 183 | realizers : (string list * (term * proof)) list Symtab.table, | 
| 184 | defs : thm list, | |
| 185 | expand : (string * term) list, | |
| 16458 | 186 | prep : (theory -> proof -> proof) option} | 
| 13402 | 187 | |
| 188 | val empty = | |
| 189 |     {realizes_eqns = empty_rules,
 | |
| 190 | typeof_eqns = empty_rules, | |
| 191 | types = [], | |
| 192 | realizers = Symtab.empty, | |
| 193 | defs = [], | |
| 194 | expand = [], | |
| 15531 | 195 | prep = NONE}; | 
| 13402 | 196 | val copy = I; | 
| 16458 | 197 | val extend = I; | 
| 13402 | 198 | |
| 16458 | 199 | fun merge _ | 
| 13402 | 200 |     (({realizes_eqns = realizes_eqns1, typeof_eqns = typeof_eqns1, types = types1,
 | 
| 201 | realizers = realizers1, defs = defs1, expand = expand1, prep = prep1}, | |
| 202 |       {realizes_eqns = realizes_eqns2, typeof_eqns = typeof_eqns2, types = types2,
 | |
| 203 | realizers = realizers2, defs = defs2, expand = expand2, prep = prep2}) : T * T) = | |
| 204 |     {realizes_eqns = merge_rules realizes_eqns1 realizes_eqns2,
 | |
| 205 | typeof_eqns = merge_rules typeof_eqns1 typeof_eqns2, | |
| 22717 | 206 | types = AList.merge (op =) (K true) (types1, types2), | 
| 19305 | 207 | realizers = Symtab.merge_list (gen_eq_set (op =) o pairself #1) (realizers1, realizers2), | 
| 22662 | 208 | defs = Library.merge Thm.eq_thm (defs1, defs2), | 
| 209 | expand = Library.merge (op =) (expand1, expand2), | |
| 15531 | 210 | prep = (case prep1 of NONE => prep2 | _ => prep1)}; | 
| 22846 | 211 | ); | 
| 13402 | 212 | |
| 213 | fun read_condeq thy = | |
| 16458 | 214 | let val thy' = add_syntax thy | 
| 13402 | 215 | in fn s => | 
| 24707 | 216 | let val t = Logic.varify (Syntax.read_prop_global thy' s) | 
| 13402 | 217 | in (map Logic.dest_equals (Logic.strip_imp_prems t), | 
| 218 | Logic.dest_equals (Logic.strip_imp_concl t)) | |
| 219 |     end handle TERM _ => error ("Not a (conditional) meta equality:\n" ^ s)
 | |
| 220 | end; | |
| 221 | ||
| 222 | (** preprocessor **) | |
| 223 | ||
| 224 | fun set_preprocessor prep thy = | |
| 225 |   let val {realizes_eqns, typeof_eqns, types, realizers,
 | |
| 226 | defs, expand, ...} = ExtractionData.get thy | |
| 227 | in | |
| 228 | ExtractionData.put | |
| 229 |       {realizes_eqns = realizes_eqns, typeof_eqns = typeof_eqns, types = types,
 | |
| 15531 | 230 | realizers = realizers, defs = defs, expand = expand, prep = SOME prep} thy | 
| 13402 | 231 | end; | 
| 232 | ||
| 233 | (** equations characterizing realizability **) | |
| 234 | ||
| 235 | fun gen_add_realizes_eqns prep_eq eqns thy = | |
| 236 |   let val {realizes_eqns, typeof_eqns, types, realizers,
 | |
| 237 | defs, expand, prep} = ExtractionData.get thy; | |
| 238 | in | |
| 239 | ExtractionData.put | |
| 23178 | 240 |       {realizes_eqns = List.foldr add_rule realizes_eqns (map (prep_eq thy) eqns),
 | 
| 13402 | 241 | typeof_eqns = typeof_eqns, types = types, realizers = realizers, | 
| 242 | defs = defs, expand = expand, prep = prep} thy | |
| 243 | end | |
| 244 | ||
| 245 | val add_realizes_eqns_i = gen_add_realizes_eqns (K I); | |
| 246 | val add_realizes_eqns = gen_add_realizes_eqns read_condeq; | |
| 247 | ||
| 248 | (** equations characterizing type of extracted program **) | |
| 249 | ||
| 250 | fun gen_add_typeof_eqns prep_eq eqns thy = | |
| 251 | let | |
| 252 |     val {realizes_eqns, typeof_eqns, types, realizers,
 | |
| 253 | defs, expand, prep} = ExtractionData.get thy; | |
| 13732 | 254 | val eqns' = map (prep_eq thy) eqns | 
| 13402 | 255 | in | 
| 256 | ExtractionData.put | |
| 257 |       {realizes_eqns = realizes_eqns, realizers = realizers,
 | |
| 23178 | 258 | typeof_eqns = List.foldr add_rule typeof_eqns eqns', | 
| 13732 | 259 | types = types, defs = defs, expand = expand, prep = prep} thy | 
| 13402 | 260 | end | 
| 261 | ||
| 262 | val add_typeof_eqns_i = gen_add_typeof_eqns (K I); | |
| 263 | val add_typeof_eqns = gen_add_typeof_eqns read_condeq; | |
| 264 | ||
| 265 | fun thaw (T as TFree (a, S)) = | |
| 16195 | 266 | if exists_string (equal ":") a then TVar (unpack_ixn a, S) else T | 
| 13402 | 267 | | thaw (Type (a, Ts)) = Type (a, map thaw Ts) | 
| 268 | | thaw T = T; | |
| 269 | ||
| 270 | fun freeze (TVar ((a, i), S)) = TFree (a ^ ":" ^ string_of_int i, S) | |
| 271 | | freeze (Type (a, Ts)) = Type (a, map freeze Ts) | |
| 272 | | freeze T = T; | |
| 273 | ||
| 274 | fun freeze_thaw f x = | |
| 20548 
8ef25fe585a8
renamed Term.map_term_types to Term.map_types (cf. Term.fold_types);
 wenzelm parents: 
19482diff
changeset | 275 | map_types thaw (f (map_types freeze x)); | 
| 13402 | 276 | |
| 16458 | 277 | fun etype_of thy vs Ts t = | 
| 13402 | 278 | let | 
| 16458 | 279 |     val {typeof_eqns, ...} = ExtractionData.get thy;
 | 
| 13402 | 280 |     fun err () = error ("Unable to determine type of extracted program for\n" ^
 | 
| 16458 | 281 | Sign.string_of_term thy t) | 
| 282 | in case strip_abs_body (freeze_thaw (condrew thy (#net typeof_eqns) | |
| 283 | [typeof_proc (Sign.defaultS thy) vs]) (list_abs (map (pair "x") (rev Ts), | |
| 13402 | 284 |       Const ("typeof", fastype_of1 (Ts, t) --> Type ("Type", [])) $ t))) of
 | 
| 285 |       Const ("Type", _) $ u => (Logic.dest_type u handle TERM _ => err ())
 | |
| 286 | | _ => err () | |
| 287 | end; | |
| 288 | ||
| 289 | (** realizers for axioms / theorems, together with correctness proofs **) | |
| 290 | ||
| 291 | fun gen_add_realizers prep_rlz rs thy = | |
| 292 |   let val {realizes_eqns, typeof_eqns, types, realizers,
 | |
| 293 | defs, expand, prep} = ExtractionData.get thy | |
| 294 | in | |
| 295 | ExtractionData.put | |
| 296 |       {realizes_eqns = realizes_eqns, typeof_eqns = typeof_eqns, types = types,
 | |
| 25389 | 297 | realizers = fold (Symtab.cons_list o prep_rlz thy) rs realizers, | 
| 13402 | 298 | defs = defs, expand = expand, prep = prep} thy | 
| 299 | end | |
| 300 | ||
| 301 | fun prep_realizer thy = | |
| 302 | let | |
| 13732 | 303 |     val {realizes_eqns, typeof_eqns, defs, types, ...} =
 | 
| 13402 | 304 | ExtractionData.get thy; | 
| 19482 
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
 wenzelm parents: 
19466diff
changeset | 305 | val procs = maps (fst o snd) types; | 
| 13732 | 306 | val rtypes = map fst types; | 
| 16800 | 307 | val eqns = Net.merge (K false) (#net realizes_eqns, #net typeof_eqns); | 
| 13402 | 308 | val thy' = add_syntax thy; | 
| 309 | val rd = ProofSyntax.read_proof thy' false | |
| 310 | in fn (thm, (vs, s1, s2)) => | |
| 311 | let | |
| 21646 
c07b5b0e8492
thm/prf: separate official name vs. additional tags;
 wenzelm parents: 
20897diff
changeset | 312 | val name = Thm.get_name thm; | 
| 21858 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 wenzelm parents: 
21646diff
changeset | 313 | val _ = name <> "" orelse error "add_realizers: unnamed theorem"; | 
| 17203 | 314 | val prop = Pattern.rewrite_term thy' | 
| 13402 | 315 | (map (Logic.dest_equals o prop_of) defs) [] (prop_of thm); | 
| 316 | val vars = vars_of prop; | |
| 13732 | 317 | val vars' = filter_out (fn v => | 
| 20664 | 318 | member (op =) rtypes (tname_of (body_type (fastype_of v)))) vars; | 
| 16458 | 319 | val T = etype_of thy' vs [] prop; | 
| 13402 | 320 | val (T', thw) = Type.freeze_thaw_type | 
| 13732 | 321 | (if T = nullT then nullT else map fastype_of vars' ---> T); | 
| 22675 
acf10be7dcca
cleaned/simplified Sign.read_typ, Thm.read_cterm etc.;
 wenzelm parents: 
22662diff
changeset | 322 | val t = map_types thw (Sign.simple_read_term thy' T' s1); | 
| 16458 | 323 | val r' = freeze_thaw (condrew thy' eqns | 
| 324 | (procs @ [typeof_proc (Sign.defaultS thy') vs, rlz_proc])) | |
| 13402 | 325 |           (Const ("realizes", T --> propT --> propT) $
 | 
| 13732 | 326 | (if T = nullT then t else list_comb (t, vars')) $ prop); | 
| 23178 | 327 | val r = List.foldr forall_intr r' (map (get_var_type r') vars); | 
| 16458 | 328 | val prf = Reconstruct.reconstruct_proof thy' r (rd s2); | 
| 13402 | 329 | in (name, (vs, (t, prf))) end | 
| 330 | end; | |
| 331 | ||
| 332 | val add_realizers_i = gen_add_realizers | |
| 333 | (fn _ => fn (name, (vs, t, prf)) => (name, (vs, (t, prf)))); | |
| 334 | val add_realizers = gen_add_realizers prep_realizer; | |
| 335 | ||
| 13714 | 336 | fun realizes_of thy vs t prop = | 
| 337 | let | |
| 338 | val thy' = add_syntax thy; | |
| 13732 | 339 |     val {realizes_eqns, typeof_eqns, defs, types, ...} =
 | 
| 13714 | 340 | ExtractionData.get thy'; | 
| 22717 | 341 | val procs = maps (rev o fst o snd) types; | 
| 16800 | 342 | val eqns = Net.merge (K false) (#net realizes_eqns, #net typeof_eqns); | 
| 17203 | 343 | val prop' = Pattern.rewrite_term thy' | 
| 13714 | 344 | (map (Logic.dest_equals o prop_of) defs) [] prop; | 
| 16458 | 345 | in freeze_thaw (condrew thy' eqns | 
| 346 | (procs @ [typeof_proc (Sign.defaultS thy') vs, rlz_proc])) | |
| 13714 | 347 |       (Const ("realizes", fastype_of t --> propT --> propT) $ t $ prop')
 | 
| 348 | end; | |
| 349 | ||
| 13402 | 350 | (** expanding theorems / definitions **) | 
| 351 | ||
| 18728 | 352 | fun add_expand_thm thm thy = | 
| 13402 | 353 | let | 
| 354 |     val {realizes_eqns, typeof_eqns, types, realizers,
 | |
| 355 | defs, expand, prep} = ExtractionData.get thy; | |
| 356 | ||
| 21646 
c07b5b0e8492
thm/prf: separate official name vs. additional tags;
 wenzelm parents: 
20897diff
changeset | 357 | val name = Thm.get_name thm; | 
| 21858 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 wenzelm parents: 
21646diff
changeset | 358 | val _ = name <> "" orelse error "add_expand_thms: unnamed theorem"; | 
| 13402 | 359 | |
| 360 | val is_def = | |
| 361 | (case strip_comb (fst (Logic.dest_equals (prop_of thm))) of | |
| 18928 
042608ffa2ec
subsituted gen_duplicates / has_duplicates for duplicates whenever appropriate
 haftmann parents: 
18921diff
changeset | 362 | (Const _, ts) => forall is_Var ts andalso not (has_duplicates (op =) ts) | 
| 22924 | 363 | andalso (PureThy.get_kind thm = Thm.definitionK orelse can (Thm.get_axiom_i thy) name) | 
| 13402 | 364 | | _ => false) handle TERM _ => false; | 
| 365 | in | |
| 366 | (ExtractionData.put (if is_def then | |
| 367 |         {realizes_eqns = realizes_eqns,
 | |
| 368 | typeof_eqns = add_rule (([], | |
| 369 | Logic.dest_equals (prop_of (Drule.abs_def thm))), typeof_eqns), | |
| 370 | types = types, | |
| 22360 
26ead7ed4f4b
moved eq_thm etc. to structure Thm in Pure/more_thm.ML;
 wenzelm parents: 
21858diff
changeset | 371 | realizers = realizers, defs = insert Thm.eq_thm thm defs, | 
| 13402 | 372 | expand = expand, prep = prep} | 
| 373 | else | |
| 374 |         {realizes_eqns = realizes_eqns, typeof_eqns = typeof_eqns, types = types,
 | |
| 375 | realizers = realizers, defs = defs, | |
| 20854 | 376 | expand = insert (op =) (name, prop_of thm) expand, prep = prep}) thy) | 
| 13402 | 377 | end; | 
| 378 | ||
| 18728 | 379 | val add_expand_thms = fold add_expand_thm; | 
| 380 | ||
| 381 | val extraction_expand = | |
| 20897 | 382 | Attrib.no_args (Thm.declaration_attribute (fn th => Context.mapping (add_expand_thm th) I)); | 
| 13402 | 383 | |
| 15801 | 384 | |
| 13732 | 385 | (** types with computational content **) | 
| 386 | ||
| 387 | fun add_types tys thy = | |
| 22717 | 388 | ExtractionData.map | 
| 389 |     (fn {realizes_eqns, typeof_eqns, types, realizers, defs, expand, prep} =>
 | |
| 13732 | 390 |       {realizes_eqns = realizes_eqns, typeof_eqns = typeof_eqns,
 | 
| 22717 | 391 | types = fold (AList.update (op =) o apfst (Sign.intern_type thy)) tys types, | 
| 392 | realizers = realizers, defs = defs, expand = expand, prep = prep}) | |
| 393 | thy; | |
| 13732 | 394 | |
| 13402 | 395 | |
| 15801 | 396 | (** Pure setup **) | 
| 397 | ||
| 26435 | 398 | val _ = Context.>> | 
| 18708 | 399 |   (add_types [("prop", ([], NONE))] #>
 | 
| 15801 | 400 | |
| 401 | add_typeof_eqns | |
| 402 | ["(typeof (PROP P)) == (Type (TYPE(Null))) ==> \ | |
| 403 |     \  (typeof (PROP Q)) == (Type (TYPE('Q))) ==>  \
 | |
| 404 |     \    (typeof (PROP P ==> PROP Q)) == (Type (TYPE('Q)))",
 | |
| 405 | ||
| 406 | "(typeof (PROP Q)) == (Type (TYPE(Null))) ==> \ | |
| 407 | \ (typeof (PROP P ==> PROP Q)) == (Type (TYPE(Null)))", | |
| 408 | ||
| 409 |       "(typeof (PROP P)) == (Type (TYPE('P))) ==>  \
 | |
| 410 |     \  (typeof (PROP Q)) == (Type (TYPE('Q))) ==>  \
 | |
| 411 |     \    (typeof (PROP P ==> PROP Q)) == (Type (TYPE('P => 'Q)))",
 | |
| 412 | ||
| 413 | "(%x. typeof (PROP P (x))) == (%x. Type (TYPE(Null))) ==> \ | |
| 414 | \ (typeof (!!x. PROP P (x))) == (Type (TYPE(Null)))", | |
| 415 | ||
| 416 |       "(%x. typeof (PROP P (x))) == (%x. Type (TYPE('P))) ==>  \
 | |
| 417 |     \    (typeof (!!x::'a. PROP P (x))) == (Type (TYPE('a => 'P)))",
 | |
| 418 | ||
| 419 |       "(%x. typeof (f (x))) == (%x. Type (TYPE('f))) ==>  \
 | |
| 18708 | 420 |     \    (typeof (f)) == (Type (TYPE('f)))"] #>
 | 
| 15801 | 421 | |
| 422 | add_realizes_eqns | |
| 423 | ["(typeof (PROP P)) == (Type (TYPE(Null))) ==> \ | |
| 424 | \ (realizes (r) (PROP P ==> PROP Q)) == \ | |
| 425 | \ (PROP realizes (Null) (PROP P) ==> PROP realizes (r) (PROP Q))", | |
| 426 | ||
| 427 |       "(typeof (PROP P)) == (Type (TYPE('P))) ==>  \
 | |
| 428 | \ (typeof (PROP Q)) == (Type (TYPE(Null))) ==> \ | |
| 429 | \ (realizes (r) (PROP P ==> PROP Q)) == \ | |
| 430 | \ (!!x::'P. PROP realizes (x) (PROP P) ==> PROP realizes (Null) (PROP Q))", | |
| 431 | ||
| 432 | "(realizes (r) (PROP P ==> PROP Q)) == \ | |
| 433 | \ (!!x. PROP realizes (x) (PROP P) ==> PROP realizes (r (x)) (PROP Q))", | |
| 434 | ||
| 435 | "(%x. typeof (PROP P (x))) == (%x. Type (TYPE(Null))) ==> \ | |
| 436 | \ (realizes (r) (!!x. PROP P (x))) == \ | |
| 437 | \ (!!x. PROP realizes (Null) (PROP P (x)))", | |
| 438 | ||
| 439 | "(realizes (r) (!!x. PROP P (x))) == \ | |
| 18708 | 440 | \ (!!x. PROP realizes (r (x)) (PROP P (x)))"] #> | 
| 15801 | 441 | |
| 442 | Attrib.add_attributes | |
| 18728 | 443 |      [("extraction_expand", extraction_expand,
 | 
| 18708 | 444 | "specify theorems / definitions to be expanded during extraction")]); | 
| 15801 | 445 | |
| 446 | ||
| 13402 | 447 | (**** extract program ****) | 
| 448 | ||
| 449 | val dummyt = Const ("dummy", dummyT);
 | |
| 450 | ||
| 451 | fun extract thms thy = | |
| 452 | let | |
| 16458 | 453 | val thy' = add_syntax thy; | 
| 13402 | 454 |     val {realizes_eqns, typeof_eqns, types, realizers, defs, expand, prep} =
 | 
| 455 | ExtractionData.get thy; | |
| 22717 | 456 | val procs = maps (rev o fst o snd) types; | 
| 13732 | 457 | val rtypes = map fst types; | 
| 16458 | 458 | val typroc = typeof_proc (Sign.defaultS thy'); | 
| 19466 | 459 | val prep = the_default (K I) prep thy' o ProofRewriteRules.elim_defs thy' false defs o | 
| 16458 | 460 |       Reconstruct.expand_proof thy' (("", NONE) :: map (apsnd SOME) expand);
 | 
| 16800 | 461 | val rrews = Net.merge (K false) (#net realizes_eqns, #net typeof_eqns); | 
| 13402 | 462 | |
| 463 | fun find_inst prop Ts ts vs = | |
| 464 | let | |
| 13732 | 465 | val rvs = relevant_vars rtypes prop; | 
| 13402 | 466 | val vars = vars_of prop; | 
| 467 | val n = Int.min (length vars, length ts); | |
| 468 | ||
| 469 | fun add_args ((Var ((a, i), _), t), (vs', tye)) = | |
| 20664 | 470 | if member (op =) rvs a then | 
| 16458 | 471 | let val T = etype_of thy' vs Ts t | 
| 13402 | 472 | in if T = nullT then (vs', tye) | 
| 473 |                else (a :: vs', (("'" ^ a, i), T) :: tye)
 | |
| 474 | end | |
| 475 | else (vs', tye) | |
| 476 | ||
| 23178 | 477 | in List.foldr add_args ([], []) (Library.take (n, vars) ~~ Library.take (n, ts)) end; | 
| 13402 | 478 | |
| 20664 | 479 | fun find (vs: string list) = Option.map snd o find_first (curry (gen_eq_set (op =)) vs o fst); | 
| 15570 | 480 | fun find' s = map snd o List.filter (equal s o fst) | 
| 13402 | 481 | |
| 13732 | 482 | fun app_rlz_rews Ts vs t = strip_abs (length Ts) (freeze_thaw | 
| 16458 | 483 | (condrew thy' rrews (procs @ [typroc vs, rlz_proc])) (list_abs | 
| 13732 | 484 | (map (pair "x") (rev Ts), t))); | 
| 485 | ||
| 486 | fun realizes_null vs prop = app_rlz_rews [] vs | |
| 487 |       (Const ("realizes", nullT --> propT --> propT) $ nullt $ prop);
 | |
| 13402 | 488 | |
| 489 | fun corr d defs vs ts Ts hs (PBound i) _ _ = (defs, PBound i) | |
| 490 | ||
| 15531 | 491 | | corr d defs vs ts Ts hs (Abst (s, SOME T, prf)) (Abst (_, _, prf')) t = | 
| 13402 | 492 | let val (defs', corr_prf) = corr d defs vs [] (T :: Ts) | 
| 493 | (dummyt :: hs) prf (incr_pboundvars 1 0 prf') | |
| 15531 | 494 | (case t of SOME (Abs (_, _, u)) => SOME u | _ => NONE) | 
| 495 | in (defs', Abst (s, SOME T, corr_prf)) end | |
| 13402 | 496 | |
| 15531 | 497 | | corr d defs vs ts Ts hs (AbsP (s, SOME prop, prf)) (AbsP (_, _, prf')) t = | 
| 13402 | 498 | let | 
| 16458 | 499 | val T = etype_of thy' vs Ts prop; | 
| 13402 | 500 | val u = if T = nullT then | 
| 15531 | 501 | (case t of SOME u => SOME (incr_boundvars 1 u) | NONE => NONE) | 
| 502 | else (case t of SOME (Abs (_, _, u)) => SOME u | _ => NONE); | |
| 13402 | 503 | val (defs', corr_prf) = corr d defs vs [] (T :: Ts) (prop :: hs) | 
| 504 | (incr_pboundvars 0 1 prf) (incr_pboundvars 0 1 prf') u; | |
| 505 |             val rlz = Const ("realizes", T --> propT --> propT)
 | |
| 506 | in (defs', | |
| 13732 | 507 |             if T = nullT then AbsP ("R",
 | 
| 15531 | 508 | SOME (app_rlz_rews Ts vs (rlz $ nullt $ prop)), | 
| 13732 | 509 | prf_subst_bounds [nullt] corr_prf) | 
| 15531 | 510 |             else Abst (s, SOME T, AbsP ("R",
 | 
| 511 | SOME (app_rlz_rews (T :: Ts) vs | |
| 13732 | 512 | (rlz $ Bound 0 $ incr_boundvars 1 prop)), corr_prf))) | 
| 13402 | 513 | end | 
| 514 | ||
| 15531 | 515 | | corr d defs vs ts Ts hs (prf % SOME t) (prf' % _) t' = | 
| 13732 | 516 | let | 
| 517 | val (Us, T) = strip_type (fastype_of1 (Ts, t)); | |
| 518 | val (defs', corr_prf) = corr d defs vs (t :: ts) Ts hs prf prf' | |
| 20664 | 519 | (if member (op =) rtypes (tname_of T) then t' | 
| 15531 | 520 | else (case t' of SOME (u $ _) => SOME u | _ => NONE)); | 
| 20664 | 521 | val u = if not (member (op =) rtypes (tname_of T)) then t else | 
| 13732 | 522 | let | 
| 16458 | 523 | val eT = etype_of thy' vs Ts t; | 
| 13732 | 524 | val (r, Us') = if eT = nullT then (nullt, Us) else | 
| 525 | (Bound (length Us), eT :: Us); | |
| 526 | val u = list_comb (incr_boundvars (length Us') t, | |
| 527 | map Bound (length Us - 1 downto 0)); | |
| 17271 | 528 | val u' = (case AList.lookup (op =) types (tname_of T) of | 
| 15531 | 529 | SOME ((_, SOME f)) => f r eT u T | 
| 13732 | 530 |                   | _ => Const ("realizes", eT --> T --> T) $ r $ u)
 | 
| 531 | in app_rlz_rews Ts vs (list_abs (map (pair "x") Us', u')) end | |
| 15531 | 532 | in (defs', corr_prf % SOME u) end | 
| 13402 | 533 | |
| 534 | | corr d defs vs ts Ts hs (prf1 %% prf2) (prf1' %% prf2') t = | |
| 535 | let | |
| 536 | val prop = Reconstruct.prop_of' hs prf2'; | |
| 16458 | 537 | val T = etype_of thy' vs Ts prop; | 
| 15531 | 538 | val (defs1, f, u) = if T = nullT then (defs, t, NONE) else | 
| 13402 | 539 | (case t of | 
| 15531 | 540 | SOME (f $ u) => (defs, SOME f, SOME u) | 
| 13402 | 541 | | _ => | 
| 542 | let val (defs1, u) = extr d defs vs [] Ts hs prf2' | |
| 15531 | 543 | in (defs1, NONE, SOME u) end) | 
| 13402 | 544 | val (defs2, corr_prf1) = corr d defs1 vs [] Ts hs prf1 prf1' f; | 
| 545 | val (defs3, corr_prf2) = corr d defs2 vs [] Ts hs prf2 prf2' u; | |
| 546 | in | |
| 547 | if T = nullT then (defs3, corr_prf1 %% corr_prf2) else | |
| 548 | (defs3, corr_prf1 % u %% corr_prf2) | |
| 549 | end | |
| 550 | ||
| 21646 
c07b5b0e8492
thm/prf: separate official name vs. additional tags;
 wenzelm parents: 
20897diff
changeset | 551 | | corr d defs vs ts Ts hs (prf0 as PThm (name, prf, prop, SOME Ts')) _ _ = | 
| 13402 | 552 | let | 
| 553 | val (vs', tye) = find_inst prop Ts ts vs; | |
| 554 | val tye' = (map fst (term_tvars prop) ~~ Ts') @ tye; | |
| 16458 | 555 | val T = etype_of thy' vs' [] prop; | 
| 13402 | 556 | val defs' = if T = nullT then defs | 
| 557 | else fst (extr d defs vs ts Ts hs prf0) | |
| 558 | in | |
| 13609 
73c3915553b4
Added check for axioms with "realizes Null A = A".
 berghofe parents: 
13417diff
changeset | 559 | if T = nullT andalso realizes_null vs' prop aconv prop then (defs, prf0) | 
| 17412 | 560 | else case Symtab.lookup realizers name of | 
| 15531 | 561 | NONE => (case find vs' (find' name defs') of | 
| 562 | NONE => | |
| 13402 | 563 | let | 
| 21858 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 wenzelm parents: 
21646diff
changeset | 564 | val _ = T = nullT orelse error "corr: internal error"; | 
| 13402 | 565 |                     val _ = msg d ("Building correctness proof for " ^ quote name ^
 | 
| 566 | (if null vs' then "" | |
| 567 | else " (relevant variables: " ^ commas_quote vs' ^ ")")); | |
| 16458 | 568 | val prf' = prep (Reconstruct.reconstruct_proof thy' prop prf); | 
| 13402 | 569 | val (defs'', corr_prf) = | 
| 15531 | 570 | corr (d + 1) defs' vs' [] [] [] prf' prf' NONE; | 
| 13732 | 571 | val corr_prop = Reconstruct.prop_of corr_prf; | 
| 23178 | 572 | val corr_prf' = List.foldr forall_intr_prf | 
| 15574 
b1d1b5bfc464
Removed practically all references to Library.foldr.
 skalberg parents: 
15570diff
changeset | 573 | (proof_combt | 
| 21646 
c07b5b0e8492
thm/prf: separate official name vs. additional tags;
 wenzelm parents: 
20897diff
changeset | 574 | (PThm (corr_name name vs', corr_prf, corr_prop, | 
| 15531 | 575 | SOME (map TVar (term_tvars corr_prop))), vfs_of corr_prop)) | 
| 22750 | 576 | (map (get_var_type corr_prop) (vfs_of prop)) | 
| 13402 | 577 | in | 
| 13732 | 578 | ((name, (vs', ((nullt, nullt), (corr_prf, corr_prf')))) :: defs'', | 
| 13402 | 579 | prf_subst_TVars tye' corr_prf') | 
| 580 | end | |
| 15531 | 581 | | SOME (_, (_, prf')) => (defs', prf_subst_TVars tye' prf')) | 
| 582 | | SOME rs => (case find vs' rs of | |
| 583 | SOME (_, prf') => (defs', prf_subst_TVars tye' prf') | |
| 584 |               | NONE => error ("corr: no realizer for instance of theorem " ^
 | |
| 16458 | 585 | quote name ^ ":\n" ^ Sign.string_of_term thy' (Envir.beta_norm | 
| 13402 | 586 | (Reconstruct.prop_of (proof_combt (prf0, ts)))))) | 
| 587 | end | |
| 588 | ||
| 15531 | 589 | | corr d defs vs ts Ts hs (prf0 as PAxm (s, prop, SOME Ts')) _ _ = | 
| 13402 | 590 | let | 
| 591 | val (vs', tye) = find_inst prop Ts ts vs; | |
| 592 | val tye' = (map fst (term_tvars prop) ~~ Ts') @ tye | |
| 593 | in | |
| 16458 | 594 | if etype_of thy' vs' [] prop = nullT andalso | 
| 13609 
73c3915553b4
Added check for axioms with "realizes Null A = A".
 berghofe parents: 
13417diff
changeset | 595 | realizes_null vs' prop aconv prop then (defs, prf0) | 
| 18956 | 596 | else case find vs' (Symtab.lookup_list realizers s) of | 
| 15531 | 597 | SOME (_, prf) => (defs, prf_subst_TVars tye' prf) | 
| 598 |             | NONE => error ("corr: no realizer for instance of axiom " ^
 | |
| 16458 | 599 | quote s ^ ":\n" ^ Sign.string_of_term thy' (Envir.beta_norm | 
| 13402 | 600 | (Reconstruct.prop_of (proof_combt (prf0, ts))))) | 
| 601 | end | |
| 602 | ||
| 603 | | corr d defs vs ts Ts hs _ _ _ = error "corr: bad proof" | |
| 604 | ||
| 605 | and extr d defs vs ts Ts hs (PBound i) = (defs, Bound i) | |
| 606 | ||
| 15531 | 607 | | extr d defs vs ts Ts hs (Abst (s, SOME T, prf)) = | 
| 13402 | 608 | let val (defs', t) = extr d defs vs [] | 
| 609 | (T :: Ts) (dummyt :: hs) (incr_pboundvars 1 0 prf) | |
| 610 | in (defs', Abs (s, T, t)) end | |
| 611 | ||
| 15531 | 612 | | extr d defs vs ts Ts hs (AbsP (s, SOME t, prf)) = | 
| 13402 | 613 | let | 
| 16458 | 614 | val T = etype_of thy' vs Ts t; | 
| 13402 | 615 | val (defs', t) = extr d defs vs [] (T :: Ts) (t :: hs) | 
| 616 | (incr_pboundvars 0 1 prf) | |
| 617 | in (defs', | |
| 618 | if T = nullT then subst_bound (nullt, t) else Abs (s, T, t)) | |
| 619 | end | |
| 620 | ||
| 15531 | 621 | | extr d defs vs ts Ts hs (prf % SOME t) = | 
| 13402 | 622 | let val (defs', u) = extr d defs vs (t :: ts) Ts hs prf | 
| 13732 | 623 | in (defs', | 
| 20664 | 624 | if member (op =) rtypes (tname_of (body_type (fastype_of1 (Ts, t)))) then u | 
| 13732 | 625 | else u $ t) | 
| 626 | end | |
| 13402 | 627 | |
| 628 | | extr d defs vs ts Ts hs (prf1 %% prf2) = | |
| 629 | let | |
| 630 | val (defs', f) = extr d defs vs [] Ts hs prf1; | |
| 631 | val prop = Reconstruct.prop_of' hs prf2; | |
| 16458 | 632 | val T = etype_of thy' vs Ts prop | 
| 13402 | 633 | in | 
| 634 | if T = nullT then (defs', f) else | |
| 635 | let val (defs'', t) = extr d defs' vs [] Ts hs prf2 | |
| 636 | in (defs'', f $ t) end | |
| 637 | end | |
| 638 | ||
| 21646 
c07b5b0e8492
thm/prf: separate official name vs. additional tags;
 wenzelm parents: 
20897diff
changeset | 639 | | extr d defs vs ts Ts hs (prf0 as PThm (s, prf, prop, SOME Ts')) = | 
| 13402 | 640 | let | 
| 641 | val (vs', tye) = find_inst prop Ts ts vs; | |
| 642 | val tye' = (map fst (term_tvars prop) ~~ Ts') @ tye | |
| 643 | in | |
| 17412 | 644 | case Symtab.lookup realizers s of | 
| 15531 | 645 | NONE => (case find vs' (find' s defs) of | 
| 646 | NONE => | |
| 13402 | 647 | let | 
| 648 |                     val _ = msg d ("Extracting " ^ quote s ^
 | |
| 649 | (if null vs' then "" | |
| 650 | else " (relevant variables: " ^ commas_quote vs' ^ ")")); | |
| 16458 | 651 | val prf' = prep (Reconstruct.reconstruct_proof thy' prop prf); | 
| 13402 | 652 | val (defs', t) = extr (d + 1) defs vs' [] [] [] prf'; | 
| 653 | val (defs'', corr_prf) = | |
| 15531 | 654 | corr (d + 1) defs' vs' [] [] [] prf' prf' (SOME t); | 
| 13402 | 655 | |
| 656 | val nt = Envir.beta_norm t; | |
| 20664 | 657 | val args = filter_out (fn v => member (op =) rtypes | 
| 658 | (tname_of (body_type (fastype_of v)))) (vfs_of prop); | |
| 15570 | 659 | val args' = List.filter (fn v => Logic.occs (v, nt)) args; | 
| 15574 
b1d1b5bfc464
Removed practically all references to Library.foldr.
 skalberg parents: 
15570diff
changeset | 660 | val t' = mkabs nt args'; | 
| 13402 | 661 | val T = fastype_of t'; | 
| 13732 | 662 | val cname = extr_name s vs'; | 
| 13402 | 663 | val c = Const (cname, T); | 
| 15574 
b1d1b5bfc464
Removed practically all references to Library.foldr.
 skalberg parents: 
15570diff
changeset | 664 | val u = mkabs (list_comb (c, args')) args; | 
| 13402 | 665 | val eqn = Logic.mk_equals (c, t'); | 
| 666 | val rlz = | |
| 667 |                       Const ("realizes", fastype_of nt --> propT --> propT);
 | |
| 13732 | 668 | val lhs = app_rlz_rews [] vs' (rlz $ nt $ prop); | 
| 669 | val rhs = app_rlz_rews [] vs' (rlz $ list_comb (c, args') $ prop); | |
| 670 | val f = app_rlz_rews [] vs' | |
| 671 |                       (Abs ("x", T, rlz $ list_comb (Bound 0, args') $ prop));
 | |
| 13402 | 672 | |
| 13732 | 673 | val corr_prf' = | 
| 674 | chtype [] equal_elim_axm %> lhs %> rhs %% | |
| 675 | (chtype [propT] symmetric_axm %> rhs %> lhs %% | |
| 676 | (chtype [propT, T] combination_axm %> f %> f %> c %> t' %% | |
| 677 | (chtype [T --> propT] reflexive_axm %> f) %% | |
| 678 | PAxm (cname ^ "_def", eqn, | |
| 15531 | 679 | SOME (map TVar (term_tvars eqn))))) %% corr_prf; | 
| 13732 | 680 | val corr_prop = Reconstruct.prop_of corr_prf'; | 
| 23178 | 681 | val corr_prf'' = List.foldr forall_intr_prf | 
| 15574 
b1d1b5bfc464
Removed practically all references to Library.foldr.
 skalberg parents: 
15570diff
changeset | 682 | (proof_combt | 
| 21646 
c07b5b0e8492
thm/prf: separate official name vs. additional tags;
 wenzelm parents: 
20897diff
changeset | 683 | (PThm (corr_name s vs', corr_prf', corr_prop, | 
| 15574 
b1d1b5bfc464
Removed practically all references to Library.foldr.
 skalberg parents: 
15570diff
changeset | 684 | SOME (map TVar (term_tvars corr_prop))), vfs_of corr_prop)) | 
| 22750 | 685 | (map (get_var_type corr_prop) (vfs_of prop)); | 
| 13402 | 686 | in | 
| 13732 | 687 | ((s, (vs', ((t', u), (corr_prf', corr_prf'')))) :: defs'', | 
| 13402 | 688 | subst_TVars tye' u) | 
| 689 | end | |
| 15531 | 690 | | SOME ((_, u), _) => (defs, subst_TVars tye' u)) | 
| 691 | | SOME rs => (case find vs' rs of | |
| 692 | SOME (t, _) => (defs, subst_TVars tye' t) | |
| 693 |               | NONE => error ("extr: no realizer for instance of theorem " ^
 | |
| 16458 | 694 | quote s ^ ":\n" ^ Sign.string_of_term thy' (Envir.beta_norm | 
| 13402 | 695 | (Reconstruct.prop_of (proof_combt (prf0, ts)))))) | 
| 696 | end | |
| 697 | ||
| 15531 | 698 | | extr d defs vs ts Ts hs (prf0 as PAxm (s, prop, SOME Ts')) = | 
| 13402 | 699 | let | 
| 700 | val (vs', tye) = find_inst prop Ts ts vs; | |
| 701 | val tye' = (map fst (term_tvars prop) ~~ Ts') @ tye | |
| 702 | in | |
| 18956 | 703 | case find vs' (Symtab.lookup_list realizers s) of | 
| 15531 | 704 | SOME (t, _) => (defs, subst_TVars tye' t) | 
| 705 |             | NONE => error ("extr: no realizer for instance of axiom " ^
 | |
| 16458 | 706 | quote s ^ ":\n" ^ Sign.string_of_term thy' (Envir.beta_norm | 
| 13402 | 707 | (Reconstruct.prop_of (proof_combt (prf0, ts))))) | 
| 708 | end | |
| 709 | ||
| 710 | | extr d defs vs ts Ts hs _ = error "extr: bad proof"; | |
| 711 | ||
| 13732 | 712 | fun prep_thm (thm, vs) = | 
| 13402 | 713 | let | 
| 22596 | 714 |         val {prop, der = (_, prf), thy, ...} = rep_thm thm;
 | 
| 21646 
c07b5b0e8492
thm/prf: separate official name vs. additional tags;
 wenzelm parents: 
20897diff
changeset | 715 | val name = Thm.get_name thm; | 
| 21858 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 wenzelm parents: 
21646diff
changeset | 716 | 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 | 717 |         val _ = etype_of thy' vs [] prop <> nullT orelse error ("theorem " ^
 | 
| 13402 | 718 | quote name ^ " has no computational content") | 
| 22596 | 719 | in (Reconstruct.reconstruct_proof thy prop prf, vs) end; | 
| 13402 | 720 | |
| 15570 | 721 | val defs = Library.foldl (fn (defs, (prf, vs)) => | 
| 13732 | 722 | fst (extr 0 defs vs [] [] [] prf)) ([], map prep_thm thms); | 
| 13402 | 723 | |
| 16149 | 724 | fun add_def (s, (vs, ((t, u), (prf, _)))) thy = | 
| 16458 | 725 | (case Sign.const_type thy (extr_name s vs) of | 
| 15531 | 726 | NONE => | 
| 13732 | 727 | let | 
| 728 | val corr_prop = Reconstruct.prop_of prf; | |
| 16287 | 729 | val ft = Type.freeze t; | 
| 730 | val fu = Type.freeze u; | |
| 22750 | 731 | val (def_thms, thy') = if t = nullt then ([], thy) else | 
| 732 | thy | |
| 22796 | 733 | |> Sign.add_consts_i [(extr_name s vs, fastype_of ft, NoSyn)] | 
| 22750 | 734 | |> PureThy.add_defs_i false [((extr_name s vs ^ "_def", | 
| 735 | Logic.mk_equals (head_of (strip_abs_body fu), ft)), [])] | |
| 13732 | 736 | in | 
| 22750 | 737 | thy' | 
| 738 | |> PureThy.store_thm ((corr_name s vs, | |
| 739 | Thm.varifyT (funpow (length (term_vars corr_prop)) | |
| 740 | (forall_elim_var 0) (forall_intr_frees | |
| 741 | (ProofChecker.thm_of_proof thy' | |
| 742 | (fst (Proofterm.freeze_thaw_prf prf)))))), []) | |
| 743 | |> snd | |
| 24624 
b8383b1bbae3
distinction between regular and default code theorems
 haftmann parents: 
24219diff
changeset | 744 | |> fold Code.add_default_func def_thms | 
| 13732 | 745 | end | 
| 15531 | 746 | | SOME _ => thy); | 
| 13402 | 747 | |
| 16149 | 748 | in | 
| 749 | thy | |
| 22796 | 750 | |> Sign.absolute_path | 
| 16149 | 751 | |> fold_rev add_def defs | 
| 22796 | 752 | |> Sign.restore_naming thy | 
| 13402 | 753 | end; | 
| 754 | ||
| 755 | ||
| 756 | (**** interface ****) | |
| 757 | ||
| 17057 | 758 | structure P = OuterParse and K = OuterKeyword; | 
| 13402 | 759 | |
| 13732 | 760 | val parse_vars = Scan.optional (P.$$$ "(" |-- P.list1 P.name --| P.$$$ ")") [];
 | 
| 761 | ||
| 24867 | 762 | val _ = | 
| 13402 | 763 | OuterSyntax.command "realizers" | 
| 764 | "specify realizers for primitive axioms / theorems, together with correctness proof" | |
| 765 | K.thy_decl | |
| 13732 | 766 | (Scan.repeat1 (P.xname -- parse_vars --| P.$$$ ":" -- P.string -- P.string) >> | 
| 13402 | 767 | (fn xs => Toplevel.theory (fn thy => add_realizers | 
| 26343 
0dd2eab7b296
simplified get_thm(s): back to plain name argument;
 wenzelm parents: 
26336diff
changeset | 768 | (map (fn (((a, vs), s1), s2) => (PureThy.get_thm thy a, (vs, s1, s2))) xs) thy))); | 
| 13402 | 769 | |
| 24867 | 770 | val _ = | 
| 13402 | 771 | OuterSyntax.command "realizability" | 
| 772 | "add equations characterizing realizability" K.thy_decl | |
| 773 | (Scan.repeat1 P.string >> (Toplevel.theory o add_realizes_eqns)); | |
| 774 | ||
| 24867 | 775 | val _ = | 
| 13402 | 776 | OuterSyntax.command "extract_type" | 
| 777 | "add equations characterizing type of extracted program" K.thy_decl | |
| 778 | (Scan.repeat1 P.string >> (Toplevel.theory o add_typeof_eqns)); | |
| 779 | ||
| 24867 | 780 | val _ = | 
| 13402 | 781 | OuterSyntax.command "extract" "extract terms from proofs" K.thy_decl | 
| 26336 
a0e2b706ce73
renamed datatype thmref to Facts.ref, tuned interfaces;
 wenzelm parents: 
25389diff
changeset | 782 | (Scan.repeat1 (P.xname -- parse_vars) >> (fn xs => Toplevel.theory (fn thy => | 
| 26343 
0dd2eab7b296
simplified get_thm(s): back to plain name argument;
 wenzelm parents: 
26336diff
changeset | 783 | extract (map (apfst (PureThy.get_thm thy)) xs) thy))); | 
| 13402 | 784 | |
| 16458 | 785 | val etype_of = etype_of o add_syntax; | 
| 13714 | 786 | |
| 13402 | 787 | end; |