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