src/HOLCF/Tools/repdef.ML
author huffman
Fri, 12 Nov 2010 06:05:26 -0800
changeset 40510 638943ad5bdc
parent 40494 db8a09daba7b
permissions -rw-r--r--
update Theory.requires with new theory name
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
33679
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
     1
(*  Title:      HOLCF/Tools/repdef.ML
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
     2
    Author:     Brian Huffman
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
     3
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
     4
Defining representable domains using algebraic deflations.
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
     5
*)
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
     6
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
     7
signature REPDEF =
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
     8
sig
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
     9
  type rep_info =
40491
6de5839e2fb3 add 'predomain' class: unpointed version of bifinite
huffman
parents: 40038
diff changeset
    10
    {
6de5839e2fb3 add 'predomain' class: unpointed version of bifinite
huffman
parents: 40038
diff changeset
    11
      emb_def : thm,
6de5839e2fb3 add 'predomain' class: unpointed version of bifinite
huffman
parents: 40038
diff changeset
    12
      prj_def : thm,
6de5839e2fb3 add 'predomain' class: unpointed version of bifinite
huffman
parents: 40038
diff changeset
    13
      defl_def : thm,
6de5839e2fb3 add 'predomain' class: unpointed version of bifinite
huffman
parents: 40038
diff changeset
    14
      liftemb_def : thm,
6de5839e2fb3 add 'predomain' class: unpointed version of bifinite
huffman
parents: 40038
diff changeset
    15
      liftprj_def : thm,
6de5839e2fb3 add 'predomain' class: unpointed version of bifinite
huffman
parents: 40038
diff changeset
    16
      liftdefl_def : thm,
40494
db8a09daba7b add class liftdomain, for bifinite domains where DEFL('a u) = u_defl('a)
huffman
parents: 40491
diff changeset
    17
      DEFL : thm
40491
6de5839e2fb3 add 'predomain' class: unpointed version of bifinite
huffman
parents: 40038
diff changeset
    18
    }
33679
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
    19
35840
01d7c4ba9050 allow sort constraints in HOL/typedef and related HOLCF variants;
wenzelm
parents: 35527
diff changeset
    20
  val add_repdef: bool -> binding option -> binding * (string * sort) list * mixfix ->
33679
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
    21
    term -> (binding * binding) option -> theory ->
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
    22
    (Typedef.info * Pcpodef.cpo_info * Pcpodef.pcpo_info * rep_info) * theory
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
    23
35840
01d7c4ba9050 allow sort constraints in HOL/typedef and related HOLCF variants;
wenzelm
parents: 35527
diff changeset
    24
  val repdef_cmd: (bool * binding) * (binding * (string * string option) list * mixfix) * string
33679
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
    25
    * (binding * binding) option -> theory -> theory
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
    26
end;
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
    27
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
    28
structure Repdef :> REPDEF =
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
    29
struct
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
    30
35527
f4282471461d fixrec and repdef modules import holcf_library
huffman
parents: 35525
diff changeset
    31
open HOLCF_Library;
f4282471461d fixrec and repdef modules import holcf_library
huffman
parents: 35525
diff changeset
    32
f4282471461d fixrec and repdef modules import holcf_library
huffman
parents: 35525
diff changeset
    33
infixr 6 ->>;
f4282471461d fixrec and repdef modules import holcf_library
huffman
parents: 35525
diff changeset
    34
infix -->>;
f4282471461d fixrec and repdef modules import holcf_library
huffman
parents: 35525
diff changeset
    35
33679
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
    36
(** type definitions **)
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
    37
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
    38
