src/Pure/axclass.ML
author ballarin
Mon Aug 08 22:14:04 2005 +0200 (2005-08-08 ago)
changeset 17034 b4d9b87c102e
parent 16486 1a12cdb6ee6b
child 17041 dee6f7047cae
permissions -rw-r--r--
After_qed takes result 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@16333
    12
  val add_axclass: bstring * xstring list -> ((bstring * string) * Attrib.src list) list
wenzelm@6379
    13
    -> theory -> theory * {intro: thm, axioms: thm list}
wenzelm@16333
    14
  val add_axclass_i: bstring * class list -> ((bstring * term) * theory attribute list) list
wenzelm@6379
    15
    -> theory -> theory * {intro: thm, axioms: thm list}
wenzelm@15876
    16
  val add_classrel_thms: thm list -> theory -> theory
wenzelm@15876
    17
  val add_arity_thms: thm list -> theory -> theory
wenzelm@16333
    18
  val add_inst_subclass: xstring * xstring -> tactic -> theory -> theory
wenzelm@11828
    19
  val add_inst_subclass_i: class * class -> tactic -> theory -> theory
wenzelm@11828
    20
  val add_inst_arity: xstring * string list * string -> tactic -> theory -> theory
wenzelm@11828
    21
  val add_inst_arity_i: string * sort list * sort -> tactic -> theory -> theory
wenzelm@16333
    22
  val instance_subclass_proof: xstring * xstring -> bool -> theory -> ProofHistory.T
wenzelm@12876
    23
  val instance_subclass_proof_i: class * class -> bool -> theory -> ProofHistory.T
wenzelm@14605
    24
  val instance_arity_proof: xstring * string list * string -> bool -> theory -> ProofHistory.T
wenzelm@14605
    25
  val instance_arity_proof_i: string * sort list * sort -> bool -> theory -> ProofHistory.T
wenzelm@15876
    26
  val intro_classes_tac: thm list -> tactic
wenzelm@15876
    27
  val default_intro_classes_tac: thm list -> tactic
wenzelm@15876
    28
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
fun intro_name c = c ^ "I";
wenzelm@6379
    70
val introN = "intro";
wenzelm@6379
    71
val axiomsN = "axioms";
wenzelm@6379
    72
wenzelm@6379
    73
wenzelm@423
    74
(* subclass relations as terms *)
wenzelm@423
    75
paulson@1498
    76
fun mk_classrel (c1, c2) = Logic.mk_inclass (aT [c1], c2);
wenzelm@423
    77
wenzelm@423
    78
fun dest_classrel tm =
wenzelm@423
    79
  let
wenzelm@3788
    80
    fun err () = raise TERM ("dest_classrel", [tm]);
wenzelm@423
    81
wenzelm@3395
    82
    val (ty, c2) = Logic.dest_inclass tm handle TERM _ => err ();
wenzelm@3395
    83
    val c1 = (case dest_varT ty of (_, [c]) => c | _ => err ())
wenzelm@3395
    84
      handle TYPE _ => err ();
wenzelm@6379
    85
  in (c1, c2) end;
wenzelm@423
    86
wenzelm@423
    87
wenzelm@423
    88
(* arities as terms *)
wenzelm@423
    89
wenzelm@14605
    90
fun mk_arity (t, Ss, c) =
wenzelm@423
    91
  let
wenzelm@14695
    92
    val tfrees = ListPair.map TFree (Term.invent_names [] "'a" (length Ss), Ss);
wenzelm@6379
    93
  in Logic.mk_inclass (Type (t, tfrees), c) end;
wenzelm@423
    94
wenzelm@14605
    95
fun mk_arities (t, Ss, S) = map (fn c => mk_arity (t, Ss, c)) S;
wenzelm@14605
    96
wenzelm@423
    97
fun dest_arity tm =
wenzelm@423
    98
  let
wenzelm@3788
    99
    fun err () = raise TERM ("dest_arity", [tm]);
wenzelm@423
   100
wenzelm@3395
   101
    val (ty, c) = Logic.dest_inclass tm handle TERM _ => err ();
wenzelm@3395
   102
    val (t, tvars) =
wenzelm@423
   103
      (case ty of
wenzelm@3395
   104
        Type (t, tys) => (t, map dest_varT tys handle TYPE _ => err ())
wenzelm@423
   105
      | _ => err ());
wenzelm@423
   106
    val ss =
wenzelm@3395
   107
      if null (gen_duplicates eq_fst tvars)
