src/Pure/axclass.ML
author wenzelm
Tue Sep 06 16:59:56 2005 +0200 (2005-09-06)
changeset 17281 3b9ff0131ed1
parent 17221 6cd180204582
child 17339 ab97ccef124a
permissions -rw-r--r--
name space prefix is now "c_class" instead of just "c";
export get_info;
tuned;
wenzelm@404
     1
(*  Title:      Pure/axclass.ML
wenzelm@404
     2
    ID:         $Id$
wenzelm@404
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@404
     4
wenzelm@6379
     5
Axiomatic type class package.
wenzelm@404
     6
*)
wenzelm@404
     7
wenzelm@404
     8
signature AX_CLASS =
wenzelm@3938
     9
sig
wenzelm@5685
    10
  val quiet_mode: bool ref
wenzelm@6379
    11
  val print_axclasses: theory -> unit
wenzelm@17281
    12
  val get_info: theory -> string -> {super_classes: class list, intro: thm, axioms: thm list}
wenzelm@16333
    13
  val add_axclass: bstring * xstring list -> ((bstring * string) * Attrib.src list) list
wenzelm@6379
    14
    -> theory -> theory * {intro: thm, axioms: thm list}
wenzelm@16333
    15
  val add_axclass_i: bstring * class list -> ((bstring * term) * theory attribute list) list
wenzelm@6379
    16
    -> theory -> theory * {intro: thm, axioms: thm list}
wenzelm@15876
    17
  val add_classrel_thms: thm list -> theory -> theory
wenzelm@15876
    18
  val add_arity_thms: thm list -> theory -> theory
wenzelm@16333
    19
  val add_inst_subclass: xstring * xstring -> tactic -> theory -> theory
wenzelm@11828
    20
  val add_inst_subclass_i: class * class -> tactic -> theory -> theory
wenzelm@11828
    21
  val add_inst_arity: xstring * string list * string -> tactic -> theory -> theory
wenzelm@11828
    22
  val add_inst_arity_i: string * sort list * sort -> tactic -> theory -> theory
haftmann@17041
    23
  val instance_subclass_proof: xstring * xstring -> (theory -> theory)
haftmann@17041
    24
    -> bool -> theory -> ProofHistory.T
haftmann@17041
    25
  val instance_subclass_proof_i: class * class -> (theory -> theory)
haftmann@17041
    26
    -> bool -> theory -> ProofHistory.T
haftmann@17041
    27
  val instance_arity_proof: xstring * string list * string -> (theory -> theory)
haftmann@17041
    28
    -> bool -> theory -> ProofHistory.T
haftmann@17041
    29
  val instance_arity_proof_i: string * sort list * sort -> (theory -> theory)
haftmann@17041
    30
    -> bool -> theory -> ProofHistory.T
wenzelm@15876
    31
  val intro_classes_tac: thm list -> tactic
wenzelm@15876
    32
  val default_intro_classes_tac: thm list -> tactic
wenzelm@15876
    33
wenzelm@15876
    34
  (*legacy interfaces*)
wenzelm@15876
    35
  val axclass_tac: thm list -> tactic
wenzelm@16333
    36
  val add_inst_subclass_x: xstring * xstring -> string list -> thm list
wenzelm@15876
    37
    -> tactic option -> theory -> theory
wenzelm@15876
    38
  val add_inst_arity_x: xstring * string list * string -> string list
wenzelm@15876
    39
    -> thm list -> tactic option -> theory -> theory
wenzelm@3938
    40
end;
wenzelm@404
    41
wenzelm@15801
    42
structure AxClass: AX_CLASS =
wenzelm@404
    43
struct
wenzelm@404
    44
wenzelm@4015
    45
wenzelm@404
    46
(** utilities **)
wenzelm@404
    47
wenzelm@5685
    48
(* messages *)
wenzelm@5685
    49
wenzelm@5685
    50
val quiet_mode = ref false;
wenzelm@5685
    51
fun message s = if ! quiet_mode then () else writeln s;
wenzelm@5685
    52
wenzelm@5685
    53
wenzelm@404
    54
(* type vars *)
wenzelm@404
    55
wenzelm@404
    56
fun map_typ_frees f (Type (t, tys)) = Type (t, map (map_typ_frees f) tys)
wenzelm@404
    57
  | map_typ_frees f (TFree a) = f a
