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