9 val set_preprocessor : (theory -> Proofterm.proof -> Proofterm.proof) -> theory -> theory |
9 val set_preprocessor : (theory -> Proofterm.proof -> Proofterm.proof) -> theory -> theory |
10 val add_realizes_eqns_i : ((term * term) list * (term * term)) list -> theory -> theory |
10 val add_realizes_eqns_i : ((term * term) list * (term * term)) list -> theory -> theory |
11 val add_realizes_eqns : string list -> theory -> theory |
11 val add_realizes_eqns : string list -> theory -> theory |
12 val add_typeof_eqns_i : ((term * term) list * (term * term)) list -> theory -> theory |
12 val add_typeof_eqns_i : ((term * term) list * (term * term)) list -> theory -> theory |
13 val add_typeof_eqns : string list -> theory -> theory |
13 val add_typeof_eqns : string list -> theory -> theory |
14 val add_realizers_i : (string * (string list * term * Proofterm.proof)) list |
14 val add_realizers_i : (Thm_Name.T * (string list * term * Proofterm.proof)) list |
15 -> theory -> theory |
15 -> theory -> theory |
16 val add_realizers : (thm * (string list * string * string)) list |
16 val add_realizers : (thm * (string list * string * string)) list |
17 -> theory -> theory |
17 -> theory -> theory |
18 val add_expand_thm : bool -> thm -> theory -> theory |
18 val add_expand_thm : bool -> thm -> theory -> theory |
19 val add_types : (xstring * ((term -> term option) list * |
19 val add_types : (xstring * ((term -> term option) list * |
116 |
116 |
117 in rew end; |
117 in rew end; |
118 |
118 |
119 val change_types = Proofterm.change_types o SOME; |
119 val change_types = Proofterm.change_types o SOME; |
120 |
120 |
121 fun extr_name s vs = Long_Name.append "extr" (space_implode "_" (s :: vs)); |
121 fun extr_name ((name, i): Thm_Name.T) vs = |
122 fun corr_name s vs = extr_name s vs ^ "_correctness"; |
122 (Long_Name.append "extr" (space_implode "_" (name :: vs)), i); |
|
123 |
|
124 fun corr_name thm_name vs = |
|
125 extr_name thm_name vs |>> suffix "_correctness"; |
123 |
126 |
124 fun msg d s = writeln (Symbol.spaces d ^ s); |
127 fun msg d s = writeln (Symbol.spaces d ^ s); |
125 |
128 |
126 fun vars_of t = map Var (build_rev (Term.add_vars t)); |
129 fun vars_of t = map Var (build_rev (Term.add_vars t)); |
127 fun frees_of t = map Free (build_rev (Term.add_frees t)); |
130 fun frees_of t = map Free (build_rev (Term.add_frees t)); |
200 type T = |
203 type T = |
201 {realizes_eqns : rules, |
204 {realizes_eqns : rules, |
202 typeof_eqns : rules, |
205 typeof_eqns : rules, |
203 types : (string * ((term -> term option) list * |
206 types : (string * ((term -> term option) list * |
204 (term -> typ -> term -> typ -> term) option)) list, |
207 (term -> typ -> term -> typ -> term) option)) list, |
205 realizers : (string list * (term * proof)) list Symtab.table, |
208 realizers : (string list * (term * proof)) list Thm_Name.Table.table, |
206 defs : thm list, |
209 defs : thm list, |
207 expand : string list, |
210 expand : Thm_Name.T list, |
208 prep : (theory -> proof -> proof) option} |
211 prep : (theory -> proof -> proof) option} |
209 |
212 |
210 val empty = |
213 val empty = |
211 {realizes_eqns = empty_rules, |
214 {realizes_eqns = empty_rules, |
212 typeof_eqns = empty_rules, |
215 typeof_eqns = empty_rules, |
213 types = [], |
216 types = [], |
214 realizers = Symtab.empty, |
217 realizers = Thm_Name.Table.empty, |
215 defs = [], |
218 defs = [], |
216 expand = [], |
219 expand = [], |
217 prep = NONE}; |
220 prep = NONE}; |
218 |
221 |
219 fun merge |
222 fun merge |
222 {realizes_eqns = realizes_eqns2, typeof_eqns = typeof_eqns2, types = types2, |
225 {realizes_eqns = realizes_eqns2, typeof_eqns = typeof_eqns2, types = types2, |
223 realizers = realizers2, defs = defs2, expand = expand2, prep = prep2}) : T = |
226 realizers = realizers2, defs = defs2, expand = expand2, prep = prep2}) : T = |
224 {realizes_eqns = merge_rules realizes_eqns1 realizes_eqns2, |
227 {realizes_eqns = merge_rules realizes_eqns1 realizes_eqns2, |
225 typeof_eqns = merge_rules typeof_eqns1 typeof_eqns2, |
228 typeof_eqns = merge_rules typeof_eqns1 typeof_eqns2, |
226 types = AList.merge (op =) (K true) (types1, types2), |
229 types = AList.merge (op =) (K true) (types1, types2), |
227 realizers = Symtab.merge_list (eq_set (op =) o apply2 #1) (realizers1, realizers2), |
230 realizers = Thm_Name.Table.merge_list (eq_set (op =) o apply2 #1) (realizers1, realizers2), |
228 defs = Library.merge Thm.eq_thm (defs1, defs2), |
231 defs = Library.merge Thm.eq_thm (defs1, defs2), |
229 expand = Library.merge (op =) (expand1, expand2), |
232 expand = Library.merge (op =) (expand1, expand2), |
230 prep = if is_some prep1 then prep1 else prep2}; |
233 prep = if is_some prep1 then prep1 else prep2}; |
231 ); |
234 ); |
232 |
235 |
317 let val {realizes_eqns, typeof_eqns, types, realizers, |
320 let val {realizes_eqns, typeof_eqns, types, realizers, |
318 defs, expand, prep} = ExtractionData.get thy |
321 defs, expand, prep} = ExtractionData.get thy |
319 in |
322 in |
320 ExtractionData.put |
323 ExtractionData.put |
321 {realizes_eqns = realizes_eqns, typeof_eqns = typeof_eqns, types = types, |
324 {realizes_eqns = realizes_eqns, typeof_eqns = typeof_eqns, types = types, |
322 realizers = fold (Symtab.cons_list o prep_rlz thy) rs realizers, |
325 realizers = fold (Thm_Name.Table.cons_list o prep_rlz thy) rs realizers, |
323 defs = defs, expand = expand, prep = prep} thy |
326 defs = defs, expand = expand, prep = prep} thy |
324 end |
327 end |
325 |
328 |
326 fun prep_realizer thy = |
329 fun prep_realizer thy = |
327 let |
330 let |
333 val thy' = add_syntax thy; |
336 val thy' = add_syntax thy; |
334 val ctxt' = Proof_Context.init_global thy'; |
337 val ctxt' = Proof_Context.init_global thy'; |
335 val rd = Proof_Syntax.read_proof thy' true false; |
338 val rd = Proof_Syntax.read_proof thy' true false; |
336 in fn (thm, (vs, s1, s2)) => |
339 in fn (thm, (vs, s1, s2)) => |
337 let |
340 let |
338 val name = Thm.derivation_name thm; |
341 val thm_name = Thm.derivation_name thm; |
339 val _ = name <> "" orelse error "add_realizers: unnamed theorem"; |
342 val _ = if Thm_Name.is_empty thm_name then error "add_realizers: unnamed theorem" else (); |
340 val prop = Thm.unconstrainT thm |> Thm.prop_of |> |
343 val prop = Thm.unconstrainT thm |> Thm.prop_of |> |
341 Pattern.rewrite_term thy' (map (Logic.dest_equals o Thm.prop_of) defs) []; |
344 Pattern.rewrite_term thy' (map (Logic.dest_equals o Thm.prop_of) defs) []; |
342 val vars = vars_of prop; |
345 val vars = vars_of prop; |
343 val vars' = filter_out (fn v => |
346 val vars' = filter_out (fn v => |
344 member (op =) rtypes (tname_of (body_type (fastype_of v)))) vars; |
347 member (op =) rtypes (tname_of (body_type (fastype_of v)))) vars; |
355 (Const ("realizes", T --> propT --> propT) $ |
358 (Const ("realizes", T --> propT --> propT) $ |
356 (if T = nullT then t else list_comb (t, vars')) $ prop); |
359 (if T = nullT then t else list_comb (t, vars')) $ prop); |
357 val r = Logic.list_implies (shyps, |
360 val r = Logic.list_implies (shyps, |
358 fold_rev Logic.all (map (get_var_type r') vars) r'); |
361 fold_rev Logic.all (map (get_var_type r') vars) r'); |
359 val prf = Proofterm.reconstruct_proof thy' r (rd s2); |
362 val prf = Proofterm.reconstruct_proof thy' r (rd s2); |
360 in (name, (vs, (t, prf))) end |
363 in (thm_name, (vs, (t, prf))) end |
361 end; |
364 end; |
362 |
365 |
363 val add_realizers_i = gen_add_realizers |
366 val add_realizers_i = gen_add_realizers |
364 (fn _ => fn (name, (vs, t, prf)) => (name, (vs, (t, prf)))); |
367 (fn _ => fn (name, (vs, t, prf)) => (name, (vs, (t, prf)))); |
365 val add_realizers = gen_add_realizers prep_realizer; |
368 val add_realizers = gen_add_realizers prep_realizer; |
409 fun add_expand_thm is_def thm thy = |
412 fun add_expand_thm is_def thm thy = |
410 let |
413 let |
411 val {realizes_eqns, typeof_eqns, types, realizers, |
414 val {realizes_eqns, typeof_eqns, types, realizers, |
412 defs, expand, prep} = ExtractionData.get thy; |
415 defs, expand, prep} = ExtractionData.get thy; |
413 |
416 |
414 val name = Thm.derivation_name thm; |
417 val thm_name = Thm.derivation_name thm; |
415 val _ = name <> "" orelse error "add_expand_thm: unnamed theorem"; |
418 val _ = if Thm_Name.is_empty thm_name then error "add_expand_thm: unnamed theorem" else (); |
416 in |
419 in |
417 thy |> ExtractionData.put |
420 thy |> ExtractionData.put |
418 (if is_def then |
421 (if is_def then |
419 {realizes_eqns = realizes_eqns, |
422 {realizes_eqns = realizes_eqns, |
420 typeof_eqns = typeof_eqns |
423 typeof_eqns = typeof_eqns |
423 realizers = realizers, defs = insert Thm.eq_thm_prop (Thm.trim_context thm) defs, |
426 realizers = realizers, defs = insert Thm.eq_thm_prop (Thm.trim_context thm) defs, |
424 expand = expand, prep = prep} |
427 expand = expand, prep = prep} |
425 else |
428 else |
426 {realizes_eqns = realizes_eqns, typeof_eqns = typeof_eqns, types = types, |
429 {realizes_eqns = realizes_eqns, typeof_eqns = typeof_eqns, types = types, |
427 realizers = realizers, defs = defs, |
430 realizers = realizers, defs = defs, |
428 expand = insert (op =) name expand, prep = prep}) |
431 expand = insert (op =) thm_name expand, prep = prep}) |
429 end; |
432 end; |
430 |
433 |
431 fun extraction_expand is_def = |
434 fun extraction_expand is_def = |
432 Thm.declaration_attribute (fn th => Context.mapping (add_expand_thm is_def th) I); |
435 Thm.declaration_attribute (fn th => Context.mapping (add_expand_thm is_def th) I); |
433 |
436 |
506 val {realizes_eqns, typeof_eqns, types, realizers, defs, expand, prep} = |
509 val {realizes_eqns, typeof_eqns, types, realizers, defs, expand, prep} = |
507 ExtractionData.get thy; |
510 ExtractionData.get thy; |
508 val procs = maps (rev o fst o snd) types; |
511 val procs = maps (rev o fst o snd) types; |
509 val rtypes = map fst types; |
512 val rtypes = map fst types; |
510 val typroc = typeof_proc []; |
513 val typroc = typeof_proc []; |
511 fun expand_name ({name, ...}: Proofterm.thm_header) = |
514 fun expand_name ({thm_name, ...}: Proofterm.thm_header) = |
512 if name = "" orelse member (op =) expand name then SOME "" else NONE; |
515 if Thm_Name.is_empty thm_name orelse member (op =) expand thm_name |
|
516 then SOME Thm_Name.empty else NONE; |
513 val prep = the_default (K I) prep thy' o |
517 val prep = the_default (K I) prep thy' o |
514 Proof_Rewrite_Rules.elim_defs thy' false (map (Thm.transfer thy) defs) o |
518 Proof_Rewrite_Rules.elim_defs thy' false (map (Thm.transfer thy) defs) o |
515 Proofterm.expand_proof thy' expand_name; |
519 Proofterm.expand_proof thy' expand_name; |
516 val rrews = Net.merge (K false) (#net realizes_eqns, #net typeof_eqns); |
520 val rrews = Net.merge (K false) (#net realizes_eqns, #net typeof_eqns); |
517 |
521 |
537 fun mk_sprfs cs tye = maps (fn (_, T) => |
541 fun mk_sprfs cs tye = maps (fn (_, T) => |
538 Proof_Rewrite_Rules.expand_of_sort_proof thy' (map SOME cs) |
542 Proof_Rewrite_Rules.expand_of_sort_proof thy' (map SOME cs) |
539 (T, Sign.defaultS thy)) tye; |
543 (T, Sign.defaultS thy)) tye; |
540 |
544 |
541 fun find (vs: string list) = Option.map snd o find_first (curry (eq_set (op =)) vs o fst); |
545 fun find (vs: string list) = Option.map snd o find_first (curry (eq_set (op =)) vs o fst); |
542 fun find' (s: string) = map_filter (fn (s', x) => if s = s' then SOME x else NONE); |
546 fun filter_thm_name (a: Thm_Name.T) = |
|
547 map_filter (fn (b, x) => if a = b then SOME x else NONE); |
543 |
548 |
544 fun app_rlz_rews Ts vs t = |
549 fun app_rlz_rews Ts vs t = |
545 strip_abs (length Ts) |
550 strip_abs (length Ts) |
546 (freeze_thaw (condrew thy' rrews (procs @ [typroc vs, rlz_proc])) |
551 (freeze_thaw (condrew thy' rrews (procs @ [typroc vs, rlz_proc])) |
547 (fold (Term.abs o pair "x") Ts t)); |
552 (fold (Term.abs o pair "x") Ts t)); |
616 (corr_prf1 % u %% corr_prf2, defs2) |
621 (corr_prf1 % u %% corr_prf2, defs2) |
617 end |
622 end |
618 |
623 |
619 | corr d vs ts Ts hs cs _ (prf0 as PThm (thm_header as {types = SOME Ts', ...}, thm_body)) _ defs = |
624 | corr d vs ts Ts hs cs _ (prf0 as PThm (thm_header as {types = SOME Ts', ...}, thm_body)) _ defs = |
620 let |
625 let |
621 val {pos, theory_name, name, prop, ...} = thm_header; |
626 val {pos, theory_name, thm_name, prop, ...} = thm_header; |
622 val prf = Proofterm.thm_body_proof_open thm_body; |
627 val prf = Proofterm.thm_body_proof_open thm_body; |
623 val (vs', tye) = find_inst prop Ts ts vs; |
628 val (vs', tye) = find_inst prop Ts ts vs; |
624 val shyps = mk_shyps tye; |
629 val shyps = mk_shyps tye; |
625 val sprfs = mk_sprfs cs tye; |
630 val sprfs = mk_sprfs cs tye; |
626 val tye' = (map fst (Term.add_tvars prop [] |> rev) ~~ Ts') @ tye; |
631 val tye' = (map fst (Term.add_tvars prop [] |> rev) ~~ Ts') @ tye; |
627 val T = etype_of thy' vs' [] prop; |
632 val T = etype_of thy' vs' [] prop; |
628 val defs' = if T = nullT then defs |
633 val defs' = if T = nullT then defs |
629 else snd (extr d vs ts Ts hs prf0 defs) |
634 else snd (extr d vs ts Ts hs prf0 defs) |
630 in |
635 in |
631 if T = nullT andalso realizes_null vs' prop aconv prop then (prf0, defs) |
636 if T = nullT andalso realizes_null vs' prop aconv prop then (prf0, defs) |
632 else (case Symtab.lookup realizers name of |
637 else (case Thm_Name.Table.lookup realizers thm_name of |
633 NONE => (case find vs' (find' name defs') of |
638 NONE => (case find vs' (filter_thm_name thm_name defs') of |
634 NONE => |
639 NONE => |
635 let |
640 let |
636 val _ = T = nullT orelse error "corr: internal error"; |
641 val _ = T = nullT orelse error "corr: internal error"; |
637 val _ = msg d ("Building correctness proof for " ^ quote name ^ |
642 val _ = |
638 (if null vs' then "" |
643 msg d ("Building correctness proof for " ^ |
639 else " (relevant variables: " ^ commas_quote vs' ^ ")")); |
644 quote (Thm_Name.short thm_name) ^ |
|
645 (if null vs' then "" |
|
646 else " (relevant variables: " ^ commas_quote vs' ^ ")")); |
640 val prf' = prep (Proofterm.reconstruct_proof thy' prop prf); |
647 val prf' = prep (Proofterm.reconstruct_proof thy' prop prf); |
641 val (corr_prf0, defs'') = corr (d + 1) vs' [] [] [] |
648 val (corr_prf0, defs'') = corr (d + 1) vs' [] [] [] |
642 (rev shyps) NONE prf' prf' defs'; |
649 (rev shyps) NONE prf' prf' defs'; |
643 val corr_prf = mkabsp shyps corr_prf0; |
650 val corr_prf = mkabsp shyps corr_prf0; |
644 val corr_prop = Proofterm.prop_of corr_prf; |
651 val corr_prop = Proofterm.prop_of corr_prf; |
645 val corr_header = |
652 val corr_header = |
646 Proofterm.thm_header (serial ()) pos theory_name |
653 Proofterm.thm_header (serial ()) pos theory_name |
647 (corr_name name vs') corr_prop |
654 (corr_name thm_name vs') corr_prop |
648 (SOME (map TVar (Term.add_tvars corr_prop [] |> rev))); |
655 (SOME (map TVar (Term.add_tvars corr_prop [] |> rev))); |
649 val corr_prf' = |
656 val corr_prf' = |
650 Proofterm.proof_combP |
657 Proofterm.proof_combP |
651 (Proofterm.proof_combt |
658 (Proofterm.proof_combt |
652 (PThm (corr_header, make_proof_body corr_prf), vfs_of corr_prop), |
659 (PThm (corr_header, make_proof_body corr_prf), vfs_of corr_prop), |
654 fold_rev Proofterm.forall_intr_proof' |
661 fold_rev Proofterm.forall_intr_proof' |
655 (map (get_var_type corr_prop) (vfs_of prop)) |> |
662 (map (get_var_type corr_prop) (vfs_of prop)) |> |
656 mkabsp shyps |
663 mkabsp shyps |
657 in |
664 in |
658 (Proofterm.proof_combP (prf_subst_TVars tye' corr_prf', sprfs), |
665 (Proofterm.proof_combP (prf_subst_TVars tye' corr_prf', sprfs), |
659 (name, (vs', ((nullt, nullt), (corr_prf, corr_prf')))) :: defs'') |
666 (thm_name, (vs', ((nullt, nullt), (corr_prf, corr_prf')))) :: defs'') |
660 end |
667 end |
661 | SOME (_, (_, prf')) => |
668 | SOME (_, (_, prf')) => |
662 (Proofterm.proof_combP (prf_subst_TVars tye' prf', sprfs), defs')) |
669 (Proofterm.proof_combP (prf_subst_TVars tye' prf', sprfs), defs')) |
663 | SOME rs => (case find vs' rs of |
670 | SOME rs => (case find vs' rs of |
664 SOME (_, prf') => (Proofterm.proof_combP (prf_subst_TVars tye' prf', sprfs), defs') |
671 SOME (_, prf') => (Proofterm.proof_combP (prf_subst_TVars tye' prf', sprfs), defs') |
665 | NONE => error ("corr: no realizer for instance of theorem " ^ |
672 | NONE => error ("corr: no realizer for instance of theorem " ^ |
666 quote name ^ ":\n" ^ Syntax.string_of_term_global thy' (Envir.beta_norm |
673 quote (Thm_Name.short thm_name) ^ ":\n" ^ |
667 (Proofterm.prop_of (Proofterm.proof_combt (prf0, ts))))))) |
674 Syntax.string_of_term_global thy' |
|
675 (Envir.beta_norm (Proofterm.prop_of (Proofterm.proof_combt (prf0, ts))))))) |
668 end |
676 end |
669 |
677 |
670 | corr d vs ts Ts hs cs _ (prf0 as PAxm (s, prop, SOME Ts')) _ defs = |
678 | corr d vs ts Ts hs cs _ (prf0 as PAxm (s, prop, SOME Ts')) _ defs = |
671 let |
679 let |
672 val (vs', tye) = find_inst prop Ts ts vs; |
680 val (vs', tye) = find_inst prop Ts ts vs; |
673 val tye' = (map fst (Term.add_tvars prop [] |> rev) ~~ Ts') @ tye |
681 val tye' = (map fst (Term.add_tvars prop [] |> rev) ~~ Ts') @ tye |
674 in |
682 in |
675 if etype_of thy' vs' [] prop = nullT andalso |
683 if etype_of thy' vs' [] prop = nullT andalso |
676 realizes_null vs' prop aconv prop then (prf0, defs) |
684 realizes_null vs' prop aconv prop then (prf0, defs) |
677 else case find vs' (Symtab.lookup_list realizers s) of |
685 else case find vs' (Thm_Name.Table.lookup_list realizers (s, 0)) of |
678 SOME (_, prf) => (Proofterm.proof_combP (prf_subst_TVars tye' prf, mk_sprfs cs tye), |
686 SOME (_, prf) => (Proofterm.proof_combP (prf_subst_TVars tye' prf, mk_sprfs cs tye), |
679 defs) |
687 defs) |
680 | NONE => error ("corr: no realizer for instance of axiom " ^ |
688 | NONE => error ("corr: no realizer for instance of axiom " ^ |
681 quote s ^ ":\n" ^ Syntax.string_of_term_global thy' (Envir.beta_norm |
689 quote s ^ ":\n" ^ Syntax.string_of_term_global thy' (Envir.beta_norm |
682 (Proofterm.prop_of (Proofterm.proof_combt (prf0, ts))))) |
690 (Proofterm.prop_of (Proofterm.proof_combt (prf0, ts))))) |
717 in (f $ t, defs'') end |
725 in (f $ t, defs'') end |
718 end |
726 end |
719 |
727 |
720 | extr d vs ts Ts hs (prf0 as PThm (thm_header as {types = SOME Ts', ...}, thm_body)) defs = |
728 | extr d vs ts Ts hs (prf0 as PThm (thm_header as {types = SOME Ts', ...}, thm_body)) defs = |
721 let |
729 let |
722 val {pos, theory_name, name = s, prop, ...} = thm_header; |
730 val {pos, theory_name, thm_name, prop, ...} = thm_header; |
723 val prf = Proofterm.thm_body_proof_open thm_body; |
731 val prf = Proofterm.thm_body_proof_open thm_body; |
724 val (vs', tye) = find_inst prop Ts ts vs; |
732 val (vs', tye) = find_inst prop Ts ts vs; |
725 val shyps = mk_shyps tye; |
733 val shyps = mk_shyps tye; |
726 val tye' = (map fst (Term.add_tvars prop [] |> rev) ~~ Ts') @ tye |
734 val tye' = (map fst (Term.add_tvars prop [] |> rev) ~~ Ts') @ tye |
727 in |
735 in |
728 case Symtab.lookup realizers s of |
736 case Thm_Name.Table.lookup realizers thm_name of |
729 NONE => (case find vs' (find' s defs) of |
737 NONE => (case find vs' (filter_thm_name thm_name defs) of |
730 NONE => |
738 NONE => |
731 let |
739 let |
732 val _ = msg d ("Extracting " ^ quote s ^ |
740 val _ = |
733 (if null vs' then "" |
741 msg d ("Extracting " ^ quote (Thm_Name.short thm_name) ^ |
734 else " (relevant variables: " ^ commas_quote vs' ^ ")")); |
742 (if null vs' then "" |
|
743 else " (relevant variables: " ^ commas_quote vs' ^ ")")); |
735 val prf' = prep (Proofterm.reconstruct_proof thy' prop prf); |
744 val prf' = prep (Proofterm.reconstruct_proof thy' prop prf); |
736 val (t, defs') = extr (d + 1) vs' [] [] [] prf' defs; |
745 val (t, defs') = extr (d + 1) vs' [] [] [] prf' defs; |
737 val (corr_prf, defs'') = corr (d + 1) vs' [] [] [] |
746 val (corr_prf, defs'') = corr (d + 1) vs' [] [] [] |
738 (rev shyps) (SOME t) prf' prf' defs'; |
747 (rev shyps) (SOME t) prf' prf' defs'; |
739 |
748 |
741 val args = filter_out (fn v => member (op =) rtypes |
750 val args = filter_out (fn v => member (op =) rtypes |
742 (tname_of (body_type (fastype_of v)))) (vfs_of prop); |
751 (tname_of (body_type (fastype_of v)))) (vfs_of prop); |
743 val args' = filter (fn v => Logic.occs (v, nt)) args; |
752 val args' = filter (fn v => Logic.occs (v, nt)) args; |
744 val t' = mkabs args' nt; |
753 val t' = mkabs args' nt; |
745 val T = fastype_of t'; |
754 val T = fastype_of t'; |
746 val cname = extr_name s vs'; |
755 val cname = Thm_Name.short (extr_name thm_name vs'); |
747 val c = Const (cname, T); |
756 val c = Const (cname, T); |
748 val u = mkabs args (list_comb (c, args')); |
757 val u = mkabs args (list_comb (c, args')); |
749 val eqn = Logic.mk_equals (c, t'); |
758 val eqn = Logic.mk_equals (c, t'); |
750 val rlz = |
759 val rlz = |
751 Const ("realizes", fastype_of nt --> propT --> propT); |
760 Const ("realizes", fastype_of nt --> propT --> propT); |
762 PAxm (Thm.def_name cname, eqn, |
771 PAxm (Thm.def_name cname, eqn, |
763 SOME (map TVar (Term.add_tvars eqn [] |> rev))))) %% corr_prf); |
772 SOME (map TVar (Term.add_tvars eqn [] |> rev))))) %% corr_prf); |
764 val corr_prop = Proofterm.prop_of corr_prf'; |
773 val corr_prop = Proofterm.prop_of corr_prf'; |
765 val corr_header = |
774 val corr_header = |
766 Proofterm.thm_header (serial ()) pos theory_name |
775 Proofterm.thm_header (serial ()) pos theory_name |
767 (corr_name s vs') corr_prop |
776 (corr_name thm_name vs') corr_prop |
768 (SOME (map TVar (Term.add_tvars corr_prop [] |> rev))); |
777 (SOME (map TVar (Term.add_tvars corr_prop [] |> rev))); |
769 val corr_prf'' = |
778 val corr_prf'' = |
770 Proofterm.proof_combP (Proofterm.proof_combt |
779 Proofterm.proof_combP (Proofterm.proof_combt |
771 (PThm (corr_header, make_proof_body corr_prf), vfs_of corr_prop), |
780 (PThm (corr_header, make_proof_body corr_prf), vfs_of corr_prop), |
772 map PBound (length shyps - 1 downto 0)) |> |
781 map PBound (length shyps - 1 downto 0)) |> |
773 fold_rev Proofterm.forall_intr_proof' |
782 fold_rev Proofterm.forall_intr_proof' |
774 (map (get_var_type corr_prop) (vfs_of prop)) |> |
783 (map (get_var_type corr_prop) (vfs_of prop)) |> |
775 mkabsp shyps |
784 mkabsp shyps |
776 in |
785 in |
777 (subst_TVars tye' u, |
786 (subst_TVars tye' u, |
778 (s, (vs', ((t', u), (corr_prf', corr_prf'')))) :: defs'') |
787 (thm_name, (vs', ((t', u), (corr_prf', corr_prf'')))) :: defs'') |
779 end |
788 end |
780 | SOME ((_, u), _) => (subst_TVars tye' u, defs)) |
789 | SOME ((_, u), _) => (subst_TVars tye' u, defs)) |
781 | SOME rs => (case find vs' rs of |
790 | SOME rs => (case find vs' rs of |
782 SOME (t, _) => (subst_TVars tye' t, defs) |
791 SOME (t, _) => (subst_TVars tye' t, defs) |
783 | NONE => error ("extr: no realizer for instance of theorem " ^ |
792 | NONE => error ("extr: no realizer for instance of theorem " ^ |
784 quote s ^ ":\n" ^ Syntax.string_of_term_global thy' (Envir.beta_norm |
793 quote (Thm_Name.short thm_name) ^ ":\n" ^ |
|
794 Syntax.string_of_term_global thy' (Envir.beta_norm |
785 (Proofterm.prop_of (Proofterm.proof_combt (prf0, ts)))))) |
795 (Proofterm.prop_of (Proofterm.proof_combt (prf0, ts)))))) |
786 end |
796 end |
787 |
797 |
788 | extr d vs ts Ts hs (prf0 as PAxm (s, prop, SOME Ts')) defs = |
798 | extr d vs ts Ts hs (prf0 as PAxm (s, prop, SOME Ts')) defs = |
789 let |
799 let |
790 val (vs', tye) = find_inst prop Ts ts vs; |
800 val (vs', tye) = find_inst prop Ts ts vs; |
791 val tye' = (map fst (Term.add_tvars prop [] |> rev) ~~ Ts') @ tye |
801 val tye' = (map fst (Term.add_tvars prop [] |> rev) ~~ Ts') @ tye |
792 in |
802 in |
793 case find vs' (Symtab.lookup_list realizers s) of |
803 case find vs' (Thm_Name.Table.lookup_list realizers (s, 0)) of |
794 SOME (t, _) => (subst_TVars tye' t, defs) |
804 SOME (t, _) => (subst_TVars tye' t, defs) |
795 | NONE => error ("extr: no realizer for instance of axiom " ^ |
805 | NONE => error ("extr: no realizer for instance of axiom " ^ |
796 quote s ^ ":\n" ^ Syntax.string_of_term_global thy' (Envir.beta_norm |
806 quote s ^ ":\n" ^ Syntax.string_of_term_global thy' (Envir.beta_norm |
797 (Proofterm.prop_of (Proofterm.proof_combt (prf0, ts))))) |
807 (Proofterm.prop_of (Proofterm.proof_combt (prf0, ts))))) |
798 end |
808 end |
803 let |
813 let |
804 val thm = Thm.transfer thy raw_thm; |
814 val thm = Thm.transfer thy raw_thm; |
805 val prop = Thm.prop_of thm; |
815 val prop = Thm.prop_of thm; |
806 val prf = Thm.proof_of thm; |
816 val prf = Thm.proof_of thm; |
807 val name = Thm.derivation_name thm; |
817 val name = Thm.derivation_name thm; |
808 val _ = name <> "" orelse error "extraction: unnamed theorem"; |
818 val _ = if Thm_Name.is_empty name then error "extraction: unnamed theorem" else (); |
809 val _ = etype_of thy' vs [] prop <> nullT orelse error ("theorem " ^ |
819 val _ = etype_of thy' vs [] prop <> nullT orelse error ("theorem " ^ |
810 quote name ^ " has no computational content") |
820 quote (Thm_Name.short name) ^ " has no computational content") |
811 in Proofterm.reconstruct_proof thy' prop prf end; |
821 in Proofterm.reconstruct_proof thy' prop prf end; |
812 |
822 |
813 val defs = |
823 val defs = |
814 fold (fn (thm, vs) => snd o (extr 0 vs [] [] [] o prep_thm vs) thm) |
824 fold (fn (thm, vs) => snd o (extr 0 vs [] [] [] o prep_thm vs) thm) |
815 thm_vss []; |
825 thm_vss []; |
816 |
826 |
817 fun add_def (s, (vs, ((t, u)))) thy = |
827 fun add_def (name, (vs, ((t, u)))) thy = |
818 let |
828 let |
819 val ft = Type.legacy_freeze t; |
829 val ft = Type.legacy_freeze t; |
820 val fu = Type.legacy_freeze u; |
830 val fu = Type.legacy_freeze u; |
821 val head = head_of (strip_abs_body fu); |
831 val head = head_of (strip_abs_body fu); |
822 val b = Binding.qualified_name (extr_name s vs); |
832 val b = Binding.qualified_name (Thm_Name.short (extr_name name vs)); |
823 in |
833 in |
824 thy |
834 thy |
825 |> Sign.add_consts [(b, fastype_of ft, NoSyn)] |
835 |> Sign.add_consts [(b, fastype_of ft, NoSyn)] |
826 |> Global_Theory.add_def |
836 |> Global_Theory.add_def |
827 (Binding.qualified_name (Thm.def_name (extr_name s vs)), Logic.mk_equals (head, ft)) |
837 (Binding.qualified_name (Thm.def_name (Thm_Name.short (extr_name name vs))), |
|
838 Logic.mk_equals (head, ft)) |
828 |-> (fn def_thm => |
839 |-> (fn def_thm => |
829 Spec_Rules.add_global b Spec_Rules.equational |
840 Spec_Rules.add_global b Spec_Rules.equational |
830 [Thm.term_of (Thm.lhs_of def_thm)] [def_thm] |
841 [Thm.term_of (Thm.lhs_of def_thm)] [def_thm] |
831 #> Code.declare_default_eqns_global [(def_thm, true)]) |
842 #> Code.declare_default_eqns_global [(def_thm, true)]) |
832 end; |
843 end; |
834 fun add_corr (s, (vs, prf)) thy = |
845 fun add_corr (s, (vs, prf)) thy = |
835 let |
846 let |
836 val corr_prop = Proofterm.prop_of prf; |
847 val corr_prop = Proofterm.prop_of prf; |
837 in |
848 in |
838 thy |
849 thy |
839 |> Global_Theory.store_thm (Binding.qualified_name (corr_name s vs), |
850 |> Global_Theory.store_thm (Binding.qualified_name (Thm_Name.short (corr_name s vs)), |
840 Thm.varifyT_global (funpow (length (vars_of corr_prop)) |
851 Thm.varifyT_global (funpow (length (vars_of corr_prop)) |
841 (Thm.forall_elim_var 0) (Thm.forall_intr_frees |
852 (Thm.forall_elim_var 0) (Thm.forall_intr_frees |
842 (Proof_Checker.thm_of_proof thy |
853 (Proof_Checker.thm_of_proof thy |
843 (fst (Proofterm.freeze_thaw_prf prf)))))) |
854 (fst (Proofterm.freeze_thaw_prf prf)))))) |
844 |> snd |
855 |> snd |
845 end; |
856 end; |
846 |
857 |
847 fun add_def_and_corr (s, (vs, ((t, u), (prf, _)))) thy = |
858 fun add_def_and_corr (s, (vs, ((t, u), (prf, _)))) thy = |
848 if is_none (Sign.const_type thy (extr_name s vs)) |
859 if is_none (Sign.const_type thy (Thm_Name.short (extr_name s vs))) |
849 then |
860 then |
850 thy |
861 thy |
851 |> not (t = nullt) ? add_def (s, (vs, ((t, u)))) |
862 |> not (t = nullt) ? add_def (s, (vs, ((t, u)))) |
852 |> add_corr (s, (vs, prf)) |
863 |> add_corr (s, (vs, prf)) |
853 else |
864 else |