src/HOL/Tools/typecopy.ML
author Philipp Meyer
Wed Sep 30 11:33:59 2009 +0200 (2009-09-30)
changeset 32866 1238cbb7c08f
parent 32378 89b19ab3b35c
child 33314 53d49370f7af
permissions -rw-r--r--
atp_minimal using chain_ths again
     1 (*  Title:      HOL/Tools/typecopy.ML
     2     Author:     Florian Haftmann, TU Muenchen
     3 
     4 Introducing copies of types using trivial typedefs; datatype-like abstraction.
     5 *)
     6 
     7 signature TYPECOPY =
     8 sig
     9   type info = { vs: (string * sort) list, constr: string, typ: typ,
    10     inject: thm, proj: string * typ, proj_def: thm }
    11   val typecopy: binding * string list -> typ -> (binding * binding) option
    12     -> theory -> (string * info) * theory
    13   val get_info: theory -> string -> info option
    14   val interpretation: (string -> theory -> theory) -> theory -> theory
    15   val add_default_code: string -> theory -> theory
    16   val setup: theory -> theory
    17 end;
    18 
    19 structure Typecopy: TYPECOPY =
    20 struct
    21 
    22 (* theory data *)
    23 
    24 type info = {
    25   vs: (string * sort) list,
    26   constr: string,
    27   typ: typ,
    28   inject: thm,
    29   proj: string * typ,
    30   proj_def: thm
    31 };
    32 
    33 structure TypecopyData = TheoryDataFun
    34 (
    35   type T = info Symtab.table;
    36   val empty = Symtab.empty;
    37   val copy = I;
    38   val extend = I;
    39   fun merge _ = Symtab.merge (K true);
    40 );
    41 
    42 val get_info = Symtab.lookup o TypecopyData.get;
    43 
    44 
    45 (* interpretation of type copies *)
    46 
    47 structure TypecopyInterpretation = InterpretationFun(type T = string val eq = op =);
    48 val interpretation = TypecopyInterpretation.interpretation;
    49 
    50 
    51 (* introducing typecopies *)
    52 
    53 fun typecopy (raw_tyco, raw_vs) raw_ty constr_proj thy =
    54   let
    55     val ty = Sign.certify_typ thy raw_ty;
    56     val vs =
    57       AList.make (the_default HOLogic.typeS o AList.lookup (op =) (Term.add_tfreesT ty [])) raw_vs;
    58     val tac = Tactic.rtac UNIV_witness 1;
    59     fun add_info tyco ( { abs_type = ty_abs, rep_type = ty_rep, Abs_name = c_abs,
    60       Rep_name = c_rep, Abs_inject = inject,
    61       Abs_inverse = inverse, ... } : Typedef.info ) thy =
    62         let
    63           val exists_thm =
    64             UNIV_I
    65             |> Drule.instantiate' [SOME (ctyp_of thy (Logic.varifyT ty_rep))] [];
    66           val inject' = inject OF [exists_thm, exists_thm];
    67           val proj_def = inverse OF [exists_thm];
    68           val info = {
    69             vs = vs,
    70             constr = c_abs,
    71             typ = ty_rep,
    72             inject = inject',
    73             proj = (c_rep, ty_abs --> ty_rep),
    74             proj_def = proj_def
    75           };
    76         in
    77           thy
    78           |> (TypecopyData.map o Symtab.update_new) (tyco, info)
    79           |> TypecopyInterpretation.data tyco
    80           |> pair (tyco, info)
    81         end
    82   in
    83     thy
    84     |> Typedef.add_typedef false (SOME raw_tyco) (raw_tyco, map fst vs, NoSyn)
    85       (HOLogic.mk_UNIV ty) (Option.map swap constr_proj) tac
    86     |-> (fn (tyco, info) => add_info tyco info)
    87   end;
    88 
    89 
    90 (* default code setup *)
    91 
    92 fun add_default_code tyco thy =
    93   let
    94     val SOME { constr = c, proj = (proj, _), proj_def = proj_eq, vs = vs,
    95       typ = ty_rep, ... } =  get_info thy tyco;
    96     val SOME { Rep_inject = proj_inject, ... } = Typedef.get_info thy tyco;
    97     val constr = (c, Logic.unvarifyT (Sign.the_const_type thy c));
    98     val ty = Type (tyco, map TFree vs);
    99     val proj = Const (proj, ty --> ty_rep);
   100     val (t_x, t_y) = (Free ("x", ty), Free ("y", ty));
   101     val eq_lhs = Const (@{const_name eq_class.eq}, ty --> ty --> HOLogic.boolT)
   102       $ t_x $ t_y;
   103     val eq_rhs = HOLogic.mk_eq (proj $ t_x, proj $ t_y);
   104     val eq = (HOLogic.mk_Trueprop o HOLogic.mk_eq) (eq_lhs, eq_rhs);
   105     fun tac eq_thm = Class.intro_classes_tac []
   106       THEN (Simplifier.rewrite_goals_tac
   107         (map Simpdata.mk_eq [eq_thm, @{thm eq}, proj_inject]))
   108           THEN ALLGOALS (rtac @{thm refl});
   109     fun mk_eq_refl thy = @{thm HOL.eq_refl}
   110       |> Thm.instantiate
   111          ([pairself (Thm.ctyp_of thy) (TVar (("'a", 0), @{sort eq}), Logic.varifyT ty)], [])
   112       |> AxClass.unoverload thy;
   113   in
   114     thy
   115     |> Code.add_datatype [constr]
   116     |> Code.add_eqn proj_eq
   117     |> TheoryTarget.instantiation ([tyco], vs, [HOLogic.class_eq])
   118     |> `(fn lthy => Syntax.check_term lthy eq)
   119     |-> (fn eq => Specification.definition
   120          (NONE, (Attrib.empty_binding, eq)))
   121     |-> (fn (_, (_, eq_thm)) =>
   122        Class.prove_instantiation_exit_result Morphism.thm
   123          (fn _ => fn eq_thm => tac eq_thm) eq_thm)
   124     |-> (fn eq_thm => Code.add_eqn eq_thm)
   125     |> (fn thy => Code.add_nbe_eqn (mk_eq_refl thy) thy)
   126   end;
   127 
   128 val setup =
   129   TypecopyInterpretation.init
   130   #> interpretation add_default_code
   131 
   132 end;