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