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