src/HOLCF/Tools/repdef.ML
author wenzelm
Sat Mar 27 21:38:38 2010 +0100 (2010-03-27)
changeset 35994 9cc3df9a606e
parent 35904 0c13e28e5e41
child 36153 1ac501e16a6a
permissions -rw-r--r--
Typedef.info: separate global and local part, only the latter is transformed by morphisms;
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@33679
     7
signature REPDEF =
huffman@33679
     8
sig
huffman@33679
     9
  type rep_info =
huffman@33679
    10
    { emb_def: thm, prj_def: thm, approx_def: thm, REP: thm }
huffman@33679
    11
wenzelm@35840
    12
  val add_repdef: bool -> binding option -> binding * (string * sort) list * mixfix ->
huffman@33679
    13
    term -> (binding * binding) option -> theory ->
huffman@33679
    14
    (Typedef.info * Pcpodef.cpo_info * Pcpodef.pcpo_info * rep_info) * theory
huffman@33679
    15
wenzelm@35840
    16
  val repdef_cmd: (bool * binding) * (binding * (string * string option) list * mixfix) * string
huffman@33679
    17
    * (binding * binding) option -> theory -> theory
huffman@33679
    18
end;
huffman@33679
    19
huffman@33679
    20
structure Repdef :> REPDEF =
huffman@33679
    21
struct
huffman@33679
    22
huffman@35527
    23
open HOLCF_Library;
huffman@35527
    24
huffman@35527
    25
infixr 6 ->>;
huffman@35527
    26
infix -->>;
huffman@35527
    27
huffman@33679
    28
(** type definitions **)
huffman@33679
    29
huffman@33679
    30
type rep_info =
huffman@33679
    31
  { emb_def: thm, prj_def: thm, approx_def: thm, REP: thm };
huffman@33679
    32
huffman@35527
    33
(* building types and terms *)
huffman@33679
    34
huffman@33679
    35
val udomT = @{typ udom};
huffman@33679
    36
fun alg_deflT T = Type (@{type_name alg_defl}, [T]);
huffman@35527
    37
fun emb_const T = Const (@{const_name emb}, T ->> udomT);
huffman@35527
    38
fun prj_const T = Const (@{const_name prj}, udomT ->> T);
huffman@35527
    39
fun approx_const T = Const (@{const_name approx}, natT --> (T ->> T));
huffman@33679
    40
huffman@35527
    41
fun cast_const T = Const (@{const_name cast}, alg_deflT T ->> T ->> T);
huffman@33679
    42
fun mk_cast (t, x) =
huffman@35527
    43
  capply_const (udomT, udomT)
huffman@35527
    44
  $ (capply_const (alg_deflT udomT, udomT ->> udomT) $ cast_const udomT $ t)
huffman@33679
    45
  $ x;
huffman@33679
    46
huffman@33679
    47
(* manipulating theorems *)
huffman@33679
    48
huffman@33679
    49
(* proving class instances *)
huffman@33679
    50
huffman@33679
    51
fun declare_type_name a =
huffman@33679
    52
  Variable.declare_constraints (Logic.mk_type (TFree (a, dummyS)));
huffman@33679
    53
huffman@33679
    54
