src/HOL/Tools/typedef_package.ML
author berghofe
Fri Jul 24 12:55:05 1998 +0200 (1998-07-24)
changeset 5180 d82a70766af0
parent 5104 230cca8452c7
child 5697 e816c4f1a396
permissions -rw-r--r--
Added new function add_typedef_i_no_def which doesn't add
definition of representing set to theory.
     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 add_typedecls: (bstring * string list * mixfix) list -> theory -> theory
    11   val prove_nonempty: cterm -> thm list -> tactic option -> thm
    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 end;
    19 
    20 structure TypedefPackage: TYPEDEF_PACKAGE =
    21 struct
    22 
    23 
    24 (** type declarations **)
    25 
    26 fun add_typedecls decls thy =
    27   let
    28     val full = Sign.full_name (Theory.sign_of thy);
    29 
    30     fun arity_of (raw_name, args, mx) =
    31       (full (Syntax.type_name raw_name mx), replicate (length args) HOLogic.termS, HOLogic.termS);
    32   in
    33     thy
    34     |> PureThy.add_typedecls decls
    35     |> Theory.add_arities_i (map arity_of decls)
    36   end;
    37 
    38 
    39 
    40 (** type definitions **)
    41 
    42 (* prove non-emptyness of a set *)   (*exception ERROR*)
    43 
    44 val is_def = Logic.is_equals o #prop o rep_thm;
    45 
    46 fun prove_nonempty cset thms usr_tac =
    47   let
    48     val {T = setT, t = set, maxidx, sign} = rep_cterm cset;
    49     val T = HOLogic.dest_setT setT;
    50     val goal = cterm_of sign
    51       (HOLogic.mk_Trueprop (HOLogic.mk_mem (Var (("x", maxidx + 1), T), set)));
    52     val tac =
    53       TRY (rewrite_goals_tac (filter is_def thms)) THEN
    54       TRY (REPEAT_FIRST (resolve_tac (filter_out is_def thms))) THEN
    55       if_none usr_tac (TRY (ALLGOALS (CLASET' blast_tac)));
    56   in
    57     prove_goalw_cterm [] goal (K [tac])
    58   end
    59   handle ERROR =>
    60     error ("Failed to prove nonemptiness of " ^ quote (string_of_cterm cset));
    61 
    62 
    63 (* gen_add_typedef *)
    64 
    65 fun gen_add_typedef prep_term no_def name (t, vs, mx) raw_set axms thms usr_tac thy =
    66   let
    67     val _ = Theory.requires thy "Set" "typedefs";
    68     val sign = sign_of thy;
    69     val full_name = Sign.full_name sign;
    70 
    71     (*rhs*)
    72     val cset = prep_term sign raw_set;
    73     val {T = setT, t = set, ...} = rep_cterm cset;
    74     val rhs_tfrees = term_tfrees set;
    75     val oldT = HOLogic.dest_setT setT handle TYPE _ =>
    76       error ("Not a set type: " ^ quote (Sign.string_of_typ sign setT));
    77 
    78     (*lhs*)
    79     val lhs_tfrees =
    80       map (fn v => (v, if_none (assoc (rhs_tfrees, v)) HOLogic.termS)) vs;
    81 
    82     val tname = Syntax.type_name t mx;
    83     val newT = Type (full_name tname, map TFree lhs_tfrees);
    84 
    85     val Rep_name = "Rep_" ^ name;
    86     val Abs_name = "Abs_" ^ name;
    87     val setC = Const (full_name name, setT);
    88     val RepC = Const (full_name Rep_name, newT --> oldT);
    89     val AbsC = Const (full_name Abs_name, oldT --> newT);
    90     val x_new = Free ("x", newT);
    91     val y_old = Free ("y", oldT);
    92     val set' = if no_def then set else setC;
    93 
    94     (*axioms*)
    95     val rep_type = HOLogic.mk_Trueprop (HOLogic.mk_mem (RepC $ x_new, set'));
    96     val rep_type_inv = HOLogic.mk_Trueprop (HOLogic.mk_eq (AbsC $ (RepC $ x_new), x_new));
    97     val abs_type_inv = Logic.mk_implies (HOLogic.mk_Trueprop (HOLogic.mk_mem (y_old, set')),
    98       HOLogic.mk_Trueprop (HOLogic.mk_eq (RepC $ (AbsC $ y_old), y_old)));
    99 
   100 
   101     (* errors *)
   102 
   103     fun show_names pairs = commas_quote (map fst pairs);
   104 
   105     val illegal_vars =
   106       if null (term_vars set) andalso null (term_tvars set) then []
   107       else ["Illegal schematic variable(s) on rhs"];
   108 
   109     val dup_lhs_tfrees =
   110       (case duplicates lhs_tfrees of [] => []
   111       | dups => ["Duplicate type variables on lhs: " ^ show_names dups]);
   112 
   113     val extra_rhs_tfrees =
   114       (case gen_rems (op =) (rhs_tfrees, lhs_tfrees) of [] => []
   115       | extras => ["Extra type variables on rhs: " ^ show_names extras]);
   116 
   117     val illegal_frees =
   118       (case term_frees set of [] => []
   119       | xs => ["Illegal variables on rhs: " ^ show_names (map dest_Free xs)]);
   120 
   121     val errs = illegal_vars @ dup_lhs_tfrees @ extra_rhs_tfrees @ illegal_frees;
   122   in
   123     if null errs then ()
   124     else error (cat_lines errs);
   125 
   126     writeln("Proving nonemptiness of " ^ quote name ^ " ...");
   127     prove_nonempty cset (map (get_axiom thy) axms @ thms) usr_tac;
   128 
   129     thy
   130     |> PureThy.add_typedecls [(t, vs, mx)]
   131     |> Theory.add_arities_i
   132      [(full_name tname, replicate (length vs) HOLogic.termS, HOLogic.termS)]
   133     |> Theory.add_consts_i
   134      ((if no_def then [] else [(name, setT, NoSyn)]) @
   135       [(Rep_name, newT --> oldT, NoSyn),
   136        (Abs_name, oldT --> newT, NoSyn)])
   137     |> (if no_def then I else (PureThy.add_defs_i o map Attribute.none)
   138      [(name ^ "_def", Logic.mk_equals (setC, set))])
   139     |> (PureThy.add_axioms_i o map Attribute.none)
   140      [(Rep_name, rep_type),
   141       (Rep_name ^ "_inverse", rep_type_inv),
   142       (Abs_name ^ "_inverse", abs_type_inv)]
   143   end
   144   handle ERROR =>
   145     error ("The error(s) above occurred in typedef " ^ quote name);
   146 
   147 
   148 (* external interfaces *)
   149 
   150 fun read_term sg str =
   151   read_cterm sg (str, HOLogic.termTVar);
   152 
   153 fun cert_term sg tm =
   154   cterm_of sg tm handle TERM (msg, _) => error msg;
   155 
   156 val add_typedef = gen_add_typedef read_term false;
   157 val add_typedef_i = gen_add_typedef cert_term false;
   158 val add_typedef_i_no_def = gen_add_typedef cert_term true;
   159 
   160 
   161 end;