src/HOL/Tools/typedef_package.ML
author wenzelm
Fri Oct 12 12:06:54 2001 +0200 (2001-10-12)
changeset 11727 a27150cc8fa5
parent 11608 c760ea8154ee
child 11740 86ac4189a1c1
permissions -rw-r--r--
test: use SkipProof.make_thm instead of Thm.assume;
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@11608
    31
val type_definitionN = "Typedef.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@11426
    72
(* prove_nonempty -- tactical version *)        (*exception ERROR*)
wenzelm@4866
    73
wenzelm@11426
    74
fun prove_nonempty thy cset goal (witn_names, witn_thms, witn_tac) =
wenzelm@6383
    75
  let
wenzelm@6383
    76
    val is_def = Logic.is_equals o #prop o Thm.rep_thm;
wenzelm@6383
    77
    val thms = PureThy.get_thmss thy witn_names @ witn_thms;
wenzelm@4866
    78
    val tac =
wenzelm@11426
    79
      rtac exI 1 THEN
wenzelm@4866
    80
      TRY (rewrite_goals_tac (filter is_def thms)) THEN
wenzelm@4866
    81
      TRY (REPEAT_FIRST (resolve_tac (filter_out is_def thms))) THEN
wenzelm@6383
    82
      if_none witn_tac (TRY (ALLGOALS (CLASET' blast_tac)));
wenzelm@4866
    83
  in
wenzelm@6383
    84
    message ("Proving non-emptiness of set " ^ quote (string_of_cterm cset) ^ " ...");
wenzelm@11426
    85
    prove_goalw_cterm [] (cterm_of (sign_of thy) goal) (K [tac])
wenzelm@6383
    86
  end handle ERROR => error ("Failed to prove non-emptiness of " ^ quote (string_of_cterm cset));
wenzelm@4866
    87
wenzelm@4866
    88
wenzelm@6383
    89
(* prepare_typedef *)
wenzelm@6383
    90
wenzelm@6383
    91
fun read_term sg used s =
wenzelm@8100
    92
  #1 (Thm.read_def_cterm (sg, K None, K None) used true (s, HOLogic.termT));
wenzelm@4866
    93
wenzelm@6383
    94
fun cert_term sg _ t = Thm.cterm_of sg t handle TERM (msg, _) => error msg;
wenzelm@6383
    95
wenzelm@6383
    96
fun err_in_typedef name =
wenzelm@6383
    97
  error ("The error(s) above occurred in typedef " ^ quote name);
wenzelm@6383
    98
wenzelm@6383
    99
fun prepare_typedef prep_term no_def name (t, vs, mx) raw_set thy =
wenzelm@4866
   100
  let
wenzelm@11608
   101
    val _ = Theory.requires thy "Typedef" "typedefs";
wenzelm@6383
   102
    val sign = Theory.sign_of thy;
wenzelm@10280
   103
    val full = Sign.full_name sign;
wenzelm@4866
   104
wenzelm@4866
   105
    (*rhs*)
wenzelm@10280
   106
    val full_name = full name;
wenzelm@6383
   107
    val cset = prep_term sign vs raw_set;
wenzelm@6383
   108
    val {T = setT, t = set, ...} = Thm.rep_cterm cset;
wenzelm@4866
   109
    val rhs_tfrees = term_tfrees set;
wenzelm@4866
   110
    val oldT = HOLogic.dest_setT setT handle TYPE _ =>
wenzelm@4866
   111
      error ("Not a set type: " ^ quote (Sign.string_of_typ sign setT));
wenzelm@11426
   112
    fun mk_nonempty A =
wenzelm@11426
   113
      HOLogic.mk_Trueprop (HOLogic.mk_exists ("x", oldT, HOLogic.mk_mem (Free ("x", oldT), A)));
wenzelm@11426
   114
    val goal = mk_nonempty set;
wenzelm@11426
   115
    val goal_pat = mk_nonempty (Var ((name, 0), setT));
wenzelm@4866
   116
wenzelm@4866
   117
    (*lhs*)
wenzelm@6383
   118
    val lhs_tfrees = map (fn v => (v, if_none (assoc (rhs_tfrees, v)) HOLogic.termS)) vs;
wenzelm@4866
   119
    val tname = Syntax.type_name t mx;
wenzelm@10280
   120
    val full_tname = full tname;
wenzelm@10280
   121
    val newT = Type (full_tname, map TFree lhs_tfrees);
wenzelm@4866
   122
wenzelm@4866
   123
    val Rep_name = "Rep_" ^ name;
wenzelm@4866
   124
    val Abs_name = "Abs_" ^ name;
wenzelm@10280
   125
wenzelm@10280
   126
    val setC = Const (full_name, setT);
wenzelm@10280
   127
    val RepC = Const (full Rep_name, newT --> oldT);
wenzelm@10280
   128
    val AbsC = Const (full Abs_name, oldT --> newT);
wenzelm@4866
   129
    val x_new = Free ("x", newT);
wenzelm@4866
   130
    val y_old = Free ("y", oldT);
wenzelm@10280
   131
wenzelm@10615
   132
    val set' = if no_def then set else setC;
wenzelm@4866
   133
wenzelm@10280
   134
    val typedef_name = "type_definition_" ^ name;
wenzelm@10280
   135
    val typedefC =
wenzelm@10280
   136
      Const (type_definitionN, (newT --> oldT) --> (oldT --> newT) --> setT --> HOLogic.boolT);
wenzelm@11426
   137
    val typedef_prop =
wenzelm@11426
   138
      Logic.mk_implies (goal, HOLogic.mk_Trueprop (typedefC $ RepC $ AbsC $ set'));
wenzelm@4866
   139
wenzelm@11426
   140
    fun typedef_att (theory, nonempty) =
wenzelm@6383
   141
      theory
wenzelm@6383
   142
      |> add_typedecls [(t, vs, mx)]
wenzelm@6383
   143
      |> Theory.add_consts_i
wenzelm@6383
   144
       ((if no_def then [] else [(name, setT, NoSyn)]) @
wenzelm@6383
   145
        [(Rep_name, newT --> oldT, NoSyn),
wenzelm@6383
   146
         (Abs_name, oldT --> newT, NoSyn)])
wenzelm@9315
   147
      |> (if no_def then I else (#1 oo (PureThy.add_defs_i false o map Thm.no_attributes))
wenzelm@6383
   148
       [Logic.mk_defpair (setC, set)])
wenzelm@11426
   149
      |> PureThy.add_axioms_i [((typedef_name, typedef_prop),
wenzelm@11727
   150
          [apsnd (fn cond_axm => Drule.standard (nonempty RS cond_axm))])]
wenzelm@11426
   151
      |> (fn (theory', axm) =>
wenzelm@11727
   152
        let fun make th = Drule.standard (th OF axm) in
wenzelm@11426
   153
          rpair (hd axm) (theory'
wenzelm@10280
   154
          |> (#1 oo PureThy.add_thms)
wenzelm@10280
   155
            ([((Rep_name, make Rep), []),
wenzelm@10280
   156
              ((Rep_name ^ "_inverse", make Rep_inverse), []),
wenzelm@10615
   157
              ((Abs_name ^ "_inverse", make Abs_inverse), []),
wenzelm@10615
   158
              ((Rep_name ^ "_inject", make Rep_inject), []),
wenzelm@10280
   159
              ((Abs_name ^ "_inject", make Abs_inject), []),
wenzelm@10280
   160
              ((Rep_name ^ "_cases", make Rep_cases),
wenzelm@10280
   161
                [RuleCases.case_names [Rep_name], InductAttrib.cases_set_global full_name]),
wenzelm@10280
   162
              ((Abs_name ^ "_cases", make Abs_cases),
wenzelm@10280
   163
                [RuleCases.case_names [Abs_name], InductAttrib.cases_type_global full_tname]),
wenzelm@10280
   164
              ((Rep_name ^ "_induct", make Rep_induct),
wenzelm@10280
   165
                [RuleCases.case_names [Rep_name], InductAttrib.induct_set_global full_name]),
wenzelm@10280
   166
              ((Abs_name ^ "_induct", make Abs_induct),
wenzelm@10675
   167
                [RuleCases.case_names [Abs_name], InductAttrib.induct_type_global full_tname])]))
wenzelm@11426
   168
        end);
wenzelm@6383
   169
wenzelm@4866
   170
wenzelm@4866
   171
    (* errors *)
wenzelm@4866
   172
wenzelm@4866
   173
    fun show_names pairs = commas_quote (map fst pairs);
wenzelm@4866
   174
wenzelm@4866
   175
    val illegal_vars =
wenzelm@4866
   176
      if null (term_vars set) andalso null (term_tvars set) then []
wenzelm@4866
   177
      else ["Illegal schematic variable(s) on rhs"];
wenzelm@4866
   178
wenzelm@4866
   179
    val dup_lhs_tfrees =
wenzelm@4866
   180
      (case duplicates lhs_tfrees of [] => []
wenzelm@4866
   181
      | dups => ["Duplicate type variables on lhs: " ^ show_names dups]);
wenzelm@4866
   182
wenzelm@4866
   183
    val extra_rhs_tfrees =
wenzelm@4866
   184
      (case gen_rems (op =) (rhs_tfrees, lhs_tfrees) of [] => []
wenzelm@4866
   185
      | extras => ["Extra type variables on rhs: " ^ show_names extras]);
wenzelm@4866
   186
wenzelm@4866
   187
    val illegal_frees =
wenzelm@4866
   188
      (case term_frees set of [] => []
wenzelm@4866
   189
      | xs => ["Illegal variables on rhs: " ^ show_names (map dest_Free xs)]);
wenzelm@4866
   190
wenzelm@4866
   191
    val errs = illegal_vars @ dup_lhs_tfrees @ extra_rhs_tfrees @ illegal_frees;
wenzelm@11426
   192
    val _ = if null errs then () else error (cat_lines errs);
wenzelm@11426
   193
wenzelm@11426
   194
    (*test theory errors now!*)
wenzelm@11426
   195
    val test_thy = Theory.copy thy;
wenzelm@11727
   196
    val _ = (test_thy,
wenzelm@11727
   197
      setmp quick_and_dirty true (SkipProof.make_thm test_thy) goal) |> typedef_att;
wenzelm@11426
   198
wenzelm@11426
   199
  in (cset, goal, goal_pat, typedef_att) end
wenzelm@11426
   200
  handle ERROR => err_in_typedef name;
wenzelm@4866
   201
wenzelm@4866
   202
wenzelm@6383
   203
(* add_typedef interfaces *)
wenzelm@4866
   204
wenzelm@6383
   205
fun gen_add_typedef prep_term no_def name typ set names thms tac thy =
wenzelm@6383
   206
  let
wenzelm@11426
   207
    val (cset, goal, _, typedef_att) = prepare_typedef prep_term no_def name typ set thy;
wenzelm@11426
   208
    val result = prove_nonempty thy cset goal (names, thms, tac);
wenzelm@11426
   209
  in (thy, result) |> typedef_att |> #1 end;
wenzelm@4866
   210
berghofe@5180
   211
val add_typedef = gen_add_typedef read_term false;
berghofe@5180
   212
val add_typedef_i = gen_add_typedef cert_term false;
berghofe@5180
   213
val add_typedef_i_no_def = gen_add_typedef cert_term true;
wenzelm@4866
   214
wenzelm@4866
   215
wenzelm@6383
   216
(* typedef_proof interface *)
wenzelm@6383
   217
wenzelm@6729
   218
fun gen_typedef_proof prep_term ((name, typ, set), comment) int thy =
wenzelm@11426
   219
  let val (_, goal, goal_pat, att) = prepare_typedef prep_term false name typ set thy;
wenzelm@11426
   220
  in thy |> IsarThy.theorem_i ((("", [att]), (goal, ([goal_pat], []))), comment) int end;
wenzelm@6383
   221
wenzelm@6383
   222
val typedef_proof = gen_typedef_proof read_term;
wenzelm@6383
   223
val typedef_proof_i = gen_typedef_proof cert_term;
wenzelm@6357
   224
wenzelm@6357
   225
wenzelm@6383
   226
wenzelm@6383
   227
(** outer syntax **)
wenzelm@6383
   228
wenzelm@6723
   229
local structure P = OuterParse and K = OuterSyntax.Keyword in
wenzelm@6383
   230
wenzelm@6357
   231
val typedeclP =
wenzelm@6723
   232
  OuterSyntax.command "typedecl" "HOL type declaration" K.thy_decl
wenzelm@7152
   233
    (P.type_args -- P.name -- P.opt_infix --| P.marg_comment >> (fn ((vs, t), mx) =>
wenzelm@6357
   234
      Toplevel.theory (add_typedecls [(t, vs, mx)])));
wenzelm@6357
   235
wenzelm@6723
   236
wenzelm@6383
   237
val typedef_proof_decl =
wenzelm@6723
   238
  Scan.option (P.$$$ "(" |-- P.name --| P.$$$ ")") --
wenzelm@6729
   239
    (P.type_args -- P.name) -- P.opt_infix -- (P.$$$ "=" |-- P.term) -- P.marg_comment;
wenzelm@6357
   240
wenzelm@6729
   241
fun mk_typedef_proof ((((opt_name, (vs, t)), mx), A), comment) =
wenzelm@6729
   242
  typedef_proof ((if_none opt_name (Syntax.type_name t mx), (t, vs, mx), A), comment);
wenzelm@6357
   243
wenzelm@6357
   244
val typedefP =
wenzelm@6723
   245
  OuterSyntax.command "typedef" "HOL type definition (requires non-emptiness proof)" K.thy_goal
wenzelm@6383
   246
    (typedef_proof_decl >> (Toplevel.print oo (Toplevel.theory_to_proof o mk_typedef_proof)));
wenzelm@6357
   247
wenzelm@6723
   248
wenzelm@6357
   249
val _ = OuterSyntax.add_parsers [typedeclP, typedefP];
wenzelm@6357
   250
wenzelm@4866
   251
end;
wenzelm@6383
   252
wenzelm@6383
   253
wenzelm@6383
   254
end;