src/HOL/indrule.ML
author oheimb
Sat Feb 15 17:52:31 1997 +0100 (1997-02-15)
changeset 2637 e9b203f854ae
parent 2270 d7513875b2b8
child 3086 a2de0be6e14d
permissions -rw-r--r--
reflecting my recent changes of the simplifier and classical reasoner
clasohm@1465
     1
(*  Title:      HOL/indrule.ML
clasohm@923
     2
    ID:         $Id$
clasohm@1465
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
clasohm@923
     4
    Copyright   1994  University of Cambridge
clasohm@923
     5
clasohm@923
     6
Induction rule module -- for Inductive/Coinductive Definitions
clasohm@923
     7
clasohm@923
     8
Proves a strong induction rule and a mutual induction rule
clasohm@923
     9
*)
clasohm@923
    10
clasohm@923
    11
signature INDRULE =
clasohm@923
    12
  sig
clasohm@1465
    13
  val induct        : thm                       (*main induction rule*)
clasohm@1465
    14
  val mutual_induct : thm                       (*mutual induction rule*)
clasohm@923
    15
  end;
clasohm@923
    16
clasohm@923
    17
clasohm@923
    18
functor Indrule_Fun
clasohm@923
    19
    (structure Inductive: sig include INDUCTIVE_ARG INDUCTIVE_I end and
clasohm@1465
    20
         Intr_elim: sig include INTR_ELIM INTR_ELIM_AUX end) : INDRULE  =
paulson@1424
    21
let
paulson@1424
    22
paulson@1424
    23
val sign = sign_of Inductive.thy;
clasohm@923
    24
paulson@1424
    25
val (Const(_,recT),rec_params) = strip_comb (hd Inductive.rec_tms);
clasohm@923
    26
paulson@1424
    27
val elem_type = Ind_Syntax.dest_setT (body_type recT);
paulson@1424
    28
val big_rec_name = space_implode "_" Intr_elim.rec_names;
clasohm@923
    29
val big_rec_tm = list_comb(Const(big_rec_name,recT), rec_params);
clasohm@923
    30
lcp@1190
    31
val _ = writeln "  Proving the induction rule...";
clasohm@923
    32
clasohm@923
    33
(*** Prove the main induction rule ***)
clasohm@923
    34
clasohm@1465
    35
val pred_name = "P";            (*name for predicate variables*)
clasohm@923
    36
clasohm@923
    37
val big_rec_def::part_rec_defs = Intr_elim.defs;
clasohm@923
    38
clasohm@923
    39
(*Used to express induction rules: adds induction hypotheses.
clasohm@923
    40
   ind_alist = [(rec_tm1,pred1),...]  -- associates predicates with rec ops
clasohm@923
    41
   prem is a premise of an intr rule*)
clasohm@923
    42
fun add_induct_prem ind_alist (prem as Const("Trueprop",_) $ 
clasohm@1465
    43
                 (Const("op :",_)$t$X), iprems) =
clasohm@923
    44
     (case gen_assoc (op aconv) (ind_alist, X) of
clasohm@1465
    45
          Some pred => prem :: Ind_Syntax.mk_Trueprop (pred $ t) :: iprems
clasohm@1465
    46
        | None => (*possibly membership in M(rec_tm), for M monotone*)
clasohm@1465
    47
            let fun mk_sb (rec_tm,pred) = 
clasohm@1465
    48
                 (case binder_types (fastype_of pred) of
clasohm@1465
    49
                      [T] => (rec_tm, 
clasohm@1465
    50
                              Ind_Syntax.Int_const T $ rec_tm $ 
clasohm@1465
    51
                                (Ind_Syntax.Collect_const T $ pred))
clasohm@1465
    52
                    | _ => error 
clasohm@1465
    53
                      "Bug: add_induct_prem called with non-unary predicate")
clasohm@1465
    54
            in  subst_free (map mk_sb ind_alist) prem :: iprems  end)
clasohm@923
    55
  | add_induct_prem ind_alist (prem,iprems) = prem :: iprems;
clasohm@923
    56
clasohm@923
    57
(*Make a premise of the induction rule.*)
clasohm@923
    58
