src/HOL/Tools/datatype_prop.ML
author haftmann
Fri May 23 16:05:04 2008 +0200 (2008-05-23)
changeset 26969 cf3f998d0631
parent 25154 6155f2faf23e
child 27002 215d64dc971e
permissions -rw-r--r--
moved case distinction over number of constructors for distinctness rules from DatatypeProp to DatatypeRepProofs
     1 (*  Title:      HOL/Tools/datatype_prop.ML
     2     ID:         $Id$
     3     Author:     Stefan Berghofer, TU Muenchen
     4 
     5 Characteristic properties of datatypes.
     6 *)
     7 
     8 signature DATATYPE_PROP =
     9 sig
    10   val distinctness_limit : int Config.T
    11   val distinctness_limit_setup : theory -> theory
    12   val indexify_names: string list -> string list
    13   val make_tnames: typ list -> string list
    14   val make_injs : DatatypeAux.descr list -> (string * sort) list -> term list list
    15   val make_distincts : DatatypeAux.descr list ->
    16     (string * sort) list -> (int * term list) list
    17   val make_ind : DatatypeAux.descr list -> (string * sort) list -> term
    18   val make_casedists : DatatypeAux.descr list -> (string * sort) list -> term list
    19   val make_primrec_Ts : DatatypeAux.descr list -> (string * sort) list ->
    20     string list -> typ list * typ list
    21   val make_primrecs : string list -> DatatypeAux.descr list ->
    22     (string * sort) list -> theory -> term list
    23   val make_cases : string list -> DatatypeAux.descr list ->
    24     (string * sort) list -> theory -> term list list
    25   val make_splits : string list -> DatatypeAux.descr list ->
    26     (string * sort) list -> theory -> (term * term) list
    27   val make_weak_case_congs : string list -> DatatypeAux.descr list ->
    28     (string * sort) list -> theory -> term list
    29   val make_case_congs : string list -> DatatypeAux.descr list ->
    30     (string * sort) list -> theory -> term list
    31   val make_nchotomys : DatatypeAux.descr list ->
    32     (string * sort) list -> term list
    33 end;
    34 
    35 structure DatatypeProp : DATATYPE_PROP =
    36 struct
    37 
    38 open DatatypeAux;
    39 
    40 (*the kind of distinctiveness axioms depends on number of constructors*)
    41 val (distinctness_limit, distinctness_limit_setup) =
    42   Attrib.config_int "datatype_distinctness_limit" 7;
    43 
    44 fun indexify_names names =
    45   let
    46     fun index (x :: xs) tab =
    47       (case AList.lookup (op =) tab x of
    48         NONE => if member (op =) xs x then (x ^ "1") :: index xs ((x, 2) :: tab) else x :: index xs tab
    49       | SOME i => (x ^ string_of_int i) :: index xs ((x, i + 1) :: tab))
    50     | index [] _ = [];
    51   in index names [] end;
    52 
    53 fun make_tnames Ts =
    54   let
    55     fun type_name (TFree (name, _)) = implode (tl (explode name))
    56       | type_name (Type (name, _)) = 
    57           let val name' = Sign.base_name name
    58           in if Syntax.is_identifier name' then name' else "x" end;
    59   in indexify_names (map type_name Ts) end;
    60 
    61 
    62 (************************* distinctness of constructors ***********************)
    63 
    64 fun make_distincts descr sorts =
    65   let
    66     val descr' = List.concat descr;
    67     val recTs = get_rec_types descr' sorts;
    68     val newTs = Library.take (length (hd descr), recTs);
    69 
    70     fun prep_constr (cname, cargs) = (cname, map (typ_of_dtyp descr' sorts) cargs);
    71 
    72     fun make_distincts' _ [] = []
    73       | make_distincts' T ((cname, cargs)::constrs) =
    74           let
    75             val frees = map Free ((make_tnames cargs) ~~ cargs);
    76             val t = list_comb (Const (cname, cargs ---> T), frees);
    77 
    78             fun make_distincts'' [] = []
    79               | make_distincts'' ((cname', cargs')::constrs') =
    80                   let
    81                     val frees' = map Free ((map ((op ^) o (rpair "'"))
    82                       (make_tnames cargs')) ~~ cargs');
    83                     val t' = list_comb (Const (cname', cargs' ---> T), frees')
    84                   in
    85                     HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.mk_eq (t, t')) ::
    86                     HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.mk_eq (t', t)) ::
    87                       make_distincts'' constrs'
    88                   end
    89 
    90           in make_distincts'' constrs @ make_distincts' T constrs end;
    91 
    92     (**** number of constructors < distinctness_limit : C_i ... ~= C_j ... ****)
    93 
    94   in
    95     map2 (fn ((_, (_, _, constrs))) => fn T =>
    96       (length constrs, make_distincts' T (map prep_constr constrs))) (hd descr) newTs
    97   end;
    98 
    99 (************************* injectivity of constructors ************************)
   100 
   101 fun make_injs descr sorts =
   102   let
   103     val descr' = flat descr;
   104     fun make_inj T (cname, cargs) =
   105       if null cargs then I else
   106         let
   107           val Ts = map (typ_of_dtyp descr' sorts) cargs;
   108           val constr_t = Const (cname, Ts ---> T);
   109           val tnames = make_tnames Ts;
   110           val frees = map Free (tnames ~~ Ts);
   111           val frees' = map Free ((map ((op ^) o (rpair "'")) tnames) ~~ Ts);
   112         in cons (HOLogic.mk_Trueprop (HOLogic.mk_eq
   113           (HOLogic.mk_eq (list_comb (constr_t, frees), list_comb (constr_t, frees')),
   114            foldr1 (HOLogic.mk_binop "op &")
   115              (map HOLogic.mk_eq (frees ~~ frees')))))
   116         end;
   117   in
   118     map2 (fn d => fn T => fold_rev (make_inj T) (#3 (snd d)) [])
   119       (hd descr) (Library.take (length (hd descr), get_rec_types descr' sorts))
   120   end;
   121 
   122 (********************************* induction **********************************)
   123 
   124 fun make_ind descr sorts =
   125   let
   126     val descr' = List.concat descr;
   127     val recTs = get_rec_types descr' sorts;
   128     val pnames = if length descr' = 1 then ["P"]
   129       else map (fn i => "P" ^ string_of_int i) (1 upto length descr');
   130 
   131     fun make_pred i T =
   132       let val T' = T --> HOLogic.boolT
   133       in Free (List.nth (pnames, i), T') end;
   134 
   135     fun make_ind_prem k T (cname, cargs) =
   136       let
   137         fun mk_prem ((dt, s), T) =
   138           let val (Us, U) = strip_type T
   139           in list_all (map (pair "x") Us, HOLogic.mk_Trueprop
   140             (make_pred (body_index dt) U $ app_bnds (Free (s, T)) (length Us)))
   141           end;
   142 
   143         val recs = List.filter is_rec_type cargs;
   144         val Ts = map (typ_of_dtyp descr' sorts) cargs;
   145         val recTs' = map (typ_of_dtyp descr' sorts) recs;
   146         val tnames = Name.variant_list pnames (make_tnames Ts);
   147         val rec_tnames = map fst (List.filter (is_rec_type o snd) (tnames ~~ cargs));
   148         val frees = tnames ~~ Ts;
   149         val prems = map mk_prem (recs ~~ rec_tnames ~~ recTs');
   150 
   151       in list_all_free (frees, Logic.list_implies (prems,
   152         HOLogic.mk_Trueprop (make_pred k T $ 
   153           list_comb (Const (cname, Ts ---> T), map Free frees))))
   154       end;
   155 
   156     val prems = List.concat (map (fn ((i, (_, _, constrs)), T) =>
   157       map (make_ind_prem i T) constrs) (descr' ~~ recTs));
   158     val tnames = make_tnames recTs;
   159     val concl = HOLogic.mk_Trueprop (foldr1 (HOLogic.mk_binop "op &")
   160       (map (fn (((i, _), T), tname) => make_pred i T $ Free (tname, T))
   161         (descr' ~~ recTs ~~ tnames)))
   162 
   163   in Logic.list_implies (prems, concl) end;
   164 
   165 (******************************* case distinction *****************************)
   166 
   167 fun make_casedists descr sorts =
   168   let
   169     val descr' = List.concat descr;
   170 
   171     fun make_casedist_prem T (cname, cargs) =
   172       let
   173         val Ts = map (typ_of_dtyp descr' sorts) cargs;
   174         val frees = Name.variant_list ["P", "y"] (make_tnames Ts) ~~ Ts;
   175         val free_ts = map Free frees
   176       in list_all_free (frees, Logic.mk_implies (HOLogic.mk_Trueprop
   177         (HOLogic.mk_eq (Free ("y", T), list_comb (Const (cname, Ts ---> T), free_ts))),
   178           HOLogic.mk_Trueprop (Free ("P", HOLogic.boolT))))
   179       end;
   180 
   181     fun make_casedist ((_, (_, _, constrs)), T) =
   182       let val prems = map (make_casedist_prem T) constrs
   183       in Logic.list_implies (prems, HOLogic.mk_Trueprop (Free ("P", HOLogic.boolT)))
   184       end
   185 
   186   in map make_casedist
   187     ((hd descr) ~~ Library.take (length (hd descr), get_rec_types descr' sorts))
   188   end;
   189 
   190 (*************** characteristic equations for primrec combinator **************)
   191 
   192 fun make_primrec_Ts descr sorts used =
   193   let
   194     val descr' = List.concat descr;
   195 
   196     val rec_result_Ts = map TFree (Name.variant_list used (replicate (length descr') "'t") ~~
   197       replicate (length descr') HOLogic.typeS);
   198 
   199     val reccomb_fn_Ts = List.concat (map (fn (i, (_, _, constrs)) =>
   200       map (fn (_, cargs) =>
   201         let
   202           val Ts = map (typ_of_dtyp descr' sorts) cargs;
   203           val recs = List.filter (is_rec_type o fst) (cargs ~~ Ts);
   204 
   205           fun mk_argT (dt, T) =
   206             binder_types T ---> List.nth (rec_result_Ts, body_index dt);
   207 
   208           val argTs = Ts @ map mk_argT recs
   209         in argTs ---> List.nth (rec_result_Ts, i)
   210         end) constrs) descr');
   211 
   212   in (rec_result_Ts, reccomb_fn_Ts) end;
   213 
   214 fun make_primrecs new_type_names descr sorts thy =
   215   let
   216     val descr' = List.concat descr;
   217     val recTs = get_rec_types descr' sorts;
   218     val used = foldr add_typ_tfree_names [] recTs;
   219 
   220     val (rec_result_Ts, reccomb_fn_Ts) = make_primrec_Ts descr sorts used;
   221 
   222     val rec_fns = map (uncurry (mk_Free "f"))
   223       (reccomb_fn_Ts ~~ (1 upto (length reccomb_fn_Ts)));
   224 
   225     val big_reccomb_name = (space_implode "_" new_type_names) ^ "_rec";
   226     val reccomb_names = map (Sign.intern_const thy)
   227       (if length descr' = 1 then [big_reccomb_name] else
   228         (map ((curry (op ^) (big_reccomb_name ^ "_")) o string_of_int)
   229           (1 upto (length descr'))));
   230     val reccombs = map (fn ((name, T), T') => list_comb
   231       (Const (name, reccomb_fn_Ts @ [T] ---> T'), rec_fns))
   232         (reccomb_names ~~ recTs ~~ rec_result_Ts);
   233 
   234     fun make_primrec T comb_t ((ts, f::fs), (cname, cargs)) =
   235       let
   236         val recs = List.filter is_rec_type cargs;
   237         val Ts = map (typ_of_dtyp descr' sorts) cargs;
   238         val recTs' = map (typ_of_dtyp descr' sorts) recs;
   239         val tnames = make_tnames Ts;
   240         val rec_tnames = map fst (List.filter (is_rec_type o snd) (tnames ~~ cargs));
   241         val frees = map Free (tnames ~~ Ts);
   242         val frees' = map Free (rec_tnames ~~ recTs');
   243 
   244         fun mk_reccomb ((dt, T), t) =
   245           let val (Us, U) = strip_type T
   246           in list_abs (map (pair "x") Us,
   247             List.nth (reccombs, body_index dt) $ app_bnds t (length Us))
   248           end;
   249 
   250         val reccombs' = map mk_reccomb (recs ~~ recTs' ~~ frees')
   251 
   252       in (ts @ [HOLogic.mk_Trueprop (HOLogic.mk_eq
   253         (comb_t $ list_comb (Const (cname, Ts ---> T), frees),
   254          list_comb (f, frees @ reccombs')))], fs)
   255       end
   256 
   257   in fst (Library.foldl (fn (x, ((dt, T), comb_t)) =>
   258     Library.foldl (make_primrec T comb_t) (x, #3 (snd dt)))
   259       (([], rec_fns), descr' ~~ recTs ~~ reccombs))
   260   end;
   261 
   262 (****************** make terms of form  t_case f1 ... fn  *********************)
   263 
   264 fun make_case_combs new_type_names descr sorts thy fname =
   265   let
   266     val descr' = List.concat descr;
   267     val recTs = get_rec_types descr' sorts;
   268     val used = foldr add_typ_tfree_names [] recTs;
   269     val newTs = Library.take (length (hd descr), recTs);
   270     val T' = TFree (Name.variant used "'t", HOLogic.typeS);
   271 
   272     val case_fn_Ts = map (fn (i, (_, _, constrs)) =>
   273       map (fn (_, cargs) =>
   274         let val Ts = map (typ_of_dtyp descr' sorts) cargs
   275         in Ts ---> T' end) constrs) (hd descr);
   276 
   277     val case_names = map (fn s =>
   278       Sign.intern_const thy (s ^ "_case")) new_type_names
   279   in
   280     map (fn ((name, Ts), T) => list_comb
   281       (Const (name, Ts @ [T] ---> T'),
   282         map (uncurry (mk_Free fname)) (Ts ~~ (1 upto length Ts))))
   283           (case_names ~~ case_fn_Ts ~~ newTs)
   284   end;
   285 
   286 (**************** characteristic equations for case combinator ****************)
   287 
   288 fun make_cases new_type_names descr sorts thy =
   289   let
   290     val descr' = List.concat descr;
   291     val recTs = get_rec_types descr' sorts;
   292     val newTs = Library.take (length (hd descr), recTs);
   293 
   294     fun make_case T comb_t ((cname, cargs), f) =
   295       let
   296         val Ts = map (typ_of_dtyp descr' sorts) cargs;
   297         val frees = map Free ((make_tnames Ts) ~~ Ts)
   298       in HOLogic.mk_Trueprop (HOLogic.mk_eq
   299         (comb_t $ list_comb (Const (cname, Ts ---> T), frees),
   300          list_comb (f, frees)))
   301       end
   302 
   303   in map (fn (((_, (_, _, constrs)), T), comb_t) =>
   304     map (make_case T comb_t) (constrs ~~ (snd (strip_comb comb_t))))
   305       ((hd descr) ~~ newTs ~~ (make_case_combs new_type_names descr sorts thy "f"))
   306   end;
   307 
   308 
   309 (*************************** the "split" - equations **************************)
   310 
   311 fun make_splits new_type_names descr sorts thy =
   312   let
   313     val descr' = List.concat descr;
   314     val recTs = get_rec_types descr' sorts;
   315     val used' = foldr add_typ_tfree_names [] recTs;
   316     val newTs = Library.take (length (hd descr), recTs);
   317     val T' = TFree (Name.variant used' "'t", HOLogic.typeS);
   318     val P = Free ("P", T' --> HOLogic.boolT);
   319 
   320     fun make_split (((_, (_, _, constrs)), T), comb_t) =
   321       let
   322         val (_, fs) = strip_comb comb_t;
   323         val used = ["P", "x"] @ (map (fst o dest_Free) fs);
   324 
   325         fun process_constr (((cname, cargs), f), (t1s, t2s)) =
   326           let
   327             val Ts = map (typ_of_dtyp descr' sorts) cargs;
   328             val frees = map Free (Name.variant_list used (make_tnames Ts) ~~ Ts);
   329             val eqn = HOLogic.mk_eq (Free ("x", T),
   330               list_comb (Const (cname, Ts ---> T), frees));
   331             val P' = P $ list_comb (f, frees)
   332           in ((foldr (fn (Free (s, T), t) => HOLogic.mk_all (s, T, t))
   333                 (HOLogic.imp $ eqn $ P') frees)::t1s,
   334               (foldr (fn (Free (s, T), t) => HOLogic.mk_exists (s, T, t))
   335                 (HOLogic.conj $ eqn $ (HOLogic.Not $ P')) frees)::t2s)
   336           end;
   337 
   338         val (t1s, t2s) = foldr process_constr ([], []) (constrs ~~ fs);
   339         val lhs = P $ (comb_t $ Free ("x", T))
   340       in
   341         (HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, mk_conj t1s)),
   342          HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, HOLogic.Not $ mk_disj t2s)))
   343       end
   344 
   345   in map make_split ((hd descr) ~~ newTs ~~
   346     (make_case_combs new_type_names descr sorts thy "f"))
   347   end;
   348 
   349 (************************* additional rules for TFL ***************************)
   350 
   351 fun make_weak_case_congs new_type_names descr sorts thy =
   352   let
   353     val case_combs = make_case_combs new_type_names descr sorts thy "f";
   354 
   355     fun mk_case_cong comb =
   356       let 
   357         val Type ("fun", [T, _]) = fastype_of comb;
   358         val M = Free ("M", T);
   359         val M' = Free ("M'", T);
   360       in
   361         Logic.mk_implies (HOLogic.mk_Trueprop (HOLogic.mk_eq (M, M')),
   362           HOLogic.mk_Trueprop (HOLogic.mk_eq (comb $ M, comb $ M')))
   363       end
   364   in
   365     map mk_case_cong case_combs
   366   end;
   367  
   368 
   369 (*---------------------------------------------------------------------------
   370  * Structure of case congruence theorem looks like this:
   371  *
   372  *    (M = M') 
   373  *    ==> (!!x1,...,xk. (M' = C1 x1..xk) ==> (f1 x1..xk = g1 x1..xk)) 
   374  *    ==> ... 
   375  *    ==> (!!x1,...,xj. (M' = Cn x1..xj) ==> (fn x1..xj = gn x1..xj)) 
   376  *    ==>
   377  *      (ty_case f1..fn M = ty_case g1..gn M')
   378  *---------------------------------------------------------------------------*)
   379 
   380 fun make_case_congs new_type_names descr sorts thy =
   381   let
   382     val case_combs = make_case_combs new_type_names descr sorts thy "f";
   383     val case_combs' = make_case_combs new_type_names descr sorts thy "g";
   384 
   385     fun mk_case_cong ((comb, comb'), (_, (_, _, constrs))) =
   386       let
   387         val Type ("fun", [T, _]) = fastype_of comb;
   388         val (_, fs) = strip_comb comb;
   389         val (_, gs) = strip_comb comb';
   390         val used = ["M", "M'"] @ map (fst o dest_Free) (fs @ gs);
   391         val M = Free ("M", T);
   392         val M' = Free ("M'", T);
   393 
   394         fun mk_clause ((f, g), (cname, _)) =
   395           let
   396             val (Ts, _) = strip_type (fastype_of f);
   397             val tnames = Name.variant_list used (make_tnames Ts);
   398             val frees = map Free (tnames ~~ Ts)
   399           in
   400             list_all_free (tnames ~~ Ts, Logic.mk_implies
   401               (HOLogic.mk_Trueprop
   402                 (HOLogic.mk_eq (M', list_comb (Const (cname, Ts ---> T), frees))),
   403                HOLogic.mk_Trueprop
   404                 (HOLogic.mk_eq (list_comb (f, frees), list_comb (g, frees)))))
   405           end
   406 
   407       in
   408         Logic.list_implies (HOLogic.mk_Trueprop (HOLogic.mk_eq (M, M')) ::
   409           map mk_clause (fs ~~ gs ~~ constrs),
   410             HOLogic.mk_Trueprop (HOLogic.mk_eq (comb $ M, comb' $ M')))
   411       end
   412 
   413   in
   414     map mk_case_cong (case_combs ~~ case_combs' ~~ hd descr)
   415   end;
   416 
   417 (*---------------------------------------------------------------------------
   418  * Structure of exhaustion theorem looks like this:
   419  *
   420  *    !v. (? y1..yi. v = C1 y1..yi) | ... | (? y1..yj. v = Cn y1..yj)
   421  *---------------------------------------------------------------------------*)
   422 
   423 fun make_nchotomys descr sorts =
   424   let
   425     val descr' = List.concat descr;
   426     val recTs = get_rec_types descr' sorts;
   427     val newTs = Library.take (length (hd descr), recTs);
   428 
   429     fun mk_eqn T (cname, cargs) =
   430       let
   431         val Ts = map (typ_of_dtyp descr' sorts) cargs;
   432         val tnames = Name.variant_list ["v"] (make_tnames Ts);
   433         val frees = tnames ~~ Ts
   434       in
   435         foldr (fn ((s, T'), t) => HOLogic.mk_exists (s, T', t))
   436           (HOLogic.mk_eq (Free ("v", T),
   437             list_comb (Const (cname, Ts ---> T), map Free frees))) frees
   438       end
   439 
   440   in map (fn ((_, (_, _, constrs)), T) =>
   441     HOLogic.mk_Trueprop (HOLogic.mk_all ("v", T, mk_disj (map (mk_eqn T) constrs))))
   442       (hd descr ~~ newTs)
   443   end;
   444 
   445 end;