src/HOL/Tools/datatype_abs_proofs.ML
author berghofe
Fri, 16 Oct 1998 18:54:55 +0200
changeset 5661 6ecb6ea25f19
parent 5578 7de426cf179c
child 5891 92e0f5e6fd17
permissions -rw-r--r--
- Changed structure of name spaces - Proofs for datatypes with unneeded parameters are working now - added additional parameter flat_names - added quiet_mode flag
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
     1
(*  Title:      HOL/Tools/datatype_abs_proofs.ML
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
     2
    ID:         $Id$
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
     3
    Author:     Stefan Berghofer
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
     4
    Copyright   1998  TU Muenchen
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
     5
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
     6
Proofs and defintions independent of concrete representation
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
     7
of datatypes  (i.e. requiring only abstract properties such as
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
     8
injectivity / distinctness of constructors and induction)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
     9
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    10
 - case distinction (exhaustion) theorems
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    11
 - characteristic equations for primrec combinators
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    12
 - characteristic equations for case combinators
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    13
 - distinctness of constructors (external version)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    14
 - equations for splitting "P (case ...)" expressions
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    15
 - datatype size function
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    16
 - "nchotomy" and "case_cong" theorems for TFL
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    17
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    18
*)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    19
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    20
signature DATATYPE_ABS_PROOFS =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    21
sig
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    22
  val prove_casedist_thms : string list -> (int * (string * DatatypeAux.dtyp list *
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    23
    (string * DatatypeAux.dtyp list) list)) list list -> (string * sort) list ->
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    24
      thm -> theory -> theory * thm list
5661
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5578
diff changeset
    25
  val prove_primrec_thms : bool -> string list -> (int * (string * DatatypeAux.dtyp list *
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    26
    (string * DatatypeAux.dtyp list) list)) list list -> (string * sort) list ->
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    27
      DatatypeAux.datatype_info Symtab.table -> thm list list -> thm list list ->
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    28
        thm -> theory -> theory * string list * thm list
5661
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5578
diff changeset
    29
  val prove_case_thms : bool -> string list -> (int * (string * DatatypeAux.dtyp list *
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    30
    (string * DatatypeAux.dtyp list) list)) list list -> (string * sort) list ->
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    31
      string list -> thm list -> theory -> theory * string list * thm list list
5661
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5578
diff changeset
    32
  val prove_distinctness_thms : bool -> string list -> (int * (string * DatatypeAux.dtyp list *
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    33
    (string * DatatypeAux.dtyp list) list)) list list -> (string * sort) list ->
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    34
      thm list list -> thm list list -> theory -> theory * thm list list
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    35
  val prove_split_thms : string list -> (int * (string * DatatypeAux.dtyp list *
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    36
    (string * DatatypeAux.dtyp list) list)) list list -> (string * sort) list ->
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    37
      thm list list -> thm list list -> thm list -> thm list list -> theory ->
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    38
        theory * (thm * thm) list
5661
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5578
diff changeset
    39
  val prove_size_thms : bool -> string list -> (int * (string * DatatypeAux.dtyp list *
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    40
    (string * DatatypeAux.dtyp list) list)) list list -> (string * sort) list ->
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    41
      string list -> thm list -> theory -> theory * thm list
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    42
  val prove_nchotomys : string list -> (int * (string * DatatypeAux.dtyp list *
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    43
    (string * DatatypeAux.dtyp list) list)) list list -> (string * sort) list ->
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    44
      thm list -> theory -> theory * thm list
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    45
  val prove_case_congs : string list -> (int * (string * DatatypeAux.dtyp list *
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    46
    (string * DatatypeAux.dtyp list) list)) list list -> (string * sort) list ->
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    47
      thm list -> thm list list -> theory -> theory * thm list
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    48
end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    49
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    50
structure DatatypeAbsProofs : DATATYPE_ABS_PROOFS =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    51
struct
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    52
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    53
open DatatypeAux;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    54
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    55
val thin = read_instantiate_sg (sign_of Set.thy) [("V", "?X : ?Y")] thin_rl;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    56
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    57
val (_ $ (_ $ (_ $ (distinct_f $ _) $ _))) = hd (prems_of distinct_lemma);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    58
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    59
(************************ case distinction theorems ***************************)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    60
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    61
fun prove_casedist_thms new_type_names descr sorts induct thy =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    62
  let
