src/HOL/Tools/datatype_prop.ML
author haftmann
Thu, 17 May 2007 19:49:17 +0200
changeset 22994 02440636214f
parent 22578 b0eb5652f210
child 24098 f1eb34ae33af
permissions -rw-r--r--
abstract size function in hologic.ML
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_prop.ML
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
     2
    ID:         $Id$
11539
0f17da240450 tuned headers;
wenzelm
parents: 10911
diff changeset
     3
    Author:     Stefan Berghofer, TU Muenchen
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
     4
11539
0f17da240450 tuned headers;
wenzelm
parents: 10911
diff changeset
     5
Characteristic properties of datatypes.
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
     6
*)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
     7
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
     8
signature DATATYPE_PROP =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
     9
sig
7015
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
    10
  val dtK : int ref
8434
5e4bba59bfaa use HOLogic.Not;
wenzelm
parents: 7704
diff changeset
    11
  val indexify_names: string list -> string list
13465
08e3fe248ba9 Exported function make_tnames.
berghofe
parents: 13340
diff changeset
    12
  val make_tnames: typ list -> string list
13641
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 13585
diff changeset
    13
  val make_injs : DatatypeAux.descr list -> (string * sort) list -> term list list
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 13585
diff changeset
    14
  val make_ind : DatatypeAux.descr list -> (string * sort) list -> term
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 13585
diff changeset
    15
  val make_casedists : DatatypeAux.descr list -> (string * sort) list -> term list
15459
16dd63c78049 Introduced function DatatypeProp.make_primrec_Ts to avoid code duplication.
berghofe
parents: 14981
diff changeset
    16
  val make_primrec_Ts : DatatypeAux.descr list -> (string * sort) list ->
16dd63c78049 Introduced function DatatypeProp.make_primrec_Ts to avoid code duplication.
berghofe
parents: 14981
diff changeset
    17
    string list -> typ list * typ list
13641
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 13585
diff changeset
    18
  val make_primrecs : string list -> DatatypeAux.descr list ->
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 13585
diff changeset
    19
    (string * sort) list -> theory -> term list
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 13585
diff changeset
    20
  val make_cases : string list -> DatatypeAux.descr list ->
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 13585
diff changeset
    21
    (string * sort) list -> theory -> term list list
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 13585
diff changeset
    22
  val make_distincts : string list -> DatatypeAux.descr list ->
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 13585
diff changeset
    23
    (string * sort) list -> theory -> term list list
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 13585
diff changeset
    24
  val make_splits : string list -> DatatypeAux.descr list ->
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 13585
diff changeset
    25
    (string * sort) list -> theory -> (term * term) list
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 13585
diff changeset
    26
  val make_size : DatatypeAux.descr list -> (string * sort) list ->
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 13585
diff changeset
    27
    theory -> term list
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 13585
diff changeset
    28
  val make_weak_case_congs : string list -> DatatypeAux.descr list ->
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 13585
diff changeset
    29
    (string * sort) list -> theory -> term list
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 13585
diff changeset
    30
  val make_case_congs : string list -> DatatypeAux.descr list ->
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 13585
diff changeset
    31
    (string * sort) list -> theory -> term list
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 13585
diff changeset
    32
  val make_nchotomys : DatatypeAux.descr list ->
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 13585
diff changeset
    33
    (string * sort) list -> term list
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    34
end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    35
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    36
structure DatatypeProp : DATATYPE_PROP =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    37
struct
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    38
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    39
open DatatypeAux;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    40
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    41
(*the kind of distinctiveness axioms depends on number of constructors*)
7015
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
    42
val dtK = ref 7;
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    43
8434
5e4bba59bfaa use HOLogic.Not;
wenzelm
parents: 7704
diff changeset
    44
fun indexify_names names =
5e4bba59bfaa use HOLogic.Not;
wenzelm
parents: 7704
diff changeset
    45
  let
5e4bba59bfaa use HOLogic.Not;
wenzelm
parents: 7704
diff changeset
    46
    fun index (x :: xs) tab =
17521
0f1c48de39f5 introduced AList module in favor of assoc etc.
haftmann
parents: 15574
diff changeset
    47
      (case AList.lookup (op =) tab x of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15459
diff changeset
    48
        NONE => if x mem xs then (x ^ "1") :: index xs ((x, 2) :: tab) else x :: index xs tab
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15459
diff changeset
    49
      | SOME i => (x ^ Library.string_of_int i) :: index xs ((x, i + 1) :: tab))
