src/HOLCF/Tools/Domain/domain_constructors.ML
author wenzelm
Fri May 28 18:15:22 2010 +0200 (2010-05-28 ago)
changeset 37165 c2e27ae53c2a
parent 37109 e67760c1b851
child 37744 3daaf23b9ab4
permissions -rw-r--r--
made SML/NJ quite happy;
     1 (*  Title:      HOLCF/Tools/domain/domain_constructors.ML
     2     Author:     Brian Huffman
     3 
     4 Defines constructor functions for a given domain isomorphism
     5 and proves related theorems.
     6 *)
     7 
     8 signature DOMAIN_CONSTRUCTORS =
     9 sig
    10   val add_domain_constructors :
    11       binding
    12       -> (binding * (bool * binding option * typ) list * mixfix) list
    13       -> Domain_Take_Proofs.iso_info
    14       -> theory
    15       -> { con_consts : term list,
    16            con_betas : thm list,
    17            nchotomy : thm,
    18            exhaust : thm,
    19            compacts : thm list,
    20            con_rews : thm list,
    21            inverts : thm list,
    22            injects : thm list,
    23            dist_les : thm list,
    24            dist_eqs : thm list,
    25            cases : thm list,
    26            sel_rews : thm list,
    27            dis_rews : thm list,
    28            match_rews : thm list
    29          } * theory;
    30 end;
    31 
    32 
    33 structure Domain_Constructors :> DOMAIN_CONSTRUCTORS =
    34 struct
    35 
    36 open HOLCF_Library;
    37 
    38 infixr 6 ->>;
    39 infix -->>;
    40 infix 9 `;
    41 
    42 (************************** miscellaneous functions ***************************)
    43 
    44 val simple_ss = HOL_basic_ss addsimps simp_thms;
    45 
    46 val beta_rules =
    47   @{thms beta_cfun cont_id cont_const cont2cont_Rep_CFun cont2cont_LAM'} @
    48   @{thms cont2cont_fst cont2cont_snd cont2cont_Pair};
    49 
    50 val beta_ss = HOL_basic_ss addsimps (simp_thms @ beta_rules);
    51 
    52 fun define_consts
    53     (specs : (binding * term * mixfix) list)
    54     (thy : theory)
    55     : (term list * thm list) * theory =
    56   let
    57     fun mk_decl (b, t, mx) = (b, fastype_of t, mx);
    58     val decls = map mk_decl specs;
    59     val thy = Cont_Consts.add_consts decls thy;
    60     fun mk_const (b, T, mx) = Const (Sign.full_name thy b, T);
    61     val consts = map mk_const decls;
    62     fun mk_def c (b, t, mx) =
    63       (Binding.suffix_name "_def" b, Logic.mk_equals (c, t));
    64     val defs = map2 mk_def consts specs;
    65     val (def_thms, thy) =
    66       PureThy.add_defs false (map Thm.no_attributes defs) thy;
    67   in
    68     ((consts, def_thms), thy)
    69   end;
    70 
    71 fun prove
    72     (thy : theory)
    73     (defs : thm list)
    74     (goal : term)
    75     (tacs : {prems: thm list, context: Proof.context} -> tactic list)
    76     : thm =
    77   let
    78     fun tac {prems, context} =
    79       rewrite_goals_tac defs THEN
    80       EVERY (tacs {prems = map (rewrite_rule defs) prems, context = context})
    81   in
    82     Goal.prove_global thy [] [] goal tac
    83   end;
    84 
    85 fun get_vars_avoiding
    86     (taken : string list)
    87     (args : (bool * typ) list)
    88     : (term list * term list) =
    89   let
    90     val Ts = map snd args;
    91     val ns = Name.variant_list taken (Datatype_Prop.make_tnames Ts);
    92     val vs = map Free (ns ~~ Ts);
    93     val nonlazy = map snd (filter_out (fst o fst) (args ~~ vs));
    94   in
    95     (vs, nonlazy)
    96   end;
    97 
    98 fun get_vars args = get_vars_avoiding [] args;
    99 
   100 (************** generating beta reduction rules from definitions **************)
   101 
   102 local
   103   fun arglist (Const _ $ Abs (s, T, t)) =
   104       let
   105         val arg = Free (s, T);
   106         val (args, body) = arglist (subst_bound (arg, t));
   107       in (arg :: args, body) end
   108     | arglist t = ([], t);
   109 in
   110   fun beta_of_def thy def_thm =
   111       let
   112         val (con, lam) = Logic.dest_equals (concl_of def_thm);
   113         val (args, rhs) = arglist lam;
   114         val lhs = list_ccomb (con, args);
   115         val goal = mk_equals (lhs, rhs);
   116         val cs = ContProc.cont_thms lam;
   117         val betas = map (fn c => mk_meta_eq (c RS @{thm beta_cfun})) cs;
   118       in
   119         prove thy (def_thm::betas) goal (K [rtac reflexive_thm 1])
   120       end;
   121 end;
   122 
   123 (******************************************************************************)
   124 (************* definitions and theorems for constructor functions *************)
   125 (******************************************************************************)
   126 
   127 fun add_constructors
   128     (spec : (binding * (bool * typ) list * mixfix) list)
   129     (abs_const : term)
   130     (iso_locale : thm)
   131     (thy : theory)
   132     =
   133   let
   134 
   135     (* get theorems about rep and abs *)
   136     val abs_strict = iso_locale RS @{thm iso.abs_strict};
   137 
   138     (* get types of type isomorphism *)
   139     val (rhsT, lhsT) = dest_cfunT (fastype_of abs_const);
   140 
   141     fun vars_of args =
   142       let
   143         val Ts = map snd args;
   144         val ns = Datatype_Prop.make_tnames Ts;
   145       in
   146         map Free (ns ~~ Ts)
   147       end;
   148 
   149     (* define constructor functions *)
   150     val ((con_consts, con_defs), thy) =
   151       let
   152         fun one_arg (lazy, T) var = if lazy then mk_up var else var;
   153         fun one_con (_,args,_) = mk_stuple (map2 one_arg args (vars_of args));
   154         fun mk_abs t = abs_const ` t;
   155         val rhss = map mk_abs (mk_sinjects (map one_con spec));
   156         fun mk_def (bind, args, mx) rhs =
   157           (bind, big_lambdas (vars_of args) rhs, mx);
   158       in
   159         define_consts (map2 mk_def spec rhss) thy
   160       end;
   161 
   162     (* prove beta reduction rules for constructors *)
   163     val con_betas = map (beta_of_def thy) con_defs;
   164 
   165     (* replace bindings with terms in constructor spec *)
   166     val spec' : (term * (bool * typ) list) list =
   167       let fun one_con con (b, args, mx) = (con, args);
   168       in map2 one_con con_consts spec end;
   169 
   170     (* prove exhaustiveness of constructors *)
   171     local
   172       fun arg2typ n (true,  T) = (n+1, mk_upT (TVar (("'a", n), @{sort cpo})))
   173         | arg2typ n (false, T) = (n+1, TVar (("'a", n), @{sort pcpo}));
   174       fun args2typ n [] = (n, oneT)
   175         | args2typ n [arg] = arg2typ n arg
   176         | args2typ n (arg::args) =
   177           let
   178             val (n1, t1) = arg2typ n arg;
   179             val (n2, t2) = args2typ n1 args
   180           in (n2, mk_sprodT (t1, t2)) end;
   181       fun cons2typ n [] = (n, oneT)
   182         | cons2typ n [con] = args2typ n (snd con)
   183         | cons2typ n (con::cons) =
   184           let
   185             val (n1, t1) = args2typ n (snd con);
   186             val (n2, t2) = cons2typ n1 cons
   187           in (n2, mk_ssumT (t1, t2)) end;
   188       val ct = ctyp_of thy (snd (cons2typ 1 spec'));
   189       val thm1 = instantiate' [SOME ct] [] @{thm exh_start};
   190       val thm2 = rewrite_rule (map mk_meta_eq @{thms ex_defined_iffs}) thm1;
   191       val thm3 = rewrite_rule [mk_meta_eq @{thm conj_assoc}] thm2;
   192 
   193       val y = Free ("y", lhsT);
   194       fun one_con (con, args) =
   195         let
   196           val (vs, nonlazy) = get_vars_avoiding ["y"] args;
   197           val eqn = mk_eq (y, list_ccomb (con, vs));
   198           val conj = foldr1 mk_conj (eqn :: map mk_defined nonlazy);
   199         in Library.foldr mk_ex (vs, conj) end;
   200       val goal = mk_trp (foldr1 mk_disj (mk_undef y :: map one_con spec'));
   201       (* first rules replace "y = UU \/ P" with "rep$y = UU \/ P" *)
   202       val tacs = [
   203           rtac (iso_locale RS @{thm iso.casedist_rule}) 1,
   204           rewrite_goals_tac [mk_meta_eq (iso_locale RS @{thm iso.iso_swap})],
   205           rtac thm3 1];
   206     in
   207       val nchotomy = prove thy con_betas goal (K tacs);
   208       val exhaust =
   209           (nchotomy RS @{thm exh_casedist0})
   210           |> rewrite_rule @{thms exh_casedists}
   211           |> Drule.zero_var_indexes;
   212     end;
   213 
   214     (* prove compactness rules for constructors *)
   215     val compacts =
   216       let
   217         val rules = @{thms compact_sinl compact_sinr compact_spair
   218                            compact_up compact_ONE};
   219         val tacs =
   220           [rtac (iso_locale RS @{thm iso.compact_abs}) 1,
   221            REPEAT (resolve_tac rules 1 ORELSE atac 1)];
   222         fun con_compact (con, args) =
   223           let
   224             val vs = vars_of args;
   225             val con_app = list_ccomb (con, vs);
   226             val concl = mk_trp (mk_compact con_app);
   227             val assms = map (mk_trp o mk_compact) vs;
   228             val goal = Logic.list_implies (assms, concl);
   229           in
   230             prove thy con_betas goal (K tacs)
   231           end;
   232       in
   233         map con_compact spec'
   234       end;
   235 
   236     (* prove strictness rules for constructors *)
   237     local
   238       fun con_strict (con, args) = 
   239         let
   240           val rules = abs_strict :: @{thms con_strict_rules};
   241           val (vs, nonlazy) = get_vars args;
   242           fun one_strict v' =
   243             let
   244               val UU = mk_bottom (fastype_of v');
   245               val vs' = map (fn v => if v = v' then UU else v) vs;
   246               val goal = mk_trp (mk_undef (list_ccomb (con, vs')));
   247               val tacs = [simp_tac (HOL_basic_ss addsimps rules) 1];
   248             in prove thy con_betas goal (K tacs) end;
   249         in map one_strict nonlazy end;
   250 
   251       fun con_defin (con, args) =
   252         let
   253           fun iff_disj (t, []) = HOLogic.mk_not t
   254             | iff_disj (t, ts) = mk_eq (t, foldr1 HOLogic.mk_disj ts);
   255           val (vs, nonlazy) = get_vars args;
   256           val lhs = mk_undef (list_ccomb (con, vs));
   257           val rhss = map mk_undef nonlazy;
   258           val goal = mk_trp (iff_disj (lhs, rhss));
   259           val rule1 = iso_locale RS @{thm iso.abs_defined_iff};
   260           val rules = rule1 :: @{thms con_defined_iff_rules};
   261           val tacs = [simp_tac (HOL_ss addsimps rules) 1];
   262         in prove thy con_betas goal (K tacs) end;
   263     in
   264       val con_stricts = maps con_strict spec';
   265       val con_defins = map con_defin spec';
   266       val con_rews = con_stricts @ con_defins;
   267     end;
   268 
   269     (* prove injectiveness of constructors *)
   270     local
   271       fun pgterm rel (con, args) =
   272         let
   273           fun prime (Free (n, T)) = Free (n^"'", T)
   274             | prime t             = t;
   275           val (xs, nonlazy) = get_vars args;
   276           val ys = map prime xs;
   277           val lhs = rel (list_ccomb (con, xs), list_ccomb (con, ys));
   278           val rhs = foldr1 mk_conj (ListPair.map rel (xs, ys));
   279           val concl = mk_trp (mk_eq (lhs, rhs));
   280           val zs = case args of [_] => [] | _ => nonlazy;
   281           val assms = map (mk_trp o mk_defined) zs;
   282           val goal = Logic.list_implies (assms, concl);
   283         in prove thy con_betas goal end;
   284       val cons' = filter (fn (_, args) => not (null args)) spec';
   285     in
   286       val inverts =
   287         let
   288           val abs_below = iso_locale RS @{thm iso.abs_below};
   289           val rules1 = abs_below :: @{thms sinl_below sinr_below spair_below up_below};
   290           val rules2 = @{thms up_defined spair_defined ONE_defined}
   291           val rules = rules1 @ rules2;
   292           val tacs = [asm_simp_tac (simple_ss addsimps rules) 1];
   293         in map (fn c => pgterm mk_below c (K tacs)) cons' end;
   294       val injects =
   295         let
   296           val abs_eq = iso_locale RS @{thm iso.abs_eq};
   297           val rules1 = abs_eq :: @{thms sinl_eq sinr_eq spair_eq up_eq};
   298           val rules2 = @{thms up_defined spair_defined ONE_defined}
   299           val rules = rules1 @ rules2;
   300           val tacs = [asm_simp_tac (simple_ss addsimps rules) 1];
   301         in map (fn c => pgterm mk_eq c (K tacs)) cons' end;
   302     end;
   303 
   304     (* prove distinctness of constructors *)
   305     local
   306       fun map_dist (f : 'a -> 'a -> 'b) (xs : 'a list) : 'b list =
   307         flat (map_index (fn (i, x) => map (f x) (nth_drop i xs)) xs);
   308       fun prime (Free (n, T)) = Free (n^"'", T)
   309         | prime t             = t;
   310       fun iff_disj (t, []) = mk_not t
   311         | iff_disj (t, ts) = mk_eq (t, foldr1 mk_disj ts);
   312       fun iff_disj2 (t, [], us) = mk_not t
   313         | iff_disj2 (t, ts, []) = mk_not t
   314         | iff_disj2 (t, ts, us) =
   315           mk_eq (t, mk_conj (foldr1 mk_disj ts, foldr1 mk_disj us));
   316       fun dist_le (con1, args1) (con2, args2) =
   317         let
   318           val (vs1, zs1) = get_vars args1;
   319           val (vs2, zs2) = get_vars args2 |> pairself (map prime);
   320           val lhs = mk_below (list_ccomb (con1, vs1), list_ccomb (con2, vs2));
   321           val rhss = map mk_undef zs1;
   322           val goal = mk_trp (iff_disj (lhs, rhss));
   323           val rule1 = iso_locale RS @{thm iso.abs_below};
   324           val rules = rule1 :: @{thms con_below_iff_rules};
   325           val tacs = [simp_tac (HOL_ss addsimps rules) 1];
   326         in prove thy con_betas goal (K tacs) end;
   327       fun dist_eq (con1, args1) (con2, args2) =
   328         let
   329           val (vs1, zs1) = get_vars args1;
   330           val (vs2, zs2) = get_vars args2 |> pairself (map prime);
   331           val lhs = mk_eq (list_ccomb (con1, vs1), list_ccomb (con2, vs2));
   332           val rhss1 = map mk_undef zs1;
   333           val rhss2 = map mk_undef zs2;
   334           val goal = mk_trp (iff_disj2 (lhs, rhss1, rhss2));
   335           val rule1 = iso_locale RS @{thm iso.abs_eq};
   336           val rules = rule1 :: @{thms con_eq_iff_rules};
   337           val tacs = [simp_tac (HOL_ss addsimps rules) 1];
   338         in prove thy con_betas goal (K tacs) end;
   339     in
   340       val dist_les = map_dist dist_le spec';
   341       val dist_eqs = map_dist dist_eq spec';
   342     end;
   343 
   344     val result =
   345       {
   346         con_consts = con_consts,
   347         con_betas = con_betas,
   348         nchotomy = nchotomy,
   349         exhaust = exhaust,
   350         compacts = compacts,
   351         con_rews = con_rews,
   352         inverts = inverts,
   353         injects = injects,
   354         dist_les = dist_les,
   355         dist_eqs = dist_eqs
   356       };
   357   in
   358     (result, thy)
   359   end;
   360 
   361 (******************************************************************************)
   362 (**************** definition and theorems for case combinator *****************)
   363 (******************************************************************************)
   364 
   365 fun add_case_combinator
   366     (spec : (term * (bool * typ) list) list)
   367     (lhsT : typ)
   368     (dbind : binding)
   369     (con_betas : thm list)
   370     (exhaust : thm)
   371     (iso_locale : thm)
   372     (rep_const : term)
   373     (thy : theory)
   374     : ((typ -> term) * thm list) * theory =
   375   let
   376 
   377     (* prove rep/abs rules *)
   378     val rep_strict = iso_locale RS @{thm iso.rep_strict};
   379     val abs_inverse = iso_locale RS @{thm iso.abs_iso};
   380 
   381     (* calculate function arguments of case combinator *)
   382     val tns = map (fst o dest_TFree) (snd (dest_Type lhsT));
   383     val resultT = TFree (Name.variant tns "'t", @{sort pcpo});
   384     fun fTs T = map (fn (_, args) => map snd args -->> T) spec;
   385     val fns = Datatype_Prop.indexify_names (map (K "f") spec);
   386     val fs = map Free (fns ~~ fTs resultT);
   387     fun caseT T = fTs T -->> (lhsT ->> T);
   388 
   389     (* definition of case combinator *)
   390     local
   391       val case_bind = Binding.suffix_name "_when" dbind;
   392       fun lambda_arg (lazy, v) t =
   393           (if lazy then mk_fup else I) (big_lambda v t);
   394       fun lambda_args []      t = mk_one_when t
   395         | lambda_args (x::[]) t = lambda_arg x t
   396         | lambda_args (x::xs) t = mk_ssplit (lambda_arg x (lambda_args xs t));
   397       fun one_con f (_, args) =
   398         let
   399           val Ts = map snd args;
   400           val ns = Name.variant_list fns (Datatype_Prop.make_tnames Ts);
   401           val vs = map Free (ns ~~ Ts);
   402         in
   403           lambda_args (map fst args ~~ vs) (list_ccomb (f, vs))
   404         end;
   405       fun mk_sscases [t] = mk_strictify t
   406         | mk_sscases ts = foldr1 mk_sscase ts;
   407       val body = mk_sscases (map2 one_con fs spec);
   408       val rhs = big_lambdas fs (mk_cfcomp (body, rep_const));
   409       val ((case_consts, case_defs), thy) =
   410           define_consts [(case_bind, rhs, NoSyn)] thy;
   411       val case_name = Sign.full_name thy case_bind;
   412     in
   413       val case_def = hd case_defs;
   414       fun case_const T = Const (case_name, caseT T);
   415       val case_app = list_ccomb (case_const resultT, fs);
   416       val thy = thy;
   417     end;
   418 
   419     (* define syntax for case combinator *)
   420     (* TODO: re-implement case syntax using a parse translation *)
   421     local
   422       open Syntax
   423       fun syntax c = Syntax.mark_const (fst (dest_Const c));
   424       fun xconst c = Long_Name.base_name (fst (dest_Const c));
   425       fun c_ast authentic con =
   426           Constant (if authentic then syntax con else xconst con);
   427       fun showint n = string_of_int (n+1);
   428       fun expvar n = Variable ("e" ^ showint n);
   429       fun argvar n (m, _) = Variable ("a" ^ showint n ^ "_" ^ showint m);
   430       fun argvars n args = map_index (argvar n) args;
   431       fun app s (l, r) = mk_appl (Constant s) [l, r];
   432       val cabs = app "_cabs";
   433       val capp = app @{const_syntax Rep_CFun};
   434       val capps = Library.foldl capp
   435       fun con1 authentic n (con,args) =
   436           Library.foldl capp (c_ast authentic con, argvars n args);
   437       fun case1 authentic (n, c) =
   438           app "_case1" (con1 authentic n c, expvar n);
   439       fun arg1 (n, (con,args)) = List.foldr cabs (expvar n) (argvars n args);
   440       fun when1 n (m, c) =
   441           if n = m then arg1 (n, c) else (Constant @{const_syntax UU});
   442       val case_constant = Constant (syntax (case_const dummyT));
   443       fun case_trans authentic =
   444           ParsePrintRule
   445             (app "_case_syntax"
   446               (Variable "x",
   447                foldr1 (app "_case2") (map_index (case1 authentic) spec)),
   448              capp (capps (case_constant, map_index arg1 spec), Variable "x"));
   449       fun one_abscon_trans authentic (n, c) =
   450           ParsePrintRule
   451             (cabs (con1 authentic n c, expvar n),
   452              capps (case_constant, map_index (when1 n) spec));
   453       fun abscon_trans authentic =
   454           map_index (one_abscon_trans authentic) spec;
   455       val trans_rules : ast Syntax.trrule list =
   456           case_trans false :: case_trans true ::
   457           abscon_trans false @ abscon_trans true;
   458     in
   459       val thy = Sign.add_trrules_i trans_rules thy;
   460     end;
   461 
   462     (* prove beta reduction rule for case combinator *)
   463     val case_beta = beta_of_def thy case_def;
   464 
   465     (* prove strictness of case combinator *)
   466     val case_strict =
   467       let
   468         val defs = case_beta :: map mk_meta_eq [rep_strict, @{thm cfcomp2}];
   469         val goal = mk_trp (mk_strict case_app);
   470         val rules = @{thms sscase1 ssplit1 strictify1 one_when1};
   471         val tacs = [resolve_tac rules 1];
   472       in prove thy defs goal (K tacs) end;
   473         
   474     (* prove rewrites for case combinator *)
   475     local
   476       fun one_case (con, args) f =
   477         let
   478           val (vs, nonlazy) = get_vars args;
   479           val assms = map (mk_trp o mk_defined) nonlazy;
   480           val lhs = case_app ` list_ccomb (con, vs);
   481           val rhs = list_ccomb (f, vs);
   482           val concl = mk_trp (mk_eq (lhs, rhs));
   483           val goal = Logic.list_implies (assms, concl);
   484           val defs = case_beta :: con_betas;
   485           val rules1 = @{thms strictify2 sscase2 sscase3 ssplit2 fup2 ID1};
   486           val rules2 = @{thms con_defined_iff_rules};
   487           val rules3 = @{thms cfcomp2 one_when2};
   488           val rules = abs_inverse :: rules1 @ rules2 @ rules3;
   489           val tacs = [asm_simp_tac (beta_ss addsimps rules) 1];
   490         in prove thy defs goal (K tacs) end;
   491     in
   492       val case_apps = map2 one_case spec fs;
   493     end
   494 
   495   in
   496     ((case_const, case_strict :: case_apps), thy)
   497   end
   498 
   499 (******************************************************************************)
   500 (************** definitions and theorems for selector functions ***************)
   501 (******************************************************************************)
   502 
   503 fun add_selectors
   504     (spec : (term * (bool * binding option * typ) list) list)
   505     (rep_const : term)
   506     (abs_inv : thm)
   507     (rep_strict : thm)
   508     (rep_strict_iff : thm)
   509     (con_betas : thm list)
   510     (thy : theory)
   511     : thm list * theory =
   512   let
   513 
   514     (* define selector functions *)
   515     val ((sel_consts, sel_defs), thy) =
   516       let
   517         fun rangeT s = snd (dest_cfunT (fastype_of s));
   518         fun mk_outl s = mk_cfcomp (from_sinl (dest_ssumT (rangeT s)), s);
   519         fun mk_outr s = mk_cfcomp (from_sinr (dest_ssumT (rangeT s)), s);
   520         fun mk_sfst s = mk_cfcomp (sfst_const (dest_sprodT (rangeT s)), s);
   521         fun mk_ssnd s = mk_cfcomp (ssnd_const (dest_sprodT (rangeT s)), s);
   522         fun mk_down s = mk_cfcomp (from_up (dest_upT (rangeT s)), s);
   523 
   524         fun sels_of_arg s (lazy, NONE,   T) = []
   525           | sels_of_arg s (lazy, SOME b, T) =
   526             [(b, if lazy then mk_down s else s, NoSyn)];
   527         fun sels_of_args s [] = []
   528           | sels_of_args s (v :: []) = sels_of_arg s v
   529           | sels_of_args s (v :: vs) =
   530             sels_of_arg (mk_sfst s) v @ sels_of_args (mk_ssnd s) vs;
   531         fun sels_of_cons s [] = []
   532           | sels_of_cons s ((con, args) :: []) = sels_of_args s args
   533           | sels_of_cons s ((con, args) :: cs) =
   534             sels_of_args (mk_outl s) args @ sels_of_cons (mk_outr s) cs;
   535         val sel_eqns : (binding * term * mixfix) list =
   536             sels_of_cons rep_const spec;
   537       in
   538         define_consts sel_eqns thy
   539       end
   540 
   541     (* replace bindings with terms in constructor spec *)
   542     val spec2 : (term * (bool * term option * typ) list) list =
   543       let
   544         fun prep_arg (lazy, NONE, T) sels = ((lazy, NONE, T), sels)
   545           | prep_arg (lazy, SOME _, T) sels =
   546             ((lazy, SOME (hd sels), T), tl sels);
   547         fun prep_con (con, args) sels =
   548             apfst (pair con) (fold_map prep_arg args sels);
   549       in
   550         fst (fold_map prep_con spec sel_consts)
   551       end;
   552 
   553     (* prove selector strictness rules *)
   554     val sel_stricts : thm list =
   555       let
   556         val rules = rep_strict :: @{thms sel_strict_rules};
   557         val tacs = [simp_tac (HOL_basic_ss addsimps rules) 1];
   558         fun sel_strict sel =
   559           let
   560             val goal = mk_trp (mk_strict sel);
   561           in
   562             prove thy sel_defs goal (K tacs)
   563           end
   564       in
   565         map sel_strict sel_consts
   566       end
   567 
   568     (* prove selector application rules *)
   569     val sel_apps : thm list =
   570       let
   571         val defs = con_betas @ sel_defs;
   572         val rules = abs_inv :: @{thms sel_app_rules};
   573         val tacs = [asm_simp_tac (simple_ss addsimps rules) 1];
   574         fun sel_apps_of (i, (con, args: (bool * term option * typ) list)) =
   575           let
   576             val Ts : typ list = map #3 args;
   577             val ns : string list = Datatype_Prop.make_tnames Ts;
   578             val vs : term list = map Free (ns ~~ Ts);
   579             val con_app : term = list_ccomb (con, vs);
   580             val vs' : (bool * term) list = map #1 args ~~ vs;
   581             fun one_same (n, sel, T) =
   582               let
   583                 val xs = map snd (filter_out fst (nth_drop n vs'));
   584                 val assms = map (mk_trp o mk_defined) xs;
   585                 val concl = mk_trp (mk_eq (sel ` con_app, nth vs n));
   586                 val goal = Logic.list_implies (assms, concl);
   587               in
   588                 prove thy defs goal (K tacs)
   589               end;
   590             fun one_diff (n, sel, T) =
   591               let
   592                 val goal = mk_trp (mk_eq (sel ` con_app, mk_bottom T));
   593               in
   594                 prove thy defs goal (K tacs)
   595               end;
   596             fun one_con (j, (_, args')) : thm list =
   597               let
   598                 fun prep (i, (lazy, NONE, T)) = NONE
   599                   | prep (i, (lazy, SOME sel, T)) = SOME (i, sel, T);
   600                 val sels : (int * term * typ) list =
   601                   map_filter prep (map_index I args');
   602               in
   603                 if i = j
   604                 then map one_same sels
   605                 else map one_diff sels
   606               end
   607           in
   608             flat (map_index one_con spec2)
   609           end
   610       in
   611         flat (map_index sel_apps_of spec2)
   612       end
   613 
   614   (* prove selector definedness rules *)
   615     val sel_defins : thm list =
   616       let
   617         val rules = rep_strict_iff :: @{thms sel_defined_iff_rules};
   618         val tacs = [simp_tac (HOL_basic_ss addsimps rules) 1];
   619         fun sel_defin sel =
   620           let
   621             val (T, U) = dest_cfunT (fastype_of sel);
   622             val x = Free ("x", T);
   623             val lhs = mk_eq (sel ` x, mk_bottom U);
   624             val rhs = mk_eq (x, mk_bottom T);
   625             val goal = mk_trp (mk_eq (lhs, rhs));
   626           in
   627             prove thy sel_defs goal (K tacs)
   628           end
   629         fun one_arg (false, SOME sel, T) = SOME (sel_defin sel)
   630           | one_arg _                    = NONE;
   631       in
   632         case spec2 of
   633           [(con, args)] => map_filter one_arg args
   634         | _             => []
   635       end;
   636 
   637   in
   638     (sel_stricts @ sel_defins @ sel_apps, thy)
   639   end
   640 
   641 (******************************************************************************)
   642 (************ definitions and theorems for discriminator functions ************)
   643 (******************************************************************************)
   644 
   645 fun add_discriminators
   646     (bindings : binding list)
   647     (spec : (term * (bool * typ) list) list)
   648     (lhsT : typ)
   649     (exhaust : thm)
   650     (case_const : typ -> term)
   651     (case_rews : thm list)
   652     (thy : theory) =
   653   let
   654 
   655     fun vars_of args =
   656       let
   657         val Ts = map snd args;
   658         val ns = Datatype_Prop.make_tnames Ts;
   659       in
   660         map Free (ns ~~ Ts)
   661       end;
   662 
   663     (* define discriminator functions *)
   664     local
   665       fun dis_fun i (j, (con, args)) =
   666         let
   667           val (vs, nonlazy) = get_vars args;
   668           val tr = if i = j then @{term TT} else @{term FF};
   669         in
   670           big_lambdas vs tr
   671         end;
   672       fun dis_eqn (i, bind) : binding * term * mixfix =
   673         let
   674           val dis_bind = Binding.prefix_name "is_" bind;
   675           val rhs = list_ccomb (case_const trT, map_index (dis_fun i) spec);
   676         in
   677           (dis_bind, rhs, NoSyn)
   678         end;
   679     in
   680       val ((dis_consts, dis_defs), thy) =
   681           define_consts (map_index dis_eqn bindings) thy
   682     end;
   683 
   684     (* prove discriminator strictness rules *)
   685     local
   686       fun dis_strict dis =
   687         let val goal = mk_trp (mk_strict dis);
   688         in prove thy dis_defs goal (K [rtac (hd case_rews) 1]) end;
   689     in
   690       val dis_stricts = map dis_strict dis_consts;
   691     end;
   692 
   693     (* prove discriminator/constructor rules *)
   694     local
   695       fun dis_app (i, dis) (j, (con, args)) =
   696         let
   697           val (vs, nonlazy) = get_vars args;
   698           val lhs = dis ` list_ccomb (con, vs);
   699           val rhs = if i = j then @{term TT} else @{term FF};
   700           val assms = map (mk_trp o mk_defined) nonlazy;
   701           val concl = mk_trp (mk_eq (lhs, rhs));
   702           val goal = Logic.list_implies (assms, concl);
   703           val tacs = [asm_simp_tac (beta_ss addsimps case_rews) 1];
   704         in prove thy dis_defs goal (K tacs) end;
   705       fun one_dis (i, dis) =
   706           map_index (dis_app (i, dis)) spec;
   707     in
   708       val dis_apps = flat (map_index one_dis dis_consts);
   709     end;
   710 
   711     (* prove discriminator definedness rules *)
   712     local
   713       fun dis_defin dis =
   714         let
   715           val x = Free ("x", lhsT);
   716           val simps = dis_apps @ @{thms dist_eq_tr};
   717           val tacs =
   718             [rtac @{thm iffI} 1,
   719              asm_simp_tac (HOL_basic_ss addsimps dis_stricts) 2,
   720              rtac exhaust 1, atac 1,
   721              DETERM_UNTIL_SOLVED (CHANGED
   722                (asm_full_simp_tac (simple_ss addsimps simps) 1))];
   723           val goal = mk_trp (mk_eq (mk_undef (dis ` x), mk_undef x));
   724         in prove thy [] goal (K tacs) end;
   725     in
   726       val dis_defins = map dis_defin dis_consts;
   727     end;
   728 
   729   in
   730     (dis_stricts @ dis_defins @ dis_apps, thy)
   731   end;
   732 
   733 (******************************************************************************)
   734 (*************** definitions and theorems for match combinators ***************)
   735 (******************************************************************************)
   736 
   737 fun add_match_combinators
   738     (bindings : binding list)
   739     (spec : (term * (bool * typ) list) list)
   740     (lhsT : typ)
   741     (exhaust : thm)
   742     (case_const : typ -> term)
   743     (case_rews : thm list)
   744     (thy : theory) =
   745   let
   746 
   747     (* get a fresh type variable for the result type *)
   748     val resultT : typ =
   749       let
   750         val ts : string list = map (fst o dest_TFree) (snd (dest_Type lhsT));
   751         val t : string = Name.variant ts "'t";
   752       in TFree (t, @{sort pcpo}) end;
   753 
   754     (* define match combinators *)
   755     local
   756       val x = Free ("x", lhsT);
   757       fun k args = Free ("k", map snd args -->> mk_matchT resultT);
   758       val fail = mk_fail resultT;
   759       fun mat_fun i (j, (con, args)) =
   760         let
   761           val (vs, nonlazy) = get_vars_avoiding ["x","k"] args;
   762         in
   763           if i = j then k args else big_lambdas vs fail
   764         end;
   765       fun mat_eqn (i, (bind, (con, args))) : binding * term * mixfix =
   766         let
   767           val mat_bind = Binding.prefix_name "match_" bind;
   768           val funs = map_index (mat_fun i) spec
   769           val body = list_ccomb (case_const (mk_matchT resultT), funs);
   770           val rhs = big_lambda x (big_lambda (k args) (body ` x));
   771         in
   772           (mat_bind, rhs, NoSyn)
   773         end;
   774     in
   775       val ((match_consts, match_defs), thy) =
   776           define_consts (map_index mat_eqn (bindings ~~ spec)) thy
   777     end;
   778 
   779     (* register match combinators with fixrec package *)
   780     local
   781       val con_names = map (fst o dest_Const o fst) spec;
   782       val mat_names = map (fst o dest_Const) match_consts;
   783     in
   784       val thy = Fixrec.add_matchers (con_names ~~ mat_names) thy;
   785     end;
   786 
   787     (* prove strictness of match combinators *)
   788     local
   789       fun match_strict mat =
   790         let
   791           val (T, (U, V)) = apsnd dest_cfunT (dest_cfunT (fastype_of mat));
   792           val k = Free ("k", U);
   793           val goal = mk_trp (mk_eq (mat ` mk_bottom T ` k, mk_bottom V));
   794           val tacs = [asm_simp_tac (beta_ss addsimps case_rews) 1];
   795         in prove thy match_defs goal (K tacs) end;
   796     in
   797       val match_stricts = map match_strict match_consts;
   798     end;
   799 
   800     (* prove match/constructor rules *)
   801     local
   802       val fail = mk_fail resultT;
   803       fun match_app (i, mat) (j, (con, args)) =
   804         let
   805           val (vs, nonlazy) = get_vars_avoiding ["k"] args;
   806           val (_, (kT, _)) = apsnd dest_cfunT (dest_cfunT (fastype_of mat));
   807           val k = Free ("k", kT);
   808           val lhs = mat ` list_ccomb (con, vs) ` k;
   809           val rhs = if i = j then list_ccomb (k, vs) else fail;
   810           val assms = map (mk_trp o mk_defined) nonlazy;
   811           val concl = mk_trp (mk_eq (lhs, rhs));
   812           val goal = Logic.list_implies (assms, concl);
   813           val tacs = [asm_simp_tac (beta_ss addsimps case_rews) 1];
   814         in prove thy match_defs goal (K tacs) end;
   815       fun one_match (i, mat) =
   816           map_index (match_app (i, mat)) spec;
   817     in
   818       val match_apps = flat (map_index one_match match_consts);
   819     end;
   820 
   821   in
   822     (match_stricts @ match_apps, thy)
   823   end;
   824 
   825 (******************************************************************************)
   826 (******************************* main function ********************************)
   827 (******************************************************************************)
   828 
   829 fun add_domain_constructors
   830     (dbind : binding)
   831     (spec : (binding * (bool * binding option * typ) list * mixfix) list)
   832     (iso_info : Domain_Take_Proofs.iso_info)
   833     (thy : theory) =
   834   let
   835     val dname = Binding.name_of dbind;
   836 
   837     (* retrieve facts about rep/abs *)
   838     val lhsT = #absT iso_info;
   839     val {rep_const, abs_const, ...} = iso_info;
   840     val abs_iso_thm = #abs_inverse iso_info;
   841     val rep_iso_thm = #rep_inverse iso_info;
   842     val iso_locale = @{thm iso.intro} OF [abs_iso_thm, rep_iso_thm];
   843     val rep_strict = iso_locale RS @{thm iso.rep_strict};
   844     val abs_strict = iso_locale RS @{thm iso.abs_strict};
   845     val rep_defined_iff = iso_locale RS @{thm iso.rep_defined_iff};
   846     val abs_defined_iff = iso_locale RS @{thm iso.abs_defined_iff};
   847 
   848     (* qualify constants and theorems with domain name *)
   849     val thy = Sign.add_path dname thy;
   850 
   851     (* define constructor functions *)
   852     val (con_result, thy) =
   853       let
   854         fun prep_arg (lazy, sel, T) = (lazy, T);
   855         fun prep_con (b, args, mx) = (b, map prep_arg args, mx);
   856         val con_spec = map prep_con spec;
   857       in
   858         add_constructors con_spec abs_const iso_locale thy
   859       end;
   860     val {con_consts, con_betas, exhaust, ...} = con_result;
   861 
   862     (* define case combinator *)
   863     val ((case_const : typ -> term, cases : thm list), thy) =
   864       let
   865         fun prep_arg (lazy, sel, T) = (lazy, T);
   866         fun prep_con c (b, args, mx) = (c, map prep_arg args);
   867         val case_spec = map2 prep_con con_consts spec;
   868       in
   869         add_case_combinator case_spec lhsT dbind
   870           con_betas exhaust iso_locale rep_const thy
   871       end;
   872 
   873     (* define and prove theorems for selector functions *)
   874     val (sel_thms : thm list, thy : theory) =
   875       let
   876         val sel_spec : (term * (bool * binding option * typ) list) list =
   877           map2 (fn con => fn (b, args, mx) => (con, args)) con_consts spec;
   878       in
   879         add_selectors sel_spec rep_const
   880           abs_iso_thm rep_strict rep_defined_iff con_betas thy
   881       end;
   882 
   883     (* define and prove theorems for discriminator functions *)
   884     val (dis_thms : thm list, thy : theory) =
   885       let
   886         val bindings = map #1 spec;
   887         fun prep_arg (lazy, sel, T) = (lazy, T);
   888         fun prep_con c (b, args, mx) = (c, map prep_arg args);
   889         val dis_spec = map2 prep_con con_consts spec;
   890       in
   891         add_discriminators bindings dis_spec lhsT
   892           exhaust case_const cases thy
   893       end
   894 
   895     (* define and prove theorems for match combinators *)
   896     val (match_thms : thm list, thy : theory) =
   897       let
   898         val bindings = map #1 spec;
   899         fun prep_arg (lazy, sel, T) = (lazy, T);
   900         fun prep_con c (b, args, mx) = (c, map prep_arg args);
   901         val mat_spec = map2 prep_con con_consts spec;
   902       in
   903         add_match_combinators bindings mat_spec lhsT
   904           exhaust case_const cases thy
   905       end
   906 
   907     (* restore original signature path *)
   908     val thy = Sign.parent_path thy;
   909 
   910     val result =
   911       { con_consts = con_consts,
   912         con_betas = con_betas,
   913         nchotomy = #nchotomy con_result,
   914         exhaust = exhaust,
   915         compacts = #compacts con_result,
   916         con_rews = #con_rews con_result,
   917         inverts = #inverts con_result,
   918         injects = #injects con_result,
   919         dist_les = #dist_les con_result,
   920         dist_eqs = #dist_eqs con_result,
   921         cases = cases,
   922         sel_rews = sel_thms,
   923         dis_rews = dis_thms,
   924         match_rews = match_thms };
   925   in
   926     (result, thy)
   927   end;
   928 
   929 end;