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