src/HOL/HOLCF/Tools/Domain/domain_isomorphism.ML
author wenzelm
Sat Aug 16 19:01:31 2014 +0200 (2014-08-16)
changeset 57958 045c96e3edf0
parent 57954 c52223cd1003
child 58936 7fbe4436952d
permissions -rw-r--r--
clarified order of rules;
     1 (*  Title:      HOL/HOLCF/Tools/Domain/domain_isomorphism.ML
     2     Author:     Brian Huffman
     3 
     4 Defines new types satisfying the given domain equations.
     5 *)
     6 
     7 signature DOMAIN_ISOMORPHISM =
     8 sig
     9   val domain_isomorphism :
    10       (string list * binding * mixfix * typ
    11        * (binding * binding) option) list ->
    12       theory ->
    13       (Domain_Take_Proofs.iso_info list
    14        * Domain_Take_Proofs.take_induct_info) * theory
    15 
    16   val define_map_functions :
    17       (binding * Domain_Take_Proofs.iso_info) list ->
    18       theory ->
    19       {
    20         map_consts : term list,
    21         map_apply_thms : thm list,
    22         map_unfold_thms : thm list,
    23         map_cont_thm : thm,
    24         deflation_map_thms : thm list
    25       }
    26       * theory
    27 
    28   val domain_isomorphism_cmd :
    29     (string list * binding * mixfix * string * (binding * binding) option) list
    30       -> theory -> theory
    31 end
    32 
    33 structure Domain_Isomorphism : DOMAIN_ISOMORPHISM =
    34 struct
    35 
    36 val beta_ss =
    37   simpset_of (put_simpset HOL_basic_ss @{context}
    38     addsimps @{thms simp_thms} addsimprocs [@{simproc beta_cfun_proc}])
    39 
    40 fun is_cpo thy T = Sign.of_sort thy (T, @{sort cpo})
    41 
    42 
    43 (******************************************************************************)
    44 (************************** building types and terms **************************)
    45 (******************************************************************************)
    46 
    47 open HOLCF_Library
    48 
    49 infixr 6 ->>
    50 infixr -->>
    51 
    52 val udomT = @{typ udom}
    53 val deflT = @{typ "udom defl"}
    54 val udeflT = @{typ "udom u defl"}
    55 
    56 fun mk_DEFL T =
    57   Const (@{const_name defl}, Term.itselfT T --> deflT) $ Logic.mk_type T
    58 
    59 fun dest_DEFL (Const (@{const_name defl}, _) $ t) = Logic.dest_type t
    60   | dest_DEFL t = raise TERM ("dest_DEFL", [t])
    61 
    62 fun mk_LIFTDEFL T =
    63   Const (@{const_name liftdefl}, Term.itselfT T --> udeflT) $ Logic.mk_type T
    64 
    65 fun dest_LIFTDEFL (Const (@{const_name liftdefl}, _) $ t) = Logic.dest_type t
    66   | dest_LIFTDEFL t = raise TERM ("dest_LIFTDEFL", [t])
    67 
    68 fun mk_u_defl t = mk_capply (@{const "u_defl"}, t)
    69 
    70 fun emb_const T = Const (@{const_name emb}, T ->> udomT)
    71 fun prj_const T = Const (@{const_name prj}, udomT ->> T)
    72 fun coerce_const (T, U) = mk_cfcomp (prj_const U, emb_const T)
    73 
    74 fun isodefl_const T =
    75   Const (@{const_name isodefl}, (T ->> T) --> deflT --> HOLogic.boolT)
    76 
    77 fun isodefl'_const T =
    78   Const (@{const_name isodefl'}, (T ->> T) --> udeflT --> HOLogic.boolT)
    79 
    80 fun mk_deflation t =
    81   Const (@{const_name deflation}, Term.fastype_of t --> boolT) $ t
    82 
    83 (* splits a cterm into the right and lefthand sides of equality *)
    84 fun dest_eqs t = HOLogic.dest_eq (HOLogic.dest_Trueprop t)
    85 
    86 fun mk_eqs (t, u) = HOLogic.mk_Trueprop (HOLogic.mk_eq (t, u))
    87 
    88 (******************************************************************************)
    89 (****************************** isomorphism info ******************************)
    90 (******************************************************************************)
    91 
    92 fun deflation_abs_rep (info : Domain_Take_Proofs.iso_info) : thm =
    93   let
    94     val abs_iso = #abs_inverse info
    95     val rep_iso = #rep_inverse info
    96     val thm = @{thm deflation_abs_rep} OF [abs_iso, rep_iso]
    97   in
    98     Drule.zero_var_indexes thm
    99   end
   100 
   101 (******************************************************************************)
   102 (*************** fixed-point definitions and unfolding theorems ***************)
   103 (******************************************************************************)
   104 
   105 fun mk_projs []      _ = []
   106   | mk_projs (x::[]) t = [(x, t)]
   107   | mk_projs (x::xs) t = (x, mk_fst t) :: mk_projs xs (mk_snd t)
   108 
   109 fun add_fixdefs
   110     (spec : (binding * term) list)
   111     (thy : theory) : (thm list * thm list * thm) * theory =
   112   let
   113     val binds = map fst spec
   114     val (lhss, rhss) = ListPair.unzip (map (dest_eqs o snd) spec)
   115     val functional = lambda_tuple lhss (mk_tuple rhss)
   116     val fixpoint = mk_fix (mk_cabs functional)
   117 
   118     (* project components of fixpoint *)
   119     val projs = mk_projs lhss fixpoint
   120 
   121     (* convert parameters to lambda abstractions *)
   122     fun mk_eqn (lhs, rhs) =
   123         case lhs of
   124           Const (@{const_name Rep_cfun}, _) $ f $ (x as Free _) =>
   125             mk_eqn (f, big_lambda x rhs)
   126         | f $ Const (@{const_name Pure.type}, T) =>
   127             mk_eqn (f, Abs ("t", T, rhs))
   128         | Const _ => Logic.mk_equals (lhs, rhs)
   129         | _ => raise TERM ("lhs not of correct form", [lhs, rhs])
   130     val eqns = map mk_eqn projs
   131 
   132     (* register constant definitions *)
   133     val (fixdef_thms, thy) =
   134       (Global_Theory.add_defs false o map Thm.no_attributes)
   135         (map Thm.def_binding binds ~~ eqns) thy
   136 
   137     (* prove applied version of definitions *)
   138     fun prove_proj (lhs, rhs) =
   139       let
   140         fun tac ctxt = rewrite_goals_tac ctxt fixdef_thms THEN
   141           (simp_tac (put_simpset beta_ss ctxt)) 1
   142         val goal = Logic.mk_equals (lhs, rhs)
   143       in Goal.prove_global thy [] [] goal (tac o #context) end
   144     val proj_thms = map prove_proj projs
   145 
   146     (* mk_tuple lhss == fixpoint *)
   147     fun pair_equalI (thm1, thm2) = @{thm Pair_equalI} OF [thm1, thm2]
   148     val tuple_fixdef_thm = foldr1 pair_equalI proj_thms
   149 
   150     val cont_thm =
   151       let
   152         val prop = mk_trp (mk_cont functional)
   153         val rules = Named_Theorems.get (Proof_Context.init_global thy) @{named_theorems cont2cont}
   154         val tac = REPEAT_ALL_NEW (match_tac (rev rules)) 1
   155       in
   156         Goal.prove_global thy [] [] prop (K tac)
   157       end
   158 
   159     val tuple_unfold_thm =
   160       (@{thm def_cont_fix_eq} OF [tuple_fixdef_thm, cont_thm])
   161       |> Local_Defs.unfold (Proof_Context.init_global thy) @{thms split_conv}
   162 
   163     fun mk_unfold_thms [] _ = []
   164       | mk_unfold_thms (n::[]) thm = [(n, thm)]
   165       | mk_unfold_thms (n::ns) thm = let
   166           val thmL = thm RS @{thm Pair_eqD1}
   167           val thmR = thm RS @{thm Pair_eqD2}
   168         in (n, thmL) :: mk_unfold_thms ns thmR end
   169     val unfold_binds = map (Binding.suffix_name "_unfold") binds
   170 
   171     (* register unfold theorems *)
   172     val (unfold_thms, thy) =
   173       (Global_Theory.add_thms o map (Thm.no_attributes o apsnd Drule.zero_var_indexes))
   174         (mk_unfold_thms unfold_binds tuple_unfold_thm) thy
   175   in
   176     ((proj_thms, unfold_thms, cont_thm), thy)
   177   end
   178 
   179 
   180 (******************************************************************************)
   181 (****************** deflation combinators and map functions *******************)
   182 (******************************************************************************)
   183 
   184 fun defl_of_typ
   185     (thy : theory)
   186     (tab1 : (typ * term) list)
   187     (tab2 : (typ * term) list)
   188     (T : typ) : term =
   189   let
   190     val defl_simps =
   191       Named_Theorems.get (Proof_Context.init_global thy) @{named_theorems domain_defl_simps}
   192     val rules = map (Thm.concl_of #> HOLogic.dest_Trueprop #> HOLogic.dest_eq) (rev defl_simps)
   193     val rules' = map (apfst mk_DEFL) tab1 @ map (apfst mk_LIFTDEFL) tab2
   194     fun proc1 t =
   195       (case dest_DEFL t of
   196         TFree (a, _) => SOME (Free ("d" ^ Library.unprefix "'" a, deflT))
   197       | _ => NONE) handle TERM _ => NONE
   198     fun proc2 t =
   199       (case dest_LIFTDEFL t of
   200         TFree (a, _) => SOME (Free ("p" ^ Library.unprefix "'" a, udeflT))
   201       | _ => NONE) handle TERM _ => NONE
   202   in
   203     Pattern.rewrite_term thy (rules @ rules') [proc1, proc2] (mk_DEFL T)
   204   end
   205 
   206 (******************************************************************************)
   207 (********************* declaring definitions and theorems *********************)
   208 (******************************************************************************)
   209 
   210 fun define_const
   211     (bind : binding, rhs : term)
   212     (thy : theory)
   213     : (term * thm) * theory =
   214   let
   215     val typ = Term.fastype_of rhs
   216     val (const, thy) = Sign.declare_const_global ((bind, typ), NoSyn) thy
   217     val eqn = Logic.mk_equals (const, rhs)
   218     val def = Thm.no_attributes (Thm.def_binding bind, eqn)
   219     val (def_thm, thy) = yield_singleton (Global_Theory.add_defs false) def thy
   220   in
   221     ((const, def_thm), thy)
   222   end
   223 
   224 fun add_qualified_thm name (dbind, thm) =
   225     yield_singleton Global_Theory.add_thms
   226       ((Binding.qualified true name dbind, thm), [])
   227 
   228 (******************************************************************************)
   229 (*************************** defining map functions ***************************)
   230 (******************************************************************************)
   231 
   232 fun define_map_functions
   233     (spec : (binding * Domain_Take_Proofs.iso_info) list)
   234     (thy : theory) =
   235   let
   236 
   237     (* retrieve components of spec *)
   238     val dbinds = map fst spec
   239     val iso_infos = map snd spec
   240     val dom_eqns = map (fn x => (#absT x, #repT x)) iso_infos
   241     val rep_abs_consts = map (fn x => (#rep_const x, #abs_const x)) iso_infos
   242 
   243     fun mapT (T as Type (_, Ts)) =
   244         (map (fn T => T ->> T) (filter (is_cpo thy) Ts)) -->> (T ->> T)
   245       | mapT T = T ->> T
   246 
   247     (* declare map functions *)
   248     fun declare_map_const (tbind, (lhsT, _)) thy =
   249       let
   250         val map_type = mapT lhsT
   251         val map_bind = Binding.suffix_name "_map" tbind
   252       in
   253         Sign.declare_const_global ((map_bind, map_type), NoSyn) thy
   254       end
   255     val (map_consts, thy) = thy |>
   256       fold_map declare_map_const (dbinds ~~ dom_eqns)
   257 
   258     (* defining equations for map functions *)
   259     local
   260       fun unprime a = Library.unprefix "'" a
   261       fun mapvar T = Free (unprime (fst (dest_TFree T)), T ->> T)
   262       fun map_lhs (map_const, lhsT) =
   263           (lhsT, list_ccomb (map_const, map mapvar (filter (is_cpo thy) (snd (dest_Type lhsT)))))
   264       val tab1 = map map_lhs (map_consts ~~ map fst dom_eqns)
   265       val Ts = (snd o dest_Type o fst o hd) dom_eqns
   266       val tab = (Ts ~~ map mapvar Ts) @ tab1
   267       fun mk_map_spec (((rep_const, abs_const), _), (lhsT, rhsT)) =
   268         let
   269           val lhs = Domain_Take_Proofs.map_of_typ thy tab lhsT
   270           val body = Domain_Take_Proofs.map_of_typ thy tab rhsT
   271           val rhs = mk_cfcomp (abs_const, mk_cfcomp (body, rep_const))
   272         in mk_eqs (lhs, rhs) end
   273     in
   274       val map_specs =
   275           map mk_map_spec (rep_abs_consts ~~ map_consts ~~ dom_eqns)
   276     end
   277 
   278     (* register recursive definition of map functions *)
   279     val map_binds = map (Binding.suffix_name "_map") dbinds
   280     val ((map_apply_thms, map_unfold_thms, map_cont_thm), thy) =
   281       add_fixdefs (map_binds ~~ map_specs) thy
   282 
   283     (* prove deflation theorems for map functions *)
   284     val deflation_abs_rep_thms = map deflation_abs_rep iso_infos
   285     val deflation_map_thm =
   286       let
   287         fun unprime a = Library.unprefix "'" a
   288         fun mk_f T = Free (unprime (fst (dest_TFree T)), T ->> T)
   289         fun mk_assm T = mk_trp (mk_deflation (mk_f T))
   290         fun mk_goal (map_const, (lhsT, _)) =
   291           let
   292             val (_, Ts) = dest_Type lhsT
   293             val map_term = list_ccomb (map_const, map mk_f (filter (is_cpo thy) Ts))
   294           in mk_deflation map_term end
   295         val assms = (map mk_assm o filter (is_cpo thy) o snd o dest_Type o fst o hd) dom_eqns
   296         val goals = map mk_goal (map_consts ~~ dom_eqns)
   297         val goal = mk_trp (foldr1 HOLogic.mk_conj goals)
   298         val adm_rules =
   299           @{thms adm_conj adm_subst [OF _ adm_deflation]
   300                  cont2cont_fst cont2cont_snd cont_id}
   301         val bottom_rules =
   302           @{thms fst_strict snd_strict deflation_bottom simp_thms}
   303         val tuple_rules =
   304           @{thms split_def fst_conv snd_conv}
   305         val deflation_rules =
   306           @{thms conjI deflation_ID}
   307           @ deflation_abs_rep_thms
   308           @ Domain_Take_Proofs.get_deflation_thms thy
   309       in
   310         Goal.prove_global thy [] assms goal (fn {prems, context = ctxt} =>
   311          EVERY
   312           [rewrite_goals_tac ctxt map_apply_thms,
   313            rtac (map_cont_thm RS @{thm cont_fix_ind}) 1,
   314            REPEAT (resolve_tac adm_rules 1),
   315            simp_tac (put_simpset HOL_basic_ss ctxt addsimps bottom_rules) 1,
   316            simp_tac (put_simpset HOL_basic_ss ctxt addsimps tuple_rules) 1,
   317            REPEAT (etac @{thm conjE} 1),
   318            REPEAT (resolve_tac (deflation_rules @ prems) 1 ORELSE atac 1)])
   319       end
   320     fun conjuncts [] _ = []
   321       | conjuncts (n::[]) thm = [(n, thm)]
   322       | conjuncts (n::ns) thm = let
   323           val thmL = thm RS @{thm conjunct1}
   324           val thmR = thm RS @{thm conjunct2}
   325         in (n, thmL):: conjuncts ns thmR end
   326     val deflation_map_binds = dbinds |>
   327         map (Binding.prefix_name "deflation_" o Binding.suffix_name "_map")
   328     val (deflation_map_thms, thy) = thy |>
   329       (Global_Theory.add_thms o map (Thm.no_attributes o apsnd Drule.zero_var_indexes))
   330         (conjuncts deflation_map_binds deflation_map_thm)
   331 
   332     (* register indirect recursion in theory data *)
   333     local
   334       fun register_map (dname, args) =
   335         Domain_Take_Proofs.add_rec_type (dname, args)
   336       val dnames = map (fst o dest_Type o fst) dom_eqns
   337       fun args (T, _) = case T of Type (_, Ts) => map (is_cpo thy) Ts | _ => []
   338       val argss = map args dom_eqns
   339     in
   340       val thy =
   341           fold register_map (dnames ~~ argss) thy
   342     end
   343 
   344     (* register deflation theorems *)
   345     val thy = fold Domain_Take_Proofs.add_deflation_thm deflation_map_thms thy
   346 
   347     val result =
   348       {
   349         map_consts = map_consts,
   350         map_apply_thms = map_apply_thms,
   351         map_unfold_thms = map_unfold_thms,
   352         map_cont_thm = map_cont_thm,
   353         deflation_map_thms = deflation_map_thms
   354       }
   355   in
   356     (result, thy)
   357   end
   358 
   359 (******************************************************************************)
   360 (******************************* main function ********************************)
   361 (******************************************************************************)
   362 
   363 fun read_typ thy str sorts =
   364   let
   365     val ctxt = Proof_Context.init_global thy
   366       |> fold (Variable.declare_typ o TFree) sorts
   367     val T = Syntax.read_typ ctxt str
   368   in (T, Term.add_tfreesT T sorts) end
   369 
   370 fun cert_typ sign raw_T sorts =
   371   let
   372     val T = Type.no_tvars (Sign.certify_typ sign raw_T)
   373       handle TYPE (msg, _, _) => error msg
   374     val sorts' = Term.add_tfreesT T sorts
   375     val _ =
   376       case duplicates (op =) (map fst sorts') of
   377         [] => ()
   378       | dups => error ("Inconsistent sort constraints for " ^ commas dups)
   379   in (T, sorts') end
   380 
   381 fun gen_domain_isomorphism
   382     (prep_typ: theory -> 'a -> (string * sort) list -> typ * (string * sort) list)
   383     (doms_raw: (string list * binding * mixfix * 'a * (binding * binding) option) list)
   384     (thy: theory)
   385     : (Domain_Take_Proofs.iso_info list
   386        * Domain_Take_Proofs.take_induct_info) * theory =
   387   let
   388     val _ = Theory.requires thy (Context.theory_name @{theory}) "domain isomorphisms"
   389 
   390     (* this theory is used just for parsing *)
   391     val tmp_thy = thy |>
   392       Sign.add_types_global (map (fn (tvs, tbind, mx, _, _) =>
   393         (tbind, length tvs, mx)) doms_raw)
   394 
   395     fun prep_dom thy (vs, t, mx, typ_raw, morphs) sorts =
   396       let val (typ, sorts') = prep_typ thy typ_raw sorts
   397       in ((vs, t, mx, typ, morphs), sorts') end
   398 
   399     val (doms : (string list * binding * mixfix * typ * (binding * binding) option) list,
   400          sorts : (string * sort) list) =
   401       fold_map (prep_dom tmp_thy) doms_raw []
   402 
   403     (* lookup function for sorts of type variables *)
   404     fun the_sort v = the (AList.lookup (op =) sorts v)
   405 
   406     (* declare arities in temporary theory *)
   407     val tmp_thy =
   408       let
   409         fun arity (vs, tbind, _, _, _) =
   410           (Sign.full_name thy tbind, map the_sort vs, @{sort "domain"})
   411       in
   412         fold Axclass.arity_axiomatization (map arity doms) tmp_thy
   413       end
   414 
   415     (* check bifiniteness of right-hand sides *)
   416     fun check_rhs (_, _, _, rhs, _) =
   417       if Sign.of_sort tmp_thy (rhs, @{sort "domain"}) then ()
   418       else error ("Type not of sort domain: " ^
   419         quote (Syntax.string_of_typ_global tmp_thy rhs))
   420     val _ = map check_rhs doms
   421 
   422     (* domain equations *)
   423     fun mk_dom_eqn (vs, tbind, _, rhs, _) =
   424       let fun arg v = TFree (v, the_sort v)
   425       in (Type (Sign.full_name tmp_thy tbind, map arg vs), rhs) end
   426     val dom_eqns = map mk_dom_eqn doms
   427 
   428     (* check for valid type parameters *)
   429     val (tyvars, _, _, _, _) = hd doms
   430     val _ = map (fn (tvs, tname, _, _, _) =>
   431       let val full_tname = Sign.full_name tmp_thy tname
   432       in
   433         (case duplicates (op =) tvs of
   434           [] =>
   435             if eq_set (op =) (tyvars, tvs) then (full_tname, tvs)
   436             else error ("Mutually recursive domains must have same type parameters")
   437         | dups => error ("Duplicate parameter(s) for domain " ^ Binding.print tname ^
   438             " : " ^ commas dups))
   439       end) doms
   440     val dbinds = map (fn (_, dbind, _, _, _) => dbind) doms
   441     val morphs = map (fn (_, _, _, _, morphs) => morphs) doms
   442 
   443     (* determine deflation combinator arguments *)
   444     val lhsTs : typ list = map fst dom_eqns
   445     val defl_rec = Free ("t", mk_tupleT (map (K deflT) lhsTs))
   446     val defl_recs = mk_projs lhsTs defl_rec
   447     val defl_recs' = map (apsnd mk_u_defl) defl_recs
   448     fun defl_body (_, _, _, rhsT, _) =
   449       defl_of_typ tmp_thy defl_recs defl_recs' rhsT
   450     val functional = Term.lambda defl_rec (mk_tuple (map defl_body doms))
   451 
   452     val tfrees = map fst (Term.add_tfrees functional [])
   453     val frees = map fst (Term.add_frees functional [])
   454     fun get_defl_flags (vs, _, _, _, _) =
   455       let
   456         fun argT v = TFree (v, the_sort v)
   457         fun mk_d v = "d" ^ Library.unprefix "'" v
   458         fun mk_p v = "p" ^ Library.unprefix "'" v
   459         val args = maps (fn v => [(mk_d v, mk_DEFL (argT v)), (mk_p v, mk_LIFTDEFL (argT v))]) vs
   460         val typeTs = map argT (filter (member (op =) tfrees) vs)
   461         val defl_args = map snd (filter (member (op =) frees o fst) args)
   462       in
   463         (typeTs, defl_args)
   464       end
   465     val defl_flagss = map get_defl_flags doms
   466 
   467     (* declare deflation combinator constants *)
   468     fun declare_defl_const ((typeTs, defl_args), (_, tbind, _, _, _)) thy =
   469       let
   470         val defl_bind = Binding.suffix_name "_defl" tbind
   471         val defl_type =
   472           map Term.itselfT typeTs ---> map fastype_of defl_args -->> deflT
   473       in
   474         Sign.declare_const_global ((defl_bind, defl_type), NoSyn) thy
   475       end
   476     val (defl_consts, thy) =
   477       fold_map declare_defl_const (defl_flagss ~~ doms) thy
   478 
   479     (* defining equations for type combinators *)
   480     fun mk_defl_term (defl_const, (typeTs, defl_args)) =
   481       let
   482         val type_args = map Logic.mk_type typeTs
   483       in
   484         list_ccomb (list_comb (defl_const, type_args), defl_args)
   485       end
   486     val defl_terms = map mk_defl_term (defl_consts ~~ defl_flagss)
   487     val defl_tab = map fst dom_eqns ~~ defl_terms
   488     val defl_tab' = map fst dom_eqns ~~ map mk_u_defl defl_terms
   489     fun mk_defl_spec (lhsT, rhsT) =
   490       mk_eqs (defl_of_typ tmp_thy defl_tab defl_tab' lhsT,
   491               defl_of_typ tmp_thy defl_tab defl_tab' rhsT)
   492     val defl_specs = map mk_defl_spec dom_eqns
   493 
   494     (* register recursive definition of deflation combinators *)
   495     val defl_binds = map (Binding.suffix_name "_defl") dbinds
   496     val ((defl_apply_thms, defl_unfold_thms, defl_cont_thm), thy) =
   497       add_fixdefs (defl_binds ~~ defl_specs) thy
   498 
   499     (* define types using deflation combinators *)
   500     fun make_repdef ((vs, tbind, mx, _, _), defl) thy =
   501       let
   502         val spec = (tbind, map (rpair dummyS) vs, mx)
   503         val ((_, _, _, {DEFL, ...}), thy) =
   504           Domaindef.add_domaindef spec defl NONE thy
   505         (* declare domain_defl_simps rules *)
   506         val thy =
   507           Context.theory_map (Named_Theorems.add_thm @{named_theorems domain_defl_simps} DEFL) thy
   508       in
   509         (DEFL, thy)
   510       end
   511     val (DEFL_thms, thy) = fold_map make_repdef (doms ~~ defl_terms) thy
   512 
   513     (* prove DEFL equations *)
   514     fun mk_DEFL_eq_thm (lhsT, rhsT) =
   515       let
   516         val goal = mk_eqs (mk_DEFL lhsT, mk_DEFL rhsT)
   517         val DEFL_simps =
   518           Named_Theorems.get (Proof_Context.init_global thy) @{named_theorems domain_defl_simps}
   519         fun tac ctxt =
   520           rewrite_goals_tac ctxt (map mk_meta_eq (rev DEFL_simps))
   521           THEN TRY (resolve_tac defl_unfold_thms 1)
   522       in
   523         Goal.prove_global thy [] [] goal (tac o #context)
   524       end
   525     val DEFL_eq_thms = map mk_DEFL_eq_thm dom_eqns
   526 
   527     (* register DEFL equations *)
   528     val DEFL_eq_binds = map (Binding.prefix_name "DEFL_eq_") dbinds
   529     val (_, thy) = thy |>
   530       (Global_Theory.add_thms o map Thm.no_attributes)
   531         (DEFL_eq_binds ~~ DEFL_eq_thms)
   532 
   533     (* define rep/abs functions *)
   534     fun mk_rep_abs ((tbind, _), (lhsT, rhsT)) thy =
   535       let
   536         val rep_bind = Binding.suffix_name "_rep" tbind
   537         val abs_bind = Binding.suffix_name "_abs" tbind
   538         val ((rep_const, rep_def), thy) =
   539             define_const (rep_bind, coerce_const (lhsT, rhsT)) thy
   540         val ((abs_const, abs_def), thy) =
   541             define_const (abs_bind, coerce_const (rhsT, lhsT)) thy
   542       in
   543         (((rep_const, abs_const), (rep_def, abs_def)), thy)
   544       end
   545     val ((rep_abs_consts, rep_abs_defs), thy) = thy
   546       |> fold_map mk_rep_abs (dbinds ~~ morphs ~~ dom_eqns)
   547       |>> ListPair.unzip
   548 
   549     (* prove isomorphism and isodefl rules *)
   550     fun mk_iso_thms ((tbind, DEFL_eq), (rep_def, abs_def)) thy =
   551       let
   552         fun make thm =
   553             Drule.zero_var_indexes (thm OF [DEFL_eq, abs_def, rep_def])
   554         val rep_iso_thm = make @{thm domain_rep_iso}
   555         val abs_iso_thm = make @{thm domain_abs_iso}
   556         val isodefl_thm = make @{thm isodefl_abs_rep}
   557         val thy = thy
   558           |> snd o add_qualified_thm "rep_iso" (tbind, rep_iso_thm)
   559           |> snd o add_qualified_thm "abs_iso" (tbind, abs_iso_thm)
   560           |> snd o add_qualified_thm "isodefl_abs_rep" (tbind, isodefl_thm)
   561       in
   562         (((rep_iso_thm, abs_iso_thm), isodefl_thm), thy)
   563       end
   564     val ((iso_thms, isodefl_abs_rep_thms), thy) =
   565       thy
   566       |> fold_map mk_iso_thms (dbinds ~~ DEFL_eq_thms ~~ rep_abs_defs)
   567       |>> ListPair.unzip
   568 
   569     (* collect info about rep/abs *)
   570     val iso_infos : Domain_Take_Proofs.iso_info list =
   571       let
   572         fun mk_info (((lhsT, rhsT), (repC, absC)), (rep_iso, abs_iso)) =
   573           {
   574             repT = rhsT,
   575             absT = lhsT,
   576             rep_const = repC,
   577             abs_const = absC,
   578             rep_inverse = rep_iso,
   579             abs_inverse = abs_iso
   580           }
   581       in
   582         map mk_info (dom_eqns ~~ rep_abs_consts ~~ iso_thms)
   583       end
   584 
   585     (* definitions and proofs related to map functions *)
   586     val (map_info, thy) =
   587         define_map_functions (dbinds ~~ iso_infos) thy
   588     val { map_consts, map_apply_thms, map_cont_thm, ...} = map_info
   589 
   590     (* prove isodefl rules for map functions *)
   591     val isodefl_thm =
   592       let
   593         fun unprime a = Library.unprefix "'" a
   594         fun mk_d T = Free ("d" ^ unprime (fst (dest_TFree T)), deflT)
   595         fun mk_p T = Free ("p" ^ unprime (fst (dest_TFree T)), udeflT)
   596         fun mk_f T = Free ("f" ^ unprime (fst (dest_TFree T)), T ->> T)
   597         fun mk_assm t =
   598           case try dest_LIFTDEFL t of
   599             SOME T => mk_trp (isodefl'_const T $ mk_f T $ mk_p T)
   600           | NONE =>
   601             let val T = dest_DEFL t
   602             in mk_trp (isodefl_const T $ mk_f T $ mk_d T) end
   603         fun mk_goal (map_const, (T, _)) =
   604           let
   605             val (_, Ts) = dest_Type T
   606             val map_term = list_ccomb (map_const, map mk_f (filter (is_cpo thy) Ts))
   607             val defl_term = defl_of_typ thy (Ts ~~ map mk_d Ts) (Ts ~~ map mk_p Ts) T
   608           in isodefl_const T $ map_term $ defl_term end
   609         val assms = (map mk_assm o snd o hd) defl_flagss
   610         val goals = map mk_goal (map_consts ~~ dom_eqns)
   611         val goal = mk_trp (foldr1 HOLogic.mk_conj goals)
   612         val adm_rules =
   613           @{thms adm_conj adm_isodefl cont2cont_fst cont2cont_snd cont_id}
   614         val bottom_rules =
   615           @{thms fst_strict snd_strict isodefl_bottom simp_thms}
   616         val tuple_rules =
   617           @{thms split_def fst_conv snd_conv}
   618         val map_ID_thms = Domain_Take_Proofs.get_map_ID_thms thy
   619         val map_ID_simps = map (fn th => th RS sym) map_ID_thms
   620         val isodefl_rules =
   621           @{thms conjI isodefl_ID_DEFL isodefl_LIFTDEFL}
   622           @ isodefl_abs_rep_thms
   623           @ rev (Named_Theorems.get (Proof_Context.init_global thy) @{named_theorems domain_isodefl})
   624       in
   625         Goal.prove_global thy [] assms goal (fn {prems, context = ctxt} =>
   626          EVERY
   627           [rewrite_goals_tac ctxt (defl_apply_thms @ map_apply_thms),
   628            rtac (@{thm cont_parallel_fix_ind}
   629              OF [defl_cont_thm, map_cont_thm]) 1,
   630            REPEAT (resolve_tac adm_rules 1),
   631            simp_tac (put_simpset HOL_basic_ss ctxt addsimps bottom_rules) 1,
   632            simp_tac (put_simpset HOL_basic_ss ctxt addsimps tuple_rules) 1,
   633            simp_tac (put_simpset HOL_basic_ss ctxt addsimps map_ID_simps) 1,
   634            REPEAT (etac @{thm conjE} 1),
   635            REPEAT (resolve_tac (isodefl_rules @ prems) 1 ORELSE atac 1)])
   636       end
   637     val isodefl_binds = map (Binding.prefix_name "isodefl_") dbinds
   638     fun conjuncts [] _ = []
   639       | conjuncts (n::[]) thm = [(n, thm)]
   640       | conjuncts (n::ns) thm = let
   641           val thmL = thm RS @{thm conjunct1}
   642           val thmR = thm RS @{thm conjunct2}
   643         in (n, thmL):: conjuncts ns thmR end
   644     val (isodefl_thms, thy) = thy |>
   645       (Global_Theory.add_thms o map (Thm.no_attributes o apsnd Drule.zero_var_indexes))
   646         (conjuncts isodefl_binds isodefl_thm)
   647     val thy =
   648       fold (Context.theory_map o Named_Theorems.add_thm @{named_theorems domain_isodefl})
   649         isodefl_thms thy
   650 
   651     (* prove map_ID theorems *)
   652     fun prove_map_ID_thm
   653         (((map_const, (lhsT, _)), DEFL_thm), isodefl_thm) =
   654       let
   655         val Ts = snd (dest_Type lhsT)
   656         fun is_cpo T = Sign.of_sort thy (T, @{sort cpo})
   657         val lhs = list_ccomb (map_const, map mk_ID (filter is_cpo Ts))
   658         val goal = mk_eqs (lhs, mk_ID lhsT)
   659         val tac = EVERY
   660           [rtac @{thm isodefl_DEFL_imp_ID} 1,
   661            stac DEFL_thm 1,
   662            rtac isodefl_thm 1,
   663            REPEAT (resolve_tac @{thms isodefl_ID_DEFL isodefl_LIFTDEFL} 1)]
   664       in
   665         Goal.prove_global thy [] [] goal (K tac)
   666       end
   667     val map_ID_binds = map (Binding.suffix_name "_map_ID") dbinds
   668     val map_ID_thms =
   669       map prove_map_ID_thm
   670         (map_consts ~~ dom_eqns ~~ DEFL_thms ~~ isodefl_thms)
   671     val (_, thy) = thy |>
   672       (Global_Theory.add_thms o map (rpair [Domain_Take_Proofs.map_ID_add]))
   673         (map_ID_binds ~~ map_ID_thms)
   674 
   675     (* definitions and proofs related to take functions *)
   676     val (take_info, thy) =
   677         Domain_Take_Proofs.define_take_functions
   678           (dbinds ~~ iso_infos) thy
   679     val { take_consts, chain_take_thms, take_0_thms, take_Suc_thms, ...} =
   680         take_info
   681 
   682     (* least-upper-bound lemma for take functions *)
   683     val lub_take_lemma =
   684       let
   685         val lhs = mk_tuple (map mk_lub take_consts)
   686         fun is_cpo T = Sign.of_sort thy (T, @{sort cpo})
   687         fun mk_map_ID (map_const, (lhsT, _)) =
   688           list_ccomb (map_const, map mk_ID (filter is_cpo (snd (dest_Type lhsT))))
   689         val rhs = mk_tuple (map mk_map_ID (map_consts ~~ dom_eqns))
   690         val goal = mk_trp (mk_eq (lhs, rhs))
   691         val map_ID_thms = Domain_Take_Proofs.get_map_ID_thms thy
   692         val start_rules =
   693             @{thms lub_Pair [symmetric] ch2ch_Pair} @ chain_take_thms
   694             @ @{thms pair_collapse split_def}
   695             @ map_apply_thms @ map_ID_thms
   696         val rules0 =
   697             @{thms iterate_0 Pair_strict} @ take_0_thms
   698         val rules1 =
   699             @{thms iterate_Suc prod_eq_iff fst_conv snd_conv}
   700             @ take_Suc_thms
   701         fun tac ctxt =
   702             EVERY
   703             [simp_tac (put_simpset HOL_basic_ss ctxt addsimps start_rules) 1,
   704              simp_tac (put_simpset HOL_basic_ss ctxt addsimps @{thms fix_def2}) 1,
   705              rtac @{thm lub_eq} 1,
   706              rtac @{thm nat.induct} 1,
   707              simp_tac (put_simpset HOL_basic_ss ctxt addsimps rules0) 1,
   708              asm_full_simp_tac (put_simpset beta_ss ctxt addsimps rules1) 1]
   709       in
   710         Goal.prove_global thy [] [] goal (tac o #context)
   711       end
   712 
   713     (* prove lub of take equals ID *)
   714     fun prove_lub_take (((dbind, take_const), map_ID_thm), (lhsT, _)) thy =
   715       let
   716         val n = Free ("n", natT)
   717         val goal = mk_eqs (mk_lub (lambda n (take_const $ n)), mk_ID lhsT)
   718         val tac =
   719             EVERY
   720             [rtac @{thm trans} 1, rtac map_ID_thm 2,
   721              cut_tac lub_take_lemma 1,
   722              REPEAT (etac @{thm Pair_inject} 1), atac 1]
   723         val lub_take_thm = Goal.prove_global thy [] [] goal (K tac)
   724       in
   725         add_qualified_thm "lub_take" (dbind, lub_take_thm) thy
   726       end
   727     val (lub_take_thms, thy) =
   728         fold_map prove_lub_take
   729           (dbinds ~~ take_consts ~~ map_ID_thms ~~ dom_eqns) thy
   730 
   731     (* prove additional take theorems *)
   732     val (take_info2, thy) =
   733         Domain_Take_Proofs.add_lub_take_theorems
   734           (dbinds ~~ iso_infos) take_info lub_take_thms thy
   735   in
   736     ((iso_infos, take_info2), thy)
   737   end
   738 
   739 val domain_isomorphism = gen_domain_isomorphism cert_typ
   740 val domain_isomorphism_cmd = snd oo gen_domain_isomorphism read_typ
   741 
   742 (******************************************************************************)
   743 (******************************** outer syntax ********************************)
   744 (******************************************************************************)
   745 
   746 local
   747 
   748 val parse_domain_iso :
   749     (string list * binding * mixfix * string * (binding * binding) option)
   750       parser =
   751   (Parse.type_args -- Parse.binding -- Parse.opt_mixfix -- (@{keyword "="} |-- Parse.typ) --
   752     Scan.option (@{keyword "morphisms"} |-- Parse.!!! (Parse.binding -- Parse.binding)))
   753     >> (fn ((((vs, t), mx), rhs), morphs) => (vs, t, mx, rhs, morphs))
   754 
   755 val parse_domain_isos = Parse.and_list1 parse_domain_iso
   756 
   757 in
   758 
   759 val _ =
   760   Outer_Syntax.command @{command_spec "domain_isomorphism"} "define domain isomorphisms (HOLCF)"
   761     (parse_domain_isos >> (Toplevel.theory o domain_isomorphism_cmd))
   762 
   763 end
   764 
   765 end