wenzelm@404
    58
  | map_typ_frees _ a = a;
wenzelm@404
    59
wenzelm@404
    60
val map_term_tfrees = map_term_types o map_typ_frees;
wenzelm@404
    61
wenzelm@404
    62
fun aT S = TFree ("'a", S);
wenzelm@404
    63
wenzelm@3395
    64
fun dest_varT (TFree (x, S)) = ((x, ~1), S)
wenzelm@3395
    65
  | dest_varT (TVar xi_S) = xi_S
wenzelm@3788
    66
  | dest_varT T = raise TYPE ("dest_varT", [T], []);
wenzelm@3395
    67
wenzelm@404
    68
wenzelm@404
    69
wenzelm@560
    70
(** abstract syntax operations **)
wenzelm@423
    71
wenzelm@6379
    72
(* names *)
wenzelm@6379
    73
wenzelm@6379
    74
val introN = "intro";
wenzelm@6379
    75
val axiomsN = "axioms";
wenzelm@6379
    76
wenzelm@6379
    77
wenzelm@423
    78
(* subclass relations as terms *)
wenzelm@423
    79
paulson@1498
    80
fun mk_classrel (c1, c2) = Logic.mk_inclass (aT [c1], c2);
wenzelm@423
    81
wenzelm@423
    82
fun dest_classrel tm =
wenzelm@423
    83
  let
wenzelm@3788
    84
    fun err () = raise TERM ("dest_classrel", [tm]);
wenzelm@423
    85
wenzelm@3395
    86
    val (ty, c2) = Logic.dest_inclass tm handle TERM _ => err ();
wenzelm@3395
    87
    val c1 = (case dest_varT ty of (_, [c]) => c | _ => err ())
wenzelm@3395
    88
      handle TYPE _ => err ();
wenzelm@6379
    89
  in (c1, c2) end;
wenzelm@423
    90
wenzelm@423
    91
wenzelm@423
    92
(* arities as terms *)
wenzelm@423
    93
wenzelm@14605
    94
fun mk_arity (t, Ss, c) =
wenzelm@423
    95
  let
wenzelm@14695
    96
    val tfrees = ListPair.map TFree (Term.invent_names [] "'a" (length Ss), Ss);
wenzelm@6379
    97
  in Logic.mk_inclass (Type (t, tfrees), c) end;
wenzelm@423
    98
wenzelm@14605
    99
fun mk_arities (t, Ss, S) = map (fn c => mk_arity (t, Ss, c)) S;
wenzelm@14605
   100
wenzelm@423
   101
fun dest_arity tm =
wenzelm@423
   102
  let
wenzelm@3788
   103
    fun err () = raise TERM ("dest_arity", [tm]);
wenzelm@423
   104
wenzelm@3395
   105
    val (ty, c) = Logic.dest_inclass tm handle TERM _ => err ();
wenzelm@3395
   106
    val (t, tvars) =
wenzelm@423
   107
      (case ty of
wenzelm@3395
   108
        Type (t, tys) => (t, map dest_varT tys handle TYPE _ => err ())
wenzelm@423
   109
      | _ => err ());
wenzelm@423
   110
    val ss =
wenzelm@3395
   111
      if null (gen_duplicates eq_fst tvars)
wenzelm@3395
   112
      then map snd tvars else err ();
wenzelm@6379
   113
  in (t, ss, c) end;
wenzelm@423
   114
wenzelm@423
   115
wenzelm@423
   116
wenzelm@6379
   117
(** axclass info **)
wenzelm@6379
   118
wenzelm@6379
   119
(* data kind 'Pure/axclasses' *)
wenzelm@6379
   120
wenzelm@6379
   121
type axclass_info =
wenzelm@6379
   122
  {super_classes: class list,
wenzelm@6379
   123
    intro: thm,
wenzelm@6379
   124
    axioms: thm list};
wenzelm@6379
   125
wenzelm@16458
   126
structure AxclassesData = TheoryDataFun
wenzelm@16458
   127
