src/HOL/Tools/Function/induction_schema.ML
author wenzelm
Thu Jun 27 17:06:22 2013 +0200 (2013-06-27)
changeset 52467 24c6ddb48cb8
parent 52223 5bb6ae8acb87
child 54742 7a86358a3c0b
permissions -rw-r--r--
tuned signature;
     1 (*  Title:      HOL/Tools/Function/induction_schema.ML
     2     Author:     Alexander Krauss, TU Muenchen
     3 
     4 A method to prove induction schemas.
     5 *)
     6 
     7 signature INDUCTION_SCHEMA =
     8 sig
     9   val mk_ind_tac : (int -> tactic) -> (int -> tactic) -> (int -> tactic)
    10                    -> Proof.context -> thm list -> tactic
    11   val induction_schema_tac : Proof.context -> thm list -> tactic
    12 end
    13 
    14 
    15 structure Induction_Schema : INDUCTION_SCHEMA =
    16 struct
    17 
    18 open Function_Lib
    19 
    20 type rec_call_info = int * (string * typ) list * term list * term list
    21 
    22 datatype scheme_case = SchemeCase of
    23  {bidx : int,
    24   qs: (string * typ) list,
    25   oqnames: string list,
    26   gs: term list,
    27   lhs: term list,
    28   rs: rec_call_info list}
    29 
    30 datatype scheme_branch = SchemeBranch of
    31  {P : term,
    32   xs: (string * typ) list,
    33   ws: (string * typ) list,
    34   Cs: term list}
    35 
    36 datatype ind_scheme = IndScheme of
    37  {T: typ, (* sum of products *)
    38   branches: scheme_branch list,
    39   cases: scheme_case list}
    40 
    41 val ind_atomize = Raw_Simplifier.rewrite true @{thms induct_atomize}
    42 val ind_rulify = Raw_Simplifier.rewrite true @{thms induct_rulify}
    43 
    44 fun meta thm = thm RS eq_reflection
    45 
    46 val sum_prod_conv = Raw_Simplifier.rewrite true
    47   (map meta (@{thm split_conv} :: @{thms sum.cases}))
    48 
    49 fun term_conv thy cv t =
    50   cv (cterm_of thy t)
    51   |> prop_of |> Logic.dest_equals |> snd
    52 
    53 fun mk_relT T = HOLogic.mk_setT (HOLogic.mk_prodT (T, T))
    54 
    55 fun dest_hhf ctxt t =
    56   let
    57     val ((params, imp), ctxt') = Variable.focus t ctxt
    58   in
    59     (ctxt', map #2 params, Logic.strip_imp_prems imp, Logic.strip_imp_concl imp)
    60   end
    61 
    62 fun mk_scheme' ctxt cases concl =
    63   let
    64     fun mk_branch concl =
    65       let
    66         val (_, ws, Cs, _ $ Pxs) = dest_hhf ctxt concl
    67         val (P, xs) = strip_comb Pxs
    68       in
    69         SchemeBranch { P=P, xs=map dest_Free xs, ws=ws, Cs=Cs }
    70       end
    71 
    72     val (branches, cases') = (* correction *)
    73       case Logic.dest_conjunctions concl of
    74         [conc] =>
    75         let
    76           val _ $ Pxs = Logic.strip_assums_concl conc
    77           val (P, _) = strip_comb Pxs
    78           val (cases', conds) =
    79             take_prefix (Term.exists_subterm (curry op aconv P)) cases
    80           val concl' = fold_rev (curry Logic.mk_implies) conds conc
    81         in
    82           ([mk_branch concl'], cases')
    83         end
    84       | concls => (map mk_branch concls, cases)
    85 
    86     fun mk_case premise =
    87       let
    88         val (ctxt', qs, prems, _ $ Plhs) = dest_hhf ctxt premise
    89         val (P, lhs) = strip_comb Plhs
    90 
    91         fun bidx Q =
    92           find_index (fn SchemeBranch {P=P',...} => Q aconv P') branches
    93 
    94         fun mk_rcinfo pr =
    95           let
    96             val (_, Gvs, Gas, _ $ Phyp) = dest_hhf ctxt' pr
    97             val (P', rcs) = strip_comb Phyp
    98           in
    99             (bidx P', Gvs, Gas, rcs)
   100           end
   101 
   102         fun is_pred v = exists (fn SchemeBranch {P,...} => v aconv P) branches
   103 
   104         val (gs, rcprs) =
   105           take_prefix (not o Term.exists_subterm is_pred) prems
   106       in
   107         SchemeCase {bidx=bidx P, qs=qs, oqnames=map fst qs(*FIXME*),
   108           gs=gs, lhs=lhs, rs=map mk_rcinfo rcprs}
   109       end
   110 
   111     fun PT_of (SchemeBranch { xs, ...}) =
   112       foldr1 HOLogic.mk_prodT (map snd xs)
   113 
   114     val ST = Balanced_Tree.make (uncurry SumTree.mk_sumT) (map PT_of branches)
   115   in
   116     IndScheme {T=ST, cases=map mk_case cases', branches=branches }
   117   end
   118 
   119 fun mk_completeness ctxt (IndScheme {cases, branches, ...}) bidx =
   120   let
   121     val SchemeBranch { xs, ws, Cs, ... } = nth branches bidx
   122     val relevant_cases = filter (fn SchemeCase {bidx=bidx', ...} => bidx' = bidx) cases
   123 
   124     val allqnames = fold (fn SchemeCase {qs, ...} => fold (insert (op =) o Free) qs) relevant_cases []
   125     val (Pbool :: xs') = map Free (Variable.variant_frees ctxt allqnames (("P", HOLogic.boolT) :: xs))
   126     val Cs' = map (Pattern.rewrite_term (Proof_Context.theory_of ctxt) (filter_out (op aconv) (map Free xs ~~ xs')) []) Cs
   127 
   128     fun mk_case (SchemeCase {qs, oqnames, gs, lhs, ...}) =
   129       HOLogic.mk_Trueprop Pbool
   130       |> fold_rev (fn x_l => curry Logic.mk_implies (HOLogic.mk_Trueprop(HOLogic.mk_eq x_l)))
   131            (xs' ~~ lhs)
   132       |> fold_rev (curry Logic.mk_implies) gs
   133       |> fold_rev mk_forall_rename (oqnames ~~ map Free qs)
   134   in
   135     HOLogic.mk_Trueprop Pbool
   136     |> fold_rev (curry Logic.mk_implies o mk_case) relevant_cases
   137     |> fold_rev (curry Logic.mk_implies) Cs'
   138     |> fold_rev (Logic.all o Free) ws
   139     |> fold_rev mk_forall_rename (map fst xs ~~ xs')
   140     |> mk_forall_rename ("P", Pbool)
   141   end
   142 
   143 fun mk_wf R (IndScheme {T, ...}) =
   144   HOLogic.Trueprop $ (Const (@{const_name wf}, mk_relT T --> HOLogic.boolT) $ R)
   145 
   146 fun mk_ineqs R (IndScheme {T, cases, branches}) =
   147   let
   148     fun inject i ts =
   149        SumTree.mk_inj T (length branches) (i + 1) (foldr1 HOLogic.mk_prod ts)
   150 
   151     val thesis = Free ("thesis", HOLogic.boolT) (* FIXME *)
   152 
   153     fun mk_pres bdx args =
   154       let
   155         val SchemeBranch { xs, ws, Cs, ... } = nth branches bdx
   156         fun replace (x, v) t = betapply (lambda (Free x) t, v)
   157         val Cs' = map (fold replace (xs ~~ args)) Cs
   158         val cse =
   159           HOLogic.mk_Trueprop thesis
   160           |> fold_rev (curry Logic.mk_implies) Cs'
   161           |> fold_rev (Logic.all o Free) ws
   162       in
   163         Logic.mk_implies (cse, HOLogic.mk_Trueprop thesis)
   164       end
   165 
   166     fun f (SchemeCase {bidx, qs, oqnames, gs, lhs, rs, ...}) =
   167       let
   168         fun g (bidx', Gvs, Gas, rcarg) =
   169           let val export =
   170             fold_rev (curry Logic.mk_implies) Gas
   171             #> fold_rev (curry Logic.mk_implies) gs
   172             #> fold_rev (Logic.all o Free) Gvs
   173             #> fold_rev mk_forall_rename (oqnames ~~ map Free qs)
   174           in
   175             (HOLogic.mk_mem (HOLogic.mk_prod (inject bidx' rcarg, inject bidx lhs), R)
   176              |> HOLogic.mk_Trueprop
   177              |> export,
   178              mk_pres bidx' rcarg
   179              |> export
   180              |> Logic.all thesis)
   181           end
   182       in
   183         map g rs
   184       end
   185   in
   186     map f cases
   187   end
   188 
   189 
   190 fun mk_ind_goal thy branches =
   191   let
   192     fun brnch (SchemeBranch { P, xs, ws, Cs, ... }) =
   193       HOLogic.mk_Trueprop (list_comb (P, map Free xs))
   194       |> fold_rev (curry Logic.mk_implies) Cs
   195       |> fold_rev (Logic.all o Free) ws
   196       |> term_conv thy ind_atomize
   197       |> Object_Logic.drop_judgment thy
   198       |> HOLogic.tupled_lambda (foldr1 HOLogic.mk_prod (map Free xs))
   199   in
   200     SumTree.mk_sumcases HOLogic.boolT (map brnch branches)
   201   end
   202 
   203 fun mk_induct_rule ctxt R x complete_thms wf_thm ineqss
   204   (IndScheme {T, cases=scases, branches}) =
   205   let
   206     val n = length branches
   207     val scases_idx = map_index I scases
   208 
   209     fun inject i ts =
   210       SumTree.mk_inj T n (i + 1) (foldr1 HOLogic.mk_prod ts)
   211     val P_of = nth (map (fn (SchemeBranch { P, ... }) => P) branches)
   212 
   213     val thy = Proof_Context.theory_of ctxt
   214     val cert = cterm_of thy
   215 
   216     val P_comp = mk_ind_goal thy branches
   217 
   218     (* Inductive Hypothesis: !!z. (z,x):R ==> P z *)
   219     val ihyp = Logic.all_const T $ Abs ("z", T,
   220       Logic.mk_implies
   221         (HOLogic.mk_Trueprop (
   222           Const (@{const_name Set.member}, HOLogic.mk_prodT (T, T) --> mk_relT T --> HOLogic.boolT) 
   223           $ (HOLogic.pair_const T T $ Bound 0 $ x)
   224           $ R),
   225          HOLogic.mk_Trueprop (P_comp $ Bound 0)))
   226       |> cert
   227 
   228     val aihyp = Thm.assume ihyp
   229 
   230     (* Rule for case splitting along the sum types *)
   231     val xss = map (fn (SchemeBranch { xs, ... }) => map Free xs) branches
   232     val pats = map_index (uncurry inject) xss
   233     val sum_split_rule =
   234       Pat_Completeness.prove_completeness ctxt [x] (P_comp $ x) xss (map single pats)
   235 
   236     fun prove_branch (bidx, (SchemeBranch { P, xs, ws, Cs, ... }, (complete_thm, pat))) =
   237       let
   238         val fxs = map Free xs
   239         val branch_hyp = Thm.assume (cert (HOLogic.mk_Trueprop (HOLogic.mk_eq (x, pat))))
   240 
   241         val C_hyps = map (cert #> Thm.assume) Cs
   242 
   243         val (relevant_cases, ineqss') =
   244           (scases_idx ~~ ineqss)
   245           |> filter (fn ((_, SchemeCase {bidx=bidx', ...}), _) => bidx' = bidx)
   246           |> split_list
   247 
   248         fun prove_case (cidx, SchemeCase {qs, gs, lhs, rs, ...}) ineq_press =
   249           let
   250             val case_hyps =
   251               map (Thm.assume o cert o HOLogic.mk_Trueprop o HOLogic.mk_eq) (fxs ~~ lhs)
   252 
   253             val cqs = map (cert o Free) qs
   254             val ags = map (Thm.assume o cert) gs
   255 
   256             val replace_x_simpset =
   257               put_simpset HOL_basic_ss ctxt addsimps (branch_hyp :: case_hyps)
   258             val sih = full_simplify replace_x_simpset aihyp
   259 
   260             fun mk_Prec (idx, Gvs, Gas, rcargs) (ineq, pres) =
   261               let
   262                 val cGas = map (Thm.assume o cert) Gas
   263                 val cGvs = map (cert o Free) Gvs
   264                 val import = fold Thm.forall_elim (cqs @ cGvs)
   265                   #> fold Thm.elim_implies (ags @ cGas)
   266                 val ipres = pres
   267                   |> Thm.forall_elim (cert (list_comb (P_of idx, rcargs)))
   268                   |> import
   269               in
   270                 sih
   271                 |> Thm.forall_elim (cert (inject idx rcargs))
   272                 |> Thm.elim_implies (import ineq) (* Psum rcargs *)
   273                 |> Conv.fconv_rule sum_prod_conv
   274                 |> Conv.fconv_rule ind_rulify
   275                 |> (fn th => th COMP ipres) (* P rs *)
   276                 |> fold_rev (Thm.implies_intr o cprop_of) cGas
   277                 |> fold_rev Thm.forall_intr cGvs
   278               end
   279 
   280             val P_recs = map2 mk_Prec rs ineq_press   (*  [P rec1, P rec2, ... ]  *)
   281 
   282             val step = HOLogic.mk_Trueprop (list_comb (P, lhs))
   283               |> fold_rev (curry Logic.mk_implies o prop_of) P_recs
   284               |> fold_rev (curry Logic.mk_implies) gs
   285               |> fold_rev (Logic.all o Free) qs
   286               |> cert
   287 
   288             val Plhs_to_Pxs_conv =
   289               foldl1 (uncurry Conv.combination_conv)
   290                 (Conv.all_conv :: map (fn ch => K (Thm.symmetric (ch RS eq_reflection))) case_hyps)
   291 
   292             val res = Thm.assume step
   293               |> fold Thm.forall_elim cqs
   294               |> fold Thm.elim_implies ags
   295               |> fold Thm.elim_implies P_recs (* P lhs *)
   296               |> Conv.fconv_rule (Conv.arg_conv Plhs_to_Pxs_conv) (* P xs *)
   297               |> fold_rev (Thm.implies_intr o cprop_of) (ags @ case_hyps)
   298               |> fold_rev Thm.forall_intr cqs (* !!qs. Gas ==> xs = lhss ==> P xs *)
   299           in
   300             (res, (cidx, step))
   301           end
   302 
   303         val (cases, steps) = split_list (map2 prove_case relevant_cases ineqss')
   304 
   305         val bstep = complete_thm
   306           |> Thm.forall_elim (cert (list_comb (P, fxs)))
   307           |> fold (Thm.forall_elim o cert) (fxs @ map Free ws)
   308           |> fold Thm.elim_implies C_hyps
   309           |> fold Thm.elim_implies cases (* P xs *)
   310           |> fold_rev (Thm.implies_intr o cprop_of) C_hyps
   311           |> fold_rev (Thm.forall_intr o cert o Free) ws
   312 
   313         val Pxs = cert (HOLogic.mk_Trueprop (P_comp $ x))
   314           |> Goal.init
   315           |> (Simplifier.rewrite_goals_tac (map meta (branch_hyp :: @{thm split_conv} :: @{thms sum.cases}))
   316               THEN CONVERSION ind_rulify 1)
   317           |> Seq.hd
   318           |> Thm.elim_implies (Conv.fconv_rule Drule.beta_eta_conversion bstep)
   319           |> Goal.finish ctxt
   320           |> Thm.implies_intr (cprop_of branch_hyp)
   321           |> fold_rev (Thm.forall_intr o cert) fxs
   322       in
   323         (Pxs, steps)
   324       end
   325 
   326     val (branches, steps) =
   327       map_index prove_branch (branches ~~ (complete_thms ~~ pats))
   328       |> split_list |> apsnd flat
   329 
   330     val istep = sum_split_rule
   331       |> fold (fn b => fn th => Drule.compose (b, 1, th)) branches
   332       |> Thm.implies_intr ihyp
   333       |> Thm.forall_intr (cert x) (* "!!x. (!!y<x. P y) ==> P x" *)
   334 
   335     val induct_rule =
   336       @{thm "wf_induct_rule"}
   337       |> (curry op COMP) wf_thm
   338       |> (curry op COMP) istep
   339 
   340     val steps_sorted = map snd (sort (int_ord o pairself fst) steps)
   341   in
   342     (steps_sorted, induct_rule)
   343   end
   344 
   345 
   346 fun mk_ind_tac comp_tac pres_tac term_tac ctxt facts =
   347   (* FIXME proper use of facts!? *)
   348   (ALLGOALS (Method.insert_tac facts)) THEN HEADGOAL (SUBGOAL (fn (t, i) =>
   349   let
   350     val (ctxt', _, cases, concl) = dest_hhf ctxt t
   351     val scheme as IndScheme {T=ST, branches, ...} = mk_scheme' ctxt' cases concl
   352     val ([Rn,xn], ctxt'') = Variable.variant_fixes ["R","x"] ctxt'
   353     val R = Free (Rn, mk_relT ST)
   354     val x = Free (xn, ST)
   355     val cert = cterm_of (Proof_Context.theory_of ctxt)
   356 
   357     val ineqss = mk_ineqs R scheme
   358       |> map (map (pairself (Thm.assume o cert)))
   359     val complete =
   360       map_range (mk_completeness ctxt scheme #> cert #> Thm.assume) (length branches)
   361     val wf_thm = mk_wf R scheme |> cert |> Thm.assume
   362 
   363     val (descent, pres) = split_list (flat ineqss)
   364     val newgoals = complete @ pres @ wf_thm :: descent
   365 
   366     val (steps, indthm) =
   367       mk_induct_rule ctxt'' R x complete wf_thm ineqss scheme
   368 
   369     fun project (i, SchemeBranch {xs, ...}) =
   370       let
   371         val inst = (foldr1 HOLogic.mk_prod (map Free xs))
   372           |> SumTree.mk_inj ST (length branches) (i + 1)
   373           |> cert
   374       in
   375         indthm
   376         |> Drule.instantiate' [] [SOME inst]
   377         |> simplify (put_simpset SumTree.sumcase_split_ss ctxt'')
   378         |> Conv.fconv_rule ind_rulify
   379       end
   380 
   381     val res = Conjunction.intr_balanced (map_index project branches)
   382       |> fold_rev Thm.implies_intr (map cprop_of newgoals @ steps)
   383       |> Drule.generalize ([], [Rn])
   384 
   385     val nbranches = length branches
   386     val npres = length pres
   387   in
   388     Thm.bicompose {flatten = false, match = false, incremented = false}
   389       (false, res, length newgoals) i
   390     THEN term_tac (i + nbranches + npres)
   391     THEN (EVERY (map (TRY o pres_tac) ((i + nbranches + npres - 1) downto (i + nbranches))))
   392     THEN (EVERY (map (TRY o comp_tac) ((i + nbranches - 1) downto i)))
   393   end))
   394 
   395 
   396 fun induction_schema_tac ctxt =
   397   mk_ind_tac (K all_tac) (assume_tac APPEND' Goal.assume_rule_tac ctxt) (K all_tac) ctxt;
   398 
   399 end