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