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