src/HOL/Tools/typedef_package.ML
author wenzelm
Thu Jan 03 17:56:15 2002 +0100 (2002-01-03)
changeset 12624 9ed65232429c
parent 12338 de0f4a63baa5
child 12694 9950c1ce9d24
permissions -rw-r--r--
tuned msg;
     1 (*  Title:      HOL/Tools/typedef_package.ML
     2     ID:         $Id$
     3     Author:     Markus Wenzel, TU Muenchen
     4     License:    GPL (GNU GENERAL PUBLIC LICENSE)
     5 
     6 Gordon/HOL-style type definitions.
     7 *)
     8 
     9 signature TYPEDEF_PACKAGE =
    10 sig
    11   val quiet_mode: bool ref
    12   val add_typedecls: (bstring * string list * mixfix) list -> theory -> theory
    13   val add_typedef_x: string -> bstring * string list * mixfix ->
    14     string -> string list -> thm list -> tactic option -> theory -> theory
    15   val add_typedef: bool -> string option -> bstring * string list * mixfix ->
    16     string -> (bstring * bstring) option -> tactic -> theory -> theory *
    17     {type_definition: thm, set_def: thm option, Rep: thm, Rep_inverse: thm,
    18       Abs_inverse: thm, Rep_inject: thm, Abs_inject: thm, Rep_cases: thm, Abs_cases: thm,
    19       Rep_induct: thm, Abs_induct: thm}
    20   val add_typedef_i: bool -> string option -> bstring * string list * mixfix ->
    21     term -> (bstring * bstring) option -> tactic -> theory -> theory *
    22     {type_definition: thm, set_def: thm option, Rep: thm, Rep_inverse: thm,
    23       Abs_inverse: thm, Rep_inject: thm, Abs_inject: thm, Rep_cases: thm, Abs_cases: thm,
    24       Rep_induct: thm, Abs_induct: thm}
    25   val typedef_proof:
    26     (string * (bstring * string list * mixfix) * string * (string * string) option) * Comment.text
    27     -> bool -> theory -> ProofHistory.T
    28   val typedef_proof_i:
    29     (string * (bstring * string list * mixfix) * term * (string * string) option) * Comment.text
    30     -> bool -> theory -> ProofHistory.T
    31 end;
    32 
    33 structure TypedefPackage: TYPEDEF_PACKAGE =
    34 struct
    35 
    36 
    37 (** theory context references **)
    38 
    39 val type_definitionN = "Typedef.type_definition";
    40 val type_definition_def = thm "type_definition_def";
    41 
    42 val Rep = thm "Rep";
    43 val Rep_inverse = thm "Rep_inverse";
    44 val Abs_inverse = thm "Abs_inverse";
    45 val Rep_inject = thm "Rep_inject";
    46 val Abs_inject = thm "Abs_inject";
    47 val Rep_cases = thm "Rep_cases";
    48 val Abs_cases = thm "Abs_cases";
    49 val Rep_induct = thm "Rep_induct";
    50 val Abs_induct = thm "Abs_induct";
    51 
    52 
    53 
    54 (** type declarations **)
    55 
    56 fun add_typedecls decls thy =
    57   let
    58     val full = Sign.full_name (Theory.sign_of thy);
    59 
    60     fun arity_of (raw_name, args, mx) =
    61       (full (Syntax.type_name raw_name mx), replicate (length args) HOLogic.typeS, HOLogic.typeS);
    62   in
    63     if can (Theory.assert_super HOL.thy) thy then
    64       thy
    65       |> PureThy.add_typedecls decls
    66       |> Theory.add_arities_i (map arity_of decls)
    67     else thy |> PureThy.add_typedecls decls
    68   end;
    69 
    70 
    71 
    72 (** type definitions **)
    73 
    74 (* messages *)
    75 
    76 val quiet_mode = ref false;
    77 fun message s = if ! quiet_mode then () else writeln s;
    78 
    79 
    80 (* prove_nonempty -- tactical version *)        (*exception ERROR*)
    81 
    82 fun prove_nonempty thy cset goal (witn1_tac, witn_names, witn_thms, witn2_tac) =
    83   let
    84     val is_def = Logic.is_equals o #prop o Thm.rep_thm;
    85     val thms = PureThy.get_thmss thy witn_names @ witn_thms;
    86     val tac =
    87       witn1_tac THEN
    88       TRY (rewrite_goals_tac (filter is_def thms)) THEN
    89       TRY (REPEAT_FIRST (resolve_tac (filter_out is_def thms))) THEN
    90       if_none witn2_tac (TRY (ALLGOALS (CLASET' blast_tac)));
    91   in
    92     message ("Proving non-emptiness of set " ^ quote (string_of_cterm cset) ^ " ...");
    93     Tactic.prove (Theory.sign_of thy) [] [] goal (K tac)
    94   end handle ERROR => error ("Failed to prove non-emptiness of " ^ quote (string_of_cterm cset));
    95 
    96 
    97 (* prepare_typedef *)
    98 
    99 fun read_term sg used s =
   100   #1 (Thm.read_def_cterm (sg, K None, K None) used true (s, HOLogic.typeT));
   101 
   102 fun cert_term sg _ t = Thm.cterm_of sg t handle TERM (msg, _) => error msg;
   103 
   104 fun err_in_typedef name =
   105   error ("The error(s) above occurred in typedef " ^ quote name);
   106 
   107 fun prepare_typedef prep_term def name (t, vs, mx) raw_set opt_morphs thy =
   108   let
   109     val _ = Theory.requires thy "Typedef" "typedefs";
   110     val sign = Theory.sign_of thy;
   111     val full = Sign.full_name sign;
   112 
   113     (*rhs*)
   114     val full_name = full name;
   115     val cset = prep_term sign vs raw_set;
   116     val {T = setT, t = set, ...} = Thm.rep_cterm cset;
   117     val rhs_tfrees = term_tfrees set;
   118     val oldT = HOLogic.dest_setT setT handle TYPE _ =>
   119       error ("Not a set type: " ^ quote (Sign.string_of_typ sign setT));
   120     fun mk_nonempty A =
   121       HOLogic.mk_Trueprop (HOLogic.mk_exists ("x", oldT, HOLogic.mk_mem (Free ("x", oldT), A)));
   122     val goal = mk_nonempty set;
   123     val vname = take_suffix Symbol.is_digit (Symbol.explode name)
   124       |> apfst implode |> apsnd (#1 o Term.read_int);
   125     val goal_pat = mk_nonempty (Var (vname, setT));
   126 
   127     (*lhs*)
   128     val lhs_tfrees = map (fn v => (v, if_none (assoc (rhs_tfrees, v)) HOLogic.typeS)) vs;
   129     val tname = Syntax.type_name t mx;
   130     val full_tname = full tname;
   131     val newT = Type (full_tname, map TFree lhs_tfrees);
   132 
   133     val (Rep_name, Abs_name) = if_none opt_morphs ("Rep_" ^ name, "Abs_" ^ name);
   134     val setC = Const (full_name, setT);
   135     val RepC = Const (full Rep_name, newT --> oldT);
   136     val AbsC = Const (full Abs_name, oldT --> newT);
   137     val x_new = Free ("x", newT);
   138     val y_old = Free ("y", oldT);
   139 
   140     val set' = if def then setC else set;
   141 
   142     val typedef_name = "type_definition_" ^ name;
   143     val typedefC =
   144       Const (type_definitionN, (newT --> oldT) --> (oldT --> newT) --> setT --> HOLogic.boolT);
   145     val typedef_prop =
   146       Logic.mk_implies (goal, HOLogic.mk_Trueprop (typedefC $ RepC $ AbsC $ set'));
   147 
   148     fun typedef_result (theory, nonempty) =
   149       theory
   150       |> add_typedecls [(t, vs, mx)]
   151       |> Theory.add_consts_i
   152        ((if def then [(name, setT, NoSyn)] else []) @
   153         [(Rep_name, newT --> oldT, NoSyn),
   154          (Abs_name, oldT --> newT, NoSyn)])
   155       |> (if def then (apsnd (Some o hd) oo (PureThy.add_defs_i false o map Thm.no_attributes))
   156            [Logic.mk_defpair (setC, set)]
   157           else rpair None)
   158       |>> PureThy.add_axioms_i [((typedef_name, typedef_prop),
   159           [apsnd (fn cond_axm => Drule.standard (nonempty RS cond_axm))])]
   160       |> (fn ((theory', [type_definition]), set_def) =>
   161         let
   162           fun make th = Drule.standard (th OF [type_definition]);
   163           val (theory'', [Rep, Rep_inverse, Abs_inverse, Rep_inject, Abs_inject,
   164               Rep_cases, Abs_cases, Rep_induct, Abs_induct]) =
   165             theory'
   166             |> Theory.add_path name
   167             |> PureThy.add_thms
   168               ([((Rep_name, make Rep), []),
   169                 ((Rep_name ^ "_inverse", make Rep_inverse), []),
   170                 ((Abs_name ^ "_inverse", make Abs_inverse), []),
   171                 ((Rep_name ^ "_inject", make Rep_inject), []),
   172                 ((Abs_name ^ "_inject", make Abs_inject), []),
   173                 ((Rep_name ^ "_cases", make Rep_cases),
   174                   [RuleCases.case_names [Rep_name], InductAttrib.cases_set_global full_name]),
   175                 ((Abs_name ^ "_cases", make Abs_cases),
   176                   [RuleCases.case_names [Abs_name], InductAttrib.cases_type_global full_tname]),
   177                 ((Rep_name ^ "_induct", make Rep_induct),
   178                   [RuleCases.case_names [Rep_name], InductAttrib.induct_set_global full_name]),
   179                 ((Abs_name ^ "_induct", make Abs_induct),
   180                   [RuleCases.case_names [Abs_name], InductAttrib.induct_type_global full_tname])])
   181             |>> Theory.parent_path;
   182           val result = {type_definition = type_definition, set_def = set_def,
   183             Rep = Rep, Rep_inverse = Rep_inverse, Abs_inverse = Abs_inverse,
   184             Rep_inject = Rep_inject, Abs_inject = Abs_inject, Rep_cases = Rep_cases,
   185             Abs_cases = Abs_cases, Rep_induct = Rep_induct, Abs_induct = Abs_induct};
   186         in ((theory'', type_definition), result) end);
   187 
   188 
   189     (* errors *)
   190 
   191     fun show_names pairs = commas_quote (map fst pairs);
   192 
   193     val illegal_vars =
   194       if null (term_vars set) andalso null (term_tvars set) then []
   195       else ["Illegal schematic variable(s) on rhs"];
   196 
   197     val dup_lhs_tfrees =
   198       (case duplicates lhs_tfrees of [] => []
   199       | dups => ["Duplicate type variables on lhs: " ^ show_names dups]);
   200 
   201     val extra_rhs_tfrees =
   202       (case gen_rems (op =) (rhs_tfrees, lhs_tfrees) of [] => []
   203       | extras => ["Extra type variables on rhs: " ^ show_names extras]);
   204 
   205     val illegal_frees =
   206       (case term_frees set of [] => []
   207       | xs => ["Illegal variables on rhs: " ^ show_names (map dest_Free xs)]);
   208 
   209     val errs = illegal_vars @ dup_lhs_tfrees @ extra_rhs_tfrees @ illegal_frees;
   210     val _ = if null errs then () else error (cat_lines errs);
   211 
   212     (*test theory errors now!*)
   213     val test_thy = Theory.copy thy;
   214     val _ = (test_thy,
   215       setmp quick_and_dirty true (SkipProof.make_thm test_thy) goal) |> typedef_result;
   216 
   217   in (cset, goal, goal_pat, typedef_result) end
   218   handle ERROR => err_in_typedef name;
   219 
   220 
   221 (* add_typedef interfaces *)
   222 
   223 fun gen_typedef prep_term def name typ set opt_morphs tac1 names thms tac2 thy =
   224   let
   225     val (cset, goal, _, typedef_result) =
   226       prepare_typedef prep_term def name typ set opt_morphs thy;
   227     val non_empty = prove_nonempty thy cset goal (tac1, names, thms, tac2);
   228     val ((thy', _), result) = (thy, non_empty) |> typedef_result;
   229   in (thy', result) end;
   230 
   231 fun sane_typedef prep_term def opt_name typ set opt_morphs tac =
   232   gen_typedef prep_term def
   233     (if_none opt_name (#1 typ)) typ set opt_morphs all_tac [] [] (Some tac);
   234 
   235 fun add_typedef_x name typ set names thms tac =
   236   #1 o gen_typedef read_term true name typ set None (Tactic.rtac exI 1) names thms tac;
   237 
   238 val add_typedef = sane_typedef read_term;
   239 val add_typedef_i = sane_typedef cert_term;
   240 
   241 
   242 (* typedef_proof interface *)
   243 
   244 fun gen_typedef_proof prep_term ((name, typ, set, opt_morphs), comment) int thy =
   245   let
   246     val (_, goal, goal_pat, att_result) =
   247       prepare_typedef prep_term true name typ set opt_morphs thy;
   248     val att = #1 o att_result;
   249   in
   250     thy |> IsarThy.theorem_i Drule.internalK (None, [])
   251       ((("", [att]), (goal, ([goal_pat], []))), comment) int
   252   end;
   253 
   254 val typedef_proof = gen_typedef_proof read_term;
   255 val typedef_proof_i = gen_typedef_proof cert_term;
   256 
   257 
   258 
   259 (** outer syntax **)
   260 
   261 local structure P = OuterParse and K = OuterSyntax.Keyword in
   262 
   263 val typedeclP =
   264   OuterSyntax.command "typedecl" "type declaration (HOL)" K.thy_decl
   265     (P.type_args -- P.name -- P.opt_infix --| P.marg_comment >> (fn ((vs, t), mx) =>
   266       Toplevel.theory (add_typedecls [(t, vs, mx)])));
   267 
   268 
   269 val typedef_proof_decl =
   270   Scan.option (P.$$$ "(" |-- P.name --| P.$$$ ")") --
   271     (P.type_args -- P.name) -- P.opt_infix -- (P.$$$ "=" |-- P.term) --
   272     Scan.option (P.$$$ "morphisms" |-- P.!!! (P.name -- P.name)) --
   273     P.marg_comment;
   274 
   275 fun mk_typedef_proof (((((opt_name, (vs, t)), mx), A), morphs), comment) =
   276   typedef_proof ((if_none opt_name (Syntax.type_name t mx), (t, vs, mx), A, morphs), comment);
   277 
   278 val typedefP =
   279   OuterSyntax.command "typedef" "HOL type definition (requires non-emptiness proof)" K.thy_goal
   280     (typedef_proof_decl >> (Toplevel.print oo (Toplevel.theory_to_proof o mk_typedef_proof)));
   281 
   282 
   283 val _ = OuterSyntax.add_keywords ["morphisms"];
   284 val _ = OuterSyntax.add_parsers [typedeclP, typedefP];
   285 
   286 end;
   287 
   288 
   289 end;