(struct
wenzelm@6379
   128
  val name = "Pure/axclasses";
wenzelm@6379
   129
  type T = axclass_info Symtab.table;
wenzelm@6379
   130
wenzelm@6379
   131
  val empty = Symtab.empty;
wenzelm@6546
   132
  val copy = I;
wenzelm@16458
   133
  val extend = I;
wenzelm@16458
   134
  fun merge _ = Symtab.merge (K true);
wenzelm@6379
   135
wenzelm@16458
   136
  fun print thy tab =
wenzelm@6379
   137
    let
wenzelm@6379
   138
      fun pretty_class c cs = Pretty.block
wenzelm@16458
   139
        (Pretty.str (Sign.extern_class thy c) :: Pretty.str " <" :: Pretty.brk 1 ::
wenzelm@16458
   140
          Pretty.breaks (map (Pretty.str o Sign.extern_class thy) cs));
wenzelm@6379
   141
wenzelm@10008
   142
      fun pretty_thms name thms = Pretty.big_list (name ^ ":")
wenzelm@16458
   143
        (map (Display.pretty_thm_sg thy) thms);
wenzelm@6379
   144
wenzelm@6379
   145
      fun pretty_axclass (name, {super_classes, intro, axioms}) = Pretty.block (Pretty.fbreaks
wenzelm@6379
   146
        [pretty_class name super_classes, pretty_thms introN [intro], pretty_thms axiomsN axioms]);
wenzelm@8720
   147
    in Pretty.writeln (Pretty.chunks (map pretty_axclass (Symtab.dest tab))) end;
wenzelm@16458
   148
end);
wenzelm@6379
   149
wenzelm@15801
   150
val _ = Context.add_setup [AxclassesData.init];
wenzelm@6379
   151
val print_axclasses = AxclassesData.print;
wenzelm@6379
   152
wenzelm@6379
   153
wenzelm@6379
   154
(* get and put data *)
wenzelm@6379
   155
wenzelm@17281
   156
val lookup_info = Symtab.curried_lookup o AxclassesData.get;
wenzelm@6379
   157
wenzelm@17281
   158
fun get_info thy c =
wenzelm@17281
   159
  (case lookup_info thy c of
skalberg@15531
   160
    NONE => error ("Unknown axclass " ^ quote c)
skalberg@15531
   161
  | SOME info => info);
wenzelm@6379
   162
wenzelm@423
   163
wenzelm@15876
   164
(* class_axms *)
wenzelm@15876
   165
wenzelm@16458
   166
fun class_axms thy =
wenzelm@16458
   167
  let val classes = Sign.classes thy in
wenzelm@16458
   168
    map (Thm.class_triv thy) classes @
