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