src/HOL/Tools/typedef_package.ML
author kleing
Mon Jun 21 10:25:57 2004 +0200 (2004-06-21)
changeset 14981 e73f8140af78
parent 14819 4dae1cb304a4
child 15259 6aa593317905
permissions -rw-r--r--
Merged in license change from Isabelle2004
wenzelm@4866
     1
(*  Title:      HOL/Tools/typedef_package.ML
wenzelm@4866
     2
    ID:         $Id$
wenzelm@4866
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@4866
     4
wenzelm@6357
     5
Gordon/HOL-style type definitions.
wenzelm@4866
     6
*)
wenzelm@4866
     7
wenzelm@4866
     8
signature TYPEDEF_PACKAGE =
wenzelm@4866
     9
sig
wenzelm@5697
    10
  val quiet_mode: bool ref
wenzelm@5104
    11
  val add_typedecls: (bstring * string list * mixfix) list -> theory -> theory
wenzelm@11822
    12
  val add_typedef_x: string -> bstring * string list * mixfix ->
wenzelm@4866
    13
    string -> string list -> thm list -> tactic option -> theory -> theory
wenzelm@11827
    14
  val add_typedef: bool -> string option -> bstring * string list * mixfix ->
wenzelm@11822
    15
    string -> (bstring * bstring) option -> tactic -> theory -> theory *
wenzelm@11822
    16
    {type_definition: thm, set_def: thm option, Rep: thm, Rep_inverse: thm,
wenzelm@11822
    17
      Abs_inverse: thm, Rep_inject: thm, Abs_inject: thm, Rep_cases: thm, Abs_cases: thm,
wenzelm@11822
    18
      Rep_induct: thm, Abs_induct: thm}
wenzelm@11827
    19
  val add_typedef_i: bool -> string option -> bstring * string list * mixfix ->
wenzelm@11822
    20
    term -> (bstring * bstring) option -> tactic -> theory -> theory *
wenzelm@11822
    21
    {type_definition: thm, set_def: thm option, Rep: thm, Rep_inverse: thm,
wenzelm@11822
    22
      Abs_inverse: thm, Rep_inject: thm, Abs_inject: thm, Rep_cases: thm, Abs_cases: thm,
wenzelm@11822
    23
      Rep_induct: thm, Abs_induct: thm}
wenzelm@13443
    24
  val typedef_proof: (bool * string) * (bstring * string list * mixfix) * string
wenzelm@13443
    25
    * (string * string) option -> bool -> theory -> ProofHistory.T
wenzelm@13443
    26
  val typedef_proof_i: (bool * string) * (bstring * string list * mixfix) * term
wenzelm@13443
    27
    * (string * string) option -> bool -> theory -> ProofHistory.T
wenzelm@4866
    28
end;
wenzelm@4866
    29
wenzelm@4866
    30
structure TypedefPackage: TYPEDEF_PACKAGE =
wenzelm@4866
    31
struct
wenzelm@4866
    32
wenzelm@4866
    33
wenzelm@10280
    34
(** theory context references **)
wenzelm@10280
    35
wenzelm@11608
    36
val type_definitionN = "Typedef.type_definition";
wenzelm@10280
    37
wenzelm@13413
    38
val Rep = thm "type_definition.Rep";
wenzelm@13413
    39
val Rep_inverse = thm "type_definition.Rep_inverse";
wenzelm@13413
    40
val Abs_inverse = thm "type_definition.Abs_inverse";
wenzelm@13413
    41
val Rep_inject = thm "type_definition.Rep_inject";
wenzelm@13413
    42
val Abs_inject = thm "type_definition.Abs_inject";
wenzelm@13413
    43
val Rep_cases = thm "type_definition.Rep_cases";
wenzelm@13413
    44
val Abs_cases = thm "type_definition.Abs_cases";
wenzelm@13413
    45
val Rep_induct = thm "type_definition.Rep_induct";
wenzelm@13413
    46