wenzelm@3395
   108
      then map snd tvars else err ();
wenzelm@6379
   109
  in (t, ss, c) end;
wenzelm@423
   110
wenzelm@423
   111
wenzelm@423
   112
wenzelm@6379
   113
(** axclass info **)
wenzelm@6379
   114
wenzelm@6379
   115
(* data kind 'Pure/axclasses' *)
wenzelm@6379
   116
wenzelm@6379
   117
type axclass_info =
wenzelm@6379
   118
  {super_classes: class list,
wenzelm@6379
   119
    intro: thm,
wenzelm@6379
   120
    axioms: thm list};
wenzelm@6379
   121
wenzelm@16458
   122
structure AxclassesData = TheoryDataFun
wenzelm@16458
   123
(struct
wenzelm@6379
   124
  val name = "Pure/axclasses";
wenzelm@6379
   125
  type T = axclass_info Symtab.table;
wenzelm@6379
   126
wenzelm@6379
   127
  val empty = Symtab.empty;
wenzelm@6546
   128
  val copy = I;
wenzelm@16458
   129
  val extend = I;
wenzelm@16458
   130
  fun merge _ = Symtab.merge (K true);
wenzelm@6379
   131
wenzelm@16458
   132
  fun print thy tab =
wenzelm@6379
   133
    let
wenzelm@6379
   134
      fun pretty_class c cs = Pretty.block
wenzelm@16458
   135
        (Pretty.str (Sign.extern_class thy c) :: Pretty.str " <" :: Pretty.brk 1 ::
wenzelm@16458
   136
          Pretty.breaks (map (Pretty.str o Sign.extern_class thy) cs));
wenzelm@6379
   137
wenzelm@10008
   138
      fun pretty_thms name thms = Pretty.big_list (name ^ ":")
wenzelm@16458
   139
        (map (Display.pretty_thm_sg thy) thms);
wenzelm@6379
   140
wenzelm@6379
   141
      fun pretty_axclass (name, {super_classes, intro, axioms}) = Pretty.block (Pretty.fbreaks
wenzelm@6379
   142
        [pretty_class name super_classes, pretty_thms introN [intro], pretty_thms axiomsN axioms]);
wenzelm@8720
   143
    in Pretty.writeln (Pretty.chunks (map pretty_axclass (Symtab.dest tab))) end;
wenzelm@16458
   144
end);
wenzelm@6379
   145
wenzelm@15801
   146
val _ = Context.add_setup [AxclassesData.init];
wenzelm@6379
   147
val print_axclasses = AxclassesData.print;
wenzelm@6379
   148
wenzelm@6379
   149
wenzelm@6379
   150
(* get and put data *)
wenzelm@6379
   151
wenzelm@16458
   152
fun lookup_axclass_info thy c = Symtab.lookup (AxclassesData.get thy, c);
wenzelm@6379
   153
wenzelm@6379
   154
fun get_axclass_info thy c =
wenzelm@16458
   155
  (case lookup_axclass_info thy c of
skalberg@15531
   156
    NONE => error ("Unknown axclass " ^ quote c)
skalberg@15531
   157
  | SOME info => info);
wenzelm@6379
   158
wenzelm@6379
   159
fun put_axclass_info c info thy =
wenzelm@6379
   160
  thy |> AxclassesData.put (Symtab.update ((c, info), AxclassesData.get thy));
wenzelm@423
   161
wenzelm@423
   162
wenzelm@15876
   163
(* class_axms *)
wenzelm@15876
   164
wenzelm@16458
   165
fun class_axms thy =
wenzelm@16458
   166
  let val classes = Sign.classes thy in
wenzelm@16458
   167
    map (Thm.class_triv thy) classes @
