src/HOL/Tools/datatype_aux.ML
author berghofe
Fri Jul 24 12:50:06 1998 +0200 (1998-07-24)
changeset 5177 0d3a168e4d44
child 5661 6ecb6ea25f19
permissions -rw-r--r--
New datatype definition package
berghofe@5177
     1
(*  Title:      HOL/Tools/datatype_aux.ML
berghofe@5177
     2
    ID:         $Id$
berghofe@5177
     3
    Author:     Stefan Berghofer
berghofe@5177
     4
    Copyright   1998  TU Muenchen
berghofe@5177
     5
berghofe@5177
     6
Auxiliary functions for defining datatypes
berghofe@5177
     7
*)
berghofe@5177
     8
berghofe@5177
     9
signature DATATYPE_AUX =
berghofe@5177
    10
sig
berghofe@5177
    11
  val foldl1 : ('a * 'a -> 'a) -> 'a list -> 'a
berghofe@5177
    12
berghofe@5177
    13
  val get_thy : string -> theory -> theory option
berghofe@5177
    14
berghofe@5177
    15
  val store_thmss : string -> string list -> thm list list -> theory -> theory
berghofe@5177
    16
  val store_thms : string -> string list -> thm list -> theory -> theory
berghofe@5177
    17
berghofe@5177
    18
  val split_conj_thm : thm -> thm list
berghofe@5177
    19
  val mk_conj : term list -> term
berghofe@5177
    20
  val mk_disj : term list -> term
berghofe@5177
    21
berghofe@5177
    22
  val indtac : thm -> int -> tactic
berghofe@5177
    23
  val exh_tac : (string -> thm) -> int -> tactic
berghofe@5177
    24
berghofe@5177
    25
  datatype dtyp =
berghofe@5177
    26
      DtTFree of string
berghofe@5177
    27
    | DtType of string * (dtyp list)
berghofe@5177
    28
    | DtRec of int;
berghofe@5177
    29
berghofe@5177
    30
  type datatype_info
berghofe@5177
    31
berghofe@5177
    32
  val dtyp_of_typ : (string * string list) list -> typ -> dtyp
berghofe@5177
    33
  val mk_Free : string -> typ -> int -> term
berghofe@5177
    34
  val is_rec_type : dtyp -> bool
berghofe@5177
    35
  val typ_of_dtyp : (int * (string * dtyp list *
berghofe@5177
    36
    (string * dtyp list) list)) list -> (string * sort) list -> dtyp -> typ
berghofe@5177
    37
  val dest_DtTFree : dtyp -> string
berghofe@5177
    38
  val dest_DtRec : dtyp -> int
berghofe@5177
    39
  val dest_TFree : typ -> string
berghofe@5177
    40
  val dest_conj : term -> term list
berghofe@5177
    41
  val get_nonrec_types : (int * (string * dtyp list *
berghofe@5177
    42
    (string * dtyp list) list)) list -> (string * sort) list -> typ list
berghofe@5177
    43
  val get_rec_types : (int * (string * dtyp list *
berghofe@5177
    44
    (string * dtyp list) list)) list -> (string * sort) list -> typ list
berghofe@5177
    45
  val check_nonempty : (int * (string * dtyp list *
berghofe@5177
    46
    (string * dtyp list) list)) list list -> unit
berghofe@5177
    47
  val unfold_datatypes : 
berghofe@5177
    48
    datatype_info Symtab.table ->
berghofe@5177
    49
      (int * (string * dtyp list *
berghofe@5177
    50
        (string * dtyp list) list)) list -> int ->
berghofe@5177
    51
          (int * (string * dtyp list *
berghofe@5177
    52
            (string * dtyp list) list)) list list * int
berghofe@5177
    53
end;
berghofe@5177
    54
berghofe@5177
    55
structure DatatypeAux : DATATYPE_AUX =
berghofe@5177
    56
struct
berghofe@5177
    57
berghofe@5177
    58
(* FIXME: move to library ? *)
berghofe@5177
    59
fun foldl1 f (x::xs) = foldl f (x, xs);
berghofe@5177
    60
berghofe@5177
    61
fun get_thy name thy = find_first
berghofe@5177
    62
  (equal name o Sign.name_of o sign_of) (ancestors_of thy);
berghofe@5177
    63
berghofe@5177
    64
(* store theorems in theory *)
berghofe@5177
    65
berghofe@5177
    66
fun store_thmss label tnames thmss thy =
berghofe@5177
    67
  foldr (fn ((tname, thms), thy') => thy' |>
berghofe@5177
    68
    (if length tnames = 1 then I else Theory.add_path tname) |>
berghofe@5177
    69
    PureThy.add_tthmss [((label, map Attribute.tthm_of thms), [])] |>
berghofe@5177
    70
    (if length tnames = 1 then I else Theory.parent_path))
berghofe@5177
    71
    (tnames ~~ thmss, thy);
berghofe@5177
    72
berghofe@5177
    73
fun store_thms label tnames thms thy =
berghofe@5177
    74
  foldr (fn ((tname, thm), thy') => thy' |>
berghofe@5177
    75
    (if length tnames = 1 then I else Theory.add_path tname) |>
berghofe@5177
    76
    PureThy.add_tthms [((label, Attribute.tthm_of thm), [])] |>
berghofe@5177
    77
    (if length tnames = 1 then I else Theory.parent_path))
berghofe@5177
    78
    (tnames ~~ thms, thy);
berghofe@5177
    79
berghofe@5177
    80
(* split theorem thm_1 & ... & thm_n into n theorems *)
berghofe@5177
    81
berghofe@5177
    82
fun split_conj_thm th =
berghofe@5177
    83
  ((th RS conjunct1)::(split_conj_thm (th RS conjunct2))) handle _ => [th];
berghofe@5177
    84
berghofe@5177
    85
val mk_conj = foldr1 (HOLogic.mk_binop "op &");
berghofe@5177
    86
val mk_disj = foldr1 (HOLogic.mk_binop "op |");
berghofe@5177
    87
berghofe@5177
    88
fun dest_conj (Const ("op &", _) $ t $ t') = t::(dest_conj t')
berghofe@5177
    89
  | dest_conj t = [t];
berghofe@5177
    90
berghofe@5177
    91
(* instantiate induction rule *)
berghofe@5177
    92
berghofe@5177
    93
fun indtac indrule i st =
berghofe@5177
    94
  let
berghofe@5177
    95
    val ts = dest_conj (HOLogic.dest_Trueprop (concl_of indrule));
berghofe@5177
    96
    val ts' = dest_conj (HOLogic.dest_Trueprop
berghofe@5177
    97
      (Logic.strip_imp_concl (nth_elem (i - 1, prems_of st))));
berghofe@5177
    98
    val getP = if can HOLogic.dest_imp (hd ts) then
berghofe@5177
    99
      (apfst Some) o HOLogic.dest_imp else pair None;
berghofe@5177
   100
    fun abstr (t1, t2) = (case t1 of
berghofe@5177
   101
        None => let val [Free (s, T)] = add_term_frees (t2, [])
berghofe@5177
   102
          in absfree (s, T, t2) end
berghofe@5177
   103
      | Some (_ $ t' $ _) => Abs ("x", fastype_of t', abstract_over (t', t2)))
berghofe@5177
   104
    val cert = cterm_of (sign_of_thm st);
berghofe@5177
   105
    val Ps = map (cert o head_of o snd o getP) ts;
berghofe@5177
   106
    val indrule' = cterm_instantiate (Ps ~~
berghofe@5177
   107
      (map (cert o abstr o getP) ts')) indrule
berghofe@5177
   108
  in
berghofe@5177
   109
    rtac indrule' i st
berghofe@5177
   110
  end;
berghofe@5177
   111
berghofe@5177
   112
(* perform exhaustive case analysis on last parameter of subgoal i *)
berghofe@5177
   113
berghofe@5177
   114
fun exh_tac exh_thm_of i state =
berghofe@5177
   115
  let
berghofe@5177
   116
    val sg = sign_of_thm state;
berghofe@5177
   117
    val prem = nth_elem (i - 1, prems_of state);
berghofe@5177
   118
    val params = Logic.strip_params prem;
berghofe@5177
   119
    val (_, Type (tname, _)) = hd (rev params);
berghofe@5177
   120
    val exhaustion = lift_rule (state, i) (exh_thm_of tname);
berghofe@5177
   121
    val prem' = hd (prems_of exhaustion);
berghofe@5177
   122
    val _ $ (_ $ lhs $ _) = hd (rev (Logic.strip_assums_hyp prem'));
berghofe@5177
   123
    val exhaustion' = cterm_instantiate [(cterm_of sg (head_of lhs),
berghofe@5177
   124
      cterm_of sg (foldr (fn ((_, T), t) => Abs ("z", T, t))
berghofe@5177
   125
        (params, Bound 0)))] exhaustion
berghofe@5177
   126
  in compose_tac (false, exhaustion', nprems_of exhaustion) i state
berghofe@5177
   127
  end;
berghofe@5177
   128
berghofe@5177
   129
(********************** Internal description of datatypes *********************)
berghofe@5177
   130
berghofe@5177
   131
datatype dtyp =
berghofe@5177
   132
    DtTFree of string
berghofe@5177
   133
  | DtType of string * (dtyp list)
berghofe@5177
   134
  | DtRec of int;
berghofe@5177
   135
berghofe@5177
   136
(* information about datatypes *)
berghofe@5177
   137
berghofe@5177
   138
type datatype_info =
berghofe@5177
   139
  {index : int,
berghofe@5177
   140
   descr : (int * (string * dtyp list *
berghofe@5177
   141
     (string * dtyp list) list)) list,
berghofe@5177
   142
   rec_names : string list,
berghofe@5177
   143
   rec_rewrites : thm list,
berghofe@5177
   144
   case_name : string,
berghofe@5177
   145
   case_rewrites : thm list,
berghofe@5177
   146
   induction : thm,
berghofe@5177
   147
   exhaustion : thm,
berghofe@5177
   148
   distinct : thm list,
berghofe@5177
   149
   inject : thm list,
berghofe@5177
   150
   nchotomy : thm,
berghofe@5177
   151
   case_cong : thm};
berghofe@5177
   152
berghofe@5177
   153
fun mk_Free s T i = Free (s ^ (string_of_int i), T);
berghofe@5177
   154
berghofe@5177
   155
fun subst_DtTFree _ substs (T as (DtTFree name)) =
berghofe@5177
   156
      (case assoc (substs, name) of
berghofe@5177
   157
         None => T
berghofe@5177
   158
       | Some U => U)
berghofe@5177
   159
  | subst_DtTFree i substs (DtType (name, ts)) =
berghofe@5177
   160
      DtType (name, map (subst_DtTFree i substs) ts)
berghofe@5177
   161
  | subst_DtTFree i _ (DtRec j) = DtRec (i + j);
berghofe@5177
   162
berghofe@5177
   163
fun dest_DtTFree (DtTFree a) = a;
berghofe@5177
   164
fun dest_DtRec (DtRec i) = i;
berghofe@5177
   165
berghofe@5177
   166
fun is_rec_type (DtType (_, dts)) = exists is_rec_type dts
berghofe@5177
   167
  | is_rec_type (DtRec _) = true
berghofe@5177
   168
  | is_rec_type _ = false;
berghofe@5177
   169
berghofe@5177
   170
fun dest_TFree (TFree (n, _)) = n;
berghofe@5177
   171
berghofe@5177
   172
fun dtyp_of_typ _ (TFree (n, _)) = DtTFree n
berghofe@5177
   173
  | dtyp_of_typ _ (TVar _) = error "Illegal schematic type variable(s)"
berghofe@5177
   174
  | dtyp_of_typ new_dts (Type (tname, Ts)) =
berghofe@5177
   175
      (case assoc (new_dts, tname) of
berghofe@5177
   176
         None => DtType (tname, map (dtyp_of_typ new_dts) Ts)
berghofe@5177
   177
       | Some vs => if map (try dest_TFree) Ts = map Some vs then
berghofe@5177
   178
             DtRec (find_index (curry op = tname o fst) new_dts)
berghofe@5177
   179
           else error ("Illegal occurence of recursive type " ^ tname));
berghofe@5177
   180
berghofe@5177
   181
fun typ_of_dtyp descr sorts (DtTFree a) = TFree (a, the (assoc (sorts, a)))
berghofe@5177
   182
  | typ_of_dtyp descr sorts (DtRec i) =
berghofe@5177
   183
      let val (s, ds, _) = the (assoc (descr, i))
berghofe@5177
   184
      in Type (s, map (typ_of_dtyp descr sorts) ds) end
berghofe@5177
   185
  | typ_of_dtyp descr sorts (DtType (s, ds)) =
berghofe@5177
   186
      Type (s, map (typ_of_dtyp descr sorts) ds);
berghofe@5177
   187
berghofe@5177
   188
(* find all non-recursive types in datatype description *)
berghofe@5177
   189
berghofe@5177
   190
fun get_nonrec_types descr sorts =
berghofe@5177
   191
  let fun add (Ts, T as DtTFree _) = T ins Ts
berghofe@5177
   192
        | add (Ts, T as DtType _) = T ins Ts
berghofe@5177
   193
        | add (Ts, _) = Ts
berghofe@5177
   194
  in map (typ_of_dtyp descr sorts) (foldl (fn (Ts, (_, (_, _, constrs))) =>
berghofe@5177
   195
    foldl (fn (Ts', (_, cargs)) =>
berghofe@5177
   196
      foldl add (Ts', cargs)) (Ts, constrs)) ([], descr))
berghofe@5177
   197
  end;
berghofe@5177
   198
berghofe@5177
   199
(* get all recursive types in datatype description *)
berghofe@5177
   200
berghofe@5177
   201
fun get_rec_types descr sorts = map (fn (_ , (s, ds, _)) =>
berghofe@5177
   202
  Type (s, map (typ_of_dtyp descr sorts) ds)) descr;
berghofe@5177
   203
berghofe@5177
   204
(* nonemptiness check for datatypes *)
berghofe@5177
   205
berghofe@5177
   206
fun check_nonempty descr =
berghofe@5177
   207
  let
berghofe@5177
   208
    val descr' = flat descr;
berghofe@5177
   209
    fun is_nonempty_dt is i =
berghofe@5177
   210
      let
berghofe@5177
   211
        val (_, _, constrs) = the (assoc (descr', i));
berghofe@5177
   212
        fun arg_nonempty (DtRec i) = if i mem is then false
berghofe@5177
   213
              else is_nonempty_dt (i::is) i
berghofe@5177
   214
          | arg_nonempty _ = true;
berghofe@5177
   215
      in exists ((forall arg_nonempty) o snd) constrs
berghofe@5177
   216
      end
berghofe@5177
   217
  in assert_all (fn (i, _) => is_nonempty_dt [i] i) (hd descr)
berghofe@5177
   218
    (fn (_, (s, _, _)) => "Nonemptiness check failed for datatype " ^ s)
berghofe@5177
   219
  end;
berghofe@5177
   220
berghofe@5177
   221
(* unfold a list of mutually recursive datatype specifications *)
berghofe@5177
   222
(* all types of the form DtType (dt_name, [..., DtRec _, ...]) *)
berghofe@5177
   223
(* need to be unfolded                                         *)
berghofe@5177
   224
berghofe@5177
   225
fun unfold_datatypes (dt_info : datatype_info Symtab.table) descr i =
berghofe@5177
   226
  let
berghofe@5177
   227
    fun get_dt_descr i tname dts =
berghofe@5177
   228
      (case Symtab.lookup (dt_info, tname) of
berghofe@5177
   229
         None => error (tname ^ " is not a datatype - can't use it in\
berghofe@5177
   230
           \ indirect recursion")
berghofe@5177
   231
       | (Some {index, descr, ...}) =>
berghofe@5177
   232
           let val (_, vars, _) = the (assoc (descr, index));
berghofe@5177
   233
               val subst = ((map dest_DtTFree vars) ~~ dts) handle _ =>
berghofe@5177
   234
                 error ("Type constructor " ^ tname ^ " used with wrong\
berghofe@5177
   235
                  \ number of arguments")
berghofe@5177
   236
           in (i + index, map (fn (j, (tn, args, cs)) => (i + j,
berghofe@5177
   237
             (tn, map (subst_DtTFree i subst) args,
berghofe@5177
   238
              map (apsnd (map (subst_DtTFree i subst))) cs))) descr)
berghofe@5177
   239
           end);
berghofe@5177
   240
berghofe@5177
   241
    (* unfold a single constructor argument *)
berghofe@5177
   242
berghofe@5177
   243
    fun unfold_arg ((i, Ts, descrs), T as (DtType (tname, dts))) =
berghofe@5177
   244
          if is_rec_type T then
berghofe@5177
   245
            let val (index, descr) = get_dt_descr i tname dts;
berghofe@5177
   246
                val (descr', i') = unfold_datatypes dt_info descr (i + length descr)
berghofe@5177
   247
            in (i', Ts @ [DtRec index], descrs @ descr') end
berghofe@5177
   248
          else (i, Ts @ [T], descrs)
berghofe@5177
   249
      | unfold_arg ((i, Ts, descrs), T) = (i, Ts @ [T], descrs);
berghofe@5177
   250
berghofe@5177
   251
    (* unfold a constructor *)
berghofe@5177
   252
berghofe@5177
   253
    fun unfold_constr ((i, constrs, descrs), (cname, cargs)) =
berghofe@5177
   254
      let val (i', cargs', descrs') = foldl unfold_arg ((i, [], descrs), cargs)
berghofe@5177
   255
      in (i', constrs @ [(cname, cargs')], descrs') end;
berghofe@5177
   256
berghofe@5177
   257
    (* unfold a single datatype *)
berghofe@5177
   258
berghofe@5177
   259
    fun unfold_datatype ((i, dtypes, descrs), (j, (tname, tvars, constrs))) =
berghofe@5177
   260
      let val (i', constrs', descrs') =
berghofe@5177
   261
        foldl unfold_constr ((i, [], descrs), constrs)
berghofe@5177
   262
      in (i', dtypes @ [(j, (tname, tvars, constrs'))], descrs')
berghofe@5177
   263
      end;
berghofe@5177
   264
berghofe@5177
   265
    val (i', descr', descrs) = foldl unfold_datatype ((i, [],[]), descr);
berghofe@5177
   266
berghofe@5177
   267
  in (descr' :: descrs, i') end;
berghofe@5177
   268
berghofe@5177
   269
end;