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