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