type rep_info =
40491
6de5839e2fb3 add 'predomain' class: unpointed version of bifinite
huffman
parents: 40038
diff changeset
    39
  {
6de5839e2fb3 add 'predomain' class: unpointed version of bifinite
huffman
parents: 40038
diff changeset
    40
    emb_def : thm,
6de5839e2fb3 add 'predomain' class: unpointed version of bifinite
huffman
parents: 40038
diff changeset
    41
    prj_def : thm,
6de5839e2fb3 add 'predomain' class: unpointed version of bifinite
huffman
parents: 40038
diff changeset
    42
    defl_def : thm,
6de5839e2fb3 add 'predomain' class: unpointed version of bifinite
huffman
parents: 40038
diff changeset
    43
    liftemb_def : thm,
6de5839e2fb3 add 'predomain' class: unpointed version of bifinite
huffman
parents: 40038
diff changeset
    44
    liftprj_def : thm,
6de5839e2fb3 add 'predomain' class: unpointed version of bifinite
huffman
parents: 40038
diff changeset
    45
    liftdefl_def : thm,
40494
db8a09daba7b add class liftdomain, for bifinite domains where DEFL('a u) = u_defl('a)
huffman
parents: 40491
diff changeset
    46
    DEFL : thm
40491
6de5839e2fb3 add 'predomain' class: unpointed version of bifinite
huffman
parents: 40038
diff changeset
    47
  };
33679
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
    48
35527
f4282471461d fixrec and repdef modules import holcf_library
huffman
parents: 35525
diff changeset
    49
(* building types and terms *)
33679
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
    50
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
    51
val udomT = @{typ udom};
39989
ad60d7311f43 renamed type and constant 'sfp' to 'defl'; replaced syntax SFP('a) with DEFL('a)
huffman
parents: 39986
diff changeset
    52
val deflT = @{typ defl};
35527
f4282471461d fixrec and repdef modules import holcf_library
huffman
parents: 35525
diff changeset
    53
fun emb_const T = Const (@{const_name emb}, T ->> udomT);
f4282471461d fixrec and repdef modules import holcf_library
huffman
parents: 35525
diff changeset
    54
fun prj_const T = Const (@{const_name prj}, udomT ->> T);
39989
ad60d7311f43 renamed type and constant 'sfp' to 'defl'; replaced syntax SFP('a) with DEFL('a)
huffman
parents: 39986
diff changeset
    55
fun defl_const T = Const (@{const_name defl}, Term.itselfT T --> deflT);
40491
6de5839e2fb3 add 'predomain' class: unpointed version of bifinite
huffman
parents: 40038
diff changeset
    56
fun liftemb_const T = Const (@{const_name liftemb}, mk_upT T ->> udomT);
6de5839e2fb3 add 'predomain' class: unpointed version of bifinite
huffman
parents: 40038
diff changeset
    57
fun liftprj_const T = Const (@{const_name liftprj}, udomT ->> mk_upT T);
6de5839e2fb3 add 'predomain' class: unpointed version of bifinite
huffman
parents: 40038
diff changeset
    58
fun liftdefl_const T = Const (@{const_name liftdefl}, Term.itselfT T --> deflT);
6de5839e2fb3 add 'predomain' class: unpointed version of bifinite
huffman
parents: 40038
diff changeset
    59
6de5839e2fb3 add 'predomain' class: unpointed version of bifinite
huffman
parents: 40038
diff changeset
    60
fun mk_u_map t =
6de5839e2fb3 add 'predomain' class: unpointed version of bifinite
huffman
parents: 40038
diff changeset
    61
  let
6de5839e2fb3 add 'predomain' class: unpointed version of bifinite
huffman
parents: 40038
diff changeset
    62
    val (T, U) = dest_cfunT (fastype_of t);
6de5839e2fb3 add 'predomain' class: unpointed version of bifinite
huffman
parents: 40038
diff changeset
    63
    val u_map_type = (T ->> U) ->> (mk_upT T ->> mk_upT U);
6de5839e2fb3 add 'predomain' class: unpointed version of bifinite
huffman
parents: 40038
diff changeset
    64
    val u_map_const = Const (@{const_name u_map}, u_map_type);
6de5839e2fb3 add 'predomain' class: unpointed version of bifinite
huffman
parents: 40038
diff changeset
    65
  in
6de5839e2fb3 add 'predomain' class: unpointed version of bifinite
huffman
parents: 40038
diff changeset
    66
    mk_capply (u_map_const, t)
