src/HOL/HOLCF/Tools/domaindef.ML
changeset 49759 ecf1d5d87e5e
parent 46961 5c6955f487e5
child 49833 1d80798e8d8a
equal deleted inserted replaced
49758:718f10c8bbfc 49759:ecf1d5d87e5e
    15       liftprj_def : thm,
    15       liftprj_def : thm,
    16       liftdefl_def : thm,
    16       liftdefl_def : thm,
    17       DEFL : thm
    17       DEFL : thm
    18     }
    18     }
    19 
    19 
    20   val add_domaindef: bool -> binding option -> binding * (string * sort) list * mixfix ->
    20   val add_domaindef: binding * (string * sort) list * mixfix ->
    21     term -> (binding * binding) option -> theory ->
    21     term -> (binding * binding) option -> theory ->
    22     (Typedef.info * Cpodef.cpo_info * Cpodef.pcpo_info * rep_info) * theory
    22     (Typedef.info * Cpodef.cpo_info * Cpodef.pcpo_info * rep_info) * theory
    23 
    23 
    24   val domaindef_cmd: (bool * binding) * (binding * (string * string option) list * mixfix) * string
    24   val domaindef_cmd: (binding * (string * string option) list * mixfix) * string
    25     * (binding * binding) option -> theory -> theory
    25     * (binding * binding) option -> theory -> theory
    26 end
    26 end
    27 
    27 
    28 structure Domaindef : DOMAINDEF =
    28 structure Domaindef : DOMAINDEF =
    29 struct
    29 struct
    76 
    76 
    77 (* proving class instances *)
    77 (* proving class instances *)
    78 
    78 
    79 fun gen_add_domaindef
    79 fun gen_add_domaindef
    80       (prep_term: Proof.context -> 'a -> term)
    80       (prep_term: Proof.context -> 'a -> term)
    81       (def: bool)
       
    82       (name: binding)
       
    83       (typ as (tname, raw_args, _) : binding * (string * sort) list * mixfix)
    81       (typ as (tname, raw_args, _) : binding * (string * sort) list * mixfix)
    84       (raw_defl: 'a)
    82       (raw_defl: 'a)
    85       (opt_morphs: (binding * binding) option)
    83       (opt_morphs: (binding * binding) option)
    86       (thy: theory)
    84       (thy: theory)
    87     : (Typedef.info * Cpodef.cpo_info * Cpodef.pcpo_info * rep_info) * theory =
    85     : (Typedef.info * Cpodef.cpo_info * Cpodef.pcpo_info * rep_info) * theory =
   104     val full_tname = Sign.full_name thy tname
   102     val full_tname = Sign.full_name thy tname
   105     val newT = Type (full_tname, map TFree lhs_tfrees)
   103     val newT = Type (full_tname, map TFree lhs_tfrees)
   106 
   104 
   107     (*morphisms*)
   105     (*morphisms*)
   108     val morphs = opt_morphs
   106     val morphs = opt_morphs
   109       |> the_default (Binding.prefix_name "Rep_" name, Binding.prefix_name "Abs_" name)
   107       |> the_default (Binding.prefix_name "Rep_" tname, Binding.prefix_name "Abs_" tname)
   110 
   108 
   111     (*set*)
   109     (*set*)
   112     val set = @{term "defl_set :: udom defl => udom set"} $ defl
   110     val set = @{term "defl_set :: udom defl => udom set"} $ defl
   113 
   111 
   114     (*pcpodef*)
   112     (*pcpodef*)
   115     val tac1 = rtac @{thm defl_set_bottom} 1
   113     val tac1 = rtac @{thm defl_set_bottom} 1
   116     val tac2 = rtac @{thm adm_defl_set} 1
   114     val tac2 = rtac @{thm adm_defl_set} 1
   117     val ((info, cpo_info, pcpo_info), thy) = thy
   115     val ((info, cpo_info, pcpo_info), thy) = thy
   118       |> Cpodef.add_pcpodef def (SOME name) typ set (SOME morphs) (tac1, tac2)
   116       |> Cpodef.add_pcpodef typ set (SOME morphs) (tac1, tac2)
   119 
   117 
   120     (*definitions*)
   118     (*definitions*)
   121     val Rep_const = Const (#Rep_name (#1 info), newT --> udomT)
   119     val Rep_const = Const (#Rep_name (#1 info), newT --> udomT)
   122     val Abs_const = Const (#Abs_name (#1 info), udomT --> newT)
   120     val Abs_const = Const (#Abs_name (#1 info), udomT --> newT)
   123     val emb_eqn = Logic.mk_equals (emb_const newT, cabs_const (newT, udomT) $ Rep_const)
   121     val emb_eqn = Logic.mk_equals (emb_const newT, cabs_const (newT, udomT) $ Rep_const)
   132     val liftdefl_eqn =
   130     val liftdefl_eqn =
   133       Logic.mk_equals (liftdefl_const newT,
   131       Logic.mk_equals (liftdefl_const newT,
   134         Abs ("t", Term.itselfT newT,
   132         Abs ("t", Term.itselfT newT,
   135           mk_capply (@{const liftdefl_of}, defl_const newT $ Logic.mk_type newT)))
   133           mk_capply (@{const liftdefl_of}, defl_const newT $ Logic.mk_type newT)))
   136 
   134 
   137     val name_def = Thm.def_binding name
   135     val name_def = Thm.def_binding tname
   138     val emb_bind = (Binding.prefix_name "emb_" name_def, [])
   136     val emb_bind = (Binding.prefix_name "emb_" name_def, [])
   139     val prj_bind = (Binding.prefix_name "prj_" name_def, [])
   137     val prj_bind = (Binding.prefix_name "prj_" name_def, [])
   140     val defl_bind = (Binding.prefix_name "defl_" name_def, [])
   138     val defl_bind = (Binding.prefix_name "defl_" name_def, [])
   141     val liftemb_bind = (Binding.prefix_name "liftemb_" name_def, [])
   139     val liftemb_bind = (Binding.prefix_name "liftemb_" name_def, [])
   142     val liftprj_bind = (Binding.prefix_name "liftprj_" name_def, [])
   140     val liftprj_bind = (Binding.prefix_name "liftprj_" name_def, [])
   177       |> Local_Theory.exit_global
   175       |> Local_Theory.exit_global
   178 
   176 
   179     (*other theorems*)
   177     (*other theorems*)
   180     val defl_thm' = Thm.transfer thy defl_def
   178     val defl_thm' = Thm.transfer thy defl_def
   181     val (DEFL_thm, thy) = thy
   179     val (DEFL_thm, thy) = thy
   182       |> Sign.add_path (Binding.name_of name)
   180       |> Sign.add_path (Binding.name_of tname)
   183       |> Global_Theory.add_thm
   181       |> Global_Theory.add_thm
   184          ((Binding.prefix_name "DEFL_" name,
   182          ((Binding.prefix_name "DEFL_" tname,
   185           Drule.zero_var_indexes (@{thm typedef_DEFL} OF [defl_thm'])), [])
   183           Drule.zero_var_indexes (@{thm typedef_DEFL} OF [defl_thm'])), [])
   186       ||> Sign.restore_naming thy
   184       ||> Sign.restore_naming thy
   187 
   185 
   188     val rep_info =
   186     val rep_info =
   189       { emb_def = emb_def, prj_def = prj_def, defl_def = defl_def,
   187       { emb_def = emb_def, prj_def = prj_def, defl_def = defl_def,
   191         liftdefl_def = liftdefl_def, DEFL = DEFL_thm }
   189         liftdefl_def = liftdefl_def, DEFL = DEFL_thm }
   192   in
   190   in
   193     ((info, cpo_info, pcpo_info, rep_info), thy)
   191     ((info, cpo_info, pcpo_info, rep_info), thy)
   194   end
   192   end
   195   handle ERROR msg =>
   193   handle ERROR msg =>
   196     cat_error msg ("The error(s) above occurred in domaindef " ^ Binding.print name)
   194     cat_error msg ("The error(s) above occurred in domaindef " ^ Binding.print tname)
   197 
   195 
   198 fun add_domaindef def opt_name typ defl opt_morphs thy =
   196 fun add_domaindef typ defl opt_morphs thy =
   199   let
   197   gen_add_domaindef Syntax.check_term typ defl opt_morphs thy
   200     val name = the_default (#1 typ) opt_name
   198 
   201   in
   199 fun domaindef_cmd ((b, raw_args, mx), A, morphs) thy =
   202     gen_add_domaindef Syntax.check_term def name typ defl opt_morphs thy
       
   203   end
       
   204 
       
   205 fun domaindef_cmd ((def, name), (b, raw_args, mx), A, morphs) thy =
       
   206   let
   200   let
   207     val ctxt = Proof_Context.init_global thy
   201     val ctxt = Proof_Context.init_global thy
   208     val args = map (apsnd (Typedecl.read_constraint ctxt)) raw_args
   202     val args = map (apsnd (Typedecl.read_constraint ctxt)) raw_args
   209   in snd (gen_add_domaindef Syntax.read_term def name (b, args, mx) A morphs thy) end
   203   in snd (gen_add_domaindef Syntax.read_term (b, args, mx) A morphs thy) end
   210 
   204 
   211 
   205 
   212 (** outer syntax **)
   206 (** outer syntax **)
   213 
   207 
   214 val domaindef_decl =
   208 val domaindef_decl =
   215   Scan.optional (@{keyword "("} |--
   209   (Parse.type_args_constrained -- Parse.binding) --
   216       ((@{keyword "open"} >> K false) -- Scan.option Parse.binding ||
   210   Parse.opt_mixfix -- (@{keyword "="} |-- Parse.term) --
   217         Parse.binding >> (fn s => (true, SOME s)))
   211   Scan.option
   218         --| @{keyword ")"}) (true, NONE) --
   212     (@{keyword "morphisms"} |-- Parse.!!! (Parse.binding -- Parse.binding))
   219     (Parse.type_args_constrained -- Parse.binding) --
   213 
   220     Parse.opt_mixfix -- (@{keyword "="} |-- Parse.term) --
   214 fun mk_domaindef (((((args, t)), mx), A), morphs) =
   221     Scan.option (@{keyword "morphisms"} |-- Parse.!!! (Parse.binding -- Parse.binding))
   215   domaindef_cmd ((t, args, mx), A, morphs)
   222 
       
   223 fun mk_domaindef ((((((def, opt_name), (args, t)), mx), A), morphs)) =
       
   224   domaindef_cmd ((def, the_default t opt_name), (t, args, mx), A, morphs)
       
   225 
   216 
   226 val _ =
   217 val _ =
   227   Outer_Syntax.command @{command_spec "domaindef"} "HOLCF definition of domains from deflations"
   218   Outer_Syntax.command @{command_spec "domaindef"} "HOLCF definition of domains from deflations"
   228     (domaindef_decl >>
   219     (domaindef_decl >>
   229       (Toplevel.print oo (Toplevel.theory o mk_domaindef)))
   220       (Toplevel.print oo (Toplevel.theory o mk_domaindef)))