src/HOL/indrule.ML
author clasohm
Wed Mar 13 11:55:25 1996 +0100 (1996-03-13 ago)
changeset 1574 5a63ab90ee8a
parent 1465 5d7a7e439cec
child 1653 1a2ffa2fbf7d
permissions -rw-r--r--
modified primrec so it can be used in MiniML/Type.thy
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),
clasohm@1465
    93
        asm_full_simp_tac (!simpset 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.
lcp@1190
   102
  Splits cartesian products in elem_type, IF nested to the right! *)
clasohm@923
   103
lcp@1190
   104
(*Given a recursive set, return the "split" predicate
clasohm@923
   105
  and a conclusion for the simultaneous induction rule*)
lcp@1190
   106
fun mk_predpair rec_tm = 
clasohm@923
   107
  let val rec_name = (#1 o dest_Const o head_of) rec_tm
paulson@1424
   108
      val T = Ind_Syntax.factors elem_type ---> Ind_Syntax.boolT
clasohm@923
   109
      val pfree = Free(pred_name ^ "_" ^ rec_name, T)
clasohm@923
   110
      val frees = mk_frees "za" (binder_types T)
clasohm@923
   111
      val qconcl = 
clasohm@1465
   112
        foldr Ind_Syntax.mk_all 
clasohm@1465
   113
          (frees, 
clasohm@1465
   114
           Ind_Syntax.imp $ (Ind_Syntax.mk_mem 
clasohm@1465
   115
                             (foldr1 Ind_Syntax.mk_Pair frees, rec_tm))
clasohm@1465
   116
                $ (list_comb (pfree,frees)))
paulson@1424
   117
  in  (Ind_Syntax.ap_split Ind_Syntax.boolT pfree (binder_types T), 
clasohm@923
   118
      qconcl)  
clasohm@923
   119
  end;
clasohm@923
   120
paulson@1424
   121
val (preds,qconcls) = split_list (map mk_predpair Inductive.rec_tms);
clasohm@923
   122
clasohm@923
   123
(*Used to form simultaneous induction lemma*)
clasohm@923
   124
fun mk_rec_imp (rec_tm,pred) = 
paulson@1424
   125
    Ind_Syntax.imp $ (Ind_Syntax.mk_mem (Bound 0, rec_tm)) $  (pred $ Bound 0);
clasohm@923
   126
clasohm@923
   127
(*To instantiate the main induction rule*)
clasohm@923
   128
val induct_concl = 
paulson@1424
   129
    Ind_Syntax.mk_Trueprop
paulson@1424
   130
      (Ind_Syntax.mk_all_imp
paulson@1424
   131
       (big_rec_tm,
clasohm@1465
   132
        Abs("z", elem_type, 
clasohm@1465
   133
            fold_bal (app Ind_Syntax.conj) 
clasohm@1465
   134
            (map mk_rec_imp (Inductive.rec_tms~~preds)))))
paulson@1424
   135
and mutual_induct_concl = 
paulson@1424
   136
    Ind_Syntax.mk_Trueprop (fold_bal (app Ind_Syntax.conj) qconcls);
clasohm@923
   137
clasohm@923
   138
val lemma = (*makes the link between the two induction rules*)
clasohm@923
   139
    prove_goalw_cterm part_rec_defs 
clasohm@1465
   140
          (cterm_of sign (Logic.mk_implies (induct_concl,
clasohm@1465
   141
                                            mutual_induct_concl)))
clasohm@1465
   142
          (fn prems =>
clasohm@1465
   143
           [cut_facts_tac prems 1,
clasohm@1465
   144
            REPEAT (eresolve_tac [asm_rl, conjE, PartE, mp] 1
clasohm@1465
   145
             ORELSE resolve_tac [allI, impI, conjI, Part_eqI, refl] 1
clasohm@1465
   146
             ORELSE dresolve_tac [spec, mp, splitD] 1)])
clasohm@923
   147
    handle e => print_sign_exn sign e;
clasohm@923
   148
clasohm@923
   149
(*Mutual induction follows by freeness of Inl/Inr.*)
clasohm@923
   150
lcp@1190
   151
(*Simplification largely reduces the mutual induction rule to the 
lcp@1190
   152
  standard rule*)
clasohm@1264
   153
val mut_ss = simpset_of "Fun"
clasohm@1264
   154
             addsimps [Inl_Inr_eq, Inr_Inl_eq, Inl_eq, Inr_eq];
lcp@1190
   155
paulson@1424
   156
val all_defs = Inductive.con_defs @ part_rec_defs;
lcp@1190
   157
clasohm@923
   158
(*Removes Collects caused by M-operators in the intro rules*)
paulson@1424
   159
val cmonos = [subset_refl RS Int_Collect_mono] RL Inductive.monos RLN
paulson@1424
   160
             (2,[rev_subsetD]);
clasohm@923
   161
clasohm@923
   162
(*Avoids backtracking by delivering the correct premise to each goal*)
clasohm@923
   163
fun mutual_ind_tac [] 0 = all_tac
clasohm@923
   164
  | mutual_ind_tac(prem::prems) i = 
clasohm@923
   165
      DETERM
clasohm@923
   166
       (SELECT_GOAL 
clasohm@1465
   167
          (
clasohm@1465
   168
           (*Simplify the assumptions and goal by unfolding Part and
clasohm@1465
   169
             using freeness of the Sum constructors; proves all but one
lcp@1190
   170
             conjunct by contradiction*)
clasohm@1465
   171
           rewrite_goals_tac all_defs  THEN
clasohm@1465
   172
           simp_tac (mut_ss addsimps [Part_def]) 1  THEN
clasohm@1465
   173
           IF_UNSOLVED (*simp_tac may have finished it off!*)
clasohm@1465
   174
             ((*simplify assumptions, but don't accept new rewrite rules!*)
clasohm@1465
   175
              asm_full_simp_tac (mut_ss setmksimps K[]) 1  THEN
clasohm@1465
   176
              (*unpackage and use "prem" in the corresponding place*)
clasohm@1465
   177
              REPEAT (rtac impI 1)  THEN
clasohm@1465
   178
              rtac (rewrite_rule all_defs prem) 1  THEN
clasohm@1465
   179
              (*prem must not be REPEATed below: could loop!*)
clasohm@1465
   180
              DEPTH_SOLVE (FIRSTGOAL (ares_tac [impI] ORELSE' 
clasohm@1465
   181
                                      eresolve_tac (conjE::mp::cmonos))))
clasohm@1465
   182
          ) i)
lcp@1190
   183
       THEN mutual_ind_tac prems (i-1);
lcp@1190
   184
lcp@1190
   185
val _ = writeln "  Proving the mutual induction rule...";
clasohm@923
   186
clasohm@923
   187
val mutual_induct_split = 
clasohm@923
   188
    prove_goalw_cterm []
clasohm@1465
   189
          (cterm_of sign
clasohm@1465
   190
           (Logic.list_implies (map (induct_prem (Inductive.rec_tms ~~ preds)) 
clasohm@1465
   191
                              Inductive.intr_tms,
clasohm@1465
   192
                          mutual_induct_concl)))
clasohm@1465
   193
          (fn prems =>
clasohm@1465
   194
           [rtac (quant_induct RS lemma) 1,
clasohm@1465
   195
            mutual_ind_tac (rev prems) (length prems)])
clasohm@923
   196
    handle e => print_sign_exn sign e;
clasohm@923
   197
clasohm@923
   198
(*Attempts to remove all occurrences of split*)
clasohm@923
   199
val split_tac =
clasohm@923
   200
    REPEAT (SOMEGOAL (FIRST' [rtac splitI, 
clasohm@1465
   201
                              dtac splitD,
clasohm@1465
   202
                              etac splitE,
clasohm@1465
   203
                              bound_hyp_subst_tac]))
clasohm@923
   204
    THEN prune_params_tac;
clasohm@923
   205
paulson@1424
   206
in
paulson@1424
   207
  struct
paulson@1424
   208
  (*strip quantifier*)
paulson@1424
   209
  val induct = standard (quant_induct RS spec RSN (2,rev_mp));
clasohm@923
   210
paulson@1424
   211
  val mutual_induct = 
paulson@1424
   212
      if length Intr_elim.rec_names > 1 orelse
clasohm@1465
   213
         length (Ind_Syntax.factors elem_type) > 1
paulson@1424
   214
      then rule_by_tactic split_tac mutual_induct_split
paulson@1424
   215
      else TrueI;
paulson@1424
   216
  end
clasohm@923
   217
end;