src/HOL/indrule.ML
author wenzelm
Thu Jan 23 14:19:16 1997 +0100 (1997-01-23)
changeset 2545 d10abc8c11fb
parent 2270 d7513875b2b8
child 2637 e9b203f854ae
permissions -rw-r--r--
added AxClasses test;
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. *)
paulson@1861
    88
val min_ss = empty_ss
paulson@1861
    89
      setmksimps (mksimps mksimps_pairs)
paulson@1861
    90
      setsolver (fn prems => resolve_tac (TrueI::refl::prems) ORELSE' atac
paulson@1861
    91
                             ORELSE' etac FalseE);
paulson@1861
    92
clasohm@923
    93
val quant_induct = 
clasohm@923
    94
    prove_goalw_cterm part_rec_defs 
paulson@1424
    95
      (cterm_of sign 
paulson@1424
    96
       (Logic.list_implies (ind_prems, 
clasohm@1465
    97
                            Ind_Syntax.mk_Trueprop (Ind_Syntax.mk_all_imp 
clasohm@1465
    98
                                                    (big_rec_tm,pred)))))
clasohm@923
    99
      (fn prems =>
clasohm@923
   100
       [rtac (impI RS allI) 1,
clasohm@1465
   101
        DETERM (etac Intr_elim.raw_induct 1),
paulson@1861
   102
        full_simp_tac (min_ss addsimps [Part_Collect]) 1,
clasohm@1465
   103
        REPEAT (FIRSTGOAL (eresolve_tac [IntE, CollectE, exE, conjE, disjE] 
clasohm@1465
   104
                           ORELSE' hyp_subst_tac)),
clasohm@1465
   105
        ind_tac (rev prems) (length prems)])
clasohm@923
   106
    handle e => print_sign_exn sign e;
clasohm@923
   107
clasohm@923
   108
(*** Prove the simultaneous induction rule ***)
clasohm@923
   109
clasohm@923
   110
(*Make distinct predicates for each inductive set.
paulson@1728
   111
  Splits cartesian products in elem_type, however nested*)
paulson@1728
   112
paulson@1728
   113
(*The components of the element type, several if it is a product*)
nipkow@1746
   114
val elem_factors = Prod_Syntax.factors elem_type;
paulson@1728
   115
val elem_frees = mk_frees "za" elem_factors;
nipkow@1746
   116
val elem_tuple = Prod_Syntax.mk_tuple elem_type elem_frees;
clasohm@923
   117
lcp@1190
   118
(*Given a recursive set, return the "split" predicate
clasohm@923
   119
  and a conclusion for the simultaneous induction rule*)
lcp@1190
   120
fun mk_predpair rec_tm = 
clasohm@923
   121
  let val rec_name = (#1 o dest_Const o head_of) rec_tm
paulson@1728
   122
      val pfree = Free(pred_name ^ "_" ^ rec_name, 
paulson@2031
   123
                       elem_factors ---> Ind_Syntax.boolT)
clasohm@923
   124
      val qconcl = 
clasohm@1465
   125
        foldr Ind_Syntax.mk_all 
paulson@1728
   126
          (elem_frees, 
paulson@1728
   127
           Ind_Syntax.imp $ (Ind_Syntax.mk_mem (elem_tuple, rec_tm))
paulson@1728
   128
                $ (list_comb (pfree, elem_frees)))
nipkow@1746
   129
  in  (Prod_Syntax.ap_split elem_type Ind_Syntax.boolT pfree, 
paulson@1728
   130
       qconcl)  
clasohm@923
   131
  end;
clasohm@923
   132
paulson@1424
   133
val (preds,qconcls) = split_list (map mk_predpair Inductive.rec_tms);
clasohm@923
   134
clasohm@923
   135
(*Used to form simultaneous induction lemma*)
clasohm@923
   136
fun mk_rec_imp (rec_tm,pred) = 
paulson@1424
   137
    Ind_Syntax.imp $ (Ind_Syntax.mk_mem (Bound 0, rec_tm)) $  (pred $ Bound 0);
clasohm@923
   138
clasohm@923
   139
(*To instantiate the main induction rule*)
clasohm@923
   140
val induct_concl = 
paulson@1424
   141
    Ind_Syntax.mk_Trueprop
paulson@1424
   142
      (Ind_Syntax.mk_all_imp
paulson@1424
   143
       (big_rec_tm,
clasohm@1465
   144
        Abs("z", elem_type, 
clasohm@1465
   145
            fold_bal (app Ind_Syntax.conj) 
paulson@2270
   146
            (ListPair.map mk_rec_imp (Inductive.rec_tms,preds)))))
paulson@1424
   147
and mutual_induct_concl = 
paulson@1424
   148
    Ind_Syntax.mk_Trueprop (fold_bal (app Ind_Syntax.conj) qconcls);
clasohm@923
   149
paulson@1728
   150
val lemma_tac = FIRST' [eresolve_tac [asm_rl, conjE, PartE, mp],
paulson@2031
   151
                        resolve_tac [allI, impI, conjI, Part_eqI, refl],
paulson@2031
   152
                        dresolve_tac [spec, mp, splitD]];
paulson@1728
   153
clasohm@923
   154
val lemma = (*makes the link between the two induction rules*)
clasohm@923
   155
    prove_goalw_cterm part_rec_defs 
clasohm@1465
   156
          (cterm_of sign (Logic.mk_implies (induct_concl,
clasohm@1465
   157
                                            mutual_induct_concl)))
clasohm@1465
   158
          (fn prems =>
clasohm@1465
   159
           [cut_facts_tac prems 1,
paulson@1728
   160
            REPEAT (rewrite_goals_tac [split RS eq_reflection] THEN
paulson@2031
   161
                    lemma_tac 1)])
clasohm@923
   162
    handle e => print_sign_exn sign e;
clasohm@923
   163
clasohm@923
   164
(*Mutual induction follows by freeness of Inl/Inr.*)
clasohm@923
   165
lcp@1190
   166
(*Simplification largely reduces the mutual induction rule to the 
lcp@1190
   167
  standard rule*)
paulson@1985
   168
val mut_ss = min_ss addsimps [Inl_not_Inr, Inr_not_Inl, Inl_eq, Inr_eq, split];
lcp@1190
   169
paulson@1728
   170
val all_defs = [split RS eq_reflection] @ Inductive.con_defs @ part_rec_defs;
lcp@1190
   171
clasohm@923
   172
(*Removes Collects caused by M-operators in the intro rules*)
paulson@1424
   173
val cmonos = [subset_refl RS Int_Collect_mono] RL Inductive.monos RLN
paulson@1424
   174
             (2,[rev_subsetD]);
clasohm@923
   175
clasohm@923
   176
(*Avoids backtracking by delivering the correct premise to each goal*)
clasohm@923
   177
fun mutual_ind_tac [] 0 = all_tac
clasohm@923
   178
  | mutual_ind_tac(prem::prems) i = 
clasohm@923
   179
      DETERM
clasohm@923
   180
       (SELECT_GOAL 
clasohm@1465
   181
          (
clasohm@1465
   182
           (*Simplify the assumptions and goal by unfolding Part and
clasohm@1465
   183
             using freeness of the Sum constructors; proves all but one
lcp@1190
   184
             conjunct by contradiction*)
clasohm@1465
   185
           rewrite_goals_tac all_defs  THEN
clasohm@1465
   186
           simp_tac (mut_ss addsimps [Part_def]) 1  THEN
clasohm@1465
   187
           IF_UNSOLVED (*simp_tac may have finished it off!*)
paulson@1867
   188
             ((*simplify assumptions*)
paulson@1728
   189
              full_simp_tac mut_ss 1  THEN
clasohm@1465
   190
              (*unpackage and use "prem" in the corresponding place*)
clasohm@1465
   191
              REPEAT (rtac impI 1)  THEN
clasohm@1465
   192
              rtac (rewrite_rule all_defs prem) 1  THEN
clasohm@1465
   193
              (*prem must not be REPEATed below: could loop!*)
clasohm@1465
   194
              DEPTH_SOLVE (FIRSTGOAL (ares_tac [impI] ORELSE' 
clasohm@1465
   195
                                      eresolve_tac (conjE::mp::cmonos))))
clasohm@1465
   196
          ) i)
lcp@1190
   197
       THEN mutual_ind_tac prems (i-1);
lcp@1190
   198
lcp@1190
   199
val _ = writeln "  Proving the mutual induction rule...";
clasohm@923
   200
clasohm@923
   201
val mutual_induct_split = 
clasohm@923
   202
    prove_goalw_cterm []
clasohm@1465
   203
          (cterm_of sign
clasohm@1465
   204
           (Logic.list_implies (map (induct_prem (Inductive.rec_tms ~~ preds)) 
clasohm@1465
   205
                              Inductive.intr_tms,
clasohm@1465
   206
                          mutual_induct_concl)))
clasohm@1465
   207
          (fn prems =>
clasohm@1465
   208
           [rtac (quant_induct RS lemma) 1,
clasohm@1465
   209
            mutual_ind_tac (rev prems) (length prems)])
clasohm@923
   210
    handle e => print_sign_exn sign e;
clasohm@923
   211
paulson@1728
   212
(** Uncurrying the predicate in the ordinary induction rule **)
paulson@1728
   213
paulson@1728
   214
(*The name "x.1" comes from the "RS spec" !*)
paulson@1728
   215
val xvar = cterm_of sign (Var(("x",1), elem_type));
paulson@1728
   216
paulson@1728
   217
(*strip quantifier and instantiate the variable to a tuple*)
paulson@1728
   218
val induct0 = quant_induct RS spec RSN (2,rev_mp) |>
paulson@1728
   219
              freezeT |>     (*Because elem_type contains TFrees not TVars*)
paulson@1728
   220
              instantiate ([], [(xvar, cterm_of sign elem_tuple)]);
clasohm@923
   221
paulson@1424
   222
in
paulson@1424
   223
  struct
paulson@1728
   224
  val induct = standard 
nipkow@1746
   225
                  (Prod_Syntax.split_rule_var
paulson@2031
   226
                    (Var((pred_name,2), elem_type --> Ind_Syntax.boolT),
paulson@2031
   227
                     induct0));
clasohm@923
   228
paulson@1728
   229
  (*Just "True" unless there's true mutual recursion.  This saves storage.*)
paulson@1424
   230
  val mutual_induct = 
paulson@1728
   231
      if length Intr_elim.rec_names > 1
nipkow@1746
   232
      then Prod_Syntax.remove_split mutual_induct_split
paulson@1424
   233
      else TrueI;
paulson@1424
   234
  end
clasohm@923
   235
end;