| author | paulson | 
| Mon, 09 Jan 2006 13:28:06 +0100 | |
| changeset 18623 | 9a5419d5ca01 | 
| parent 18358 | 0a733e11021a | 
| child 18708 | 4b3dadb4fe33 | 
| 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 | |
| 40 | |> Theory.root_path | |
| 41 |   |> Theory.add_types [("Type", 0, NoSyn), ("Null", 0, NoSyn)]
 | |
| 42 | |> Theory.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 = | |
| 52 |   Const ("Type", itselfT T --> Type ("Type", [])) $ Logic.mk_type T;
 | |
| 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), _), _) => | 
| 57 |             if a mem vs then TFree ("'" ^ a ^ ":" ^ string_of_int i, defaultS)
 | |
| 58 | else nullT | |
| 59 | | (Free (a, _), _) => | |
| 60 |             if a mem vs then TFree ("'" ^ a, defaultS) else nullT
 | |
| 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)
 | 
| 83 | (Pattern.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) =
 | 
| 15574 
b1d1b5bfc464
Removed practically all references to Library.foldr.
 skalberg parents: 
15570diff
changeset | 87 |   foldr add_rule {next = next, rs = rs1, net = net} (rs2 \\ rs1);
 | 
| 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 | 
| 15570 | 104 | fun ren t = getOpt (Term.rename_abs tm1 tm t, 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) | 
| 15399 
683d83051d6a
Added term cache to function condrew in order to speed up rewriting.
 berghofe parents: 
14981diff
changeset | 116 | (Net.match_term rules (Pattern.eta_contract tm))) | 
| 
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 | |
| 15574 
b1d1b5bfc464
Removed practically all references to Library.foldr.
 skalberg parents: 
15570diff
changeset | 139 | val mkabs = 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 | ||
| 15574 
b1d1b5bfc464
Removed practically all references to Library.foldr.
 skalberg parents: 
