src/HOL/HOLCF/Tools/domaindef.ML
author huffman
Sun Dec 19 18:15:21 2010 -0800 (2010-12-19)
changeset 41296 6aaf80ea9715
parent 41292 2b7bc8d9fd6e
child 41436 480978f80eae
permissions -rw-r--r--
switch to transparent ascription, to avoid warning messages
huffman@33679
     1
(*  Title:      HOLCF/Tools/repdef.ML
huffman@33679
     2
    Author:     Brian Huffman
huffman@33679
     3
huffman@33679
     4
Defining representable domains using algebraic deflations.
huffman@33679
     5
*)
huffman@33679
     6
huffman@40575
     7
signature DOMAINDEF =
huffman@33679
     8
sig
huffman@33679
     9
  type rep_info =
huffman@40491
    10
    {
huffman@40491
    11
      emb_def : thm,
huffman@40491
    12
      prj_def : thm,
huffman@40491
    13
      defl_def : thm,
huffman@40491
    14
      liftemb_def : thm,
huffman@40491
    15
      liftprj_def : thm,
huffman@40491
    16
      liftdefl_def : thm,
huffman@40494
    17
      DEFL : thm
huffman@40491
    18
    }
huffman@33679
    19
huffman@40575
    20
  val add_domaindef: bool -> binding option -> binding * (string * sort) list * mixfix ->
huffman@33679
    21
    term -> (binding * binding) option -> theory ->
huffman@40772
    22
    (Typedef.info * Cpodef.cpo_info * Cpodef.pcpo_info * rep_info) * theory
huffman@33679
    23
huffman@40575
    24
  val domaindef_cmd: (bool * binding) * (binding * (string * string option) list * mixfix) * string
huffman@33679
    25
    * (binding * binding) option -> theory -> theory
huffman@40832
    26
end
huffman@33679
    27
huffman@41296
    28
structure Domaindef : DOMAINDEF =
huffman@33679
    29
struct
huffman@33679
    30
huffman@40832
    31
open HOLCF_Library
huffman@35527
    32
huffman@40832
    33
infixr 6 ->>
huffman@40832
    34
infix -->>
huffman@35527
    35
huffman@33679
    36
(** type definitions **)
huffman@33679
    37
huffman@33679
    38
type rep_info =
huffman@40491
    39
  {
huffman@40491
    40
    emb_def : thm,
huffman@40491
    41
    prj_def : thm,
huffman@40491
    42
    defl_def : thm,
huffman@40491
    43
    liftemb_def : thm,
huffman@40491
    44
    liftprj_def : thm,
huffman@40491
    45
    liftdefl_def : thm,
huffman@40494
    46
    DEFL : thm
huffman@40832
    47
  }
huffman@33679
    48
huffman@35527
    49
(* building types and terms *)
huffman@33679
    50
huffman@40832
    51
val udomT = @{typ udom}
huffman@41287
    52
val deflT = @{typ "udom defl"}
huffman@41292
    53
val udeflT = @{typ "udom u defl"}
huffman@40832
    54
fun emb_const T = Const (@{const_name emb}, T ->> udomT)
huffman@40832
    55
fun prj_const T = Const (@{const_name prj}, udomT ->> T)
huffman@40832
    56
fun defl_const T = Const (@{const_name defl}, Term.itselfT T --> deflT)
huffman@41292
    57
fun liftemb_const T = Const (@{const_name liftemb}, mk_upT T ->> mk_upT udomT)
huffman@41292
    58
fun liftprj_const T = Const (@{const_name liftprj}, mk_upT udomT ->> mk_upT T)
huffman@41292
    59
fun liftdefl_const T = Const (@{const_name liftdefl}, Term.itselfT T --> udeflT)
huffman@40491
    60
huffman@40491
    61
fun mk_u_map t =
huffman@40491
    62
  let
huffman@40832
    63
    val (T, U) = dest_cfunT (fastype_of t)
huffman@40832
    64
    val u_map_type = (T ->> U) ->> (mk_upT T ->> mk_upT U)
huffman@40832
    65
    val u_map_const = Const (@{const_name u_map}, u_map_type)
huffman@40491
    66
  in
huffman@40491
    67
    mk_capply (u_map_const, t)
huffman@40832
    68
  end
huffman@33679
    69
