src/Pure/Proof/extraction.ML
author wenzelm
Fri, 16 Mar 2012 18:20:12 +0100
changeset 46961 5c6955f487e5
parent 46909 3c73a121a387
child 48646 91281e9472d8
permissions -rw-r--r--
outer syntax command definitions based on formal command_spec derived from theory header declarations;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
     1
(*  Title:      Pure/Proof/extraction.ML
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
     2
    Author:     Stefan Berghofer, TU Muenchen
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
     3
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
     4
Extraction of programs from proofs.
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
     5
*)
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
     6
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
     7
signature EXTRACTION =
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
     8
sig
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16363
diff changeset
     9
  val set_preprocessor : (theory -> Proofterm.proof -> Proofterm.proof) -> theory -> theory
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
    10
  val add_realizes_eqns_i : ((term * term) list * (term * term)) list -> theory -> theory
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
    11
  val add_realizes_eqns : string list -> theory -> theory
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
    12
  val add_typeof_eqns_i : ((term * term) list * (term * term)) list -> theory -> theory
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
    13
  val add_typeof_eqns : string list -> theory -> theory
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
    14
  val add_realizers_i : (string * (string list * term * Proofterm.proof)) list
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
    15
    -> theory -> theory
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
    16
  val add_realizers : (thm * (string list * string * string)) list
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
    17
    -> theory -> theory
33704
6aeb8454efc1 add_expand_thm: explicit indication of is_def instead of fragile heuristic, tuned signature;
wenzelm
parents: 33522
diff changeset
    18
  val add_expand_thm : bool -> thm -> theory -> theory
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
    19
  val add_types : (xstring * ((term -> term option) list *
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
    20
    (term -> typ -> term -> typ -> term) option)) list -> theory -> theory
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
    21
  val extract : (thm * string list) list -> theory -> theory
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
    22
  val nullT : typ
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
    23
  val nullt : term
13714
bdd483321f4b - exported functions etype_of and mk_typ
berghofe
parents: 13609
diff changeset
    24
  val mk_typ : typ -> term
bdd483321f4b - exported functions etype_of and mk_typ
berghofe
parents: 13609
diff changeset
    25
  val etype_of : theory -> string list -> typ list -> term -> typ
bdd483321f4b - exported functions etype_of and mk_typ
berghofe
parents: 13609
diff changeset
    26
  val realizes_of: theory -> string list -> term -> term -> term
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
    27
  val abs_corr_shyps: theory -> thm -> string list -> term list -> Proofterm.proof -> Proofterm.proof
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
    28
end;
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
    29
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
    30
structure Extraction : EXTRACTION =
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
    31
struct
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
    32
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
    33
(**** tools ****)
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
    34
42407
5b9dd52f5dca prefer internal types, via Simple_Syntax.read_typ;
wenzelm
parents: 42406
diff changeset
    35
val typ = Simple_Syntax.read_typ;
5b9dd52f5dca prefer internal types, via Simple_Syntax.read_typ;
wenzelm
parents: 42406
diff changeset
    36
5b9dd52f5dca prefer internal types, via Simple_Syntax.read_typ;
wenzelm
parents: 42406
diff changeset
    37
val add_syntax =
5b9dd52f5dca prefer internal types, via Simple_Syntax.read_typ;
wenzelm
parents: 42406
diff changeset
    38
  Theory.copy
5b9dd52f5dca prefer internal types, via Simple_Syntax.read_typ;
wenzelm
parents: 42406
diff changeset
    39
  #> Sign.root_path
5b9dd52f5dca prefer internal types, via Simple_Syntax.read_typ;
wenzelm
parents: 42406
diff changeset
    40
  #> Sign.add_types_global [(Binding.name "Type", 0, NoSyn), (Binding.name "Null", 0, NoSyn)]
5b9dd52f5dca prefer internal types, via Simple_Syntax.read_typ;
wenzelm
parents: 42406
diff changeset
    41
  #> Sign.add_consts_i
5b9dd52f5dca prefer internal types, via Simple_Syntax.read_typ;
wenzelm
parents: 42406
diff changeset
    42
      [(Binding.name "typeof", typ "'b => Type", NoSyn),
5b9dd52f5dca prefer internal types, via Simple_Syntax.read_typ;
wenzelm
parents: 42406
diff changeset
    43
       (Binding.name "Type", typ "'a itself => Type", NoSyn),
5b9dd52f5dca prefer internal types, via Simple_Syntax.read_typ;
wenzelm
parents: 42406
diff changeset
    44
       (Binding.name "Null", typ "Null", NoSyn),
5b9dd52f5dca prefer internal types, via Simple_Syntax.read_typ;
wenzelm
parents: 42406
diff changeset
    45
       (Binding.name "realizes", typ "'a => 'b => 'b", NoSyn)];
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
    46
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
    47
val nullT = Type ("Null", []);
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
    48
val nullt = Const ("Null", nullT);
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
    49
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
    50
fun mk_typ T =
19391
4812d28c90a6 Term.itselfT;
wenzelm
parents: 19305
diff changeset
    51
  Const ("Type", Term.itselfT T --> Type ("Type", [])) $ Logic.mk_type T;
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
    52
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
    53
fun typeof_proc defaultS vs (Const ("typeof", _) $ u) =
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
    54
      SOME (mk_typ (case strip_comb u of
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
    55
          (Var ((a, i), _), _) =>
20664
ffbc5a57191a member (op =);
wenzelm
parents: 20548
diff changeset
    56
            if member (op =) vs a then TFree ("'" ^ a ^ ":" ^ string_of_int i, defaultS)
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
    57
            else nullT
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
    58
        | (Free (a, _), _) =>
20664
ffbc5a57191a member (op =);
wenzelm
parents: 20548
diff changeset
    59
            if member (op =) vs a then TFree ("'" ^ a, defaultS) else nullT
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
    60
        | _ => nullT))
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
    61
  | typeof_proc _ _ _ = NONE;
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
    62
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
    63
fun rlz_proc (Const ("realizes", Type (_, [Type ("Null", []), _])) $ r $ t) = SOME t
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
    64
  | rlz_proc (Const ("realizes", Type (_, [T, _])) $ r $ t) =
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
    65
      (case strip_comb t of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
    66
         (Var (ixn, U), ts) => SOME (list_comb (Var (ixn, T --> U), r :: ts))
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
    67
       | (Free (s, U), ts) => SOME (list_comb (Free (s, T --> U), r :: ts))
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
    68
       | _ => NONE)
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
    69
  | rlz_proc _ = NONE;
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
    70
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
    71
val unpack_ixn = apfst implode o apsnd (fst o read_int o tl) o
40627
becf5d5187cc renamed raw "explode" function to "raw_explode" to emphasize its meaning;
wenzelm
parents: 40132
diff changeset
    72
  take_prefix (fn s => s <> ":") o raw_explode;
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
    73
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
    74
type rules =
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
    75
  {next: int, rs: ((term * term) list * (term * term)) list,
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
    76
   net: (int * ((term * term) list * (term * term))) Net.net};
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
    77
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
    78
val empty_rules : rules = {next = 0, rs = [], net = Net.empty};
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
    79
33337
9c3b9bf81e8b eliminated some old folds;
wenzelm
parents: 33317
diff changeset
    80
fun add_rule (r as (_, (lhs, _))) ({next, rs, net} : rules) =
16800
90eff1b52428 improved Net interface;
wenzelm
parents: 16790
diff changeset
    81
  {next = next - 1, rs = r :: rs, net = Net.insert_term (K false)
18956
c050ae1f8f52 Envir.(beta_)eta_contract;
wenzelm
parents: 18928
diff changeset
    82
     (Envir.eta_contract lhs, (next, r)) net};
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
    83
32784
1a5dde5079ac eliminated redundant bindings;
wenzelm
parents: 32738
diff changeset
    84
fun merge_rules ({next, rs = rs1, net} : rules) ({rs = rs2, ...} : rules) =
33337
9c3b9bf81e8b eliminated some old folds;
wenzelm
parents: 33317
diff changeset
    85
  fold_rev add_rule (subtract (op =) rs1 rs2) {next = next, rs = rs1, net = net};
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
    86
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16363
diff changeset
    87
fun condrew thy rules procs =
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
    88
  let
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
    89
    fun rew tm =