fun induct_prem ind_alist intr =
clasohm@923
    59
  let val quantfrees = map dest_Free (term_frees intr \\ rec_params)
clasohm@923
    60
      val iprems = foldr (add_induct_prem ind_alist)
clasohm@1465
    61
                         (Logic.strip_imp_prems intr,[])
paulson@1424
    62
      val (t,X) = Ind_Syntax.rule_concl intr
clasohm@923
    63
      val (Some pred) = gen_assoc (op aconv) (ind_alist, X)
paulson@1424
    64
      val concl = Ind_Syntax.mk_Trueprop (pred $ t)
paulson@1424
    65
  in list_all_free (quantfrees, Logic.list_implies (iprems,concl)) end
clasohm@923
    66
  handle Bind => error"Recursion term not found in conclusion";
clasohm@923
    67
clasohm@923
    68
(*Avoids backtracking by delivering the correct premise to each goal*)
clasohm@923
    69
fun ind_tac [] 0 = all_tac
clasohm@923
    70
  | ind_tac(prem::prems) i = 
clasohm@1465
    71
        DEPTH_SOLVE_1 (ares_tac [Part_eqI, prem, refl] i) THEN
clasohm@1465
    72
        ind_tac prems (i-1);
clasohm@923
    73
paulson@1424
    74
val pred = Free(pred_name, elem_type --> Ind_Syntax.boolT);
clasohm@923
    75
paulson@1424
    76
val ind_prems = map (induct_prem (map (rpair pred) Inductive.rec_tms)) 
paulson@1424
    77
                    Inductive.intr_tms;
clasohm@923
    78
lcp@1190
    79
(*Debugging code...
lcp@1190
    80
val _ = writeln "ind_prems = ";
lcp@1190
    81
val _ = seq (writeln o Sign.string_of_term sign) ind_prems;
lcp@1190
    82
*)
lcp@1190
    83
paulson@1861
    84
(*We use a MINIMAL simpset because others (such as HOL_ss) contain too many
paulson@1861
    85
  simplifications.  If the premises get simplified, then the proofs will 
paulson@1861
    86
  fail.  This arose with a premise of the form {(F n,G n)|n . True}, which
paulson@1861
    87
  expanded to something containing ...&True. *)
oheimb@2637
    88
val min_ss = HOL_basic_ss;
paulson@1861
    89
clasohm@923
    90
val quant_induct = 
clasohm@923
    91
    prove_goalw_cterm part_rec_defs 
paulson@1424
    92
      (cterm_of sign 
paulson@1424
    93
       (Logic.list_implies (ind_prems, 
clasohm@1465
    94
                            Ind_Syntax.mk_Trueprop (Ind_Syntax.mk_all_imp 
clasohm@1465
    95
                                                    (big_rec_tm,pred)))))
