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