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