clasohm@923
    96
      (fn prems =>
clasohm@923
    97
       [rtac (impI RS allI) 1,
clasohm@1465
    98
        DETERM (etac Intr_elim.raw_induct 1),
paulson@1861
    99
        full_simp_tac (min_ss addsimps [Part_Collect]) 1,
clasohm@1465
   100
        REPEAT (FIRSTGOAL (eresolve_tac [IntE, CollectE, exE, conjE, disjE] 
clasohm@1465
   101
                           ORELSE' hyp_subst_tac)),
clasohm@1465
   102
        ind_tac (rev prems) (length prems)])
clasohm@923
   103
    handle e => print_sign_exn sign e;
clasohm@923
   104
clasohm@923
   105
(*** Prove the simultaneous induction rule ***)
clasohm@923
   106
clasohm@923
   107
(*Make distinct predicates for each inductive set.
paulson@1728
   108
  Splits cartesian products in elem_type, however nested*)
paulson@1728
   109
paulson@1728
   110
(*The components of the element type, several if it is a product*)
nipkow@1746
   111
val elem_factors = Prod_Syntax.factors elem_type;
paulson@1728
   112
val elem_frees = mk_frees "za" elem_factors;
nipkow@1746
   113
val elem_tuple = Prod_Syntax.mk_tuple elem_type elem_frees;
clasohm@923
   114
lcp@1190
   115
(*Given a recursive set, return the "split" predicate
clasohm@923
   116
  and a conclusion for the simultaneous induction rule*)
lcp@1190
   117
fun mk_predpair rec_tm = 
clasohm@923
   118
  let val rec_name = (#1 o dest_Const o head_of) rec_tm
paulson@1728
   119
      val pfree = Free(pred_name ^ "_" ^ rec_name, 
paulson@2031
   120
                       elem_factors ---> Ind_Syntax.boolT)
clasohm@923
   121
      val qconcl = 
clasohm@1465
   122
        foldr Ind_Syntax.mk_all 
paulson@1728
   123
          (elem_frees, 
paulson@1728
   124
           Ind_Syntax.imp $ (Ind_Syntax.mk_mem (elem_tuple, rec_tm))
paulson@1728
   125
                $ (list_comb (pfree, elem_frees)))
nipkow@1746
   126
  in  (Prod_Syntax.ap_split elem_type Ind_Syntax.boolT pfree, 
paulson@1728
   127
       qconcl)  
clasohm@923
   128
  end;
clasohm@923
   129
paulson@1424
   130
val (preds,qconcls) = split_list (map mk_predpair Inductive.rec_tms);
clasohm@923
   131
clasohm@923
   132
(*Used to form simultaneous induction lemma*)
clasohm@923
   133
fun mk_rec_imp (rec_tm,pred) = 
paulson@1424
   134
    Ind_Syntax.imp $ (Ind_Syntax.mk_mem (Bound 0, rec_tm)) $  (pred $ Bound 0);
clasohm@923
   135
clasohm@923
   136
(*To instantiate the main induction rule*)
clasohm@923
   137
val induct_concl = 
paulson@1424
   138
    Ind_Syntax.mk_Trueprop
paulson@1424
   139
      (Ind_Syntax.mk_all_imp
paulson@1424
   140
       (big_rec_tm,
clasohm@1465
   141
        Abs("z", elem_type, 
clasohm@1465
   142
            fold_bal (app Ind_Syntax.conj) 
paulson@2270
   143
            (ListPair.map mk_rec_imp (Inductive.rec_tms,preds)))))
paulson@1424
   144
and mutual_induct_concl = 
paulson@1424
   145
    Ind_Syntax.mk_Trueprop (fold_bal (app Ind_Syntax.conj) qconcls);
clasohm@923
   146
paulson@1728
   147
val lemma_tac = FIRST' [eresolve_tac [asm_rl, conjE, PartE, mp],
paulson@2031
   148
                        resolve_tac [allI, impI, conjI, Part_eqI, refl],
paulson@2031
   149
                        dresolve_tac [spec, mp, splitD]];
paulson@1728
   150
clasohm@923
   151
val lemma = (*makes the link between the two induction rules*)
clasohm@923
   152
    prove_goalw_cterm part_rec_defs 
clasohm@1465
   153
          (cterm_of sign (Logic.mk_implies (induct_concl,
clasohm@1465
   154
                                            mutual_induct_concl)))
clasohm@1465
   155
          (fn prems =>
clasohm@1465
   156
           [cut_facts_tac prems 1,
paulson@1728
   157
            REPEAT (rewrite_goals_tac [split RS eq_reflection] THEN
paulson@2031
   158
                    lemma_tac 1)])
clasohm@923
   159
    handle e => print_sign_exn sign e;
clasohm@923
   160
clasohm@923
   161
(*Mutual induction follows by freeness of Inl/Inr.*)
clasohm@923
   162
lcp@1190
   163
(*Simplification largely reduces the mutual induction rule to the 
lcp@1190
   164
  standard rule*)
paulson@1985
   165
val mut_ss = min_ss addsimps [Inl_not_Inr, Inr_not_Inl, Inl_eq, Inr_eq, split];
lcp@1190
   166
paulson@1728
   167
val all_defs = [split RS eq_reflection] @ Inductive.con_defs @ part_rec_defs;
lcp@1190
   168
clasohm@923
   169
(*Removes Collects caused by M-operators in the intro rules*)
paulson@1424
   170
val cmonos = [subset_refl RS Int_Collect_mono] RL Inductive.monos RLN
paulson@1424
   171
             (2,[rev_subsetD]);
clasohm@923
   172
clasohm@923
   173
(*Avoids backtracking by delivering the correct premise to each goal*)
clasohm@923
   174
fun mutual_ind_tac [] 0 = all_tac
clasohm@923
   175
  | mutual_ind_tac(prem::prems) i = 
clasohm@923
   176
      DETERM
clasohm@923
   177
       (SELECT_GOAL 
clasohm@1465
   178
          (
clasohm@1465
   179
           (*Simplify the assumptions and goal by unfolding Part and
clasohm@1465
   180
             using freeness of the Sum constructors; proves all but one
lcp@1190
   181
             conjunct by contradiction*)
clasohm@1465
   182
           rewrite_goals_tac all_defs  THEN
clasohm@1465
   183
           simp_tac (mut_ss addsimps [Part_def]) 1  THEN
clasohm@1465
   184
           IF_UNSOLVED (*simp_tac may have finished it off!*)
paulson@1867
   185
             ((*simplify assumptions*)
paulson@1728
   186
              full_simp_tac mut_ss 1  THEN
clasohm@1465
   187
              (*unpackage and use "prem" in the corresponding place*)
clasohm@1465
   188
              REPEAT (rtac impI 1)  THEN
clasohm@1465
   189
              rtac (rewrite_rule all_defs prem) 1  THEN
clasohm@1465
   190
              (*prem must not be REPEATed below: could loop!*)
clasohm@1465
   191
              DEPTH_SOLVE (FIRSTGOAL (ares_tac [impI] ORELSE' 
clasohm@1465
   192
                                      eresolve_tac (conjE::mp::cmonos))))
clasohm@1465
   193
          ) i)
lcp@1190
   194
       THEN mutual_ind_tac prems (i-1);
lcp@1190
   195
lcp@1190
   196
val _ = writeln "  Proving the mutual induction rule...";
clasohm@923
   197
clasohm@923
   198
val mutual_induct_split = 
clasohm@923
   199
    prove_goalw_cterm []
clasohm@1465
   200
          (cterm_of sign
clasohm@1465
   201
           (Logic.list_implies (map (induct_prem (Inductive.rec_tms ~~ preds)) 
clasohm@1465
   202
                              Inductive.intr_tms,
clasohm@1465
   203
                          mutual_induct_concl)))
clasohm@1465
   204
          (fn prems =>
clasohm@1465
   205
           [rtac (quant_induct RS lemma) 1,
clasohm@1465
   206
            mutual_ind_tac (rev prems) (length prems)])
clasohm@923
   207
    handle e => print_sign_exn sign e;
clasohm@923
   208
paulson@1728
   209
(** Uncurrying the predicate in the ordinary induction rule **)
paulson@1728
   210
paulson@1728
   211
(*The name "x.1" comes from the "RS spec" !*)
paulson@1728
   212
val xvar = cterm_of sign (Var(("x",1), elem_type));
paulson@1728
   213
paulson@1728
   214
(*strip quantifier and instantiate the variable to a tuple*)
paulson@1728
   215
val induct0 = quant_induct RS spec RSN (2,rev_mp) |>
paulson@1728
   216
              freezeT |>     (*Because elem_type contains TFrees not TVars*)
paulson@1728
   217
              instantiate ([], [(xvar, cterm_of sign elem_tuple)]);
clasohm@923
   218
paulson@1424
   219
in
paulson@1424
   220
  struct
paulson@1728
   221
  val induct = standard 
nipkow@1746
   222
                  (Prod_Syntax.split_rule_var
paulson@2031
   223
                    (Var((pred_name,2), elem_type --> Ind_Syntax.boolT),
paulson@2031
   224
                     induct0));
clasohm@923
   225
paulson@1728
   226
  (*Just "True" unless there's true mutual recursion.  This saves storage.*)
paulson@1424
   227
  val mutual_induct = 
paulson@1728
   228
      if length Intr_elim.rec_names > 1
nipkow@1746
   229
      then Prod_Syntax.remove_split mutual_induct_split
paulson@1424
   230
      else TrueI;
paulson@1424
   231
  end
clasohm@923
   232
end;