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