fun gen_add_repdef
huffman@33679
    55
      (prep_term: Proof.context -> 'a -> term)
huffman@33679
    56
      (def: bool)
huffman@33679
    57
      (name: binding)
wenzelm@35840
    58
      (typ as (tname, raw_args, mx) : binding * (string * sort) list * mixfix)
huffman@33679
    59
      (raw_defl: 'a)
huffman@33679
    60
      (opt_morphs: (binding * binding) option)
huffman@33679
    61
      (thy: theory)
huffman@33679
    62
    : (Typedef.info * Pcpodef.cpo_info * Pcpodef.pcpo_info * rep_info) * theory =
huffman@33679
    63
  let
huffman@33679
    64
    val _ = Theory.requires thy "Representable" "repdefs";
huffman@33679
    65
huffman@33679
    66
    (*rhs*)
wenzelm@35840
    67
    val (_, tmp_lthy) =
wenzelm@35840
    68
      thy |> Theory.copy |> Theory_Target.init NONE
wenzelm@35840
    69
      |> Typedecl.predeclare_constraints (tname, raw_args, mx);
wenzelm@35840
    70
    val defl = prep_term tmp_lthy raw_defl;
huffman@35904
    71
    val tmp_lthy = tmp_lthy |> Variable.declare_constraints defl;
wenzelm@35840
    72
huffman@33679
    73
    val deflT = Term.fastype_of defl;
huffman@33679
    74
    val _ = if deflT = @{typ "udom alg_defl"} then ()
wenzelm@35840
    75
            else error ("Not type udom alg_defl: " ^ quote (Syntax.string_of_typ tmp_lthy deflT));
huffman@33679
    76
huffman@33679
    77
    (*lhs*)
huffman@35904
    78
    val lhs_tfrees = map (fn (a, _) => (a, ProofContext.default_sort tmp_lthy (a, ~1))) raw_args;
huffman@33679
    79
    val lhs_sorts = map snd lhs_tfrees;
huffman@33679
    80
    val full_tname = Sign.full_name thy tname;
huffman@33679
    81
    val newT = Type (full_tname, map TFree lhs_tfrees);
huffman@33679
    82
huffman@33679
    83
    (*morphisms*)
huffman@33679
    84
    val morphs = opt_morphs
huffman@33679
    85
      |> the_default (Binding.prefix_name "Rep_" name, Binding.prefix_name "Abs_" name);
huffman@33679
    86
huffman@33679
    87
    (*set*)
huffman@33679
    88
    val in_defl = @{term "in_deflation :: udom => udom alg_defl => bool"};
huffman@33679
    89
    val set = HOLogic.Collect_const udomT $ Abs ("x", udomT, in_defl $ Bound 0 $ defl);
huffman@33679
    90
huffman@33679
    91
    (*pcpodef*)
huffman@33679
    92
    val tac1 = rtac @{thm CollectI} 1 THEN rtac @{thm bottom_in_deflation} 1;
huffman@33679
    93
    val tac2 = rtac @{thm adm_mem_Collect_in_deflation} 1;
huffman@35904
    94
    val ((info, cpo_info, pcpo_info), thy) = thy
huffman@33679
    95
      |> Pcpodef.add_pcpodef def (SOME name) typ set (SOME morphs) (tac1, tac2);
huffman@33679
    96
huffman@33679
    97
    (*definitions*)
wenzelm@35994
    98
    val Rep_const = Const (#Rep_name (#1 info), newT --> udomT);
wenzelm@35994
    99
    val Abs_const = Const (#Abs_name (#1 info), udomT --> newT);
huffman@35527
   100
    val emb_eqn = Logic.mk_equals (emb_const newT, cabs_const (newT, udomT) $ Rep_const);
huffman@35527
   101
    val prj_eqn = Logic.mk_equals (prj_const newT, cabs_const (udomT, newT) $
huffman@33679
   102
      Abs ("x", udomT, Abs_const $ mk_cast (defl, Bound 0)));
huffman@33679
   103
    val repdef_approx_const =
huffman@33679
   104
      Const (@{const_name repdef_approx}, (newT --> udomT) --> (udomT --> newT)
huffman@35527
   105
        --> alg_deflT udomT --> natT --> (newT ->> newT));
huffman@33679
   106
    val approx_eqn = Logic.mk_equals (approx_const newT,
huffman@33679
   107
      repdef_approx_const $ Rep_const $ Abs_const $ defl);
huffman@35904
   108
    val name_def = Binding.suffix_name "_def" name;
huffman@35904
   109
    val emb_bind = (Binding.prefix_name "emb_" name_def, []);
huffman@35904
   110
    val prj_bind = (Binding.prefix_name "prj_" name_def, []);
huffman@35904
   111
    val approx_bind = (Binding.prefix_name "approx_" name_def, []);
huffman@33679
   112
huffman@33679
   113
    (*instantiate class rep*)
huffman@35904
   114
    val lthy = thy
huffman@35904
   115
      |> Theory_Target.instantiation ([full_tname], lhs_tfrees, @{sort rep});
huffman@35904
   116
    val ((_, (_, emb_ldef)), lthy) =
huffman@35904
   117
        Specification.definition (NONE, (emb_bind, emb_eqn)) lthy;
huffman@35904
   118
    val ((_, (_, prj_ldef)), lthy) =
huffman@35904
   119
        Specification.definition (NONE, (prj_bind, prj_eqn)) lthy;
huffman@35904
   120
    val ((_, (_, approx_ldef)), lthy) =
huffman@35904
   121
        Specification.definition (NONE, (approx_bind, approx_eqn)) lthy;
huffman@35904
   122
    val ctxt_thy = ProofContext.init (ProofContext.theory_of lthy);
huffman@35904
   123
    val emb_def = singleton (ProofContext.export lthy ctxt_thy) emb_ldef;
huffman@35904
   124
    val prj_def = singleton (ProofContext.export lthy ctxt_thy) prj_ldef;
huffman@35904
   125
    val approx_def = singleton (ProofContext.export lthy ctxt_thy) approx_ldef;
huffman@33826
   126
    val type_definition_thm =
huffman@33826
   127
      MetaSimplifier.rewrite_rule
wenzelm@35994
   128
        (the_list (#set_def (#2 info)))
wenzelm@35994
   129
        (#type_definition (#2 info));
huffman@33679
   130
    val typedef_thms =
huffman@33826
   131
      [type_definition_thm, #below_def cpo_info, emb_def, prj_def, approx_def];
huffman@35904
   132
    val thy = lthy
huffman@33679
   133
      |> Class.prove_instantiation_instance
huffman@33679
   134
          (K (Tactic.rtac (@{thm typedef_rep_class} OF typedef_thms) 1))
huffman@33681
   135
      |> Local_Theory.exit_global;
huffman@33679
   136
huffman@33679
   137
    (*other theorems*)
huffman@35904
   138
    val typedef_thms' = map (Thm.transfer thy)
huffman@33826
   139
      [type_definition_thm, #below_def cpo_info, emb_def, prj_def];
huffman@35904
   140
    val (REP_thm, thy) = thy
huffman@33679
   141
      |> Sign.add_path (Binding.name_of name)
huffman@35904
   142
      |> PureThy.add_thm
huffman@35904
   143
         ((Binding.prefix_name "REP_" name,
huffman@35904
   144
          Drule.export_without_context (@{thm typedef_REP} OF typedef_thms')), [])
huffman@35904
   145
      ||> Sign.restore_naming thy;
huffman@33679
   146
huffman@33679
   147
    val rep_info =
huffman@33679
   148
      { emb_def = emb_def, prj_def = prj_def, approx_def = approx_def, REP = REP_thm };
huffman@33679
   149
  in
huffman@35904
   150
    ((info, cpo_info, pcpo_info, rep_info), thy)
huffman@33679
   151
  end
huffman@33679
   152
  handle ERROR msg =>
huffman@33679
   153
    cat_error msg ("The error(s) above occurred in repdef " ^ quote (Binding.str_of name));
huffman@33679
   154
huffman@33679
   155
fun add_repdef def opt_name typ defl opt_morphs thy =
huffman@33679
   156
  let
huffman@33679
   157
    val name = the_default (#1 typ) opt_name;
huffman@33679
   158
  in
huffman@33679
   159
    gen_add_repdef Syntax.check_term def name typ defl opt_morphs thy
huffman@33679
   160
  end;
huffman@33679
   161
wenzelm@35840
   162
fun repdef_cmd ((def, name), (b, raw_args, mx), A, morphs) thy =
wenzelm@35840
   163
  let
wenzelm@35840
   164
    val ctxt = ProofContext.init thy;
wenzelm@35840
   165
    val args = map (apsnd (Typedecl.read_constraint ctxt)) raw_args;
wenzelm@35840
   166
  in snd (gen_add_repdef Syntax.read_term def name (b, args, mx) A morphs thy) end;
wenzelm@35840
   167
huffman@33679
   168
huffman@33679
   169
(** outer syntax **)
huffman@33679
   170
huffman@33679
   171
local structure P = OuterParse and K = OuterKeyword in
huffman@33679
   172
huffman@33679
   173
val repdef_decl =
huffman@33679
   174
  Scan.optional (P.$$$ "(" |--
huffman@33679
   175
      ((P.$$$ "open" >> K false) -- Scan.option P.binding || P.binding >> (fn s => (true, SOME s)))
huffman@33679
   176
        --| P.$$$ ")") (true, NONE) --
wenzelm@35840
   177
    (P.type_args_constrained -- P.binding) -- P.opt_mixfix -- (P.$$$ "=" |-- P.term) --
huffman@33679
   178
    Scan.option (P.$$$ "morphisms" |-- P.!!! (P.binding -- P.binding));
huffman@33679
   179
wenzelm@35840
   180
fun mk_repdef ((((((def, opt_name), (args, t)), mx), A), morphs)) =
wenzelm@35840
   181
  repdef_cmd ((def, the_default t opt_name), (t, args, mx), A, morphs);
huffman@33679
   182
huffman@33679
   183
val _ =
huffman@33812
   184
  OuterSyntax.command "repdef" "HOLCF definition of representable domains" K.thy_decl
huffman@33679
   185
    (repdef_decl >>
huffman@33679
   186
      (Toplevel.print oo (Toplevel.theory o mk_repdef)));
huffman@33679
   187
huffman@33679
   188
end;
huffman@33679
   189
huffman@33679
   190
end;