5661
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5578
diff changeset
    63
    val _ = message "Proving case distinction theorems...";
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    64
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    65
    val descr' = flat descr;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    66
    val recTs = get_rec_types descr' sorts;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    67
    val newTs = take (length (hd descr), recTs);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    68
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    69
    val induct_Ps = map head_of (dest_conj (HOLogic.dest_Trueprop (concl_of induct)));
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    70
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    71
    fun prove_casedist_thm ((i, t), T) =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    72
      let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    73
        val dummyPs = map (fn (Var (_, Type (_, [T', T'']))) =>
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    74
          Abs ("z", T', Const ("True", T''))) induct_Ps;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    75
        val P = Abs ("z", T, HOLogic.imp $ HOLogic.mk_eq (Var (("a", 0), T), Bound 0) $
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    76
          Var (("P", 0), HOLogic.boolT))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    77
        val insts = take (i, dummyPs) @ (P::(drop (i + 1, dummyPs)));
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    78
        val cert = cterm_of (sign_of thy);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    79
        val insts' = (map cert induct_Ps) ~~ (map cert insts);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    80
        val induct' = refl RS ((nth_elem (i,
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    81
          split_conj_thm (cterm_instantiate insts' induct))) RSN (2, rev_mp))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    82
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    83
      in prove_goalw_cterm [] (cert t) (fn prems =>
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    84
        [rtac induct' 1,
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    85
         REPEAT (rtac TrueI 1),
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    86
         REPEAT ((rtac impI 1) THEN (eresolve_tac prems 1)),
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    87
         REPEAT (rtac TrueI 1)])
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    88
      end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    89
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    90
    val casedist_thms = map prove_casedist_thm ((0 upto (length newTs - 1)) ~~
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    91
      (DatatypeProp.make_casedists descr sorts) ~~ newTs)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    92
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    93
  in
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    94
    (store_thms "exhaust" new_type_names casedist_thms thy, casedist_thms)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    95
  end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    96
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    97
(*************************** primrec combinators ******************************)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    98
5661
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5578
diff changeset
    99
fun prove_primrec_thms flat_names new_type_names descr sorts
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   100
    (dt_info : datatype_info Symtab.table) constr_inject dist_rewrites induct thy =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   101
  let
5661
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5578
diff changeset
   102
    val _ = message "Constructing primrec combinators...";
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5578
diff changeset
   103
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5578
diff changeset
   104
    val big_name = space_implode "_" new_type_names;
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5578
diff changeset
   105
    val thy0 = add_path flat_names big_name thy;
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   106
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   107
    val descr' = flat descr;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   108
    val recTs = get_rec_types descr' sorts;
5578
7de426cf179c Package now chooses type variable names more carefully to
berghofe
parents: 5553
diff changeset
   109
    val used = foldr add_typ_tfree_names (recTs, []);
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   110
    val newTs = take (length (hd descr), recTs);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   111
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   112
    val induct_Ps = map head_of (dest_conj (HOLogic.dest_Trueprop (concl_of induct)));
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   113
5661
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5578
diff changeset
   114
    val big_rec_name' = big_name ^ "_rec_set";
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5578
diff changeset
   115
    val rec_set_names = map (Sign.full_name (sign_of thy0))
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   116
      (if length descr' = 1 then [big_rec_name'] else
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   117
        (map ((curry (op ^) (big_rec_name' ^ "_")) o string_of_int)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   118
          (1 upto (length descr'))));
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   119
5578
7de426cf179c Package now chooses type variable names more carefully to
berghofe
parents: 5553
diff changeset
   120
    val rec_result_Ts = map TFree (variantlist (replicate (length descr') "'t", used) ~~
7de426cf179c Package now chooses type variable names more carefully to
berghofe
parents: 5553
diff changeset
   121
      replicate (length descr') HOLogic.termS);
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   122
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   123
    val reccomb_fn_Ts = flat (map (fn (i, (_, _, constrs)) =>
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   124
      map (fn (_, cargs) =>
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   125
        let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   126
          val recs = filter is_rec_type cargs;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   127
          val argTs = (map (typ_of_dtyp descr' sorts) cargs) @
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   128
            (map (fn r => nth_elem (dest_DtRec r, rec_result_Ts)) recs)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   129
        in argTs ---> nth_elem (i, rec_result_Ts)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   130
        end) constrs) descr');
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   131
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   132
    val rec_set_Ts = map (fn (T1, T2) => reccomb_fn_Ts ---> HOLogic.mk_setT
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   133
      (HOLogic.mk_prodT (T1, T2))) (recTs ~~ rec_result_Ts);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   134
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   135
    val rec_fns = map (uncurry (mk_Free "f"))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   136
      (reccomb_fn_Ts ~~ (1 upto (length reccomb_fn_Ts)));
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   137
    val rec_sets = map (fn c => list_comb (Const c, rec_fns))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   138
      (rec_set_names ~~ rec_set_Ts);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   139
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   140
    (* introduction rules for graph of primrec function *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   141
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   142
    fun make_rec_intr T set_name ((rec_intr_ts, l), (cname, cargs)) =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   143
      let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   144
        fun mk_prem (dt, (j, k, prems, t1s, t2s)) =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   145
          let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   146
            val T = typ_of_dtyp descr' sorts dt;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   147
            val free1 = mk_Free "x" T j
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   148
          in (case dt of
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   149
             DtRec m =>
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   150
               let val free2 = mk_Free "y" (nth_elem (m, rec_result_Ts)) k
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   151
               in (j + 1, k + 1, (HOLogic.mk_Trueprop (HOLogic.mk_mem
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   152
                 (HOLogic.mk_prod (free1, free2), nth_elem (m, rec_sets))))::prems,
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   153
                   free1::t1s, free2::t2s)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   154
               end
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   155
           | _ => (j + 1, k, prems, free1::t1s, t2s))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   156
          end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   157
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   158
        val Ts = map (typ_of_dtyp descr' sorts) cargs;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   159
        val (_, _, prems, t1s, t2s) = foldr mk_prem (cargs, (1, 1, [], [], []))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   160
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   161
      in (rec_intr_ts @ [Logic.list_implies (prems, HOLogic.mk_Trueprop (HOLogic.mk_mem
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   162
        (HOLogic.mk_prod (list_comb (Const (cname, Ts ---> T), t1s),
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   163
          list_comb (nth_elem (l, rec_fns), t1s @ t2s)), set_name)))], l + 1)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   164
      end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   165
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   166
    val (rec_intr_ts, _) = foldl (fn (x, ((d, T), set_name)) =>
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   167
      foldl (make_rec_intr T set_name) (x, #3 (snd d)))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   168
        (([], 0), descr' ~~ recTs ~~ rec_sets);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   169
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   170
    val (thy1, {intrs = rec_intrs, elims = rec_elims, ...}) =
5661
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5578
diff changeset
   171
      setmp InductivePackage.quiet_mode (!quiet_mode)
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5578
diff changeset
   172
        (InductivePackage.add_inductive_i false true big_rec_name' false false true
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5578
diff changeset
   173
           rec_sets rec_intr_ts [] []) thy0;
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   174
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   175
    (* prove uniqueness and termination of primrec combinators *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   176
5661
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5578
diff changeset
   177
    val _ = message "Proving termination and uniqueness of primrec functions...";
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   178
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   179
    fun mk_unique_tac ((tac, intrs), ((((i, (tname, _, constrs)), elim), T), T')) =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   180
      let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   181
        val distinct_tac = (etac Pair_inject 1) THEN
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   182
          (if i < length newTs then
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   183
             full_simp_tac (HOL_ss addsimps (nth_elem (i, dist_rewrites))) 1
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   184
           else full_simp_tac (HOL_ss addsimps
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   185
             ((#distinct (the (Symtab.lookup (dt_info, tname)))) @
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   186
               [Suc_Suc_eq, Suc_not_Zero, Zero_not_Suc])) 1);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   187
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   188
        val inject = map (fn r => r RS iffD1)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   189
          (if i < length newTs then nth_elem (i, constr_inject)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   190
            else #inject (the (Symtab.lookup (dt_info, tname))));
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   191
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   192
        fun mk_unique_constr_tac n ((tac, intr::intrs, j), (cname, cargs)) =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   193
          let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   194
            val k = length (filter is_rec_type cargs)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   195
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   196
          in (EVERY [DETERM tac,
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   197
                REPEAT (etac ex1E 1), rtac ex1I 1,
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   198
                DEPTH_SOLVE_1 (ares_tac [intr] 1),
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   199
                REPEAT_DETERM_N k (etac thin 1),
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   200
                etac elim 1,
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   201
                REPEAT_DETERM_N j distinct_tac,
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   202
                etac Pair_inject 1, TRY (dresolve_tac inject 1),
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   203
                REPEAT (etac conjE 1), hyp_subst_tac 1,
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   204
                REPEAT (etac allE 1),
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   205
                REPEAT (dtac mp 1 THEN atac 1),
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   206
                TRY (hyp_subst_tac 1),
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   207
                rtac refl 1,
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   208
                REPEAT_DETERM_N (n - j - 1) distinct_tac],
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   209
              intrs, j + 1)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   210
          end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   211
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   212
        val (tac', intrs', _) = foldl (mk_unique_constr_tac (length constrs))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   213
          ((tac, intrs, 0), constrs);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   214
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   215
      in (tac', intrs') end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   216
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   217
    val rec_unique_thms =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   218
      let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   219
        val rec_unique_ts = map (fn (((set_t, T1), T2), i) =>
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   220
          Const ("Ex1", (T2 --> HOLogic.boolT) --> HOLogic.boolT) $
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   221
            absfree ("y", T2, HOLogic.mk_mem (HOLogic.mk_prod
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   222
              (mk_Free "x" T1 i, Free ("y", T2)), set_t)))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   223
                (rec_sets ~~ recTs ~~ rec_result_Ts ~~ (1 upto length recTs));
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   224
        val cert = cterm_of (sign_of thy1)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   225
        val insts = map (fn ((i, T), t) => absfree ("x" ^ (string_of_int i), T, t))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   226
          ((1 upto length recTs) ~~ recTs ~~ rec_unique_ts);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   227
        val induct' = cterm_instantiate ((map cert induct_Ps) ~~
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   228
          (map cert insts)) induct;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   229
        val (tac, _) = foldl mk_unique_tac
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   230
          ((rtac induct' 1, rec_intrs), descr' ~~ rec_elims ~~ recTs ~~ rec_result_Ts)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   231
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   232
      in split_conj_thm (prove_goalw_cterm []
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   233
        (cert (HOLogic.mk_Trueprop (mk_conj rec_unique_ts))) (K [tac]))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   234
      end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   235
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   236
    val rec_total_thms = map (fn r =>
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   237
      r RS ex1_implies_ex RS (select_eq_Ex RS iffD2)) rec_unique_thms;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   238
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   239
    (* define primrec combinators *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   240
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   241
    val big_reccomb_name = (space_implode "_" new_type_names) ^ "_rec";
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   242
    val reccomb_names = map (Sign.full_name (sign_of thy1))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   243
      (if length descr' = 1 then [big_reccomb_name] else
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   244
        (map ((curry (op ^) (big_reccomb_name ^ "_")) o string_of_int)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   245
          (1 upto (length descr'))));
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   246
    val reccombs = map (fn ((name, T), T') => list_comb
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   247
      (Const (name, reccomb_fn_Ts @ [T] ---> T'), rec_fns))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   248
        (reccomb_names ~~ recTs ~~ rec_result_Ts);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   249
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   250
    val thy2 = thy1 |>
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   251
      Theory.add_consts_i (map (fn ((name, T), T') =>
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   252
        (Sign.base_name name, reccomb_fn_Ts @ [T] ---> T', NoSyn))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   253
          (reccomb_names ~~ recTs ~~ rec_result_Ts)) |>
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   254
      Theory.add_defs_i (map (fn ((((name, comb), set), T), T') =>
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   255
        ((Sign.base_name name) ^ "_def", Logic.mk_equals
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   256
          (comb $ Free ("x", T),
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   257
           Const ("Eps", (T' --> HOLogic.boolT) --> T') $ absfree ("y", T',
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   258
             HOLogic.mk_mem (HOLogic.mk_prod (Free ("x", T), Free ("y", T')), set)))))
5661
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5578
diff changeset
   259
               (reccomb_names ~~ reccombs ~~ rec_sets ~~ recTs ~~ rec_result_Ts)) |>
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5578
diff changeset
   260
      parent_path flat_names;
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   261
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   262
    val reccomb_defs = map ((get_def thy2) o Sign.base_name) reccomb_names;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   263
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   264
    (* prove characteristic equations for primrec combinators *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   265
5661
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5578
diff changeset
   266
    val _ = message "Proving characteristic theorems for primrec combinators..."
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   267
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   268
    val rec_thms = map (fn t => prove_goalw_cterm reccomb_defs
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   269
      (cterm_of (sign_of thy2) t) (fn _ =>
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   270
        [rtac select1_equality 1,
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   271
         resolve_tac rec_unique_thms 1,
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   272
         resolve_tac rec_intrs 1,
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   273
         REPEAT (resolve_tac rec_total_thms 1)]))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   274
           (DatatypeProp.make_primrecs new_type_names descr sorts thy2)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   275
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   276
  in
5661
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5578
diff changeset
   277
    (thy2 |> Theory.add_path (space_implode "_" new_type_names) |>
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5578
diff changeset
   278
             PureThy.add_tthmss [(("recs", map Attribute.tthm_of rec_thms), [])] |>
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5578
diff changeset
   279
             Theory.parent_path,
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   280
     reccomb_names, rec_thms)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   281
  end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   282
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   283
(***************************** case combinators *******************************)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   284
5661
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5578
diff changeset
   285
fun prove_case_thms flat_names new_type_names descr sorts reccomb_names primrec_thms thy =
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   286
  let
5661
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5578
diff changeset
   287
    val _ = message "Proving characteristic theorems for case combinators...";
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5578
diff changeset
   288
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5578
diff changeset
   289
    val thy1 = add_path flat_names (space_implode "_" new_type_names) thy;
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   290
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   291
    val descr' = flat descr;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   292
    val recTs = get_rec_types descr' sorts;
5578
7de426cf179c Package now chooses type variable names more carefully to
berghofe
parents: 5553
diff changeset
   293
    val used = foldr add_typ_tfree_names (recTs, []);
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   294
    val newTs = take (length (hd descr), recTs);
5578
7de426cf179c Package now chooses type variable names more carefully to
berghofe
parents: 5553
diff changeset
   295
    val T' = TFree (variant used "'t", HOLogic.termS);
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   296
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   297
    val case_dummy_fns = map (fn (_, (_, _, constrs)) => map (fn (_, cargs) =>
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   298
      let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   299
        val Ts = map (typ_of_dtyp descr' sorts) cargs;
5578
7de426cf179c Package now chooses type variable names more carefully to
berghofe
parents: 5553
diff changeset
   300
        val Ts' = replicate (length (filter is_rec_type cargs)) T'
7de426cf179c Package now chooses type variable names more carefully to
berghofe
parents: 5553
diff changeset
   301
      in Const ("arbitrary", Ts @ Ts' ---> T')
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   302
      end) constrs) descr';
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   303
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   304
    val case_names = map (fn s =>
5661
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5578
diff changeset
   305
      Sign.full_name (sign_of thy1) (s ^ "_case")) new_type_names;
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   306
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   307
    (* define case combinators via primrec combinators *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   308
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   309
    val (case_defs, thy2) = foldl (fn ((defs, thy),
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   310
      ((((i, (_, _, constrs)), T), name), recname)) =>
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   311
        let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   312
          val (fns1, fns2) = ListPair.unzip (map (fn ((_, cargs), j) =>
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   313
            let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   314
              val Ts = map (typ_of_dtyp descr' sorts) cargs;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   315
              val Ts' = Ts @ (replicate (length (filter is_rec_type cargs)) T');
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   316
              val frees' = map (uncurry (mk_Free "x")) (Ts' ~~ (1 upto length Ts'));
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   317
              val frees = take (length cargs, frees');
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   318
              val free = mk_Free "f" (Ts ---> T') j
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   319
            in
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   320
             (free, list_abs_free (map dest_Free frees',
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   321
               list_comb (free, frees)))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   322
            end) (constrs ~~ (1 upto length constrs)));
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   323
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   324
          val caseT = (map (snd o dest_Free) fns1) @ [T] ---> T';
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   325
          val fns = (flat (take (i, case_dummy_fns))) @
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   326
            fns2 @ (flat (drop (i + 1, case_dummy_fns)));
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   327
          val reccomb = Const (recname, (map fastype_of fns) @ [T] ---> T');
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   328
          val decl = (Sign.base_name name, caseT, NoSyn);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   329
          val def = ((Sign.base_name name) ^ "_def",
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   330
            Logic.mk_equals (list_comb (Const (name, caseT), fns1),
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   331
              list_comb (reccomb, (flat (take (i, case_dummy_fns))) @
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   332
                fns2 @ (flat (drop (i + 1, case_dummy_fns))) )));
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   333
          val thy' = thy |>
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   334
            Theory.add_consts_i [decl] |> Theory.add_defs_i [def];
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   335
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   336
        in (defs @ [get_def thy' (Sign.base_name name)], thy')
5661
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5578
diff changeset
   337
        end) (([], thy1), (hd descr) ~~ newTs ~~ case_names ~~
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   338
          (take (length newTs, reccomb_names)));
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   339
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   340
    val case_thms = map (map (fn t => prove_goalw_cterm (case_defs @
5553
ae42b36a50c2 renamed mk_meta_eq to mk_eq
oheimb
parents: 5303
diff changeset
   341
      (map mk_meta_eq primrec_thms)) (cterm_of (sign_of thy2) t)
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   342
        (fn _ => [rtac refl 1])))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   343
          (DatatypeProp.make_cases new_type_names descr sorts thy2);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   344
5661
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5578
diff changeset
   345
    val thy3 = thy2 |> Theory.add_trrules_i
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5578
diff changeset
   346
      (DatatypeProp.make_case_trrules new_type_names descr) |>
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5578
diff changeset
   347
      parent_path flat_names;
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   348
5661
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5578
diff changeset
   349
  in
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5578
diff changeset
   350
    (store_thmss "cases" new_type_names case_thms thy3, case_names, case_thms)
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   351
  end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   352
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   353
(************************ distinctness of constructors ************************)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   354
5661
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5578
diff changeset
   355
fun prove_distinctness_thms flat_names new_type_names descr sorts dist_rewrites case_thms thy =
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   356
  let
5661
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5578
diff changeset
   357
    val thy1 = add_path flat_names (space_implode "_" new_type_names) thy;
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5578
diff changeset
   358
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   359
    val descr' = flat descr;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   360
    val recTs = get_rec_types descr' sorts;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   361
    val newTs = take (length (hd descr), recTs);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   362
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   363
    (*--------------------------------------------------------------------*)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   364
    (* define t_ord - functions for proving distinctness of constructors: *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   365
    (*  t_ord C_i ... = i                                                 *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   366
    (*--------------------------------------------------------------------*)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   367
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   368
    fun define_ord ((thy, ord_defs), (((_, (_, _, constrs)), T), tname)) =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   369
      if length constrs < DatatypeProp.dtK then (thy, ord_defs)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   370
      else
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   371
        let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   372
          val Tss = map ((map (typ_of_dtyp descr' sorts)) o snd) constrs;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   373
          val ts = map HOLogic.mk_nat (0 upto length constrs - 1);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   374
          val mk_abs = foldr (fn (T, t') => Abs ("x", T, t'));
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   375
          val fs = map mk_abs (Tss ~~ ts);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   376
          val fTs = map (fn Ts => Ts ---> HOLogic.natT) Tss;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   377
          val ord_name = Sign.full_name (sign_of thy) (tname ^ "_ord");
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   378
          val case_name = Sign.intern_const (sign_of thy) (tname ^ "_case");
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   379
          val ordT = T --> HOLogic.natT;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   380
          val caseT = fTs ---> ordT;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   381
          val defpair = (tname ^ "_ord_def", Logic.mk_equals
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   382
            (Const (ord_name, ordT), list_comb (Const (case_name, caseT), fs)));
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   383
          val thy' = thy |>
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   384
            Theory.add_consts_i [(tname ^ "_ord", ordT, NoSyn)] |>
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   385
            Theory.add_defs_i [defpair];
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   386
          val def = get_def thy' (tname ^ "_ord")
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   387
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   388
        in (thy', ord_defs @ [def]) end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   389
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   390
    val (thy2, ord_defs) =
5661
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5578
diff changeset
   391
      foldl define_ord ((thy1, []), (hd descr) ~~ newTs ~~ new_type_names);
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   392
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   393
    (**** number of constructors < dtK ****)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   394
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   395
    fun prove_distinct_thms _ [] = []
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   396
      | prove_distinct_thms dist_rewrites' (t::_::ts) =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   397
          let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   398
            val dist_thm = prove_goalw_cterm [] (cterm_of (sign_of thy2) t) (fn _ =>
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   399
              [simp_tac (HOL_ss addsimps dist_rewrites') 1])
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   400
          in dist_thm::(standard (dist_thm RS not_sym))::
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   401
            (prove_distinct_thms dist_rewrites' ts)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   402
          end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   403
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   404
    val distinct_thms = map (fn ((((_, (_, _, constrs)), ts),
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   405
      dist_rewrites'), case_thms) =>
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   406
        if length constrs < DatatypeProp.dtK then
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   407
          prove_distinct_thms dist_rewrites' ts
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   408
        else 
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   409
          let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   410
            val t::ts' = rev ts;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   411
            val (_ $ (_ $ (_ $ (f $ _) $ _))) = hd (Logic.strip_imp_prems t);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   412
            val cert = cterm_of (sign_of thy2);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   413
            val distinct_lemma' = cterm_instantiate
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   414
              [(cert distinct_f, cert f)] distinct_lemma;
5553
ae42b36a50c2 renamed mk_meta_eq to mk_eq
oheimb
parents: 5303
diff changeset
   415
            val rewrites = ord_defs @ (map mk_meta_eq case_thms)
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   416
          in
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   417
            (map (fn t => prove_goalw_cterm rewrites (cert t)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   418
              (fn _ => [rtac refl 1])) (rev ts')) @ [standard distinct_lemma']
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   419
          end) ((hd descr) ~~ (DatatypeProp.make_distincts new_type_names
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   420
            descr sorts thy2) ~~ dist_rewrites ~~ case_thms)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   421
5661
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5578
diff changeset
   422
  in
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5578
diff changeset
   423
    (thy2 |> parent_path flat_names |>
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5578
diff changeset
   424
             store_thmss "distinct" new_type_names distinct_thms,
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5578
diff changeset
   425
     distinct_thms)
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   426
  end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   427
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   428
(******************************* case splitting *******************************)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   429
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   430
fun prove_split_thms new_type_names descr sorts constr_inject dist_rewrites
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   431
    casedist_thms case_thms thy =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   432
  let
5661
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5578
diff changeset
   433
    val _ = message "Proving equations for case splitting...";
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   434
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   435
    val descr' = flat descr;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   436
    val recTs = get_rec_types descr' sorts;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   437
    val newTs = take (length (hd descr), recTs);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   438
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   439
    fun prove_split_thms ((((((t1, t2), inject), dist_rewrites'),
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   440
        exhaustion), case_thms'), T) =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   441
      let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   442
        val cert = cterm_of (sign_of thy);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   443
        val _ $ (_ $ lhs $ _) = hd (Logic.strip_assums_hyp (hd (prems_of exhaustion)));
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   444
        val exhaustion' = cterm_instantiate
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   445
          [(cert lhs, cert (Free ("x", T)))] exhaustion;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   446
        val tacsf = K [rtac exhaustion' 1, ALLGOALS (asm_simp_tac
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   447
          (HOL_ss addsimps (dist_rewrites' @ inject @ case_thms')))]
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   448
      in
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   449
        (prove_goalw_cterm [] (cert t1) tacsf,
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   450
         prove_goalw_cterm [] (cert t2) tacsf)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   451
      end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   452
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   453
    val split_thm_pairs = map prove_split_thms
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   454
      ((DatatypeProp.make_splits new_type_names descr sorts thy) ~~ constr_inject ~~
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   455
        dist_rewrites ~~ casedist_thms ~~ case_thms ~~ newTs);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   456
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   457
    val (split_thms, split_asm_thms) = ListPair.unzip split_thm_pairs
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   458
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   459
  in
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   460
    (thy |> store_thms "split" new_type_names split_thms |>
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   461
            store_thms "split_asm" new_type_names split_asm_thms,
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   462
     split_thm_pairs)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   463
  end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   464
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   465
(******************************* size functions *******************************)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   466
5661
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5578
diff changeset
   467
fun prove_size_thms flat_names new_type_names descr sorts reccomb_names primrec_thms thy =
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   468
  let
5661
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5578
diff changeset
   469
    val _ = message "Proving equations for size function...";
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5578
diff changeset
   470
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5578
diff changeset
   471
    val big_name = space_implode "_" new_type_names;
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5578
diff changeset
   472
    val thy1 = add_path flat_names big_name thy;
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   473
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   474
    val descr' = flat descr;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   475
    val recTs = get_rec_types descr' sorts;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   476
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   477
    val big_size_name = space_implode "_" new_type_names ^ "_size";
5661
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5578
diff changeset
   478
    val size_name = Sign.intern_const (sign_of (the (get_thy "Arith" thy1))) "size";
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   479
    val size_names = replicate (length (hd descr)) size_name @
5661
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5578
diff changeset
   480
      map (Sign.full_name (sign_of thy1))
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   481
        (if length (flat (tl descr)) = 1 then [big_size_name] else
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   482
          map (fn i => big_size_name ^ "_" ^ string_of_int i)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   483
            (1 upto length (flat (tl descr))));
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   484
    val def_names = map (fn i => big_size_name ^ "_def_" ^ string_of_int i)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   485
      (1 upto length recTs);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   486
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   487
    val plus_t = Const ("op +", [HOLogic.natT, HOLogic.natT] ---> HOLogic.natT);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   488
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   489
    fun make_sizefun (_, cargs) =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   490
      let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   491
        val Ts = map (typ_of_dtyp descr' sorts) cargs;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   492
        val k = length (filter is_rec_type cargs);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   493
        val t = if k = 0 then HOLogic.zero else
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   494
          foldl1 (app plus_t) (map Bound (k - 1 downto 0) @ [HOLogic.mk_nat 1])
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   495
      in
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   496
        foldr (fn (T, t') => Abs ("x", T, t')) (Ts @ replicate k HOLogic.natT, t)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   497
      end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   498
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   499
    val fs = flat (map (fn (_, (_, _, constrs)) => map make_sizefun constrs) descr');
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   500
    val fTs = map fastype_of fs;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   501
5661
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5578
diff changeset
   502
    val thy' = thy1 |>
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   503
      Theory.add_consts_i (map (fn (s, T) =>
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   504
        (Sign.base_name s, T --> HOLogic.natT, NoSyn))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   505
          (drop (length (hd descr), size_names ~~ recTs))) |>
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   506
      Theory.add_defs_i (map (fn (((s, T), def_name), rec_name) =>
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   507
        (def_name, Logic.mk_equals (Const (s, T --> HOLogic.natT),
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   508
          list_comb (Const (rec_name, fTs @ [T] ---> HOLogic.natT), fs))))
5661
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5578
diff changeset
   509
            (size_names ~~ recTs ~~ def_names ~~ reccomb_names)) |>
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5578
diff changeset
   510
      parent_path flat_names;
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   511
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   512
    val size_def_thms = map (get_axiom thy') def_names;
5553
ae42b36a50c2 renamed mk_meta_eq to mk_eq
oheimb
parents: 5303
diff changeset
   513
    val rewrites = size_def_thms @ map mk_meta_eq primrec_thms;
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   514
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   515
    val size_thms = map (fn t => prove_goalw_cterm rewrites
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   516
      (cterm_of (sign_of thy') t) (fn _ => [rtac refl 1]))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   517
        (DatatypeProp.make_size new_type_names descr sorts thy')
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   518
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   519
  in
5661
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5578
diff changeset
   520
    (thy' |> Theory.add_path big_name |>
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5578
diff changeset
   521
             PureThy.add_tthmss [(("size", map Attribute.tthm_of size_thms), [])] |>
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5578
diff changeset
   522
             Theory.parent_path,
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   523
     size_thms)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   524
  end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   525
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   526
(************************* additional theorems for TFL ************************)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   527
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   528
fun prove_nchotomys new_type_names descr sorts casedist_thms thy =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   529
  let
5661
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5578
diff changeset
   530
    val _ = message "Proving additional theorems for TFL...";
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   531
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   532
    fun prove_nchotomy (t, exhaustion) =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   533
      let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   534
        (* For goal i, select the correct disjunct to attack, then prove it *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   535
        fun tac i 0 = EVERY [TRY (rtac disjI1 i),
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   536
              hyp_subst_tac i, REPEAT (rtac exI i), rtac refl i]
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   537
          | tac i n = rtac disjI2 i THEN tac i (n - 1)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   538
      in 
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   539
        prove_goalw_cterm [] (cterm_of (sign_of thy) t) (fn _ =>
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   540
          [rtac allI 1,
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   541
           exh_tac (K exhaustion) 1,
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   542
           ALLGOALS (fn i => tac i (i-1))])
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   543
      end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   544
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   545
    val nchotomys =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   546
      map prove_nchotomy (DatatypeProp.make_nchotomys descr sorts ~~ casedist_thms)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   547
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   548
  in
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   549
    (store_thms "nchotomy" new_type_names nchotomys thy, nchotomys)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   550
  end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   551
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   552
fun prove_case_congs new_type_names descr sorts nchotomys case_thms thy =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   553
  let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   554
    fun prove_case_cong ((t, nchotomy), case_rewrites) =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   555
      let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   556
        val (Const ("==>", _) $ tm $ _) = t;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   557
        val (Const ("Trueprop", _) $ (Const ("op =", _) $ _ $ Ma)) = tm;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   558
        val cert = cterm_of (sign_of thy);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   559
        val nchotomy' = nchotomy RS spec;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   560
        val nchotomy'' = cterm_instantiate
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   561
          [(cert (hd (add_term_vars (concl_of nchotomy', []))), cert Ma)] nchotomy'
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   562
      in
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   563
        prove_goalw_cterm [] (cert t) (fn prems => 
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   564
          let val simplify = asm_simp_tac (HOL_ss addsimps (prems @ case_rewrites))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   565
          in [simp_tac (HOL_ss addsimps [hd prems]) 1,
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   566
              cut_facts_tac [nchotomy''] 1,
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   567
              REPEAT (etac disjE 1 THEN REPEAT (etac exE 1) THEN simplify 1),
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   568
              REPEAT (etac exE 1) THEN simplify 1 (* Get last disjunct *)]
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   569
          end)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   570
      end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   571
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   572
    val case_congs = map prove_case_cong (DatatypeProp.make_case_congs
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   573
      new_type_names descr sorts thy ~~ nchotomys ~~ case_thms)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   574
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   575
  in
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   576
    (store_thms "case_cong" new_type_names case_congs thy, case_congs)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   577
  end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   578
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   579
end;