src/HOL/Tools/typedef_package.ML
author wenzelm
Thu Aug 03 17:30:36 2006 +0200 (2006-08-03)
changeset 20328 5b240a4216b0
parent 20046 9c8909fc5865
child 20357 5fb92bd3aaea
permissions -rw-r--r--
RuleInsts.bires_inst_tac;
wenzelm@4866
     1
(*  Title:      HOL/Tools/typedef_package.ML
wenzelm@4866
     2
    ID:         $Id$
wenzelm@16458
     3
    Author:     Markus Wenzel and Stefan Berghofer, 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@19705
    12
  type info =
wenzelm@19705
    13
   {rep_type: typ, abs_type: typ, Rep_name: string, Abs_name: string,
wenzelm@19705
    14
    type_definition: thm, set_def: thm option, Rep: thm, Rep_inverse: thm,
wenzelm@19705
    15
    Abs_inverse: thm, Rep_inject: thm, Abs_inject: thm, Rep_cases: thm,
wenzelm@19705
    16
    Abs_cases: thm, Rep_induct: thm, Abs_induct: thm};
wenzelm@19705
    17
  val get_info: theory -> string -> info option
wenzelm@11827
    18
  val add_typedef: bool -> string option -> bstring * string list * mixfix ->
wenzelm@19705
    19
    string -> (bstring * bstring) option -> tactic -> theory -> info * theory
wenzelm@11827
    20
  val add_typedef_i: bool -> string option -> bstring * string list * mixfix ->
wenzelm@19705
    21
    term -> (bstring * bstring) option -> tactic -> theory -> info * theory
wenzelm@17339
    22
  val typedef: (bool * string) * (bstring * string list * mixfix) * string
wenzelm@17339
    23
    * (string * string) option -> theory -> Proof.state
wenzelm@17339
    24
  val typedef_i: (bool * string) * (bstring * string list * mixfix) * term
wenzelm@17339
    25
    * (string * string) option -> theory -> Proof.state
wenzelm@18708
    26
  val setup: theory -> theory
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
wenzelm@13413
    37
val Rep = thm "type_definition.Rep";
wenzelm@13413
    38
val Rep_inverse = thm "type_definition.Rep_inverse";
wenzelm@13413
    39
val Abs_inverse = thm "type_definition.Abs_inverse";
wenzelm@13413
    40
val Rep_inject = thm "type_definition.Rep_inject";
wenzelm@13413
    41
val Abs_inject = thm "type_definition.Abs_inject";
wenzelm@13413
    42
val Rep_cases = thm "type_definition.Rep_cases";
wenzelm@13413
    43
val Abs_cases = thm "type_definition.Abs_cases";
wenzelm@13413
    44
val Rep_induct = thm "type_definition.Rep_induct";
wenzelm@13413
    45
val Abs_induct = thm "type_definition.Abs_induct";
wenzelm@10280
    46
wenzelm@10280
    47
wenzelm@10280
    48
wenzelm@17922
    49
(** type declarations **)
wenzelm@17922
    50
wenzelm@19510
    51
fun HOL_arity (raw_name, args, mx) thy =
wenzelm@19510
    52
  thy |> AxClass.axiomatize_arity_i
wenzelm@19510
    53
    (Sign.full_name thy (Syntax.type_name raw_name mx),
wenzelm@19510
    54
      replicate (length args) HOLogic.typeS, HOLogic.typeS);
wenzelm@19510
    55
wenzelm@17922
    56
fun add_typedecls decls thy =
wenzelm@19510
    57
  thy
wenzelm@19510
    58
  |> Theory.add_typedecls decls
wenzelm@19510
    59
  |> can (Theory.assert_super HOL.thy) ? fold HOL_arity decls;
wenzelm@17922
    60
wenzelm@17922
    61
wenzelm@17922
    62
wenzelm@17922
    63
(** type definitions **)
wenzelm@17922
    64
wenzelm@17922
    65
(* messages *)
wenzelm@17922
    66
wenzelm@17922
    67
val quiet_mode = ref false;
wenzelm@17922
    68
fun message s = if ! quiet_mode then () else writeln s;
wenzelm@17922
    69
wenzelm@17922
    70
wenzelm@17922
    71
(* theory data *)
berghofe@15259
    72
wenzelm@19705
    73
type info =
wenzelm@19705
    74
 {rep_type: typ, abs_type: typ, Rep_name: string, Abs_name: string,
wenzelm@19705
    75
  type_definition: thm, set_def: thm option, Rep: thm, Rep_inverse: thm,
wenzelm@19705
    76
  Abs_inverse: thm, Rep_inject: thm, Abs_inject: thm, Rep_cases: thm,
wenzelm@19705
    77
  Abs_cases: thm, Rep_induct: thm, Abs_induct: thm};
haftmann@19459
    78
wenzelm@16458
    79
structure TypedefData = TheoryDataFun
wenzelm@16458
    80
(struct
berghofe@15259
    81
  val name = "HOL/typedef";
wenzelm@19705
    82
  type T = info Symtab.table;
berghofe@15259
    83
  val empty = Symtab.empty;
berghofe@15259
    84
  val copy = I;
wenzelm@16458
    85
  val extend = I;
wenzelm@19617
    86
  fun merge _ tabs : T = Symtab.merge (K true) tabs;
wenzelm@16458
    87
  fun print _ _ = ();
wenzelm@16458
    88
end);
berghofe@15259
    89
wenzelm@19705
    90
val get_info = Symtab.lookup o TypedefData.get;
wenzelm@19705
    91
fun put_info name info = TypedefData.map (Symtab.update (name, info));
berghofe@15259
    92
berghofe@15259
    93
wenzelm@6383
    94
(* prepare_typedef *)
wenzelm@6383
    95
wenzelm@16458
    96
fun read_term thy used s =
wenzelm@16458
    97
  #1 (Thm.read_def_cterm (thy, K NONE, K NONE) used true (s, HOLogic.typeT));
wenzelm@4866
    98
wenzelm@16458
    99
fun cert_term thy _ t = Thm.cterm_of thy t handle TERM (msg, _) => error msg;
wenzelm@6383
   100
wenzelm@18678
   101
fun err_in_typedef msg name =
wenzelm@18678
   102
  cat_error msg ("The error(s) above occurred in typedef " ^ quote name);
wenzelm@6383
   103
wenzelm@11822
   104
fun prepare_typedef prep_term def name (t, vs, mx) raw_set opt_morphs thy =
wenzelm@4866
   105
  let
wenzelm@11608
   106
    val _ = Theory.requires thy "Typedef" "typedefs";
wenzelm@16458
   107
    val full = Sign.full_name thy;
wenzelm@4866
   108
wenzelm@4866
   109
    (*rhs*)
wenzelm@10280
   110
    val full_name = full name;
wenzelm@16458
   111
    val cset = prep_term thy vs raw_set;
wenzelm@6383
   112
    val {T = setT, t = set, ...} = Thm.rep_cterm cset;
wenzelm@17280
   113
    val rhs_tfrees = Term.add_tfrees set [];
wenzelm@17280
   114
    val rhs_tfreesT = Term.add_tfreesT setT [];
wenzelm@4866
   115
    val oldT = HOLogic.dest_setT setT handle TYPE _ =>
wenzelm@16458
   116
      error ("Not a set type: " ^ quote (Sign.string_of_typ thy setT));
wenzelm@11426
   117
    fun mk_nonempty A =
wenzelm@11426
   118
      HOLogic.mk_Trueprop (HOLogic.mk_exists ("x", oldT, HOLogic.mk_mem (Free ("x", oldT), A)));
wenzelm@11426
   119
    val goal = mk_nonempty set;
wenzelm@19473
   120
    val goal_pat = mk_nonempty (Var (the_default (name, 0) (Syntax.read_variable name), setT));
wenzelm@4866
   121
wenzelm@4866
   122
    (*lhs*)
wenzelm@17280
   123
    val defS = Sign.defaultS thy;
wenzelm@19473
   124
    val lhs_tfrees = map (fn v => (v, the_default defS (AList.lookup (op =) rhs_tfrees v))) vs;
wenzelm@17280
   125
    val args_setT = lhs_tfrees
wenzelm@17280
   126
      |> filter (member (op =) rhs_tfrees andf (not o member (op =) rhs_tfreesT))
wenzelm@17280
   127
      |> map TFree;
wenzelm@17280
   128
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@19473
   133
    val (Rep_name, Abs_name) = the_default ("Rep_" ^ name, "Abs_" ^ name) opt_morphs;
wenzelm@19391
   134
    val setT' = map Term.itselfT args_setT ---> setT;
wenzelm@17280
   135
    val setC = Term.list_comb (Const (full_name, setT'), map Logic.mk_type args_setT);
wenzelm@10280
   136
    val RepC = Const (full Rep_name, newT --> oldT);
wenzelm@10280
   137
    val AbsC = Const (full Abs_name, oldT --> newT);
wenzelm@4866
   138
    val x_new = Free ("x", newT);
wenzelm@4866
   139
    val y_old = Free ("y", oldT);
wenzelm@10280
   140
wenzelm@11822
   141
    val set' = if def then setC else set;
wenzelm@4866
   142
wenzelm@10280
   143
    val typedef_name = "type_definition_" ^ name;
wenzelm@10280
   144
    val typedefC =
wenzelm@10280
   145
      Const (type_definitionN, (newT --> oldT) --> (oldT --> newT) --> setT --> HOLogic.boolT);
wenzelm@11426
   146
    val typedef_prop =
wenzelm@11426
   147
      Logic.mk_implies (goal, HOLogic.mk_Trueprop (typedefC $ RepC $ AbsC $ set'));
wenzelm@19705
   148
    val typedef_deps = Term.fold_aterms (fn Const c => insert (op =) c | _ => I) set' [];
wenzelm@4866
   149
wenzelm@19705
   150
    fun add_def eq thy =
wenzelm@19705
   151
      if def then
haftmann@18358
   152
        thy
wenzelm@19705
   153
        |> PureThy.add_defs_i false [Thm.no_attributes eq]
wenzelm@19705
   154
        |-> (fn [th] => pair (SOME th))
wenzelm@19705
   155
      else (NONE, thy);
haftmann@18358
   156
haftmann@19342
   157
    fun typedef_result nonempty context =
wenzelm@18728
   158
      Context.the_theory context
wenzelm@6383
   159
      |> add_typedecls [(t, vs, mx)]
wenzelm@6383
   160
      |> Theory.add_consts_i
wenzelm@17280
   161
       ((if def then [(name, setT', NoSyn)] else []) @
wenzelm@6383
   162
        [(Rep_name, newT --> oldT, NoSyn),
wenzelm@6383
   163
         (Abs_name, oldT --> newT, NoSyn)])
wenzelm@19705
   164
      |> add_def (Logic.mk_defpair (setC, set))
haftmann@18377
   165
      ||>> PureThy.add_axioms_i [((typedef_name, typedef_prop),
wenzelm@20046
   166
          [apsnd (fn cond_axm => nonempty RS cond_axm)])]
wenzelm@19705
   167
      ||> Theory.add_deps "" (dest_Const RepC) typedef_deps
wenzelm@19705
   168
      ||> Theory.add_deps "" (dest_Const AbsC) typedef_deps
wenzelm@19705
   169
      |-> (fn (set_def, [type_definition]) => fn thy1 =>
wenzelm@11822
   170
        let
wenzelm@11822
   171
          fun make th = Drule.standard (th OF [type_definition]);
haftmann@19342
   172
          val abs_inject = make Abs_inject;
haftmann@19342
   173
          val abs_inverse = make Abs_inverse;
haftmann@18377
   174
          val ([Rep, Rep_inverse, Abs_inverse, Rep_inject, Abs_inject,
wenzelm@19705
   175
              Rep_cases, Abs_cases, Rep_induct, Abs_induct], thy2) =
wenzelm@19705
   176
            thy1
wenzelm@12338
   177
            |> Theory.add_path name
wenzelm@12338
   178
            |> PureThy.add_thms
wenzelm@11822
   179
              ([((Rep_name, make Rep), []),
wenzelm@11822
   180
                ((Rep_name ^ "_inverse", make Rep_inverse), []),
haftmann@19342
   181
                ((Abs_name ^ "_inverse", abs_inverse), []),
wenzelm@11822
   182
                ((Rep_name ^ "_inject", make Rep_inject), []),
haftmann@19342
   183
                ((Abs_name ^ "_inject", abs_inject), []),
wenzelm@11822
   184
                ((Rep_name ^ "_cases", make Rep_cases),
wenzelm@18728
   185
                  [RuleCases.case_names [Rep_name], InductAttrib.cases_set full_name]),
wenzelm@11822
   186
                ((Abs_name ^ "_cases", make Abs_cases),
wenzelm@18728
   187
                  [RuleCases.case_names [Abs_name], InductAttrib.cases_type full_tname]),
wenzelm@11822
   188
                ((Rep_name ^ "_induct", make Rep_induct),
wenzelm@18728
   189
                  [RuleCases.case_names [Rep_name], InductAttrib.induct_set full_name]),
wenzelm@11822
   190
                ((Abs_name ^ "_induct", make Abs_induct),
wenzelm@18728
   191
                  [RuleCases.case_names [Abs_name], InductAttrib.induct_type full_tname])])
wenzelm@19705
   192
            ||> Theory.parent_path;
wenzelm@19705
   193
          val info = {rep_type = oldT, abs_type = newT,
wenzelm@19705
   194
            Rep_name = full Rep_name, Abs_name = full Abs_name,
wenzelm@19705
   195
              type_definition = type_definition, set_def = set_def,
wenzelm@19705
   196
              Rep = Rep, Rep_inverse = Rep_inverse, Abs_inverse = Abs_inverse,
wenzelm@19705
   197
              Rep_inject = Rep_inject, Abs_inject = Abs_inject, Rep_cases = Rep_cases,
wenzelm@11822
   198
            Abs_cases = Abs_cases, Rep_induct = Rep_induct, Abs_induct = Abs_induct};
wenzelm@19705
   199
          val thy3 = thy2 |> put_info full_tname info;
wenzelm@19705
   200
        in (info, Context.Theory thy3) end);
wenzelm@6383
   201
wenzelm@4866
   202
wenzelm@4866
   203
    (* errors *)
wenzelm@4866
   204
wenzelm@4866
   205
    fun show_names pairs = commas_quote (map fst pairs);
wenzelm@4866
   206
wenzelm@4866
   207
    val illegal_vars =
wenzelm@4866
   208
      if null (term_vars set) andalso null (term_tvars set) then []
wenzelm@4866
   209
      else ["Illegal schematic variable(s) on rhs"];
wenzelm@4866
   210
wenzelm@4866
   211
    val dup_lhs_tfrees =
wenzelm@18964
   212
      (case duplicates (op =) lhs_tfrees of [] => []
wenzelm@4866
   213
      | dups => ["Duplicate type variables on lhs: " ^ show_names dups]);
wenzelm@4866
   214
wenzelm@4866
   215
    val extra_rhs_tfrees =
wenzelm@17280
   216
      (case fold (remove (op =)) lhs_tfrees rhs_tfrees of [] => []
wenzelm@4866
   217
      | extras => ["Extra type variables on rhs: " ^ show_names extras]);
wenzelm@4866
   218
wenzelm@4866
   219
    val illegal_frees =
wenzelm@4866
   220
      (case term_frees set of [] => []
wenzelm@4866
   221
      | xs => ["Illegal variables on rhs: " ^ show_names (map dest_Free xs)]);
wenzelm@4866
   222
wenzelm@4866
   223
    val errs = illegal_vars @ dup_lhs_tfrees @ extra_rhs_tfrees @ illegal_frees;
wenzelm@11426
   224
    val _ = if null errs then () else error (cat_lines errs);
wenzelm@11426
   225
wenzelm@11426
   226
    (*test theory errors now!*)
wenzelm@11426
   227
    val test_thy = Theory.copy thy;
wenzelm@19705
   228
    val _ =
haftmann@19342
   229
      Context.Theory test_thy
haftmann@19342
   230
      |> typedef_result (setmp quick_and_dirty true (SkipProof.make_thm test_thy) goal);
wenzelm@11426
   231
wenzelm@11822
   232
  in (cset, goal, goal_pat, typedef_result) end
wenzelm@18678
   233
  handle ERROR msg => err_in_typedef msg name;
wenzelm@4866
   234
wenzelm@4866
   235
wenzelm@6383
   236
(* add_typedef interfaces *)
wenzelm@4866
   237
wenzelm@17339
   238
local
wenzelm@17339
   239
wenzelm@17922
   240
fun gen_typedef prep_term def opt_name typ set opt_morphs tac thy =
wenzelm@6383
   241
  let
wenzelm@17922
   242
    val name = the_default (#1 typ) opt_name;
wenzelm@11822
   243
    val (cset, goal, _, typedef_result) =
wenzelm@11822
   244
      prepare_typedef prep_term def name typ set opt_morphs thy;
wenzelm@17922
   245
    val _ = message ("Proving non-emptiness of set " ^ quote (string_of_cterm cset) ^ " ...");
wenzelm@20046
   246
    val non_empty = Goal.prove_global thy [] [] goal (K tac) handle ERROR msg =>
wenzelm@18678
   247
      cat_error msg ("Failed to prove non-emptiness of " ^ quote (string_of_cterm cset));
haftmann@19342
   248
  in
haftmann@19342
   249
    Context.Theory thy
haftmann@19342
   250
    |> typedef_result non_empty
haftmann@19342
   251
    ||> Context.the_theory
haftmann@19342
   252
  end;
wenzelm@4866
   253
wenzelm@17339
   254
in
wenzelm@17339
   255
wenzelm@17922
   256
val add_typedef = gen_typedef read_term;
wenzelm@17922
   257
val add_typedef_i = gen_typedef cert_term;
wenzelm@4866
   258
wenzelm@17339
   259
end;
wenzelm@4866
   260
wenzelm@17339
   261
wenzelm@17339
   262
(* Isar typedef interface *)
wenzelm@6383
   263
wenzelm@17339
   264
local
wenzelm@17339
   265
wenzelm@17339
   266
fun gen_typedef prep_term ((def, name), typ, set, opt_morphs) thy =
wenzelm@11822
   267
  let
wenzelm@19705
   268
    val (_, goal, goal_pat, typedef_result) =
wenzelm@13443
   269
      prepare_typedef prep_term def name typ set opt_morphs thy;
haftmann@19342
   270
    fun att (thy, th) =
wenzelm@19705
   271
      let val ({type_definition, ...}, thy') = typedef_result th thy
wenzelm@19705
   272
      in (thy', type_definition) end;
wenzelm@19585
   273
  in IsarThy.theorem_i PureThy.internalK ("", [att]) (goal, [goal_pat]) thy end;
wenzelm@17339
   274
wenzelm@17339
   275
in
wenzelm@6383
   276
wenzelm@17339
   277
val typedef = gen_typedef read_term;
wenzelm@17339
   278
val typedef_i = gen_typedef cert_term;
wenzelm@17339
   279
wenzelm@19705
   280
end;
berghofe@15259
   281
haftmann@19459
   282
val setup = TypedefData.init;
berghofe@15259
   283
berghofe@15259
   284
berghofe@15259
   285
wenzelm@6383
   286
(** outer syntax **)
wenzelm@6383
   287
wenzelm@17057
   288
local structure P = OuterParse and K = OuterKeyword in
wenzelm@6383
   289
wenzelm@6357
   290
val typedeclP =
wenzelm@12624
   291
  OuterSyntax.command "typedecl" "type declaration (HOL)" K.thy_decl
wenzelm@12876
   292
    (P.type_args -- P.name -- P.opt_infix >> (fn ((vs, t), mx) =>
wenzelm@6357
   293
      Toplevel.theory (add_typedecls [(t, vs, mx)])));
wenzelm@6357
   294
wenzelm@6723
   295
wenzelm@17339
   296
val typedef_decl =
wenzelm@16126
   297
  Scan.optional (P.$$$ "(" |--
wenzelm@16126
   298
      ((P.$$$ "open" >> K false) -- Scan.option P.name || P.name >> (fn s => (true, SOME s)))
wenzelm@16126
   299
        --| P.$$$ ")") (true, NONE) --
wenzelm@11744
   300
    (P.type_args -- P.name) -- P.opt_infix -- (P.$$$ "=" |-- P.term) --
wenzelm@12876
   301
    Scan.option (P.$$$ "morphisms" |-- P.!!! (P.name -- P.name));
wenzelm@6357
   302
wenzelm@17339
   303
fun mk_typedef ((((((def, opt_name), (vs, t)), mx), A), morphs)) =
wenzelm@19473
   304
  typedef ((def, the_default (Syntax.type_name t mx) opt_name), (t, vs, mx), A, morphs);
wenzelm@6357
   305
wenzelm@6357
   306
val typedefP =
wenzelm@6723
   307
  OuterSyntax.command "typedef" "HOL type definition (requires non-emptiness proof)" K.thy_goal
wenzelm@17339
   308
    (typedef_decl >> (Toplevel.print oo (Toplevel.theory_to_proof o mk_typedef)));
wenzelm@6357
   309
wenzelm@6723
   310
wenzelm@11744
   311
val _ = OuterSyntax.add_keywords ["morphisms"];
wenzelm@6357
   312
val _ = OuterSyntax.add_parsers [typedeclP, typedefP];
wenzelm@6357
   313
wenzelm@4866
   314
end;
wenzelm@6383
   315
wenzelm@6383
   316
end;