6de5839e2fb3 add 'predomain' class: unpointed version of bifinite
huffman
parents: 40038
diff changeset
    67
  end;
33679
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
    68
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
    69
fun mk_cast (t, x) =
35527
f4282471461d fixrec and repdef modules import holcf_library
huffman
parents: 35525
diff changeset
    70
  capply_const (udomT, udomT)
39989
ad60d7311f43 renamed type and constant 'sfp' to 'defl'; replaced syntax SFP('a) with DEFL('a)
huffman
parents: 39986
diff changeset
    71
  $ (capply_const (deflT, udomT ->> udomT) $ @{const cast} $ t)
33679
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
    72
  $ x;
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
    73
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
    74
(* manipulating theorems *)
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
    75
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
    76
(* proving class instances *)
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
    77
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
    78
fun declare_type_name a =
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
    79
  Variable.declare_constraints (Logic.mk_type (TFree (a, dummyS)));
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
    80
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
    81
fun gen_add_repdef
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
    82
      (prep_term: Proof.context -> 'a -> term)
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
    83
      (def: bool)
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
    84
      (name: binding)
35840
01d7c4ba9050 allow sort constraints in HOL/typedef and related HOLCF variants;
wenzelm
parents: 35527
diff changeset
    85
      (typ as (tname, raw_args, mx) : binding * (string * sort) list * mixfix)
33679
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
    86
      (raw_defl: 'a)
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
    87
      (opt_morphs: (binding * binding) option)
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
    88
      (thy: theory)
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
    89
    : (Typedef.info * Pcpodef.cpo_info * Pcpodef.pcpo_info * rep_info) * theory =
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
    90
  let
40510
638943ad5bdc update Theory.requires with new theory name
huffman
parents: 40494
diff changeset
    91
    val _ = Theory.requires thy "Domain" "repdefs";
33679
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
    92
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
    93
    (*rhs*)
36153
1ac501e16a6a replaced slightly odd Typedecl.predeclare_constraints by plain declaration of type arguments -- also avoid "recursive" declaration of type constructor, which can cause problems with sequential definitions B.foo = A.foo;
wenzelm
parents: 35994
diff changeset
    94
    val tmp_ctxt =
36610
bafd82950e24 renamed ProofContext.init to ProofContext.init_global to emphasize that this is not the real thing;
wenzelm
parents: 36241
diff changeset
    95
      ProofContext.init_global thy
36153
1ac501e16a6a replaced slightly odd Typedecl.predeclare_constraints by plain declaration of type arguments -- also avoid "recursive" declaration of type constructor, which can cause problems with sequential definitions B.foo = A.foo;
wenzelm
parents: 35994
diff changeset
    96
      |> fold (Variable.declare_typ o TFree) raw_args;
1ac501e16a6a replaced slightly odd Typedecl.predeclare_constraints by plain declaration of type arguments -- also avoid "recursive" declaration of type constructor, which can cause problems with sequential definitions B.foo = A.foo;
wenzelm
parents: 35994
diff changeset
    97
    val defl = prep_term tmp_ctxt raw_defl;
1ac501e16a6a replaced slightly odd Typedecl.predeclare_constraints by plain declaration of type arguments -- also avoid "recursive" declaration of type constructor, which can cause problems with sequential definitions B.foo = A.foo;
wenzelm
parents: 35994
diff changeset
    98
    val tmp_ctxt = tmp_ctxt |> Variable.declare_constraints defl;
35840
01d7c4ba9050 allow sort constraints in HOL/typedef and related HOLCF variants;
wenzelm
parents: 35527
diff changeset
    99
33679
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
   100
    val deflT = Term.fastype_of defl;
39989
ad60d7311f43 renamed type and constant 'sfp' to 'defl'; replaced syntax SFP('a) with DEFL('a)
huffman
parents: 39986
diff changeset
   101
    val _ = if deflT = @{typ "defl"} then ()
ad60d7311f43 renamed type and constant 'sfp' to 'defl'; replaced syntax SFP('a) with DEFL('a)
huffman
parents: 39986
diff changeset
   102
            else error ("Not type defl: " ^ quote (Syntax.string_of_typ tmp_ctxt deflT));
33679
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
   103
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
   104
    (*lhs*)
36153
1ac501e16a6a replaced slightly odd Typedecl.predeclare_constraints by plain declaration of type arguments -- also avoid "recursive" declaration of type constructor, which can cause problems with sequential definitions B.foo = A.foo;
wenzelm
parents: 35994
diff changeset
   105
    val lhs_tfrees = map (ProofContext.check_tfree tmp_ctxt) raw_args;
33679
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
   106
    val lhs_sorts = map snd lhs_tfrees;
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
   107
    val full_tname = Sign.full_name thy tname;
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
   108
    val newT = Type (full_tname, map TFree lhs_tfrees);
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
   109
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
   110
    (*morphisms*)
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
   111
    val morphs = opt_morphs
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
   112
      |> the_default (Binding.prefix_name "Rep_" name, Binding.prefix_name "Abs_" name);
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
   113
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
   114
    (*set*)
40038
9d061b3d8f46 replace 'in_defl' relation and '_ ::: _' syntax with 'defl_set' function
huffman
parents: 39989
diff changeset
   115
    val set = @{const defl_set} $ defl;
33679
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
   116
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
   117
    (*pcpodef*)
40038
9d061b3d8f46 replace 'in_defl' relation and '_ ::: _' syntax with 'defl_set' function
huffman
parents: 39989
diff changeset
   118
    val tac1 = rtac @{thm defl_set_bottom} 1;
9d061b3d8f46 replace 'in_defl' relation and '_ ::: _' syntax with 'defl_set' function
huffman
parents: 39989
diff changeset
   119
    val tac2 = rtac @{thm adm_defl_set} 1;
35904
0c13e28e5e41 fix ML warnings in repdef.ML
huffman
parents: 35840
diff changeset
   120
    val ((info, cpo_info, pcpo_info), thy) = thy
33679
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
   121
      |> Pcpodef.add_pcpodef def (SOME name) typ set (SOME morphs) (tac1, tac2);
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
   122
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
   123
    (*definitions*)
35994
9cc3df9a606e Typedef.info: separate global and local part, only the latter is transformed by morphisms;
wenzelm
parents: 35904
diff changeset
   124
    val Rep_const = Const (#Rep_name (#1 info), newT --> udomT);
9cc3df9a606e Typedef.info: separate global and local part, only the latter is transformed by morphisms;
wenzelm
parents: 35904
diff changeset
   125
    val Abs_const = Const (#Abs_name (#1 info), udomT --> newT);
35527
f4282471461d fixrec and repdef modules import holcf_library
huffman
parents: 35525
diff changeset
   126
    val emb_eqn = Logic.mk_equals (emb_const newT, cabs_const (newT, udomT) $ Rep_const);
f4282471461d fixrec and repdef modules import holcf_library
huffman
parents: 35525
diff changeset
   127
    val prj_eqn = Logic.mk_equals (prj_const newT, cabs_const (udomT, newT) $
33679
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
   128
      Abs ("x", udomT, Abs_const $ mk_cast (defl, Bound 0)));
39989
ad60d7311f43 renamed type and constant 'sfp' to 'defl'; replaced syntax SFP('a) with DEFL('a)
huffman
parents: 39986
diff changeset
   129
    val defl_eqn = Logic.mk_equals (defl_const newT,
39974
b525988432e9 major reorganization/simplification of HOLCF type classes:
huffman
parents: 39557
diff changeset
   130
      Abs ("x", Term.itselfT newT, defl));
40491
6de5839e2fb3 add 'predomain' class: unpointed version of bifinite
huffman
parents: 40038
diff changeset
   131
    val liftemb_eqn =
6de5839e2fb3 add 'predomain' class: unpointed version of bifinite
huffman
parents: 40038
diff changeset
   132
      Logic.mk_equals (liftemb_const newT,
6de5839e2fb3 add 'predomain' class: unpointed version of bifinite
huffman
parents: 40038
diff changeset
   133
      mk_cfcomp (@{term "udom_emb u_approx"}, mk_u_map (emb_const newT)));
6de5839e2fb3 add 'predomain' class: unpointed version of bifinite
huffman
parents: 40038
diff changeset
   134
    val liftprj_eqn =
6de5839e2fb3 add 'predomain' class: unpointed version of bifinite
huffman
parents: 40038
diff changeset
   135
      Logic.mk_equals (liftprj_const newT,
6de5839e2fb3 add 'predomain' class: unpointed version of bifinite
huffman
parents: 40038
diff changeset
   136
      mk_cfcomp (mk_u_map (prj_const newT), @{term "udom_prj u_approx"}));
6de5839e2fb3 add 'predomain' class: unpointed version of bifinite
huffman
parents: 40038
diff changeset
   137
    val liftdefl_eqn =
6de5839e2fb3 add 'predomain' class: unpointed version of bifinite
huffman
parents: 40038
diff changeset
   138
      Logic.mk_equals (liftdefl_const newT,
6de5839e2fb3 add 'predomain' class: unpointed version of bifinite
huffman
parents: 40038
diff changeset
   139
        Abs ("t", Term.itselfT newT,
6de5839e2fb3 add 'predomain' class: unpointed version of bifinite
huffman
parents: 40038
diff changeset
   140
          mk_capply (@{const u_defl}, defl_const newT $ Logic.mk_type newT)));
6de5839e2fb3 add 'predomain' class: unpointed version of bifinite
huffman
parents: 40038
diff changeset
   141
35904
0c13e28e5e41 fix ML warnings in repdef.ML
huffman
parents: 35840
diff changeset
   142
    val name_def = Binding.suffix_name "_def" name;
0c13e28e5e41 fix ML warnings in repdef.ML
huffman
parents: 35840
diff changeset
   143
    val emb_bind = (Binding.prefix_name "emb_" name_def, []);
0c13e28e5e41 fix ML warnings in repdef.ML
huffman
parents: 35840
diff changeset
   144
    val prj_bind = (Binding.prefix_name "prj_" name_def, []);
39989
ad60d7311f43 renamed type and constant 'sfp' to 'defl'; replaced syntax SFP('a) with DEFL('a)
huffman
parents: 39986
diff changeset
   145
    val defl_bind = (Binding.prefix_name "defl_" name_def, []);
40491
6de5839e2fb3 add 'predomain' class: unpointed version of bifinite
huffman
parents: 40038
diff changeset
   146
    val liftemb_bind = (Binding.prefix_name "liftemb_" name_def, []);
6de5839e2fb3 add 'predomain' class: unpointed version of bifinite
huffman
parents: 40038
diff changeset
   147
    val liftprj_bind = (Binding.prefix_name "liftprj_" name_def, []);
6de5839e2fb3 add 'predomain' class: unpointed version of bifinite
huffman
parents: 40038
diff changeset
   148
    val liftdefl_bind = (Binding.prefix_name "liftdefl_" name_def, []);
33679
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
   149
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
   150
    (*instantiate class rep*)
35904
0c13e28e5e41 fix ML warnings in repdef.ML
huffman
parents: 35840
diff changeset
   151
    val lthy = thy
40494
db8a09daba7b add class liftdomain, for bifinite domains where DEFL('a u) = u_defl('a)
huffman
parents: 40491
diff changeset
   152
      |> Class.instantiation ([full_tname], lhs_tfrees, @{sort liftdomain});
35904
0c13e28e5e41 fix ML warnings in repdef.ML
huffman
parents: 35840
diff changeset
   153
    val ((_, (_, emb_ldef)), lthy) =
0c13e28e5e41 fix ML warnings in repdef.ML
huffman
parents: 35840
diff changeset
   154
        Specification.definition (NONE, (emb_bind, emb_eqn)) lthy;
0c13e28e5e41 fix ML warnings in repdef.ML
huffman
parents: 35840
diff changeset
   155
    val ((_, (_, prj_ldef)), lthy) =
0c13e28e5e41 fix ML warnings in repdef.ML
huffman
parents: 35840
diff changeset
   156
        Specification.definition (NONE, (prj_bind, prj_eqn)) lthy;
39989
ad60d7311f43 renamed type and constant 'sfp' to 'defl'; replaced syntax SFP('a) with DEFL('a)
huffman
parents: 39986
diff changeset
   157
    val ((_, (_, defl_ldef)), lthy) =
ad60d7311f43 renamed type and constant 'sfp' to 'defl'; replaced syntax SFP('a) with DEFL('a)
huffman
parents: 39986
diff changeset
   158
        Specification.definition (NONE, (defl_bind, defl_eqn)) lthy;
40491
6de5839e2fb3 add 'predomain' class: unpointed version of bifinite
huffman
parents: 40038
diff changeset
   159
    val ((_, (_, liftemb_ldef)), lthy) =
6de5839e2fb3 add 'predomain' class: unpointed version of bifinite
huffman
parents: 40038
diff changeset
   160
        Specification.definition (NONE, (liftemb_bind, liftemb_eqn)) lthy;
6de5839e2fb3 add 'predomain' class: unpointed version of bifinite
huffman
parents: 40038
diff changeset
   161
    val ((_, (_, liftprj_ldef)), lthy) =
6de5839e2fb3 add 'predomain' class: unpointed version of bifinite
huffman
parents: 40038
diff changeset
   162
        Specification.definition (NONE, (liftprj_bind, liftprj_eqn)) lthy;
6de5839e2fb3 add 'predomain' class: unpointed version of bifinite
huffman
parents: 40038
diff changeset
   163
    val ((_, (_, liftdefl_ldef)), lthy) =
6de5839e2fb3 add 'predomain' class: unpointed version of bifinite
huffman
parents: 40038
diff changeset
   164
        Specification.definition (NONE, (liftdefl_bind, liftdefl_eqn)) lthy;
36610
bafd82950e24 renamed ProofContext.init to ProofContext.init_global to emphasize that this is not the real thing;
wenzelm
parents: 36241
diff changeset
   165
    val ctxt_thy = ProofContext.init_global (ProofContext.theory_of lthy);
35904
0c13e28e5e41 fix ML warnings in repdef.ML
huffman
parents: 35840
diff changeset
   166
    val emb_def = singleton (ProofContext.export lthy ctxt_thy) emb_ldef;
0c13e28e5e41 fix ML warnings in repdef.ML
huffman
parents: 35840
diff changeset
   167
    val prj_def = singleton (ProofContext.export lthy ctxt_thy) prj_ldef;
39989
ad60d7311f43 renamed type and constant 'sfp' to 'defl'; replaced syntax SFP('a) with DEFL('a)
huffman
parents: 39986
diff changeset
   168
    val defl_def = singleton (ProofContext.export lthy ctxt_thy) defl_ldef;
40491
6de5839e2fb3 add 'predomain' class: unpointed version of bifinite
huffman
parents: 40038
diff changeset
   169
    val liftemb_def = singleton (ProofContext.export lthy ctxt_thy) liftemb_ldef;
6de5839e2fb3 add 'predomain' class: unpointed version of bifinite
huffman
parents: 40038
diff changeset
   170
    val liftprj_def = singleton (ProofContext.export lthy ctxt_thy) liftprj_ldef;
6de5839e2fb3 add 'predomain' class: unpointed version of bifinite
huffman
parents: 40038
diff changeset
   171
    val liftdefl_def = singleton (ProofContext.export lthy ctxt_thy) liftdefl_ldef;
33826
7f12ab745298 make repdef work without (open) option
huffman
parents: 33812
diff changeset
   172
    val type_definition_thm =
7f12ab745298 make repdef work without (open) option
huffman
parents: 33812
diff changeset
   173
      MetaSimplifier.rewrite_rule
35994
9cc3df9a606e Typedef.info: separate global and local part, only the latter is transformed by morphisms;
wenzelm
parents: 35904
diff changeset
   174
        (the_list (#set_def (#2 info)))
9cc3df9a606e Typedef.info: separate global and local part, only the latter is transformed by morphisms;
wenzelm
parents: 35904
diff changeset
   175
        (#type_definition (#2 info));
33679
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
   176
    val typedef_thms =
40491
6de5839e2fb3 add 'predomain' class: unpointed version of bifinite
huffman
parents: 40038
diff changeset
   177
      [type_definition_thm, #below_def cpo_info, emb_def, prj_def, defl_def,
6de5839e2fb3 add 'predomain' class: unpointed version of bifinite
huffman
parents: 40038
diff changeset
   178
      liftemb_def, liftprj_def, liftdefl_def];
35904
0c13e28e5e41 fix ML warnings in repdef.ML
huffman
parents: 35840
diff changeset
   179
    val thy = lthy
33679
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
   180
      |> Class.prove_instantiation_instance
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
   181
          (K (Tactic.rtac (@{thm typedef_rep_class} OF typedef_thms) 1))
33681
cddea85bc87b LocalTheory -> Local_Theory
huffman
parents: 33679
diff changeset
   182
      |> Local_Theory.exit_global;
33679
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
   183
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
   184
    (*other theorems*)
39989
ad60d7311f43 renamed type and constant 'sfp' to 'defl'; replaced syntax SFP('a) with DEFL('a)
huffman
parents: 39986
diff changeset
   185
    val defl_thm' = Thm.transfer thy defl_def;
ad60d7311f43 renamed type and constant 'sfp' to 'defl'; replaced syntax SFP('a) with DEFL('a)
huffman
parents: 39986
diff changeset
   186
    val (DEFL_thm, thy) = thy
33679
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
   187
      |> Sign.add_path (Binding.name_of name)
39557
fe5722fce758 renamed structure PureThy to Pure_Thy and moved most content to Global_Theory, to emphasize that this is global-only;
wenzelm
parents: 38348
diff changeset
   188
      |> Global_Theory.add_thm
39989
ad60d7311f43 renamed type and constant 'sfp' to 'defl'; replaced syntax SFP('a) with DEFL('a)
huffman
parents: 39986
diff changeset
   189
         ((Binding.prefix_name "DEFL_" name,
ad60d7311f43 renamed type and constant 'sfp' to 'defl'; replaced syntax SFP('a) with DEFL('a)
huffman
parents: 39986
diff changeset
   190
          Drule.zero_var_indexes (@{thm typedef_DEFL} OF [defl_thm'])), [])
35904
0c13e28e5e41 fix ML warnings in repdef.ML
huffman
parents: 35840
diff changeset
   191
      ||> Sign.restore_naming thy;
33679
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
   192
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
   193
    val rep_info =
40491
6de5839e2fb3 add 'predomain' class: unpointed version of bifinite
huffman
parents: 40038
diff changeset
   194
      { emb_def = emb_def, prj_def = prj_def, defl_def = defl_def,
6de5839e2fb3 add 'predomain' class: unpointed version of bifinite
huffman
parents: 40038
diff changeset
   195
        liftemb_def = liftemb_def, liftprj_def = liftprj_def,
40494
db8a09daba7b add class liftdomain, for bifinite domains where DEFL('a u) = u_defl('a)
huffman
parents: 40491
diff changeset
   196
        liftdefl_def = liftdefl_def, DEFL = DEFL_thm };
33679
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
   197
  in
35904
0c13e28e5e41 fix ML warnings in repdef.ML
huffman
parents: 35840
diff changeset
   198
    ((info, cpo_info, pcpo_info, rep_info), thy)
33679
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
   199
  end
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
   200
  handle ERROR msg =>
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
   201
    cat_error msg ("The error(s) above occurred in repdef " ^ quote (Binding.str_of name));
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
   202
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
   203
fun add_repdef def opt_name typ defl opt_morphs thy =
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
   204
  let
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
   205
    val name = the_default (#1 typ) opt_name;
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
   206
  in
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
   207
    gen_add_repdef Syntax.check_term def name typ defl opt_morphs thy
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
   208
  end;
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
   209
35840
01d7c4ba9050 allow sort constraints in HOL/typedef and related HOLCF variants;
wenzelm
parents: 35527
diff changeset
   210
fun repdef_cmd ((def, name), (b, raw_args, mx), A, morphs) thy =
01d7c4ba9050 allow sort constraints in HOL/typedef and related HOLCF variants;
wenzelm
parents: 35527
diff changeset
   211
  let
36610
bafd82950e24 renamed ProofContext.init to ProofContext.init_global to emphasize that this is not the real thing;
wenzelm
parents: 36241
diff changeset
   212
    val ctxt = ProofContext.init_global thy;
35840
01d7c4ba9050 allow sort constraints in HOL/typedef and related HOLCF variants;
wenzelm
parents: 35527
diff changeset
   213
    val args = map (apsnd (Typedecl.read_constraint ctxt)) raw_args;
01d7c4ba9050 allow sort constraints in HOL/typedef and related HOLCF variants;
wenzelm
parents: 35527
diff changeset
   214
  in snd (gen_add_repdef Syntax.read_term def name (b, args, mx) A morphs thy) end;
01d7c4ba9050 allow sort constraints in HOL/typedef and related HOLCF variants;
wenzelm
parents: 35527
diff changeset
   215
33679
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
   216
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
   217
(** outer syntax **)
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
   218
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
   219
val repdef_decl =
36960
01594f816e3a prefer structure Keyword, Parse, Parse_Spec, Outer_Syntax;
wenzelm
parents: 36610
diff changeset
   220
  Scan.optional (Parse.$$$ "(" |--
01594f816e3a prefer structure Keyword, Parse, Parse_Spec, Outer_Syntax;
wenzelm
parents: 36610
diff changeset
   221
      ((Parse.$$$ "open" >> K false) -- Scan.option Parse.binding ||
01594f816e3a prefer structure Keyword, Parse, Parse_Spec, Outer_Syntax;
wenzelm
parents: 36610
diff changeset
   222
        Parse.binding >> (fn s => (true, SOME s)))
01594f816e3a prefer structure Keyword, Parse, Parse_Spec, Outer_Syntax;
wenzelm
parents: 36610
diff changeset
   223
        --| Parse.$$$ ")") (true, NONE) --
01594f816e3a prefer structure Keyword, Parse, Parse_Spec, Outer_Syntax;
wenzelm
parents: 36610
diff changeset
   224
    (Parse.type_args_constrained -- Parse.binding) --
01594f816e3a prefer structure Keyword, Parse, Parse_Spec, Outer_Syntax;
wenzelm
parents: 36610
diff changeset
   225
    Parse.opt_mixfix -- (Parse.$$$ "=" |-- Parse.term) --
01594f816e3a prefer structure Keyword, Parse, Parse_Spec, Outer_Syntax;
wenzelm
parents: 36610
diff changeset
   226
    Scan.option (Parse.$$$ "morphisms" |-- Parse.!!! (Parse.binding -- Parse.binding));
33679
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
   227
35840
01d7c4ba9050 allow sort constraints in HOL/typedef and related HOLCF variants;
wenzelm
parents: 35527
diff changeset
   228
fun mk_repdef ((((((def, opt_name), (args, t)), mx), A), morphs)) =
01d7c4ba9050 allow sort constraints in HOL/typedef and related HOLCF variants;
wenzelm
parents: 35527
diff changeset
   229
  repdef_cmd ((def, the_default t opt_name), (t, args, mx), A, morphs);
33679
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
   230
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
   231
val _ =
36960
01594f816e3a prefer structure Keyword, Parse, Parse_Spec, Outer_Syntax;
wenzelm
parents: 36610
diff changeset
   232
  Outer_Syntax.command "repdef" "HOLCF definition of representable domains" Keyword.thy_decl
33679
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
   233
    (repdef_decl >>
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
   234
      (Toplevel.print oo (Toplevel.theory o mk_repdef)));
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
   235
331712879666 automate definition of representable domains from algebraic deflations
huffman
parents:
diff changeset
   236
end;