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