src/Pure/Proof/extraction.ML
changeset 16458 4c6fd0c01d28
parent 16363 c686a606dfba
child 16486 1a12cdb6ee6b
equal deleted inserted replaced
16457:e0f22edf38a5 16458:4c6fd0c01d28
     5 Extraction of programs from proofs.
     5 Extraction of programs from proofs.
     6 *)
     6 *)
     7 
     7 
     8 signature EXTRACTION =
     8 signature EXTRACTION =
     9 sig
     9 sig
    10   val set_preprocessor : (Sign.sg -> Proofterm.proof -> Proofterm.proof) -> theory -> theory
    10   val set_preprocessor : (theory -> Proofterm.proof -> Proofterm.proof) -> theory -> theory
    11   val add_realizes_eqns_i : ((term * term) list * (term * term)) list -> theory -> theory
    11   val add_realizes_eqns_i : ((term * term) list * (term * term)) list -> theory -> theory
    12   val add_realizes_eqns : string list -> theory -> theory
    12   val add_realizes_eqns : string list -> theory -> theory
    13   val add_typeof_eqns_i : ((term * term) list * (term * term)) list -> theory -> theory
    13   val add_typeof_eqns_i : ((term * term) list * (term * term)) list -> theory -> theory
    14   val add_typeof_eqns : string list -> theory -> theory
    14   val add_typeof_eqns : string list -> theory -> theory
    15   val add_realizers_i : (string * (string list * term * Proofterm.proof)) list
    15   val add_realizers_i : (string * (string list * term * Proofterm.proof)) list
    84 
    84 
    85 fun merge_rules
    85 fun merge_rules
    86   ({next, rs = rs1, net} : rules) ({next = next2, rs = rs2, ...} : rules) =
    86   ({next, rs = rs1, net} : rules) ({next = next2, rs = rs2, ...} : rules) =
    87   foldr add_rule {next = next, rs = rs1, net = net} (rs2 \\ rs1);
    87   foldr add_rule {next = next, rs = rs1, net = net} (rs2 \\ rs1);
    88 
    88 
    89 fun condrew sign rules procs =
    89 fun condrew thy rules procs =
    90   let
    90   let
    91     val tsig = Sign.tsig_of sign;
    91     val tsig = Sign.tsig_of thy;
    92 
    92 
    93     fun rew tm =
    93     fun rew tm =
    94       Pattern.rewrite_term tsig [] (condrew' :: procs) tm
    94       Pattern.rewrite_term tsig [] (condrew' :: procs) tm
    95     and condrew' tm =
    95     and condrew' tm =
    96       let
    96       let
   110           val env' = Envir.Envir
   110           val env' = Envir.Envir
   111             {maxidx = Library.foldl Int.max
   111             {maxidx = Library.foldl Int.max
   112               (~1, map (Int.max o pairself maxidx_of_term) prems'),
   112               (~1, map (Int.max o pairself maxidx_of_term) prems'),
   113              iTs = Tenv, asol = tenv};
   113              iTs = Tenv, asol = tenv};
   114           val env'' = Library.foldl (fn (env, p) =>
   114           val env'' = Library.foldl (fn (env, p) =>
   115             Pattern.unify (sign, env, [pairself (lookup rew) p])) (env', prems')
   115             Pattern.unify (thy, env, [pairself (lookup rew) p])) (env', prems')
   116         in SOME (Envir.norm_term env'' (inc (ren tm2)))
   116         in SOME (Envir.norm_term env'' (inc (ren tm2)))
   117         end handle Pattern.MATCH => NONE | Pattern.Unif => NONE)
   117         end handle Pattern.MATCH => NONE | Pattern.Unif => NONE)
   118           (sort (Int.compare o pairself fst)
   118           (sort (Int.compare o pairself fst)
   119             (Net.match_term rules (Pattern.eta_contract tm)))
   119             (Net.match_term rules (Pattern.eta_contract tm)))
   120       end;
   120       end;
   176 
   176 
   177 (**** theory data ****)
   177 (**** theory data ****)
   178 
   178 
   179 (* data kind 'Pure/extraction' *)
   179 (* data kind 'Pure/extraction' *)
   180 
   180 
   181 structure ExtractionArgs =
   181 structure ExtractionData = TheoryDataFun
   182 struct
   182 (struct
   183   val name = "Pure/extraction";
   183   val name = "Pure/extraction";
   184   type T =
   184   type T =
   185     {realizes_eqns : rules,
   185     {realizes_eqns : rules,
   186      typeof_eqns : rules,
   186      typeof_eqns : rules,
   187      types : (string * ((term -> term option) list *
   187      types : (string * ((term -> term option) list *
   188        (term -> typ -> term -> typ -> term) option)) list,
   188        (term -> typ -> term -> typ -> term) option)) list,
   189      realizers : (string list * (term * proof)) list Symtab.table,
   189      realizers : (string list * (term * proof)) list Symtab.table,
   190      defs : thm list,
   190      defs : thm list,
   191      expand : (string * term) list,
   191      expand : (string * term) list,
   192      prep : (Sign.sg -> proof -> proof) option}
   192      prep : (theory -> proof -> proof) option}
   193 
   193 
   194   val empty =
   194   val empty =
   195     {realizes_eqns = empty_rules,
   195     {realizes_eqns = empty_rules,
   196      typeof_eqns = empty_rules,
   196      typeof_eqns = empty_rules,
   197      types = [],
   197      types = [],
   198      realizers = Symtab.empty,
   198      realizers = Symtab.empty,
   199      defs = [],
   199      defs = [],
   200      expand = [],
   200      expand = [],
   201      prep = NONE};
   201      prep = NONE};
   202   val copy = I;
   202   val copy = I;
   203   val prep_ext = I;
   203   val extend = I;
   204 
   204 
   205   fun merge
   205   fun merge _
   206     (({realizes_eqns = realizes_eqns1, typeof_eqns = typeof_eqns1, types = types1,
   206     (({realizes_eqns = realizes_eqns1, typeof_eqns = typeof_eqns1, types = types1,
   207        realizers = realizers1, defs = defs1, expand = expand1, prep = prep1},
   207        realizers = realizers1, defs = defs1, expand = expand1, prep = prep1},
   208       {realizes_eqns = realizes_eqns2, typeof_eqns = typeof_eqns2, types = types2,
   208       {realizes_eqns = realizes_eqns2, typeof_eqns = typeof_eqns2, types = types2,
   209        realizers = realizers2, defs = defs2, expand = expand2, prep = prep2}) : T * T) =
   209        realizers = realizers2, defs = defs2, expand = expand2, prep = prep2}) : T * T) =
   210     {realizes_eqns = merge_rules realizes_eqns1 realizes_eqns2,
   210     {realizes_eqns = merge_rules realizes_eqns1 realizes_eqns2,
   214        (realizers1, realizers2),
   214        (realizers1, realizers2),
   215      defs = gen_merge_lists eq_thm defs1 defs2,
   215      defs = gen_merge_lists eq_thm defs1 defs2,
   216      expand = merge_lists expand1 expand2,
   216      expand = merge_lists expand1 expand2,
   217      prep = (case prep1 of NONE => prep2 | _ => prep1)};
   217      prep = (case prep1 of NONE => prep2 | _ => prep1)};
   218 
   218 
   219   fun print sg (x : T) = ();
   219   fun print _ _ = ();
   220 end;
   220 end);
   221 
   221 
   222 structure ExtractionData = TheoryDataFun(ExtractionArgs);
       
   223 val _ = Context.add_setup [ExtractionData.init];
   222 val _ = Context.add_setup [ExtractionData.init];
   224 
   223 
   225 fun read_condeq thy =
   224 fun read_condeq thy =
   226   let val sg = sign_of (add_syntax thy)
   225   let val thy' = add_syntax thy
   227   in fn s =>
   226   in fn s =>
   228     let val t = Logic.varify (term_of (read_cterm sg (s, propT)))
   227     let val t = Logic.varify (term_of (read_cterm thy' (s, propT)))
   229     in (map Logic.dest_equals (Logic.strip_imp_prems t),
   228     in (map Logic.dest_equals (Logic.strip_imp_prems t),
   230       Logic.dest_equals (Logic.strip_imp_concl t))
   229       Logic.dest_equals (Logic.strip_imp_concl t))
   231     end handle TERM _ => error ("Not a (conditional) meta equality:\n" ^ s)
   230     end handle TERM _ => error ("Not a (conditional) meta equality:\n" ^ s)
   232   end;
   231   end;
   233 
   232 
   284   | freeze T = T;
   283   | freeze T = T;
   285 
   284 
   286 fun freeze_thaw f x =
   285 fun freeze_thaw f x =
   287   map_term_types thaw (f (map_term_types freeze x));
   286   map_term_types thaw (f (map_term_types freeze x));
   288 
   287 
   289 fun etype_of sg vs Ts t =
   288 fun etype_of thy vs Ts t =
   290   let
   289   let
   291     val {typeof_eqns, ...} = ExtractionData.get_sg sg;
   290     val {typeof_eqns, ...} = ExtractionData.get thy;
   292     fun err () = error ("Unable to determine type of extracted program for\n" ^
   291     fun err () = error ("Unable to determine type of extracted program for\n" ^
   293       Sign.string_of_term sg t)
   292       Sign.string_of_term thy t)
   294   in case strip_abs_body (freeze_thaw (condrew sg (#net typeof_eqns)
   293   in case strip_abs_body (freeze_thaw (condrew thy (#net typeof_eqns)
   295     [typeof_proc (Sign.defaultS sg) vs]) (list_abs (map (pair "x") (rev Ts),
   294     [typeof_proc (Sign.defaultS thy) vs]) (list_abs (map (pair "x") (rev Ts),
   296       Const ("typeof", fastype_of1 (Ts, t) --> Type ("Type", [])) $ t))) of
   295       Const ("typeof", fastype_of1 (Ts, t) --> Type ("Type", [])) $ t))) of
   297       Const ("Type", _) $ u => (Logic.dest_type u handle TERM _ => err ())
   296       Const ("Type", _) $ u => (Logic.dest_type u handle TERM _ => err ())
   298     | _ => err ()
   297     | _ => err ()
   299   end;
   298   end;
   300 
   299 
   317       ExtractionData.get thy;
   316       ExtractionData.get thy;
   318     val procs = List.concat (map (fst o snd) types);
   317     val procs = List.concat (map (fst o snd) types);
   319     val rtypes = map fst types;
   318     val rtypes = map fst types;
   320     val eqns = Net.merge (#net realizes_eqns, #net typeof_eqns, K false);
   319     val eqns = Net.merge (#net realizes_eqns, #net typeof_eqns, K false);
   321     val thy' = add_syntax thy;
   320     val thy' = add_syntax thy;
   322     val sign = sign_of thy';
       
   323     val tsg = Sign.tsig_of sign;
       
   324     val rd = ProofSyntax.read_proof thy' false
   321     val rd = ProofSyntax.read_proof thy' false
   325   in fn (thm, (vs, s1, s2)) =>
   322   in fn (thm, (vs, s1, s2)) =>
   326     let
   323     let
   327       val name = Thm.name_of_thm thm;
   324       val name = Thm.name_of_thm thm;
   328       val _ = assert (name <> "") "add_realizers: unnamed theorem";
   325       val _ = assert (name <> "") "add_realizers: unnamed theorem";
   329       val prop = Pattern.rewrite_term tsg
   326       val prop = Pattern.rewrite_term (Sign.tsig_of thy')
   330         (map (Logic.dest_equals o prop_of) defs) [] (prop_of thm);
   327         (map (Logic.dest_equals o prop_of) defs) [] (prop_of thm);
   331       val vars = vars_of prop;
   328       val vars = vars_of prop;
   332       val vars' = filter_out (fn v =>
   329       val vars' = filter_out (fn v =>
   333         tname_of (body_type (fastype_of v)) mem rtypes) vars;
   330         tname_of (body_type (fastype_of v)) mem rtypes) vars;
   334       val T = etype_of sign vs [] prop;
   331       val T = etype_of thy' vs [] prop;
   335       val (T', thw) = Type.freeze_thaw_type
   332       val (T', thw) = Type.freeze_thaw_type
   336         (if T = nullT then nullT else map fastype_of vars' ---> T);
   333         (if T = nullT then nullT else map fastype_of vars' ---> T);
   337       val t = map_term_types thw (term_of (read_cterm sign (s1, T')));
   334       val t = map_term_types thw (term_of (read_cterm thy' (s1, T')));
   338       val r' = freeze_thaw (condrew sign eqns
   335       val r' = freeze_thaw (condrew thy' eqns
   339         (procs @ [typeof_proc (Sign.defaultS sign) vs, rlz_proc]))
   336         (procs @ [typeof_proc (Sign.defaultS thy') vs, rlz_proc]))
   340           (Const ("realizes", T --> propT --> propT) $
   337           (Const ("realizes", T --> propT --> propT) $
   341             (if T = nullT then t else list_comb (t, vars')) $ prop);
   338             (if T = nullT then t else list_comb (t, vars')) $ prop);
   342       val r = foldr forall_intr r' (map (get_var_type r') vars);
   339       val r = foldr forall_intr r' (map (get_var_type r') vars);
   343       val prf = Reconstruct.reconstruct_proof sign r (rd s2);
   340       val prf = Reconstruct.reconstruct_proof thy' r (rd s2);
   344     in (name, (vs, (t, prf))) end
   341     in (name, (vs, (t, prf))) end
   345   end;
   342   end;
   346 
   343 
   347 val add_realizers_i = gen_add_realizers
   344 val add_realizers_i = gen_add_realizers
   348   (fn _ => fn (name, (vs, t, prf)) => (name, (vs, (t, prf))));
   345   (fn _ => fn (name, (vs, t, prf)) => (name, (vs, (t, prf))));
   349 val add_realizers = gen_add_realizers prep_realizer;
   346 val add_realizers = gen_add_realizers prep_realizer;
   350 
   347 
   351 fun realizes_of thy vs t prop =
   348 fun realizes_of thy vs t prop =
   352   let
   349   let
   353     val thy' = add_syntax thy;
   350     val thy' = add_syntax thy;
   354     val sign = sign_of thy';
       
   355     val {realizes_eqns, typeof_eqns, defs, types, ...} =
   351     val {realizes_eqns, typeof_eqns, defs, types, ...} =
   356       ExtractionData.get thy';
   352       ExtractionData.get thy';
   357     val procs = List.concat (map (fst o snd) types);
   353     val procs = List.concat (map (fst o snd) types);
   358     val eqns = Net.merge (#net realizes_eqns, #net typeof_eqns, K false);
   354     val eqns = Net.merge (#net realizes_eqns, #net typeof_eqns, K false);
   359     val prop' = Pattern.rewrite_term (Sign.tsig_of sign)
   355     val prop' = Pattern.rewrite_term (Sign.tsig_of thy')
   360       (map (Logic.dest_equals o prop_of) defs) [] prop;
   356       (map (Logic.dest_equals o prop_of) defs) [] prop;
   361   in freeze_thaw (condrew sign eqns
   357   in freeze_thaw (condrew thy' eqns
   362     (procs @ [typeof_proc (Sign.defaultS sign) vs, rlz_proc]))
   358     (procs @ [typeof_proc (Sign.defaultS thy') vs, rlz_proc]))
   363       (Const ("realizes", fastype_of t --> propT --> propT) $ t $ prop')
   359       (Const ("realizes", fastype_of t --> propT --> propT) $ t $ prop')
   364   end;
   360   end;
   365 
   361 
   366 (** expanding theorems / definitions **)
   362 (** expanding theorems / definitions **)
   367 
   363 
   401   let val {realizes_eqns, typeof_eqns, types, realizers,
   397   let val {realizes_eqns, typeof_eqns, types, realizers,
   402     defs, expand, prep} = ExtractionData.get thy;
   398     defs, expand, prep} = ExtractionData.get thy;
   403   in
   399   in
   404     ExtractionData.put
   400     ExtractionData.put
   405       {realizes_eqns = realizes_eqns, typeof_eqns = typeof_eqns,
   401       {realizes_eqns = realizes_eqns, typeof_eqns = typeof_eqns,
   406        types = map (apfst (Sign.intern_type (sign_of thy))) tys @ types,
   402        types = map (apfst (Sign.intern_type thy)) tys @ types,
   407        realizers = realizers, defs = defs, expand = expand, prep = prep} thy
   403        realizers = realizers, defs = defs, expand = expand, prep = prep} thy
   408   end;
   404   end;
   409 
   405 
   410 
   406 
   411 (** Pure setup **)
   407 (** Pure setup **)
   464 
   460 
   465 val dummyt = Const ("dummy", dummyT);
   461 val dummyt = Const ("dummy", dummyT);
   466 
   462 
   467 fun extract thms thy =
   463 fun extract thms thy =
   468   let
   464   let
   469     val sg = sign_of (add_syntax thy);
   465     val thy' = add_syntax thy;
   470     val tsg = Sign.tsig_of sg;
       
   471     val {realizes_eqns, typeof_eqns, types, realizers, defs, expand, prep} =
   466     val {realizes_eqns, typeof_eqns, types, realizers, defs, expand, prep} =
   472       ExtractionData.get thy;
   467       ExtractionData.get thy;
   473     val procs = List.concat (map (fst o snd) types);
   468     val procs = List.concat (map (fst o snd) types);
   474     val rtypes = map fst types;
   469     val rtypes = map fst types;
   475     val typroc = typeof_proc (Sign.defaultS sg);
   470     val typroc = typeof_proc (Sign.defaultS thy');
   476     val prep = getOpt (prep, K I) sg o ProofRewriteRules.elim_defs sg false defs o
   471     val prep = getOpt (prep, K I) thy' o ProofRewriteRules.elim_defs thy' false defs o
   477       Reconstruct.expand_proof sg (("", NONE) :: map (apsnd SOME) expand);
   472       Reconstruct.expand_proof thy' (("", NONE) :: map (apsnd SOME) expand);
   478     val rrews = Net.merge (#net realizes_eqns, #net typeof_eqns, K false);
   473     val rrews = Net.merge (#net realizes_eqns, #net typeof_eqns, K false);
   479 
   474 
   480     fun find_inst prop Ts ts vs =
   475     fun find_inst prop Ts ts vs =
   481       let
   476       let
   482         val rvs = relevant_vars rtypes prop;
   477         val rvs = relevant_vars rtypes prop;
   483         val vars = vars_of prop;
   478         val vars = vars_of prop;
   484         val n = Int.min (length vars, length ts);
   479         val n = Int.min (length vars, length ts);
   485 
   480 
   486         fun add_args ((Var ((a, i), _), t), (vs', tye)) =
   481         fun add_args ((Var ((a, i), _), t), (vs', tye)) =
   487           if a mem rvs then
   482           if a mem rvs then
   488             let val T = etype_of sg vs Ts t
   483             let val T = etype_of thy' vs Ts t
   489             in if T = nullT then (vs', tye)
   484             in if T = nullT then (vs', tye)
   490                else (a :: vs', (("'" ^ a, i), T) :: tye)
   485                else (a :: vs', (("'" ^ a, i), T) :: tye)
   491             end
   486             end
   492           else (vs', tye)
   487           else (vs', tye)
   493 
   488 
   495 
   490 
   496     fun find vs = Option.map snd o find_first (curry eq_set vs o fst);
   491     fun find vs = Option.map snd o find_first (curry eq_set vs o fst);
   497     fun find' s = map snd o List.filter (equal s o fst)
   492     fun find' s = map snd o List.filter (equal s o fst)
   498 
   493 
   499     fun app_rlz_rews Ts vs t = strip_abs (length Ts) (freeze_thaw
   494     fun app_rlz_rews Ts vs t = strip_abs (length Ts) (freeze_thaw
   500       (condrew sg rrews (procs @ [typroc vs, rlz_proc])) (list_abs
   495       (condrew thy' rrews (procs @ [typroc vs, rlz_proc])) (list_abs
   501         (map (pair "x") (rev Ts), t)));
   496         (map (pair "x") (rev Ts), t)));
   502 
   497 
   503     fun realizes_null vs prop = app_rlz_rews [] vs
   498     fun realizes_null vs prop = app_rlz_rews [] vs
   504       (Const ("realizes", nullT --> propT --> propT) $ nullt $ prop);
   499       (Const ("realizes", nullT --> propT --> propT) $ nullt $ prop);
   505 
   500 
   511             (case t of SOME (Abs (_, _, u)) => SOME u | _ => NONE)
   506             (case t of SOME (Abs (_, _, u)) => SOME u | _ => NONE)
   512           in (defs', Abst (s, SOME T, corr_prf)) end
   507           in (defs', Abst (s, SOME T, corr_prf)) end
   513 
   508 
   514       | corr d defs vs ts Ts hs (AbsP (s, SOME prop, prf)) (AbsP (_, _, prf')) t =
   509       | corr d defs vs ts Ts hs (AbsP (s, SOME prop, prf)) (AbsP (_, _, prf')) t =
   515           let
   510           let
   516             val T = etype_of sg vs Ts prop;
   511             val T = etype_of thy' vs Ts prop;
   517             val u = if T = nullT then 
   512             val u = if T = nullT then 
   518                 (case t of SOME u => SOME (incr_boundvars 1 u) | NONE => NONE)
   513                 (case t of SOME u => SOME (incr_boundvars 1 u) | NONE => NONE)
   519               else (case t of SOME (Abs (_, _, u)) => SOME u | _ => NONE);
   514               else (case t of SOME (Abs (_, _, u)) => SOME u | _ => NONE);
   520             val (defs', corr_prf) = corr d defs vs [] (T :: Ts) (prop :: hs)
   515             val (defs', corr_prf) = corr d defs vs [] (T :: Ts) (prop :: hs)
   521               (incr_pboundvars 0 1 prf) (incr_pboundvars 0 1 prf') u;
   516               (incr_pboundvars 0 1 prf) (incr_pboundvars 0 1 prf') u;
   535             val (defs', corr_prf) = corr d defs vs (t :: ts) Ts hs prf prf'
   530             val (defs', corr_prf) = corr d defs vs (t :: ts) Ts hs prf prf'
   536               (if tname_of T mem rtypes then t'
   531               (if tname_of T mem rtypes then t'
   537                else (case t' of SOME (u $ _) => SOME u | _ => NONE));
   532                else (case t' of SOME (u $ _) => SOME u | _ => NONE));
   538             val u = if not (tname_of T mem rtypes) then t else
   533             val u = if not (tname_of T mem rtypes) then t else
   539               let
   534               let
   540                 val eT = etype_of sg vs Ts t;
   535                 val eT = etype_of thy' vs Ts t;
   541                 val (r, Us') = if eT = nullT then (nullt, Us) else
   536                 val (r, Us') = if eT = nullT then (nullt, Us) else
   542                   (Bound (length Us), eT :: Us);
   537                   (Bound (length Us), eT :: Us);
   543                 val u = list_comb (incr_boundvars (length Us') t,
   538                 val u = list_comb (incr_boundvars (length Us') t,
   544                   map Bound (length Us - 1 downto 0));
   539                   map Bound (length Us - 1 downto 0));
   545                 val u' = (case assoc (types, tname_of T) of
   540                 val u' = (case assoc (types, tname_of T) of
   549           in (defs', corr_prf % SOME u) end
   544           in (defs', corr_prf % SOME u) end
   550 
   545 
   551       | corr d defs vs ts Ts hs (prf1 %% prf2) (prf1' %% prf2') t =
   546       | corr d defs vs ts Ts hs (prf1 %% prf2) (prf1' %% prf2') t =
   552           let
   547           let
   553             val prop = Reconstruct.prop_of' hs prf2';
   548             val prop = Reconstruct.prop_of' hs prf2';
   554             val T = etype_of sg vs Ts prop;
   549             val T = etype_of thy' vs Ts prop;
   555             val (defs1, f, u) = if T = nullT then (defs, t, NONE) else
   550             val (defs1, f, u) = if T = nullT then (defs, t, NONE) else
   556               (case t of
   551               (case t of
   557                  SOME (f $ u) => (defs, SOME f, SOME u)
   552                  SOME (f $ u) => (defs, SOME f, SOME u)
   558                | _ =>
   553                | _ =>
   559                  let val (defs1, u) = extr d defs vs [] Ts hs prf2'
   554                  let val (defs1, u) = extr d defs vs [] Ts hs prf2'
   567 
   562 
   568       | corr d defs vs ts Ts hs (prf0 as PThm ((name, _), prf, prop, SOME Ts')) _ _ =
   563       | corr d defs vs ts Ts hs (prf0 as PThm ((name, _), prf, prop, SOME Ts')) _ _ =
   569           let
   564           let
   570             val (vs', tye) = find_inst prop Ts ts vs;
   565             val (vs', tye) = find_inst prop Ts ts vs;
   571             val tye' = (map fst (term_tvars prop) ~~ Ts') @ tye;
   566             val tye' = (map fst (term_tvars prop) ~~ Ts') @ tye;
   572             val T = etype_of sg vs' [] prop;
   567             val T = etype_of thy' vs' [] prop;
   573             val defs' = if T = nullT then defs
   568             val defs' = if T = nullT then defs
   574               else fst (extr d defs vs ts Ts hs prf0)
   569               else fst (extr d defs vs ts Ts hs prf0)
   575           in
   570           in
   576             if T = nullT andalso realizes_null vs' prop aconv prop then (defs, prf0)
   571             if T = nullT andalso realizes_null vs' prop aconv prop then (defs, prf0)
   577             else case Symtab.lookup (realizers, name) of
   572             else case Symtab.lookup (realizers, name) of
   580                   let
   575                   let
   581                     val _ = assert (T = nullT) "corr: internal error";
   576                     val _ = assert (T = nullT) "corr: internal error";
   582                     val _ = msg d ("Building correctness proof for " ^ quote name ^
   577                     val _ = msg d ("Building correctness proof for " ^ quote name ^
   583                       (if null vs' then ""
   578                       (if null vs' then ""
   584                        else " (relevant variables: " ^ commas_quote vs' ^ ")"));
   579                        else " (relevant variables: " ^ commas_quote vs' ^ ")"));
   585                     val prf' = prep (Reconstruct.reconstruct_proof sg prop prf);
   580                     val prf' = prep (Reconstruct.reconstruct_proof thy' prop prf);
   586                     val (defs'', corr_prf) =
   581                     val (defs'', corr_prf) =
   587                       corr (d + 1) defs' vs' [] [] [] prf' prf' NONE;
   582                       corr (d + 1) defs' vs' [] [] [] prf' prf' NONE;
   588                     val corr_prop = Reconstruct.prop_of corr_prf;
   583                     val corr_prop = Reconstruct.prop_of corr_prf;
   589                     val corr_prf' = foldr forall_intr_prf
   584                     val corr_prf' = foldr forall_intr_prf
   590                       (proof_combt
   585                       (proof_combt
   597                   end
   592                   end
   598               | SOME (_, (_, prf')) => (defs', prf_subst_TVars tye' prf'))
   593               | SOME (_, (_, prf')) => (defs', prf_subst_TVars tye' prf'))
   599             | SOME rs => (case find vs' rs of
   594             | SOME rs => (case find vs' rs of
   600                 SOME (_, prf') => (defs', prf_subst_TVars tye' prf')
   595                 SOME (_, prf') => (defs', prf_subst_TVars tye' prf')
   601               | NONE => error ("corr: no realizer for instance of theorem " ^
   596               | NONE => error ("corr: no realizer for instance of theorem " ^
   602                   quote name ^ ":\n" ^ Sign.string_of_term sg (Envir.beta_norm
   597                   quote name ^ ":\n" ^ Sign.string_of_term thy' (Envir.beta_norm
   603                     (Reconstruct.prop_of (proof_combt (prf0, ts))))))
   598                     (Reconstruct.prop_of (proof_combt (prf0, ts))))))
   604           end
   599           end
   605 
   600 
   606       | corr d defs vs ts Ts hs (prf0 as PAxm (s, prop, SOME Ts')) _ _ =
   601       | corr d defs vs ts Ts hs (prf0 as PAxm (s, prop, SOME Ts')) _ _ =
   607           let
   602           let
   608             val (vs', tye) = find_inst prop Ts ts vs;
   603             val (vs', tye) = find_inst prop Ts ts vs;
   609             val tye' = (map fst (term_tvars prop) ~~ Ts') @ tye
   604             val tye' = (map fst (term_tvars prop) ~~ Ts') @ tye
   610           in
   605           in
   611             if etype_of sg vs' [] prop = nullT andalso
   606             if etype_of thy' vs' [] prop = nullT andalso
   612               realizes_null vs' prop aconv prop then (defs, prf0)
   607               realizes_null vs' prop aconv prop then (defs, prf0)
   613             else case find vs' (Symtab.lookup_multi (realizers, s)) of
   608             else case find vs' (Symtab.lookup_multi (realizers, s)) of
   614               SOME (_, prf) => (defs, prf_subst_TVars tye' prf)
   609               SOME (_, prf) => (defs, prf_subst_TVars tye' prf)
   615             | NONE => error ("corr: no realizer for instance of axiom " ^
   610             | NONE => error ("corr: no realizer for instance of axiom " ^
   616                 quote s ^ ":\n" ^ Sign.string_of_term sg (Envir.beta_norm
   611                 quote s ^ ":\n" ^ Sign.string_of_term thy' (Envir.beta_norm
   617                   (Reconstruct.prop_of (proof_combt (prf0, ts)))))
   612                   (Reconstruct.prop_of (proof_combt (prf0, ts)))))
   618           end
   613           end
   619 
   614 
   620       | corr d defs vs ts Ts hs _ _ _ = error "corr: bad proof"
   615       | corr d defs vs ts Ts hs _ _ _ = error "corr: bad proof"
   621 
   616 
   626             (T :: Ts) (dummyt :: hs) (incr_pboundvars 1 0 prf)
   621             (T :: Ts) (dummyt :: hs) (incr_pboundvars 1 0 prf)
   627           in (defs', Abs (s, T, t)) end
   622           in (defs', Abs (s, T, t)) end
   628 
   623 
   629       | extr d defs vs ts Ts hs (AbsP (s, SOME t, prf)) =
   624       | extr d defs vs ts Ts hs (AbsP (s, SOME t, prf)) =
   630           let
   625           let
   631             val T = etype_of sg vs Ts t;
   626             val T = etype_of thy' vs Ts t;
   632             val (defs', t) = extr d defs vs [] (T :: Ts) (t :: hs)
   627             val (defs', t) = extr d defs vs [] (T :: Ts) (t :: hs)
   633               (incr_pboundvars 0 1 prf)
   628               (incr_pboundvars 0 1 prf)
   634           in (defs',
   629           in (defs',
   635             if T = nullT then subst_bound (nullt, t) else Abs (s, T, t))
   630             if T = nullT then subst_bound (nullt, t) else Abs (s, T, t))
   636           end
   631           end
   644 
   639 
   645       | extr d defs vs ts Ts hs (prf1 %% prf2) =
   640       | extr d defs vs ts Ts hs (prf1 %% prf2) =
   646           let
   641           let
   647             val (defs', f) = extr d defs vs [] Ts hs prf1;
   642             val (defs', f) = extr d defs vs [] Ts hs prf1;
   648             val prop = Reconstruct.prop_of' hs prf2;
   643             val prop = Reconstruct.prop_of' hs prf2;
   649             val T = etype_of sg vs Ts prop
   644             val T = etype_of thy' vs Ts prop
   650           in
   645           in
   651             if T = nullT then (defs', f) else
   646             if T = nullT then (defs', f) else
   652               let val (defs'', t) = extr d defs' vs [] Ts hs prf2
   647               let val (defs'', t) = extr d defs' vs [] Ts hs prf2
   653               in (defs'', f $ t) end
   648               in (defs'', f $ t) end
   654           end
   649           end
   663                 NONE =>
   658                 NONE =>
   664                   let
   659                   let
   665                     val _ = msg d ("Extracting " ^ quote s ^
   660                     val _ = msg d ("Extracting " ^ quote s ^
   666                       (if null vs' then ""
   661                       (if null vs' then ""
   667                        else " (relevant variables: " ^ commas_quote vs' ^ ")"));
   662                        else " (relevant variables: " ^ commas_quote vs' ^ ")"));
   668                     val prf' = prep (Reconstruct.reconstruct_proof sg prop prf);
   663                     val prf' = prep (Reconstruct.reconstruct_proof thy' prop prf);
   669                     val (defs', t) = extr (d + 1) defs vs' [] [] [] prf';
   664                     val (defs', t) = extr (d + 1) defs vs' [] [] [] prf';
   670                     val (defs'', corr_prf) =
   665                     val (defs'', corr_prf) =
   671                       corr (d + 1) defs' vs' [] [] [] prf' prf' (SOME t);
   666                       corr (d + 1) defs' vs' [] [] [] prf' prf' (SOME t);
   672 
   667 
   673                     val nt = Envir.beta_norm t;
   668                     val nt = Envir.beta_norm t;
   706                   end
   701                   end
   707               | SOME ((_, u), _) => (defs, subst_TVars tye' u))
   702               | SOME ((_, u), _) => (defs, subst_TVars tye' u))
   708             | SOME rs => (case find vs' rs of
   703             | SOME rs => (case find vs' rs of
   709                 SOME (t, _) => (defs, subst_TVars tye' t)
   704                 SOME (t, _) => (defs, subst_TVars tye' t)
   710               | NONE => error ("extr: no realizer for instance of theorem " ^
   705               | NONE => error ("extr: no realizer for instance of theorem " ^
   711                   quote s ^ ":\n" ^ Sign.string_of_term sg (Envir.beta_norm
   706                   quote s ^ ":\n" ^ Sign.string_of_term thy' (Envir.beta_norm
   712                     (Reconstruct.prop_of (proof_combt (prf0, ts))))))
   707                     (Reconstruct.prop_of (proof_combt (prf0, ts))))))
   713           end
   708           end
   714 
   709 
   715       | extr d defs vs ts Ts hs (prf0 as PAxm (s, prop, SOME Ts')) =
   710       | extr d defs vs ts Ts hs (prf0 as PAxm (s, prop, SOME Ts')) =
   716           let
   711           let
   718             val tye' = (map fst (term_tvars prop) ~~ Ts') @ tye
   713             val tye' = (map fst (term_tvars prop) ~~ Ts') @ tye
   719           in
   714           in
   720             case find vs' (Symtab.lookup_multi (realizers, s)) of
   715             case find vs' (Symtab.lookup_multi (realizers, s)) of
   721               SOME (t, _) => (defs, subst_TVars tye' t)
   716               SOME (t, _) => (defs, subst_TVars tye' t)
   722             | NONE => error ("extr: no realizer for instance of axiom " ^
   717             | NONE => error ("extr: no realizer for instance of axiom " ^
   723                 quote s ^ ":\n" ^ Sign.string_of_term sg (Envir.beta_norm
   718                 quote s ^ ":\n" ^ Sign.string_of_term thy' (Envir.beta_norm
   724                   (Reconstruct.prop_of (proof_combt (prf0, ts)))))
   719                   (Reconstruct.prop_of (proof_combt (prf0, ts)))))
   725           end
   720           end
   726 
   721 
   727       | extr d defs vs ts Ts hs _ = error "extr: bad proof";
   722       | extr d defs vs ts Ts hs _ = error "extr: bad proof";
   728 
   723 
   729     fun prep_thm (thm, vs) =
   724     fun prep_thm (thm, vs) =
   730       let
   725       let
   731         val {prop, der = (_, prf), sign, ...} = rep_thm thm;
   726         val {prop, der = (_, prf), sign, ...} = rep_thm thm;
   732         val name = Thm.name_of_thm thm;
   727         val name = Thm.name_of_thm thm;
   733         val _ = assert (name <> "") "extraction: unnamed theorem";
   728         val _ = assert (name <> "") "extraction: unnamed theorem";
   734         val _ = assert (etype_of sg vs [] prop <> nullT) ("theorem " ^
   729         val _ = assert (etype_of thy' vs [] prop <> nullT) ("theorem " ^
   735           quote name ^ " has no computational content")
   730           quote name ^ " has no computational content")
   736       in (Reconstruct.reconstruct_proof sign prop prf, vs) end;
   731       in (Reconstruct.reconstruct_proof sign prop prf, vs) end;
   737 
   732 
   738     val defs = Library.foldl (fn (defs, (prf, vs)) =>
   733     val defs = Library.foldl (fn (defs, (prf, vs)) =>
   739       fst (extr 0 defs vs [] [] [] prf)) ([], map prep_thm thms);
   734       fst (extr 0 defs vs [] [] [] prf)) ([], map prep_thm thms);
   740 
   735 
   741     fun add_def (s, (vs, ((t, u), (prf, _)))) thy =
   736     fun add_def (s, (vs, ((t, u), (prf, _)))) thy =
   742       (case Sign.const_type (sign_of thy) (extr_name s vs) of
   737       (case Sign.const_type thy (extr_name s vs) of
   743          NONE =>
   738          NONE =>
   744            let
   739            let
   745              val corr_prop = Reconstruct.prop_of prf;
   740              val corr_prop = Reconstruct.prop_of prf;
   746              val ft = Type.freeze t;
   741              val ft = Type.freeze t;
   747              val fu = Type.freeze u;
   742              val fu = Type.freeze u;
   796     (Scan.repeat1 (P.xname -- parse_vars) >> (fn xs => Toplevel.theory
   791     (Scan.repeat1 (P.xname -- parse_vars) >> (fn xs => Toplevel.theory
   797       (fn thy => extract (map (apfst (PureThy.get_thm thy o rpair NONE)) xs) thy)));
   792       (fn thy => extract (map (apfst (PureThy.get_thm thy o rpair NONE)) xs) thy)));
   798 
   793 
   799 val _ = OuterSyntax.add_parsers [realizersP, realizabilityP, typeofP, extractP];
   794 val _ = OuterSyntax.add_parsers [realizersP, realizabilityP, typeofP, extractP];
   800 
   795 
   801 val etype_of = etype_of o sign_of o add_syntax;
   796 val etype_of = etype_of o add_syntax;
   802 
   797 
   803 end;
   798 end;