wenzelm@16458
   168
    List.mapPartial (Option.map #intro o lookup_axclass_info thy) classes
wenzelm@15876
   169
  end;
wenzelm@15876
   170
wenzelm@15876
   171
wenzelm@423
   172
wenzelm@423
   173
(** add axiomatic type classes **)
wenzelm@404
   174
wenzelm@15876
   175
local
wenzelm@404
   176
wenzelm@404
   177
fun err_bad_axsort ax c =
wenzelm@404
   178
  error ("Sort constraint in axiom " ^ quote ax ^ " not supersort of " ^ quote c);
wenzelm@404
   179
wenzelm@404
   180
fun err_bad_tfrees ax =
wenzelm@404
   181
  error ("More than one type variable in axiom " ^ quote ax);
wenzelm@404
   182
wenzelm@6379
   183
fun ext_axclass prep_class prep_axm prep_att (bclass, raw_super_classes) raw_axioms_atts thy =
wenzelm@404
   184
  let
wenzelm@16458
   185
    val class = Sign.full_name thy bclass;
wenzelm@16458
   186
    val super_classes = map (prep_class thy) raw_super_classes;
wenzelm@16458
   187
    val axms = map (prep_axm thy o fst) raw_axioms_atts;
wenzelm@6379
   188
    val atts = map (map (prep_att thy) o snd) raw_axioms_atts;
wenzelm@404
   189
wenzelm@6379
   190
    (*declare class*)
wenzelm@6379
   191
    val class_thy =
wenzelm@6379
   192
      thy |> Theory.add_classes_i [(bclass, super_classes)];
wenzelm@404
   193
wenzelm@6379
   194
    (*prepare abstract axioms*)
wenzelm@404
   195
    fun abs_axm ax =
wenzelm@404
   196
      if null (term_tfrees ax) then
wenzelm@14854
   197
        Logic.mk_implies (Logic.mk_inclass (aT [], class), ax)
wenzelm@3788
   198
      else map_term_tfrees (K (aT [class])) ax;
wenzelm@6379
   199
    val abs_axms = map (abs_axm o #2) axms;
wenzelm@404
   200
wenzelm@404
   201
    fun axm_sort (name, ax) =
wenzelm@404
   202
      (case term_tfrees ax of
wenzelm@404
   203
        [] => []
wenzelm@16458
   204
      | [(_, S)] => if Sign.subsort class_thy ([class], S) then S else err_bad_axsort name class
wenzelm@404
   205
      | _ => err_bad_tfrees name);
wenzelm@16458
   206
    val axS = Sign.certify_sort class_thy (List.concat (map axm_sort axms));
wenzelm@404
   207
paulson@1498
   208
    val int_axm = Logic.close_form o map_term_tfrees (K (aT axS));
paulson@1498
   209
    fun inclass c = Logic.mk_inclass (aT axS, c);
wenzelm@404
   210
paulson@1498
   211
    val intro_axm = Logic.list_implies
wenzelm@6379
   212
      (map inclass super_classes @ map (int_axm o #2) axms, inclass class);
wenzelm@6379
   213
wenzelm@6379
   214
    (*declare axioms and rule*)
wenzelm@8420
   215
    val (axms_thy, ([intro], [axioms])) =
wenzelm@6379
   216
      class_thy
wenzelm@6379
   217
      |> Theory.add_path bclass
wenzelm@6379
   218
      |> PureThy.add_axioms_i [Thm.no_attributes (introN, intro_axm)]
wenzelm@8420
   219
      |>>> PureThy.add_axiomss_i [Thm.no_attributes (axiomsN, abs_axms)];
wenzelm@6379
   220
    val info = {super_classes = super_classes, intro = intro, axioms = axioms};
wenzelm@6379
   221
wenzelm@6379
   222
    (*store info*)
wenzelm@6379
   223
    val final_thy =
wenzelm@6379
   224
      axms_thy
wenzelm@8420
   225
      |> (#1 o PureThy.add_thms ((map #1 axms ~~ axioms) ~~ atts))
wenzelm@16180
   226
      |> Theory.restore_naming class_thy
wenzelm@8420
   227
      |> (#1 o PureThy.add_thms [Thm.no_attributes (intro_name bclass, intro)])
wenzelm@6379
   228
      |> put_axclass_info 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
wenzelm@15876
   320
fun inst_proof mk_prop add_thms inst int theory =
wenzelm@15876
   321
  theory
ballarin@17034
   322
  |> IsarThy.multi_theorem_i Drule.internalK (K I) 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@6719
   361
local structure P = OuterParse and K = OuterSyntax.Keyword 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@12876
   371
    ((P.xname -- ((P.$$$ "\\<subseteq>" || P.$$$ "<") |-- P.xname) >> instance_subclass_proof ||
wenzelm@14605
   372
      (P.xname -- (P.$$$ "::" |-- P.arity) >> P.triple2) >> instance_arity_proof)
wenzelm@6379
   373
      >> (Toplevel.print oo Toplevel.theory_to_proof));
wenzelm@6379
   374
wenzelm@6379
   375
val _ = OuterSyntax.add_parsers [axclassP, instanceP];
wenzelm@6379
   376
wenzelm@6379
   377
end;
wenzelm@3949
   378
wenzelm@15876
   379
wenzelm@15876
   380
wenzelm@15876
   381
(** old-style instantiation proofs **)
wenzelm@15876
   382
wenzelm@15876
   383
(* axclass_tac *)
wenzelm@15876
   384
wenzelm@15876
   385
(*(1) repeatedly resolve goals of form "OFCLASS(ty, c_class)",
wenzelm@15876
   386
      try class_trivs first, then "cI" axioms
wenzelm@15876
   387
  (2) rewrite goals using user supplied definitions
wenzelm@15876
   388
  (3) repeatedly resolve goals with user supplied non-definitions*)
wenzelm@15876
   389
wenzelm@15876
   390
val is_def = Logic.is_equals o #prop o rep_thm;
wenzelm@15876
   391
wenzelm@15876
   392
fun axclass_tac thms =
wenzelm@15876
   393
  let
wenzelm@15876
   394
    val defs = List.filter is_def thms;
wenzelm@15876
   395
    val non_defs = filter_out is_def thms;
wenzelm@15876
   396
  in
wenzelm@15876
   397
    intro_classes_tac [] THEN
wenzelm@15876
   398
    TRY (rewrite_goals_tac defs) THEN
wenzelm@15876
   399
    TRY (REPEAT_FIRST (fn i => assume_tac i ORELSE resolve_tac non_defs i))
wenzelm@15876
   400
  end;
wenzelm@15876
   401
wenzelm@15876
   402
wenzelm@15876
   403
(* instance declarations *)
wenzelm@15876
   404
wenzelm@15876
   405
local
wenzelm@15876
   406
wenzelm@16458
   407
fun prove mk_prop str_of thy prop thms usr_tac =
wenzelm@16458
   408
  (Tactic.prove thy [] [] (mk_prop prop)
wenzelm@15973
   409
      (K (axclass_tac thms THEN (if_none usr_tac all_tac)))
wenzelm@15876
   410
    handle ERROR => error ("The error(s) above occurred while trying to prove " ^
wenzelm@16458
   411
     quote (str_of thy prop))) |> Drule.standard;
wenzelm@15876
   412
wenzelm@16458
   413
val prove_subclass = prove mk_classrel (fn thy => fn (c1, c2) =>
wenzelm@16458
   414
  Pretty.string_of_classrel (Sign.pp thy) [c1, c2]);
wenzelm@15876
   415
wenzelm@16458
   416
val prove_arity = prove mk_arity (fn thy => fn (t, Ss, c) =>
wenzelm@16458
   417
  Pretty.string_of_arity (Sign.pp thy) (t, Ss, [c]));
wenzelm@15876
   418
wenzelm@15876
   419
fun witnesses thy names thms =
wenzelm@16486
   420
  PureThy.get_thmss thy (map Name names) @
wenzelm@16458
   421
  thms @
wenzelm@16458
   422
  List.filter is_def (map snd (axioms_of thy));
wenzelm@15876
   423
wenzelm@15876
   424
in
wenzelm@15876
   425
wenzelm@15876
   426
fun add_inst_subclass_x raw_c1_c2 names thms usr_tac thy =
wenzelm@16458
   427
  let val (c1, c2) = read_classrel thy raw_c1_c2 in
wenzelm@16458
   428
    message ("Proving class inclusion " ^ quote (Sign.string_of_classrel thy [c1, c2]) ^ " ...");
wenzelm@16458
   429
    thy |> add_classrel_thms [prove_subclass thy (c1, c2) (witnesses thy names thms) usr_tac]
wenzelm@15876
   430
  end;
wenzelm@15876
   431
wenzelm@15876
   432
fun add_inst_arity_x raw_arity names thms usr_tac thy =
wenzelm@15876
   433
  let
wenzelm@16458
   434
    val (t, Ss, cs) = read_arity thy raw_arity;
wenzelm@15876
   435
    val wthms = witnesses thy names thms;
wenzelm@15876
   436
    fun prove c =
wenzelm@15876
   437
     (message ("Proving type arity " ^
wenzelm@16458
   438
        quote (Sign.string_of_arity thy (t, Ss, [c])) ^ " ...");
wenzelm@16458
   439
        prove_arity thy (t, Ss, c) wthms usr_tac);
wenzelm@15876
   440
  in add_arity_thms (map prove cs) thy end;
wenzelm@15876
   441
wenzelm@404
   442
end;
wenzelm@15876
   443
wenzelm@15876
   444
end;