huffman@33679
    70
fun mk_cast (t, x) =
huffman@35527
    71
  capply_const (udomT, udomT)
huffman@41287
    72
  $ (capply_const (deflT, udomT ->> udomT) $ @{term "cast :: udom defl -> udom -> udom"} $ t)
huffman@40832
    73
  $ x
huffman@33679
    74
huffman@33679
    75
(* manipulating theorems *)
huffman@33679
    76
huffman@33679
    77
(* proving class instances *)
huffman@33679
    78
huffman@33679
    79
fun declare_type_name a =
huffman@40832
    80
  Variable.declare_constraints (Logic.mk_type (TFree (a, dummyS)))
huffman@33679
    81
huffman@40575
    82
fun gen_add_domaindef
huffman@33679
    83
      (prep_term: Proof.context -> 'a -> term)
huffman@33679
    84
      (def: bool)
huffman@33679
    85
      (name: binding)
wenzelm@35840
    86
      (typ as (tname, raw_args, mx) : binding * (string * sort) list * mixfix)
huffman@33679
    87
      (raw_defl: 'a)
huffman@33679
    88
      (opt_morphs: (binding * binding) option)
huffman@33679
    89
      (thy: theory)
huffman@40772
    90
    : (Typedef.info * Cpodef.cpo_info * Cpodef.pcpo_info * rep_info) * theory =
huffman@33679
    91
  let
huffman@40832
    92
    val _ = Theory.requires thy "Domain" "domaindefs"
huffman@33679
    93
huffman@33679
    94
    (*rhs*)
wenzelm@36153
    95
    val tmp_ctxt =
wenzelm@36610
    96
      ProofContext.init_global thy
huffman@40832
    97
      |> fold (Variable.declare_typ o TFree) raw_args
huffman@40832
    98
    val defl = prep_term tmp_ctxt raw_defl
huffman@40832
    99
    val tmp_ctxt = tmp_ctxt |> Variable.declare_constraints defl
wenzelm@35840
   100
huffman@40832
   101
    val deflT = Term.fastype_of defl
huffman@41287
   102
    val _ = if deflT = @{typ "udom defl"} then ()
huffman@40832
   103
            else error ("Not type defl: " ^ quote (Syntax.string_of_typ tmp_ctxt deflT))
huffman@33679
   104
huffman@33679
   105
    (*lhs*)
huffman@40832
   106
    val lhs_tfrees = map (ProofContext.check_tfree tmp_ctxt) raw_args
huffman@40832
   107
    val lhs_sorts = map snd lhs_tfrees
huffman@40832
   108
    val full_tname = Sign.full_name thy tname
huffman@40832
   109
    val newT = Type (full_tname, map TFree lhs_tfrees)
huffman@33679
   110
huffman@33679
   111
    (*morphisms*)
huffman@33679
   112
    val morphs = opt_morphs
huffman@40832
   113
      |> the_default (Binding.prefix_name "Rep_" name, Binding.prefix_name "Abs_" name)
huffman@33679
   114
huffman@33679
   115
    (*set*)
huffman@41287
   116
    val set = @{term "defl_set :: udom defl => udom => bool"} $ defl
huffman@33679
   117
huffman@33679
   118
    (*pcpodef*)
huffman@40832
   119
    val tac1 = rtac @{thm defl_set_bottom} 1
huffman@40832
   120
    val tac2 = rtac @{thm adm_defl_set} 1
huffman@35904
   121
    val ((info, cpo_info, pcpo_info), thy) = thy
huffman@40832
   122
      |> Cpodef.add_pcpodef def (SOME name) typ set (SOME morphs) (tac1, tac2)
huffman@33679
   123
huffman@33679
   124
    (*definitions*)
huffman@40832
   125
    val Rep_const = Const (#Rep_name (#1 info), newT --> udomT)
huffman@40832
   126
    val Abs_const = Const (#Abs_name (#1 info), udomT --> newT)
huffman@40832
   127
    val emb_eqn = Logic.mk_equals (emb_const newT, cabs_const (newT, udomT) $ Rep_const)
huffman@35527
   128
    val prj_eqn = Logic.mk_equals (prj_const newT, cabs_const (udomT, newT) $
huffman@40832
   129
      Abs ("x", udomT, Abs_const $ mk_cast (defl, Bound 0)))
huffman@39989
   130
    val defl_eqn = Logic.mk_equals (defl_const newT,
huffman@40832
   131
      Abs ("x", Term.itselfT newT, defl))
huffman@40491
   132
    val liftemb_eqn =
huffman@41292
   133
      Logic.mk_equals (liftemb_const newT, mk_u_map (emb_const newT))
huffman@40491
   134
    val liftprj_eqn =
huffman@41292
   135
      Logic.mk_equals (liftprj_const newT, mk_u_map (prj_const newT))
huffman@40491
   136
    val liftdefl_eqn =
huffman@40491
   137
      Logic.mk_equals (liftdefl_const newT,
huffman@40491
   138
        Abs ("t", Term.itselfT newT,
huffman@41292
   139
          mk_capply (@{const pdefl}, defl_const newT $ Logic.mk_type newT)))
huffman@40491
   140
huffman@40832
   141
    val name_def = Binding.suffix_name "_def" name
huffman@40832
   142
    val emb_bind = (Binding.prefix_name "emb_" name_def, [])
huffman@40832
   143
    val prj_bind = (Binding.prefix_name "prj_" name_def, [])
huffman@40832
   144
    val defl_bind = (Binding.prefix_name "defl_" name_def, [])
huffman@40832
   145
    val liftemb_bind = (Binding.prefix_name "liftemb_" name_def, [])
huffman@40832
   146
    val liftprj_bind = (Binding.prefix_name "liftprj_" name_def, [])
huffman@40832
   147
    val liftdefl_bind = (Binding.prefix_name "liftdefl_" name_def, [])
huffman@33679
   148
huffman@33679
   149
    (*instantiate class rep*)
huffman@35904
   150
    val lthy = thy
huffman@41292
   151
      |> Class.instantiation ([full_tname], lhs_tfrees, @{sort domain})
huffman@35904
   152
    val ((_, (_, emb_ldef)), lthy) =
huffman@40832
   153
        Specification.definition (NONE, (emb_bind, emb_eqn)) lthy
huffman@35904
   154
    val ((_, (_, prj_ldef)), lthy) =
huffman@40832
   155
        Specification.definition (NONE, (prj_bind, prj_eqn)) lthy
huffman@39989
   156
    val ((_, (_, defl_ldef)), lthy) =
huffman@40832
   157
        Specification.definition (NONE, (defl_bind, defl_eqn)) lthy
huffman@40491
   158
    val ((_, (_, liftemb_ldef)), lthy) =
huffman@40832
   159
        Specification.definition (NONE, (liftemb_bind, liftemb_eqn)) lthy
huffman@40491
   160
    val ((_, (_, liftprj_ldef)), lthy) =
huffman@40832
   161
        Specification.definition (NONE, (liftprj_bind, liftprj_eqn)) lthy
huffman@40491
   162
    val ((_, (_, liftdefl_ldef)), lthy) =
huffman@40832
   163
        Specification.definition (NONE, (liftdefl_bind, liftdefl_eqn)) lthy
huffman@40832
   164
    val ctxt_thy = ProofContext.init_global (ProofContext.theory_of lthy)
huffman@40832
   165
    val emb_def = singleton (ProofContext.export lthy ctxt_thy) emb_ldef
huffman@40832
   166
    val prj_def = singleton (ProofContext.export lthy ctxt_thy) prj_ldef
huffman@40832
   167
    val defl_def = singleton (ProofContext.export lthy ctxt_thy) defl_ldef
huffman@40832
   168
    val liftemb_def = singleton (ProofContext.export lthy ctxt_thy) liftemb_ldef
huffman@40832
   169
    val liftprj_def = singleton (ProofContext.export lthy ctxt_thy) liftprj_ldef
huffman@40832
   170
    val liftdefl_def = singleton (ProofContext.export lthy ctxt_thy) liftdefl_ldef
huffman@33826
   171
    val type_definition_thm =
wenzelm@41228
   172
      Raw_Simplifier.rewrite_rule
wenzelm@35994
   173
        (the_list (#set_def (#2 info)))
huffman@40832
   174
        (#type_definition (#2 info))
huffman@33679
   175
    val typedef_thms =
huffman@40491
   176
      [type_definition_thm, #below_def cpo_info, emb_def, prj_def, defl_def,
huffman@40832
   177
      liftemb_def, liftprj_def, liftdefl_def]
huffman@35904
   178
    val thy = lthy
huffman@33679
   179
      |> Class.prove_instantiation_instance
huffman@41292
   180
          (K (Tactic.rtac (@{thm typedef_domain_class} OF typedef_thms) 1))
huffman@40832
   181
      |> Local_Theory.exit_global
huffman@33679
   182
huffman@33679
   183
    (*other theorems*)
huffman@40832
   184
    val defl_thm' = Thm.transfer thy defl_def
huffman@39989
   185
    val (DEFL_thm, thy) = thy
huffman@33679
   186
      |> Sign.add_path (Binding.name_of name)
wenzelm@39557
   187
      |> Global_Theory.add_thm
huffman@39989
   188
         ((Binding.prefix_name "DEFL_" name,
huffman@39989
   189
          Drule.zero_var_indexes (@{thm typedef_DEFL} OF [defl_thm'])), [])
huffman@40832
   190
      ||> Sign.restore_naming thy
huffman@33679
   191
huffman@33679
   192
    val rep_info =
huffman@40491
   193
      { emb_def = emb_def, prj_def = prj_def, defl_def = defl_def,
huffman@40491
   194
        liftemb_def = liftemb_def, liftprj_def = liftprj_def,
huffman@40832
   195
        liftdefl_def = liftdefl_def, DEFL = DEFL_thm }
huffman@33679
   196
  in
huffman@35904
   197
    ((info, cpo_info, pcpo_info, rep_info), thy)
huffman@33679
   198
  end
huffman@33679
   199
  handle ERROR msg =>
huffman@40832
   200
    cat_error msg ("The error(s) above occurred in domaindef " ^ quote (Binding.str_of name))
huffman@33679
   201
huffman@40575
   202
fun add_domaindef def opt_name typ defl opt_morphs thy =
huffman@33679
   203
  let
huffman@40832
   204
    val name = the_default (#1 typ) opt_name
huffman@33679
   205
  in
huffman@40575
   206
    gen_add_domaindef Syntax.check_term def name typ defl opt_morphs thy
huffman@40832
   207
  end
huffman@33679
   208
huffman@40575
   209
fun domaindef_cmd ((def, name), (b, raw_args, mx), A, morphs) thy =
wenzelm@35840
   210
  let
huffman@40832
   211
    val ctxt = ProofContext.init_global thy
huffman@40832
   212
    val args = map (apsnd (Typedecl.read_constraint ctxt)) raw_args
huffman@40832
   213
  in snd (gen_add_domaindef Syntax.read_term def name (b, args, mx) A morphs thy) end
wenzelm@35840
   214
huffman@33679
   215
huffman@33679
   216
(** outer syntax **)
huffman@33679
   217
huffman@40575
   218
val domaindef_decl =
wenzelm@36960
   219
  Scan.optional (Parse.$$$ "(" |--
wenzelm@36960
   220
      ((Parse.$$$ "open" >> K false) -- Scan.option Parse.binding ||
wenzelm@36960
   221
        Parse.binding >> (fn s => (true, SOME s)))
wenzelm@36960
   222
        --| Parse.$$$ ")") (true, NONE) --
wenzelm@36960
   223
    (Parse.type_args_constrained -- Parse.binding) --
wenzelm@36960
   224
    Parse.opt_mixfix -- (Parse.$$$ "=" |-- Parse.term) --
huffman@40832
   225
    Scan.option (Parse.$$$ "morphisms" |-- Parse.!!! (Parse.binding -- Parse.binding))
huffman@33679
   226
huffman@40575
   227
fun mk_domaindef ((((((def, opt_name), (args, t)), mx), A), morphs)) =
huffman@40832
   228
  domaindef_cmd ((def, the_default t opt_name), (t, args, mx), A, morphs)
huffman@33679
   229
huffman@33679
   230
val _ =
huffman@40575
   231
  Outer_Syntax.command "domaindef" "HOLCF definition of domains from deflations" Keyword.thy_decl
huffman@40575
   232
    (domaindef_decl >>
huffman@40832
   233
      (Toplevel.print oo (Toplevel.theory o mk_domaindef)))
huffman@33679
   234
huffman@40832
   235
end