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