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