src/HOL/Tools/inductive_package.ML
author berghofe
Wed, 15 Jul 1998 18:26:15 +0200
changeset 5149 10f0be29c0d1
parent 5120 f7f5442c934a
child 5179 819f90f278db
permissions -rw-r--r--
Fixed bug in transform_rule.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5094
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
     1
(*  Title:      HOL/Tools/inductive_package.ML
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
     2
    ID:         $Id$
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
     4
                Stefan Berghofer,   TU Muenchen
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
     5
    Copyright   1994  University of Cambridge
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
     6
                1998  TU Muenchen     
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
     7
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
     8
(Co)Inductive Definition module for HOL
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
     9
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    10
Features:
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    11
* least or greatest fixedpoints
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    12
* user-specified product and sum constructions
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    13
* mutually recursive definitions
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    14
* definitions involving arbitrary monotone operators
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    15
* automatically proves introduction and elimination rules
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    16
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    17
The recursive sets must *already* be declared as constants in parent theory!
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    18
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    19
  Introduction rules have the form
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    20
  [| ti:M(Sj), ..., P(x), ... |] ==> t: Sk |]
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    21
  where M is some monotone operator (usually the identity)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    22
  P(x) is any side condition on the free variables
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    23
  ti, t are any terms
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    24
  Sj, Sk are two of the sets being defined in mutual recursion
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    25
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    26
Sums are used only for mutual recursion;
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    27
Products are used only to derive "streamlined" induction rules for relations
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    28
*)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    29
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    30
signature INDUCTIVE_PACKAGE =
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    31
sig
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    32
  val add_inductive_i : bool -> bool -> bstring -> bool -> bool -> bool -> term list ->
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    33
    term list -> thm list -> thm list -> theory -> theory *
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    34
      {defs:thm list, elims:thm list, raw_induct:thm, induct:thm,
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    35
       intrs:thm list,
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    36
       mk_cases:thm list -> string -> thm, mono:thm,
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    37
       unfold:thm}
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    38
  val add_inductive : bool -> bool -> string list -> string list
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    39
    -> thm list -> thm list -> theory -> theory *
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    40
      {defs:thm list, elims:thm list, raw_induct:thm, induct:thm,
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    41
       intrs:thm list,
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    42
       mk_cases:thm list -> string -> thm, mono:thm,
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    43
       unfold:thm}
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    44
end;
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    45
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    46
structure InductivePackage : INDUCTIVE_PACKAGE =
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    47
struct
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    48
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    49
(*For proving monotonicity of recursion operator*)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    50
val basic_monos = [subset_refl, imp_refl, disj_mono, conj_mono, 
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    51
                   ex_mono, Collect_mono, in_mono, vimage_mono];
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    52
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    53
val Const _ $ (vimage_f $ _) $ _ = HOLogic.dest_Trueprop (concl_of vimageD);
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    54
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    55
(*Delete needless equality assumptions*)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    56
val refl_thin = prove_goal HOL.thy "!!P. [| a=a;  P |] ==> P"
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    57
     (fn _ => [assume_tac 1]);
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    58
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    59
(*For simplifying the elimination rule*)
5120
f7f5442c934a Removed disjE from list of rules used to simplify elimination
berghofe
parents: 5108
diff changeset
    60