15570diff
changeset | 148 | fun relevant_vars types prop = foldr (fn | 
| 13402 | 149 | (Var ((a, i), T), vs) => (case strip_type T of | 
| 150 | (_, Type (s, _)) => if s mem types then a :: vs else vs | |
| 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 | ||
| 174 | (* data kind 'Pure/extraction' *) | |
| 175 | ||
| 16458 | 176 | structure ExtractionData = TheoryDataFun | 
| 177 | (struct | |
| 13402 | 178 | val name = "Pure/extraction"; | 
| 179 | type T = | |
| 180 |     {realizes_eqns : rules,
 | |
| 181 | typeof_eqns : rules, | |
| 13732 | 182 | types : (string * ((term -> term option) list * | 
| 183 | (term -> typ -> term -> typ -> term) option)) list, | |
| 13402 | 184 | realizers : (string list * (term * proof)) list Symtab.table, | 
| 185 | defs : thm list, | |
| 186 | expand : (string * term) list, | |
| 16458 | 187 | prep : (theory -> proof -> proof) option} | 
| 13402 | 188 | |
| 189 | val empty = | |
| 190 |     {realizes_eqns = empty_rules,
 | |
| 191 | typeof_eqns = empty_rules, | |
| 192 | types = [], | |
| 193 | realizers = Symtab.empty, | |
| 194 | defs = [], | |
| 195 | expand = [], | |
| 15531 | 196 | prep = NONE}; | 
| 13402 | 197 | val copy = I; | 
| 16458 | 198 | val extend = I; | 
| 13402 | 199 | |
| 16458 | 200 | fun merge _ | 
| 13402 | 201 |     (({realizes_eqns = realizes_eqns1, typeof_eqns = typeof_eqns1, types = types1,
 | 
| 202 | realizers = realizers1, defs = defs1, expand = expand1, prep = prep1}, | |
| 203 |       {realizes_eqns = realizes_eqns2, typeof_eqns = typeof_eqns2, types = types2,
 | |
| 204 | realizers = realizers2, defs = defs2, expand = expand2, prep = prep2}) : T * T) = | |
| 205 |     {realizes_eqns = merge_rules realizes_eqns1 realizes_eqns2,
 | |
| 206 | typeof_eqns = merge_rules typeof_eqns1 typeof_eqns2, | |
| 13732 | 207 | types = merge_alists types1 types2, | 
| 13402 | 208 | realizers = Symtab.merge_multi' (eq_set o pairself #1) | 
| 209 | (realizers1, realizers2), | |
| 210 | defs = gen_merge_lists eq_thm defs1 defs2, | |
| 211 | expand = merge_lists expand1 expand2, | |
| 15531 | 212 | prep = (case prep1 of NONE => prep2 | _ => prep1)}; | 
| 13402 | 213 | |
| 16458 | 214 | fun print _ _ = (); | 
| 215 | end); | |
| 13402 | 216 | |
| 15801 | 217 | val _ = Context.add_setup [ExtractionData.init]; | 
| 13402 | 218 | |
| 219 | fun read_condeq thy = | |
| 16458 | 220 | let val thy' = add_syntax thy | 
| 13402 | 221 | in fn s => | 
| 16458 | 222 | let val t = Logic.varify (term_of (read_cterm thy' (s, propT))) | 
| 13402 | 223 | in (map Logic.dest_equals (Logic.strip_imp_prems t), | 
| 224 | Logic.dest_equals (Logic.strip_imp_concl t)) | |
| 225 |     end handle TERM _ => error ("Not a (conditional) meta equality:\n" ^ s)
 | |
| 226 | end; | |
| 227 | ||
| 228 | (** preprocessor **) | |
| 229 | ||
| 230 | fun set_preprocessor prep thy = | |
| 231 |   let val {realizes_eqns, typeof_eqns, types, realizers,
 | |
| 232 | defs, expand, ...} = ExtractionData.get thy | |
| 233 | in | |
| 234 | ExtractionData.put | |
| 235 |       {realizes_eqns = realizes_eqns, typeof_eqns = typeof_eqns, types = types,
 | |
| 15531 | 236 | realizers = realizers, defs = defs, expand = expand, prep = SOME prep} thy | 
| 13402 | 237 | end; | 
| 238 | ||
| 239 | (** equations characterizing realizability **) | |
| 240 | ||
| 241 | fun gen_add_realizes_eqns prep_eq eqns thy = | |
| 242 |   let val {realizes_eqns, typeof_eqns, types, realizers,
 | |
| 243 | defs, expand, prep} = ExtractionData.get thy; | |
| 244 | in | |
| 245 | ExtractionData.put | |
| 15574 
b1d1b5bfc464
Removed practically all references to Library.foldr.
 skalberg parents: 
15570diff
changeset | 246 |       {realizes_eqns = foldr add_rule realizes_eqns (map (prep_eq thy) eqns),
 | 
| 13402 | 247 | typeof_eqns = typeof_eqns, types = types, realizers = realizers, | 
| 248 | defs = defs, expand = expand, prep = prep} thy | |
| 249 | end | |
| 250 | ||
| 251 | val add_realizes_eqns_i = gen_add_realizes_eqns (K I); | |
| 252 | val add_realizes_eqns = gen_add_realizes_eqns read_condeq; | |
| 253 | ||
| 254 | (** equations characterizing type of extracted program **) | |
| 255 | ||
| 256 | fun gen_add_typeof_eqns prep_eq eqns thy = | |
| 257 | let | |
| 258 |     val {realizes_eqns, typeof_eqns, types, realizers,
 | |
| 259 | defs, expand, prep} = ExtractionData.get thy; | |
| 13732 | 260 | val eqns' = map (prep_eq thy) eqns | 
| 13402 | 261 | in | 
| 262 | ExtractionData.put | |
| 263 |       {realizes_eqns = realizes_eqns, realizers = realizers,
 | |
| 15574 
b1d1b5bfc464
Removed practically all references to Library.foldr.
 skalberg parents: 
15570diff
changeset | 264 | typeof_eqns = foldr add_rule typeof_eqns eqns', | 
| 13732 | 265 | types = types, defs = defs, expand = expand, prep = prep} thy | 
| 13402 | 266 | end | 
| 267 | ||
| 268 | val add_typeof_eqns_i = gen_add_typeof_eqns (K I); | |
| 269 | val add_typeof_eqns = gen_add_typeof_eqns read_condeq; | |
| 270 | ||
| 271 | fun thaw (T as TFree (a, S)) = | |
| 16195 | 272 | if exists_string (equal ":") a then TVar (unpack_ixn a, S) else T | 
| 13402 | 273 | | thaw (Type (a, Ts)) = Type (a, map thaw Ts) | 
| 274 | | thaw T = T; | |
| 275 | ||
| 276 | fun freeze (TVar ((a, i), S)) = TFree (a ^ ":" ^ string_of_int i, S) | |
| 277 | | freeze (Type (a, Ts)) = Type (a, map freeze Ts) | |
| 278 | | freeze T = T; | |
| 279 | ||
| 280 | fun freeze_thaw f x = | |
| 281 | map_term_types thaw (f (map_term_types freeze x)); | |
| 282 | ||
| 16458 | 283 | fun etype_of thy vs Ts t = | 
| 13402 | 284 | let | 
| 16458 | 285 |     val {typeof_eqns, ...} = ExtractionData.get thy;
 | 
| 13402 | 286 |     fun err () = error ("Unable to determine type of extracted program for\n" ^
 | 
| 16458 | 287 | Sign.string_of_term thy t) | 
| 288 | in case strip_abs_body (freeze_thaw (condrew thy (#net typeof_eqns) | |
| 289 | [typeof_proc (Sign.defaultS thy) vs]) (list_abs (map (pair "x") (rev Ts), | |
| 13402 | 290 |       Const ("typeof", fastype_of1 (Ts, t) --> Type ("Type", [])) $ t))) of
 | 
| 291 |       Const ("Type", _) $ u => (Logic.dest_type u handle TERM _ => err ())
 | |
| 292 | | _ => err () | |
| 293 | end; | |
| 294 | ||
| 295 | (** realizers for axioms / theorems, together with correctness proofs **) | |
| 296 | ||
| 297 | fun gen_add_realizers prep_rlz rs thy = | |
| 298 |   let val {realizes_eqns, typeof_eqns, types, realizers,
 | |
| 299 | defs, expand, prep} = ExtractionData.get thy | |
| 300 | in | |
| 301 | ExtractionData.put | |
| 302 |       {realizes_eqns = realizes_eqns, typeof_eqns = typeof_eqns, types = types,
 | |
| 17412 | 303 | realizers = fold (Symtab.update_multi o prep_rlz thy) rs realizers, | 
| 13402 | 304 | defs = defs, expand = expand, prep = prep} thy | 
| 305 | end | |
| 306 | ||
| 307 | fun prep_realizer thy = | |
| 308 | let | |
| 13732 | 309 |     val {realizes_eqns, typeof_eqns, defs, types, ...} =
 | 
| 13402 | 310 | ExtractionData.get thy; | 
| 15570 | 311 | val procs = List.concat (map (fst o snd) types); | 
| 13732 | 312 | val rtypes = map fst types; | 
| 16800 | 313 | val eqns = Net.merge (K false) (#net realizes_eqns, #net typeof_eqns); | 
| 13402 | 314 | val thy' = add_syntax thy; | 
| 315 | val rd = ProofSyntax.read_proof thy' false | |
| 316 | in fn (thm, (vs, s1, s2)) => | |
| 317 | let | |
| 318 | val name = Thm.name_of_thm thm; | |
| 319 | val _ = assert (name <> "") "add_realizers: unnamed theorem"; | |
| 17203 | 320 | val prop = Pattern.rewrite_term thy' | 
| 13402 | 321 | (map (Logic.dest_equals o prop_of) defs) [] (prop_of thm); | 
| 322 | val vars = vars_of prop; | |
| 13732 | 323 | val vars' = filter_out (fn v => | 
| 324 | tname_of (body_type (fastype_of v)) mem rtypes) vars; | |
| 16458 | 325 | val T = etype_of thy' vs [] prop; | 
| 13402 | 326 | val (T', thw) = Type.freeze_thaw_type | 
| 13732 | 327 | (if T = nullT then nullT else map fastype_of vars' ---> T); | 
| 16458 | 328 | val t = map_term_types thw (term_of (read_cterm thy' (s1, T'))); | 
| 329 | val r' = freeze_thaw (condrew thy' eqns | |
| 330 | (procs @ [typeof_proc (Sign.defaultS thy') vs, rlz_proc])) | |
| 13402 | 331 |           (Const ("realizes", T --> propT --> propT) $
 | 
| 13732 | 332 | (if T = nullT then t else list_comb (t, vars')) $ prop); | 
| 15574 
b1d1b5bfc464
Removed practically all references to Library.foldr.
 skalberg parents: 
15570diff
changeset | 333 | val r = foldr forall_intr r' (map (get_var_type r') vars); | 
| 16458 | 334 | val prf = Reconstruct.reconstruct_proof thy' r (rd s2); | 
| 13402 | 335 | in (name, (vs, (t, prf))) end | 
| 336 | end; | |
| 337 | ||
| 338 | val add_realizers_i = gen_add_realizers | |
| 339 | (fn _ => fn (name, (vs, t, prf)) => (name, (vs, (t, prf)))); | |
| 340 | val add_realizers = gen_add_realizers prep_realizer; | |
| 341 | ||
| 13714 | 342 | fun realizes_of thy vs t prop = | 
| 343 | let | |
| 344 | val thy' = add_syntax thy; | |
| 13732 | 345 |     val {realizes_eqns, typeof_eqns, defs, types, ...} =
 | 
| 13714 | 346 | ExtractionData.get thy'; | 
| 15570 | 347 | val procs = List.concat (map (fst o snd) types); | 
| 16800 | 348 | val eqns = Net.merge (K false) (#net realizes_eqns, #net typeof_eqns); | 
| 17203 | 349 | val prop' = Pattern.rewrite_term thy' | 
| 13714 | 350 | (map (Logic.dest_equals o prop_of) defs) [] prop; | 
| 16458 | 351 | in freeze_thaw (condrew thy' eqns | 
| 352 | (procs @ [typeof_proc (Sign.defaultS thy') vs, rlz_proc])) | |
| 13714 | 353 |       (Const ("realizes", fastype_of t --> propT --> propT) $ t $ prop')
 | 
| 354 | end; | |
| 355 | ||
| 13402 | 356 | (** expanding theorems / definitions **) | 
| 357 | ||
| 358 | fun add_expand_thm (thy, thm) = | |
| 359 | let | |
| 360 |     val {realizes_eqns, typeof_eqns, types, realizers,
 | |
| 361 | defs, expand, prep} = ExtractionData.get thy; | |
| 362 | ||
| 363 | val name = Thm.name_of_thm thm; | |
| 364 | val _ = assert (name <> "") "add_expand_thms: unnamed theorem"; | |
| 365 | ||
| 366 | val is_def = | |
| 367 | (case strip_comb (fst (Logic.dest_equals (prop_of thm))) of | |
| 368 | (Const _, ts) => forall is_Var ts andalso null (duplicates ts) | |
| 16349 | 369 | andalso can (Thm.get_axiom_i thy) name | 
| 13402 | 370 | | _ => false) handle TERM _ => false; | 
| 371 | in | |
| 372 | (ExtractionData.put (if is_def then | |
| 373 |         {realizes_eqns = realizes_eqns,
 | |
| 374 | typeof_eqns = add_rule (([], | |
| 375 | Logic.dest_equals (prop_of (Drule.abs_def thm))), typeof_eqns), | |
| 376 | types = types, | |
| 377 | realizers = realizers, defs = gen_ins eq_thm (thm, defs), | |
| 378 | expand = expand, prep = prep} | |
| 379 | else | |
| 380 |         {realizes_eqns = realizes_eqns, typeof_eqns = typeof_eqns, types = types,
 | |
| 381 | realizers = realizers, defs = defs, | |
| 382 | expand = (name, prop_of thm) ins expand, prep = prep}) thy, thm) | |
| 383 | end; | |
| 384 | ||
| 15570 | 385 | fun add_expand_thms thms thy = Library.foldl (fst o add_expand_thm) (thy, thms); | 
| 13402 | 386 | |
| 15801 | 387 | |
| 13732 | 388 | (** types with computational content **) | 
| 389 | ||
| 390 | fun add_types tys thy = | |
| 391 |   let val {realizes_eqns, typeof_eqns, types, realizers,
 | |
| 392 | defs, expand, prep} = ExtractionData.get thy; | |
| 393 | in | |
| 394 | ExtractionData.put | |
| 395 |       {realizes_eqns = realizes_eqns, typeof_eqns = typeof_eqns,
 | |
| 16458 | 396 | types = map (apfst (Sign.intern_type thy)) tys @ types, | 
| 13732 | 397 | realizers = realizers, defs = defs, expand = expand, prep = prep} thy | 
| 398 | end; | |
| 399 | ||
| 13402 | 400 | |
| 15801 | 401 | (** Pure setup **) | 
| 402 | ||
| 403 | val _ = Context.add_setup | |
| 404 |   [add_types [("prop", ([], NONE))],
 | |
| 405 | ||
| 406 | add_typeof_eqns | |
| 407 | ["(typeof (PROP P)) == (Type (TYPE(Null))) ==> \ | |
| 408 |     \  (typeof (PROP Q)) == (Type (TYPE('Q))) ==>  \
 | |
| 409 |     \    (typeof (PROP P ==> PROP Q)) == (Type (TYPE('Q)))",
 | |
| 410 | ||
| 411 | "(typeof (PROP Q)) == (Type (TYPE(Null))) ==> \ | |
| 412 | \ (typeof (PROP P ==> PROP Q)) == (Type (TYPE(Null)))", | |
| 413 | ||
| 414 |       "(typeof (PROP P)) == (Type (TYPE('P))) ==>  \
 | |
| 415 |     \  (typeof (PROP Q)) == (Type (TYPE('Q))) ==>  \
 | |
| 416 |     \    (typeof (PROP P ==> PROP Q)) == (Type (TYPE('P => 'Q)))",
 | |
| 417 | ||
| 418 | "(%x. typeof (PROP P (x))) == (%x. Type (TYPE(Null))) ==> \ | |
| 419 | \ (typeof (!!x. PROP P (x))) == (Type (TYPE(Null)))", | |
| 420 | ||
| 421 |       "(%x. typeof (PROP P (x))) == (%x. Type (TYPE('P))) ==>  \
 | |
| 422 |     \    (typeof (!!x::'a. PROP P (x))) == (Type (TYPE('a => 'P)))",
 | |
| 423 | ||
| 424 |       "(%x. typeof (f (x))) == (%x. Type (TYPE('f))) ==>  \
 | |
| 425 |     \    (typeof (f)) == (Type (TYPE('f)))"],
 | |
| 426 | ||
| 427 | add_realizes_eqns | |
| 428 | ["(typeof (PROP P)) == (Type (TYPE(Null))) ==> \ | |
| 429 | \ (realizes (r) (PROP P ==> PROP Q)) == \ | |
| 430 | \ (PROP realizes (Null) (PROP P) ==> PROP realizes (r) (PROP Q))", | |
| 431 | ||
| 432 |       "(typeof (PROP P)) == (Type (TYPE('P))) ==>  \
 | |
| 433 | \ (typeof (PROP Q)) == (Type (TYPE(Null))) ==> \ | |
| 434 | \ (realizes (r) (PROP P ==> PROP Q)) == \ | |
| 435 | \ (!!x::'P. PROP realizes (x) (PROP P) ==> PROP realizes (Null) (PROP Q))", | |
| 436 | ||
| 437 | "(realizes (r) (PROP P ==> PROP Q)) == \ | |
| 438 | \ (!!x. PROP realizes (x) (PROP P) ==> PROP realizes (r (x)) (PROP Q))", | |
| 439 | ||
| 440 | "(%x. typeof (PROP P (x))) == (%x. Type (TYPE(Null))) ==> \ | |
| 441 | \ (realizes (r) (!!x. PROP P (x))) == \ | |
| 442 | \ (!!x. PROP realizes (Null) (PROP P (x)))", | |
| 443 | ||
| 444 | "(realizes (r) (!!x. PROP P (x))) == \ | |
| 445 | \ (!!x. PROP realizes (r (x)) (PROP P (x)))"], | |
| 446 | ||
| 447 | Attrib.add_attributes | |
| 448 |      [("extraction_expand",
 | |
| 449 | (Attrib.no_args add_expand_thm, K Attrib.undef_local_attribute), | |
| 450 | "specify theorems / definitions to be expanded during extraction")]]; | |
| 451 | ||
| 452 | ||
| 13402 | 453 | (**** extract program ****) | 
| 454 | ||
| 455 | val dummyt = Const ("dummy", dummyT);
 | |
| 456 | ||
| 457 | fun extract thms thy = | |
| 458 | let | |
| 16458 | 459 | val thy' = add_syntax thy; | 
| 13402 | 460 |     val {realizes_eqns, typeof_eqns, types, realizers, defs, expand, prep} =
 | 
| 461 | ExtractionData.get thy; | |
| 15570 | 462 | val procs = List.concat (map (fst o snd) types); | 
| 13732 | 463 | val rtypes = map fst types; | 
| 16458 | 464 | val typroc = typeof_proc (Sign.defaultS thy'); | 
| 465 | val prep = getOpt (prep, K I) thy' o ProofRewriteRules.elim_defs thy' false defs o | |
| 466 |       Reconstruct.expand_proof thy' (("", NONE) :: map (apsnd SOME) expand);
 | |
| 16800 | 467 | val rrews = Net.merge (K false) (#net realizes_eqns, #net typeof_eqns); | 
| 13402 | 468 | |
| 469 | fun find_inst prop Ts ts vs = | |
| 470 | let | |
| 13732 | 471 | val rvs = relevant_vars rtypes prop; | 
| 13402 | 472 | val vars = vars_of prop; | 
| 473 | val n = Int.min (length vars, length ts); | |
| 474 | ||
| 475 | fun add_args ((Var ((a, i), _), t), (vs', tye)) = | |
| 476 | if a mem rvs then | |
| 16458 | 477 | let val T = etype_of thy' vs Ts t | 
| 13402 | 478 | in if T = nullT then (vs', tye) | 
| 479 |                else (a :: vs', (("'" ^ a, i), T) :: tye)
 | |
| 480 | end | |
| 481 | else (vs', tye) | |
| 482 | ||
| 15574 
b1d1b5bfc464
Removed practically all references to Library.foldr.
 skalberg parents: 
15570diff
changeset | 483 | in foldr add_args ([], []) (Library.take (n, vars) ~~ Library.take (n, ts)) end; | 
| 13402 | 484 | |
| 15570 | 485 | fun find vs = Option.map snd o find_first (curry eq_set vs o fst); | 
| 486 | fun find' s = map snd o List.filter (equal s o fst) | |
| 13402 | 487 | |
| 13732 | 488 | fun app_rlz_rews Ts vs t = strip_abs (length Ts) (freeze_thaw | 
| 16458 | 489 | (condrew thy' rrews (procs @ [typroc vs, rlz_proc])) (list_abs | 
| 13732 | 490 | (map (pair "x") (rev Ts), t))); | 
| 491 | ||
| 492 | fun realizes_null vs prop = app_rlz_rews [] vs | |
| 493 |       (Const ("realizes", nullT --> propT --> propT) $ nullt $ prop);
 | |
| 13402 | 494 | |
| 495 | fun corr d defs vs ts Ts hs (PBound i) _ _ = (defs, PBound i) | |
| 496 | ||
| 15531 | 497 | | corr d defs vs ts Ts hs (Abst (s, SOME T, prf)) (Abst (_, _, prf')) t = | 
| 13402 | 498 | let val (defs', corr_prf) = corr d defs vs [] (T :: Ts) | 
| 499 | (dummyt :: hs) prf (incr_pboundvars 1 0 prf') | |
| 15531 | 500 | (case t of SOME (Abs (_, _, u)) => SOME u | _ => NONE) | 
| 501 | in (defs', Abst (s, SOME T, corr_prf)) end | |
| 13402 | 502 | |
| 15531 | 503 | | corr d defs vs ts Ts hs (AbsP (s, SOME prop, prf)) (AbsP (_, _, prf')) t = | 
| 13402 | 504 | let | 
| 16458 | 505 | val T = etype_of thy' vs Ts prop; | 
| 13402 | 506 | val u = if T = nullT then | 
| 15531 | 507 | (case t of SOME u => SOME (incr_boundvars 1 u) | NONE => NONE) | 
| 508 | else (case t of SOME (Abs (_, _, u)) => SOME u | _ => NONE); | |
| 13402 | 509 | val (defs', corr_prf) = corr d defs vs [] (T :: Ts) (prop :: hs) | 
| 510 | (incr_pboundvars 0 1 prf) (incr_pboundvars 0 1 prf') u; | |
| 511 |             val rlz = Const ("realizes", T --> propT --> propT)
 | |
| 512 | in (defs', | |
| 13732 | 513 |             if T = nullT then AbsP ("R",
 | 
| 15531 | 514 | SOME (app_rlz_rews Ts vs (rlz $ nullt $ prop)), | 
| 13732 | 515 | prf_subst_bounds [nullt] corr_prf) | 
| 15531 | 516 |             else Abst (s, SOME T, AbsP ("R",
 | 
| 517 | SOME (app_rlz_rews (T :: Ts) vs | |
| 13732 | 518 | (rlz $ Bound 0 $ incr_boundvars 1 prop)), corr_prf))) | 
| 13402 | 519 | end | 
| 520 | ||
| 15531 | 521 | | corr d defs vs ts Ts hs (prf % SOME t) (prf' % _) t' = | 
| 13732 | 522 | let | 
| 523 | val (Us, T) = strip_type (fastype_of1 (Ts, t)); | |
| 524 | val (defs', corr_prf) = corr d defs vs (t :: ts) Ts hs prf prf' | |
| 525 | (if tname_of T mem rtypes then t' | |
| 15531 | 526 | else (case t' of SOME (u $ _) => SOME u | _ => NONE)); | 
| 13732 | 527 | val u = if not (tname_of T mem rtypes) then t else | 
| 528 | let | |
| 16458 | 529 | val eT = etype_of thy' vs Ts t; | 
| 13732 | 530 | val (r, Us') = if eT = nullT then (nullt, Us) else | 
| 531 | (Bound (length Us), eT :: Us); | |
| 532 | val u = list_comb (incr_boundvars (length Us') t, | |
| 533 | map Bound (length Us - 1 downto 0)); | |
| 17271 | 534 | val u' = (case AList.lookup (op =) types (tname_of T) of | 
| 15531 | 535 | SOME ((_, SOME f)) => f r eT u T | 
| 13732 | 536 |                   | _ => Const ("realizes", eT --> T --> T) $ r $ u)
 | 
| 537 | in app_rlz_rews Ts vs (list_abs (map (pair "x") Us', u')) end | |
| 15531 | 538 | in (defs', corr_prf % SOME u) end | 
| 13402 | 539 | |
| 540 | | corr d defs vs ts Ts hs (prf1 %% prf2) (prf1' %% prf2') t = | |
| 541 | let | |
| 542 | val prop = Reconstruct.prop_of' hs prf2'; | |
| 16458 | 543 | val T = etype_of thy' vs Ts prop; | 
| 15531 | 544 | val (defs1, f, u) = if T = nullT then (defs, t, NONE) else | 
| 13402 | 545 | (case t of | 
| 15531 | 546 | SOME (f $ u) => (defs, SOME f, SOME u) | 
| 13402 | 547 | | _ => | 
| 548 | let val (defs1, u) = extr d defs vs [] Ts hs prf2' | |
| 15531 | 549 | in (defs1, NONE, SOME u) end) | 
| 13402 | 550 | val (defs2, corr_prf1) = corr d defs1 vs [] Ts hs prf1 prf1' f; | 
| 551 | val (defs3, corr_prf2) = corr d defs2 vs [] Ts hs prf2 prf2' u; | |
| 552 | in | |
| 553 | if T = nullT then (defs3, corr_prf1 %% corr_prf2) else | |
| 554 | (defs3, corr_prf1 % u %% corr_prf2) | |
| 555 | end | |
| 556 | ||
| 15531 | 557 | | corr d defs vs ts Ts hs (prf0 as PThm ((name, _), prf, prop, SOME Ts')) _ _ = | 
| 13402 | 558 | let | 
| 559 | val (vs', tye) = find_inst prop Ts ts vs; | |
| 560 | val tye' = (map fst (term_tvars prop) ~~ Ts') @ tye; | |
| 16458 | 561 | val T = etype_of thy' vs' [] prop; | 
| 13402 | 562 | val defs' = if T = nullT then defs | 
| 563 | else fst (extr d defs vs ts Ts hs prf0) | |
| 564 | in | |
| 13609 
73c3915553b4
Added check for axioms with "realizes Null A = A".
 berghofe parents: 
13417diff
changeset | 565 | if T = nullT andalso realizes_null vs' prop aconv prop then (defs, prf0) | 
| 17412 | 566 | else case Symtab.lookup realizers name of | 
| 15531 | 567 | NONE => (case find vs' (find' name defs') of | 
| 568 | NONE => | |
| 13402 | 569 | let | 
| 570 | val _ = assert (T = nullT) "corr: internal error"; | |
| 571 |                     val _ = msg d ("Building correctness proof for " ^ quote name ^
 | |
| 572 | (if null vs' then "" | |
| 573 | else " (relevant variables: " ^ commas_quote vs' ^ ")")); | |
| 16458 | 574 | val prf' = prep (Reconstruct.reconstruct_proof thy' prop prf); | 
| 13402 | 575 | val (defs'', corr_prf) = | 
| 15531 | 576 | corr (d + 1) defs' vs' [] [] [] prf' prf' NONE; | 
| 13732 | 577 | val corr_prop = Reconstruct.prop_of corr_prf; | 
| 15574 
b1d1b5bfc464
Removed practically all references to Library.foldr.
 skalberg parents: 
15570diff
changeset | 578 | val corr_prf' = foldr forall_intr_prf | 
| 
b1d1b5bfc464
Removed practically all references to Library.foldr.
 skalberg parents: 
15570diff
changeset | 579 | (proof_combt | 
| 13793 | 580 | (PThm ((corr_name name vs', []), corr_prf, corr_prop, | 
| 15531 | 581 | SOME (map TVar (term_tvars corr_prop))), vfs_of corr_prop)) | 
| 15574 
b1d1b5bfc464
Removed practically all references to Library.foldr.
 skalberg parents: 
15570diff
changeset | 582 | (map (get_var_type corr_prop) (vfs_of prop)) | 
| 13402 | 583 | in | 
| 13732 | 584 | ((name, (vs', ((nullt, nullt), (corr_prf, corr_prf')))) :: defs'', | 
| 13402 | 585 | prf_subst_TVars tye' corr_prf') | 
| 586 | end | |
| 15531 | 587 | | SOME (_, (_, prf')) => (defs', prf_subst_TVars tye' prf')) | 
| 588 | | SOME rs => (case find vs' rs of | |
| 589 | SOME (_, prf') => (defs', prf_subst_TVars tye' prf') | |
| 590 |               | NONE => error ("corr: no realizer for instance of theorem " ^
 | |
| 16458 | 591 | quote name ^ ":\n" ^ Sign.string_of_term thy' (Envir.beta_norm | 
| 13402 | 592 | (Reconstruct.prop_of (proof_combt (prf0, ts)))))) | 
| 593 | end | |
| 594 | ||
| 15531 | 595 | | corr d defs vs ts Ts hs (prf0 as PAxm (s, prop, SOME Ts')) _ _ = | 
| 13402 | 596 | let | 
| 597 | val (vs', tye) = find_inst prop Ts ts vs; | |
| 598 | val tye' = (map fst (term_tvars prop) ~~ Ts') @ tye | |
| 599 | in | |
| 16458 | 600 | if etype_of thy' vs' [] prop = nullT andalso | 
| 13609 
73c3915553b4
Added check for axioms with "realizes Null A = A".
 berghofe parents: 
13417diff
changeset | 601 | realizes_null vs' prop aconv prop then (defs, prf0) | 
| 17412 | 602 | else case find vs' (Symtab.lookup_multi realizers s) of | 
| 15531 | 603 | SOME (_, prf) => (defs, prf_subst_TVars tye' prf) | 
| 604 |             | NONE => error ("corr: no realizer for instance of axiom " ^
 | |
| 16458 | 605 | quote s ^ ":\n" ^ Sign.string_of_term thy' (Envir.beta_norm | 
| 13402 | 606 | (Reconstruct.prop_of (proof_combt (prf0, ts))))) | 
| 607 | end | |
| 608 | ||
| 609 | | corr d defs vs ts Ts hs _ _ _ = error "corr: bad proof" | |
| 610 | ||
| 611 | and extr d defs vs ts Ts hs (PBound i) = (defs, Bound i) | |
| 612 | ||
| 15531 | 613 | | extr d defs vs ts Ts hs (Abst (s, SOME T, prf)) = | 
| 13402 | 614 | let val (defs', t) = extr d defs vs [] | 
| 615 | (T :: Ts) (dummyt :: hs) (incr_pboundvars 1 0 prf) | |
| 616 | in (defs', Abs (s, T, t)) end | |
| 617 | ||
| 15531 | 618 | | extr d defs vs ts Ts hs (AbsP (s, SOME t, prf)) = | 
| 13402 | 619 | let | 
| 16458 | 620 | val T = etype_of thy' vs Ts t; | 
| 13402 | 621 | val (defs', t) = extr d defs vs [] (T :: Ts) (t :: hs) | 
| 622 | (incr_pboundvars 0 1 prf) | |
| 623 | in (defs', | |
| 624 | if T = nullT then subst_bound (nullt, t) else Abs (s, T, t)) | |
| 625 | end | |
| 626 | ||
| 15531 | 627 | | extr d defs vs ts Ts hs (prf % SOME t) = | 
| 13402 | 628 | let val (defs', u) = extr d defs vs (t :: ts) Ts hs prf | 
| 13732 | 629 | in (defs', | 
| 630 | if tname_of (body_type (fastype_of1 (Ts, t))) mem rtypes then u | |
| 631 | else u $ t) | |
| 632 | end | |
| 13402 | 633 | |
| 634 | | extr d defs vs ts Ts hs (prf1 %% prf2) = | |
| 635 | let | |
| 636 | val (defs', f) = extr d defs vs [] Ts hs prf1; | |
| 637 | val prop = Reconstruct.prop_of' hs prf2; | |
| 16458 | 638 | val T = etype_of thy' vs Ts prop | 
| 13402 | 639 | in | 
| 640 | if T = nullT then (defs', f) else | |
| 641 | let val (defs'', t) = extr d defs' vs [] Ts hs prf2 | |
| 642 | in (defs'', f $ t) end | |
| 643 | end | |
| 644 | ||
| 15531 | 645 | | extr d defs vs ts Ts hs (prf0 as PThm ((s, _), prf, prop, SOME Ts')) = | 
| 13402 | 646 | let | 
| 647 | val (vs', tye) = find_inst prop Ts ts vs; | |
| 648 | val tye' = (map fst (term_tvars prop) ~~ Ts') @ tye | |
| 649 | in | |
| 17412 | 650 | case Symtab.lookup realizers s of | 
| 15531 | 651 | NONE => (case find vs' (find' s defs) of | 
| 652 | NONE => | |
| 13402 | 653 | let | 
| 654 |                     val _ = msg d ("Extracting " ^ quote s ^
 | |
| 655 | (if null vs' then "" | |
| 656 | else " (relevant variables: " ^ commas_quote vs' ^ ")")); | |
| 16458 | 657 | val prf' = prep (Reconstruct.reconstruct_proof thy' prop prf); | 
| 13402 | 658 | val (defs', t) = extr (d + 1) defs vs' [] [] [] prf'; | 
| 659 | val (defs'', corr_prf) = | |
| 15531 | 660 | corr (d + 1) defs' vs' [] [] [] prf' prf' (SOME t); | 
| 13402 | 661 | |
| 662 | val nt = Envir.beta_norm t; | |
| 13732 | 663 | val args = filter_out (fn v => tname_of (body_type | 
| 664 | (fastype_of v)) mem rtypes) (vfs_of prop); | |
| 15570 | 665 | val args' = List.filter (fn v => Logic.occs (v, nt)) args; | 
| 15574 
b1d1b5bfc464
Removed practically all references to Library.foldr.
 skalberg parents: 
15570diff
changeset | 666 | val t' = mkabs nt args'; | 
| 13402 | 667 | val T = fastype_of t'; | 
| 13732 | 668 | val cname = extr_name s vs'; | 
| 13402 | 669 | val c = Const (cname, T); | 
| 15574 
b1d1b5bfc464
Removed practically all references to Library.foldr.
 skalberg parents: 
15570diff
changeset | 670 | val u = mkabs (list_comb (c, args')) args; | 
| 13402 | 671 | val eqn = Logic.mk_equals (c, t'); | 
| 672 | val rlz = | |
| 673 |                       Const ("realizes", fastype_of nt --> propT --> propT);
 | |
| 13732 | 674 | val lhs = app_rlz_rews [] vs' (rlz $ nt $ prop); | 
| 675 | val rhs = app_rlz_rews [] vs' (rlz $ list_comb (c, args') $ prop); | |
| 676 | val f = app_rlz_rews [] vs' | |
| 677 |                       (Abs ("x", T, rlz $ list_comb (Bound 0, args') $ prop));
 | |
| 13402 | 678 | |
| 13732 | 679 | val corr_prf' = | 
| 680 | chtype [] equal_elim_axm %> lhs %> rhs %% | |
| 681 | (chtype [propT] symmetric_axm %> rhs %> lhs %% | |
| 682 | (chtype [propT, T] combination_axm %> f %> f %> c %> t' %% | |
| 683 | (chtype [T --> propT] reflexive_axm %> f) %% | |
| 684 | PAxm (cname ^ "_def", eqn, | |
| 15531 | 685 | SOME (map TVar (term_tvars eqn))))) %% corr_prf; | 
| 13732 | 686 | val corr_prop = Reconstruct.prop_of corr_prf'; | 
| 15574 
b1d1b5bfc464
Removed practically all references to Library.foldr.
 skalberg parents: 
15570diff
changeset | 687 | val corr_prf'' = foldr forall_intr_prf | 
| 
b1d1b5bfc464
Removed practically all references to Library.foldr.
 skalberg parents: 
15570diff
changeset | 688 | (proof_combt | 
| 13732 | 689 | (PThm ((corr_name s vs', []), corr_prf', corr_prop, | 
| 15574 
b1d1b5bfc464
Removed practically all references to Library.foldr.
 skalberg parents: 
15570diff
changeset | 690 | SOME (map TVar (term_tvars corr_prop))), vfs_of corr_prop)) | 
| 
b1d1b5bfc464
Removed practically all references to Library.foldr.
 skalberg parents: 
15570diff
changeset | 691 | (map (get_var_type corr_prop) (vfs_of prop)); | 
| 13402 | 692 | in | 
| 13732 | 693 | ((s, (vs', ((t', u), (corr_prf', corr_prf'')))) :: defs'', | 
| 13402 | 694 | subst_TVars tye' u) | 
| 695 | end | |
| 15531 | 696 | | SOME ((_, u), _) => (defs, subst_TVars tye' u)) | 
| 697 | | SOME rs => (case find vs' rs of | |
| 698 | SOME (t, _) => (defs, subst_TVars tye' t) | |
| 699 |               | NONE => error ("extr: no realizer for instance of theorem " ^
 | |
| 16458 | 700 | quote s ^ ":\n" ^ Sign.string_of_term thy' (Envir.beta_norm | 
| 13402 | 701 | (Reconstruct.prop_of (proof_combt (prf0, ts)))))) | 
| 702 | end | |
| 703 | ||
| 15531 | 704 | | extr d defs vs ts Ts hs (prf0 as PAxm (s, prop, SOME Ts')) = | 
| 13402 | 705 | let | 
| 706 | val (vs', tye) = find_inst prop Ts ts vs; | |
| 707 | val tye' = (map fst (term_tvars prop) ~~ Ts') @ tye | |
| 708 | in | |
| 17412 | 709 | case find vs' (Symtab.lookup_multi realizers s) of | 
| 15531 | 710 | SOME (t, _) => (defs, subst_TVars tye' t) | 
| 711 |             | NONE => error ("extr: no realizer for instance of axiom " ^
 | |
| 16458 | 712 | quote s ^ ":\n" ^ Sign.string_of_term thy' (Envir.beta_norm | 
| 13402 | 713 | (Reconstruct.prop_of (proof_combt (prf0, ts))))) | 
| 714 | end | |
| 715 | ||
| 716 | | extr d defs vs ts Ts hs _ = error "extr: bad proof"; | |
| 717 | ||
| 13732 | 718 | fun prep_thm (thm, vs) = | 
| 13402 | 719 | let | 
| 720 |         val {prop, der = (_, prf), sign, ...} = rep_thm thm;
 | |
| 721 | val name = Thm.name_of_thm thm; | |
| 722 | val _ = assert (name <> "") "extraction: unnamed theorem"; | |
| 16458 | 723 |         val _ = assert (etype_of thy' vs [] prop <> nullT) ("theorem " ^
 | 
| 13402 | 724 | quote name ^ " has no computational content") | 
| 13732 | 725 | in (Reconstruct.reconstruct_proof sign prop prf, vs) end; | 
| 13402 | 726 | |
| 15570 | 727 | val defs = Library.foldl (fn (defs, (prf, vs)) => | 
| 13732 | 728 | fst (extr 0 defs vs [] [] [] prf)) ([], map prep_thm thms); | 
| 13402 | 729 | |
| 16149 | 730 | fun add_def (s, (vs, ((t, u), (prf, _)))) thy = | 
| 16458 | 731 | (case Sign.const_type thy (extr_name s vs) of | 
| 15531 | 732 | NONE => | 
| 13732 | 733 | let | 
| 734 | val corr_prop = Reconstruct.prop_of prf; | |
| 16287 | 735 | val ft = Type.freeze t; | 
| 736 | val fu = Type.freeze u; | |
| 13732 | 737 | val thy' = if t = nullt then thy else thy |> | 
| 738 | Theory.add_consts_i [(extr_name s vs, fastype_of ft, NoSyn)] |> | |
| 18358 | 739 | snd o PureThy.add_defs_i false [((extr_name s vs ^ "_def", | 
| 13732 | 740 | Logic.mk_equals (head_of (strip_abs_body fu), ft)), [])]; | 
| 741 | in | |
| 18358 | 742 | snd (PureThy.store_thm ((corr_name s vs, | 
| 13732 | 743 | Thm.varifyT (funpow (length (term_vars corr_prop)) | 
| 744 | (forall_elim_var 0) (forall_intr_frees | |
| 745 | (ProofChecker.thm_of_proof thy' | |
| 746 | (fst (Proofterm.freeze_thaw_prf prf)))))), []) thy') | |
| 747 | end | |
| 15531 | 748 | | SOME _ => thy); | 
| 13402 | 749 | |
| 16149 | 750 | in | 
| 751 | thy | |
| 752 | |> Theory.absolute_path | |
| 753 | |> fold_rev add_def defs | |
| 754 | |> Theory.restore_naming thy | |
| 13402 | 755 | end; | 
| 756 | ||
| 757 | ||
| 758 | (**** interface ****) | |
| 759 | ||
| 17057 | 760 | structure P = OuterParse and K = OuterKeyword; | 
| 13402 | 761 | |
| 13732 | 762 | val parse_vars = Scan.optional (P.$$$ "(" |-- P.list1 P.name --| P.$$$ ")") [];
 | 
| 763 | ||
| 13402 | 764 | val realizersP = | 
| 765 | OuterSyntax.command "realizers" | |
| 766 | "specify realizers for primitive axioms / theorems, together with correctness proof" | |
| 767 | K.thy_decl | |
| 13732 | 768 | (Scan.repeat1 (P.xname -- parse_vars --| P.$$$ ":" -- P.string -- P.string) >> | 
| 13402 | 769 | (fn xs => Toplevel.theory (fn thy => add_realizers | 
| 770 | (map (fn (((a, vs), s1), s2) => | |
| 16486 | 771 | (PureThy.get_thm thy (Name a), (vs, s1, s2))) xs) thy))); | 
| 13402 | 772 | |
| 773 | val realizabilityP = | |
| 774 | OuterSyntax.command "realizability" | |
| 775 | "add equations characterizing realizability" K.thy_decl | |
| 776 | (Scan.repeat1 P.string >> (Toplevel.theory o add_realizes_eqns)); | |
| 777 | ||
| 778 | val typeofP = | |
| 779 | OuterSyntax.command "extract_type" | |
| 780 | "add equations characterizing type of extracted program" K.thy_decl | |
| 781 | (Scan.repeat1 P.string >> (Toplevel.theory o add_typeof_eqns)); | |
| 782 | ||
| 783 | val extractP = | |
| 784 | OuterSyntax.command "extract" "extract terms from proofs" K.thy_decl | |
| 13732 | 785 | (Scan.repeat1 (P.xname -- parse_vars) >> (fn xs => Toplevel.theory | 
| 16486 | 786 | (fn thy => extract (map (apfst (PureThy.get_thm thy o Name)) xs) thy))); | 
| 13402 | 787 | |
| 15801 | 788 | val _ = OuterSyntax.add_parsers [realizersP, realizabilityP, typeofP, extractP]; | 
| 13402 | 789 | |
| 16458 | 790 | val etype_of = etype_of o add_syntax; | 
| 13714 | 791 | |
| 13402 | 792 | end; |