src/HOL/Tools/typedef.ML
author huffman
Fri Mar 30 12:32:35 2012 +0200 (2012-03-30)
changeset 47220 52426c62b5d0
parent 46961 5c6955f487e5
child 47238 289dcbdd5984
permissions -rw-r--r--
replace lemmas eval_nat_numeral with a simpler reformulation
     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, set_def: thm option, Rep: thm, Rep_inverse: thm,
    13     Abs_inverse: thm, 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: bool -> binding option -> binding * (string * sort) list * mixfix ->
    21     term -> (binding * binding) option -> tactic -> local_theory -> (string * info) * local_theory
    22   val add_typedef_global: bool -> binding option -> binding * (string * sort) list * mixfix ->
    23     term -> (binding * binding) option -> tactic -> theory -> (string * info) * theory
    24   val typedef: (bool * binding) * (binding * (string * sort) list * mixfix) * term *
    25     (binding * binding) option -> local_theory -> Proof.state
    26   val typedef_cmd: (bool * binding) * (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, set_def: thm option, Rep: thm, Rep_inverse: thm,
    42     Abs_inverse: thm, 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,
    49       set_def, Rep, Rep_inverse, Abs_inverse, Rep_inject, Abs_inject,
    50       Rep_cases, Abs_cases, Rep_induct, Abs_induct}) = info;
    51   in
    52     (global_info,
    53      {inhabited = thm inhabited, type_definition = thm type_definition,
    54       set_def = Option.map thm set_def, Rep = thm Rep, Rep_inverse = thm Rep_inverse,
    55       Abs_inverse = thm Abs_inverse, Rep_inject = thm Rep_inject, Abs_inject = thm Abs_inject,
    56       Rep_cases = thm Rep_cases, Abs_cases = thm Abs_cases, Rep_induct = thm Rep_induct,
    57       Abs_induct = thm Abs_induct})
    58   end;
    59 
    60 structure Data = Generic_Data
    61 (
    62   type T = info list Symtab.table;
    63   val empty = Symtab.empty;
    64   val extend = I;
    65   fun merge data = Symtab.merge_list (K true) data;
    66 );
    67 
    68 val get_info = Symtab.lookup_list o Data.get o Context.Proof;
    69 val get_info_global = Symtab.lookup_list o Data.get o Context.Theory;
    70 
    71 fun put_info name info = Data.map (Symtab.cons_list (name, info));
    72 
    73 
    74 (* global interpretation *)
    75 
    76 structure Typedef_Interpretation = Interpretation(type T = string val eq = op =);
    77 val interpretation = Typedef_Interpretation.interpretation;
    78 
    79 val setup = Typedef_Interpretation.init;
    80 
    81 
    82 (* primitive typedef axiomatization -- for fresh typedecl *)
    83 
    84 fun mk_inhabited A =
    85   let val T = HOLogic.dest_setT (Term.fastype_of A)
    86   in HOLogic.mk_Trueprop (HOLogic.exists_const T $ Abs ("x", T, HOLogic.mk_mem (Bound 0, A))) end;
    87 
    88 fun mk_typedef newT oldT RepC AbsC A =
    89   let
    90     val typedefC =
    91       Const (@{const_name type_definition},
    92         (newT --> oldT) --> (oldT --> newT) --> HOLogic.mk_setT oldT --> HOLogic.boolT);
    93   in Logic.mk_implies (mk_inhabited A, HOLogic.mk_Trueprop (typedefC $ RepC $ AbsC $ A)) end;
    94 
    95 fun primitive_typedef typedef_name newT oldT Rep_name Abs_name A lthy =
    96   let
    97     (* errors *)
    98 
    99     fun show_names pairs = commas_quote (map fst pairs);
   100 
   101     val lhs_tfrees = Term.add_tfreesT newT [];
   102     val rhs_tfrees = Term.add_tfreesT oldT [];
   103     val _ =
   104       (case fold (remove (op =)) lhs_tfrees rhs_tfrees of [] => ()
   105       | extras => error ("Extra type variables in representing set: " ^ show_names extras));
   106 
   107     val _ =
   108       (case Term.add_frees A [] of [] => []
   109       | xs => error ("Illegal variables in representing set: " ^ show_names xs));
   110 
   111 
   112     (* axiomatization *)
   113 
   114     val ((RepC, AbsC), consts_lthy) = lthy
   115       |> Local_Theory.background_theory_result
   116         (Sign.declare_const lthy ((Rep_name, newT --> oldT), NoSyn) ##>>
   117           Sign.declare_const lthy ((Abs_name, oldT --> newT), NoSyn));
   118 
   119     val typedef_deps = Term.add_consts A [];
   120 
   121     val ((axiom_name, axiom), axiom_lthy) = consts_lthy
   122       |> Local_Theory.background_theory_result
   123         (Thm.add_axiom consts_lthy (typedef_name, mk_typedef newT oldT RepC AbsC A) ##>
   124           Theory.add_deps consts_lthy "" (dest_Const RepC) typedef_deps ##>
   125           Theory.add_deps consts_lthy "" (dest_Const AbsC) typedef_deps);
   126 
   127   in ((RepC, AbsC, axiom_name, axiom), axiom_lthy) end;
   128 
   129 
   130 (* prepare_typedef *)
   131 
   132 fun prepare_typedef prep_term def_set name (tname, raw_args, mx) raw_set opt_morphs lthy =
   133   let
   134     val full_name = Local_Theory.full_name lthy name;
   135     val bname = Binding.name_of name;
   136 
   137 
   138     (* rhs *)
   139 
   140     val tmp_ctxt = lthy |> fold (Variable.declare_typ o TFree) raw_args;
   141     val set = prep_term tmp_ctxt raw_set;
   142     val tmp_ctxt' = tmp_ctxt |> Variable.declare_term set;
   143 
   144     val setT = Term.fastype_of set;
   145     val oldT = HOLogic.dest_setT setT handle TYPE _ =>
   146       error ("Not a set type: " ^ quote (Syntax.string_of_typ lthy setT));
   147 
   148     val goal = mk_inhabited set;
   149     val goal_pat = mk_inhabited (Var (the_default (bname, 0) (Lexicon.read_variable bname), setT));
   150 
   151 
   152     (* lhs *)
   153 
   154     val args = map (Proof_Context.check_tfree tmp_ctxt') raw_args;
   155     val (newT, typedecl_lthy) = lthy
   156       |> Typedecl.typedecl (tname, args, mx)
   157       ||> Variable.declare_term set;
   158 
   159     val Type (full_tname, type_args) = newT;
   160     val lhs_tfrees = map Term.dest_TFree type_args;
   161 
   162 
   163     (* set definition *)
   164 
   165     val ((set', set_def), set_lthy) =
   166       if def_set then
   167         typedecl_lthy
   168         |> Local_Theory.define ((name, NoSyn), ((Thm.def_binding name, []), set))
   169         |>> (fn (set', (_, set_def)) => (set', SOME set_def))
   170       else ((set, NONE), typedecl_lthy);
   171 
   172 
   173     (* axiomatization *)
   174 
   175     val (Rep_name, Abs_name) =
   176       (case opt_morphs of
   177         NONE => (Binding.prefix_name "Rep_" name, Binding.prefix_name "Abs_" name)
   178       | SOME morphs => morphs);
   179 
   180     val typedef_name = Binding.prefix_name "type_definition_" name;
   181 
   182     val ((RepC, AbsC, axiom_name, typedef), typedef_lthy) =
   183       let
   184         val thy = Proof_Context.theory_of set_lthy;
   185         val cert = Thm.cterm_of thy;
   186         val ((defs, _), A) =
   187           Local_Defs.export_cterm set_lthy (Proof_Context.init_global thy) (cert set')
   188           ||> Thm.term_of;
   189 
   190         val ((RepC, AbsC, axiom_name, axiom), axiom_lthy) = set_lthy
   191           |> primitive_typedef typedef_name newT oldT Rep_name Abs_name A;
   192 
   193         val cert = Thm.cterm_of (Proof_Context.theory_of axiom_lthy);
   194         val typedef =
   195           Local_Defs.contract axiom_lthy defs (cert (mk_typedef newT oldT RepC AbsC set')) axiom;
   196       in ((RepC, AbsC, axiom_name, typedef), axiom_lthy) end;
   197 
   198     val alias_lthy = typedef_lthy
   199       |> Local_Theory.const_alias Rep_name (#1 (Term.dest_Const RepC))
   200       |> Local_Theory.const_alias Abs_name (#1 (Term.dest_Const AbsC));
   201 
   202 
   203     (* result *)
   204 
   205     fun note_qualify ((b, atts), th) =
   206       Local_Theory.note ((Binding.qualify false bname b, map (Attrib.internal o K) atts), [th])
   207       #>> (fn (_, [th']) => th');
   208 
   209     fun typedef_result inhabited lthy1 =
   210       let
   211         val cert = Thm.cterm_of (Proof_Context.theory_of lthy1);
   212         val inhabited' =
   213           Local_Defs.contract lthy1 (the_list set_def) (cert (mk_inhabited set')) inhabited;
   214         val typedef' = inhabited' RS typedef;
   215         fun make th = Goal.norm_result (typedef' RS th);
   216         val (((((((((((_, [type_definition]), Rep), Rep_inverse), Abs_inverse), Rep_inject),
   217             Abs_inject), Rep_cases), Abs_cases), Rep_induct), Abs_induct), lthy2) = lthy1
   218           |> Local_Theory.note ((typedef_name, []), [typedef'])
   219           ||>> note_qualify ((Rep_name, []), make @{thm type_definition.Rep})
   220           ||>> note_qualify ((Binding.suffix_name "_inverse" Rep_name, []),
   221               make @{thm type_definition.Rep_inverse})
   222           ||>> note_qualify ((Binding.suffix_name "_inverse" Abs_name, []),
   223               make @{thm type_definition.Abs_inverse})
   224           ||>> note_qualify ((Binding.suffix_name "_inject" Rep_name, []),
   225               make @{thm type_definition.Rep_inject})
   226           ||>> note_qualify ((Binding.suffix_name "_inject" Abs_name, []),
   227               make @{thm type_definition.Abs_inject})
   228           ||>> note_qualify ((Binding.suffix_name "_cases" Rep_name,
   229                 [Rule_Cases.case_names [Binding.name_of Rep_name], Induct.cases_pred full_name]),
   230               make @{thm type_definition.Rep_cases})
   231           ||>> note_qualify ((Binding.suffix_name "_cases" Abs_name,
   232                 [Rule_Cases.case_names [Binding.name_of Abs_name], Induct.cases_type full_tname]),
   233               make @{thm type_definition.Abs_cases})
   234           ||>> note_qualify ((Binding.suffix_name "_induct" Rep_name,
   235                 [Rule_Cases.case_names [Binding.name_of Rep_name], Induct.induct_pred full_name]),
   236               make @{thm type_definition.Rep_induct})
   237           ||>> note_qualify ((Binding.suffix_name "_induct" Abs_name,
   238                 [Rule_Cases.case_names [Binding.name_of Abs_name], Induct.induct_type full_tname]),
   239               make @{thm type_definition.Abs_induct});
   240 
   241         val info =
   242           ({rep_type = oldT, abs_type = newT, Rep_name = #1 (Term.dest_Const RepC),
   243             Abs_name = #1 (Term.dest_Const AbsC), axiom_name = axiom_name},
   244            {inhabited = inhabited, type_definition = type_definition, set_def = set_def,
   245             Rep = Rep, Rep_inverse = Rep_inverse, Abs_inverse = Abs_inverse,
   246             Rep_inject = Rep_inject, Abs_inject = Abs_inject, Rep_cases = Rep_cases,
   247           Abs_cases = Abs_cases, Rep_induct = Rep_induct, Abs_induct = Abs_induct});
   248       in
   249         lthy2
   250         |> Local_Theory.declaration {syntax = false, pervasive = true}
   251           (fn phi => put_info full_tname (transform_info phi info))
   252         |> Local_Theory.background_theory (Typedef_Interpretation.data full_tname)
   253         |> pair (full_tname, info)
   254       end;
   255 
   256   in ((goal, goal_pat, typedef_result), alias_lthy) end
   257   handle ERROR msg =>
   258     cat_error msg ("The error(s) above occurred in typedef " ^ Binding.print name);
   259 
   260 
   261 (* add_typedef: tactic interface *)
   262 
   263 fun add_typedef def opt_name typ set opt_morphs tac lthy =
   264   let
   265     val name = the_default (#1 typ) opt_name;
   266     val ((goal, _, typedef_result), lthy') =
   267       prepare_typedef Syntax.check_term def name typ set opt_morphs lthy;
   268     val inhabited =
   269       Goal.prove lthy' [] [] goal (K tac)
   270       |> Goal.norm_result |> Thm.close_derivation;
   271   in typedef_result inhabited lthy' end;
   272 
   273 fun add_typedef_global def opt_name typ set opt_morphs tac =
   274   Named_Target.theory_init
   275   #> add_typedef def opt_name typ set opt_morphs tac
   276   #> Local_Theory.exit_result_global (apsnd o transform_info);
   277 
   278 
   279 (* typedef: proof interface *)
   280 
   281 local
   282 
   283 fun gen_typedef prep_term prep_constraint ((def, name), (b, raw_args, mx), set, opt_morphs) lthy =
   284   let
   285     val args = map (apsnd (prep_constraint lthy)) raw_args;
   286     val ((goal, goal_pat, typedef_result), lthy') =
   287       prepare_typedef prep_term def name (b, args, mx) set opt_morphs lthy;
   288     fun after_qed [[th]] = snd o typedef_result th;
   289   in Proof.theorem NONE after_qed [[(goal, [goal_pat])]] lthy' end;
   290 
   291 in
   292 
   293 val typedef = gen_typedef Syntax.check_term (K I);
   294 val typedef_cmd = gen_typedef Syntax.read_term Typedecl.read_constraint;
   295 
   296 end;
   297 
   298 
   299 
   300 (** outer syntax **)
   301 
   302 val _ =
   303   Outer_Syntax.local_theory_to_proof @{command_spec "typedef"}
   304     "HOL type definition (requires non-emptiness proof)"
   305     (Scan.optional (@{keyword "("} |--
   306         ((@{keyword "open"} >> K false) -- Scan.option Parse.binding ||
   307           Parse.binding >> (fn s => (true, SOME s))) --| @{keyword ")"}) (true, NONE) --
   308       (Parse.type_args_constrained -- Parse.binding) --
   309         Parse.opt_mixfix -- (@{keyword "="} |-- Parse.term) --
   310         Scan.option (@{keyword "morphisms"} |-- Parse.!!! (Parse.binding -- Parse.binding))
   311     >> (fn ((((((def, opt_name), (args, t)), mx), A), morphs)) =>
   312         typedef_cmd ((def, the_default t opt_name), (t, args, mx), A, morphs)));
   313 
   314 end;
   315