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