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