src/HOL/Tools/datatype_aux.ML
author wenzelm
Mon Oct 02 14:21:12 2000 +0200 (2000-10-02)
changeset 10120 0f315aeee16e
parent 9740 1c5b0f27de56
child 11539 0f17da240450
permissions -rw-r--r--
info: weak_case_cong;
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@5661
    11
  val quiet_mode : bool ref
berghofe@5661
    12
  val message : string -> unit
berghofe@5661
    13
  
berghofe@5177
    14
  val foldl1 : ('a * 'a -> 'a) -> 'a list -> 'a
berghofe@5177
    15
berghofe@5661
    16
  val add_path : bool -> string -> theory -> theory
berghofe@5661
    17
  val parent_path : bool -> theory -> theory
berghofe@5661
    18
wenzelm@8435
    19
  val store_thmss : string -> string list -> thm list list -> theory -> theory * thm list list
wenzelm@8435
    20
  val store_thms_atts : string -> string list -> theory attribute list list -> thm list
wenzelm@8435
    21
    -> theory -> theory * thm list
wenzelm@8435
    22
  val store_thms : string -> string list -> thm list -> theory -> theory * thm list
berghofe@5177
    23
berghofe@5177
    24
  val split_conj_thm : thm -> thm list
berghofe@5177
    25
  val mk_conj : term list -> term
berghofe@5177
    26
  val mk_disj : term list -> term
berghofe@5177
    27
berghofe@5177
    28
  val indtac : thm -> int -> tactic
berghofe@5177
    29
  val exh_tac : (string -> thm) -> int -> tactic
berghofe@5177
    30
berghofe@7015
    31
  datatype simproc_dist = QuickAndDirty
berghofe@7015
    32
                        | FewConstrs of thm list
berghofe@7015
    33
                        | ManyConstrs of thm * simpset;
berghofe@7015
    34
berghofe@5177
    35
  datatype dtyp =
berghofe@5177
    36
      DtTFree of string
berghofe@5177
    37
    | DtType of string * (dtyp list)
berghofe@5177
    38
    | DtRec of int;
wenzelm@8404
    39
  type descr
berghofe@5177
    40
  type datatype_info
berghofe@5177
    41
berghofe@7015
    42
  exception Datatype
berghofe@9740
    43
  val name_of_typ : typ -> string
berghofe@5177
    44
  val dtyp_of_typ : (string * string list) list -> typ -> dtyp
berghofe@5177
    45
  val mk_Free : string -> typ -> int -> term
berghofe@5177
    46
  val is_rec_type : dtyp -> bool
berghofe@5177
    47
  val typ_of_dtyp : (int * (string * dtyp list *
berghofe@5177
    48
    (string * dtyp list) list)) list -> (string * sort) list -> dtyp -> typ
berghofe@5177
    49
  val dest_DtTFree : dtyp -> string
berghofe@5177
    50
  val dest_DtRec : dtyp -> int
berghofe@5177
    51
  val dest_TFree : typ -> string
berghofe@5177
    52
  val get_nonrec_types : (int * (string * dtyp list *
berghofe@5177
    53
    (string * dtyp list) list)) list -> (string * sort) list -> typ list
berghofe@7015
    54
  val get_branching_types : (int * (string * dtyp list *
berghofe@7015
    55
    (string * dtyp list) list)) list -> (string * sort) list -> typ list
berghofe@5177
    56
  val get_rec_types : (int * (string * dtyp list *
berghofe@5177
    57
    (string * dtyp list) list)) list -> (string * sort) list -> typ list
berghofe@5177
    58
  val check_nonempty : (int * (string * dtyp list *
berghofe@5177
    59
    (string * dtyp list) list)) list list -> unit
berghofe@5177
    60
  val unfold_datatypes : 
berghofe@7015
    61
    Sign.sg -> (int * (string * dtyp list * (string * dtyp list) list)) list ->
berghofe@7015
    62
      (string * sort) list -> datatype_info Symtab.table ->
berghofe@7015
    63
        (int * (string * dtyp list * (string * dtyp list) list)) list -> int ->
berghofe@5177
    64
          (int * (string * dtyp list *
berghofe@5177
    65
            (string * dtyp list) list)) list list * int
berghofe@5177
    66
end;
berghofe@5177
    67
berghofe@5177
    68
structure DatatypeAux : DATATYPE_AUX =
berghofe@5177
    69
struct
berghofe@5177
    70
berghofe@5661
    71
val quiet_mode = ref false;
berghofe@5661
    72
fun message s = if !quiet_mode then () else writeln s;
berghofe@5661
    73
berghofe@5177
    74
(* FIXME: move to library ? *)
berghofe@5177
    75
fun foldl1 f (x::xs) = foldl f (x, xs);
berghofe@5177
    76
berghofe@5661
    77
fun add_path flat_names s = if flat_names then I else Theory.add_path s;
berghofe@5661
    78
fun parent_path flat_names = if flat_names then I else Theory.parent_path;
berghofe@5661
    79
wenzelm@8435
    80
berghofe@5177
    81
(* store theorems in theory *)
berghofe@5177
    82
berghofe@5177
    83
fun store_thmss label tnames thmss thy =
wenzelm@8435
    84
  (thy, tnames ~~ thmss) |>
wenzelm@8435
    85
  foldl_map (fn (thy', (tname, thms)) => thy' |>
berghofe@5661
    86
    Theory.add_path tname |>
wenzelm@8435
    87
    (apsnd hd o PureThy.add_thmss [((label, thms), [])]) |>>
wenzelm@8435
    88
    Theory.parent_path);
berghofe@5177
    89
wenzelm@8435
    90
fun store_thms_atts label tnames attss thms thy =
wenzelm@8435
    91
  (thy, tnames ~~ attss ~~ thms) |>
wenzelm@8435
    92
  foldl_map (fn (thy', ((tname, atts), thm)) => thy' |>
berghofe@5661
    93
    Theory.add_path tname |>
wenzelm@8435
    94
    (apsnd hd o PureThy.add_thms [((label, thm), atts)]) |>>
wenzelm@8435
    95
    Theory.parent_path);
wenzelm@8435
    96
wenzelm@8435
    97
fun store_thms label tnames = store_thms_atts label tnames (replicate (length tnames) []);
wenzelm@8435
    98
berghofe@5177
    99
berghofe@5177
   100
(* split theorem thm_1 & ... & thm_n into n theorems *)
berghofe@5177
   101
berghofe@5177
   102
fun split_conj_thm th =
berghofe@7015
   103
  ((th RS conjunct1)::(split_conj_thm (th RS conjunct2))) handle THM _ => [th];
berghofe@5177
   104
berghofe@5177
   105
val mk_conj = foldr1 (HOLogic.mk_binop "op &");
berghofe@5177
   106
val mk_disj = foldr1 (HOLogic.mk_binop "op |");
berghofe@5177
   107
berghofe@5177
   108
berghofe@5177
   109
(* instantiate induction rule *)
berghofe@5177
   110
berghofe@5177
   111
fun indtac indrule i st =
berghofe@5177
   112
  let
wenzelm@8305
   113
    val ts = HOLogic.dest_conj (HOLogic.dest_Trueprop (concl_of indrule));
wenzelm@8305
   114
    val ts' = HOLogic.dest_conj (HOLogic.dest_Trueprop
berghofe@5177
   115
      (Logic.strip_imp_concl (nth_elem (i - 1, prems_of st))));
berghofe@5177
   116
    val getP = if can HOLogic.dest_imp (hd ts) then
berghofe@5177
   117
      (apfst Some) o HOLogic.dest_imp else pair None;
berghofe@5177
   118
    fun abstr (t1, t2) = (case t1 of
berghofe@5177
   119
        None => let val [Free (s, T)] = add_term_frees (t2, [])
berghofe@5177
   120
          in absfree (s, T, t2) end
berghofe@5177
   121
      | Some (_ $ t' $ _) => Abs ("x", fastype_of t', abstract_over (t', t2)))
wenzelm@6394
   122
    val cert = cterm_of (Thm.sign_of_thm st);
berghofe@5177
   123
    val Ps = map (cert o head_of o snd o getP) ts;
berghofe@5177
   124
    val indrule' = cterm_instantiate (Ps ~~
berghofe@5177
   125
      (map (cert o abstr o getP) ts')) indrule
berghofe@5177
   126
  in
berghofe@5177
   127
    rtac indrule' i st
berghofe@5177
   128
  end;
berghofe@5177
   129
berghofe@5177
   130
(* perform exhaustive case analysis on last parameter of subgoal i *)
berghofe@5177
   131
berghofe@5177
   132
fun exh_tac exh_thm_of i state =
berghofe@5177
   133
  let
wenzelm@6394
   134
    val sg = Thm.sign_of_thm state;
berghofe@5177
   135
    val prem = nth_elem (i - 1, prems_of state);
berghofe@5177
   136
    val params = Logic.strip_params prem;
berghofe@5177
   137
    val (_, Type (tname, _)) = hd (rev params);
berghofe@5177
   138
    val exhaustion = lift_rule (state, i) (exh_thm_of tname);
berghofe@5177
   139
    val prem' = hd (prems_of exhaustion);
berghofe@5177
   140
    val _ $ (_ $ lhs $ _) = hd (rev (Logic.strip_assums_hyp prem'));
berghofe@5177
   141
    val exhaustion' = cterm_instantiate [(cterm_of sg (head_of lhs),
berghofe@5177
   142
      cterm_of sg (foldr (fn ((_, T), t) => Abs ("z", T, t))
berghofe@5177
   143
        (params, Bound 0)))] exhaustion
berghofe@5177
   144
  in compose_tac (false, exhaustion', nprems_of exhaustion) i state
berghofe@5177
   145
  end;
berghofe@5177
   146
berghofe@7015
   147
(* handling of distinctness theorems *)
berghofe@7015
   148
berghofe@7015
   149
datatype simproc_dist = QuickAndDirty
berghofe@7015
   150
                      | FewConstrs of thm list
berghofe@7015
   151
                      | ManyConstrs of thm * simpset;
berghofe@7015
   152
berghofe@5177
   153
(********************** Internal description of datatypes *********************)
berghofe@5177
   154
berghofe@5177
   155
datatype dtyp =
berghofe@5177
   156
    DtTFree of string
berghofe@5177
   157
  | DtType of string * (dtyp list)
berghofe@5177
   158
  | DtRec of int;
berghofe@5177
   159
berghofe@5177
   160
(* information about datatypes *)
berghofe@5177
   161
wenzelm@8404
   162
type descr = (int * (string * dtyp list * (string * dtyp list) list)) list;
wenzelm@8404
   163
berghofe@5177
   164
type datatype_info =
berghofe@5177
   165
  {index : int,
wenzelm@8404
   166
   descr : descr,
berghofe@5177
   167
   rec_names : string list,
berghofe@5177
   168
   rec_rewrites : thm list,
berghofe@5177
   169
   case_name : string,
berghofe@5177
   170
   case_rewrites : thm list,
berghofe@5177
   171
   induction : thm,
berghofe@5177
   172
   exhaustion : thm,
berghofe@7015
   173
   distinct : simproc_dist,
berghofe@5177
   174
   inject : thm list,
berghofe@5177
   175
   nchotomy : thm,
wenzelm@10120
   176
   case_cong : thm,
wenzelm@10120
   177
   weak_case_cong : thm};
berghofe@5177
   178
berghofe@5177
   179
fun mk_Free s T i = Free (s ^ (string_of_int i), T);
berghofe@5177
   180
berghofe@5177
   181
fun subst_DtTFree _ substs (T as (DtTFree name)) =
berghofe@5177
   182
      (case assoc (substs, name) of
berghofe@5177
   183
         None => T
berghofe@5177
   184
       | Some U => U)
berghofe@5177
   185
  | subst_DtTFree i substs (DtType (name, ts)) =
berghofe@5177
   186
      DtType (name, map (subst_DtTFree i substs) ts)
berghofe@5177
   187
  | subst_DtTFree i _ (DtRec j) = DtRec (i + j);
berghofe@5177
   188
berghofe@7015
   189
exception Datatype;
berghofe@7015
   190
berghofe@7015
   191
fun dest_DtTFree (DtTFree a) = a
berghofe@7015
   192
  | dest_DtTFree _ = raise Datatype;
berghofe@7015
   193
berghofe@7015
   194
fun dest_DtRec (DtRec i) = i
berghofe@7015
   195
  | dest_DtRec _ = raise Datatype;
berghofe@5177
   196
berghofe@5177
   197
fun is_rec_type (DtType (_, dts)) = exists is_rec_type dts
berghofe@5177
   198
  | is_rec_type (DtRec _) = true
berghofe@5177
   199
  | is_rec_type _ = false;
berghofe@5177
   200
berghofe@5177
   201
fun dest_TFree (TFree (n, _)) = n;
berghofe@5177
   202
berghofe@9740
   203
fun name_of_typ (Type (s, Ts)) = space_implode "_"
berghofe@9740
   204
      (filter (not o equal "") (map name_of_typ Ts) @ [Sign.base_name s])
berghofe@9740
   205
  | name_of_typ _ = "";
berghofe@9740
   206
berghofe@5177
   207
fun dtyp_of_typ _ (TFree (n, _)) = DtTFree n
berghofe@5177
   208
  | dtyp_of_typ _ (TVar _) = error "Illegal schematic type variable(s)"
berghofe@5177
   209
  | dtyp_of_typ new_dts (Type (tname, Ts)) =
berghofe@5177
   210
      (case assoc (new_dts, tname) of
berghofe@5177
   211
         None => DtType (tname, map (dtyp_of_typ new_dts) Ts)
berghofe@5177
   212
       | Some vs => if map (try dest_TFree) Ts = map Some vs then
berghofe@5177
   213
             DtRec (find_index (curry op = tname o fst) new_dts)
berghofe@5177
   214
           else error ("Illegal occurence of recursive type " ^ tname));
berghofe@5177
   215
berghofe@5177
   216
fun typ_of_dtyp descr sorts (DtTFree a) = TFree (a, the (assoc (sorts, a)))
berghofe@5177
   217
  | typ_of_dtyp descr sorts (DtRec i) =
berghofe@5177
   218
      let val (s, ds, _) = the (assoc (descr, i))
berghofe@5177
   219
      in Type (s, map (typ_of_dtyp descr sorts) ds) end
berghofe@5177
   220
  | typ_of_dtyp descr sorts (DtType (s, ds)) =
berghofe@5177
   221
      Type (s, map (typ_of_dtyp descr sorts) ds);
berghofe@5177
   222
berghofe@5177
   223
(* find all non-recursive types in datatype description *)
berghofe@5177
   224
berghofe@5177
   225
fun get_nonrec_types descr sorts =
berghofe@5177
   226
  let fun add (Ts, T as DtTFree _) = T ins Ts
berghofe@7015
   227
        | add (Ts, T as DtType ("fun", [_, DtRec _])) = Ts
berghofe@5177
   228
        | add (Ts, T as DtType _) = T ins Ts
berghofe@5177
   229
        | add (Ts, _) = Ts
berghofe@5177
   230
  in map (typ_of_dtyp descr sorts) (foldl (fn (Ts, (_, (_, _, constrs))) =>
berghofe@5177
   231
    foldl (fn (Ts', (_, cargs)) =>
berghofe@5177
   232
      foldl add (Ts', cargs)) (Ts, constrs)) ([], descr))
berghofe@5177
   233
  end;
berghofe@5177
   234
berghofe@5177
   235
(* get all recursive types in datatype description *)
berghofe@5177
   236
berghofe@5177
   237
fun get_rec_types descr sorts = map (fn (_ , (s, ds, _)) =>
berghofe@5177
   238
  Type (s, map (typ_of_dtyp descr sorts) ds)) descr;
berghofe@5177
   239
berghofe@7015
   240
(* get all branching types *)
berghofe@7015
   241
berghofe@7015
   242
fun get_branching_types descr sorts = 
berghofe@7015
   243
  let fun add (Ts, DtType ("fun", [T, DtRec _])) = T ins Ts
berghofe@7015
   244
        | add (Ts, _) = Ts
berghofe@7015
   245
  in map (typ_of_dtyp descr sorts) (foldl (fn (Ts, (_, (_, _, constrs))) =>
berghofe@7015
   246
    foldl (fn (Ts', (_, cargs)) =>
berghofe@7015
   247
      foldl add (Ts', cargs)) (Ts, constrs)) ([], descr))
berghofe@7015
   248
  end;
berghofe@7015
   249
berghofe@5177
   250
(* nonemptiness check for datatypes *)
berghofe@5177
   251
berghofe@5177
   252
fun check_nonempty descr =
berghofe@5177
   253
  let
berghofe@5177
   254
    val descr' = flat descr;
berghofe@5177
   255
    fun is_nonempty_dt is i =
berghofe@5177
   256
      let
berghofe@5177
   257
        val (_, _, constrs) = the (assoc (descr', i));
berghofe@5177
   258
        fun arg_nonempty (DtRec i) = if i mem is then false
berghofe@5177
   259
              else is_nonempty_dt (i::is) i
berghofe@7015
   260
          | arg_nonempty (DtType ("fun", [_, T])) = arg_nonempty T
berghofe@5177
   261
          | arg_nonempty _ = true;
berghofe@5177
   262
      in exists ((forall arg_nonempty) o snd) constrs
berghofe@5177
   263
      end
berghofe@5177
   264
  in assert_all (fn (i, _) => is_nonempty_dt [i] i) (hd descr)
berghofe@5177
   265
    (fn (_, (s, _, _)) => "Nonemptiness check failed for datatype " ^ s)
berghofe@5177
   266
  end;
berghofe@5177
   267
berghofe@5177
   268
(* unfold a list of mutually recursive datatype specifications *)
berghofe@5177
   269
(* all types of the form DtType (dt_name, [..., DtRec _, ...]) *)
berghofe@5177
   270
(* need to be unfolded                                         *)
berghofe@5177
   271
berghofe@7015
   272
fun unfold_datatypes sign orig_descr sorts (dt_info : datatype_info Symtab.table) descr i =
berghofe@5177
   273
  let
berghofe@7015
   274
    fun typ_error T msg = error ("Non-admissible type expression\n" ^
berghofe@7015
   275
      Sign.string_of_typ sign (typ_of_dtyp (orig_descr @ descr) sorts T) ^ "\n" ^ msg);
berghofe@7015
   276
berghofe@7015
   277
    fun get_dt_descr T i tname dts =
berghofe@5177
   278
      (case Symtab.lookup (dt_info, tname) of
berghofe@7015
   279
         None => typ_error T (tname ^ " is not a datatype - can't use it in\
berghofe@7015
   280
           \ nested recursion")
berghofe@5177
   281
       | (Some {index, descr, ...}) =>
berghofe@5177
   282
           let val (_, vars, _) = the (assoc (descr, index));
berghofe@7015
   283
               val subst = ((map dest_DtTFree vars) ~~ dts) handle LIST _ =>
berghofe@7015
   284
                 typ_error T ("Type constructor " ^ tname ^ " used with wrong\
berghofe@5177
   285
                  \ number of arguments")
berghofe@5177
   286
           in (i + index, map (fn (j, (tn, args, cs)) => (i + j,
berghofe@5177
   287
             (tn, map (subst_DtTFree i subst) args,
berghofe@5177
   288
              map (apsnd (map (subst_DtTFree i subst))) cs))) descr)
berghofe@5177
   289
           end);
berghofe@5177
   290
berghofe@5177
   291
    (* unfold a single constructor argument *)
berghofe@5177
   292
berghofe@5177
   293
    fun unfold_arg ((i, Ts, descrs), T as (DtType (tname, dts))) =
berghofe@5177
   294
          if is_rec_type T then
berghofe@7015
   295
            if tname = "fun" then
berghofe@7015
   296
              if is_rec_type (hd dts) then
berghofe@7015
   297
                typ_error T "Non-strictly positive recursive occurrence of type"
berghofe@7015
   298
              else
berghofe@7015
   299
                (case hd (tl dts) of
berghofe@7015
   300
                   DtType ("fun", _) => typ_error T "Curried function types not allowed"
berghofe@7015
   301
                 | T' => let val (i', [T''], descrs') = unfold_arg ((i, [], descrs), T')
berghofe@7015
   302
                         in (i', Ts @ [DtType (tname, [hd dts, T''])], descrs') end)
berghofe@7015
   303
            else
berghofe@7015
   304
              let val (index, descr) = get_dt_descr T i tname dts;
berghofe@7015
   305
                  val (descr', i') = unfold_datatypes sign orig_descr sorts dt_info descr (i + length descr)
berghofe@7015
   306
              in (i', Ts @ [DtRec index], descrs @ descr') end
berghofe@5177
   307
          else (i, Ts @ [T], descrs)
berghofe@5177
   308
      | unfold_arg ((i, Ts, descrs), T) = (i, Ts @ [T], descrs);
berghofe@5177
   309
berghofe@5177
   310
    (* unfold a constructor *)
berghofe@5177
   311
berghofe@5177
   312
    fun unfold_constr ((i, constrs, descrs), (cname, cargs)) =
berghofe@5177
   313
      let val (i', cargs', descrs') = foldl unfold_arg ((i, [], descrs), cargs)
berghofe@5177
   314
      in (i', constrs @ [(cname, cargs')], descrs') end;
berghofe@5177
   315
berghofe@5177
   316
    (* unfold a single datatype *)
berghofe@5177
   317
berghofe@5177
   318
    fun unfold_datatype ((i, dtypes, descrs), (j, (tname, tvars, constrs))) =
berghofe@5177
   319
      let val (i', constrs', descrs') =
berghofe@5177
   320
        foldl unfold_constr ((i, [], descrs), constrs)
berghofe@5177
   321
      in (i', dtypes @ [(j, (tname, tvars, constrs'))], descrs')
berghofe@5177
   322
      end;
berghofe@5177
   323
berghofe@5177
   324
    val (i', descr', descrs) = foldl unfold_datatype ((i, [],[]), descr);
berghofe@5177
   325
berghofe@5177
   326
  in (descr' :: descrs, i') end;
berghofe@5177
   327
berghofe@5177
   328
end;