17203
29b2563f5c11 refer to theory instead of low-level tsig;
wenzelm
parents: 17057
diff changeset
    90
      Pattern.rewrite_term thy [] (condrew' :: procs) tm
15399
683d83051d6a Added term cache to function condrew in order to speed up rewriting.
berghofe
parents: 14981
diff changeset
    91
    and condrew' tm =
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
    92
      let
32738
15bb09ca0378 explicit indication of Unsynchronized.ref;
wenzelm
parents: 32035
diff changeset
    93
        val cache = Unsynchronized.ref ([] : (term * term) list);
17232
148c241d2492 some 'assoc' etc. refactoring
haftmann
parents: 17223
diff changeset
    94
        fun lookup f x = (case AList.lookup (op =) (!cache) x of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
    95
            NONE =>
15399
683d83051d6a Added term cache to function condrew in order to speed up rewriting.
berghofe
parents: 14981
diff changeset
    96
              let val y = f x
683d83051d6a Added term cache to function condrew in order to speed up rewriting.
berghofe
parents: 14981
diff changeset
    97
              in (cache := (x, y) :: !cache; y) end
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
    98
          | SOME y => y);
15399
683d83051d6a Added term cache to function condrew in order to speed up rewriting.
berghofe
parents: 14981
diff changeset
    99
      in
683d83051d6a Added term cache to function condrew in order to speed up rewriting.
berghofe
parents: 14981
diff changeset
   100
        get_first (fn (_, (prems, (tm1, tm2))) =>
683d83051d6a Added term cache to function condrew in order to speed up rewriting.
berghofe
parents: 14981
diff changeset
   101
        let
19466
wenzelm
parents: 19391
diff changeset
   102
          fun ren t = the_default t (Term.rename_abs tm1 tm t);
15399
683d83051d6a Added term cache to function condrew in order to speed up rewriting.
berghofe
parents: 14981
diff changeset
   103
          val inc = Logic.incr_indexes ([], maxidx_of_term tm + 1);
18184
43c4589a9a78 tuned Pattern.match/unify;
wenzelm
parents: 17412
diff changeset
   104
          val env as (Tenv, tenv) = Pattern.match thy (inc tm1, tm) (Vartab.empty, Vartab.empty);
32035
8e77b6a250d5 tuned/modernized Envir.subst_XXX;
wenzelm
parents: 32032
diff changeset
   105
          val prems' = map (pairself (Envir.subst_term 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
   106
          val env' = Envir.Envir
32032
a6a6e8031c14 tuned/modernized Envir operations;
wenzelm
parents: 30718
diff changeset
   107
            {maxidx = fold (fn (t, u) => Term.maxidx_term t #> Term.maxidx_term u) prems' ~1,
a6a6e8031c14 tuned/modernized Envir operations;
wenzelm
parents: 30718
diff changeset
   108
             tenv = tenv, tyenv = Tenv};
18184
43c4589a9a78 tuned Pattern.match/unify;
wenzelm
parents: 17412
diff changeset
   109
          val env'' = fold (Pattern.unify thy o pairself (lookup rew)) prems' env';
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   110
        in SOME (Envir.norm_term env'' (inc (ren tm2)))
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   111
        end handle Pattern.MATCH => NONE | Pattern.Unif => NONE)
16486
1a12cdb6ee6b get_thm(s): Name;
wenzelm
parents: 16458
diff changeset
   112
          (sort (int_ord o pairself fst)
18956
c050ae1f8f52 Envir.(beta_)eta_contract;
wenzelm
parents: 18928
diff changeset
   113
            (Net.match_term rules (Envir.eta_contract tm)))
15399
683d83051d6a Added term cache to function condrew in order to speed up rewriting.
berghofe
parents: 14981
diff changeset
   114
      end;
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   115
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   116
  in rew end;
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   117
37310
96e2b9a6f074 do not open Proofterm, which is very ould style;
wenzelm
parents: 37237
diff changeset
   118
val chtype = Proofterm.change_type o SOME;
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   119
30364
577edc39b501 moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents: 30344
diff changeset
   120
fun extr_name s vs = Long_Name.append "extr" (space_implode "_" (s :: vs));
16195
wenzelm
parents: 16149
diff changeset
   121
fun corr_name s vs = extr_name s vs ^ "_correctness";
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   122
40132
7ee65dbffa31 renamed Output.priority to Output.urgent_message to emphasize its special role more clearly;
wenzelm
parents: 39557
diff changeset
   123
fun msg d s = Output.urgent_message (Symbol.spaces d ^ s);
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   124
28812
413695e07bd4 Frees in PThms are now quantified in the order of their appearance in the
berghofe
parents: 28805
diff changeset
   125
fun vars_of t = map Var (rev (Term.add_vars t []));
413695e07bd4 Frees in PThms are now quantified in the order of their appearance in the
berghofe
parents: 28805
diff changeset
   126
fun frees_of t = map Free (rev (Term.add_frees t []));
413695e07bd4 Frees in PThms are now quantified in the order of their appearance in the
berghofe
parents: 28805
diff changeset
   127
fun vfs_of t = vars_of t @ frees_of t;
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   128
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   129
val mkabs = fold_rev (fn v => fn t => Abs ("x", fastype_of v, abstract_over (v, t)));
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   130
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   131
val mkabsp = fold_rev (fn t => fn prf => AbsP ("H", SOME t, prf));
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   132
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   133
fun strip_abs 0 t = t
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   134
  | strip_abs n (Abs (_, _, t)) = strip_abs (n-1) t
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   135
  | strip_abs _ _ = error "strip_abs: not an abstraction";
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   136
37310
96e2b9a6f074 do not open Proofterm, which is very ould style;
wenzelm
parents: 37237
diff changeset
   137
val prf_subst_TVars = Proofterm.map_proof_types o typ_subst_TVars;
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   138
40844
5895c525739d more direct use of binder_types/body_type;
wenzelm
parents: 40627
diff changeset
   139
fun relevant_vars types prop =
5895c525739d more direct use of binder_types/body_type;
wenzelm
parents: 40627
diff changeset
   140
  List.foldr
5895c525739d more direct use of binder_types/body_type;
wenzelm
parents: 40627
diff changeset
   141
    (fn (Var ((a, _), T), vs) =>
5895c525739d more direct use of binder_types/body_type;
wenzelm
parents: 40627
diff changeset
   142
        (case body_type T of
5895c525739d more direct use of binder_types/body_type;
wenzelm
parents: 40627
diff changeset
   143
          Type (s, _) => if member (op =) types s then a :: vs else vs
5895c525739d more direct use of binder_types/body_type;
wenzelm
parents: 40627
diff changeset
   144
        | _ => vs)
5895c525739d more direct use of binder_types/body_type;
wenzelm
parents: 40627
diff changeset
   145
      | (_, vs) => vs) [] (vars_of prop);
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   146
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   147
fun tname_of (Type (s, _)) = s
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   148
  | tname_of _ = "";
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   149
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   150
fun get_var_type t =
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   151
  let
16865
fb39dcfc1c24 tuned fold on terms;
wenzelm
parents: 16800
diff changeset
   152
    val vs = Term.add_vars t [];
fb39dcfc1c24 tuned fold on terms;
wenzelm
parents: 16800
diff changeset
   153
    val fs = Term.add_frees t [];
42406
05f2468d6b36 eliminated obsolete Proof_Syntax.strip_sorts_consttypes;
wenzelm
parents: 42375
diff changeset
   154
  in
05f2468d6b36 eliminated obsolete Proof_Syntax.strip_sorts_consttypes;
wenzelm
parents: 42375
diff changeset
   155
    fn Var (ixn, _) =>
05f2468d6b36 eliminated obsolete Proof_Syntax.strip_sorts_consttypes;
wenzelm
parents: 42375
diff changeset
   156
        (case AList.lookup (op =) vs ixn of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   157
          NONE => error "get_var_type: no such variable in term"
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   158
        | SOME T => Var (ixn, T))
42406
05f2468d6b36 eliminated obsolete Proof_Syntax.strip_sorts_consttypes;
wenzelm
parents: 42375
diff changeset
   159
     | Free (s, _) =>
05f2468d6b36 eliminated obsolete Proof_Syntax.strip_sorts_consttypes;
wenzelm
parents: 42375
diff changeset
   160
        (case AList.lookup (op =) fs s of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   161
          NONE => error "get_var_type: no such variable in term"
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   162
        | SOME T => Free (s, T))
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   163
    | _ => error "get_var_type: not a variable"
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   164
  end;
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   165
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   166
fun read_term thy T s =
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   167
  let
42360
da8817d01e7c modernized structure Proof_Context;
wenzelm
parents: 40844
diff changeset
   168
    val ctxt = Proof_Context.init_global thy
42406
05f2468d6b36 eliminated obsolete Proof_Syntax.strip_sorts_consttypes;
wenzelm
parents: 42375
diff changeset
   169
      |> Config.put Type_Infer_Context.const_sorts false
42360
da8817d01e7c modernized structure Proof_Context;
wenzelm
parents: 40844
diff changeset
   170
      |> Proof_Context.set_defsort [];
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   171
    val parse = if T = propT then Syntax.parse_prop else Syntax.parse_term;
39288
f1ae2493d93f eliminated aliases of Type.constraint;
wenzelm
parents: 38761
diff changeset
   172
  in parse ctxt s |> Type.constraint T |> Syntax.check_term ctxt end;
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   173
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   174
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   175
(**** theory data ****)
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   176
22846
fb79144af9a3 simplified DataFun interfaces;
wenzelm
parents: 22796
diff changeset
   177
(* theory data *)
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   178
33522
737589bb9bb8 adapted Theory_Data;
wenzelm
parents: 33388
diff changeset
   179
structure ExtractionData = Theory_Data
22846
fb79144af9a3 simplified DataFun interfaces;
wenzelm
parents: 22796
diff changeset
   180
(
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   181
  type T =
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   182
    {realizes_eqns : rules,
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   183
     typeof_eqns : rules,
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   184
     types : (string * ((term -> term option) list *
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   185
       (term -> typ -> term -> typ -> term) option)) list,
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   186
     realizers : (string list * (term * proof)) list Symtab.table,
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   187
     defs : thm list,
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   188
     expand : string list,
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16363
diff changeset
   189
     prep : (theory -> proof -> proof) option}
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   190
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   191
  val empty =
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   192
    {realizes_eqns = empty_rules,
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   193
     typeof_eqns = empty_rules,
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   194
     types = [],
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   195
     realizers = Symtab.empty,
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   196
     defs = [],
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   197
     expand = [],
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   198
     prep = NONE};
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16363
diff changeset
   199
  val extend = I;
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   200
33522
737589bb9bb8 adapted Theory_Data;
wenzelm
parents: 33388
diff changeset
   201
  fun merge
737589bb9bb8 adapted Theory_Data;
wenzelm
parents: 33388
diff changeset
   202
    ({realizes_eqns = realizes_eqns1, typeof_eqns = typeof_eqns1, types = types1,
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   203
       realizers = realizers1, defs = defs1, expand = expand1, prep = prep1},
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   204
      {realizes_eqns = realizes_eqns2, typeof_eqns = typeof_eqns2, types = types2,
33522
737589bb9bb8 adapted Theory_Data;
wenzelm
parents: 33388
diff changeset
   205
       realizers = realizers2, defs = defs2, expand = expand2, prep = prep2}) : T =
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   206
    {realizes_eqns = merge_rules realizes_eqns1 realizes_eqns2,
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   207
     typeof_eqns = merge_rules typeof_eqns1 typeof_eqns2,
22717
74dbc7696083 canonical merge operations
haftmann
parents: 22675
diff changeset
   208
     types = AList.merge (op =) (K true) (types1, types2),
33038
8f9594c31de4 dropped redundant gen_ prefix
haftmann
parents: 32784
diff changeset
   209
     realizers = Symtab.merge_list (eq_set (op =) o pairself #1) (realizers1, realizers2),
22662
3e492ba59355 canonical merge operations
haftmann
parents: 22596
diff changeset
   210
     defs = Library.merge Thm.eq_thm (defs1, defs2),
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   211
     expand = Library.merge (op =) (expand1, expand2),
38761
b32975d3db3e theory data merge: prefer left side uniformly;
wenzelm
parents: 37310
diff changeset
   212
     prep = if is_some prep1 then prep1 else prep2};
22846
fb79144af9a3 simplified DataFun interfaces;
wenzelm
parents: 22796
diff changeset
   213
);
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   214
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   215
fun read_condeq thy =
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16363
diff changeset
   216
  let val thy' = add_syntax thy
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   217
  in fn s =>
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   218
    let val t = Logic.varify_global (read_term thy' propT s)
35424
08c37d7bd2ad more precise scope of exception handler;
wenzelm
parents: 33957
diff changeset
   219
    in
08c37d7bd2ad more precise scope of exception handler;
wenzelm
parents: 33957
diff changeset
   220
      (map Logic.dest_equals (Logic.strip_imp_prems t),
08c37d7bd2ad more precise scope of exception handler;
wenzelm
parents: 33957
diff changeset
   221
        Logic.dest_equals (Logic.strip_imp_concl t))
08c37d7bd2ad more precise scope of exception handler;
wenzelm
parents: 33957
diff changeset
   222
      handle TERM _ => error ("Not a (conditional) meta equality:\n" ^ s)
08c37d7bd2ad more precise scope of exception handler;
wenzelm
parents: 33957
diff changeset
   223
    end
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   224
  end;
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   225
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   226
(** preprocessor **)
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   227
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   228
fun set_preprocessor prep thy =
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   229
  let val {realizes_eqns, typeof_eqns, types, realizers,
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   230
    defs, expand, ...} = ExtractionData.get thy
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   231
  in
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   232
    ExtractionData.put
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   233
      {realizes_eqns = realizes_eqns, typeof_eqns = typeof_eqns, types = types,
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   234
       realizers = realizers, defs = defs, expand = expand, prep = SOME prep} thy
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   235
  end;
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   236
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   237
(** equations characterizing realizability **)
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   238
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   239
fun gen_add_realizes_eqns prep_eq eqns thy =
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   240
  let val {realizes_eqns, typeof_eqns, types, realizers,
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   241
    defs, expand, prep} = ExtractionData.get thy;
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   242
  in
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   243
    ExtractionData.put
33337
9c3b9bf81e8b eliminated some old folds;
wenzelm
parents: 33317
diff changeset
   244
      {realizes_eqns = fold_rev add_rule (map (prep_eq thy) eqns) realizes_eqns,
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   245
       typeof_eqns = typeof_eqns, types = types, realizers = realizers,
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   246
       defs = defs, expand = expand, prep = prep} thy
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   247
  end
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   248
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   249
val add_realizes_eqns_i = gen_add_realizes_eqns (K I);
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   250
val add_realizes_eqns = gen_add_realizes_eqns read_condeq;
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   251
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   252
(** equations characterizing type of extracted program **)
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   253
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   254
fun gen_add_typeof_eqns prep_eq eqns thy =
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   255
  let
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   256
    val {realizes_eqns, typeof_eqns, types, realizers,
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   257
      defs, expand, prep} = ExtractionData.get thy;
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   258
    val eqns' = map (prep_eq thy) eqns
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   259
  in
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   260
    ExtractionData.put
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   261
      {realizes_eqns = realizes_eqns, realizers = realizers,
33337
9c3b9bf81e8b eliminated some old folds;
wenzelm
parents: 33317
diff changeset
   262
       typeof_eqns = fold_rev add_rule eqns' typeof_eqns,
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   263
       types = types, defs = defs, expand = expand, prep = prep} thy
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   264
  end
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   265
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   266
val add_typeof_eqns_i = gen_add_typeof_eqns (K I);
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   267
val add_typeof_eqns = gen_add_typeof_eqns read_condeq;
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   268
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   269
fun thaw (T as TFree (a, S)) =
28375
c879d88d038a eliminated polymorphic equality;
wenzelm
parents: 28370
diff changeset
   270
      if exists_string (fn s => s = ":") a then TVar (unpack_ixn a, S) else T
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   271
  | thaw (Type (a, Ts)) = Type (a, map thaw Ts)
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   272
  | thaw T = T;
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   273
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   274
fun freeze (TVar ((a, i), S)) = TFree (a ^ ":" ^ string_of_int i, S)
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   275
  | freeze (Type (a, Ts)) = Type (a, map freeze Ts)
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   276
  | freeze T = T;
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   277
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   278
fun freeze_thaw f x =
20548
8ef25fe585a8 renamed Term.map_term_types to Term.map_types (cf. Term.fold_types);
wenzelm
parents: 19482
diff changeset
   279
  map_types thaw (f (map_types freeze x));
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   280
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16363
diff changeset
   281
fun etype_of thy vs Ts t =
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   282
  let
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16363
diff changeset
   283
    val {typeof_eqns, ...} = ExtractionData.get thy;
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   284
    fun err () = error ("Unable to determine type of extracted program for\n" ^
26939
1035c89b4c02 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26653
diff changeset
   285
      Syntax.string_of_term_global thy t)
46219
426ed18eba43 discontinued old-style Term.list_abs in favour of plain Term.abs;
wenzelm
parents: 44057
diff changeset
   286
  in
426ed18eba43 discontinued old-style Term.list_abs in favour of plain Term.abs;
wenzelm
parents: 44057
diff changeset
   287
    (case
426ed18eba43 discontinued old-style Term.list_abs in favour of plain Term.abs;
wenzelm
parents: 44057
diff changeset
   288
      strip_abs_body
426ed18eba43 discontinued old-style Term.list_abs in favour of plain Term.abs;
wenzelm
parents: 44057
diff changeset
   289
        (freeze_thaw (condrew thy (#net typeof_eqns) [typeof_proc [] vs])
426ed18eba43 discontinued old-style Term.list_abs in favour of plain Term.abs;
wenzelm
parents: 44057
diff changeset
   290
          (fold (Term.abs o pair "x") Ts
426ed18eba43 discontinued old-style Term.list_abs in favour of plain Term.abs;
wenzelm
parents: 44057
diff changeset
   291
            (Const ("typeof", fastype_of1 (Ts, t) --> Type ("Type", [])) $ t))) of
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   292
      Const ("Type", _) $ u => (Logic.dest_type u handle TERM _ => err ())
46219
426ed18eba43 discontinued old-style Term.list_abs in favour of plain Term.abs;
wenzelm
parents: 44057
diff changeset
   293
    | _ => err ())
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   294
  end;
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   295
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   296
(** realizers for axioms / theorems, together with correctness proofs **)
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   297
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   298
fun gen_add_realizers prep_rlz rs thy =
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   299
  let val {realizes_eqns, typeof_eqns, types, realizers,
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   300
    defs, expand, prep} = ExtractionData.get thy
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   301
  in
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   302
    ExtractionData.put
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   303
      {realizes_eqns = realizes_eqns, typeof_eqns = typeof_eqns, types = types,
25389
3e58c7cb5a73 renamed Symtab.update_list to Symtab.cons_list;
wenzelm
parents: 24867
diff changeset
   304
       realizers = fold (Symtab.cons_list o prep_rlz thy) rs realizers,
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   305
       defs = defs, expand = expand, prep = prep} thy
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   306
  end
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   307
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   308
fun prep_realizer thy =
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   309
  let
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   310
    val {realizes_eqns, typeof_eqns, defs, types, ...} =
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   311
      ExtractionData.get thy;
19482
9f11af8f7ef9 tuned basic list operators (flat, maps, map_filter);
wenzelm
parents: 19466
diff changeset
   312
    val procs = maps (fst o snd) types;
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   313
    val rtypes = map fst types;
16800
90eff1b52428 improved Net interface;
wenzelm
parents: 16790
diff changeset
   314
    val eqns = Net.merge (K false) (#net realizes_eqns, #net typeof_eqns);
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   315
    val thy' = add_syntax thy;
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   316
    val rd = Proof_Syntax.read_proof thy' true false;
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   317
  in fn (thm, (vs, s1, s2)) =>
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   318
    let
36744
6e1f3d609a68 renamed Thm.get_name -> Thm.derivation_name and Thm.put_name -> Thm.name_derivation, to emphasize the true nature of these operations;
wenzelm
parents: 36620
diff changeset
   319
      val name = Thm.derivation_name thm;
21858
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 21646
diff changeset
   320
      val _ = name <> "" orelse error "add_realizers: unnamed theorem";
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   321
      val prop = Thm.unconstrainT thm |> prop_of |>
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   322
        Pattern.rewrite_term thy' (map (Logic.dest_equals o prop_of) defs) [];
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   323
      val vars = vars_of prop;
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   324
      val vars' = filter_out (fn v =>
20664
ffbc5a57191a member (op =);
wenzelm
parents: 20548
diff changeset
   325
        member (op =) rtypes (tname_of (body_type (fastype_of v)))) vars;
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   326
      val shyps = maps (fn Var ((x, i), _) =>
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   327
        if member (op =) vs x then Logic.mk_of_sort
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   328
          (TVar (("'" ^ x, i), []), Sign.defaultS thy')
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   329
        else []) vars;
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16363
diff changeset
   330
      val T = etype_of thy' vs [] prop;
33832
cff42395c246 explicitly mark some legacy freeze operations;
wenzelm
parents: 33704
diff changeset
   331
      val (T', thw) = Type.legacy_freeze_thaw_type
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   332
        (if T = nullT then nullT else map fastype_of vars' ---> T);
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   333
      val t = map_types thw (read_term thy' T' s1);
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16363
diff changeset
   334
      val r' = freeze_thaw (condrew thy' eqns
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   335
        (procs @ [typeof_proc [] vs, rlz_proc]))
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   336
          (Const ("realizes", T --> propT --> propT) $
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   337
            (if T = nullT then t else list_comb (t, vars')) $ prop);
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   338
      val r = Logic.list_implies (shyps,
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   339
        fold_rev Logic.all (map (get_var_type r') vars) r');
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16363
diff changeset
   340
      val prf = Reconstruct.reconstruct_proof thy' r (rd s2);
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   341
    in (name, (vs, (t, prf))) end
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   342
  end;
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   343
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   344
val add_realizers_i = gen_add_realizers
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   345
  (fn _ => fn (name, (vs, t, prf)) => (name, (vs, (t, prf))));
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   346
val add_realizers = gen_add_realizers prep_realizer;
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   347
13714
bdd483321f4b - exported functions etype_of and mk_typ
berghofe
parents: 13609
diff changeset
   348
fun realizes_of thy vs t prop =
bdd483321f4b - exported functions etype_of and mk_typ
berghofe
parents: 13609
diff changeset
   349
  let
bdd483321f4b - exported functions etype_of and mk_typ
berghofe
parents: 13609
diff changeset
   350
    val thy' = add_syntax thy;
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   351
    val {realizes_eqns, typeof_eqns, defs, types, ...} =
13714
bdd483321f4b - exported functions etype_of and mk_typ
berghofe
parents: 13609
diff changeset
   352
      ExtractionData.get thy';
22717
74dbc7696083 canonical merge operations
haftmann
parents: 22675
diff changeset
   353
    val procs = maps (rev o fst o snd) types;
16800
90eff1b52428 improved Net interface;
wenzelm
parents: 16790
diff changeset
   354
    val eqns = Net.merge (K false) (#net realizes_eqns, #net typeof_eqns);
17203
29b2563f5c11 refer to theory instead of low-level tsig;
wenzelm
parents: 17057
diff changeset
   355
    val prop' = Pattern.rewrite_term thy'
13714
bdd483321f4b - exported functions etype_of and mk_typ
berghofe
parents: 13609
diff changeset
   356
      (map (Logic.dest_equals o prop_of) defs) [] prop;
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16363
diff changeset
   357
  in freeze_thaw (condrew thy' eqns
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   358
    (procs @ [typeof_proc [] vs, rlz_proc]))
13714
bdd483321f4b - exported functions etype_of and mk_typ
berghofe
parents: 13609
diff changeset
   359
      (Const ("realizes", fastype_of t --> propT --> propT) $ t $ prop')
bdd483321f4b - exported functions etype_of and mk_typ
berghofe
parents: 13609
diff changeset
   360
  end;
bdd483321f4b - exported functions etype_of and mk_typ
berghofe
parents: 13609
diff changeset
   361
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   362
fun abs_corr_shyps thy thm vs xs prf =
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   363
  let
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   364
    val S = Sign.defaultS thy;
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   365
    val ((atyp_map, constraints, _), prop') =
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   366
      Logic.unconstrainT (#shyps (rep_thm thm)) (prop_of thm);
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   367
    val atyps = fold_types (fold_atyps (insert (op =))) (prop_of thm) [];
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   368
    val Ts = map_filter (fn ((v, i), _) => if member (op =) vs v then
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   369
        SOME (TVar (("'" ^ v, i), [])) else NONE)
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   370
      (rev (Term.add_vars prop' []));
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   371
    val cs = maps (fn T => map (pair T) S) Ts;
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   372
    val constraints' = map Logic.mk_of_class cs;
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   373
    val cs' = rev (cs @ map (Logic.dest_of_class o snd) constraints);
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   374
    fun typ_map T = Type.strip_sorts
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   375
      (map_atyps (fn U => if member (op =) atyps U then atyp_map U else U) T);
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   376
    fun mk_hyp (T, c) = Hyp (Logic.mk_of_class (typ_map T, c));
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   377
    val xs' = map (map_types typ_map) xs
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   378
  in
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   379
    prf |>
37310
96e2b9a6f074 do not open Proofterm, which is very ould style;
wenzelm
parents: 37237
diff changeset
   380
    Same.commit (Proofterm.map_proof_same (map_types typ_map) typ_map mk_hyp) |>
96e2b9a6f074 do not open Proofterm, which is very ould style;
wenzelm
parents: 37237
diff changeset
   381
    fold_rev Proofterm.implies_intr_proof' (map snd constraints) |>
96e2b9a6f074 do not open Proofterm, which is very ould style;
wenzelm
parents: 37237
diff changeset
   382
    fold_rev Proofterm.forall_intr_proof' xs' |>
96e2b9a6f074 do not open Proofterm, which is very ould style;
wenzelm
parents: 37237
diff changeset
   383
    fold_rev Proofterm.implies_intr_proof' constraints'
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   384
  end;
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   385
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   386
(** expanding theorems / definitions **)
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   387
33704
6aeb8454efc1 add_expand_thm: explicit indication of is_def instead of fragile heuristic, tuned signature;
wenzelm
parents: 33522
diff changeset
   388
fun add_expand_thm is_def thm thy =
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   389
  let
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   390
    val {realizes_eqns, typeof_eqns, types, realizers,
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   391
      defs, expand, prep} = ExtractionData.get thy;
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   392
36744
6e1f3d609a68 renamed Thm.get_name -> Thm.derivation_name and Thm.put_name -> Thm.name_derivation, to emphasize the true nature of these operations;
wenzelm
parents: 36620
diff changeset
   393
    val name = Thm.derivation_name thm;
33704
6aeb8454efc1 add_expand_thm: explicit indication of is_def instead of fragile heuristic, tuned signature;
wenzelm
parents: 33522
diff changeset
   394
    val _ = name <> "" orelse error "add_expand_thm: unnamed theorem";
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   395
  in
33704
6aeb8454efc1 add_expand_thm: explicit indication of is_def instead of fragile heuristic, tuned signature;
wenzelm
parents: 33522
diff changeset
   396
    thy |> ExtractionData.put
6aeb8454efc1 add_expand_thm: explicit indication of is_def instead of fragile heuristic, tuned signature;
wenzelm
parents: 33522
diff changeset
   397
      (if is_def then
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   398
        {realizes_eqns = realizes_eqns,
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   399
         typeof_eqns = add_rule ([], Logic.dest_equals (map_types
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   400
           Type.strip_sorts (prop_of (Drule.abs_def thm)))) typeof_eqns,
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   401
         types = types,
22360
26ead7ed4f4b moved eq_thm etc. to structure Thm in Pure/more_thm.ML;
wenzelm
parents: 21858
diff changeset
   402
         realizers = realizers, defs = insert Thm.eq_thm thm defs,
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   403
         expand = expand, prep = prep}
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   404
      else
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   405
        {realizes_eqns = realizes_eqns, typeof_eqns = typeof_eqns, types = types,
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   406
         realizers = realizers, defs = defs,
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   407
         expand = insert (op =) name expand, prep = prep})
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   408
  end;
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   409
33704
6aeb8454efc1 add_expand_thm: explicit indication of is_def instead of fragile heuristic, tuned signature;
wenzelm
parents: 33522
diff changeset
   410
fun extraction_expand is_def =
6aeb8454efc1 add_expand_thm: explicit indication of is_def instead of fragile heuristic, tuned signature;
wenzelm
parents: 33522
diff changeset
   411
  Thm.declaration_attribute (fn th => Context.mapping (add_expand_thm is_def th) I);
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   412
15801
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   413
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   414
(** types with computational content **)
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   415
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   416
fun add_types tys thy =
22717
74dbc7696083 canonical merge operations
haftmann
parents: 22675
diff changeset
   417
  ExtractionData.map
74dbc7696083 canonical merge operations
haftmann
parents: 22675
diff changeset
   418
    (fn {realizes_eqns, typeof_eqns, types, realizers, defs, expand, prep} =>
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   419
      {realizes_eqns = realizes_eqns, typeof_eqns = typeof_eqns,
22717
74dbc7696083 canonical merge operations
haftmann
parents: 22675
diff changeset
   420
       types = fold (AList.update (op =) o apfst (Sign.intern_type thy)) tys types,
74dbc7696083 canonical merge operations
haftmann
parents: 22675
diff changeset
   421
       realizers = realizers, defs = defs, expand = expand, prep = prep})
74dbc7696083 canonical merge operations
haftmann
parents: 22675
diff changeset
   422
    thy;
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   423
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   424
15801
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   425
(** Pure setup **)
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   426
26463
9283b4185fdf Context.>> : operate on Context.generic;
wenzelm
parents: 26435
diff changeset
   427
val _ = Context.>> (Context.map_theory
18708
4b3dadb4fe33 setup: theory -> theory;
wenzelm
parents: 18358
diff changeset
   428
  (add_types [("prop", ([], NONE))] #>
15801
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   429
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   430
   add_typeof_eqns
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   431
     ["(typeof (PROP P)) == (Type (TYPE(Null))) ==>  \
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   432
    \  (typeof (PROP Q)) == (Type (TYPE('Q))) ==>  \
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   433
    \    (typeof (PROP P ==> PROP Q)) == (Type (TYPE('Q)))",
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   434
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   435
      "(typeof (PROP Q)) == (Type (TYPE(Null))) ==>  \
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   436
    \    (typeof (PROP P ==> PROP Q)) == (Type (TYPE(Null)))",
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   437
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   438
      "(typeof (PROP P)) == (Type (TYPE('P))) ==>  \
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   439
    \  (typeof (PROP Q)) == (Type (TYPE('Q))) ==>  \
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   440
    \    (typeof (PROP P ==> PROP Q)) == (Type (TYPE('P => 'Q)))",
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   441
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   442
      "(%x. typeof (PROP P (x))) == (%x. Type (TYPE(Null))) ==>  \
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   443
    \    (typeof (!!x. PROP P (x))) == (Type (TYPE(Null)))",
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   444
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   445
      "(%x. typeof (PROP P (x))) == (%x. Type (TYPE('P))) ==>  \
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   446
    \    (typeof (!!x::'a. PROP P (x))) == (Type (TYPE('a => 'P)))",
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   447
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   448
      "(%x. typeof (f (x))) == (%x. Type (TYPE('f))) ==>  \
18708
4b3dadb4fe33 setup: theory -> theory;
wenzelm
parents: 18358
diff changeset
   449
    \    (typeof (f)) == (Type (TYPE('f)))"] #>
15801
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   450
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   451
   add_realizes_eqns
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   452
     ["(typeof (PROP P)) == (Type (TYPE(Null))) ==>  \
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   453
    \    (realizes (r) (PROP P ==> PROP Q)) ==  \
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   454
    \    (PROP realizes (Null) (PROP P) ==> PROP realizes (r) (PROP Q))",
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   455
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   456
      "(typeof (PROP P)) == (Type (TYPE('P))) ==>  \
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   457
    \  (typeof (PROP Q)) == (Type (TYPE(Null))) ==>  \
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   458
    \    (realizes (r) (PROP P ==> PROP Q)) ==  \
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   459
    \    (!!x::'P. PROP realizes (x) (PROP P) ==> PROP realizes (Null) (PROP Q))",
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   460
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   461
      "(realizes (r) (PROP P ==> PROP Q)) ==  \
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   462
    \  (!!x. PROP realizes (x) (PROP P) ==> PROP realizes (r (x)) (PROP Q))",
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   463
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   464
      "(%x. typeof (PROP P (x))) == (%x. Type (TYPE(Null))) ==>  \
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   465
    \    (realizes (r) (!!x. PROP P (x))) ==  \
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   466
    \    (!!x. PROP realizes (Null) (PROP P (x)))",
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   467
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   468
      "(realizes (r) (!!x. PROP P (x))) ==  \
18708
4b3dadb4fe33 setup: theory -> theory;
wenzelm
parents: 18358
diff changeset
   469
    \  (!!x. PROP realizes (r (x)) (PROP P (x)))"] #>
15801
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   470
33704
6aeb8454efc1 add_expand_thm: explicit indication of is_def instead of fragile heuristic, tuned signature;
wenzelm
parents: 33522
diff changeset
   471
   Attrib.setup (Binding.name "extraction_expand") (Scan.succeed (extraction_expand false))
6aeb8454efc1 add_expand_thm: explicit indication of is_def instead of fragile heuristic, tuned signature;
wenzelm
parents: 33522
diff changeset
   472
     "specify theorems to be expanded during extraction" #>
6aeb8454efc1 add_expand_thm: explicit indication of is_def instead of fragile heuristic, tuned signature;
wenzelm
parents: 33522
diff changeset
   473
   Attrib.setup (Binding.name "extraction_expand_def") (Scan.succeed (extraction_expand true))
6aeb8454efc1 add_expand_thm: explicit indication of is_def instead of fragile heuristic, tuned signature;
wenzelm
parents: 33522
diff changeset
   474
     "specify definitions to be expanded during extraction"));
15801
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   475
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   476
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   477
(**** extract program ****)
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   478
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   479
val dummyt = Const ("dummy", dummyT);
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   480
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   481
fun extract thms thy =
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   482
  let
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16363
diff changeset
   483
    val thy' = add_syntax thy;
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   484
    val {realizes_eqns, typeof_eqns, types, realizers, defs, expand, prep} =
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   485
      ExtractionData.get thy;
22717
74dbc7696083 canonical merge operations
haftmann
parents: 22675
diff changeset
   486
    val procs = maps (rev o fst o snd) types;
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   487
    val rtypes = map fst types;
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   488
    val typroc = typeof_proc [];
19466
wenzelm
parents: 19391
diff changeset
   489
    val prep = the_default (K I) prep thy' o ProofRewriteRules.elim_defs thy' false defs o
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   490
      Reconstruct.expand_proof thy' (map (rpair NONE) ("" :: expand));
16800
90eff1b52428 improved Net interface;
wenzelm
parents: 16790
diff changeset
   491
    val rrews = Net.merge (K false) (#net realizes_eqns, #net typeof_eqns);
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   492
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   493
    fun find_inst prop Ts ts vs =
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   494
      let
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   495
        val rvs = relevant_vars rtypes prop;
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   496
        val vars = vars_of prop;
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   497
        val n = Int.min (length vars, length ts);
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   498
33337
9c3b9bf81e8b eliminated some old folds;
wenzelm
parents: 33317
diff changeset
   499
        fun add_args (Var ((a, i), _), t) (vs', tye) =
20664
ffbc5a57191a member (op =);
wenzelm
parents: 20548
diff changeset
   500
          if member (op =) rvs a then
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16363
diff changeset
   501
            let val T = etype_of thy' vs Ts t
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   502
            in if T = nullT then (vs', tye)
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   503
               else (a :: vs', (("'" ^ a, i), T) :: tye)
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   504
            end
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   505
          else (vs', tye)
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   506
33957
e9afca2118d4 normalized uncurry take/drop
haftmann
parents: 33955
diff changeset
   507
      in fold_rev add_args (take n vars ~~ take n ts) ([], []) end;
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   508
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   509
    fun mk_shyps tye = maps (fn (ixn, _) =>
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   510
      Logic.mk_of_sort (TVar (ixn, []), Sign.defaultS thy)) tye;
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   511
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   512
    fun mk_sprfs cs tye = maps (fn (_, T) =>
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   513
      ProofRewriteRules.mk_of_sort_proof thy (map SOME cs)
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   514
        (T, Sign.defaultS thy)) tye;
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   515
33038
8f9594c31de4 dropped redundant gen_ prefix
haftmann
parents: 32784
diff changeset
   516
    fun find (vs: string list) = Option.map snd o find_first (curry (eq_set (op =)) vs o fst);
28375
c879d88d038a eliminated polymorphic equality;
wenzelm
parents: 28370
diff changeset
   517
    fun find' (s: string) = map_filter (fn (s', x) => if s = s' then SOME x else NONE);
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   518
46219
426ed18eba43 discontinued old-style Term.list_abs in favour of plain Term.abs;
wenzelm
parents: 44057
diff changeset
   519
    fun app_rlz_rews Ts vs t =
426ed18eba43 discontinued old-style Term.list_abs in favour of plain Term.abs;
wenzelm
parents: 44057
diff changeset
   520
      strip_abs (length Ts)
426ed18eba43 discontinued old-style Term.list_abs in favour of plain Term.abs;
wenzelm
parents: 44057
diff changeset
   521
        (freeze_thaw (condrew thy' rrews (procs @ [typroc vs, rlz_proc]))
426ed18eba43 discontinued old-style Term.list_abs in favour of plain Term.abs;
wenzelm
parents: 44057
diff changeset
   522
          (fold (Term.abs o pair "x") Ts t));
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   523
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   524
    fun realizes_null vs prop = app_rlz_rews [] vs
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   525
      (Const ("realizes", nullT --> propT --> propT) $ nullt $ prop);
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   526
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   527
    fun corr d defs vs ts Ts hs cs (PBound i) _ _ = (defs, PBound i)
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   528
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   529
      | corr d defs vs ts Ts hs cs (Abst (s, SOME T, prf)) (Abst (_, _, prf')) t =
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   530
          let val (defs', corr_prf) = corr d defs vs [] (T :: Ts)
37310
96e2b9a6f074 do not open Proofterm, which is very ould style;
wenzelm
parents: 37237
diff changeset
   531
            (dummyt :: hs) cs prf (Proofterm.incr_pboundvars 1 0 prf')
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   532
            (case t of SOME (Abs (_, _, u)) => SOME u | _ => NONE)
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   533
          in (defs', Abst (s, SOME T, corr_prf)) end
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   534
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   535
      | corr d defs vs ts Ts hs cs (AbsP (s, SOME prop, prf)) (AbsP (_, _, prf')) t =
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   536
          let
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16363
diff changeset
   537
            val T = etype_of thy' vs Ts prop;
42407
5b9dd52f5dca prefer internal types, via Simple_Syntax.read_typ;
wenzelm
parents: 42406
diff changeset
   538
            val u = if T = nullT then
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   539
                (case t of SOME u => SOME (incr_boundvars 1 u) | NONE => NONE)
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   540
              else (case t of SOME (Abs (_, _, u)) => SOME u | _ => NONE);
37310
96e2b9a6f074 do not open Proofterm, which is very ould style;
wenzelm
parents: 37237
diff changeset
   541
            val (defs', corr_prf) =
96e2b9a6f074 do not open Proofterm, which is very ould style;
wenzelm
parents: 37237
diff changeset
   542
              corr d defs vs [] (T :: Ts) (prop :: hs)
96e2b9a6f074 do not open Proofterm, which is very ould style;
wenzelm
parents: 37237
diff changeset
   543
                (prop :: cs) (Proofterm.incr_pboundvars 0 1 prf)
96e2b9a6f074 do not open Proofterm, which is very ould style;
wenzelm
parents: 37237
diff changeset
   544
                (Proofterm.incr_pboundvars 0 1 prf') u;
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   545
            val rlz = Const ("realizes", T --> propT --> propT)
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   546
          in (defs',
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   547
            if T = nullT then AbsP ("R",
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   548
              SOME (app_rlz_rews Ts vs (rlz $ nullt $ prop)),
37310
96e2b9a6f074 do not open Proofterm, which is very ould style;
wenzelm
parents: 37237
diff changeset
   549
                Proofterm.prf_subst_bounds [nullt] corr_prf)
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   550
            else Abst (s, SOME T, AbsP ("R",
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   551
              SOME (app_rlz_rews (T :: Ts) vs
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   552
                (rlz $ Bound 0 $ incr_boundvars 1 prop)), corr_prf)))
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   553
          end
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   554
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   555
      | corr d defs vs ts Ts hs cs (prf % SOME t) (prf' % _) t' =
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   556
          let
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   557
            val (Us, T) = strip_type (fastype_of1 (Ts, t));
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   558
            val (defs', corr_prf) = corr d defs vs (t :: ts) Ts hs cs prf prf'
20664
ffbc5a57191a member (op =);
wenzelm
parents: 20548
diff changeset
   559
              (if member (op =) rtypes (tname_of T) then t'
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   560
               else (case t' of SOME (u $ _) => SOME u | _ => NONE));
20664
ffbc5a57191a member (op =);
wenzelm
parents: 20548
diff changeset
   561
            val u = if not (member (op =) rtypes (tname_of T)) then t else
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   562
              let
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16363
diff changeset
   563
                val eT = etype_of thy' vs Ts t;
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   564
                val (r, Us') = if eT = nullT then (nullt, Us) else
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   565
                  (Bound (length Us), eT :: Us);
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   566
                val u = list_comb (incr_boundvars (length Us') t,
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   567
                  map Bound (length Us - 1 downto 0));
17271
2756a73f63a5 introduced some new-style AList operations
haftmann
parents: 17232
diff changeset
   568
                val u' = (case AList.lookup (op =) types (tname_of T) of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   569
                    SOME ((_, SOME f)) => f r eT u T
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   570
                  | _ => Const ("realizes", eT --> T --> T) $ r $ u)
46219
426ed18eba43 discontinued old-style Term.list_abs in favour of plain Term.abs;
wenzelm
parents: 44057
diff changeset
   571
              in app_rlz_rews Ts vs (fold_rev (Term.abs o pair "x") Us' u') end
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   572
          in (defs', corr_prf % SOME u) end
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   573
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   574
      | corr d defs vs ts Ts hs cs (prf1 %% prf2) (prf1' %% prf2') t =
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   575
          let
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   576
            val prop = Reconstruct.prop_of' hs prf2';
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16363
diff changeset
   577
            val T = etype_of thy' vs Ts prop;
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   578
            val (defs1, f, u) = if T = nullT then (defs, t, NONE) else
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   579
              (case t of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   580
                 SOME (f $ u) => (defs, SOME f, SOME u)
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   581
               | _ =>
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   582
                 let val (defs1, u) = extr d defs vs [] Ts hs prf2'
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   583
                 in (defs1, NONE, SOME u) end)
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   584
            val (defs2, corr_prf1) = corr d defs1 vs [] Ts hs cs prf1 prf1' f;
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   585
            val (defs3, corr_prf2) = corr d defs2 vs [] Ts hs cs prf2 prf2' u;
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   586
          in
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   587
            if T = nullT then (defs3, corr_prf1 %% corr_prf2) else
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   588
              (defs3, corr_prf1 % u %% corr_prf2)
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   589
          end
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   590
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   591
      | corr d defs vs ts Ts hs cs (prf0 as PThm (_, ((name, prop, SOME Ts'), body))) _ _ =
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   592
          let
37310
96e2b9a6f074 do not open Proofterm, which is very ould style;
wenzelm
parents: 37237
diff changeset
   593
            val prf = Proofterm.join_proof body;
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   594
            val (vs', tye) = find_inst prop Ts ts vs;
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   595
            val shyps = mk_shyps tye;
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   596
            val sprfs = mk_sprfs cs tye;
36042
85efdadee8ae switched PThm/PAxm etc. to use canonical order of type variables (term variables unchanged)
krauss
parents: 35985
diff changeset
   597
            val tye' = (map fst (Term.add_tvars prop [] |> rev) ~~ Ts') @ tye;
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16363
diff changeset
   598
            val T = etype_of thy' vs' [] prop;
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   599
            val defs' = if T = nullT then defs
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   600
              else fst (extr d defs vs ts Ts hs prf0)
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   601
          in
13609
73c3915553b4 Added check for axioms with "realizes Null A = A".
berghofe
parents: 13417
diff changeset
   602
            if T = nullT andalso realizes_null vs' prop aconv prop then (defs, prf0)
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17271
diff changeset
   603
            else case Symtab.lookup realizers name of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   604
              NONE => (case find vs' (find' name defs') of
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   605
                NONE =>
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   606
                  let
21858
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 21646
diff changeset
   607
                    val _ = T = nullT orelse error "corr: internal error";
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   608
                    val _ = msg d ("Building correctness proof for " ^ quote name ^
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   609
                      (if null vs' then ""
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   610
                       else " (relevant variables: " ^ commas_quote vs' ^ ")"));
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16363
diff changeset
   611
                    val prf' = prep (Reconstruct.reconstruct_proof thy' prop prf);
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   612
                    val (defs'', corr_prf0) = corr (d + 1) defs' vs' [] [] []
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   613
                      (rev shyps) prf' prf' NONE;
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   614
                    val corr_prf = mkabsp shyps corr_prf0;
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   615
                    val corr_prop = Reconstruct.prop_of corr_prf;
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   616
                    val corr_prf' =
37310
96e2b9a6f074 do not open Proofterm, which is very ould style;
wenzelm
parents: 37237
diff changeset
   617
                      Proofterm.proof_combP (Proofterm.proof_combt
28805
8136e5736808 Thm.proof_of returns proof_body;
wenzelm
parents: 28674
diff changeset
   618
                         (PThm (serial (),
36042
85efdadee8ae switched PThm/PAxm etc. to use canonical order of type variables (term variables unchanged)
krauss
parents: 35985
diff changeset
   619
                          ((corr_name name vs', corr_prop, SOME (map TVar (Term.add_tvars corr_prop [] |> rev))),
37310
96e2b9a6f074 do not open Proofterm, which is very ould style;
wenzelm
parents: 37237
diff changeset
   620
                            Future.value (Proofterm.approximate_proof_body corr_prf))),
96e2b9a6f074 do not open Proofterm, which is very ould style;
wenzelm
parents: 37237
diff changeset
   621
                              vfs_of corr_prop),
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   622
                              map PBound (length shyps - 1 downto 0)) |>
37310
96e2b9a6f074 do not open Proofterm, which is very ould style;
wenzelm
parents: 37237
diff changeset
   623
                      fold_rev Proofterm.forall_intr_proof'
96e2b9a6f074 do not open Proofterm, which is very ould style;
wenzelm
parents: 37237
diff changeset
   624
                        (map (get_var_type corr_prop) (vfs_of prop)) |>
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   625
                      mkabsp shyps
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   626
                  in
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   627
                    ((name, (vs', ((nullt, nullt), (corr_prf, corr_prf')))) :: defs'',
37310
96e2b9a6f074 do not open Proofterm, which is very ould style;
wenzelm
parents: 37237
diff changeset
   628
                     Proofterm.proof_combP (prf_subst_TVars tye' corr_prf', sprfs))
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   629
                  end
37310
96e2b9a6f074 do not open Proofterm, which is very ould style;
wenzelm
parents: 37237
diff changeset
   630
              | SOME (_, (_, prf')) =>
96e2b9a6f074 do not open Proofterm, which is very ould style;
wenzelm
parents: 37237
diff changeset
   631
                  (defs', Proofterm.proof_combP (prf_subst_TVars tye' prf', sprfs)))
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   632
            | SOME rs => (case find vs' rs of
37310
96e2b9a6f074 do not open Proofterm, which is very ould style;
wenzelm
parents: 37237
diff changeset
   633
                SOME (_, prf') => (defs', Proofterm.proof_combP (prf_subst_TVars tye' prf', sprfs))
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   634
              | NONE => error ("corr: no realizer for instance of theorem " ^
26939
1035c89b4c02 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26653
diff changeset
   635
                  quote name ^ ":\n" ^ Syntax.string_of_term_global thy' (Envir.beta_norm
37310
96e2b9a6f074 do not open Proofterm, which is very ould style;
wenzelm
parents: 37237
diff changeset
   636
                    (Reconstruct.prop_of (Proofterm.proof_combt (prf0, ts))))))
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   637
          end
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   638
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   639
      | corr d defs vs ts Ts hs cs (prf0 as PAxm (s, prop, SOME Ts')) _ _ =
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   640
          let
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   641
            val (vs', tye) = find_inst prop Ts ts vs;
36042
85efdadee8ae switched PThm/PAxm etc. to use canonical order of type variables (term variables unchanged)
krauss
parents: 35985
diff changeset
   642
            val tye' = (map fst (Term.add_tvars prop [] |> rev) ~~ Ts') @ tye
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   643
          in
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16363
diff changeset
   644
            if etype_of thy' vs' [] prop = nullT andalso
13609
73c3915553b4 Added check for axioms with "realizes Null A = A".
berghofe
parents: 13417
diff changeset
   645
              realizes_null vs' prop aconv prop then (defs, prf0)
18956
c050ae1f8f52 Envir.(beta_)eta_contract;
wenzelm
parents: 18928
diff changeset
   646
            else case find vs' (Symtab.lookup_list realizers s) of
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   647
              SOME (_, prf) => (defs,
37310
96e2b9a6f074 do not open Proofterm, which is very ould style;
wenzelm
parents: 37237
diff changeset
   648
                Proofterm.proof_combP (prf_subst_TVars tye' prf, mk_sprfs cs tye))
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   649
            | NONE => error ("corr: no realizer for instance of axiom " ^
26939
1035c89b4c02 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26653
diff changeset
   650
                quote s ^ ":\n" ^ Syntax.string_of_term_global thy' (Envir.beta_norm
37310
96e2b9a6f074 do not open Proofterm, which is very ould style;
wenzelm
parents: 37237
diff changeset
   651
                  (Reconstruct.prop_of (Proofterm.proof_combt (prf0, ts)))))
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   652
          end
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   653
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   654
      | corr d defs vs ts Ts hs _ _ _ _ = error "corr: bad proof"
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   655
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   656
    and extr d defs vs ts Ts hs (PBound i) = (defs, Bound i)
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   657
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   658
      | extr d defs vs ts Ts hs (Abst (s, SOME T, prf)) =
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   659
          let val (defs', t) = extr d defs vs []
37310
96e2b9a6f074 do not open Proofterm, which is very ould style;
wenzelm
parents: 37237
diff changeset
   660
            (T :: Ts) (dummyt :: hs) (Proofterm.incr_pboundvars 1 0 prf)
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   661
          in (defs', Abs (s, T, t)) end
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   662
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   663
      | extr d defs vs ts Ts hs (AbsP (s, SOME t, prf)) =
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   664
          let
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16363
diff changeset
   665
            val T = etype_of thy' vs Ts t;
37310
96e2b9a6f074 do not open Proofterm, which is very ould style;
wenzelm
parents: 37237
diff changeset
   666
            val (defs', t) =
96e2b9a6f074 do not open Proofterm, which is very ould style;
wenzelm
parents: 37237
diff changeset
   667
              extr d defs vs [] (T :: Ts) (t :: hs) (Proofterm.incr_pboundvars 0 1 prf)
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   668
          in (defs',
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   669
            if T = nullT then subst_bound (nullt, t) else Abs (s, T, t))
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   670
          end
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   671
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   672
      | extr d defs vs ts Ts hs (prf % SOME t) =
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   673
          let val (defs', u) = extr d defs vs (t :: ts) Ts hs prf
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   674
          in (defs',
20664
ffbc5a57191a member (op =);
wenzelm
parents: 20548
diff changeset
   675
            if member (op =) rtypes (tname_of (body_type (fastype_of1 (Ts, t)))) then u
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   676
            else u $ t)
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   677
          end
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   678
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   679
      | extr d defs vs ts Ts hs (prf1 %% prf2) =
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   680
          let
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   681
            val (defs', f) = extr d defs vs [] Ts hs prf1;
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   682
            val prop = Reconstruct.prop_of' hs prf2;
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16363
diff changeset
   683
            val T = etype_of thy' vs Ts prop
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   684
          in
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   685
            if T = nullT then (defs', f) else
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   686
              let val (defs'', t) = extr d defs' vs [] Ts hs prf2
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   687
              in (defs'', f $ t) end
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   688
          end
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   689
28805
8136e5736808 Thm.proof_of returns proof_body;
wenzelm
parents: 28674
diff changeset
   690
      | extr d defs vs ts Ts hs (prf0 as PThm (_, ((s, prop, SOME Ts'), body))) =
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   691
          let
37310
96e2b9a6f074 do not open Proofterm, which is very ould style;
wenzelm
parents: 37237
diff changeset
   692
            val prf = Proofterm.join_proof body;
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   693
            val (vs', tye) = find_inst prop Ts ts vs;
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   694
            val shyps = mk_shyps tye;
36042
85efdadee8ae switched PThm/PAxm etc. to use canonical order of type variables (term variables unchanged)
krauss
parents: 35985
diff changeset
   695
            val tye' = (map fst (Term.add_tvars prop [] |> rev) ~~ Ts') @ tye
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   696
          in
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17271
diff changeset
   697
            case Symtab.lookup realizers s of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   698
              NONE => (case find vs' (find' s defs) of
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   699
                NONE =>
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   700
                  let
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   701
                    val _ = msg d ("Extracting " ^ quote s ^
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   702
                      (if null vs' then ""
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   703
                       else " (relevant variables: " ^ commas_quote vs' ^ ")"));
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16363
diff changeset
   704
                    val prf' = prep (Reconstruct.reconstruct_proof thy' prop prf);
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   705
                    val (defs', t) = extr (d + 1) defs vs' [] [] [] prf';
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   706
                    val (defs'', corr_prf) = corr (d + 1) defs' vs' [] [] []
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   707
                      (rev shyps) prf' prf' (SOME t);
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   708
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   709
                    val nt = Envir.beta_norm t;
20664
ffbc5a57191a member (op =);
wenzelm
parents: 20548
diff changeset
   710
                    val args = filter_out (fn v => member (op =) rtypes
ffbc5a57191a member (op =);
wenzelm
parents: 20548
diff changeset
   711
                      (tname_of (body_type (fastype_of v)))) (vfs_of prop);
33317
b4534348b8fd standardized filter/filter_out;
wenzelm
parents: 33245
diff changeset
   712
                    val args' = filter (fn v => Logic.occs (v, nt)) args;
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   713
                    val t' = mkabs args' nt;
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   714
                    val T = fastype_of t';
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   715
                    val cname = extr_name s vs';
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   716
                    val c = Const (cname, T);
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   717
                    val u = mkabs args (list_comb (c, args'));
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   718
                    val eqn = Logic.mk_equals (c, t');
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   719
                    val rlz =
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   720
                      Const ("realizes", fastype_of nt --> propT --> propT);
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   721
                    val lhs = app_rlz_rews [] vs' (rlz $ nt $ prop);
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   722
                    val rhs = app_rlz_rews [] vs' (rlz $ list_comb (c, args') $ prop);
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   723
                    val f = app_rlz_rews [] vs'
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   724
                      (Abs ("x", T, rlz $ list_comb (Bound 0, args') $ prop));
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   725
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   726
                    val corr_prf' = mkabsp shyps
37310
96e2b9a6f074 do not open Proofterm, which is very ould style;
wenzelm
parents: 37237
diff changeset
   727
                      (chtype [] Proofterm.equal_elim_axm %> lhs %> rhs %%
96e2b9a6f074 do not open Proofterm, which is very ould style;
wenzelm
parents: 37237
diff changeset
   728
                       (chtype [propT] Proofterm.symmetric_axm %> rhs %> lhs %%
96e2b9a6f074 do not open Proofterm, which is very ould style;
wenzelm
parents: 37237
diff changeset
   729
                         (chtype [T, propT] Proofterm.combination_axm %> f %> f %> c %> t' %%
96e2b9a6f074 do not open Proofterm, which is very ould style;
wenzelm
parents: 37237
diff changeset
   730
                           (chtype [T --> propT] Proofterm.reflexive_axm %> f) %%
46909
3c73a121a387 more explicit indication of def names;
wenzelm
parents: 46219
diff changeset
   731
                           PAxm (Thm.def_name cname, eqn,
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   732
                             SOME (map TVar (Term.add_tvars eqn [] |> rev))))) %% corr_prf);
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   733
                    val corr_prop = Reconstruct.prop_of corr_prf';
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   734
                    val corr_prf'' =
37310
96e2b9a6f074 do not open Proofterm, which is very ould style;
wenzelm
parents: 37237
diff changeset
   735
                      Proofterm.proof_combP (Proofterm.proof_combt
28805
8136e5736808 Thm.proof_of returns proof_body;
wenzelm
parents: 28674
diff changeset
   736
                        (PThm (serial (),
36042
85efdadee8ae switched PThm/PAxm etc. to use canonical order of type variables (term variables unchanged)
krauss
parents: 35985
diff changeset
   737
                         ((corr_name s vs', corr_prop, SOME (map TVar (Term.add_tvars corr_prop [] |> rev))),
37310
96e2b9a6f074 do not open Proofterm, which is very ould style;
wenzelm
parents: 37237
diff changeset
   738
                           Future.value (Proofterm.approximate_proof_body corr_prf'))),
96e2b9a6f074 do not open Proofterm, which is very ould style;
wenzelm
parents: 37237
diff changeset
   739
                            vfs_of corr_prop),
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   740
                             map PBound (length shyps - 1 downto 0)) |>
37310
96e2b9a6f074 do not open Proofterm, which is very ould style;
wenzelm
parents: 37237
diff changeset
   741
                      fold_rev Proofterm.forall_intr_proof'
96e2b9a6f074 do not open Proofterm, which is very ould style;
wenzelm
parents: 37237
diff changeset
   742
                        (map (get_var_type corr_prop) (vfs_of prop)) |>
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   743
                      mkabsp shyps
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   744
                  in
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   745
                    ((s, (vs', ((t', u), (corr_prf', corr_prf'')))) :: defs'',
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   746
                     subst_TVars tye' u)
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   747
                  end
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   748
              | SOME ((_, u), _) => (defs, subst_TVars tye' u))
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   749
            | SOME rs => (case find vs' rs of
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   750
                SOME (t, _) => (defs, subst_TVars tye' t)
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   751
              | NONE => error ("extr: no realizer for instance of theorem " ^
26939
1035c89b4c02 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26653
diff changeset
   752
                  quote s ^ ":\n" ^ Syntax.string_of_term_global thy' (Envir.beta_norm
37310
96e2b9a6f074 do not open Proofterm, which is very ould style;
wenzelm
parents: 37237
diff changeset
   753
                    (Reconstruct.prop_of (Proofterm.proof_combt (prf0, ts))))))
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   754
          end
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   755
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   756
      | extr d defs vs ts Ts hs (prf0 as PAxm (s, prop, SOME Ts')) =
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   757
          let
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   758
            val (vs', tye) = find_inst prop Ts ts vs;
36042
85efdadee8ae switched PThm/PAxm etc. to use canonical order of type variables (term variables unchanged)
krauss
parents: 35985
diff changeset
   759
            val tye' = (map fst (Term.add_tvars prop [] |> rev) ~~ Ts') @ tye
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   760
          in
18956
c050ae1f8f52 Envir.(beta_)eta_contract;
wenzelm
parents: 18928
diff changeset
   761
            case find vs' (Symtab.lookup_list realizers s) of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   762
              SOME (t, _) => (defs, subst_TVars tye' t)
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   763
            | NONE => error ("extr: no realizer for instance of axiom " ^
26939
1035c89b4c02 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26653
diff changeset
   764
                quote s ^ ":\n" ^ Syntax.string_of_term_global thy' (Envir.beta_norm
37310
96e2b9a6f074 do not open Proofterm, which is very ould style;
wenzelm
parents: 37237
diff changeset
   765
                  (Reconstruct.prop_of (Proofterm.proof_combt (prf0, ts)))))
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   766
          end
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   767
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   768
      | extr d defs vs ts Ts hs _ = error "extr: bad proof";
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   769
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   770
    fun prep_thm (thm, vs) =
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   771
      let
26626
c6231d64d264 rep_cterm/rep_thm: no longer dereference theory_ref;
wenzelm
parents: 26481
diff changeset
   772
        val thy = Thm.theory_of_thm thm;
c6231d64d264 rep_cterm/rep_thm: no longer dereference theory_ref;
wenzelm
parents: 26481
diff changeset
   773
        val prop = Thm.prop_of thm;
28814
463c9e9111ae clarified Thm.proof_body_of vs. Thm.proof_of;
wenzelm
parents: 28812
diff changeset
   774
        val prf = Thm.proof_of thm;
36744
6e1f3d609a68 renamed Thm.get_name -> Thm.derivation_name and Thm.put_name -> Thm.name_derivation, to emphasize the true nature of these operations;
wenzelm
parents: 36620
diff changeset
   775
        val name = Thm.derivation_name thm;
21858
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 21646
diff changeset
   776
        val _ = name <> "" orelse error "extraction: unnamed theorem";
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 21646
diff changeset
   777
        val _ = etype_of thy' vs [] prop <> nullT orelse error ("theorem " ^
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   778
          quote name ^ " has no computational content")
22596
d0d2af4db18f rep_thm/cterm/ctyp: removed obsolete sign field;
wenzelm
parents: 22360
diff changeset
   779
      in (Reconstruct.reconstruct_proof thy prop prf, vs) end;
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   780
33245
65232054ffd0 eliminated some old folds;
wenzelm
parents: 33038
diff changeset
   781
    val defs =
65232054ffd0 eliminated some old folds;
wenzelm
parents: 33038
diff changeset
   782
      fold (fn (prf, vs) => fn defs => fst (extr 0 defs vs [] [] [] prf))
65232054ffd0 eliminated some old folds;
wenzelm
parents: 33038
diff changeset
   783
        (map prep_thm thms) [];
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   784
16149
d8cac577493c Theory.restore_naming;
wenzelm
parents: 15801
diff changeset
   785
    fun add_def (s, (vs, ((t, u), (prf, _)))) thy =
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16363
diff changeset
   786
      (case Sign.const_type thy (extr_name s vs) of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   787
         NONE =>
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   788
           let
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   789
             val corr_prop = Reconstruct.prop_of prf;
33832
cff42395c246 explicitly mark some legacy freeze operations;
wenzelm
parents: 33704
diff changeset
   790
             val ft = Type.legacy_freeze t;
cff42395c246 explicitly mark some legacy freeze operations;
wenzelm
parents: 33704
diff changeset
   791
             val fu = Type.legacy_freeze u;
22750
bff5d59de79b adds extracted program to code theorem table
haftmann
parents: 22717
diff changeset
   792
             val (def_thms, thy') = if t = nullt then ([], thy) else
bff5d59de79b adds extracted program to code theorem table
haftmann
parents: 22717
diff changeset
   793
               thy
30435
e62d6ecab6ad explicit Binding.qualified_name -- prevents implicitly qualified bstring;
wenzelm
parents: 30364
diff changeset
   794
               |> Sign.add_consts_i [(Binding.qualified_name (extr_name s vs), fastype_of ft, NoSyn)]
46909
3c73a121a387 more explicit indication of def names;
wenzelm
parents: 46219
diff changeset
   795
               |> Global_Theory.add_defs false
3c73a121a387 more explicit indication of def names;
wenzelm
parents: 46219
diff changeset
   796
                  [((Binding.qualified_name (Thm.def_name (extr_name s vs)),
22750
bff5d59de79b adds extracted program to code theorem table
haftmann
parents: 22717
diff changeset
   797
                    Logic.mk_equals (head_of (strip_abs_body fu), ft)), [])]
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   798
           in
22750
bff5d59de79b adds extracted program to code theorem table
haftmann
parents: 22717
diff changeset
   799
             thy'
39557
fe5722fce758 renamed structure PureThy to Pure_Thy and moved most content to Global_Theory, to emphasize that this is global-only;
wenzelm
parents: 39288
diff changeset
   800
             |> Global_Theory.store_thm (Binding.qualified_name (corr_name s vs),
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36953
diff changeset
   801
                  Thm.varifyT_global (funpow (length (vars_of corr_prop))
35985
0bbf0d2348f9 moved Drule.forall_intr_frees to Thm.forall_intr_frees (in more_thm.ML, which is loaded before pure_thy.ML);
wenzelm
parents: 35845
diff changeset
   802
                    (Thm.forall_elim_var 0) (Thm.forall_intr_frees
44057
fda143b5c2f5 modernized strcture Proof_Checker;
wenzelm
parents: 42407
diff changeset
   803
                      (Proof_Checker.thm_of_proof thy'
26481
92e901171cc8 simplified PureThy.store_thm;
wenzelm
parents: 26463
diff changeset
   804
                       (fst (Proofterm.freeze_thaw_prf prf))))))
22750
bff5d59de79b adds extracted program to code theorem table
haftmann
parents: 22717
diff changeset
   805
             |> snd
28370
37f56e6e702d removed obsolete name convention "func"
haftmann
parents: 27865
diff changeset
   806
             |> fold Code.add_default_eqn def_thms
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   807
           end
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   808
       | SOME _ => thy);
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   809
16149
d8cac577493c Theory.restore_naming;
wenzelm
parents: 15801
diff changeset
   810
  in
d8cac577493c Theory.restore_naming;
wenzelm
parents: 15801
diff changeset
   811
    thy
30435
e62d6ecab6ad explicit Binding.qualified_name -- prevents implicitly qualified bstring;
wenzelm
parents: 30364
diff changeset
   812
    |> Sign.root_path
16149
d8cac577493c Theory.restore_naming;
wenzelm
parents: 15801
diff changeset
   813
    |> fold_rev add_def defs
22796
34c316d7b630 renamed some old names Theory.xxx to Sign.xxx;
wenzelm
parents: 22750
diff changeset
   814
    |> Sign.restore_naming thy
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   815
  end;
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   816
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   817
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   818
(**** interface ****)
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   819
36950
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 36744
diff changeset
   820
val parse_vars = Scan.optional (Parse.$$$ "(" |-- Parse.list1 Parse.name --| Parse.$$$ ")") [];
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   821
24867
e5b55d7be9bb simplified interfaces for outer syntax;
wenzelm
parents: 24712
diff changeset
   822
val _ =
46961
5c6955f487e5 outer syntax command definitions based on formal command_spec derived from theory header declarations;
wenzelm
parents: 46909
diff changeset
   823
  Outer_Syntax.command ("realizers", Keyword.thy_decl)
5c6955f487e5 outer syntax command definitions based on formal command_spec derived from theory header declarations;
wenzelm
parents: 46909
diff changeset
   824
    "specify realizers for primitive axioms / theorems, together with correctness proof"
36950
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 36744
diff changeset
   825
    (Scan.repeat1 (Parse.xname -- parse_vars --| Parse.$$$ ":" -- Parse.string -- Parse.string) >>
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   826
     (fn xs => Toplevel.theory (fn thy => add_realizers
39557
fe5722fce758 renamed structure PureThy to Pure_Thy and moved most content to Global_Theory, to emphasize that this is global-only;
wenzelm
parents: 39288
diff changeset
   827
       (map (fn (((a, vs), s1), s2) => (Global_Theory.get_thm thy a, (vs, s1, s2))) xs) thy)));
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   828
24867
e5b55d7be9bb simplified interfaces for outer syntax;
wenzelm
parents: 24712
diff changeset
   829
val _ =
46961
5c6955f487e5 outer syntax command definitions based on formal command_spec derived from theory header declarations;
wenzelm
parents: 46909
diff changeset
   830
  Outer_Syntax.command ("realizability", Keyword.thy_decl)
5c6955f487e5 outer syntax command definitions based on formal command_spec derived from theory header declarations;
wenzelm
parents: 46909
diff changeset
   831
    "add equations characterizing realizability"
5c6955f487e5 outer syntax command definitions based on formal command_spec derived from theory header declarations;
wenzelm
parents: 46909
diff changeset
   832
    (Scan.repeat1 Parse.string >> (Toplevel.theory o add_realizes_eqns));
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   833
24867
e5b55d7be9bb simplified interfaces for outer syntax;
wenzelm
parents: 24712
diff changeset
   834
val _ =
46961
5c6955f487e5 outer syntax command definitions based on formal command_spec derived from theory header declarations;
wenzelm
parents: 46909
diff changeset
   835
  Outer_Syntax.command ("extract_type", Keyword.thy_decl)
5c6955f487e5 outer syntax command definitions based on formal command_spec derived from theory header declarations;
wenzelm
parents: 46909
diff changeset
   836
    "add equations characterizing type of extracted program"
5c6955f487e5 outer syntax command definitions based on formal command_spec derived from theory header declarations;
wenzelm
parents: 46909
diff changeset
   837
    (Scan.repeat1 Parse.string >> (Toplevel.theory o add_typeof_eqns));
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   838
24867
e5b55d7be9bb simplified interfaces for outer syntax;
wenzelm
parents: 24712
diff changeset
   839
val _ =
46961
5c6955f487e5 outer syntax command definitions based on formal command_spec derived from theory header declarations;
wenzelm
parents: 46909
diff changeset
   840
  Outer_Syntax.command ("extract", Keyword.thy_decl) "extract terms from proofs"
36950
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 36744
diff changeset
   841
    (Scan.repeat1 (Parse.xname -- parse_vars) >> (fn xs => Toplevel.theory (fn thy =>
39557
fe5722fce758 renamed structure PureThy to Pure_Thy and moved most content to Global_Theory, to emphasize that this is global-only;
wenzelm
parents: 39288
diff changeset
   842
      extract (map (apfst (Global_Theory.get_thm thy)) xs) thy)));
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   843
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16363
diff changeset
   844
val etype_of = etype_of o add_syntax;
13714
bdd483321f4b - exported functions etype_of and mk_typ
berghofe
parents: 13609
diff changeset
   845
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   846
end;