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