8434
5e4bba59bfaa use HOLogic.Not;
wenzelm
parents: 7704
diff changeset
    50
    | index [] _ = [];
5e4bba59bfaa use HOLogic.Not;
wenzelm
parents: 7704
diff changeset
    51
  in index names [] end;
5e4bba59bfaa use HOLogic.Not;
wenzelm
parents: 7704
diff changeset
    52
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    53
fun make_tnames Ts =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    54
  let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    55
    fun type_name (TFree (name, _)) = implode (tl (explode name))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    56
      | type_name (Type (name, _)) = 
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    57
          let val name' = Sign.base_name name
8434
5e4bba59bfaa use HOLogic.Not;
wenzelm
parents: 7704
diff changeset
    58
          in if Syntax.is_identifier name' then name' else "x" end;
5e4bba59bfaa use HOLogic.Not;
wenzelm
parents: 7704
diff changeset
    59
  in indexify_names (map type_name Ts) end;
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    60
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    61
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    62
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    63
(************************* injectivity of constructors ************************)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    64
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    65
fun make_injs descr sorts =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    66
  let
21078
101aefd61aac slight cleanup
haftmann
parents: 20071
diff changeset
    67
    val descr' = flat descr;
101aefd61aac slight cleanup
haftmann
parents: 20071
diff changeset
    68
    fun make_inj T (cname, cargs) =