val Abs_induct = thm "type_definition.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@12338
    57
      (full (Syntax.type_name raw_name mx), replicate (length args) HOLogic.typeS, HOLogic.typeS);
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@11827
    78
fun prove_nonempty thy cset goal (witn1_tac, witn_names, witn_thms, witn2_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@11827
    83
      witn1_tac 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@11827
    86
      if_none witn2_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@11968
    89
    Tactic.prove (Theory.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@12338
    96
  #1 (Thm.read_def_cterm (sg, K None, K None) used true (s, HOLogic.typeT));
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@11822
   103
fun prepare_typedef prep_term 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@14819
   120
      |> apfst implode |> apsnd (#1 o Library.read_int);
wenzelm@11744
   121
    val goal_pat = mk_nonempty (Var (vname, setT));
wenzelm@4866
   122
wenzelm@4866
   123
    (*lhs*)
wenzelm@12338
   124
    val lhs_tfrees = map (fn v => (v, if_none (assoc (rhs_tfrees, v)) HOLogic.typeS)) 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@11822
   136
    val set' = if def then setC else set;
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@11822
   144
    fun typedef_result (theory, nonempty) =
wenzelm@6383
   145
      theory
wenzelm@6383
   146
      |> add_typedecls [(t, vs, mx)]
wenzelm@6383
   147
      |> Theory.add_consts_i
wenzelm@11822
   148
       ((if def then [(name, setT, NoSyn)] else []) @
wenzelm@6383
   149
        [(Rep_name, newT --> oldT, NoSyn),
wenzelm@6383
   150
         (Abs_name, oldT --> newT, NoSyn)])
wenzelm@11822
   151
      |> (if def then (apsnd (Some o hd) oo (PureThy.add_defs_i false o map Thm.no_attributes))
wenzelm@11822
   152
           [Logic.mk_defpair (setC, set)]
wenzelm@11822
   153
          else rpair None)
wenzelm@14570
   154
      |>>> PureThy.add_axioms_i [((typedef_name, typedef_prop),
wenzelm@11727
   155
          [apsnd (fn cond_axm => Drule.standard (nonempty RS cond_axm))])]
wenzelm@14570
   156
      |>> Theory.add_finals_i false [RepC, AbsC]
wenzelm@14570
   157
      |> (fn (theory', (set_def, [type_definition])) =>
wenzelm@11822
   158
        let
wenzelm@11822
   159
          fun make th = Drule.standard (th OF [type_definition]);
wenzelm@11822
   160
          val (theory'', [Rep, Rep_inverse, Abs_inverse, Rep_inject, Abs_inject,
wenzelm@11822
   161
              Rep_cases, Abs_cases, Rep_induct, Abs_induct]) =
wenzelm@12338
   162
            theory'
wenzelm@12338
   163
            |> Theory.add_path name
wenzelm@12338
   164
            |> PureThy.add_thms
wenzelm@11822
   165
              ([((Rep_name, make Rep), []),
wenzelm@11822
   166
                ((Rep_name ^ "_inverse", make Rep_inverse), []),
wenzelm@11822
   167
                ((Abs_name ^ "_inverse", make Abs_inverse), []),
wenzelm@11822
   168
                ((Rep_name ^ "_inject", make Rep_inject), []),
wenzelm@11822
   169
                ((Abs_name ^ "_inject", make Abs_inject), []),
wenzelm@11822
   170
                ((Rep_name ^ "_cases", make Rep_cases),
wenzelm@11822
   171
                  [RuleCases.case_names [Rep_name], InductAttrib.cases_set_global full_name]),
wenzelm@11822
   172
                ((Abs_name ^ "_cases", make Abs_cases),
wenzelm@11822
   173
                  [RuleCases.case_names [Abs_name], InductAttrib.cases_type_global full_tname]),
wenzelm@11822
   174
                ((Rep_name ^ "_induct", make Rep_induct),
wenzelm@11822
   175
                  [RuleCases.case_names [Rep_name], InductAttrib.induct_set_global full_name]),
wenzelm@11822
   176
                ((Abs_name ^ "_induct", make Abs_induct),
wenzelm@12338
   177
                  [RuleCases.case_names [Abs_name], InductAttrib.induct_type_global full_tname])])
wenzelm@12338
   178
            |>> Theory.parent_path;
wenzelm@11822
   179
          val result = {type_definition = type_definition, set_def = set_def,
wenzelm@11822
   180
            Rep = Rep, Rep_inverse = Rep_inverse, Abs_inverse = Abs_inverse,
wenzelm@11822
   181
            Rep_inject = Rep_inject, Abs_inject = Abs_inject, Rep_cases = Rep_cases,
wenzelm@11822
   182
            Abs_cases = Abs_cases, Rep_induct = Rep_induct, Abs_induct = Abs_induct};
wenzelm@11822
   183
        in ((theory'', type_definition), result) end);
wenzelm@6383
   184
wenzelm@4866
   185
wenzelm@4866
   186
    (* errors *)
wenzelm@4866
   187
wenzelm@4866
   188
    fun show_names pairs = commas_quote (map fst pairs);
wenzelm@4866
   189
wenzelm@4866
   190
    val illegal_vars =
wenzelm@4866
   191
      if null (term_vars set) andalso null (term_tvars set) then []
wenzelm@4866
   192
      else ["Illegal schematic variable(s) on rhs"];
wenzelm@4866
   193
wenzelm@4866
   194
    val dup_lhs_tfrees =
wenzelm@4866
   195
      (case duplicates lhs_tfrees of [] => []
wenzelm@4866
   196
      | dups => ["Duplicate type variables on lhs: " ^ show_names dups]);
wenzelm@4866
   197
wenzelm@4866
   198
    val extra_rhs_tfrees =
wenzelm@4866
   199
      (case gen_rems (op =) (rhs_tfrees, lhs_tfrees) of [] => []
wenzelm@4866
   200
      | extras => ["Extra type variables on rhs: " ^ show_names extras]);
wenzelm@4866
   201
wenzelm@4866
   202
    val illegal_frees =
wenzelm@4866
   203
      (case term_frees set of [] => []
wenzelm@4866
   204
      | xs => ["Illegal variables on rhs: " ^ show_names (map dest_Free xs)]);
wenzelm@4866
   205
wenzelm@4866
   206
    val errs = illegal_vars @ dup_lhs_tfrees @ extra_rhs_tfrees @ illegal_frees;
wenzelm@11426
   207
    val _ = if null errs then () else error (cat_lines errs);
wenzelm@11426
   208
wenzelm@11426
   209
    (*test theory errors now!*)
wenzelm@11426
   210
    val test_thy = Theory.copy thy;
wenzelm@11727
   211
    val _ = (test_thy,
wenzelm@11822
   212
      setmp quick_and_dirty true (SkipProof.make_thm test_thy) goal) |> typedef_result;
wenzelm@11426
   213
wenzelm@11822
   214
  in (cset, goal, goal_pat, typedef_result) end
wenzelm@11426
   215
  handle ERROR => err_in_typedef name;
wenzelm@4866
   216
wenzelm@4866
   217
wenzelm@6383
   218
(* add_typedef interfaces *)
wenzelm@4866
   219
wenzelm@11827
   220
fun gen_typedef prep_term def name typ set opt_morphs tac1 names thms tac2 thy =
wenzelm@6383
   221
  let
wenzelm@11822
   222
    val (cset, goal, _, typedef_result) =
wenzelm@11822
   223
      prepare_typedef prep_term def name typ set opt_morphs thy;
wenzelm@11827
   224
    val non_empty = prove_nonempty thy cset goal (tac1, names, thms, tac2);
wenzelm@11822
   225
    val ((thy', _), result) = (thy, non_empty) |> typedef_result;
wenzelm@11822
   226
  in (thy', result) end;
wenzelm@4866
   227
wenzelm@11827
   228
fun sane_typedef prep_term def opt_name typ set opt_morphs tac =
wenzelm@11827
   229
  gen_typedef prep_term def
wenzelm@11827
   230
    (if_none opt_name (#1 typ)) typ set opt_morphs all_tac [] [] (Some tac);
wenzelm@11822
   231
wenzelm@11822
   232
fun add_typedef_x name typ set names thms tac =
wenzelm@11827
   233
  #1 o gen_typedef read_term true name typ set None (Tactic.rtac exI 1) names thms tac;
wenzelm@11822
   234
wenzelm@11822
   235
val add_typedef = sane_typedef read_term;
wenzelm@11822
   236
val add_typedef_i = sane_typedef cert_term;
wenzelm@4866
   237
wenzelm@4866
   238
wenzelm@6383
   239
(* typedef_proof interface *)
wenzelm@6383
   240
wenzelm@13443
   241
fun gen_typedef_proof prep_term ((def, name), typ, set, opt_morphs) int thy =
wenzelm@11822
   242
  let
wenzelm@11822
   243
    val (_, goal, goal_pat, att_result) =
wenzelm@13443
   244
      prepare_typedef prep_term def name typ set opt_morphs thy;
wenzelm@11822
   245
    val att = #1 o att_result;
wenzelm@12876
   246
  in thy |> IsarThy.theorem_i Drule.internalK (("", [att]), (goal, ([goal_pat], []))) int end;
wenzelm@6383
   247
wenzelm@6383
   248
val typedef_proof = gen_typedef_proof read_term;
wenzelm@6383
   249
val typedef_proof_i = gen_typedef_proof cert_term;
wenzelm@6357
   250
wenzelm@6357
   251
wenzelm@6383
   252
wenzelm@6383
   253
(** outer syntax **)
wenzelm@6383
   254
wenzelm@6723
   255
local structure P = OuterParse and K = OuterSyntax.Keyword in
wenzelm@6383
   256
wenzelm@6357
   257
val typedeclP =
wenzelm@12624
   258
  OuterSyntax.command "typedecl" "type declaration (HOL)" K.thy_decl
wenzelm@12876
   259
    (P.type_args -- P.name -- P.opt_infix >> (fn ((vs, t), mx) =>
wenzelm@6357
   260
      Toplevel.theory (add_typedecls [(t, vs, mx)])));
wenzelm@6357
   261
wenzelm@6723
   262
wenzelm@6383
   263
val typedef_proof_decl =
wenzelm@13443
   264
  Scan.optional (P.$$$ "(" |-- P.!!!
wenzelm@13443
   265
      (((P.$$$ "open" >> K false) -- Scan.option P.name || P.name >> (fn s => (true, Some s)))
wenzelm@13443
   266
        --| P.$$$ ")")) (true, None) --
wenzelm@11744
   267
    (P.type_args -- P.name) -- P.opt_infix -- (P.$$$ "=" |-- P.term) --
wenzelm@12876
   268
    Scan.option (P.$$$ "morphisms" |-- P.!!! (P.name -- P.name));
wenzelm@6357
   269
wenzelm@13443
   270
fun mk_typedef_proof ((((((def, opt_name), (vs, t)), mx), A), morphs)) =
wenzelm@13443
   271
  typedef_proof ((def, if_none opt_name (Syntax.type_name t mx)), (t, vs, mx), A, morphs);
wenzelm@6357
   272
wenzelm@6357
   273
val typedefP =
wenzelm@6723
   274
  OuterSyntax.command "typedef" "HOL type definition (requires non-emptiness proof)" K.thy_goal
wenzelm@6383
   275
    (typedef_proof_decl >> (Toplevel.print oo (Toplevel.theory_to_proof o mk_typedef_proof)));
wenzelm@6357
   276
wenzelm@6723
   277
wenzelm@11744
   278
val _ = OuterSyntax.add_keywords ["morphisms"];
wenzelm@6357
   279
val _ = OuterSyntax.add_parsers [typedeclP, typedefP];
wenzelm@6357
   280
wenzelm@4866
   281
end;
wenzelm@6383
   282
wenzelm@6383
   283
end;