wenzelm@17281
   169
    List.mapPartial (Option.map #intro o lookup_info thy) classes
wenzelm@15876
   170
  end;
wenzelm@15876
   171
wenzelm@15876
   172
wenzelm@423
   173
wenzelm@423
   174
(** add axiomatic type classes **)
wenzelm@404
   175
wenzelm@15876
   176
local
wenzelm@404
   177
wenzelm@404
   178
fun err_bad_axsort ax c =
wenzelm@404
   179
  error ("Sort constraint in axiom " ^ quote ax ^ " not supersort of " ^ quote c);
wenzelm@404
   180
wenzelm@404
   181
fun err_bad_tfrees ax =
wenzelm@404
   182
  error ("More than one type variable in axiom " ^ quote ax);
wenzelm@404
   183
wenzelm@6379
   184
fun ext_axclass prep_class prep_axm prep_att (bclass, raw_super_classes) raw_axioms_atts thy =
wenzelm@404
   185
  let
wenzelm@16458
   186
    val class = Sign.full_name thy bclass;
wenzelm@16458
   187
    val super_classes = map (prep_class thy) raw_super_classes;
wenzelm@16458
   188
    val axms = map (prep_axm thy o fst) raw_axioms_atts;
wenzelm@6379
   189
    val atts = map (map (prep_att thy) o snd) raw_axioms_atts;
wenzelm@404
   190
wenzelm@6379
   191
    (*declare class*)
wenzelm@6379
   192
    val class_thy =
wenzelm@6379
   193
      thy |> Theory.add_classes_i [(bclass, super_classes)];
wenzelm@404
   194
wenzelm@6379
   195
    (*prepare abstract axioms*)
wenzelm@404
   196
    fun abs_axm ax =
wenzelm@404
   197
      if null (term_tfrees ax) then
wenzelm@14854
   198
        Logic.mk_implies (Logic.mk_inclass (aT [], class), ax)
wenzelm@3788
   199
      else map_term_tfrees (K (aT [class])) ax;
wenzelm@6379
   200
    val abs_axms = map (abs_axm o #2) axms;
wenzelm@404
   201
wenzelm@404
   202
    fun axm_sort (name, ax) =
wenzelm@404
   203
      (case term_tfrees ax of
wenzelm@404
   204
        [] => []
wenzelm@16458
   205
      | [(_, S)] => if Sign.subsort class_thy ([class], S) then S else err_bad_axsort name class
wenzelm@404
   206
      | _ => err_bad_tfrees name);
wenzelm@16458
   207
    val axS = Sign.certify_sort class_thy (List.concat (map axm_sort axms));
wenzelm@404
   208
paulson@1498
   209
    val int_axm = Logic.close_form o map_term_tfrees (K (aT axS));
paulson@1498
   210
    fun inclass c = Logic.mk_inclass (aT axS, c);
wenzelm@404
   211
paulson@1498
   212
    val intro_axm = Logic.list_implies
wenzelm@6379
   213
      (map inclass super_classes @ map (int_axm o #2) axms, inclass class);
wenzelm@6379
   214
wenzelm@6379
   215
    (*declare axioms and rule*)
wenzelm@8420
   216
    val (axms_thy, ([intro], [axioms])) =
wenzelm@6379
   217
      class_thy
wenzelm@17281
   218
      |> Theory.add_path (Sign.const_of_class bclass)
wenzelm@6379
   219
      |> PureThy.add_axioms_i [Thm.no_attributes (introN, intro_axm)]
wenzelm@8420
   220
      |>>> PureThy.add_axiomss_i [Thm.no_attributes (axiomsN, abs_axms)];
wenzelm@6379
   221
    val info = {super_classes = super_classes, intro = intro, axioms = axioms};
wenzelm@6379
   222
wenzelm@6379
   223
    (*store info*)
wenzelm@6379
   224
    val final_thy =
wenzelm@6379
   225
      axms_thy
wenzelm@8420
   226
      |> (#1 o PureThy.add_thms ((map #1 axms ~~ axioms) ~~ atts))
wenzelm@16180
   227
      |> Theory.restore_naming class_thy
wenzelm@17281
   228
      |> AxclassesData.map (Symtab.curried_update (class, info));
wenzelm@6379
   229
  in (final_thy, {intro = intro, axioms = axioms}) end;
wenzelm@404
   230
wenzelm@15876
   231
in
wenzelm@404
   232
wenzelm@6390
   233
val add_axclass = ext_axclass Sign.intern_class Theory.read_axm Attrib.global_attribute;
wenzelm@6390
   234
val add_axclass_i = ext_axclass (K I) Theory.cert_axm (K I);
wenzelm@404
   235
wenzelm@15876
   236
end;
wenzelm@15876
   237
wenzelm@15876
   238
wenzelm@15876
   239
wenzelm@15876
   240
(** proven class instantiation **)
wenzelm@15876
   241
wenzelm@15876
   242
(* add thms to type signature *)
wenzelm@15876
   243
wenzelm@15876
   244
fun add_classrel_thms thms thy =
wenzelm@15876
   245
  let
wenzelm@15876
   246
    fun prep_thm thm =
wenzelm@15876
   247
      let
wenzelm@15876
   248
        val prop = Drule.plain_prop_of (Thm.transfer thy thm);
wenzelm@15876
   249
        val (c1, c2) = dest_classrel prop handle TERM _ =>
wenzelm@15876
   250
          raise THM ("add_classrel_thms: not a class relation", 0, [thm]);
wenzelm@15876
   251
      in (c1, c2) end;
wenzelm@15876
   252
  in Theory.add_classrel_i (map prep_thm thms) thy end;
wenzelm@15876
   253
wenzelm@15876
   254
fun add_arity_thms thms thy =
wenzelm@15876
   255
  let
wenzelm@15876
   256
    fun prep_thm thm =
wenzelm@15876
   257
      let
wenzelm@15876
   258
        val prop = Drule.plain_prop_of (Thm.transfer thy thm);
wenzelm@15876
   259
        val (t, ss, c) = dest_arity prop handle TERM _ =>
wenzelm@15876
   260
          raise THM ("add_arity_thms: not an arity", 0, [thm]);
wenzelm@15876
   261
      in (t, ss, [c]) end;
wenzelm@15876
   262
  in Theory.add_arities_i (map prep_thm thms) thy end;
wenzelm@15876
   263
wenzelm@15876
   264
wenzelm@15876
   265
(* prepare classes and arities *)
wenzelm@15876
   266
wenzelm@16458
   267
fun read_class thy c = Sign.certify_class thy (Sign.intern_class thy c);
wenzelm@15876
   268
wenzelm@16458
   269
fun test_classrel thy cc = (Type.add_classrel (Sign.pp thy) [cc] (Sign.tsig_of thy); cc);
wenzelm@16458
   270
fun cert_classrel thy = test_classrel thy o Library.pairself (Sign.certify_class thy);
wenzelm@16458
   271
fun read_classrel thy = test_classrel thy o Library.pairself (read_class thy);
wenzelm@15876
   272
wenzelm@16458
   273
fun test_arity thy ar = (Type.add_arities (Sign.pp thy) [ar] (Sign.tsig_of thy); ar);
wenzelm@15876
   274
wenzelm@16458
   275
fun prep_arity prep_tycon prep_sort prep thy (t, Ss, x) =
wenzelm@16458
   276
  test_arity thy (prep_tycon thy t, map (prep_sort thy) Ss, prep thy x);
wenzelm@15876
   277
wenzelm@16364
   278
val read_arity = prep_arity Sign.intern_type Sign.read_sort Sign.read_sort;
wenzelm@15876
   279
val cert_arity = prep_arity (K I) Sign.certify_sort Sign.certify_sort;
wenzelm@404
   280
wenzelm@404
   281
wenzelm@15876
   282
(* instance declarations -- tactical proof *)
wenzelm@15876
   283
wenzelm@15876
   284
local
wenzelm@423
   285
wenzelm@15876
   286
fun ext_inst_subclass prep_classrel raw_cc tac thy =
wenzelm@15876
   287
  let
wenzelm@16458
   288
    val (c1, c2) = prep_classrel thy raw_cc;
wenzelm@16458
   289
    val txt = quote (Sign.string_of_classrel thy [c1, c2]);
wenzelm@15876
   290
    val _ = message ("Proving class inclusion " ^ txt ^ " ...");
wenzelm@16458
   291
    val th = Tactic.prove thy [] [] (mk_classrel (c1, c2)) (K tac) handle ERROR =>
wenzelm@15876
   292
      error ("The error(s) above occurred while trying to prove " ^ txt);
wenzelm@15876
   293
  in add_classrel_thms [th] thy end;
wenzelm@404
   294
wenzelm@15876
   295
fun ext_inst_arity prep_arity raw_arity tac thy =
wenzelm@15876
   296
  let
wenzelm@16458
   297
    val arity = prep_arity thy raw_arity;
wenzelm@16458
   298
    val txt = quote (Sign.string_of_arity thy arity);
wenzelm@15876
   299
    val _ = message ("Proving type arity " ^ txt ^ " ...");
wenzelm@15876
   300
    val props = (mk_arities arity);
wenzelm@16458
   301
    val ths = Tactic.prove_multi thy [] [] props
wenzelm@15876
   302
        (fn _ => Tactic.smart_conjunction_tac (length props) THEN tac)
wenzelm@15876
   303
      handle ERROR => error ("The error(s) above occurred while trying to prove " ^ txt);
wenzelm@15876
   304
  in add_arity_thms ths thy end;
wenzelm@15876
   305
wenzelm@15876
   306
in
wenzelm@15876
   307
wenzelm@15876
   308
val add_inst_subclass = ext_inst_subclass read_classrel;
wenzelm@15876
   309
val add_inst_subclass_i = ext_inst_subclass cert_classrel;
wenzelm@15876
   310
val add_inst_arity = ext_inst_arity read_arity;
wenzelm@15876
   311
val add_inst_arity_i = ext_inst_arity cert_arity;
wenzelm@15876
   312
wenzelm@15876
   313
end;
wenzelm@404
   314
wenzelm@423
   315
wenzelm@15876
   316
(* instance declarations -- Isar proof *)
wenzelm@15876
   317
wenzelm@15876
   318
local
wenzelm@15876
   319
haftmann@17041
   320
fun inst_proof mk_prop add_thms inst after_qed int theory =
wenzelm@15876
   321
  theory
haftmann@17041
   322
  |> IsarThy.multi_theorem_i Drule.internalK (K after_qed) ProofContext.export_standard
wenzelm@15876
   323
    ("", [fn (thy, th) => (add_thms [th] thy, th)]) []
wenzelm@16458
   324
    (map (fn t => (("", []), [(t, ([], []))])) (mk_prop theory inst)) int;
wenzelm@15876
   325
wenzelm@15876
   326
in
wenzelm@15876
   327
wenzelm@15876
   328
val instance_subclass_proof =
wenzelm@15876
   329
  inst_proof (single oo (mk_classrel oo read_classrel)) add_classrel_thms;
wenzelm@15876
   330
val instance_subclass_proof_i =
wenzelm@15876
   331
  inst_proof (single oo (mk_classrel oo cert_classrel)) add_classrel_thms;
wenzelm@15876
   332
val instance_arity_proof = inst_proof (mk_arities oo read_arity) add_arity_thms;
wenzelm@15876
   333
val instance_arity_proof_i = inst_proof (mk_arities oo cert_arity) add_arity_thms;
wenzelm@15876
   334
wenzelm@15876
   335
end;
wenzelm@15876
   336
wenzelm@15876
   337
wenzelm@15876
   338
(* tactics and methods *)
wenzelm@6379
   339
wenzelm@14605
   340
fun intro_classes_tac facts st =
wenzelm@14605
   341
  (ALLGOALS (Method.insert_tac facts THEN'
wenzelm@16458
   342
      REPEAT_ALL_NEW (resolve_tac (class_axms (Thm.theory_of_thm st))))
wenzelm@14605
   343
    THEN Tactic.distinct_subgoals_tac) st;
wenzelm@6379
   344
wenzelm@14605
   345
fun default_intro_classes_tac [] = intro_classes_tac []
wenzelm@14605
   346
  | default_intro_classes_tac _ = Tactical.no_tac;    (*no error message!*)
wenzelm@10309
   347
wenzelm@10309
   348
fun default_tac rules ctxt facts =
wenzelm@14605
   349
  HEADGOAL (Method.some_rule_tac rules ctxt facts) ORELSE
wenzelm@14605
   350
    default_intro_classes_tac facts;
wenzelm@10309
   351
wenzelm@15801
   352
val _ = Context.add_setup [Method.add_methods
wenzelm@15801
   353
 [("intro_classes", Method.no_args (Method.METHOD intro_classes_tac),
wenzelm@15801
   354
   "back-chain introduction rules of axiomatic type classes"),
wenzelm@15801
   355
  ("default", Method.thms_ctxt_args (Method.METHOD oo default_tac), "apply some intro/elim rule")]];
wenzelm@10309
   356
wenzelm@10309
   357
wenzelm@423
   358
wenzelm@15876
   359
(** outer syntax **)
wenzelm@6379
   360
wenzelm@17057
   361
local structure P = OuterParse and K = OuterKeyword in
wenzelm@3949
   362
wenzelm@6379
   363
val axclassP =
wenzelm@6719
   364
  OuterSyntax.command "axclass" "define axiomatic type class" K.thy_decl
wenzelm@12876
   365
    ((P.name -- Scan.optional ((P.$$$ "\\<subseteq>" || P.$$$ "<") |--
wenzelm@12876
   366
        P.!!! (P.list1 P.xname)) []) -- Scan.repeat P.spec_name
wenzelm@6379
   367
      >> (fn (cls, axs) => Toplevel.theory (#1 o add_axclass cls axs)));
wenzelm@6379
   368
wenzelm@6379
   369
val instanceP =
wenzelm@6719
   370
  OuterSyntax.command "instance" "prove type arity or subclass relation" K.thy_goal
wenzelm@17281
   371
   ((P.xname -- ((P.$$$ "\\<subseteq>" || P.$$$ "<") |-- P.xname)
wenzelm@17281
   372
      >> (fn i => instance_subclass_proof i I) ||
wenzelm@17281
   373
    (P.xname -- (P.$$$ "::" |-- P.arity) >> P.triple2)
wenzelm@17281
   374
      >> (fn i => instance_arity_proof i I))
wenzelm@17281
   375
   >> (Toplevel.print oo Toplevel.theory_to_proof));
wenzelm@6379
   376
wenzelm@6379
   377
val _ = OuterSyntax.add_parsers [axclassP, instanceP];
wenzelm@6379
   378
wenzelm@6379
   379
end;
wenzelm@3949
   380
wenzelm@15876
   381
wenzelm@15876
   382
wenzelm@15876
   383
(** old-style instantiation proofs **)
wenzelm@15876
   384
wenzelm@15876
   385
(* axclass_tac *)
wenzelm@15876
   386
wenzelm@15876
   387
(*(1) repeatedly resolve goals of form "OFCLASS(ty, c_class)",
wenzelm@15876
   388
      try class_trivs first, then "cI" axioms
wenzelm@15876
   389
  (2) rewrite goals using user supplied definitions
wenzelm@15876
   390
  (3) repeatedly resolve goals with user supplied non-definitions*)
wenzelm@15876
   391
wenzelm@15876
   392
val is_def = Logic.is_equals o #prop o rep_thm;
wenzelm@15876
   393
wenzelm@15876
   394
fun axclass_tac thms =
wenzelm@15876
   395
  let
wenzelm@15876
   396
    val defs = List.filter is_def thms;
wenzelm@15876
   397
    val non_defs = filter_out is_def thms;
wenzelm@15876
   398
  in
wenzelm@15876
   399
    intro_classes_tac [] THEN
wenzelm@15876
   400
    TRY (rewrite_goals_tac defs) THEN
wenzelm@15876
   401
    TRY (REPEAT_FIRST (fn i => assume_tac i ORELSE resolve_tac non_defs i))
wenzelm@15876
   402
  end;
wenzelm@15876
   403
wenzelm@15876
   404
wenzelm@15876
   405
(* instance declarations *)
wenzelm@15876
   406
wenzelm@15876
   407
local
wenzelm@15876
   408
wenzelm@16458
   409
fun prove mk_prop str_of thy prop thms usr_tac =
wenzelm@16458
   410
  (Tactic.prove thy [] [] (mk_prop prop)
wenzelm@15973
   411
      (K (axclass_tac thms THEN (if_none usr_tac all_tac)))
wenzelm@15876
   412
    handle ERROR => error ("The error(s) above occurred while trying to prove " ^
wenzelm@16458
   413
     quote (str_of thy prop))) |> Drule.standard;
wenzelm@15876
   414
wenzelm@16458
   415
val prove_subclass = prove mk_classrel (fn thy => fn (c1, c2) =>
wenzelm@16458
   416
  Pretty.string_of_classrel (Sign.pp thy) [c1, c2]);
wenzelm@15876
   417
wenzelm@16458
   418
val prove_arity = prove mk_arity (fn thy => fn (t, Ss, c) =>
wenzelm@16458
   419
  Pretty.string_of_arity (Sign.pp thy) (t, Ss, [c]));
wenzelm@15876
   420
wenzelm@15876
   421
fun witnesses thy names thms =
wenzelm@16486
   422
  PureThy.get_thmss thy (map Name names) @
wenzelm@16458
   423
  thms @
wenzelm@16458
   424
  List.filter is_def (map snd (axioms_of thy));
wenzelm@15876
   425
wenzelm@15876
   426
in
wenzelm@15876
   427
wenzelm@15876
   428
fun add_inst_subclass_x raw_c1_c2 names thms usr_tac thy =
wenzelm@16458
   429
  let val (c1, c2) = read_classrel thy raw_c1_c2 in
wenzelm@16458
   430
    message ("Proving class inclusion " ^ quote (Sign.string_of_classrel thy [c1, c2]) ^ " ...");
wenzelm@16458
   431
    thy |> add_classrel_thms [prove_subclass thy (c1, c2) (witnesses thy names thms) usr_tac]
wenzelm@15876
   432
  end;
wenzelm@15876
   433
wenzelm@15876
   434
fun add_inst_arity_x raw_arity names thms usr_tac thy =
wenzelm@15876
   435
  let
wenzelm@16458
   436
    val (t, Ss, cs) = read_arity thy raw_arity;
wenzelm@15876
   437
    val wthms = witnesses thy names thms;
wenzelm@15876
   438
    fun prove c =
wenzelm@15876
   439
     (message ("Proving type arity " ^
wenzelm@16458
   440
        quote (Sign.string_of_arity thy (t, Ss, [c])) ^ " ...");
wenzelm@16458
   441
        prove_arity thy (t, Ss, c) wthms usr_tac);
wenzelm@15876
   442
  in add_arity_thms (map prove cs) thy end;
wenzelm@15876
   443
wenzelm@404
   444
end;
wenzelm@15876
   445
wenzelm@15876
   446
end;