101aefd61aac slight cleanup
haftmann
parents: 20071
diff changeset
    69
      if null cargs then I else
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    70
        let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    71
          val Ts = map (typ_of_dtyp descr' sorts) cargs;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    72
          val constr_t = Const (cname, Ts ---> T);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    73
          val tnames = make_tnames Ts;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    74
          val frees = map Free (tnames ~~ Ts);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    75
          val frees' = map Free ((map ((op ^) o (rpair "'")) tnames) ~~ Ts);
21078
101aefd61aac slight cleanup
haftmann
parents: 20071
diff changeset
    76
        in cons (HOLogic.mk_Trueprop (HOLogic.mk_eq
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    77
          (HOLogic.mk_eq (list_comb (constr_t, frees), list_comb (constr_t, frees')),
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    78
           foldr1 (HOLogic.mk_binop "op &")
21078
101aefd61aac slight cleanup
haftmann
parents: 20071
diff changeset
    79
             (map HOLogic.mk_eq (frees ~~ frees')))))
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    80
        end;
21078
101aefd61aac slight cleanup
haftmann
parents: 20071
diff changeset
    81
  in
101aefd61aac slight cleanup
haftmann
parents: 20071
diff changeset
    82
    map2 (fn d => fn T => fold_rev (make_inj T) (#3 (snd d)) [])
101aefd61aac slight cleanup
haftmann
parents: 20071
diff changeset
    83
      (hd descr) (Library.take (length (hd descr), get_rec_types descr' sorts))
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    84
  end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    85
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    86
(********************************* induction **********************************)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    87
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    88
fun make_ind descr sorts =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    89
  let
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
    90
    val descr' = List.concat descr;
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    91
    val recTs = get_rec_types descr' sorts;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    92
    val pnames = if length descr' = 1 then ["P"]
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    93
      else map (fn i => "P" ^ string_of_int i) (1 upto length descr');
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    94
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    95
    fun make_pred i T =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    96
      let val T' = T --> HOLogic.boolT
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
    97
      in Free (List.nth (pnames, i), T') end;
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    98
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    99
    fun make_ind_prem k T (cname, cargs) =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   100
      let
13641
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 13585
diff changeset
   101
        fun mk_prem ((dt, s), T) =
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 13585
diff changeset
   102
          let val (Us, U) = strip_type T
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 13585
diff changeset
   103
          in list_all (map (pair "x") Us, HOLogic.mk_Trueprop
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 13585
diff changeset
   104
            (make_pred (body_index dt) U $ app_bnds (Free (s, T)) (length Us)))
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 13585
diff changeset
   105
          end;
7015
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   106
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   107
        val recs = List.filter is_rec_type cargs;
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   108
        val Ts = map (typ_of_dtyp descr' sorts) cargs;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   109
        val recTs' = map (typ_of_dtyp descr' sorts) recs;
20071
8f3e1ddb50e6 replaced Term.variant(list) by Name.variant(_list);
wenzelm
parents: 19233
diff changeset
   110
        val tnames = Name.variant_list pnames (make_tnames Ts);
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   111
        val rec_tnames = map fst (List.filter (is_rec_type o snd) (tnames ~~ cargs));
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   112
        val frees = tnames ~~ Ts;
7015
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   113
        val prems = map mk_prem (recs ~~ rec_tnames ~~ recTs');
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   114
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   115
      in list_all_free (frees, Logic.list_implies (prems,
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   116
        HOLogic.mk_Trueprop (make_pred k T $ 
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   117
          list_comb (Const (cname, Ts ---> T), map Free frees))))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   118
      end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   119
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   120
    val prems = List.concat (map (fn ((i, (_, _, constrs)), T) =>
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   121
      map (make_ind_prem i T) constrs) (descr' ~~ recTs));
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   122
    val tnames = make_tnames recTs;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   123
    val concl = HOLogic.mk_Trueprop (foldr1 (HOLogic.mk_binop "op &")
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   124
      (map (fn (((i, _), T), tname) => make_pred i T $ Free (tname, T))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   125
        (descr' ~~ recTs ~~ tnames)))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   126
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   127
  in Logic.list_implies (prems, concl) end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   128
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   129
(******************************* case distinction *****************************)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   130
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   131
fun make_casedists descr sorts =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   132
  let
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   133
    val descr' = List.concat descr;
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   134
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   135
    fun make_casedist_prem T (cname, cargs) =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   136
      let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   137
        val Ts = map (typ_of_dtyp descr' sorts) cargs;
20071
8f3e1ddb50e6 replaced Term.variant(list) by Name.variant(_list);
wenzelm
parents: 19233
diff changeset
   138
        val frees = Name.variant_list ["P", "y"] (make_tnames Ts) ~~ Ts;
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   139
        val free_ts = map Free frees
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   140
      in list_all_free (frees, Logic.mk_implies (HOLogic.mk_Trueprop
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   141
        (HOLogic.mk_eq (Free ("y", T), list_comb (Const (cname, Ts ---> T), free_ts))),
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   142
          HOLogic.mk_Trueprop (Free ("P", HOLogic.boolT))))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   143
      end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   144
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   145
    fun make_casedist ((_, (_, _, constrs)), T) =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   146
      let val prems = map (make_casedist_prem T) constrs
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   147
      in Logic.list_implies (prems, HOLogic.mk_Trueprop (Free ("P", HOLogic.boolT)))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   148
      end
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   149
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   150
  in map make_casedist
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   151
    ((hd descr) ~~ Library.take (length (hd descr), get_rec_types descr' sorts))
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   152
  end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   153
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   154
(*************** characteristic equations for primrec combinator **************)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   155
15459
16dd63c78049 Introduced function DatatypeProp.make_primrec_Ts to avoid code duplication.
berghofe
parents: 14981
diff changeset
   156
fun make_primrec_Ts descr sorts used =
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   157
  let
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   158
    val descr' = List.concat descr;
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   159
20071
8f3e1ddb50e6 replaced Term.variant(list) by Name.variant(_list);
wenzelm
parents: 19233
diff changeset
   160
    val rec_result_Ts = map TFree (Name.variant_list used (replicate (length descr') "'t") ~~
12338
de0f4a63baa5 renamed class "term" to "type" (actually "HOL.type");
wenzelm
parents: 11957
diff changeset
   161
      replicate (length descr') HOLogic.typeS);
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   162
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   163
    val reccomb_fn_Ts = List.concat (map (fn (i, (_, _, constrs)) =>
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   164
      map (fn (_, cargs) =>
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   165
        let
7015
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   166
          val Ts = map (typ_of_dtyp descr' sorts) cargs;
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   167
          val recs = List.filter (is_rec_type o fst) (cargs ~~ Ts);
7015
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   168
13641
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 13585
diff changeset
   169
          fun mk_argT (dt, T) =
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   170
            binder_types T ---> List.nth (rec_result_Ts, body_index dt);
7015
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   171
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   172
          val argTs = Ts @ map mk_argT recs
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   173
        in argTs ---> List.nth (rec_result_Ts, i)
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   174
        end) constrs) descr');
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   175
15459
16dd63c78049 Introduced function DatatypeProp.make_primrec_Ts to avoid code duplication.
berghofe
parents: 14981
diff changeset
   176
  in (rec_result_Ts, reccomb_fn_Ts) end;
16dd63c78049 Introduced function DatatypeProp.make_primrec_Ts to avoid code duplication.
berghofe
parents: 14981
diff changeset
   177
16dd63c78049 Introduced function DatatypeProp.make_primrec_Ts to avoid code duplication.
berghofe
parents: 14981
diff changeset
   178
fun make_primrecs new_type_names descr sorts thy =
16dd63c78049 Introduced function DatatypeProp.make_primrec_Ts to avoid code duplication.
berghofe
parents: 14981
diff changeset
   179
  let
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   180
    val descr' = List.concat descr;
15459
16dd63c78049 Introduced function DatatypeProp.make_primrec_Ts to avoid code duplication.
berghofe
parents: 14981
diff changeset
   181
    val recTs = get_rec_types descr' sorts;
15574
b1d1b5bfc464 Removed practically all references to Library.foldr.
skalberg
parents: 15570
diff changeset
   182
    val used = foldr add_typ_tfree_names [] recTs;
15459
16dd63c78049 Introduced function DatatypeProp.make_primrec_Ts to avoid code duplication.
berghofe
parents: 14981
diff changeset
   183
16dd63c78049 Introduced function DatatypeProp.make_primrec_Ts to avoid code duplication.
berghofe
parents: 14981
diff changeset
   184
    val (rec_result_Ts, reccomb_fn_Ts) = make_primrec_Ts descr sorts used;
16dd63c78049 Introduced function DatatypeProp.make_primrec_Ts to avoid code duplication.
berghofe
parents: 14981
diff changeset
   185
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   186
    val rec_fns = map (uncurry (mk_Free "f"))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   187
      (reccomb_fn_Ts ~~ (1 upto (length reccomb_fn_Ts)));
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   188
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   189
    val big_reccomb_name = (space_implode "_" new_type_names) ^ "_rec";
22578
b0eb5652f210 removed obsolete sign_of/sign_of_thm;
wenzelm
parents: 21621
diff changeset
   190
    val reccomb_names = map (Sign.intern_const thy)
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   191
      (if length descr' = 1 then [big_reccomb_name] else
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   192
        (map ((curry (op ^) (big_reccomb_name ^ "_")) o string_of_int)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   193
          (1 upto (length descr'))));
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   194
    val reccombs = map (fn ((name, T), T') => list_comb
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   195
      (Const (name, reccomb_fn_Ts @ [T] ---> T'), rec_fns))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   196
        (reccomb_names ~~ recTs ~~ rec_result_Ts);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   197
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   198
    fun make_primrec T comb_t ((ts, f::fs), (cname, cargs)) =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   199
      let
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   200
        val recs = List.filter is_rec_type cargs;
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   201
        val Ts = map (typ_of_dtyp descr' sorts) cargs;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   202
        val recTs' = map (typ_of_dtyp descr' sorts) recs;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   203
        val tnames = make_tnames Ts;
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   204
        val rec_tnames = map fst (List.filter (is_rec_type o snd) (tnames ~~ cargs));
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   205
        val frees = map Free (tnames ~~ Ts);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   206
        val frees' = map Free (rec_tnames ~~ recTs');
7015
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   207
13641
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 13585
diff changeset
   208
        fun mk_reccomb ((dt, T), t) =
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 13585
diff changeset
   209
          let val (Us, U) = strip_type T
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 13585
diff changeset
   210
          in list_abs (map (pair "x") Us,
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   211
            List.nth (reccombs, body_index dt) $ app_bnds t (length Us))
13641
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 13585
diff changeset
   212
          end;
7015
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   213
13641
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 13585
diff changeset
   214
        val reccombs' = map mk_reccomb (recs ~~ recTs' ~~ frees')
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   215
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   216
      in (ts @ [HOLogic.mk_Trueprop (HOLogic.mk_eq
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   217
        (comb_t $ list_comb (Const (cname, Ts ---> T), frees),
13641
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 13585
diff changeset
   218
         list_comb (f, frees @ reccombs')))], fs)
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   219
      end
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   220
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   221
  in fst (Library.foldl (fn (x, ((dt, T), comb_t)) =>
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   222
    Library.foldl (make_primrec T comb_t) (x, #3 (snd dt)))
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   223
      (([], rec_fns), descr' ~~ recTs ~~ reccombs))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   224
  end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   225
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   226
(****************** make terms of form  t_case f1 ... fn  *********************)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   227
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   228
fun make_case_combs new_type_names descr sorts thy fname =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   229
  let
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   230
    val descr' = List.concat descr;
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   231
    val recTs = get_rec_types descr' sorts;
15574
b1d1b5bfc464 Removed practically all references to Library.foldr.
skalberg
parents: 15570
diff changeset
   232
    val used = foldr add_typ_tfree_names [] recTs;
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   233
    val newTs = Library.take (length (hd descr), recTs);
20071
8f3e1ddb50e6 replaced Term.variant(list) by Name.variant(_list);
wenzelm
parents: 19233
diff changeset
   234
    val T' = TFree (Name.variant used "'t", HOLogic.typeS);
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   235
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   236
    val case_fn_Ts = map (fn (i, (_, _, constrs)) =>
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   237
      map (fn (_, cargs) =>
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   238
        let val Ts = map (typ_of_dtyp descr' sorts) cargs
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   239
        in Ts ---> T' end) constrs) (hd descr);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   240
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   241
    val case_names = map (fn s =>
22578
b0eb5652f210 removed obsolete sign_of/sign_of_thm;
wenzelm
parents: 21621
diff changeset
   242
      Sign.intern_const thy (s ^ "_case")) new_type_names
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   243
  in
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   244
    map (fn ((name, Ts), T) => list_comb
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   245
      (Const (name, Ts @ [T] ---> T'),
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   246
        map (uncurry (mk_Free fname)) (Ts ~~ (1 upto length Ts))))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   247
          (case_names ~~ case_fn_Ts ~~ newTs)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   248
  end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   249
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   250
(**************** characteristic equations for case combinator ****************)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   251
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   252
fun make_cases new_type_names descr sorts thy =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   253
  let
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   254
    val descr' = List.concat descr;
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   255
    val recTs = get_rec_types descr' sorts;
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   256
    val newTs = Library.take (length (hd descr), recTs);
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   257
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   258
    fun make_case T comb_t ((cname, cargs), f) =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   259
      let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   260
        val Ts = map (typ_of_dtyp descr' sorts) cargs;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   261
        val frees = map Free ((make_tnames Ts) ~~ Ts)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   262
      in HOLogic.mk_Trueprop (HOLogic.mk_eq
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   263
        (comb_t $ list_comb (Const (cname, Ts ---> T), frees),
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   264
         list_comb (f, frees)))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   265
      end
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   266
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   267
  in map (fn (((_, (_, _, constrs)), T), comb_t) =>
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   268
    map (make_case T comb_t) (constrs ~~ (snd (strip_comb comb_t))))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   269
      ((hd descr) ~~ newTs ~~ (make_case_combs new_type_names descr sorts thy "f"))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   270
  end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   271
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   272
(************************* distinctness of constructors ***********************)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   273
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   274
fun make_distincts new_type_names descr sorts thy =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   275
  let
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   276
    val descr' = List.concat descr;
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   277
    val recTs = get_rec_types descr' sorts;
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   278
    val newTs = Library.take (length (hd descr), recTs);
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   279
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   280
    (**** number of constructors < dtK : C_i ... ~= C_j ... ****)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   281
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   282
    fun make_distincts_1 _ [] = []
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   283
      | make_distincts_1 T ((cname, cargs)::constrs) =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   284
          let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   285
            val Ts = map (typ_of_dtyp descr' sorts) cargs;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   286
            val frees = map Free ((make_tnames Ts) ~~ Ts);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   287
            val t = list_comb (Const (cname, Ts ---> T), frees);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   288
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   289
            fun make_distincts' [] = []
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   290
              | make_distincts' ((cname', cargs')::constrs') =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   291
                  let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   292
                    val Ts' = map (typ_of_dtyp descr' sorts) cargs';
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   293
                    val frees' = map Free ((map ((op ^) o (rpair "'"))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   294
                      (make_tnames Ts')) ~~ Ts');
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   295
                    val t' = list_comb (Const (cname', Ts' ---> T), frees')
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   296
                  in
8434
5e4bba59bfaa use HOLogic.Not;
wenzelm
parents: 7704
diff changeset
   297
                    (HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.mk_eq (t, t')))::
5e4bba59bfaa use HOLogic.Not;
wenzelm
parents: 7704
diff changeset
   298
                    (HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.mk_eq (t', t)))::
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   299
                      (make_distincts' constrs')
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   300
                  end
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   301
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   302
          in (make_distincts' constrs) @ (make_distincts_1 T constrs)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   303
          end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   304
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   305
  in map (fn (((_, (_, _, constrs)), T), tname) =>
7015
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   306
      if length constrs < !dtK then make_distincts_1 T constrs else [])
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   307
        ((hd descr) ~~ newTs ~~ new_type_names)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   308
  end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   309
7015
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   310
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   311
(*************************** the "split" - equations **************************)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   312
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   313
fun make_splits new_type_names descr sorts thy =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   314
  let
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   315
    val descr' = List.concat descr;
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   316
    val recTs = get_rec_types descr' sorts;
15574
b1d1b5bfc464 Removed practically all references to Library.foldr.
skalberg
parents: 15570
diff changeset
   317
    val used' = foldr add_typ_tfree_names [] recTs;
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   318
    val newTs = Library.take (length (hd descr), recTs);
20071
8f3e1ddb50e6 replaced Term.variant(list) by Name.variant(_list);
wenzelm
parents: 19233
diff changeset
   319
    val T' = TFree (Name.variant used' "'t", HOLogic.typeS);
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   320
    val P = Free ("P", T' --> HOLogic.boolT);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   321
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   322
    fun make_split (((_, (_, _, constrs)), T), comb_t) =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   323
      let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   324
        val (_, fs) = strip_comb comb_t;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   325
        val used = ["P", "x"] @ (map (fst o dest_Free) fs);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   326
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   327
        fun process_constr (((cname, cargs), f), (t1s, t2s)) =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   328
          let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   329
            val Ts = map (typ_of_dtyp descr' sorts) cargs;
20071
8f3e1ddb50e6 replaced Term.variant(list) by Name.variant(_list);
wenzelm
parents: 19233
diff changeset
   330
            val frees = map Free (Name.variant_list used (make_tnames Ts) ~~ Ts);
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   331
            val eqn = HOLogic.mk_eq (Free ("x", T),
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   332
              list_comb (Const (cname, Ts ---> T), frees));
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   333
            val P' = P $ list_comb (f, frees)
15574
b1d1b5bfc464 Removed practically all references to Library.foldr.
skalberg
parents: 15570
diff changeset
   334
          in ((foldr (fn (Free (s, T), t) => HOLogic.mk_all (s, T, t))
b1d1b5bfc464 Removed practically all references to Library.foldr.
skalberg
parents: 15570
diff changeset
   335
                (HOLogic.imp $ eqn $ P') frees)::t1s,
b1d1b5bfc464 Removed practically all references to Library.foldr.
skalberg
parents: 15570
diff changeset
   336
              (foldr (fn (Free (s, T), t) => HOLogic.mk_exists (s, T, t))
b1d1b5bfc464 Removed practically all references to Library.foldr.
skalberg
parents: 15570
diff changeset
   337
                (HOLogic.conj $ eqn $ (HOLogic.Not $ P')) frees)::t2s)
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   338
          end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   339
15574
b1d1b5bfc464 Removed practically all references to Library.foldr.
skalberg
parents: 15570
diff changeset
   340
        val (t1s, t2s) = foldr process_constr ([], []) (constrs ~~ fs);
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   341
        val lhs = P $ (comb_t $ Free ("x", T))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   342
      in
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   343
        (HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, mk_conj t1s)),
8434
5e4bba59bfaa use HOLogic.Not;
wenzelm
parents: 7704
diff changeset
   344
         HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, HOLogic.Not $ mk_disj t2s)))
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   345
      end
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   346
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   347
  in map make_split ((hd descr) ~~ newTs ~~
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   348
    (make_case_combs new_type_names descr sorts thy "f"))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   349
  end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   350
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   351
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   352
(******************************* size functions *******************************)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   353
9739
8470c4662685 Improved names for size function.
berghofe
parents: 9060
diff changeset
   354
fun make_size descr sorts thy =
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   355
  let
22994
02440636214f abstract size function in hologic.ML
haftmann
parents: 22578
diff changeset
   356
    val descr' = flat descr;
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   357
    val recTs = get_rec_types descr' sorts;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   358
22994
02440636214f abstract size function in hologic.ML
haftmann
parents: 22578
diff changeset
   359
    val Const (size_name, _) = HOLogic.size_const dummyT;
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   360
    val size_names = replicate (length (hd descr)) size_name @
22578
b0eb5652f210 removed obsolete sign_of/sign_of_thm;
wenzelm
parents: 21621
diff changeset
   361
      map (Sign.intern_const thy) (indexify_names
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   362
        (map (fn T => name_of_typ T ^ "_size") (Library.drop (length (hd descr), recTs))));
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   363
    val size_consts = map (fn (s, T) =>
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   364
      Const (s, T --> HOLogic.natT)) (size_names ~~ recTs);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   365
22994
02440636214f abstract size function in hologic.ML
haftmann
parents: 22578
diff changeset
   366
    fun plus (t1, t2) = Const ("HOL.plus_class.plus", HOLogic.natT --> HOLogic.natT --> HOLogic.natT) $ t1 $ t2;
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   367
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   368
    fun make_size_eqn size_const T (cname, cargs) =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   369
      let
22994
02440636214f abstract size function in hologic.ML
haftmann
parents: 22578
diff changeset
   370
        val recs = filter is_rec_type cargs;
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   371
        val Ts = map (typ_of_dtyp descr' sorts) cargs;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   372
        val recTs = map (typ_of_dtyp descr' sorts) recs;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   373
        val tnames = make_tnames Ts;
22994
02440636214f abstract size function in hologic.ML
haftmann
parents: 22578
diff changeset
   374
        val rec_tnames = map fst (filter (is_rec_type o snd) (tnames ~~ cargs));
02440636214f abstract size function in hologic.ML
haftmann
parents: 22578
diff changeset
   375
        val ts = map (fn ((r, s), T) => nth size_consts (dest_DtRec r) $
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   376
          Free (s, T)) (recs ~~ rec_tnames ~~ recTs);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   377
        val t = if ts = [] then HOLogic.zero else
21621
f9fd69d96c4e slight cleanup in hologic.ML
haftmann
parents: 21078
diff changeset
   378
          foldl1 plus (ts @ [HOLogic.Suc_zero])
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   379
      in
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   380
        HOLogic.mk_Trueprop (HOLogic.mk_eq (size_const $
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   381
          list_comb (Const (cname, Ts ---> T), map Free (tnames ~~ Ts)), t))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   382
      end
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   383
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   384
  in
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   385
    List.concat (map (fn (((_, (_, _, constrs)), size_const), T) =>
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   386
      map (make_size_eqn size_const T) constrs) (descr' ~~ size_consts ~~ recTs))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   387
  end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   388
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   389
(************************* additional rules for TFL ***************************)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   390
8601
8fb3a81b4ccf added weak_case_cong feature
nipkow
parents: 8434
diff changeset
   391
fun make_weak_case_congs new_type_names descr sorts thy =
8fb3a81b4ccf added weak_case_cong feature
nipkow
parents: 8434
diff changeset
   392
  let
8fb3a81b4ccf added weak_case_cong feature
nipkow
parents: 8434
diff changeset
   393
    val case_combs = make_case_combs new_type_names descr sorts thy "f";
8fb3a81b4ccf added weak_case_cong feature
nipkow
parents: 8434
diff changeset
   394
8fb3a81b4ccf added weak_case_cong feature
nipkow
parents: 8434
diff changeset
   395
    fun mk_case_cong comb =
8fb3a81b4ccf added weak_case_cong feature
nipkow
parents: 8434
diff changeset
   396
      let 
8fb3a81b4ccf added weak_case_cong feature
nipkow
parents: 8434
diff changeset
   397
        val Type ("fun", [T, _]) = fastype_of comb;
8fb3a81b4ccf added weak_case_cong feature
nipkow
parents: 8434
diff changeset
   398
        val M = Free ("M", T);
8fb3a81b4ccf added weak_case_cong feature
nipkow
parents: 8434
diff changeset
   399
        val M' = Free ("M'", T);
8fb3a81b4ccf added weak_case_cong feature
nipkow
parents: 8434
diff changeset
   400
      in
8fb3a81b4ccf added weak_case_cong feature
nipkow
parents: 8434
diff changeset
   401
        Logic.mk_implies (HOLogic.mk_Trueprop (HOLogic.mk_eq (M, M')),
8fb3a81b4ccf added weak_case_cong feature
nipkow
parents: 8434
diff changeset
   402
          HOLogic.mk_Trueprop (HOLogic.mk_eq (comb $ M, comb $ M')))
8fb3a81b4ccf added weak_case_cong feature
nipkow
parents: 8434
diff changeset
   403
      end
8fb3a81b4ccf added weak_case_cong feature
nipkow
parents: 8434
diff changeset
   404
  in
8fb3a81b4ccf added weak_case_cong feature
nipkow
parents: 8434
diff changeset
   405
    map mk_case_cong case_combs
8fb3a81b4ccf added weak_case_cong feature
nipkow
parents: 8434
diff changeset
   406
  end;
8fb3a81b4ccf added weak_case_cong feature
nipkow
parents: 8434
diff changeset
   407
 
8fb3a81b4ccf added weak_case_cong feature
nipkow
parents: 8434
diff changeset
   408
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   409
(*---------------------------------------------------------------------------
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   410
 * Structure of case congruence theorem looks like this:
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   411
 *
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   412
 *    (M = M') 
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   413
 *    ==> (!!x1,...,xk. (M' = C1 x1..xk) ==> (f1 x1..xk = g1 x1..xk)) 
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   414
 *    ==> ... 
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   415
 *    ==> (!!x1,...,xj. (M' = Cn x1..xj) ==> (fn x1..xj = gn x1..xj)) 
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   416
 *    ==>
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   417
 *      (ty_case f1..fn M = ty_case g1..gn M')
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   418
 *---------------------------------------------------------------------------*)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   419
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   420
fun make_case_congs new_type_names descr sorts thy =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   421
  let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   422
    val case_combs = make_case_combs new_type_names descr sorts thy "f";
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   423
    val case_combs' = make_case_combs new_type_names descr sorts thy "g";
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   424
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   425
    fun mk_case_cong ((comb, comb'), (_, (_, _, constrs))) =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   426
      let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   427
        val Type ("fun", [T, _]) = fastype_of comb;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   428
        val (_, fs) = strip_comb comb;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   429
        val (_, gs) = strip_comb comb';
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   430
        val used = ["M", "M'"] @ map (fst o dest_Free) (fs @ gs);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   431
        val M = Free ("M", T);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   432
        val M' = Free ("M'", T);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   433
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   434
        fun mk_clause ((f, g), (cname, _)) =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   435
          let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   436
            val (Ts, _) = strip_type (fastype_of f);
20071
8f3e1ddb50e6 replaced Term.variant(list) by Name.variant(_list);
wenzelm
parents: 19233
diff changeset
   437
            val tnames = Name.variant_list used (make_tnames Ts);
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   438
            val frees = map Free (tnames ~~ Ts)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   439
          in
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   440
            list_all_free (tnames ~~ Ts, Logic.mk_implies
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   441
              (HOLogic.mk_Trueprop
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   442
                (HOLogic.mk_eq (M', list_comb (Const (cname, Ts ---> T), frees))),
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   443
               HOLogic.mk_Trueprop
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   444
                (HOLogic.mk_eq (list_comb (f, frees), list_comb (g, frees)))))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   445
          end
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   446
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   447
      in
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   448
        Logic.list_implies (HOLogic.mk_Trueprop (HOLogic.mk_eq (M, M')) ::
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   449
          map mk_clause (fs ~~ gs ~~ constrs),
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   450
            HOLogic.mk_Trueprop (HOLogic.mk_eq (comb $ M, comb' $ M')))
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
  in
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   454
    map mk_case_cong (case_combs ~~ case_combs' ~~ hd descr)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   455
  end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   456
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   457
(*---------------------------------------------------------------------------
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   458
 * Structure of exhaustion theorem looks like this:
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   459
 *
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   460
 *    !v. (? y1..yi. v = C1 y1..yi) | ... | (? y1..yj. v = Cn y1..yj)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   461
 *---------------------------------------------------------------------------*)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   462
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   463
fun make_nchotomys descr sorts =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   464
  let
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   465
    val descr' = List.concat descr;
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   466
    val recTs = get_rec_types descr' sorts;
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   467
    val newTs = Library.take (length (hd descr), recTs);
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   468
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   469
    fun mk_eqn T (cname, cargs) =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   470
      let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   471
        val Ts = map (typ_of_dtyp descr' sorts) cargs;
20071
8f3e1ddb50e6 replaced Term.variant(list) by Name.variant(_list);
wenzelm
parents: 19233
diff changeset
   472
        val tnames = Name.variant_list ["v"] (make_tnames Ts);
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   473
        val frees = tnames ~~ Ts
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   474
      in
15574
b1d1b5bfc464 Removed practically all references to Library.foldr.
skalberg
parents: 15570
diff changeset
   475
        foldr (fn ((s, T'), t) => HOLogic.mk_exists (s, T', t))
b1d1b5bfc464 Removed practically all references to Library.foldr.
skalberg
parents: 15570
diff changeset
   476
          (HOLogic.mk_eq (Free ("v", T),
b1d1b5bfc464 Removed practically all references to Library.foldr.
skalberg
parents: 15570
diff changeset
   477
            list_comb (Const (cname, Ts ---> T), map Free frees))) frees
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   478
      end
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   479
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   480
  in map (fn ((_, (_, _, constrs)), T) =>
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   481
    HOLogic.mk_Trueprop (HOLogic.mk_all ("v", T, mk_disj (map (mk_eqn T) constrs))))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   482
      (hd descr ~~ newTs)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   483
  end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   484
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   485
end;