val elim_rls = [asm_rl, FalseE, refl_thin, conjE, exE, Pair_inject];
5094
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    61
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    62
val vimage_name = Sign.intern_const (sign_of Vimage.thy) "op -``";
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    63
val mono_name = Sign.intern_const (sign_of Ord.thy) "mono";
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    64
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    65
(* make injections needed in mutually recursive definitions *)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    66
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    67
fun mk_inj cs sumT c x =
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    68
  let
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    69
    fun mk_inj' T n i =
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    70
      if n = 1 then x else
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    71
      let val n2 = n div 2;
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    72
          val Type (_, [T1, T2]) = T
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    73
      in
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    74
        if i <= n2 then
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    75
          Const ("Inl", T1 --> T) $ (mk_inj' T1 n2 i)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    76
        else
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    77
          Const ("Inr", T2 --> T) $ (mk_inj' T2 (n - n2) (i - n2))
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    78
      end
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    79
  in mk_inj' sumT (length cs) (1 + find_index_eq c cs)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    80
  end;
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    81
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    82
(* make "vimage" terms for selecting out components of mutually rec.def. *)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    83
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    84
fun mk_vimage cs sumT t c = if length cs < 2 then t else
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    85
  let
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    86
    val cT = HOLogic.dest_setT (fastype_of c);
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    87
    val vimageT = [cT --> sumT, HOLogic.mk_setT sumT] ---> HOLogic.mk_setT cT
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    88
  in
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    89
    Const (vimage_name, vimageT) $
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    90
      Abs ("y", cT, mk_inj cs sumT c (Bound 0)) $ t
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    91
  end;
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    92
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    93
(**************************** well-formedness checks **************************)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    94
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    95
fun err_in_rule sign t msg = error ("Ill-formed introduction rule\n" ^
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    96
  (Sign.string_of_term sign t) ^ "\n" ^ msg);
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    97
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    98
fun err_in_prem sign t p msg = error ("Ill-formed premise\n" ^
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
    99
  (Sign.string_of_term sign p) ^ "\nin introduction rule\n" ^
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   100
  (Sign.string_of_term sign t) ^ "\n" ^ msg);
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   101
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   102
val msg1 = "Conclusion of introduction rule must have form\
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   103
          \ ' t : S_i '";
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   104
val msg2 = "Premises mentioning recursive sets must have form\
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   105
          \ ' t : M S_i '";
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   106
val msg3 = "Recursion term on left of member symbol";
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   107
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   108
fun check_rule sign cs r =
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   109
  let
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   110
    fun check_prem prem = if exists (Logic.occs o (rpair prem)) cs then
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   111
         (case prem of
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   112
           (Const ("op :", _) $ t $ u) =>
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   113
             if exists (Logic.occs o (rpair t)) cs then
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   114
               err_in_prem sign r prem msg3 else ()
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   115
         | _ => err_in_prem sign r prem msg2)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   116
        else ()
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   117
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   118
  in (case (HOLogic.dest_Trueprop o Logic.strip_imp_concl) r of
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   119
        (Const ("op :", _) $ _ $ u) =>
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   120
          if u mem cs then map (check_prem o HOLogic.dest_Trueprop)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   121
            (Logic.strip_imp_prems r)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   122
          else err_in_rule sign r msg1
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   123
      | _ => err_in_rule sign r msg1)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   124
  end;
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   125
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   126
fun try' f msg sign t = (f t) handle _ => error (msg ^ Sign.string_of_term sign t);
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   127
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   128
(*********************** properties of (co)inductive sets *********************)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   129
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   130
(***************************** elimination rules ******************************)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   131
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   132
fun mk_elims cs cTs params intr_ts =
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   133
  let
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   134
    val used = foldr add_term_names (intr_ts, []);
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   135
    val [aname, pname] = variantlist (["a", "P"], used);
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   136
    val P = HOLogic.mk_Trueprop (Free (pname, HOLogic.boolT));
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   137
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   138
    fun dest_intr r =
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   139
      let val Const ("op :", _) $ t $ u =
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   140
        HOLogic.dest_Trueprop (Logic.strip_imp_concl r)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   141
      in (u, t, Logic.strip_imp_prems r) end;
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   142
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   143
    val intrs = map dest_intr intr_ts;
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   144
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   145
    fun mk_elim (c, T) =
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   146
      let
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   147
        val a = Free (aname, T);
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   148
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   149
        fun mk_elim_prem (_, t, ts) =
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   150
          list_all_free (map dest_Free ((foldr add_term_frees (t::ts, [])) \\ params),
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   151
            Logic.list_implies (HOLogic.mk_Trueprop (HOLogic.mk_eq (a, t)) :: ts, P));
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   152
      in
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   153
        Logic.list_implies (HOLogic.mk_Trueprop (HOLogic.mk_mem (a, c)) ::
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   154
          map mk_elim_prem (filter (equal c o #1) intrs), P)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   155
      end
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   156
  in
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   157
    map mk_elim (cs ~~ cTs)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   158
  end;
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   159
        
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   160
(***************** premises and conclusions of induction rules ****************)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   161
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   162
fun mk_indrule cs cTs params intr_ts =
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   163
  let
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   164
    val used = foldr add_term_names (intr_ts, []);
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   165
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   166
    (* predicates for induction rule *)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   167
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   168
    val preds = map Free (variantlist (if length cs < 2 then ["P"] else
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   169
      map (fn i => "P" ^ string_of_int i) (1 upto length cs), used) ~~
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   170
        map (fn T => T --> HOLogic.boolT) cTs);
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   171
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   172
    (* transform an introduction rule into a premise for induction rule *)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   173
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   174
    fun mk_ind_prem r =
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   175
      let
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   176
        val frees = map dest_Free ((add_term_frees (r, [])) \\ params);
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   177
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   178
        fun subst (prem as (Const ("op :", T) $ t $ u), prems) =
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   179
              let val n = find_index_eq u cs in
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   180
                if n >= 0 then prem :: (nth_elem (n, preds)) $ t :: prems else
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   181
                  (subst_free (map (fn (c, P) => (c, HOLogic.mk_binop "op Int"
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   182
                    (c, HOLogic.Collect_const (HOLogic.dest_setT
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   183
                      (fastype_of c)) $ P))) (cs ~~ preds)) prem)::prems
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   184
              end
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   185
          | subst (prem, prems) = prem::prems;
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   186
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   187
        val Const ("op :", _) $ t $ u =
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   188
          HOLogic.dest_Trueprop (Logic.strip_imp_concl r)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   189
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   190
      in list_all_free (frees,
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   191
           Logic.list_implies (map HOLogic.mk_Trueprop (foldr subst
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   192
             (map HOLogic.dest_Trueprop (Logic.strip_imp_prems r), [])),
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   193
               HOLogic.mk_Trueprop (nth_elem (find_index_eq u cs, preds) $ t)))
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   194
      end;
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   195
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   196
    val ind_prems = map mk_ind_prem intr_ts;
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   197
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   198
    (* make conclusions for induction rules *)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   199
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   200
    fun mk_ind_concl ((c, P), (ts, x)) =
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   201
      let val T = HOLogic.dest_setT (fastype_of c);
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   202
          val Ts = HOLogic.prodT_factors T;
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   203
          val (frees, x') = foldr (fn (T', (fs, s)) =>
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   204
            ((Free (s, T'))::fs, bump_string s)) (Ts, ([], x));
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   205
          val tuple = HOLogic.mk_tuple T frees;
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   206
      in ((HOLogic.mk_binop "op -->"
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   207
        (HOLogic.mk_mem (tuple, c), P $ tuple))::ts, x')
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   208
      end;
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   209
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   210
    val mutual_ind_concl = HOLogic.mk_Trueprop (foldr1 (app HOLogic.conj)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   211
        (fst (foldr mk_ind_concl (cs ~~ preds, ([], "xa")))))
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   212
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   213
  in (preds, ind_prems, mutual_ind_concl)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   214
  end;
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   215
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   216
(********************** proofs for (co)inductive sets *************************)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   217
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   218
(**************************** prove monotonicity ******************************)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   219
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   220
fun prove_mono setT fp_fun monos thy =
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   221
  let
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   222
    val _ = writeln "  Proving monotonicity...";
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   223
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   224
    val mono = prove_goalw_cterm [] (cterm_of (sign_of thy) (HOLogic.mk_Trueprop
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   225
      (Const (mono_name, (setT --> setT) --> HOLogic.boolT) $ fp_fun)))
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   226
        (fn _ => [rtac monoI 1, REPEAT (ares_tac (basic_monos @ monos) 1)])
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   227
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   228
  in mono end;
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   229
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   230
(************************* prove introduction rules ***************************)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   231
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   232
fun prove_intrs coind mono fp_def intr_ts con_defs rec_sets_defs thy =
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   233
  let
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   234
    val _ = writeln "  Proving the introduction rules...";
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   235
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   236
    val unfold = standard (mono RS (fp_def RS
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   237
      (if coind then def_gfp_Tarski else def_lfp_Tarski)));
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   238
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   239
    fun select_disj 1 1 = []
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   240
      | select_disj _ 1 = [rtac disjI1]
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   241
      | select_disj n i = (rtac disjI2)::(select_disj (n - 1) (i - 1));
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   242
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   243
    val intrs = map (fn (i, intr) => prove_goalw_cterm rec_sets_defs
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   244
      (cterm_of (sign_of thy) intr) (fn prems =>
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   245
       [(*insert prems and underlying sets*)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   246
       cut_facts_tac prems 1,
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   247
       stac unfold 1,
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   248
       REPEAT (resolve_tac [vimageI2, CollectI] 1),
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   249
       (*Now 1-2 subgoals: the disjunction, perhaps equality.*)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   250
       EVERY1 (select_disj (length intr_ts) i),
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   251
       (*Not ares_tac, since refl must be tried before any equality assumptions;
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   252
         backtracking may occur if the premises have extra variables!*)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   253
       DEPTH_SOLVE_1 (resolve_tac [refl,exI,conjI] 1 APPEND assume_tac 1),
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   254
       (*Now solve the equations like Inl 0 = Inl ?b2*)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   255
       rewrite_goals_tac con_defs,
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   256
       REPEAT (rtac refl 1)])) (1 upto (length intr_ts) ~~ intr_ts)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   257
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   258
  in (intrs, unfold) end;
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   259
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   260
(*************************** prove elimination rules **************************)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   261
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   262
fun prove_elims cs cTs params intr_ts unfold rec_sets_defs thy =
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   263
  let
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   264
    val _ = writeln "  Proving the elimination rules...";
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   265
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   266
    val rules1 = [CollectE, disjE, make_elim vimageD];
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   267
    val rules2 = [exE, conjE, Inl_neq_Inr, Inr_neq_Inl] @
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   268
      map make_elim [Inl_inject, Inr_inject];
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   269
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   270
    val elims = map (fn t => prove_goalw_cterm rec_sets_defs
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   271
      (cterm_of (sign_of thy) t) (fn prems =>
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   272
        [cut_facts_tac [hd prems] 1,
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   273
         dtac (unfold RS subst) 1,
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   274
         REPEAT (FIRSTGOAL (eresolve_tac rules1)),
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   275
         REPEAT (FIRSTGOAL (eresolve_tac rules2)),
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   276
         EVERY (map (fn prem =>
5149
10f0be29c0d1 Fixed bug in transform_rule.
berghofe
parents: 5120
diff changeset
   277
           DEPTH_SOLVE_1 (ares_tac [prem, conjI] 1)) (tl prems))]))
5094
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   278
      (mk_elims cs cTs params intr_ts)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   279
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   280
  in elims end;
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   281
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   282
(** derivation of simplified elimination rules **)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   283
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   284
(*Applies freeness of the given constructors, which *must* be unfolded by
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   285
  the given defs.  Cannot simply use the local con_defs because con_defs=[] 
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   286
  for inference systems.
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   287
 *)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   288
fun con_elim_tac simps =
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   289
  let val elim_tac = REPEAT o (eresolve_tac elim_rls)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   290
  in ALLGOALS(EVERY'[elim_tac,
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   291
                 asm_full_simp_tac (simpset_of Nat.thy addsimps simps),
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   292
                 elim_tac,
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   293
                 REPEAT o bound_hyp_subst_tac])
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   294
     THEN prune_params_tac
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   295
  end;
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   296
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   297
(*String s should have the form t:Si where Si is an inductive set*)
5120
f7f5442c934a Removed disjE from list of rules used to simplify elimination
berghofe
parents: 5108
diff changeset
   298
fun mk_cases elims simps s =
5094
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   299
  let val prem = assume (read_cterm (sign_of_thm (hd elims)) (s, propT));
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   300
      val elims' = map (try (fn r =>
5120
f7f5442c934a Removed disjE from list of rules used to simplify elimination
berghofe
parents: 5108
diff changeset
   301
        rule_by_tactic (con_elim_tac simps) (prem RS r) |> standard)) elims
5094
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   302
  in case find_first is_some elims' of
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   303
       Some (Some r) => r
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   304
     | None => error ("mk_cases: string '" ^ s ^ "' not of form 't : S_i'")
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   305
  end;
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   306
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   307
(**************************** prove induction rule ****************************)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   308
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   309
fun prove_indrule cs cTs sumT rec_const params intr_ts mono
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   310
    fp_def rec_sets_defs thy =
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   311
  let
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   312
    val _ = writeln "  Proving the induction rule...";
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   313
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   314
    val sign = sign_of thy;
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   315
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   316
    val (preds, ind_prems, mutual_ind_concl) = mk_indrule cs cTs params intr_ts;
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   317
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   318
    (* make predicate for instantiation of abstract induction rule *)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   319
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   320
    fun mk_ind_pred _ [P] = P
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   321
      | mk_ind_pred T Ps =
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   322
         let val n = (length Ps) div 2;
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   323
             val Type (_, [T1, T2]) = T
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   324
         in Const ("sum_case",
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   325
           [T1 --> HOLogic.boolT, T2 --> HOLogic.boolT, T] ---> HOLogic.boolT) $
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   326
             mk_ind_pred T1 (take (n, Ps)) $ mk_ind_pred T2 (drop (n, Ps))
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   327
         end;
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   328
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   329
    val ind_pred = mk_ind_pred sumT preds;
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   330
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   331
    val ind_concl = HOLogic.mk_Trueprop
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   332
      (HOLogic.all_const sumT $ Abs ("x", sumT, HOLogic.mk_binop "op -->"
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   333
        (HOLogic.mk_mem (Bound 0, rec_const), ind_pred $ Bound 0)));
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   334
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   335
    (* simplification rules for vimage and Collect *)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   336
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   337
    val vimage_simps = if length cs < 2 then [] else
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   338
      map (fn c => prove_goalw_cterm [] (cterm_of sign
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   339
        (HOLogic.mk_Trueprop (HOLogic.mk_eq
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   340
          (mk_vimage cs sumT (HOLogic.Collect_const sumT $ ind_pred) c,
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   341
           HOLogic.Collect_const (HOLogic.dest_setT (fastype_of c)) $
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   342
             nth_elem (find_index_eq c cs, preds)))))
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   343
        (fn _ => [rtac vimage_Collect 1, rewrite_goals_tac
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   344
           (map mk_meta_eq [sum_case_Inl, sum_case_Inr]),
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   345
          rtac refl 1])) cs;
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   346
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   347
    val induct = prove_goalw_cterm [] (cterm_of sign
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   348
      (Logic.list_implies (ind_prems, ind_concl))) (fn prems =>
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   349
        [rtac (impI RS allI) 1,
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   350
         DETERM (etac (mono RS (fp_def RS def_induct)) 1),
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   351
         rewrite_goals_tac (map mk_meta_eq (vimage_Int::vimage_simps)),
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   352
         fold_goals_tac rec_sets_defs,
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   353
         (*This CollectE and disjE separates out the introduction rules*)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   354
         REPEAT (FIRSTGOAL (eresolve_tac [CollectE, disjE])),
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   355
         (*Now break down the individual cases.  No disjE here in case
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   356
           some premise involves disjunction.*)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   357
         REPEAT (FIRSTGOAL (eresolve_tac [IntE, CollectE, exE, conjE] 
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   358
                     ORELSE' hyp_subst_tac)),
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   359
         rewrite_goals_tac (map mk_meta_eq [sum_case_Inl, sum_case_Inr]),
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   360
         EVERY (map (fn prem =>
5149
10f0be29c0d1 Fixed bug in transform_rule.
berghofe
parents: 5120
diff changeset
   361
           DEPTH_SOLVE_1 (ares_tac [prem, conjI, refl] 1)) prems)]);
5094
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   362
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   363
    val lemma = prove_goalw_cterm rec_sets_defs (cterm_of sign
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   364
      (Logic.mk_implies (ind_concl, mutual_ind_concl))) (fn prems =>
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   365
        [cut_facts_tac prems 1,
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   366
         REPEAT (EVERY
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   367
           [REPEAT (resolve_tac [conjI, impI] 1),
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   368
            TRY (dtac vimageD 1), etac allE 1, dtac mp 1, atac 1,
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   369
            rewrite_goals_tac (map mk_meta_eq [sum_case_Inl, sum_case_Inr]),
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   370
            atac 1])])
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   371
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   372
  in standard (split_rule (induct RS lemma))
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   373
  end;
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   374
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   375
(*************** definitional introduction of (co)inductive sets **************)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   376
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   377
fun add_ind_def verbose declare_consts alt_name coind no_elim no_ind cs
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   378
    intr_ts monos con_defs thy params paramTs cTs cnames =
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   379
  let
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   380
    val _ = if verbose then writeln ("Proofs for " ^
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   381
      (if coind then "co" else "") ^ "inductive set(s) " ^ commas cnames) else ();
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   382
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   383
    val sumT = fold_bal (fn (T, U) => Type ("+", [T, U])) cTs;
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   384
    val setT = HOLogic.mk_setT sumT;
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   385
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   386
    val fp_name = if coind then Sign.intern_const (sign_of Gfp.thy) "gfp"
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   387
      else Sign.intern_const (sign_of Lfp.thy) "lfp";
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   388
5149
10f0be29c0d1 Fixed bug in transform_rule.
berghofe
parents: 5120
diff changeset
   389
    val used = foldr add_term_names (intr_ts, []);
10f0be29c0d1 Fixed bug in transform_rule.
berghofe
parents: 5120
diff changeset
   390
    val [sname, xname] = variantlist (["S", "x"], used);
10f0be29c0d1 Fixed bug in transform_rule.
berghofe
parents: 5120
diff changeset
   391
5094
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   392
    (* transform an introduction rule into a conjunction  *)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   393
    (*   [| t : ... S_i ... ; ... |] ==> u : S_j          *)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   394
    (* is transformed into                                *)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   395
    (*   x = Inj_j u & t : ... Inj_i -`` S ... & ...      *)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   396
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   397
    fun transform_rule r =
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   398
      let
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   399
        val frees = map dest_Free ((add_term_frees (r, [])) \\ params);
5149
10f0be29c0d1 Fixed bug in transform_rule.
berghofe
parents: 5120
diff changeset
   400
        val subst = subst_free
10f0be29c0d1 Fixed bug in transform_rule.
berghofe
parents: 5120
diff changeset
   401
          (cs ~~ (map (mk_vimage cs sumT (Free (sname, setT))) cs));
5094
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   402
        val Const ("op :", _) $ t $ u =
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   403
          HOLogic.dest_Trueprop (Logic.strip_imp_concl r)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   404
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   405
      in foldr (fn ((x, T), P) => HOLogic.mk_exists (x, T, P))
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   406
        (frees, foldr1 (app HOLogic.conj)
5149
10f0be29c0d1 Fixed bug in transform_rule.
berghofe
parents: 5120
diff changeset
   407
          (((HOLogic.eq_const sumT) $ Free (xname, sumT) $ (mk_inj cs sumT u t))::
5094
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   408
            (map (subst o HOLogic.dest_Trueprop)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   409
              (Logic.strip_imp_prems r))))
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   410
      end
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   411
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   412
    (* make a disjunction of all introduction rules *)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   413
5149
10f0be29c0d1 Fixed bug in transform_rule.
berghofe
parents: 5120
diff changeset
   414
    val fp_fun = absfree (sname, setT, (HOLogic.Collect_const sumT) $
10f0be29c0d1 Fixed bug in transform_rule.
berghofe
parents: 5120
diff changeset
   415
      absfree (xname, sumT, foldr1 (app HOLogic.disj) (map transform_rule intr_ts)));
5094
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   416
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   417
    (* add definiton of recursive sets to theory *)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   418
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   419
    val rec_name = if alt_name = "" then space_implode "_" cnames else alt_name;
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   420
    val full_rec_name = Sign.full_name (sign_of thy) rec_name;
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   421
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   422
    val rec_const = list_comb
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   423
      (Const (full_rec_name, paramTs ---> setT), params);
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   424
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   425
    val fp_def_term = Logic.mk_equals (rec_const,
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   426
      Const (fp_name, (setT --> setT) --> setT) $ fp_fun)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   427
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   428
    val def_terms = fp_def_term :: (if length cs < 2 then [] else
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   429
      map (fn c => Logic.mk_equals (c, mk_vimage cs sumT rec_const c)) cs);
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   430
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   431
    val thy' = thy |>
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   432
      (if declare_consts then
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   433
        Theory.add_consts_i (map (fn (c, n) =>
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   434
          (n, paramTs ---> fastype_of c, NoSyn)) (cs ~~ cnames))
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   435
       else I) |>
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   436
      (if length cs < 2 then I else
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   437
       Theory.add_consts_i [(rec_name, paramTs ---> setT, NoSyn)]) |>
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   438
      Theory.add_path rec_name |>
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   439
      PureThy.add_defss_i [(("defs", def_terms), [])];
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   440
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   441
    (* get definitions from theory *)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   442
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   443
    val fp_def::rec_sets_defs = get_thms thy' "defs";
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   444
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   445
    (* prove and store theorems *)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   446
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   447
    val mono = prove_mono setT fp_fun monos thy';
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   448
    val (intrs, unfold) = prove_intrs coind mono fp_def intr_ts con_defs
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   449
      rec_sets_defs thy';
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   450
    val elims = if no_elim then [] else
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   451
      prove_elims cs cTs params intr_ts unfold rec_sets_defs thy';
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   452
    val raw_induct = if no_ind then TrueI else
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   453
      if coind then standard (rule_by_tactic
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   454
        (rewrite_tac [mk_meta_eq vimage_Un] THEN
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   455
          fold_tac rec_sets_defs) (mono RS (fp_def RS def_Collect_coinduct)))
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   456
      else
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   457
        prove_indrule cs cTs sumT rec_const params intr_ts mono fp_def
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   458
          rec_sets_defs thy';
5108
4074c7d86d44 Fixed bug (improper handling of flag no_ind).
berghofe
parents: 5094
diff changeset
   459
    val induct = if coind orelse no_ind orelse length cs > 1 then raw_induct
5094
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   460
      else standard (raw_induct RSN (2, rev_mp));
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   461
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   462
    val thy'' = thy' |>
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   463
      PureThy.add_tthmss [(("intrs", map Attribute.tthm_of intrs), [])] |>
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   464
      (if no_elim then I else PureThy.add_tthmss
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   465
        [(("elims", map Attribute.tthm_of elims), [])]) |>
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   466
      (if no_ind then I else PureThy.add_tthms
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   467
        [(((if coind then "co" else "") ^ "induct",
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   468
          Attribute.tthm_of induct), [])]) |>
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   469
      Theory.parent_path;
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   470
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   471
  in (thy'',
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   472
    {defs = fp_def::rec_sets_defs,
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   473
     mono = mono,
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   474
     unfold = unfold,
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   475
     intrs = intrs,
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   476
     elims = elims,
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   477
     mk_cases = mk_cases elims,
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   478
     raw_induct = raw_induct,
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   479
     induct = induct})
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   480
  end;
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   481
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   482
(***************** axiomatic introduction of (co)inductive sets ***************)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   483
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   484
fun add_ind_axm verbose declare_consts alt_name coind no_elim no_ind cs
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   485
    intr_ts monos con_defs thy params paramTs cTs cnames =
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   486
  let
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   487
    val _ = if verbose then writeln ("Adding axioms for " ^
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   488
      (if coind then "co" else "") ^ "inductive set(s) " ^ commas cnames) else ();
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   489
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   490
    val rec_name = if alt_name = "" then space_implode "_" cnames else alt_name;
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   491
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   492
    val elim_ts = mk_elims cs cTs params intr_ts;
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   493
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   494
    val (_, ind_prems, mutual_ind_concl) = mk_indrule cs cTs params intr_ts;
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   495
    val ind_t = Logic.list_implies (ind_prems, mutual_ind_concl);
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   496
    
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   497
    val thy' = thy |>
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   498
      (if declare_consts then
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   499
        Theory.add_consts_i (map (fn (c, n) =>
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   500
          (n, paramTs ---> fastype_of c, NoSyn)) (cs ~~ cnames))
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   501
       else I) |>
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   502
      Theory.add_path rec_name |>
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   503
      PureThy.add_axiomss_i [(("intrs", intr_ts), []), (("elims", elim_ts), [])] |>
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   504
      (if coind then I
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   505
       else PureThy.add_axioms_i [(("internal_induct", ind_t), [])]);
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   506
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   507
    val intrs = get_thms thy' "intrs";
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   508
    val elims = get_thms thy' "elims";
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   509
    val raw_induct = if coind then TrueI else
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   510
      standard (split_rule (get_thm thy' "internal_induct"));
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   511
    val induct = if coind orelse length cs > 1 then raw_induct
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   512
      else standard (raw_induct RSN (2, rev_mp));
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   513
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   514
    val thy'' = thy' |>
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   515
      (if coind then I
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   516
       else PureThy.add_tthms [(("induct", Attribute.tthm_of induct), [])]) |>
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   517
      Theory.parent_path
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   518
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   519
  in (thy'',
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   520
    {defs = [],
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   521
     mono = TrueI,
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   522
     unfold = TrueI,
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   523
     intrs = intrs,
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   524
     elims = elims,
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   525
     mk_cases = mk_cases elims,
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   526
     raw_induct = raw_induct,
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   527
     induct = induct})
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   528
  end;
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   529
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   530
(********************** introduction of (co)inductive sets ********************)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   531
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   532
fun add_inductive_i verbose declare_consts alt_name coind no_elim no_ind cs
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   533
    intr_ts monos con_defs thy =
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   534
  let
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   535
    val _ = Theory.requires thy "Inductive"
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   536
      ((if coind then "co" else "") ^ "inductive definitions");
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   537
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   538
    val sign = sign_of thy;
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   539
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   540
    (*parameters should agree for all mutually recursive components*)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   541
    val (_, params) = strip_comb (hd cs);
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   542
    val paramTs = map (try' (snd o dest_Free) "Parameter in recursive\
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   543
      \ component is not a free variable: " sign) params;
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   544
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   545
    val cTs = map (try' (HOLogic.dest_setT o fastype_of)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   546
      "Recursive component not of type set: " sign) cs;
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   547
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   548
    val cnames = map (try' (Sign.base_name o fst o dest_Const o head_of)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   549
      "Recursive set not previously declared as constant: " sign) cs;
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   550
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   551
    val _ = assert_all Syntax.is_identifier cnames
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   552
       (fn a => "Base name of recursive set not an identifier: " ^ a);
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   553
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   554
    val _ = map (check_rule sign cs) intr_ts;
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   555
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   556
  in
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   557
    (if !quick_and_dirty then add_ind_axm else add_ind_def)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   558
      verbose declare_consts alt_name coind no_elim no_ind cs intr_ts monos
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   559
        con_defs thy params paramTs cTs cnames
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   560
  end;
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   561
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   562
(***************************** external interface *****************************)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   563
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   564
fun add_inductive verbose coind c_strings intr_strings monos con_defs thy =
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   565
  let
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   566
    val sign = sign_of thy;
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   567
    val cs = map (readtm (sign_of thy) HOLogic.termTVar) c_strings;
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   568
    val intr_ts = map (readtm (sign_of thy) propT) intr_strings;
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   569
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   570
    (* the following code ensures that each recursive set *)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   571
    (* always has the same type in all introduction rules *)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   572
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   573
    val {tsig, ...} = Sign.rep_sg sign;
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   574
    val add_term_consts_2 =
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   575
      foldl_aterms (fn (cs, Const c) => c ins cs | (cs, _) => cs);
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   576
    fun varify (t, (i, ts)) =
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   577
      let val t' = map_term_types (incr_tvar (i + 1)) (Type.varify (t, []))
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   578
      in (maxidx_of_term t', t'::ts) end;
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   579
    val (i, cs') = foldr varify (cs, (~1, []));
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   580
    val (i', intr_ts') = foldr varify (intr_ts, (i, []));
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   581
    val rec_consts = foldl add_term_consts_2 ([], cs');
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   582
    val intr_consts = foldl add_term_consts_2 ([], intr_ts');
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   583
    fun unify (env, (cname, cT)) =
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   584
      let val consts = map snd (filter (fn c => fst c = cname) intr_consts)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   585
      in (foldl (fn ((env', j'), Tp) => Type.unify tsig j' env' Tp)
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   586
        (env, (replicate (length consts) cT) ~~ consts)) handle _ =>
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   587
          error ("Occurrences of constant '" ^ cname ^
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   588
            "' have incompatible types")
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   589
      end;
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   590
    val (env, _) = foldl unify (([], i'), rec_consts);
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   591
    fun typ_subst_TVars_2 env T = let val T' = typ_subst_TVars env T
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   592
      in if T = T' then T else typ_subst_TVars_2 env T' end;
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   593
    val subst = fst o Type.freeze_thaw o
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   594
      (map_term_types (typ_subst_TVars_2 env));
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   595
    val cs'' = map subst cs';
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   596
    val intr_ts'' = map subst intr_ts';
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   597
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   598
  in add_inductive_i verbose false "" coind false false cs'' intr_ts''
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   599
    monos con_defs thy
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   600
  end;
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   601
ddcc3c114a0e New inductive definition package
berghofe
parents:
diff changeset
   602
end;