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