src/HOL/Tools/inductive_realizer.ML
changeset 31781 861e675f01e6
parent 31723 f5cafe803b55
child 31783 cfbe9609ceb1
equal deleted inserted replaced
31775:2b04504fcb69 31781:861e675f01e6
   303     val dts = List.mapPartial (fn (s, rs) => if s mem rsets then
   303     val dts = List.mapPartial (fn (s, rs) => if s mem rsets then
   304       SOME (dt_of_intrs thy1' vs nparms rs) else NONE) rss;
   304       SOME (dt_of_intrs thy1' vs nparms rs) else NONE) rss;
   305 
   305 
   306     (** datatype representing computational content of inductive set **)
   306     (** datatype representing computational content of inductive set **)
   307 
   307 
   308     val ((dummies, dt_info), thy2) =
   308     val ((dummies, (dt_names_rules)), thy2) =
   309       thy1
   309       thy1
   310       |> add_dummies (Datatype.add_datatype
   310       |> add_dummies (Datatype.add_datatype
   311            { strict = false, flat_names = false, quiet = false } (map (Binding.name_of o #2) dts))
   311            { strict = false, flat_names = false, quiet = false } (map (Binding.name_of o #2) dts))
   312            (map (pair false) dts) []
   312            (map (pair false) dts) []
   313       ||> Extraction.add_typeof_eqns_i ty_eqs
   313       ||> Extraction.add_typeof_eqns_i ty_eqs
   314       ||> Extraction.add_realizes_eqns_i rlz_eqs;
   314       ||> Extraction.add_realizes_eqns_i rlz_eqs;
   315     fun get f = (these oo Option.map) f;
   315     val rec_thms = these (Option.map (#rec_thms o snd) dt_names_rules);
       
   316     val case_thms = these (Option.map (#case_thms o snd) dt_names_rules);
   316     val rec_names = distinct (op =) (map (fst o dest_Const o head_of o fst o
   317     val rec_names = distinct (op =) (map (fst o dest_Const o head_of o fst o
   317       HOLogic.dest_eq o HOLogic.dest_Trueprop o prop_of) (get #rec_thms dt_info));
   318       HOLogic.dest_eq o HOLogic.dest_Trueprop o prop_of) rec_thms);
   318     val (constrss, _) = fold_map (fn (s, rs) => fn (recs, dummies) =>
   319     val (constrss, _) = fold_map (fn (s, rs) => fn (recs, dummies) =>
   319       if member (op =) rsets s then
   320       if member (op =) rsets s then
   320         let
   321         let
   321           val (d :: dummies') = dummies;
   322           val (d :: dummies') = dummies;
   322           val (recs1, recs2) = chop (length rs) (if d then tl recs else recs)
   323           val (recs1, recs2) = chop (length rs) (if d then tl recs else recs)
   323         in (map (head_of o hd o rev o snd o strip_comb o fst o
   324         in (map (head_of o hd o rev o snd o strip_comb o fst o
   324           HOLogic.dest_eq o HOLogic.dest_Trueprop o prop_of) recs1, (recs2, dummies'))
   325           HOLogic.dest_eq o HOLogic.dest_Trueprop o prop_of) recs1, (recs2, dummies'))
   325         end
   326         end
   326       else (replicate (length rs) Extraction.nullt, (recs, dummies)))
   327       else (replicate (length rs) Extraction.nullt, (recs, dummies)))
   327         rss (get #rec_thms dt_info, dummies);
   328         rss (rec_thms, dummies);
   328     val rintrs = map (fn (intr, c) => Envir.eta_contract
   329     val rintrs = map (fn (intr, c) => Envir.eta_contract
   329       (Extraction.realizes_of thy2 vs
   330       (Extraction.realizes_of thy2 vs
   330         (if c = Extraction.nullt then c else list_comb (c, map Var (rev
   331         (if c = Extraction.nullt then c else list_comb (c, map Var (rev
   331           (Term.add_vars (prop_of intr) []) \\ params'))) (prop_of intr)))
   332           (Term.add_vars (prop_of intr) []) \\ params'))) (prop_of intr)))
   332             (maps snd rss ~~ List.concat constrss);
   333             (maps snd rss ~~ List.concat constrss);
   384           in
   385           in
   385             (Logic.strip_imp_prems Q', P, Logic.strip_imp_concl Q')
   386             (Logic.strip_imp_prems Q', P, Logic.strip_imp_concl Q')
   386           end) (rlzs ~~ rnames);
   387           end) (rlzs ~~ rnames);
   387         val concl = HOLogic.mk_Trueprop (foldr1 HOLogic.mk_conj (map
   388         val concl = HOLogic.mk_Trueprop (foldr1 HOLogic.mk_conj (map
   388           (fn (_, _ $ P, _ $ Q) => HOLogic.mk_imp (P, Q)) rlzs'));
   389           (fn (_, _ $ P, _ $ Q) => HOLogic.mk_imp (P, Q)) rlzs'));
   389         val rews = map mk_meta_eq
   390         val rews = map mk_meta_eq (fst_conv :: snd_conv :: rec_thms);
   390           (fst_conv :: snd_conv :: get #rec_thms dt_info);
       
   391         val thm = Goal.prove_global thy [] prems concl (fn {prems, ...} => EVERY
   391         val thm = Goal.prove_global thy [] prems concl (fn {prems, ...} => EVERY
   392           [rtac (#raw_induct ind_info) 1,
   392           [rtac (#raw_induct ind_info) 1,
   393            rewrite_goals_tac rews,
   393            rewrite_goals_tac rews,
   394            REPEAT ((resolve_tac prems THEN_ALL_NEW EVERY'
   394            REPEAT ((resolve_tac prems THEN_ALL_NEW EVERY'
   395              [K (rewrite_goals_tac rews), ObjectLogic.atomize_prems_tac,
   395              [K (rewrite_goals_tac rews), ObjectLogic.atomize_prems_tac,
   415       end;
   415       end;
   416 
   416 
   417     (** realizer for elimination rules **)
   417     (** realizer for elimination rules **)
   418 
   418 
   419     val case_names = map (fst o dest_Const o head_of o fst o HOLogic.dest_eq o
   419     val case_names = map (fst o dest_Const o head_of o fst o HOLogic.dest_eq o
   420       HOLogic.dest_Trueprop o prop_of o hd) (get #case_thms dt_info);
   420       HOLogic.dest_Trueprop o prop_of o hd) case_thms;
   421 
   421 
   422     fun add_elim_realizer Ps
   422     fun add_elim_realizer Ps
   423       (((((elim, elimR), intrs), case_thms), case_name), dummy) thy =
   423       (((((elim, elimR), intrs), case_thms), case_name), dummy) thy =
   424       let
   424       let
   425         val (prem :: prems) = prems_of elim;
   425         val (prem :: prems) = prems_of elim;
   474       if s mem rsets then SOME (p, intrs) else NONE)
   474       if s mem rsets then SOME (p, intrs) else NONE)
   475         (rss' ~~ (elims ~~ #elims ind_info));
   475         (rss' ~~ (elims ~~ #elims ind_info));
   476     val thy6 = Library.foldl (fn (thy, p as (((((elim, _), _), _), _), _)) => thy |>
   476     val thy6 = Library.foldl (fn (thy, p as (((((elim, _), _), _), _), _)) => thy |>
   477       add_elim_realizer [] p |> add_elim_realizer [fst (fst (dest_Var
   477       add_elim_realizer [] p |> add_elim_realizer [fst (fst (dest_Var
   478         (HOLogic.dest_Trueprop (concl_of elim))))] p) (thy5,
   478         (HOLogic.dest_Trueprop (concl_of elim))))] p) (thy5,
   479            elimps ~~ get #case_thms dt_info ~~ case_names ~~ dummies)
   479            elimps ~~ case_thms ~~ case_names ~~ dummies)
   480 
   480 
   481   in Sign.restore_naming thy thy6 end;
   481   in Sign.restore_naming thy thy6 end;
   482 
   482 
   483 fun add_ind_realizers name rsets thy =
   483 fun add_ind_realizers name rsets thy =
   484   let
   484   let