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