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