src/HOL/Tools/typedef.ML
author wenzelm
Fri Oct 12 21:22:35 2012 +0200 (2012-10-12)
changeset 49835 31f32ec4d766
parent 49834 b27bbb021df1
child 54883 dd04a8b654fc
permissions -rw-r--r--
discontinued typedef with alternative name;
     1 (*  Title:      HOL/Tools/typedef.ML
     2     Author:     Markus Wenzel and Stefan Berghofer, TU Muenchen
     3 
     4 Gordon/HOL-style type definitions: create a new syntactic type
     5 represented by a non-empty set.
     6 *)
     7 
     8 signature TYPEDEF =
     9 sig
    10   type info =
    11    {rep_type: typ, abs_type: typ, Rep_name: string, Abs_name: string, axiom_name: string} *
    12    {inhabited: thm, type_definition: thm, Rep: thm, Rep_inverse: thm, Abs_inverse: thm,
    13     Rep_inject: thm, Abs_inject: thm, Rep_cases: thm, Abs_cases: thm,
    14     Rep_induct: thm, Abs_induct: thm}
    15   val transform_info: morphism -> info -> info
    16   val get_info: Proof.context -> string -> info list
    17   val get_info_global: theory -> string -> info list
    18   val interpretation: (string -> theory -> theory) -> theory -> theory
    19   val setup: theory -> theory
    20   val add_typedef: binding * (string * sort) list * mixfix ->
    21     term -> (binding * binding) option -> tactic -> local_theory -> (string * info) * local_theory
    22   val add_typedef_global: binding * (string * sort) list * mixfix ->
    23     term -> (binding * binding) option -> tactic -> theory -> (string * info) * theory
    24   val typedef: (binding * (string * sort) list * mixfix) * term *
    25     (binding * binding) option -> local_theory -> Proof.state
    26   val typedef_cmd: (binding * (string * string option) list * mixfix) * string *
    27     (binding * binding) option -> local_theory -> Proof.state
    28 end;
    29 
    30 structure Typedef: TYPEDEF =
    31 struct
    32 
    33 (** type definitions **)
    34 
    35 (* theory data *)
    36 
    37 type info =
    38   (*global part*)
    39   {rep_type: typ, abs_type: typ, Rep_name: string, Abs_name: string, axiom_name: string} *
    40   (*local part*)
    41   {inhabited: thm, type_definition: thm, Rep: thm, Rep_inverse: thm, Abs_inverse: thm,
    42     Rep_inject: thm, Abs_inject: thm, Rep_cases: thm, Abs_cases: thm,
    43     Rep_induct: thm, Abs_induct: thm};
    44 
    45 fun transform_info phi (info: info) =
    46   let
    47     val thm = Morphism.thm phi;
    48     val (global_info, {inhabited, type_definition, Rep, Rep_inverse, Abs_inverse,
    49       Rep_inject, Abs_inject, Rep_cases, Abs_cases, Rep_induct, Abs_induct}) = info;
    50   in
    51     (global_info,
    52      {inhabited = thm inhabited, type_definition = thm type_definition,
    53       Rep = thm Rep, Rep_inverse = thm Rep_inverse, Abs_inverse = thm Abs_inverse,
    54       Rep_inject = thm Rep_inject, Abs_inject = thm Abs_inject,
    55       Rep_cases = thm Rep_cases, Abs_cases = thm Abs_cases,
    56       Rep_induct = thm Rep_induct, Abs_induct = thm Abs_induct})
    57   end;
    58 
    59 structure Data = Generic_Data
    60 (
    61   type T = info list Symtab.table;
    62   val empty = Symtab.empty;
    63   val extend = I;
    64   fun merge data = Symtab.merge_list (K true) data;
    65 );
    66 
    67 val get_info = Symtab.lookup_list o Data.get o Context.Proof;
    68 val get_info_global = Symtab.lookup_list o Data.get o Context.Theory;
    69 
    70 fun put_info name info = Data.map (Symtab.cons_list (name, info));
    71 
    72 
    73 (* global interpretation *)
    74 
    75 structure Typedef_Interpretation = Interpretation(type T = string val eq = op =);
    76 val interpretation = Typedef_Interpretation.interpretation;
    77 
    78 val setup = Typedef_Interpretation.init;
    79 
    80 
    81 (* primitive typedef axiomatization -- for fresh typedecl *)
    82 
    83 fun mk_inhabited A =
    84   let val T = HOLogic.dest_setT (Term.fastype_of A)
    85   in HOLogic.mk_Trueprop (HOLogic.exists_const T $ Abs ("x", T, HOLogic.mk_mem (Bound 0, A))) end;
    86 
    87 fun mk_typedef newT oldT RepC AbsC A =
    88   let
    89     val typedefC =
    90       Const (@{const_name type_definition},
    91         (newT --> oldT) --> (oldT --> newT) --> HOLogic.mk_setT oldT --> HOLogic.boolT);
    92   in Logic.mk_implies (mk_inhabited A, HOLogic.mk_Trueprop (typedefC $ RepC $ AbsC $ A)) end;
    93 
    94 fun primitive_typedef typedef_name newT oldT Rep_name Abs_name A lthy =
    95   let
    96     (* errors *)
    97 
    98     fun show_names pairs = commas_quote (map fst pairs);
    99 
   100     val lhs_tfrees = Term.add_tfreesT newT [];
   101     val rhs_tfrees = Term.add_tfreesT oldT [];
   102     val _ =
   103       (case fold (remove (op =)) lhs_tfrees rhs_tfrees of [] => ()
   104       | extras => error ("Extra type variables in representing set: " ^ show_names extras));
   105 
   106     val _ =
   107       (case Term.add_frees A [] of [] => []
   108       | xs => error ("Illegal variables in representing set: " ^ show_names xs));
   109 
   110 
   111     (* axiomatization *)
   112 
   113     val ((RepC, AbsC), consts_lthy) = lthy
   114       |> Local_Theory.background_theory_result
   115         (Sign.declare_const lthy ((Rep_name, newT --> oldT), NoSyn) ##>>
   116           Sign.declare_const lthy ((Abs_name, oldT --> newT), NoSyn));
   117 
   118     val typedef_deps = Term.add_consts A [];
   119 
   120     val ((axiom_name, axiom), axiom_lthy) = consts_lthy
   121       |> Local_Theory.background_theory_result
   122         (Thm.add_axiom consts_lthy (typedef_name, mk_typedef newT oldT RepC AbsC A) ##>
   123           Theory.add_deps consts_lthy "" (dest_Const RepC) typedef_deps ##>
   124           Theory.add_deps consts_lthy "" (dest_Const AbsC) typedef_deps);
   125 
   126   in ((RepC, AbsC, axiom_name, axiom), axiom_lthy) end;
   127 
   128 
   129 (* prepare_typedef *)
   130 
   131 fun prepare_typedef prep_term (name, raw_args, mx) raw_set opt_morphs lthy =
   132   let
   133     val bname = Binding.name_of name;
   134 
   135 
   136     (* rhs *)
   137 
   138     val tmp_ctxt = lthy |> fold (Variable.declare_typ o TFree) raw_args;
   139     val set = prep_term tmp_ctxt raw_set;
   140     val tmp_ctxt' = tmp_ctxt |> Variable.declare_term set;
   141 
   142     val setT = Term.fastype_of set;
   143     val oldT = HOLogic.dest_setT setT handle TYPE _ =>
   144       error ("Not a set type: " ^ quote (Syntax.string_of_typ lthy setT));
   145 
   146     val goal = mk_inhabited set;
   147     val goal_pat = mk_inhabited (Var (the_default (bname, 0) (Lexicon.read_variable bname), setT));
   148 
   149 
   150     (* lhs *)
   151 
   152     val args = map (Proof_Context.check_tfree tmp_ctxt') raw_args;
   153     val (newT, typedecl_lthy) = lthy
   154       |> Typedecl.typedecl (name, args, mx)
   155       ||> Variable.declare_term set;
   156 
   157     val Type (full_name, type_args) = newT;
   158     val lhs_tfrees = map Term.dest_TFree type_args;
   159 
   160 
   161     (* axiomatization *)
   162 
   163     val (Rep_name, Abs_name) =
   164       (case opt_morphs of
   165         NONE => (Binding.prefix_name "Rep_" name, Binding.prefix_name "Abs_" name)
   166       | SOME morphs => morphs);
   167 
   168     val typedef_name = Binding.prefix_name "type_definition_" name;
   169 
   170     val ((RepC, AbsC, axiom_name, typedef), typedef_lthy) = typedecl_lthy
   171       |> primitive_typedef typedef_name newT oldT Rep_name Abs_name set;
   172 
   173     val alias_lthy = typedef_lthy
   174       |> Local_Theory.const_alias Rep_name (#1 (Term.dest_Const RepC))
   175       |> Local_Theory.const_alias Abs_name (#1 (Term.dest_Const AbsC));
   176 
   177 
   178     (* result *)
   179 
   180     fun note_qualify ((b, atts), th) =
   181       Local_Theory.note ((Binding.qualify false bname b, map (Attrib.internal o K) atts), [th])
   182       #>> (fn (_, [th']) => th');
   183 
   184     fun typedef_result inhabited lthy1 =
   185       let
   186         val cert = Thm.cterm_of (Proof_Context.theory_of lthy1);
   187         val typedef' = inhabited RS typedef;
   188         fun make th = Goal.norm_result (typedef' RS th);
   189         val (((((((((((_, [type_definition]), Rep), Rep_inverse), Abs_inverse), Rep_inject),
   190             Abs_inject), Rep_cases), Abs_cases), Rep_induct), Abs_induct), lthy2) = lthy1
   191           |> Local_Theory.note ((typedef_name, []), [typedef'])
   192           ||>> note_qualify ((Rep_name, []), make @{thm type_definition.Rep})
   193           ||>> note_qualify ((Binding.suffix_name "_inverse" Rep_name, []),
   194               make @{thm type_definition.Rep_inverse})
   195           ||>> note_qualify ((Binding.suffix_name "_inverse" Abs_name, []),
   196               make @{thm type_definition.Abs_inverse})
   197           ||>> note_qualify ((Binding.suffix_name "_inject" Rep_name, []),
   198               make @{thm type_definition.Rep_inject})
   199           ||>> note_qualify ((Binding.suffix_name "_inject" Abs_name, []),
   200               make @{thm type_definition.Abs_inject})
   201           ||>> note_qualify ((Binding.suffix_name "_cases" Rep_name,
   202                 [Rule_Cases.case_names [Binding.name_of Rep_name], Induct.cases_pred full_name]),
   203               make @{thm type_definition.Rep_cases})
   204           ||>> note_qualify ((Binding.suffix_name "_cases" Abs_name,
   205                 [Rule_Cases.case_names [Binding.name_of Abs_name], Induct.cases_type full_name]),
   206               make @{thm type_definition.Abs_cases})
   207           ||>> note_qualify ((Binding.suffix_name "_induct" Rep_name,
   208                 [Rule_Cases.case_names [Binding.name_of Rep_name], Induct.induct_pred full_name]),
   209               make @{thm type_definition.Rep_induct})
   210           ||>> note_qualify ((Binding.suffix_name "_induct" Abs_name,
   211                 [Rule_Cases.case_names [Binding.name_of Abs_name], Induct.induct_type full_name]),
   212               make @{thm type_definition.Abs_induct});
   213 
   214         val info =
   215           ({rep_type = oldT, abs_type = newT, Rep_name = #1 (Term.dest_Const RepC),
   216             Abs_name = #1 (Term.dest_Const AbsC), axiom_name = axiom_name},
   217            {inhabited = inhabited, type_definition = type_definition,
   218             Rep = Rep, Rep_inverse = Rep_inverse, Abs_inverse = Abs_inverse,
   219             Rep_inject = Rep_inject, Abs_inject = Abs_inject, Rep_cases = Rep_cases,
   220           Abs_cases = Abs_cases, Rep_induct = Rep_induct, Abs_induct = Abs_induct});
   221       in
   222         lthy2
   223         |> Local_Theory.declaration {syntax = false, pervasive = true}
   224           (fn phi => put_info full_name (transform_info phi info))
   225         |> Local_Theory.background_theory (Typedef_Interpretation.data full_name)
   226         |> pair (full_name, info)
   227       end;
   228 
   229   in ((goal, goal_pat, typedef_result), alias_lthy) end
   230   handle ERROR msg =>
   231     cat_error msg ("The error(s) above occurred in typedef " ^ Binding.print name);
   232 
   233 
   234 (* add_typedef: tactic interface *)
   235 
   236 fun add_typedef typ set opt_morphs tac lthy =
   237   let
   238     val ((goal, _, typedef_result), lthy') =
   239       prepare_typedef Syntax.check_term typ set opt_morphs lthy;
   240     val inhabited =
   241       Goal.prove lthy' [] [] goal (K tac)
   242       |> Goal.norm_result |> Thm.close_derivation;
   243   in typedef_result inhabited lthy' end;
   244 
   245 fun add_typedef_global typ set opt_morphs tac =
   246   Named_Target.theory_init
   247   #> add_typedef typ set opt_morphs tac
   248   #> Local_Theory.exit_result_global (apsnd o transform_info);
   249 
   250 
   251 (* typedef: proof interface *)
   252 
   253 local
   254 
   255 fun gen_typedef prep_term prep_constraint ((b, raw_args, mx), set, opt_morphs) lthy =
   256   let
   257     val args = map (apsnd (prep_constraint lthy)) raw_args;
   258     val ((goal, goal_pat, typedef_result), lthy') =
   259       prepare_typedef prep_term (b, args, mx) set opt_morphs lthy;
   260     fun after_qed [[th]] = snd o typedef_result th;
   261   in Proof.theorem NONE after_qed [[(goal, [goal_pat])]] lthy' end;
   262 
   263 in
   264 
   265 val typedef = gen_typedef Syntax.check_term (K I);
   266 val typedef_cmd = gen_typedef Syntax.read_term Typedecl.read_constraint;
   267 
   268 end;
   269 
   270 
   271 
   272 (** outer syntax **)
   273 
   274 val _ =
   275   Outer_Syntax.local_theory_to_proof @{command_spec "typedef"}
   276     "HOL type definition (requires non-emptiness proof)"
   277     (Parse.type_args_constrained -- Parse.binding -- Parse.opt_mixfix --
   278       (@{keyword "="} |-- Parse.term) --
   279       Scan.option (@{keyword "morphisms"} |-- Parse.!!! (Parse.binding -- Parse.binding))
   280     >> (fn ((((vs, t), mx), A), morphs) => fn lthy => typedef_cmd ((t, vs, mx), A, morphs) lthy));
   281 
   282 end;
   283