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