src/HOL/Tools/datatype_prop.ML
author berghofe
Fri, 24 Jul 1998 12:50:06 +0200
changeset 5177 0d3a168e4d44
child 5578 7de426cf179c
permissions -rw-r--r--
New datatype definition package
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$
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
Characteristic properties of datatypes
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
     7
*)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
     8
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
     9
signature DATATYPE_PROP =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    10
sig
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    11
  val dtK : int
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    12
  val make_injs : (int * (string * DatatypeAux.dtyp list *
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    13
    (string * DatatypeAux.dtyp list) list)) list list -> (string * sort) list ->
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    14
      term list list
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    15
  val make_ind : (int * (string * DatatypeAux.dtyp list *
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    16
    (string * DatatypeAux.dtyp list) list)) list list -> (string * sort) list -> term
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    17
  val make_casedists : (int * (string * DatatypeAux.dtyp list *
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    18
    (string * DatatypeAux.dtyp list) list)) list list -> (string * sort) list -> term list
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    19
  val make_primrecs : string list -> (int * (string * DatatypeAux.dtyp list *
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    20
    (string * DatatypeAux.dtyp list) list)) list list -> (string * sort) list ->
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    21
      theory -> term list
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    22
  val make_cases : 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
      theory -> term list list
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    25
  val make_distincts : string list -> (int * (string * DatatypeAux.dtyp list *
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
      theory -> term list list
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    28
  val make_splits : string list -> (int * (string * DatatypeAux.dtyp list *
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    29
    (string * DatatypeAux.dtyp list) list)) list list -> (string * sort) list ->
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    30
      theory -> (term * term) list
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    31
  val make_case_trrules : string list -> (int * (string * DatatypeAux.dtyp list *
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    32
    (string * DatatypeAux.dtyp list) list)) list list -> ast Syntax.trrule list
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    33
  val make_size : string list -> (int * (string * DatatypeAux.dtyp list *
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    34
    (string * DatatypeAux.dtyp list) list)) list list -> (string * sort) list ->
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    35
      theory -> term list
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    36
  val make_case_congs : string list -> (int * (string * DatatypeAux.dtyp list *
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    37
    (string * DatatypeAux.dtyp list) list)) list list -> (string * sort) list ->
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    38
      theory -> term list
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    39
  val make_nchotomys : (int * (string * DatatypeAux.dtyp list *
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    40
    (string * DatatypeAux.dtyp list) list)) list list -> (string * sort) list -> term list
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    41
end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    42
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    43
structure DatatypeProp : DATATYPE_PROP =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    44
struct
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    45
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    46
open DatatypeAux;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    47
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    48
(*the kind of distinctiveness axioms depends on number of constructors*)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    49
val dtK = 7;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    50
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    51
fun make_tnames Ts =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    52
  let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    53
    fun type_name (TFree (name, _)) = implode (tl (explode name))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    54
      | type_name (Type (name, _)) = 
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    55
          let val name' = Sign.base_name name
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    56
          in if Syntax.is_identifier name' then name' else "x"
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    57
          end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    58
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    59
    fun index_vnames (vn::vns) tab =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    60
          (case assoc (tab, vn) of
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    61
             None => if vn mem vns
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    62
                     then (vn ^ "1") :: index_vnames vns ((vn, 2)::tab)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    63
                     else vn :: index_vnames vns tab
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    64
           | Some i => (vn ^ (string_of_int i))::
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    65
                     index_vnames vns ((vn, i + 1)::tab))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    66
      | index_vnames [] _ = []
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    67
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    68
  in index_vnames (map type_name Ts) []
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    69
  end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    70
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    71
(** FIXME: move to hologic.ML ? **)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    72
val Not = Const ("Not", HOLogic.boolT --> HOLogic.boolT);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    73
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    74
(************************* injectivity of constructors ************************)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    75
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    76
fun make_injs descr sorts =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    77
  let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    78
    val descr' = flat descr;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    79
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    80
    fun make_inj T ((cname, cargs), injs) =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    81
      if null cargs then injs else
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    82
        let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    83
          val Ts = map (typ_of_dtyp descr' sorts) cargs;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    84
          val constr_t = Const (cname, Ts ---> T);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    85
          val tnames = make_tnames Ts;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    86
          val frees = map Free (tnames ~~ Ts);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    87
          val frees' = map Free ((map ((op ^) o (rpair "'")) tnames) ~~ Ts);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    88
        in (HOLogic.mk_Trueprop (HOLogic.mk_eq
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    89
          (HOLogic.mk_eq (list_comb (constr_t, frees), list_comb (constr_t, frees')),
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    90
           foldr1 (HOLogic.mk_binop "op &")
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    91
             (map HOLogic.mk_eq (frees ~~ frees')))))::injs
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    92
        end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    93
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    94
  in map (fn (d, T) => foldr (make_inj T) (#3 (snd d), []))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    95
    ((hd descr) ~~ take (length (hd descr), get_rec_types descr' sorts))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    96
  end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    97
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    98
(********************************* induction **********************************)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    99
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   100
fun make_ind descr sorts =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   101
  let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   102
    val descr' = flat descr;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   103
    val recTs = get_rec_types descr' sorts;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   104
    val pnames = if length descr' = 1 then ["P"]
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   105
      else map (fn i => "P" ^ string_of_int i) (1 upto length descr');
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   106
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   107
    fun make_pred i T =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   108
      let val T' = T --> HOLogic.boolT
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   109
      in Free (nth_elem (i, pnames), T') end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   110
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   111
    fun make_ind_prem k T (cname, cargs) =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   112
      let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   113
        val recs = filter is_rec_type cargs;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   114
        val Ts = map (typ_of_dtyp descr' sorts) cargs;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   115
        val recTs' = map (typ_of_dtyp descr' sorts) recs;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   116
        val tnames = variantlist (make_tnames Ts, pnames);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   117
        val rec_tnames = map fst (filter (is_rec_type o snd) (tnames ~~ cargs));
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   118
        val frees = tnames ~~ Ts;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   119
        val prems = map (fn ((r, s), T) => HOLogic.mk_Trueprop
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   120
          (make_pred (dest_DtRec r) T $ Free (s, T))) (recs ~~ rec_tnames ~~ recTs');
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   121
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   122
      in list_all_free (frees, Logic.list_implies (prems,
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   123
        HOLogic.mk_Trueprop (make_pred k T $ 
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   124
          list_comb (Const (cname, Ts ---> T), map Free frees))))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   125
      end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   126
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   127
    val prems = flat (map (fn ((i, (_, _, constrs)), T) =>
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   128
      map (make_ind_prem i T) constrs) (descr' ~~ recTs));
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   129
    val tnames = make_tnames recTs;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   130
    val concl = HOLogic.mk_Trueprop (foldr1 (HOLogic.mk_binop "op &")
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   131
      (map (fn (((i, _), T), tname) => make_pred i T $ Free (tname, T))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   132
        (descr' ~~ recTs ~~ tnames)))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   133
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   134
  in Logic.list_implies (prems, concl) end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   135
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   136
(******************************* case distinction *****************************)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   137
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   138
fun make_casedists descr sorts =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   139
  let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   140
    val descr' = flat descr;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   141
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   142
    fun make_casedist_prem T (cname, cargs) =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   143
      let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   144
        val Ts = map (typ_of_dtyp descr' sorts) cargs;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   145
        val frees = variantlist (make_tnames Ts, ["P", "y"]) ~~ Ts;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   146
        val free_ts = map Free frees
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   147
      in list_all_free (frees, Logic.mk_implies (HOLogic.mk_Trueprop
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   148
        (HOLogic.mk_eq (Free ("y", T), list_comb (Const (cname, Ts ---> T), free_ts))),
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   149
          HOLogic.mk_Trueprop (Free ("P", HOLogic.boolT))))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   150
      end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   151
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   152
    fun make_casedist ((_, (_, _, constrs)), T) =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   153
      let val prems = map (make_casedist_prem T) constrs
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   154
      in Logic.list_implies (prems, HOLogic.mk_Trueprop (Free ("P", HOLogic.boolT)))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   155
      end
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   156
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   157
  in map make_casedist
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   158
    ((hd descr) ~~ take (length (hd descr), get_rec_types descr' sorts))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   159
  end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   160
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   161
(*************** characteristic equations for primrec combinator **************)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   162
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   163
fun make_primrecs new_type_names descr sorts thy =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   164
  let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   165
    val sign = sign_of thy;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   166
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   167
    val descr' = flat descr;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   168
    val recTs = get_rec_types descr' sorts;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   169
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   170
    val rec_result_Ts = map (fn (i, _) =>
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   171
      TFree ("'t" ^ (string_of_int (i + 1)), HOLogic.termS)) descr';
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   172
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   173
    val reccomb_fn_Ts = flat (map (fn (i, (_, _, constrs)) =>
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   174
      map (fn (_, cargs) =>
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   175
        let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   176
          val recs = filter is_rec_type cargs;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   177
          val argTs = (map (typ_of_dtyp descr' sorts) cargs) @
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   178
            (map (fn r => nth_elem (dest_DtRec r, rec_result_Ts)) recs)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   179
        in argTs ---> nth_elem (i, rec_result_Ts)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   180
        end) constrs) descr');
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   181
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   182
    val rec_fns = map (uncurry (mk_Free "f"))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   183
      (reccomb_fn_Ts ~~ (1 upto (length reccomb_fn_Ts)));
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   184
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   185
    val big_reccomb_name = (space_implode "_" new_type_names) ^ "_rec";
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   186
    val reccomb_names = map (Sign.intern_const sign)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   187
      (if length descr' = 1 then [big_reccomb_name] else
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   188
        (map ((curry (op ^) (big_reccomb_name ^ "_")) o string_of_int)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   189
          (1 upto (length descr'))));
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   190
    val reccombs = map (fn ((name, T), T') => list_comb
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   191
      (Const (name, reccomb_fn_Ts @ [T] ---> T'), rec_fns))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   192
        (reccomb_names ~~ recTs ~~ rec_result_Ts);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   193
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   194
    fun make_primrec T comb_t ((ts, f::fs), (cname, cargs)) =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   195
      let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   196
        val recs = filter is_rec_type cargs;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   197
        val Ts = map (typ_of_dtyp descr' sorts) cargs;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   198
        val recTs' = map (typ_of_dtyp descr' sorts) recs;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   199
        val tnames = make_tnames Ts;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   200
        val rec_tnames = map fst (filter (is_rec_type o snd) (tnames ~~ cargs));
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   201
        val frees = map Free (tnames ~~ Ts);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   202
        val frees' = map Free (rec_tnames ~~ recTs');
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   203
        val reccombs' = map (fn (DtRec i) => nth_elem (i, reccombs)) recs
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   204
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   205
      in (ts @ [HOLogic.mk_Trueprop (HOLogic.mk_eq
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   206
        (comb_t $ list_comb (Const (cname, Ts ---> T), frees),
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   207
         list_comb (f, frees @ (map (uncurry ap) (reccombs' ~~ frees')))))], fs)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   208
      end
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   209
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   210
  in fst (foldl (fn (x, ((dt, T), comb_t)) =>
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   211
    foldl (make_primrec T comb_t) (x, #3 (snd dt)))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   212
      (([], rec_fns), descr' ~~ recTs ~~ reccombs))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   213
  end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   214
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   215
(****************** make terms of form  t_case f1 ... fn  *********************)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   216
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   217
fun make_case_combs new_type_names descr sorts thy fname =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   218
  let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   219
    val descr' = flat descr;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   220
    val recTs = get_rec_types descr' sorts;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   221
    val newTs = take (length (hd descr), recTs);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   222
    val T' = TFree ("'t", HOLogic.termS);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   223
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   224
    val case_fn_Ts = map (fn (i, (_, _, constrs)) =>
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   225
      map (fn (_, cargs) =>
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   226
        let val Ts = map (typ_of_dtyp descr' sorts) cargs
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   227
        in Ts ---> T' end) constrs) (hd descr);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   228
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   229
    val case_names = map (fn s =>
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   230
      Sign.intern_const (sign_of thy) (s ^ "_case")) new_type_names
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   231
  in
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   232
    map (fn ((name, Ts), T) => list_comb
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   233
      (Const (name, Ts @ [T] ---> T'),
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   234
        map (uncurry (mk_Free fname)) (Ts ~~ (1 upto length Ts))))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   235
          (case_names ~~ case_fn_Ts ~~ newTs)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   236
  end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   237
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   238
(**************** characteristic equations for case combinator ****************)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   239
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   240
fun make_cases new_type_names descr sorts thy =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   241
  let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   242
    val descr' = flat descr;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   243
    val recTs = get_rec_types descr' sorts;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   244
    val newTs = take (length (hd descr), recTs);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   245
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   246
    fun make_case T comb_t ((cname, cargs), f) =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   247
      let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   248
        val Ts = map (typ_of_dtyp descr' sorts) cargs;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   249
        val frees = map Free ((make_tnames Ts) ~~ Ts)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   250
      in HOLogic.mk_Trueprop (HOLogic.mk_eq
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   251
        (comb_t $ list_comb (Const (cname, Ts ---> T), frees),
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   252
         list_comb (f, frees)))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   253
      end
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   254
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   255
  in map (fn (((_, (_, _, constrs)), T), comb_t) =>
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   256
    map (make_case T comb_t) (constrs ~~ (snd (strip_comb comb_t))))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   257
      ((hd descr) ~~ newTs ~~ (make_case_combs new_type_names descr sorts thy "f"))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   258
  end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   259
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   260
(************************* distinctness of constructors ***********************)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   261
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   262
fun make_distincts new_type_names descr sorts thy =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   263
  let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   264
    val descr' = flat descr;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   265
    val recTs = get_rec_types descr' sorts;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   266
    val newTs = take (length (hd descr), recTs);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   267
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   268
    (**** number of constructors < dtK : C_i ... ~= C_j ... ****)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   269
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   270
    fun make_distincts_1 _ [] = []
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   271
      | make_distincts_1 T ((cname, cargs)::constrs) =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   272
          let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   273
            val Ts = map (typ_of_dtyp descr' sorts) cargs;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   274
            val frees = map Free ((make_tnames Ts) ~~ Ts);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   275
            val t = list_comb (Const (cname, Ts ---> T), frees);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   276
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   277
            fun make_distincts' [] = []
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   278
              | make_distincts' ((cname', cargs')::constrs') =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   279
                  let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   280
                    val Ts' = map (typ_of_dtyp descr' sorts) cargs';
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   281
                    val frees' = map Free ((map ((op ^) o (rpair "'"))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   282
                      (make_tnames Ts')) ~~ Ts');
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   283
                    val t' = list_comb (Const (cname', Ts' ---> T), frees')
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   284
                  in
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   285
                    (HOLogic.mk_Trueprop (Not $ HOLogic.mk_eq (t, t')))::
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   286
                    (HOLogic.mk_Trueprop (Not $ HOLogic.mk_eq (t', t)))::
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   287
                      (make_distincts' constrs')
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   288
                  end
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   289
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   290
          in (make_distincts' constrs) @ (make_distincts_1 T constrs)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   291
          end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   292
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   293
    (**** number of constructors >= dtK : t_ord C_i ... = i ****)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   294
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   295
    fun make_distincts_2 T tname i constrs =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   296
      let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   297
        val ord_name = Sign.intern_const (sign_of thy) (tname ^ "_ord");
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   298
        val ord_t = Const (ord_name, T --> HOLogic.natT)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   299
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   300
      in (case constrs of
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   301
          [] => [Logic.mk_implies (HOLogic.mk_Trueprop (Not $ HOLogic.mk_eq
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   302
             (ord_t $ Free ("x", T), ord_t $ Free ("y", T))),
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   303
               HOLogic.mk_Trueprop (Not $ HOLogic.mk_eq
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   304
                 (Free ("x", T), Free ("y", T))))]
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   305
        | ((cname, cargs)::constrs) =>
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   306
            let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   307
              val Ts = map (typ_of_dtyp descr' sorts) cargs;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   308
              val frees = map Free ((make_tnames Ts) ~~ Ts);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   309
            in
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   310
              (HOLogic.mk_Trueprop (HOLogic.mk_eq (ord_t $
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   311
                list_comb (Const (cname, Ts ---> T), frees), HOLogic.mk_nat i)))::
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   312
                  (make_distincts_2 T tname (i + 1) constrs)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   313
            end)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   314
      end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   315
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   316
  in map (fn (((_, (_, _, constrs)), T), tname) =>
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   317
      if length constrs < dtK then make_distincts_1 T constrs
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   318
      else make_distincts_2 T tname 0 constrs)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   319
        ((hd descr) ~~ newTs ~~ new_type_names)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   320
  end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   321
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   322
(*************************** the "split" - equations **************************)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   323
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   324
fun make_splits new_type_names descr sorts thy =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   325
  let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   326
    val descr' = flat descr;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   327
    val recTs = get_rec_types descr' sorts;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   328
    val newTs = take (length (hd descr), recTs);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   329
    val T' = TFree ("'t", HOLogic.termS);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   330
    val P = Free ("P", T' --> HOLogic.boolT);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   331
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   332
    fun make_split (((_, (_, _, constrs)), T), comb_t) =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   333
      let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   334
        val (_, fs) = strip_comb comb_t;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   335
        val used = ["P", "x"] @ (map (fst o dest_Free) fs);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   336
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   337
        fun process_constr (((cname, cargs), f), (t1s, t2s)) =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   338
          let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   339
            val Ts = map (typ_of_dtyp descr' sorts) cargs;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   340
            val frees = map Free (variantlist (make_tnames Ts, used) ~~ Ts);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   341
            val eqn = HOLogic.mk_eq (Free ("x", T),
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   342
              list_comb (Const (cname, Ts ---> T), frees));
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   343
            val P' = P $ list_comb (f, frees)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   344
          in ((foldr (fn (Free (s, T), t) => HOLogic.mk_all (s, T, t))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   345
                (frees, HOLogic.imp $ eqn $ P'))::t1s,
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   346
              (foldr (fn (Free (s, T), t) => HOLogic.mk_exists (s, T, t))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   347
                (frees, HOLogic.conj $ eqn $ (Not $ P')))::t2s)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   348
          end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   349
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   350
        val (t1s, t2s) = foldr process_constr (constrs ~~ fs, ([], []));
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   351
        val lhs = P $ (comb_t $ Free ("x", T))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   352
      in
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   353
        (HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, mk_conj t1s)),
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   354
         HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, Not $ mk_disj t2s)))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   355
      end
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   356
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   357
  in map make_split ((hd descr) ~~ newTs ~~
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   358
    (make_case_combs new_type_names descr sorts thy "f"))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   359
  end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   360
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   361
(************************ translation rules for case **************************)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   362
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   363
fun make_case_trrules new_type_names descr =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   364
  let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   365
    fun mk_asts i j ((cname, cargs)::constrs) =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   366
      let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   367
        val k = length cargs;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   368
        val xs = map (fn i => Variable ("x" ^ string_of_int i)) (i upto i + k - 1);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   369
        val t = Variable ("t" ^ string_of_int j);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   370
        val ast = Ast.mk_appl (Constant "@case1")
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   371
          [Ast.mk_appl (Constant (Sign.base_name cname)) xs, t];
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   372
        val ast' = foldr (fn (x, y) =>
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   373
          Ast.mk_appl (Constant "_abs") [x, y]) (xs, t)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   374
      in
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   375
        (case constrs of
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   376
            [] => (ast, [ast'])
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   377
          | cs => let val (ast'', asts) = mk_asts (i + k) (j + 1) cs
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   378
              in (Ast.mk_appl (Constant "@case2") [ast, ast''],
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   379
                  ast'::asts)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   380
              end)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   381
      end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   382
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   383
    fun mk_trrule ((_, (_, _, constrs)), tname) =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   384
      let val (ast, asts) = mk_asts 1 1 constrs
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   385
      in Syntax.ParsePrintRule
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   386
        (Ast.mk_appl (Constant "@case") [Variable "t", ast],
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   387
         Ast.mk_appl (Constant (tname ^ "_case"))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   388
           (asts @ [Variable "t"]))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   389
      end
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   390
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   391
  in
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   392
    map mk_trrule (hd descr ~~ new_type_names)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   393
  end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   394
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   395
(******************************* size functions *******************************)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   396
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   397
fun make_size new_type_names descr sorts thy =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   398
  let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   399
    val descr' = flat descr;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   400
    val recTs = get_rec_types descr' sorts;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   401
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   402
    val big_size_name = space_implode "_" new_type_names ^ "_size";
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   403
    val size_name = Sign.intern_const (sign_of (the (get_thy "Arith" thy))) "size";
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   404
    val size_names = replicate (length (hd descr)) size_name @
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   405
      map (Sign.intern_const (sign_of thy))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   406
        (if length (flat (tl descr)) = 1 then [big_size_name] else
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   407
          map (fn i => big_size_name ^ "_" ^ string_of_int i)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   408
            (1 upto length (flat (tl descr))));
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   409
    val size_consts = map (fn (s, T) =>
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   410
      Const (s, T --> HOLogic.natT)) (size_names ~~ recTs);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   411
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   412
    val plus_t = Const ("op +", [HOLogic.natT, HOLogic.natT] ---> HOLogic.natT);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   413
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   414
    fun make_size_eqn size_const T (cname, cargs) =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   415
      let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   416
        val recs = filter is_rec_type cargs;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   417
        val Ts = map (typ_of_dtyp descr' sorts) cargs;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   418
        val recTs = map (typ_of_dtyp descr' sorts) recs;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   419
        val tnames = make_tnames Ts;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   420
        val rec_tnames = map fst (filter (is_rec_type o snd) (tnames ~~ cargs));
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   421
        val ts = map (fn ((r, s), T) => nth_elem (dest_DtRec r, size_consts) $
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   422
          Free (s, T)) (recs ~~ rec_tnames ~~ recTs);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   423
        val t = if ts = [] then HOLogic.zero else
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   424
          foldl1 (app plus_t) (ts @ [HOLogic.mk_nat 1])
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   425
      in
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   426
        HOLogic.mk_Trueprop (HOLogic.mk_eq (size_const $
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   427
          list_comb (Const (cname, Ts ---> T), map Free (tnames ~~ Ts)), t))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   428
      end
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   429
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   430
  in
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   431
    flat (map (fn (((_, (_, _, constrs)), size_const), T) =>
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   432
      map (make_size_eqn size_const T) constrs) (descr' ~~ size_consts ~~ recTs))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   433
  end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   434
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   435
(************************* additional rules for TFL ***************************)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   436
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   437
(*---------------------------------------------------------------------------
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   438
 * Structure of case congruence theorem looks like this:
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   439
 *
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   440
 *    (M = M') 
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   441
 *    ==> (!!x1,...,xk. (M' = C1 x1..xk) ==> (f1 x1..xk = g1 x1..xk)) 
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   442
 *    ==> ... 
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   443
 *    ==> (!!x1,...,xj. (M' = Cn x1..xj) ==> (fn x1..xj = gn x1..xj)) 
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   444
 *    ==>
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   445
 *      (ty_case f1..fn M = ty_case g1..gn M')
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   446
 *---------------------------------------------------------------------------*)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   447
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   448
fun make_case_congs new_type_names descr sorts thy =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   449
  let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   450
    val case_combs = make_case_combs new_type_names descr sorts thy "f";
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   451
    val case_combs' = make_case_combs new_type_names descr sorts thy "g";
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   452
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   453
    fun mk_case_cong ((comb, comb'), (_, (_, _, constrs))) =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   454
      let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   455
        val Type ("fun", [T, _]) = fastype_of comb;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   456
        val (_, fs) = strip_comb comb;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   457
        val (_, gs) = strip_comb comb';
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   458
        val used = ["M", "M'"] @ map (fst o dest_Free) (fs @ gs);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   459
        val M = Free ("M", T);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   460
        val M' = Free ("M'", T);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   461
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   462
        fun mk_clause ((f, g), (cname, _)) =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   463
          let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   464
            val (Ts, _) = strip_type (fastype_of f);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   465
            val tnames = variantlist (make_tnames Ts, used);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   466
            val frees = map Free (tnames ~~ Ts)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   467
          in
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   468
            list_all_free (tnames ~~ Ts, Logic.mk_implies
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   469
              (HOLogic.mk_Trueprop
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   470
                (HOLogic.mk_eq (M', list_comb (Const (cname, Ts ---> T), frees))),
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   471
               HOLogic.mk_Trueprop
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   472
                (HOLogic.mk_eq (list_comb (f, frees), list_comb (g, frees)))))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   473
          end
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   474
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   475
      in
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   476
        Logic.list_implies (HOLogic.mk_Trueprop (HOLogic.mk_eq (M, M')) ::
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   477
          map mk_clause (fs ~~ gs ~~ constrs),
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   478
            HOLogic.mk_Trueprop (HOLogic.mk_eq (comb $ M, comb' $ M')))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   479
      end
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   480
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   481
  in
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   482
    map mk_case_cong (case_combs ~~ case_combs' ~~ hd descr)
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
(*---------------------------------------------------------------------------
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   486
 * Structure of exhaustion theorem looks like this:
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   487
 *
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   488
 *    !v. (? y1..yi. v = C1 y1..yi) | ... | (? y1..yj. v = Cn y1..yj)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   489
 *---------------------------------------------------------------------------*)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   490
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   491
fun make_nchotomys descr sorts =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   492
  let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   493
    val descr' = flat descr;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   494
    val recTs = get_rec_types descr' sorts;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   495
    val newTs = take (length (hd descr), recTs);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   496
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   497
    fun mk_eqn T (cname, cargs) =
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   498
      let
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   499
        val Ts = map (typ_of_dtyp descr' sorts) cargs;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   500
        val tnames = variantlist (make_tnames Ts, ["v"]);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   501
        val frees = tnames ~~ Ts
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   502
      in
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   503
        foldr (fn ((s, T'), t) => HOLogic.mk_exists (s, T', t))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   504
          (frees, HOLogic.mk_eq (Free ("v", T),
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   505
            list_comb (Const (cname, Ts ---> T), map Free frees)))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   506
      end
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   507
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   508
  in map (fn ((_, (_, _, constrs)), T) =>
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   509
    HOLogic.mk_Trueprop (HOLogic.mk_all ("v", T, mk_disj (map (mk_eqn T) constrs))))
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   510
      (hd descr ~~ newTs)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   511
  end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   512
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   513
end;