src/HOL/Tools/typedef_package.ML
author wenzelm
Wed Jan 05 11:56:04 2000 +0100 (2000-01-05)
changeset 8100 6186ee807f2e
parent 7481 d44c77be268c
child 8141 d6d896e81cd7
permissions -rw-r--r--
replaced HOLogic.termTVar by HOLogic.termT;
     1 (*  Title:      HOL/Tools/typedef_package.ML
     2     ID:         $Id$
     3     Author:     Markus Wenzel, TU Muenchen
     4 
     5 Gordon/HOL-style type definitions.
     6 *)
     7 
     8 signature TYPEDEF_PACKAGE =
     9 sig
    10   val quiet_mode: bool ref
    11   val add_typedecls: (bstring * string list * mixfix) list -> theory -> theory
    12   val add_typedef: string -> bstring * string list * mixfix ->
    13     string -> string list -> thm list -> tactic option -> theory -> theory
    14   val add_typedef_i: string -> bstring * string list * mixfix ->
    15     term -> string list -> thm list -> tactic option -> theory -> theory
    16   val add_typedef_i_no_def: string -> bstring * string list * mixfix ->
    17     term -> string list -> thm list -> tactic option -> theory -> theory
    18   val typedef_proof: (string * (bstring * string list * mixfix) * string) * Comment.text
    19     -> bool -> theory -> ProofHistory.T
    20   val typedef_proof_i: (string * (bstring * string list * mixfix) * term) * Comment.text
    21     -> bool -> theory -> ProofHistory.T
    22 end;
    23 
    24 structure TypedefPackage: TYPEDEF_PACKAGE =
    25 struct
    26 
    27 
    28 (** type declarations **)
    29 
    30 fun add_typedecls decls thy =
    31   let
    32     val full = Sign.full_name (Theory.sign_of thy);
    33 
    34     fun arity_of (raw_name, args, mx) =
    35       (full (Syntax.type_name raw_name mx), replicate (length args) HOLogic.termS, HOLogic.termS);
    36   in
    37     thy
    38     |> PureThy.add_typedecls decls
    39     |> Theory.add_arities_i (map arity_of decls)
    40   end;
    41 
    42 
    43 
    44 (** type definitions **)
    45 
    46 (* messages *)
    47 
    48 val quiet_mode = ref false;
    49 fun message s = if ! quiet_mode then () else writeln s;
    50 
    51 
    52 (* non-emptiness of set *)              (*exception ERROR*)
    53 
    54 fun check_nonempty cset thm =
    55   let
    56     val {t, sign, maxidx, ...} = Thm.rep_cterm cset;
    57     val {prop, ...} = Thm.rep_thm (Thm.transfer_sg sign (Drule.standard thm));
    58     val matches = Pattern.matches (Sign.tsig_of sign);
    59   in
    60     (case try (HOLogic.dest_mem o HOLogic.dest_Trueprop) prop of
    61       None => raise ERROR
    62     | Some (_, A) => if matches (Logic.incr_indexes ([], maxidx) A, t) then () else raise ERROR)
    63   end handle ERROR => error ("Bad non-emptiness theorem " ^ Display.string_of_thm thm ^
    64     "\nfor set " ^ quote (Display.string_of_cterm cset));
    65 
    66 fun goal_nonempty ex cset =
    67   let
    68     val {T = setT, t = A, maxidx, sign} = Thm.rep_cterm cset;
    69     val T = HOLogic.dest_setT setT;
    70     val tm =
    71       if ex then HOLogic.mk_exists ("x", T, HOLogic.mk_mem (Free ("x", T), A))
    72       else HOLogic.mk_mem (Var (("x", maxidx + 1), T), A);   (*old-style version*)
    73   in Thm.cterm_of sign (HOLogic.mk_Trueprop tm) end;
    74 
    75 fun prove_nonempty thy cset (witn_names, witn_thms, witn_tac) =
    76   let
    77     val is_def = Logic.is_equals o #prop o Thm.rep_thm;
    78     val thms = PureThy.get_thmss thy witn_names @ witn_thms;
    79     val tac =
    80       TRY (rewrite_goals_tac (filter is_def thms)) THEN
    81       TRY (REPEAT_FIRST (resolve_tac (filter_out is_def thms))) THEN
    82       if_none witn_tac (TRY (ALLGOALS (CLASET' blast_tac)));
    83   in
    84     message ("Proving non-emptiness of set " ^ quote (string_of_cterm cset) ^ " ...");
    85     prove_goalw_cterm [] (goal_nonempty false cset) (K [tac])
    86   end handle ERROR => error ("Failed to prove non-emptiness of " ^ quote (string_of_cterm cset));
    87 
    88 
    89 (* prepare_typedef *)
    90 
    91 fun read_term sg used s =
    92   #1 (Thm.read_def_cterm (sg, K None, K None) used true (s, HOLogic.termT));
    93 
    94 fun cert_term sg _ t = Thm.cterm_of sg t handle TERM (msg, _) => error msg;
    95 
    96 fun err_in_typedef name =
    97   error ("The error(s) above occurred in typedef " ^ quote name);
    98 
    99 fun prepare_typedef prep_term no_def name (t, vs, mx) raw_set thy =
   100   let
   101     val _ = Theory.requires thy "Set" "typedefs";
   102     val sign = Theory.sign_of thy;
   103     val full_name = Sign.full_name sign;
   104 
   105     (*rhs*)
   106     val cset = prep_term sign vs raw_set;
   107     val {T = setT, t = set, ...} = Thm.rep_cterm cset;
   108     val rhs_tfrees = term_tfrees set;
   109     val oldT = HOLogic.dest_setT setT handle TYPE _ =>
   110       error ("Not a set type: " ^ quote (Sign.string_of_typ sign setT));
   111 
   112     (*lhs*)
   113     val lhs_tfrees = map (fn v => (v, if_none (assoc (rhs_tfrees, v)) HOLogic.termS)) vs;
   114     val tname = Syntax.type_name t mx;
   115     val newT = Type (full_name tname, map TFree lhs_tfrees);
   116 
   117     val Rep_name = "Rep_" ^ name;
   118     val Abs_name = "Abs_" ^ name;
   119     val setC = Const (full_name name, setT);
   120     val RepC = Const (full_name Rep_name, newT --> oldT);
   121     val AbsC = Const (full_name Abs_name, oldT --> newT);
   122     val x_new = Free ("x", newT);
   123     val y_old = Free ("y", oldT);
   124     val set' = if no_def then set else setC;
   125 
   126     (*axioms*)
   127     val rep_type = HOLogic.mk_Trueprop (HOLogic.mk_mem (RepC $ x_new, set'));
   128     val rep_type_inv = HOLogic.mk_Trueprop (HOLogic.mk_eq (AbsC $ (RepC $ x_new), x_new));
   129     val abs_type_inv = Logic.mk_implies (HOLogic.mk_Trueprop (HOLogic.mk_mem (y_old, set')),
   130       HOLogic.mk_Trueprop (HOLogic.mk_eq (RepC $ (AbsC $ y_old), y_old)));
   131 
   132     (*theory extender*)
   133     fun do_typedef theory =
   134       theory
   135       |> Theory.assert_super thy
   136       |> add_typedecls [(t, vs, mx)]
   137       |> Theory.add_consts_i
   138        ((if no_def then [] else [(name, setT, NoSyn)]) @
   139         [(Rep_name, newT --> oldT, NoSyn),
   140          (Abs_name, oldT --> newT, NoSyn)])
   141       |> (if no_def then I else (PureThy.add_defs_i o map Thm.no_attributes)
   142        [Logic.mk_defpair (setC, set)])
   143       |> (PureThy.add_axioms_i o map Thm.no_attributes)
   144        [(Rep_name, rep_type),
   145         (Rep_name ^ "_inverse", rep_type_inv),
   146         (Abs_name ^ "_inverse", abs_type_inv)]
   147       handle ERROR => err_in_typedef name;
   148 
   149 
   150     (* errors *)
   151 
   152     fun show_names pairs = commas_quote (map fst pairs);
   153 
   154     val illegal_vars =
   155       if null (term_vars set) andalso null (term_tvars set) then []
   156       else ["Illegal schematic variable(s) on rhs"];
   157 
   158     val dup_lhs_tfrees =
   159       (case duplicates lhs_tfrees of [] => []
   160       | dups => ["Duplicate type variables on lhs: " ^ show_names dups]);
   161 
   162     val extra_rhs_tfrees =
   163       (case gen_rems (op =) (rhs_tfrees, lhs_tfrees) of [] => []
   164       | extras => ["Extra type variables on rhs: " ^ show_names extras]);
   165 
   166     val illegal_frees =
   167       (case term_frees set of [] => []
   168       | xs => ["Illegal variables on rhs: " ^ show_names (map dest_Free xs)]);
   169 
   170     val errs = illegal_vars @ dup_lhs_tfrees @ extra_rhs_tfrees @ illegal_frees;
   171   in
   172     if null errs then () else error (cat_lines errs);
   173     (cset, do_typedef)
   174   end handle ERROR => err_in_typedef name;
   175 
   176 
   177 (* add_typedef interfaces *)
   178 
   179 fun gen_add_typedef prep_term no_def name typ set names thms tac thy =
   180   let
   181     val (cset, do_typedef) = prepare_typedef prep_term no_def name typ set thy;
   182     val result = prove_nonempty thy cset (names, thms, tac);
   183   in check_nonempty cset result; thy |> do_typedef end;
   184 
   185 val add_typedef = gen_add_typedef read_term false;
   186 val add_typedef_i = gen_add_typedef cert_term false;
   187 val add_typedef_i_no_def = gen_add_typedef cert_term true;
   188 
   189 
   190 (* typedef_proof interface *)
   191 
   192 fun typedef_attribute cset do_typedef (thy, thm) =
   193   (check_nonempty cset (thm RS (select_eq_Ex RS iffD2)); (thy |> do_typedef, thm));
   194 
   195 fun gen_typedef_proof prep_term ((name, typ, set), comment) int thy =
   196   let
   197     val (cset, do_typedef) = prepare_typedef prep_term false name typ set thy;
   198     val goal = Thm.term_of (goal_nonempty true cset);
   199   in
   200     thy
   201     |> IsarThy.theorem_i (("", [typedef_attribute cset do_typedef], (goal, ([], []))), comment) int
   202   end;
   203 
   204 val typedef_proof = gen_typedef_proof read_term;
   205 val typedef_proof_i = gen_typedef_proof cert_term;
   206 
   207 
   208 
   209 (** outer syntax **)
   210 
   211 local structure P = OuterParse and K = OuterSyntax.Keyword in
   212 
   213 val typedeclP =
   214   OuterSyntax.command "typedecl" "HOL type declaration" K.thy_decl
   215     (P.type_args -- P.name -- P.opt_infix --| P.marg_comment >> (fn ((vs, t), mx) =>
   216       Toplevel.theory (add_typedecls [(t, vs, mx)])));
   217 
   218 
   219 val typedef_proof_decl =
   220   Scan.option (P.$$$ "(" |-- P.name --| P.$$$ ")") --
   221     (P.type_args -- P.name) -- P.opt_infix -- (P.$$$ "=" |-- P.term) -- P.marg_comment;
   222 
   223 fun mk_typedef_proof ((((opt_name, (vs, t)), mx), A), comment) =
   224   typedef_proof ((if_none opt_name (Syntax.type_name t mx), (t, vs, mx), A), comment);
   225 
   226 val typedefP =
   227   OuterSyntax.command "typedef" "HOL type definition (requires non-emptiness proof)" K.thy_goal
   228     (typedef_proof_decl >> (Toplevel.print oo (Toplevel.theory_to_proof o mk_typedef_proof)));
   229 
   230 
   231 val _ = OuterSyntax.add_parsers [typedeclP, typedefP];
   232 
   233 end;
   234 
   235 
   236 end;