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