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