src/HOLCF/Tools/repdef.ML
author huffman
Fri Nov 13 15:31:20 2009 -0800 (2009-11-13)
changeset 33679 331712879666
child 33681 cddea85bc87b
permissions -rw-r--r--
automate definition of representable domains from algebraic deflations
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
huffman@33679
    12
  val add_repdef: bool -> binding option -> binding * string 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
huffman@33679
    16
  val repdef_cmd: (bool * binding) * (binding * string 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@33679
    23
(** type definitions **)
huffman@33679
    24
huffman@33679
    25
type rep_info =
huffman@33679
    26
  { emb_def: thm, prj_def: thm, approx_def: thm, REP: thm };
huffman@33679
    27
huffman@33679
    28
(* building terms *)
huffman@33679
    29
huffman@33679
    30
fun adm_const T = Const (@{const_name adm}, (T --> HOLogic.boolT) --> HOLogic.boolT);
huffman@33679
    31
fun mk_adm (x, T, P) = adm_const T $ absfree (x, T, P);
huffman@33679
    32
huffman@33679
    33
fun below_const T = Const (@{const_name below}, T --> T --> HOLogic.boolT);
huffman@33679
    34
huffman@33679
    35
val natT = @{typ nat};
huffman@33679
    36
val udomT = @{typ udom};
huffman@33679
    37
fun alg_deflT T = Type (@{type_name alg_defl}, [T]);
huffman@33679
    38
fun cfunT (T, U) = Type (@{type_name "->"}, [T, U]);
huffman@33679
    39
fun emb_const T = Const (@{const_name emb}, cfunT (T, udomT));
huffman@33679
    40
fun prj_const T = Const (@{const_name prj}, cfunT (udomT, T));
huffman@33679
    41
fun approx_const T = Const (@{const_name approx}, natT --> cfunT (T, T));
huffman@33679
    42
huffman@33679
    43
fun LAM_const (T, U) = Const (@{const_name Abs_CFun}, (T --> U) --> cfunT (T, U));
huffman@33679
    44
fun APP_const (T, U) = Const (@{const_name Rep_CFun}, cfunT (T, U) --> (T --> U));
huffman@33679
    45
fun cast_const T = Const (@{const_name cast}, cfunT (alg_deflT T, cfunT (T, T)));
huffman@33679
    46
fun mk_cast (t, x) =
huffman@33679
    47
  APP_const (udomT, udomT)
huffman@33679
    48
  $ (APP_const (alg_deflT udomT, cfunT (udomT, udomT)) $ cast_const udomT $ t)
huffman@33679
    49
  $ x;
huffman@33679
    50
huffman@33679
    51
(* manipulating theorems *)
huffman@33679
    52
huffman@33679
    53
(* proving class instances *)
huffman@33679
    54
huffman@33679
    55
fun declare_type_name a =
huffman@33679
    56
  Variable.declare_constraints (Logic.mk_type (TFree (a, dummyS)));
huffman@33679
    57
huffman@33679
    58
fun gen_add_repdef
huffman@33679
    59
      (prep_term: Proof.context -> 'a -> term)
huffman@33679
    60
      (def: bool)
huffman@33679
    61
      (name: binding)
huffman@33679
    62
      (typ as (t, vs, mx) : binding * string list * mixfix)
huffman@33679
    63
      (raw_defl: 'a)
huffman@33679
    64
      (opt_morphs: (binding * binding) option)
huffman@33679
    65
      (thy: theory)
huffman@33679
    66
    : (Typedef.info * Pcpodef.cpo_info * Pcpodef.pcpo_info * rep_info) * theory =
huffman@33679
    67
  let
huffman@33679
    68
    val _ = Theory.requires thy "Representable" "repdefs";
huffman@33679
    69
    val ctxt = ProofContext.init thy;
huffman@33679
    70
huffman@33679
    71
    (*rhs*)
huffman@33679
    72
    val defl = prep_term (ctxt |> fold declare_type_name vs) raw_defl;
huffman@33679
    73
    val deflT = Term.fastype_of defl;
huffman@33679
    74
    val _ = if deflT = @{typ "udom alg_defl"} then ()
huffman@33679
    75
            else error ("Not type udom alg_defl: " ^ quote (Syntax.string_of_typ ctxt deflT));
huffman@33679
    76
    val rhs_tfrees = Term.add_tfrees defl [];
huffman@33679
    77
huffman@33679
    78
    (*lhs*)
huffman@33679
    79
    val defS = Sign.defaultS thy;
huffman@33679
    80
    val lhs_tfrees = map (fn v => (v, the_default defS (AList.lookup (op =) rhs_tfrees v))) vs;
huffman@33679
    81
    val lhs_sorts = map snd lhs_tfrees;
huffman@33679
    82
    val tname = Binding.map_name (Syntax.type_name mx) t;
huffman@33679
    83
    val full_tname = Sign.full_name thy tname;
huffman@33679
    84
    val newT = Type (full_tname, map TFree lhs_tfrees);
huffman@33679
    85
huffman@33679
    86
    (*morphisms*)
huffman@33679
    87
    val morphs = opt_morphs
huffman@33679
    88
      |> the_default (Binding.prefix_name "Rep_" name, Binding.prefix_name "Abs_" name);
huffman@33679
    89
huffman@33679
    90
    (*set*)
huffman@33679
    91
    val in_defl = @{term "in_deflation :: udom => udom alg_defl => bool"};
huffman@33679
    92
    val set = HOLogic.Collect_const udomT $ Abs ("x", udomT, in_defl $ Bound 0 $ defl);
huffman@33679
    93
huffman@33679
    94
    (*pcpodef*)
huffman@33679
    95
    val tac1 = rtac @{thm CollectI} 1 THEN rtac @{thm bottom_in_deflation} 1;
huffman@33679
    96
    val tac2 = rtac @{thm adm_mem_Collect_in_deflation} 1;
huffman@33679
    97
    val ((info, cpo_info, pcpo_info), thy2) = thy
huffman@33679
    98
      |> Pcpodef.add_pcpodef def (SOME name) typ set (SOME morphs) (tac1, tac2);
huffman@33679
    99
huffman@33679
   100
    (*definitions*)
huffman@33679
   101
    val Rep_const = Const (#Rep_name info, newT --> udomT);
huffman@33679
   102
    val Abs_const = Const (#Abs_name info, udomT --> newT);
huffman@33679
   103
    val emb_eqn = Logic.mk_equals (emb_const newT, LAM_const (newT, udomT) $ Rep_const);
huffman@33679
   104
    val prj_eqn = Logic.mk_equals (prj_const newT, LAM_const (udomT, newT) $
huffman@33679
   105
      Abs ("x", udomT, Abs_const $ mk_cast (defl, Bound 0)));
huffman@33679
   106
    val repdef_approx_const =
huffman@33679
   107
      Const (@{const_name repdef_approx}, (newT --> udomT) --> (udomT --> newT)
huffman@33679
   108
        --> alg_deflT udomT --> natT --> cfunT (newT, newT));
huffman@33679
   109
    val approx_eqn = Logic.mk_equals (approx_const newT,
huffman@33679
   110
      repdef_approx_const $ Rep_const $ Abs_const $ defl);
huffman@33679
   111
huffman@33679
   112
    (*instantiate class rep*)
huffman@33679
   113
    val name_def = Binding.suffix_name "_def" name;
huffman@33679
   114
    val ([emb_ldef, prj_ldef, approx_ldef], lthy3) = thy2
huffman@33679
   115
      |> Theory_Target.instantiation ([full_tname], lhs_tfrees, @{sort rep})
huffman@33679
   116
      |> fold_map Specification.definition
huffman@33679
   117
          [ (NONE, ((Binding.prefix_name "emb_" name_def, []), emb_eqn))
huffman@33679
   118
          , (NONE, ((Binding.prefix_name "prj_" name_def, []), prj_eqn))
huffman@33679
   119
          , (NONE, ((Binding.prefix_name "approx_" name_def, []), approx_eqn)) ]
huffman@33679
   120
      |>> map (snd o snd);
huffman@33679
   121
    val ctxt_thy = ProofContext.init (ProofContext.theory_of lthy3);
huffman@33679
   122
    val [emb_def, prj_def, approx_def] =
huffman@33679
   123
      ProofContext.export lthy3 ctxt_thy [emb_ldef, prj_ldef, approx_ldef];
huffman@33679
   124
    val typedef_thms =
huffman@33679
   125
      [#type_definition info, #below_def cpo_info, emb_def, prj_def, approx_def];
huffman@33679
   126
    val thy4 = lthy3
huffman@33679
   127
      |> Class.prove_instantiation_instance
huffman@33679
   128
          (K (Tactic.rtac (@{thm typedef_rep_class} OF typedef_thms) 1))
huffman@33679
   129
      |> LocalTheory.exit_global;
huffman@33679
   130
huffman@33679
   131
    (*other theorems*)
huffman@33679
   132
    val typedef_thms' = map (Thm.transfer thy4)
huffman@33679
   133
      [#type_definition info, #below_def cpo_info, emb_def, prj_def];
huffman@33679
   134
    val ([REP_thm], thy5) = thy4
huffman@33679
   135
      |> Sign.add_path (Binding.name_of name)
huffman@33679
   136
      |> PureThy.add_thms
huffman@33679
   137
        [((Binding.prefix_name "REP_" name,
huffman@33679
   138
          Drule.standard (@{thm typedef_REP} OF typedef_thms')), [])]
huffman@33679
   139
      ||> Sign.parent_path;
huffman@33679
   140
huffman@33679
   141
    val rep_info =
huffman@33679
   142
      { emb_def = emb_def, prj_def = prj_def, approx_def = approx_def, REP = REP_thm };
huffman@33679
   143
  in
huffman@33679
   144
    ((info, cpo_info, pcpo_info, rep_info), thy5)
huffman@33679
   145
  end
huffman@33679
   146
  handle ERROR msg =>
huffman@33679
   147
    cat_error msg ("The error(s) above occurred in repdef " ^ quote (Binding.str_of name));
huffman@33679
   148
huffman@33679
   149
fun add_repdef def opt_name typ defl opt_morphs thy =
huffman@33679
   150
  let
huffman@33679
   151
    val name = the_default (#1 typ) opt_name;
huffman@33679
   152
  in
huffman@33679
   153
    gen_add_repdef Syntax.check_term def name typ defl opt_morphs thy
huffman@33679
   154
  end;
huffman@33679
   155
huffman@33679
   156
fun repdef_cmd ((def, name), typ, A, morphs) =
huffman@33679
   157
  snd o gen_add_repdef Syntax.read_term def name typ A morphs;
huffman@33679
   158
huffman@33679
   159
(** outer syntax **)
huffman@33679
   160
huffman@33679
   161
local structure P = OuterParse and K = OuterKeyword in
huffman@33679
   162
huffman@33679
   163
val repdef_decl =
huffman@33679
   164
  Scan.optional (P.$$$ "(" |--
huffman@33679
   165
      ((P.$$$ "open" >> K false) -- Scan.option P.binding || P.binding >> (fn s => (true, SOME s)))
huffman@33679
   166
        --| P.$$$ ")") (true, NONE) --
huffman@33679
   167
    (P.type_args -- P.binding) -- P.opt_infix -- (P.$$$ "=" |-- P.term) --
huffman@33679
   168
    Scan.option (P.$$$ "morphisms" |-- P.!!! (P.binding -- P.binding));
huffman@33679
   169
huffman@33679
   170
fun mk_repdef ((((((def, opt_name), (vs, t)), mx), A), morphs)) =
huffman@33679
   171
  repdef_cmd
huffman@33679
   172
    ((def, the_default (Binding.map_name (Syntax.type_name mx) t) opt_name), (t, vs, mx), A, morphs);
huffman@33679
   173
huffman@33679
   174
val _ =
huffman@33679
   175
  OuterSyntax.command "repdef" "HOLCF definition of representable domains" K.thy_goal
huffman@33679
   176
    (repdef_decl >>
huffman@33679
   177
      (Toplevel.print oo (Toplevel.theory o mk_repdef)));
huffman@33679
   178
huffman@33679
   179
end;
huffman@33679
   180
huffman@33679
   181
end;