src/Pure/Proof/extraction.ML
author wenzelm
Wed, 31 Dec 2008 18:53:16 +0100
changeset 29270 0eade173f77e
parent 29265 5b4247055bd7
child 29579 cb520b766e00
permissions -rw-r--r--
moved old add_type_XXX, add_term_XXX etc. to structure OldTerm;
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
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
    18
  val add_expand_thms : thm list -> 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
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
    27
end;
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
    28
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
    29
structure Extraction : EXTRACTION =
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
    30
struct
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
    31
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
    32
open Proofterm;
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
    33
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
    34
(**** tools ****)
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
    35
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
    36
fun add_syntax thy =
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
    37
  thy
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
    38
  |> Theory.copy
24712
64ed05609568 proper Sign operations instead of Theory aliases;
wenzelm
parents: 24707
diff changeset
    39
  |> Sign.root_path
22796
34c316d7b630 renamed some old names Theory.xxx to Sign.xxx;
wenzelm
parents: 22750
diff changeset
    40
  |> Sign.add_types [("Type", 0, NoSyn), ("Null", 0, NoSyn)]
34c316d7b630 renamed some old names Theory.xxx to Sign.xxx;
wenzelm
parents: 22750
diff changeset
    41
  |> Sign.add_consts
14854
61bdf2ae4dc5 removed obsolete sort 'logic';
wenzelm
parents: 14472
diff changeset
    42
      [("typeof", "'b::{} => Type", NoSyn),
61bdf2ae4dc5 removed obsolete sort 'logic';
wenzelm
parents: 14472
diff changeset
    43
       ("Type", "'a::{} itself => Type", NoSyn),
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
    44
       ("Null", "Null", NoSyn),
14854
61bdf2ae4dc5 removed obsolete sort 'logic';
wenzelm
parents: 14472
diff changeset
    45
       ("realizes", "'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
28375
c879d88d038a eliminated polymorphic equality;
wenzelm
parents: 28370
diff changeset
    72
  take_prefix (fn s => s <> ":") o 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
e6e826bb8c3c Added program extraction module.
berghofe
parents:
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
13417
12cc77f90811 Tuned type constraint of function merge_rules to make smlnj happy.
berghofe
parents: 13402
diff changeset
    84
fun merge_rules
12cc77f90811 Tuned type constraint of function merge_rules to make smlnj happy.
berghofe
parents: 13402
diff changeset
    85
  ({next, rs = rs1, net} : rules) ({next = next2, rs = rs2, ...} : rules) =
23178
07ba6b58b3d2 simplified/unified list fold;
wenzelm
parents: 22924
diff changeset
    86
  List.foldr add_rule {next = next, rs = rs1, net = net} (subtract (op =) rs1 rs2);
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
    87
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16363
diff changeset
    88
fun condrew thy rules procs =
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
    89
  let
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
    90
    fun rew tm =
17203
29b2563f5c11 refer to theory instead of low-level tsig;
wenzelm
parents: 17057
diff changeset
    91
      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
    92
    and condrew' tm =
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
    93
      let
15399
683d83051d6a Added term cache to function condrew in order to speed up rewriting.
berghofe
parents: 14981
diff changeset
    94
        val cache = ref ([] : (term * term) list);
17232
148c241d2492 some 'assoc' etc. refactoring
haftmann
parents: 17223
diff changeset
    95
        fun lookup f x = (case AList.lookup (op =) (!cache) x of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
    96
            NONE =>
15399
683d83051d6a Added term cache to function condrew in order to speed up rewriting.
berghofe
parents: 14981
diff changeset
    97
              let val y = f x
683d83051d6a Added term cache to function condrew in order to speed up rewriting.
berghofe
parents: 14981
diff changeset
    98
              in (cache := (x, y) :: !cache; y) end
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
    99
          | SOME y => y);
15399
683d83051d6a Added term cache to function condrew in order to speed up rewriting.
berghofe
parents: 14981
diff changeset
   100
      in
683d83051d6a Added term cache to function condrew in order to speed up rewriting.
berghofe
parents: 14981
diff changeset
   101
        get_first (fn (_, (prems, (tm1, tm2))) =>
683d83051d6a Added term cache to function condrew in order to speed up rewriting.
berghofe
parents: 14981
diff changeset
   102
        let
19466
wenzelm
parents: 19391
diff changeset
   103
          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
   104
          val inc = Logic.incr_indexes ([], maxidx_of_term tm + 1);
18184
43c4589a9a78 tuned Pattern.match/unify;
wenzelm
parents: 17412
diff changeset
   105
          val env as (Tenv, tenv) = Pattern.match thy (inc tm1, tm) (Vartab.empty, Vartab.empty);
15798
016f3be5a5ec Adapted to new interface of instantiation and unification / matching functions.
berghofe
parents: 15574
diff changeset
   106
          val prems' = map (pairself (Envir.subst_vars env o inc o ren)) prems;
15399
683d83051d6a Added term cache to function condrew in order to speed up rewriting.
berghofe
parents: 14981
diff changeset
   107
          val env' = Envir.Envir
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   108
            {maxidx = Library.foldl Int.max
15399
683d83051d6a Added term cache to function condrew in order to speed up rewriting.
berghofe
parents: 14981
diff changeset
   109
              (~1, map (Int.max o pairself maxidx_of_term) prems'),
15798
016f3be5a5ec Adapted to new interface of instantiation and unification / matching functions.
berghofe
parents: 15574
diff changeset
   110
             iTs = Tenv, asol = tenv};
18184
43c4589a9a78 tuned Pattern.match/unify;
wenzelm
parents: 17412
diff changeset
   111
          val env'' = fold (Pattern.unify thy o pairself (lookup rew)) prems' env';
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   112
        in SOME (Envir.norm_term env'' (inc (ren tm2)))
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   113
        end handle Pattern.MATCH => NONE | Pattern.Unif => NONE)
16486
1a12cdb6ee6b get_thm(s): Name;
wenzelm
parents: 16458
diff changeset
   114
          (sort (int_ord o pairself fst)
18956
c050ae1f8f52 Envir.(beta_)eta_contract;
wenzelm
parents: 18928
diff changeset
   115
            (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
   116
      end;
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   117
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   118
  in rew end;
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   119
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   120
val chtype = change_type o SOME;
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   121
16195
wenzelm
parents: 16149
diff changeset
   122
fun extr_name s vs = NameSpace.append "extr" (space_implode "_" (s :: vs));
wenzelm
parents: 16149
diff changeset
   123
fun corr_name s vs = extr_name s vs ^ "_correctness";
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   124
16195
wenzelm
parents: 16149
diff changeset
   125
fun msg d s = priority (Symbol.spaces d ^ s);
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   126
28812
413695e07bd4 Frees in PThms are now quantified in the order of their appearance in the
berghofe
parents: 28805
diff changeset
   127
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
   128
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
   129
fun vfs_of t = vars_of t @ frees_of t;
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   130
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   131
fun forall_intr_prf (t, prf) =
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   132
  let val (a, T) = (case t of Var ((a, _), T) => (a, T) | Free p => p)
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   133
  in Abst (a, SOME T, prf_abstract_over t prf) end;
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   134
23178
07ba6b58b3d2 simplified/unified list fold;
wenzelm
parents: 22924
diff changeset
   135
val mkabs = List.foldr (fn (v, t) => Abs ("x", fastype_of v, abstract_over (v, t)));
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   136
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   137
fun strip_abs 0 t = t
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   138
  | strip_abs n (Abs (_, _, t)) = strip_abs (n-1) t
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   139
  | strip_abs _ _ = error "strip_abs: not an abstraction";
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   140
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   141
fun prf_subst_TVars tye =
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   142
  map_proof_terms (subst_TVars tye) (typ_subst_TVars tye);
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   143
23178
07ba6b58b3d2 simplified/unified list fold;
wenzelm
parents: 22924
diff changeset
   144
fun relevant_vars types prop = List.foldr (fn
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   145
      (Var ((a, i), T), vs) => (case strip_type T of
20664
ffbc5a57191a member (op =);
wenzelm
parents: 20548
diff changeset
   146
        (_, Type (s, _)) => if member (op =) types s then a :: vs else vs
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   147
      | _ => vs)
15574
b1d1b5bfc464 Removed practically all references to Library.foldr.
skalberg
parents: 15570
diff changeset
   148
    | (_, vs) => vs) [] (vars_of prop);
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   149
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   150
fun tname_of (Type (s, _)) = s
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   151
  | tname_of _ = "";
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   152
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   153
fun get_var_type t =
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   154
  let
16865
fb39dcfc1c24 tuned fold on terms;
wenzelm
parents: 16800
diff changeset
   155
    val vs = Term.add_vars t [];
fb39dcfc1c24 tuned fold on terms;
wenzelm
parents: 16800
diff changeset
   156
    val fs = Term.add_frees t [];
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   157
  in fn 
17232
148c241d2492 some 'assoc' etc. refactoring
haftmann
parents: 17223
diff changeset
   158
      Var (ixn, _) => (case AList.lookup (op =) vs ixn of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   159
          NONE => error "get_var_type: no such variable in term"
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   160
        | SOME T => Var (ixn, T))
17232
148c241d2492 some 'assoc' etc. refactoring
haftmann
parents: 17223
diff changeset
   161
    | Free (s, _) => (case AList.lookup (op =) fs s of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   162
          NONE => error "get_var_type: no such variable in term"
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   163
        | SOME T => Free (s, T))
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   164
    | _ => error "get_var_type: not a variable"
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   165
  end;
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   166
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   167
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   168
(**** theory data ****)
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   169
22846
fb79144af9a3 simplified DataFun interfaces;
wenzelm
parents: 22796
diff changeset
   170
(* theory data *)
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   171
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16363
diff changeset
   172
structure ExtractionData = TheoryDataFun
22846
fb79144af9a3 simplified DataFun interfaces;
wenzelm
parents: 22796
diff changeset
   173
(
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   174
  type T =
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   175
    {realizes_eqns : rules,
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   176
     typeof_eqns : rules,
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   177
     types : (string * ((term -> term option) list *
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   178
       (term -> typ -> term -> typ -> term) option)) list,
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   179
     realizers : (string list * (term * proof)) list Symtab.table,
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   180
     defs : thm list,
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   181
     expand : (string * term) list,
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16363
diff changeset
   182
     prep : (theory -> proof -> proof) option}
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   183
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   184
  val empty =
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   185
    {realizes_eqns = empty_rules,
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   186
     typeof_eqns = empty_rules,
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   187
     types = [],
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   188
     realizers = Symtab.empty,
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   189
     defs = [],
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   190
     expand = [],
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   191
     prep = NONE};
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   192
  val copy = I;
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16363
diff changeset
   193
  val extend = I;
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   194
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16363
diff changeset
   195
  fun merge _
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   196
    (({realizes_eqns = realizes_eqns1, typeof_eqns = typeof_eqns1, types = types1,
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   197
       realizers = realizers1, defs = defs1, expand = expand1, prep = prep1},
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   198
      {realizes_eqns = realizes_eqns2, typeof_eqns = typeof_eqns2, types = types2,
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   199
       realizers = realizers2, defs = defs2, expand = expand2, prep = prep2}) : T * T) =
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   200
    {realizes_eqns = merge_rules realizes_eqns1 realizes_eqns2,
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   201
     typeof_eqns = merge_rules typeof_eqns1 typeof_eqns2,
22717
74dbc7696083 canonical merge operations
haftmann
parents: 22675
diff changeset
   202
     types = AList.merge (op =) (K true) (types1, types2),
19305
5c16895d548b avoid polymorphic equality;
wenzelm
parents: 18956
diff changeset
   203
     realizers = Symtab.merge_list (gen_eq_set (op =) o pairself #1) (realizers1, realizers2),
22662
3e492ba59355 canonical merge operations
haftmann
parents: 22596
diff changeset
   204
     defs = Library.merge Thm.eq_thm (defs1, defs2),
3e492ba59355 canonical merge operations
haftmann
parents: 22596
diff changeset
   205
     expand = Library.merge (op =) (expand1, expand2),
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   206
     prep = (case prep1 of NONE => prep2 | _ => prep1)};
22846
fb79144af9a3 simplified DataFun interfaces;
wenzelm
parents: 22796
diff changeset
   207
);
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   208
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   209
fun read_condeq thy =
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16363
diff changeset
   210
  let val thy' = add_syntax thy
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   211
  in fn s =>
24707
dfeb98f84e93 Syntax.parse/check/read;
wenzelm
parents: 24624
diff changeset
   212
    let val t = Logic.varify (Syntax.read_prop_global thy' s)
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   213
    in (map Logic.dest_equals (Logic.strip_imp_prems t),
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   214
      Logic.dest_equals (Logic.strip_imp_concl t))
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   215
    end handle TERM _ => error ("Not a (conditional) meta equality:\n" ^ s)
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   216
  end;
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   217
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   218
(** preprocessor **)
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   219
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   220
fun set_preprocessor prep thy =
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   221
  let val {realizes_eqns, typeof_eqns, types, realizers,
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   222
    defs, expand, ...} = ExtractionData.get thy
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   223
  in
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   224
    ExtractionData.put
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   225
      {realizes_eqns = realizes_eqns, typeof_eqns = typeof_eqns, types = types,
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   226
       realizers = realizers, defs = defs, expand = expand, prep = SOME prep} thy
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   227
  end;
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   228
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   229
(** equations characterizing realizability **)
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   230
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   231
fun gen_add_realizes_eqns prep_eq eqns thy =
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   232
  let val {realizes_eqns, typeof_eqns, types, realizers,
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   233
    defs, expand, prep} = ExtractionData.get thy;
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   234
  in
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   235
    ExtractionData.put
23178
07ba6b58b3d2 simplified/unified list fold;
wenzelm
parents: 22924
diff changeset
   236
      {realizes_eqns = List.foldr add_rule realizes_eqns (map (prep_eq thy) eqns),
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   237
       typeof_eqns = typeof_eqns, types = types, realizers = realizers,
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   238
       defs = defs, expand = expand, prep = prep} thy
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   239
  end
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   240
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   241
val add_realizes_eqns_i = gen_add_realizes_eqns (K I);
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   242
val add_realizes_eqns = gen_add_realizes_eqns read_condeq;
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   243
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   244
(** equations characterizing type of extracted program **)
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   245
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   246
fun gen_add_typeof_eqns prep_eq eqns thy =
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   247
  let
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   248
    val {realizes_eqns, typeof_eqns, types, realizers,
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   249
      defs, expand, prep} = ExtractionData.get thy;
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   250
    val eqns' = map (prep_eq thy) eqns
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   251
  in
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   252
    ExtractionData.put
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   253
      {realizes_eqns = realizes_eqns, realizers = realizers,
23178
07ba6b58b3d2 simplified/unified list fold;
wenzelm
parents: 22924
diff changeset
   254
       typeof_eqns = List.foldr add_rule typeof_eqns eqns',
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   255
       types = types, defs = defs, expand = expand, prep = prep} thy
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   256
  end
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   257
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   258
val add_typeof_eqns_i = gen_add_typeof_eqns (K I);
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   259
val add_typeof_eqns = gen_add_typeof_eqns read_condeq;
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   260
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   261
fun thaw (T as TFree (a, S)) =
28375
c879d88d038a eliminated polymorphic equality;
wenzelm
parents: 28370
diff changeset
   262
      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
   263
  | thaw (Type (a, Ts)) = Type (a, map thaw Ts)
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   264
  | thaw T = T;
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   265
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   266
fun freeze (TVar ((a, i), S)) = TFree (a ^ ":" ^ string_of_int i, S)
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   267
  | freeze (Type (a, Ts)) = Type (a, map freeze Ts)
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   268
  | freeze T = T;
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   269
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   270
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
   271
  map_types thaw (f (map_types freeze x));
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   272
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16363
diff changeset
   273
fun etype_of thy vs Ts t =
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   274
  let
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16363
diff changeset
   275
    val {typeof_eqns, ...} = ExtractionData.get thy;
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   276
    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
   277
      Syntax.string_of_term_global thy t)
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16363
diff changeset
   278
  in case strip_abs_body (freeze_thaw (condrew thy (#net typeof_eqns)
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16363
diff changeset
   279
    [typeof_proc (Sign.defaultS thy) vs]) (list_abs (map (pair "x") (rev Ts),
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   280
      Const ("typeof", fastype_of1 (Ts, t) --> Type ("Type", [])) $ t))) of
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   281
      Const ("Type", _) $ u => (Logic.dest_type u handle TERM _ => err ())
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   282
    | _ => err ()
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   283
  end;
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   284
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   285
(** realizers for axioms / theorems, together with correctness proofs **)
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   286
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   287
fun gen_add_realizers prep_rlz rs thy =
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   288
  let val {realizes_eqns, typeof_eqns, types, realizers,
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   289
    defs, expand, prep} = ExtractionData.get thy
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   290
  in
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   291
    ExtractionData.put
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   292
      {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
   293
       realizers = fold (Symtab.cons_list o prep_rlz thy) rs realizers,
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   294
       defs = defs, expand = expand, prep = prep} thy
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   295
  end
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   296
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   297
fun prep_realizer thy =
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   298
  let
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   299
    val {realizes_eqns, typeof_eqns, defs, types, ...} =
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   300
      ExtractionData.get thy;
19482
9f11af8f7ef9 tuned basic list operators (flat, maps, map_filter);
wenzelm
parents: 19466
diff changeset
   301
    val procs = maps (fst o snd) types;
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   302
    val rtypes = map fst types;
16800
90eff1b52428 improved Net interface;
wenzelm
parents: 16790
diff changeset
   303
    val eqns = Net.merge (K false) (#net realizes_eqns, #net typeof_eqns);
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   304
    val thy' = add_syntax thy;
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   305
    val rd = ProofSyntax.read_proof thy' false
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   306
  in fn (thm, (vs, s1, s2)) =>
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   307
    let
21646
c07b5b0e8492 thm/prf: separate official name vs. additional tags;
wenzelm
parents: 20897
diff changeset
   308
      val name = Thm.get_name thm;
21858
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 21646
diff changeset
   309
      val _ = name <> "" orelse error "add_realizers: unnamed theorem";
17203
29b2563f5c11 refer to theory instead of low-level tsig;
wenzelm
parents: 17057
diff changeset
   310
      val prop = Pattern.rewrite_term thy'
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   311
        (map (Logic.dest_equals o prop_of) defs) [] (prop_of thm);
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   312
      val vars = vars_of prop;
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   313
      val vars' = filter_out (fn v =>
20664
ffbc5a57191a member (op =);
wenzelm
parents: 20548
diff changeset
   314
        member (op =) rtypes (tname_of (body_type (fastype_of v)))) vars;
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16363
diff changeset
   315
      val T = etype_of thy' vs [] prop;
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   316
      val (T', thw) = Type.freeze_thaw_type
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   317
        (if T = nullT then nullT else map fastype_of vars' ---> T);
27251
121991a4884d OldGoals.simple_read_term;
wenzelm
parents: 26939
diff changeset
   318
      val t = map_types thw (OldGoals.simple_read_term thy' T' s1);
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16363
diff changeset
   319
      val r' = freeze_thaw (condrew thy' eqns
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16363
diff changeset
   320
        (procs @ [typeof_proc (Sign.defaultS thy') vs, rlz_proc]))
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   321
          (Const ("realizes", T --> propT --> propT) $
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   322
            (if T = nullT then t else list_comb (t, vars')) $ prop);
27330
1af2598b5f7d Logic.all/mk_equals/mk_implies;
wenzelm
parents: 27251
diff changeset
   323
      val r = fold_rev Logic.all (map (get_var_type r') vars) r';
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16363
diff changeset
   324
      val prf = Reconstruct.reconstruct_proof thy' r (rd s2);
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   325
    in (name, (vs, (t, prf))) end
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   326
  end;
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   327
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   328
val add_realizers_i = gen_add_realizers
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   329
  (fn _ => fn (name, (vs, t, prf)) => (name, (vs, (t, prf))));
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   330
val add_realizers = gen_add_realizers prep_realizer;
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   331
13714
bdd483321f4b - exported functions etype_of and mk_typ
berghofe
parents: 13609
diff changeset
   332
fun realizes_of thy vs t prop =
bdd483321f4b - exported functions etype_of and mk_typ
berghofe
parents: 13609
diff changeset
   333
  let
bdd483321f4b - exported functions etype_of and mk_typ
berghofe
parents: 13609
diff changeset
   334
    val thy' = add_syntax thy;
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   335
    val {realizes_eqns, typeof_eqns, defs, types, ...} =
13714
bdd483321f4b - exported functions etype_of and mk_typ
berghofe
parents: 13609
diff changeset
   336
      ExtractionData.get thy';
22717
74dbc7696083 canonical merge operations
haftmann
parents: 22675
diff changeset
   337
    val procs = maps (rev o fst o snd) types;
16800
90eff1b52428 improved Net interface;
wenzelm
parents: 16790
diff changeset
   338
    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
   339
    val prop' = Pattern.rewrite_term thy'
13714
bdd483321f4b - exported functions etype_of and mk_typ
berghofe
parents: 13609
diff changeset
   340
      (map (Logic.dest_equals o prop_of) defs) [] prop;
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16363
diff changeset
   341
  in freeze_thaw (condrew thy' eqns
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16363
diff changeset
   342
    (procs @ [typeof_proc (Sign.defaultS thy') vs, rlz_proc]))
13714
bdd483321f4b - exported functions etype_of and mk_typ
berghofe
parents: 13609
diff changeset
   343
      (Const ("realizes", fastype_of t --> propT --> propT) $ t $ prop')
bdd483321f4b - exported functions etype_of and mk_typ
berghofe
parents: 13609
diff changeset
   344
  end;
bdd483321f4b - exported functions etype_of and mk_typ
berghofe
parents: 13609
diff changeset
   345
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   346
(** expanding theorems / definitions **)
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   347
18728
6790126ab5f6 simplified type attribute;
wenzelm
parents: 18708
diff changeset
   348
fun add_expand_thm thm thy =
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   349
  let
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   350
    val {realizes_eqns, typeof_eqns, types, realizers,
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   351
      defs, expand, prep} = ExtractionData.get thy;
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   352
21646
c07b5b0e8492 thm/prf: separate official name vs. additional tags;
wenzelm
parents: 20897
diff changeset
   353
    val name = Thm.get_name thm;
21858
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 21646
diff changeset
   354
    val _ = name <> "" orelse error "add_expand_thms: unnamed theorem";
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   355
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   356
    val is_def =
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   357
      (case strip_comb (fst (Logic.dest_equals (prop_of thm))) of
18928
042608ffa2ec subsituted gen_duplicates / has_duplicates for duplicates whenever appropriate
haftmann
parents: 18921
diff changeset
   358
         (Const _, ts) => forall is_Var ts andalso not (has_duplicates (op =) ts)
28674
08a77c495dc1 renamed Thm.get_axiom_i to Thm.axiom;
wenzelm
parents: 28375
diff changeset
   359
           andalso (Thm.get_kind thm = Thm.definitionK orelse can (Thm.axiom thy) name)
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   360
       | _ => false) handle TERM _ => false;
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   361
  in
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   362
    (ExtractionData.put (if is_def then
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   363
        {realizes_eqns = realizes_eqns,
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   364
         typeof_eqns = add_rule (([],
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   365
           Logic.dest_equals (prop_of (Drule.abs_def thm))), typeof_eqns),
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   366
         types = types,
22360
26ead7ed4f4b moved eq_thm etc. to structure Thm in Pure/more_thm.ML;
wenzelm
parents: 21858
diff changeset
   367
         realizers = realizers, defs = insert Thm.eq_thm thm defs,
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   368
         expand = expand, prep = prep}
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   369
      else
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   370
        {realizes_eqns = realizes_eqns, typeof_eqns = typeof_eqns, types = types,
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   371
         realizers = realizers, defs = defs,
20854
f9cf9e62d11c insert replacing ins ins_int ins_string
haftmann
parents: 20664
diff changeset
   372
         expand = insert (op =) (name, prop_of thm) expand, prep = prep}) thy)
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   373
  end;
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   374
18728
6790126ab5f6 simplified type attribute;
wenzelm
parents: 18708
diff changeset
   375
val add_expand_thms = fold add_expand_thm;
6790126ab5f6 simplified type attribute;
wenzelm
parents: 18708
diff changeset
   376
6790126ab5f6 simplified type attribute;
wenzelm
parents: 18708
diff changeset
   377
val extraction_expand =
20897
3f8d2834b2c4 attribute: Context.mapping;
wenzelm
parents: 20854
diff changeset
   378
  Attrib.no_args (Thm.declaration_attribute (fn th => Context.mapping (add_expand_thm th) I));
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   379
15801
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   380
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   381
(** types with computational content **)
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   382
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   383
fun add_types tys thy =
22717
74dbc7696083 canonical merge operations
haftmann
parents: 22675
diff changeset
   384
  ExtractionData.map
74dbc7696083 canonical merge operations
haftmann
parents: 22675
diff changeset
   385
    (fn {realizes_eqns, typeof_eqns, types, realizers, defs, expand, prep} =>
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   386
      {realizes_eqns = realizes_eqns, typeof_eqns = typeof_eqns,
22717
74dbc7696083 canonical merge operations
haftmann
parents: 22675
diff changeset
   387
       types = fold (AList.update (op =) o apfst (Sign.intern_type thy)) tys types,
74dbc7696083 canonical merge operations
haftmann
parents: 22675
diff changeset
   388
       realizers = realizers, defs = defs, expand = expand, prep = prep})
74dbc7696083 canonical merge operations
haftmann
parents: 22675
diff changeset
   389
    thy;
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   390
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   391
15801
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   392
(** Pure setup **)
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   393
26463
9283b4185fdf Context.>> : operate on Context.generic;
wenzelm
parents: 26435
diff changeset
   394
val _ = Context.>> (Context.map_theory
18708
4b3dadb4fe33 setup: theory -> theory;
wenzelm
parents: 18358
diff changeset
   395
  (add_types [("prop", ([], NONE))] #>
15801
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   396
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   397
   add_typeof_eqns
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   398
     ["(typeof (PROP P)) == (Type (TYPE(Null))) ==>  \
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   399
    \  (typeof (PROP Q)) == (Type (TYPE('Q))) ==>  \
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   400
    \    (typeof (PROP P ==> PROP Q)) == (Type (TYPE('Q)))",
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   401
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   402
      "(typeof (PROP Q)) == (Type (TYPE(Null))) ==>  \
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   403
    \    (typeof (PROP P ==> PROP Q)) == (Type (TYPE(Null)))",
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   404
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   405
      "(typeof (PROP P)) == (Type (TYPE('P))) ==>  \
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   406
    \  (typeof (PROP Q)) == (Type (TYPE('Q))) ==>  \
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   407
    \    (typeof (PROP P ==> PROP Q)) == (Type (TYPE('P => 'Q)))",
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   408
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   409
      "(%x. typeof (PROP P (x))) == (%x. Type (TYPE(Null))) ==>  \
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   410
    \    (typeof (!!x. PROP P (x))) == (Type (TYPE(Null)))",
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   411
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   412
      "(%x. typeof (PROP P (x))) == (%x. Type (TYPE('P))) ==>  \
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   413
    \    (typeof (!!x::'a. PROP P (x))) == (Type (TYPE('a => 'P)))",
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   414
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   415
      "(%x. typeof (f (x))) == (%x. Type (TYPE('f))) ==>  \
18708
4b3dadb4fe33 setup: theory -> theory;
wenzelm
parents: 18358
diff changeset
   416
    \    (typeof (f)) == (Type (TYPE('f)))"] #>
15801
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   417
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   418
   add_realizes_eqns
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   419
     ["(typeof (PROP P)) == (Type (TYPE(Null))) ==>  \
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   420
    \    (realizes (r) (PROP P ==> PROP Q)) ==  \
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   421
    \    (PROP realizes (Null) (PROP P) ==> PROP realizes (r) (PROP Q))",
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   422
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   423
      "(typeof (PROP P)) == (Type (TYPE('P))) ==>  \
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   424
    \  (typeof (PROP Q)) == (Type (TYPE(Null))) ==>  \
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   425
    \    (realizes (r) (PROP P ==> PROP Q)) ==  \
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   426
    \    (!!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
   427
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   428
      "(realizes (r) (PROP P ==> PROP Q)) ==  \
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   429
    \  (!!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
   430
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   431
      "(%x. typeof (PROP P (x))) == (%x. Type (TYPE(Null))) ==>  \
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   432
    \    (realizes (r) (!!x. PROP P (x))) ==  \
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 15798
diff changeset
   433
    \    (!!x. PROP realizes (Null) (PROP P (x)))",
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
      "(realizes (r) (!!x. PROP P (x))) ==  \
18708
4b3dadb4fe33 setup: theory -> theory;
wenzelm
parents: 18358
diff changeset
   436
    \  (!!x. PROP realizes (r (x)) (PROP P (x)))"] #>
15801
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
   Attrib.add_attributes
18728
6790126ab5f6 simplified type attribute;
wenzelm
parents: 18708
diff changeset
   439
     [("extraction_expand", extraction_expand,
26463
9283b4185fdf Context.>> : operate on Context.generic;
wenzelm
parents: 26435
diff changeset
   440
       "specify theorems / definitions to be expanded during extraction")]));
15801
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
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   443
(**** extract program ****)
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   444
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   445
val dummyt = Const ("dummy", dummyT);
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   446
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   447
fun extract thms thy =
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   448
  let
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16363
diff changeset
   449
    val thy' = add_syntax thy;
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   450
    val {realizes_eqns, typeof_eqns, types, realizers, defs, expand, prep} =
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   451
      ExtractionData.get thy;
22717
74dbc7696083 canonical merge operations
haftmann
parents: 22675
diff changeset
   452
    val procs = maps (rev o fst o snd) types;
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   453
    val rtypes = map fst types;
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16363
diff changeset
   454
    val typroc = typeof_proc (Sign.defaultS thy');
19466
wenzelm
parents: 19391
diff changeset
   455
    val prep = the_default (K I) prep thy' o ProofRewriteRules.elim_defs thy' false defs o
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16363
diff changeset
   456
      Reconstruct.expand_proof thy' (("", NONE) :: map (apsnd SOME) expand);
16800
90eff1b52428 improved Net interface;
wenzelm
parents: 16790
diff changeset
   457
    val rrews = Net.merge (K false) (#net realizes_eqns, #net typeof_eqns);
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   458
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   459
    fun find_inst prop Ts ts vs =
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   460
      let
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   461
        val rvs = relevant_vars rtypes prop;
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   462
        val vars = vars_of prop;
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   463
        val n = Int.min (length vars, length ts);
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   464
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   465
        fun add_args ((Var ((a, i), _), t), (vs', tye)) =
20664
ffbc5a57191a member (op =);
wenzelm
parents: 20548
diff changeset
   466
          if member (op =) rvs a then
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16363
diff changeset
   467
            let val T = etype_of thy' vs Ts t
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   468
            in if T = nullT then (vs', tye)
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   469
               else (a :: vs', (("'" ^ a, i), T) :: tye)
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   470
            end
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   471
          else (vs', tye)
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   472
23178
07ba6b58b3d2 simplified/unified list fold;
wenzelm
parents: 22924
diff changeset
   473
      in List.foldr add_args ([], []) (Library.take (n, vars) ~~ Library.take (n, ts)) end;
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   474
20664
ffbc5a57191a member (op =);
wenzelm
parents: 20548
diff changeset
   475
    fun find (vs: string list) = Option.map snd o find_first (curry (gen_eq_set (op =)) vs o fst);
28375
c879d88d038a eliminated polymorphic equality;
wenzelm
parents: 28370
diff changeset
   476
    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
   477
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   478
    fun app_rlz_rews Ts vs t = strip_abs (length Ts) (freeze_thaw
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16363
diff changeset
   479
      (condrew thy' rrews (procs @ [typroc vs, rlz_proc])) (list_abs
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   480
        (map (pair "x") (rev Ts), t)));
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   481
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   482
    fun realizes_null vs prop = app_rlz_rews [] vs
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   483
      (Const ("realizes", nullT --> propT --> propT) $ nullt $ prop);
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   484
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   485
    fun corr d defs vs ts Ts hs (PBound i) _ _ = (defs, PBound i)
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   486
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   487
      | corr d defs vs ts Ts hs (Abst (s, SOME T, prf)) (Abst (_, _, prf')) t =
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   488
          let val (defs', corr_prf) = corr d defs vs [] (T :: Ts)
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   489
            (dummyt :: hs) prf (incr_pboundvars 1 0 prf')
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   490
            (case t of SOME (Abs (_, _, u)) => SOME u | _ => NONE)
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   491
          in (defs', Abst (s, SOME T, corr_prf)) end
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   492
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   493
      | corr d defs vs ts Ts hs (AbsP (s, SOME prop, prf)) (AbsP (_, _, prf')) t =
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   494
          let
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16363
diff changeset
   495
            val T = etype_of thy' vs Ts prop;
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   496
            val u = if T = nullT then 
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   497
                (case t of SOME u => SOME (incr_boundvars 1 u) | NONE => NONE)
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   498
              else (case t of SOME (Abs (_, _, u)) => SOME u | _ => NONE);
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   499
            val (defs', corr_prf) = corr d defs vs [] (T :: Ts) (prop :: hs)
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   500
              (incr_pboundvars 0 1 prf) (incr_pboundvars 0 1 prf') u;
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   501
            val rlz = Const ("realizes", T --> propT --> propT)
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   502
          in (defs',
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   503
            if T = nullT then AbsP ("R",
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   504
              SOME (app_rlz_rews Ts vs (rlz $ nullt $ prop)),
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   505
                prf_subst_bounds [nullt] corr_prf)
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   506
            else Abst (s, SOME T, AbsP ("R",
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   507
              SOME (app_rlz_rews (T :: Ts) vs
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   508
                (rlz $ Bound 0 $ incr_boundvars 1 prop)), corr_prf)))
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   509
          end
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   510
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   511
      | corr d defs vs ts Ts hs (prf % SOME t) (prf' % _) t' =
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   512
          let
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   513
            val (Us, T) = strip_type (fastype_of1 (Ts, t));
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   514
            val (defs', corr_prf) = corr d defs vs (t :: ts) Ts hs prf prf'
20664
ffbc5a57191a member (op =);
wenzelm
parents: 20548
diff changeset
   515
              (if member (op =) rtypes (tname_of T) then t'
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   516
               else (case t' of SOME (u $ _) => SOME u | _ => NONE));
20664
ffbc5a57191a member (op =);
wenzelm
parents: 20548
diff changeset
   517
            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
   518
              let
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16363
diff changeset
   519
                val eT = etype_of thy' vs Ts t;
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   520
                val (r, Us') = if eT = nullT then (nullt, Us) else
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   521
                  (Bound (length Us), eT :: Us);
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   522
                val u = list_comb (incr_boundvars (length Us') t,
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   523
                  map Bound (length Us - 1 downto 0));
17271
2756a73f63a5 introduced some new-style AList operations
haftmann
parents: 17232
diff changeset
   524
                val u' = (case AList.lookup (op =) types (tname_of T) of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   525
                    SOME ((_, SOME f)) => f r eT u T
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   526
                  | _ => Const ("realizes", eT --> T --> T) $ r $ u)
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   527
              in app_rlz_rews Ts vs (list_abs (map (pair "x") Us', u')) end
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   528
          in (defs', corr_prf % SOME u) end
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   529
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   530
      | corr d defs vs ts Ts hs (prf1 %% prf2) (prf1' %% prf2') t =
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   531
          let
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   532
            val prop = Reconstruct.prop_of' hs prf2';
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16363
diff changeset
   533
            val T = etype_of thy' vs Ts prop;
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   534
            val (defs1, f, u) = if T = nullT then (defs, t, NONE) else
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   535
              (case t of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   536
                 SOME (f $ u) => (defs, SOME f, SOME u)
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   537
               | _ =>
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   538
                 let val (defs1, u) = extr d defs vs [] Ts hs prf2'
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   539
                 in (defs1, NONE, SOME u) end)
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   540
            val (defs2, corr_prf1) = corr d defs1 vs [] Ts hs prf1 prf1' f;
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   541
            val (defs3, corr_prf2) = corr d defs2 vs [] Ts hs prf2 prf2' u;
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   542
          in
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   543
            if T = nullT then (defs3, corr_prf1 %% corr_prf2) else
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   544
              (defs3, corr_prf1 % u %% corr_prf2)
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   545
          end
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   546
28805
8136e5736808 Thm.proof_of returns proof_body;
wenzelm
parents: 28674
diff changeset
   547
      | corr d defs vs ts Ts hs (prf0 as PThm (_, ((name, prop, SOME Ts'), body))) _ _ =
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   548
          let
28805
8136e5736808 Thm.proof_of returns proof_body;
wenzelm
parents: 28674
diff changeset
   549
            val prf = force_proof body;
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   550
            val (vs', tye) = find_inst prop Ts ts vs;
29270
0eade173f77e moved old add_type_XXX, add_term_XXX etc. to structure OldTerm;
wenzelm
parents: 29265
diff changeset
   551
            val tye' = (map fst (OldTerm.term_tvars prop) ~~ Ts') @ tye;
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16363
diff changeset
   552
            val T = etype_of thy' vs' [] prop;
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   553
            val defs' = if T = nullT then defs
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   554
              else fst (extr d defs vs ts Ts hs prf0)
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   555
          in
13609
73c3915553b4 Added check for axioms with "realizes Null A = A".
berghofe
parents: 13417
diff changeset
   556
            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
   557
            else case Symtab.lookup realizers name of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   558
              NONE => (case find vs' (find' name defs') of
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   559
                NONE =>
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   560
                  let
21858
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 21646
diff changeset
   561
                    val _ = T = nullT orelse error "corr: internal error";
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   562
                    val _ = msg d ("Building correctness proof for " ^ quote name ^
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   563
                      (if null vs' then ""
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   564
                       else " (relevant variables: " ^ commas_quote vs' ^ ")"));
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16363
diff changeset
   565
                    val prf' = prep (Reconstruct.reconstruct_proof thy' prop prf);
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   566
                    val (defs'', corr_prf) =
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   567
                      corr (d + 1) defs' vs' [] [] [] prf' prf' NONE;
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   568
                    val corr_prop = Reconstruct.prop_of corr_prf;
23178
07ba6b58b3d2 simplified/unified list fold;
wenzelm
parents: 22924
diff changeset
   569
                    val corr_prf' = List.foldr forall_intr_prf
15574
b1d1b5bfc464 Removed practically all references to Library.foldr.
skalberg
parents: 15570
diff changeset
   570
                      (proof_combt
28805
8136e5736808 Thm.proof_of returns proof_body;
wenzelm
parents: 28674
diff changeset
   571
                         (PThm (serial (),
29270
0eade173f77e moved old add_type_XXX, add_term_XXX etc. to structure OldTerm;
wenzelm
parents: 29265
diff changeset
   572
                          ((corr_name name vs', corr_prop, SOME (map TVar (OldTerm.term_tvars corr_prop))),
28805
8136e5736808 Thm.proof_of returns proof_body;
wenzelm
parents: 28674
diff changeset
   573
                            Lazy.value (make_proof_body corr_prf))), vfs_of corr_prop))
22750
bff5d59de79b adds extracted program to code theorem table
haftmann
parents: 22717
diff changeset
   574
                      (map (get_var_type corr_prop) (vfs_of prop))
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   575
                  in
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   576
                    ((name, (vs', ((nullt, nullt), (corr_prf, corr_prf')))) :: defs'',
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   577
                     prf_subst_TVars tye' corr_prf')
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   578
                  end
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   579
              | SOME (_, (_, prf')) => (defs', prf_subst_TVars tye' prf'))
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   580
            | SOME rs => (case find vs' rs of
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   581
                SOME (_, prf') => (defs', prf_subst_TVars tye' prf')
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   582
              | 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
   583
                  quote name ^ ":\n" ^ Syntax.string_of_term_global thy' (Envir.beta_norm
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   584
                    (Reconstruct.prop_of (proof_combt (prf0, ts))))))
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   585
          end
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   586
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   587
      | corr d defs vs ts Ts hs (prf0 as PAxm (s, prop, SOME Ts')) _ _ =
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   588
          let
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   589
            val (vs', tye) = find_inst prop Ts ts vs;
29270
0eade173f77e moved old add_type_XXX, add_term_XXX etc. to structure OldTerm;
wenzelm
parents: 29265
diff changeset
   590
            val tye' = (map fst (OldTerm.term_tvars prop) ~~ Ts') @ tye
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   591
          in
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16363
diff changeset
   592
            if etype_of thy' vs' [] prop = nullT andalso
13609
73c3915553b4 Added check for axioms with "realizes Null A = A".
berghofe
parents: 13417
diff changeset
   593
              realizes_null vs' prop aconv prop then (defs, prf0)
18956
c050ae1f8f52 Envir.(beta_)eta_contract;
wenzelm
parents: 18928
diff changeset
   594
            else case find vs' (Symtab.lookup_list realizers s) of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   595
              SOME (_, prf) => (defs, prf_subst_TVars tye' prf)
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   596
            | 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
   597
                quote s ^ ":\n" ^ Syntax.string_of_term_global thy' (Envir.beta_norm
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   598
                  (Reconstruct.prop_of (proof_combt (prf0, ts)))))
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   599
          end
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   600
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   601
      | corr d defs vs ts Ts hs _ _ _ = error "corr: bad proof"
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   602
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   603
    and extr d defs vs ts Ts hs (PBound i) = (defs, Bound i)
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   604
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   605
      | extr d defs vs ts Ts hs (Abst (s, SOME T, prf)) =
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   606
          let val (defs', t) = extr d defs vs []
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   607
            (T :: Ts) (dummyt :: hs) (incr_pboundvars 1 0 prf)
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   608
          in (defs', Abs (s, T, t)) end
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   609
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   610
      | extr d defs vs ts Ts hs (AbsP (s, SOME t, prf)) =
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   611
          let
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16363
diff changeset
   612
            val T = etype_of thy' vs Ts t;
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   613
            val (defs', t) = extr d defs vs [] (T :: Ts) (t :: hs)
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   614
              (incr_pboundvars 0 1 prf)
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   615
          in (defs',
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   616
            if T = nullT then subst_bound (nullt, t) else Abs (s, T, t))
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   617
          end
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   618
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   619
      | extr d defs vs ts Ts hs (prf % SOME t) =
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   620
          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
   621
          in (defs',
20664
ffbc5a57191a member (op =);
wenzelm
parents: 20548
diff changeset
   622
            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
   623
            else u $ t)
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   624
          end
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   625
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   626
      | extr d defs vs ts Ts hs (prf1 %% prf2) =
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   627
          let
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   628
            val (defs', f) = extr d defs vs [] Ts hs prf1;
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   629
            val prop = Reconstruct.prop_of' hs prf2;
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16363
diff changeset
   630
            val T = etype_of thy' vs Ts prop
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   631
          in
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   632
            if T = nullT then (defs', f) else
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   633
              let val (defs'', t) = extr d defs' vs [] Ts hs prf2
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   634
              in (defs'', f $ t) end
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   635
          end
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   636
28805
8136e5736808 Thm.proof_of returns proof_body;
wenzelm
parents: 28674
diff changeset
   637
      | 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
   638
          let
28805
8136e5736808 Thm.proof_of returns proof_body;
wenzelm
parents: 28674
diff changeset
   639
            val prf = force_proof body;
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   640
            val (vs', tye) = find_inst prop Ts ts vs;
29270
0eade173f77e moved old add_type_XXX, add_term_XXX etc. to structure OldTerm;
wenzelm
parents: 29265
diff changeset
   641
            val tye' = (map fst (OldTerm.term_tvars prop) ~~ Ts') @ tye
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   642
          in
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17271
diff changeset
   643
            case Symtab.lookup realizers s of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   644
              NONE => (case find vs' (find' s defs) of
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   645
                NONE =>
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   646
                  let
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   647
                    val _ = msg d ("Extracting " ^ quote s ^
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   648
                      (if null vs' then ""
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   649
                       else " (relevant variables: " ^ commas_quote vs' ^ ")"));
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16363
diff changeset
   650
                    val prf' = prep (Reconstruct.reconstruct_proof thy' prop prf);
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   651
                    val (defs', t) = extr (d + 1) defs vs' [] [] [] prf';
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   652
                    val (defs'', corr_prf) =
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   653
                      corr (d + 1) defs' vs' [] [] [] prf' prf' (SOME t);
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   654
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   655
                    val nt = Envir.beta_norm t;
20664
ffbc5a57191a member (op =);
wenzelm
parents: 20548
diff changeset
   656
                    val args = filter_out (fn v => member (op =) rtypes
ffbc5a57191a member (op =);
wenzelm
parents: 20548
diff changeset
   657
                      (tname_of (body_type (fastype_of v)))) (vfs_of prop);
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   658
                    val args' = List.filter (fn v => Logic.occs (v, nt)) args;
15574
b1d1b5bfc464 Removed practically all references to Library.foldr.
skalberg
parents: 15570
diff changeset
   659
                    val t' = mkabs nt args';
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   660
                    val T = fastype_of t';
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   661
                    val cname = extr_name s vs';
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   662
                    val c = Const (cname, T);
15574
b1d1b5bfc464 Removed practically all references to Library.foldr.
skalberg
parents: 15570
diff changeset
   663
                    val u = mkabs (list_comb (c, args')) args;
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   664
                    val eqn = Logic.mk_equals (c, t');
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   665
                    val rlz =
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   666
                      Const ("realizes", fastype_of nt --> propT --> propT);
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   667
                    val lhs = app_rlz_rews [] vs' (rlz $ nt $ prop);
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   668
                    val rhs = app_rlz_rews [] vs' (rlz $ list_comb (c, args') $ prop);
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   669
                    val f = app_rlz_rews [] vs'
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   670
                      (Abs ("x", T, rlz $ list_comb (Bound 0, args') $ prop));
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   671
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   672
                    val corr_prf' =
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   673
                      chtype [] equal_elim_axm %> lhs %> rhs %%
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   674
                       (chtype [propT] symmetric_axm %> rhs %> lhs %%
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   675
                         (chtype [propT, T] combination_axm %> f %> f %> c %> t' %%
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   676
                           (chtype [T --> propT] reflexive_axm %> f) %%
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   677
                           PAxm (cname ^ "_def", eqn,
29270
0eade173f77e moved old add_type_XXX, add_term_XXX etc. to structure OldTerm;
wenzelm
parents: 29265
diff changeset
   678
                             SOME (map TVar (OldTerm.term_tvars eqn))))) %% corr_prf;
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   679
                    val corr_prop = Reconstruct.prop_of corr_prf';
23178
07ba6b58b3d2 simplified/unified list fold;
wenzelm
parents: 22924
diff changeset
   680
                    val corr_prf'' = List.foldr forall_intr_prf
15574
b1d1b5bfc464 Removed practically all references to Library.foldr.
skalberg
parents: 15570
diff changeset
   681
                      (proof_combt
28805
8136e5736808 Thm.proof_of returns proof_body;
wenzelm
parents: 28674
diff changeset
   682
                        (PThm (serial (),
29270
0eade173f77e moved old add_type_XXX, add_term_XXX etc. to structure OldTerm;
wenzelm
parents: 29265
diff changeset
   683
                         ((corr_name s vs', corr_prop, SOME (map TVar (OldTerm.term_tvars corr_prop))),
28805
8136e5736808 Thm.proof_of returns proof_body;
wenzelm
parents: 28674
diff changeset
   684
                           Lazy.value (make_proof_body corr_prf'))), vfs_of corr_prop))
22750
bff5d59de79b adds extracted program to code theorem table
haftmann
parents: 22717
diff changeset
   685
                      (map (get_var_type corr_prop) (vfs_of prop));
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   686
                  in
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   687
                    ((s, (vs', ((t', u), (corr_prf', corr_prf'')))) :: defs'',
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   688
                     subst_TVars tye' u)
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   689
                  end
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   690
              | SOME ((_, u), _) => (defs, subst_TVars tye' u))
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   691
            | SOME rs => (case find vs' rs of
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   692
                SOME (t, _) => (defs, subst_TVars tye' t)
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   693
              | 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
   694
                  quote s ^ ":\n" ^ Syntax.string_of_term_global thy' (Envir.beta_norm
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   695
                    (Reconstruct.prop_of (proof_combt (prf0, ts))))))
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   696
          end
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   697
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   698
      | extr d defs vs ts Ts hs (prf0 as PAxm (s, prop, SOME Ts')) =
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   699
          let
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   700
            val (vs', tye) = find_inst prop Ts ts vs;
29270
0eade173f77e moved old add_type_XXX, add_term_XXX etc. to structure OldTerm;
wenzelm
parents: 29265
diff changeset
   701
            val tye' = (map fst (OldTerm.term_tvars prop) ~~ Ts') @ tye
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   702
          in
18956
c050ae1f8f52 Envir.(beta_)eta_contract;
wenzelm
parents: 18928
diff changeset
   703
            case find vs' (Symtab.lookup_list realizers s) of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   704
              SOME (t, _) => (defs, subst_TVars tye' t)
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   705
            | 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
   706
                quote s ^ ":\n" ^ Syntax.string_of_term_global thy' (Envir.beta_norm
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   707
                  (Reconstruct.prop_of (proof_combt (prf0, ts)))))
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   708
          end
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   709
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   710
      | extr d defs vs ts Ts hs _ = error "extr: bad proof";
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   711
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   712
    fun prep_thm (thm, vs) =
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   713
      let
26626
c6231d64d264 rep_cterm/rep_thm: no longer dereference theory_ref;
wenzelm
parents: 26481
diff changeset
   714
        val thy = Thm.theory_of_thm thm;
c6231d64d264 rep_cterm/rep_thm: no longer dereference theory_ref;
wenzelm
parents: 26481
diff changeset
   715
        val prop = Thm.prop_of thm;
28814
463c9e9111ae clarified Thm.proof_body_of vs. Thm.proof_of;
wenzelm
parents: 28812
diff changeset
   716
        val prf = Thm.proof_of thm;
21646
c07b5b0e8492 thm/prf: separate official name vs. additional tags;
wenzelm
parents: 20897
diff changeset
   717
        val name = Thm.get_name thm;
21858
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 21646
diff changeset
   718
        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
   719
        val _ = etype_of thy' vs [] prop <> nullT orelse error ("theorem " ^
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   720
          quote name ^ " has no computational content")
22596
d0d2af4db18f rep_thm/cterm/ctyp: removed obsolete sign field;
wenzelm
parents: 22360
diff changeset
   721
      in (Reconstruct.reconstruct_proof thy prop prf, vs) end;
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   722
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   723
    val defs = Library.foldl (fn (defs, (prf, vs)) =>
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   724
      fst (extr 0 defs vs [] [] [] prf)) ([], map prep_thm thms);
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   725
16149
d8cac577493c Theory.restore_naming;
wenzelm
parents: 15801
diff changeset
   726
    fun add_def (s, (vs, ((t, u), (prf, _)))) thy =
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16363
diff changeset
   727
      (case Sign.const_type thy (extr_name s vs) of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   728
         NONE =>
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   729
           let
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   730
             val corr_prop = Reconstruct.prop_of prf;
16287
7a03b4b4df67 Type.freeze;
wenzelm
parents: 16195
diff changeset
   731
             val ft = Type.freeze t;
7a03b4b4df67 Type.freeze;
wenzelm
parents: 16195
diff changeset
   732
             val fu = Type.freeze u;
22750
bff5d59de79b adds extracted program to code theorem table
haftmann
parents: 22717
diff changeset
   733
             val (def_thms, thy') = if t = nullt then ([], thy) else
bff5d59de79b adds extracted program to code theorem table
haftmann
parents: 22717
diff changeset
   734
               thy
22796
34c316d7b630 renamed some old names Theory.xxx to Sign.xxx;
wenzelm
parents: 22750
diff changeset
   735
               |> Sign.add_consts_i [(extr_name s vs, fastype_of ft, NoSyn)]
27691
ce171cbd4b93 PureThy: dropped note_thmss_qualified, dropped _i suffix
haftmann
parents: 27330
diff changeset
   736
               |> PureThy.add_defs false [((extr_name s vs ^ "_def",
22750
bff5d59de79b adds extracted program to code theorem table
haftmann
parents: 22717
diff changeset
   737
                    Logic.mk_equals (head_of (strip_abs_body fu), ft)), [])]
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   738
           in
22750
bff5d59de79b adds extracted program to code theorem table
haftmann
parents: 22717
diff changeset
   739
             thy'
26481
92e901171cc8 simplified PureThy.store_thm;
wenzelm
parents: 26463
diff changeset
   740
             |> PureThy.store_thm (corr_name s vs,
29265
5b4247055bd7 moved old add_term_vars, add_term_frees etc. to structure OldTerm;
wenzelm
parents: 28814
diff changeset
   741
                  Thm.varifyT (funpow (length (OldTerm.term_vars corr_prop))
26653
60e0cf6bef89 Thm.forall_elim_var(s);
wenzelm
parents: 26626
diff changeset
   742
                    (Thm.forall_elim_var 0) (forall_intr_frees
22750
bff5d59de79b adds extracted program to code theorem table
haftmann
parents: 22717
diff changeset
   743
                      (ProofChecker.thm_of_proof thy'
26481
92e901171cc8 simplified PureThy.store_thm;
wenzelm
parents: 26463
diff changeset
   744
                       (fst (Proofterm.freeze_thaw_prf prf))))))
22750
bff5d59de79b adds extracted program to code theorem table
haftmann
parents: 22717
diff changeset
   745
             |> snd
28370
37f56e6e702d removed obsolete name convention "func"
haftmann
parents: 27865
diff changeset
   746
             |> fold Code.add_default_eqn def_thms
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   747
           end
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15457
diff changeset
   748
       | SOME _ => thy);
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   749
16149
d8cac577493c Theory.restore_naming;
wenzelm
parents: 15801
diff changeset
   750
  in
d8cac577493c Theory.restore_naming;
wenzelm
parents: 15801
diff changeset
   751
    thy
22796
34c316d7b630 renamed some old names Theory.xxx to Sign.xxx;
wenzelm
parents: 22750
diff changeset
   752
    |> Sign.absolute_path
16149
d8cac577493c Theory.restore_naming;
wenzelm
parents: 15801
diff changeset
   753
    |> fold_rev add_def defs
22796
34c316d7b630 renamed some old names Theory.xxx to Sign.xxx;
wenzelm
parents: 22750
diff changeset
   754
    |> Sign.restore_naming thy
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   755
  end;
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   756
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   757
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   758
(**** interface ****)
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   759
17057
0934ac31985f OuterKeyword;
wenzelm
parents: 16983
diff changeset
   760
structure P = OuterParse and K = OuterKeyword;
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   761
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   762
val parse_vars = Scan.optional (P.$$$ "(" |-- P.list1 P.name --| P.$$$ ")") [];
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   763
24867
e5b55d7be9bb simplified interfaces for outer syntax;
wenzelm
parents: 24712
diff changeset
   764
val _ =
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   765
  OuterSyntax.command "realizers"
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   766
  "specify realizers for primitive axioms / theorems, together with correctness proof"
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   767
  K.thy_decl
13732
f8badfef5cf2 Correctness proofs are now modular, too.
berghofe
parents: 13719
diff changeset
   768
    (Scan.repeat1 (P.xname -- parse_vars --| P.$$$ ":" -- P.string -- P.string) >>
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   769
     (fn xs => Toplevel.theory (fn thy => add_realizers
26343
0dd2eab7b296 simplified get_thm(s): back to plain name argument;
wenzelm
parents: 26336
diff changeset
   770
       (map (fn (((a, vs), s1), s2) => (PureThy.get_thm thy a, (vs, s1, s2))) xs) thy)));
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   771
24867
e5b55d7be9bb simplified interfaces for outer syntax;
wenzelm
parents: 24712
diff changeset
   772
val _ =
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   773
  OuterSyntax.command "realizability"
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   774
  "add equations characterizing realizability" K.thy_decl
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   775
  (Scan.repeat1 P.string >> (Toplevel.theory o add_realizes_eqns));
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   776
24867
e5b55d7be9bb simplified interfaces for outer syntax;
wenzelm
parents: 24712
diff changeset
   777
val _ =
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   778
  OuterSyntax.command "extract_type"
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   779
  "add equations characterizing type of extracted program" K.thy_decl
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   780
  (Scan.repeat1 P.string >> (Toplevel.theory o add_typeof_eqns));
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   781
24867
e5b55d7be9bb simplified interfaces for outer syntax;
wenzelm
parents: 24712
diff changeset
   782
val _ =
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   783
  OuterSyntax.command "extract" "extract terms from proofs" K.thy_decl
26336
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 25389
diff changeset
   784
    (Scan.repeat1 (P.xname -- parse_vars) >> (fn xs => Toplevel.theory (fn thy =>
26343
0dd2eab7b296 simplified get_thm(s): back to plain name argument;
wenzelm
parents: 26336
diff changeset
   785
      extract (map (apfst (PureThy.get_thm thy)) xs) thy)));
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   786
16458
4c6fd0c01d28 accomodate change of TheoryDataFun;
wenzelm
parents: 16363
diff changeset
   787
val etype_of = etype_of o add_syntax;
13714
bdd483321f4b - exported functions etype_of and mk_typ
berghofe
parents: 13609
diff changeset
   788
13402
e6e826bb8c3c Added program extraction module.
berghofe
parents:
diff changeset
   789
end;