src/Pure/axclass.ML
author wenzelm
Tue May 31 11:53:13 2005 +0200 (2005-05-31 ago)
changeset 16122 864fda4a4056
parent 15973 5fd94d84470f
child 16180 a51be5cbd81d
permissions -rw-r--r--
renamed cond_extern to extern;
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@15703
    12
  val add_axclass: bclass * xclass list -> ((bstring * string) * Attrib.src list) list
wenzelm@6379
    13
    -> theory -> theory * {intro: thm, axioms: thm list}
wenzelm@6379
    14
  val add_axclass_i: bclass * 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@11828
    18
  val add_inst_subclass: xclass * xclass -> 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@12876
    22
  val instance_subclass_proof: xclass * xclass -> 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@15876
    31
  val add_inst_subclass_x: xclass * xclass -> 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@6379
   122
structure AxclassesArgs =
wenzelm@6379
   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@6379
   129
  val prep_ext = I;
wenzelm@6379
   130
  fun merge (tab1, tab2) = Symtab.merge (K true) (tab1, tab2);
wenzelm@6379
   131
wenzelm@6379
   132
  fun print sg tab =
wenzelm@6379
   133
    let
wenzelm@16122
   134
      val ext_class = Sign.extern sg Sign.classK;
wenzelm@16122
   135
      val ext_thm = PureThy.extern_thm_sg sg;
wenzelm@6379
   136
wenzelm@6379
   137
      fun pretty_class c cs = Pretty.block
wenzelm@6379
   138
        (Pretty.str (ext_class c) :: Pretty.str " <" :: Pretty.brk 1 ::
wenzelm@6379
   139
          Pretty.breaks (map (Pretty.str o ext_class) cs));
wenzelm@6379
   140
wenzelm@10008
   141
      fun pretty_thms name thms = Pretty.big_list (name ^ ":")
wenzelm@10008
   142
        (map (Display.pretty_thm_sg sg) thms);
wenzelm@6379
   143
wenzelm@6379
   144
      fun pretty_axclass (name, {super_classes, intro, axioms}) = Pretty.block (Pretty.fbreaks
wenzelm@6379
   145
        [pretty_class name super_classes, pretty_thms introN [intro], pretty_thms axiomsN axioms]);
wenzelm@8720
   146
    in Pretty.writeln (Pretty.chunks (map pretty_axclass (Symtab.dest tab))) end;
wenzelm@6379
   147
end;
wenzelm@6379
   148
wenzelm@6379
   149
structure AxclassesData = TheoryDataFun(AxclassesArgs);
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@6379
   156
fun lookup_axclass_info_sg sg c = Symtab.lookup (AxclassesData.get_sg sg, c);
wenzelm@6379
   157
wenzelm@6379
   158
fun get_axclass_info thy c =
wenzelm@6379
   159
  (case lookup_axclass_info_sg (Theory.sign_of thy) c of
skalberg@15531
   160
    NONE => error ("Unknown axclass " ^ quote c)
skalberg@15531
   161
  | SOME info => info);
wenzelm@6379
   162
wenzelm@6379
   163
fun put_axclass_info c info thy =
wenzelm@6379
   164
  thy |> AxclassesData.put (Symtab.update ((c, info), AxclassesData.get thy));
wenzelm@423
   165
wenzelm@423
   166
wenzelm@15876
   167
(* class_axms *)
wenzelm@15876
   168
wenzelm@15876
   169
fun class_axms sign =
wenzelm@15876
   170
  let val classes = Sign.classes sign in
wenzelm@15876
   171
    map (Thm.class_triv sign) classes @
wenzelm@15876
   172
    List.mapPartial (Option.map #intro o lookup_axclass_info_sg sign) classes
wenzelm@15876
   173
  end;
wenzelm@15876
   174
wenzelm@15876
   175
wenzelm@423
   176
wenzelm@423
   177
(** add axiomatic type classes **)
wenzelm@404
   178
wenzelm@15876
   179
local
wenzelm@404
   180
wenzelm@404
   181
fun err_bad_axsort ax c =
wenzelm@404
   182
  error ("Sort constraint in axiom " ^ quote ax ^ " not supersort of " ^ quote c);
wenzelm@404
   183
wenzelm@404
   184
fun err_bad_tfrees ax =
wenzelm@404
   185
  error ("More than one type variable in axiom " ^ quote ax);
wenzelm@404
   186
wenzelm@6379
   187
fun ext_axclass prep_class prep_axm prep_att (bclass, raw_super_classes) raw_axioms_atts thy =
wenzelm@404
   188
  let
wenzelm@6379
   189
    val sign = Theory.sign_of thy;
wenzelm@3938
   190
wenzelm@6379
   191
    val class = Sign.full_name sign bclass;
wenzelm@6379
   192
    val super_classes = map (prep_class sign) raw_super_classes;
wenzelm@6379
   193
    val axms = map (prep_axm sign o fst) raw_axioms_atts;
wenzelm@6379
   194
    val atts = map (map (prep_att thy) o snd) raw_axioms_atts;
wenzelm@404
   195
wenzelm@6379
   196
    (*declare class*)
wenzelm@6379
   197
    val class_thy =
wenzelm@6379
   198
      thy |> Theory.add_classes_i [(bclass, super_classes)];
wenzelm@6379
   199
    val class_sign = Theory.sign_of class_thy;
wenzelm@404
   200
wenzelm@6379
   201
    (*prepare abstract axioms*)
wenzelm@404
   202
    fun abs_axm ax =
wenzelm@404
   203
      if null (term_tfrees ax) then
wenzelm@14854
   204
        Logic.mk_implies (Logic.mk_inclass (aT [], class), ax)
wenzelm@3788
   205
      else map_term_tfrees (K (aT [class])) ax;
wenzelm@6379
   206
    val abs_axms = map (abs_axm o #2) axms;
wenzelm@404
   207
wenzelm@404
   208
    fun axm_sort (name, ax) =
wenzelm@404
   209
      (case term_tfrees ax of
wenzelm@404
   210
        [] => []
wenzelm@6379
   211
      | [(_, S)] => if Sign.subsort class_sign ([class], S) then S else err_bad_axsort name class
wenzelm@404
   212
      | _ => err_bad_tfrees name);
skalberg@15570
   213
    val axS = Sign.certify_sort class_sign (List.concat (map axm_sort axms));
wenzelm@404
   214
paulson@1498
   215
    val int_axm = Logic.close_form o map_term_tfrees (K (aT axS));
paulson@1498
   216
    fun inclass c = Logic.mk_inclass (aT axS, c);
wenzelm@404
   217
paulson@1498
   218
    val intro_axm = Logic.list_implies
wenzelm@6379
   219
      (map inclass super_classes @ map (int_axm o #2) axms, inclass class);
wenzelm@6379
   220
wenzelm@6379
   221
    (*declare axioms and rule*)
wenzelm@8420
   222
    val (axms_thy, ([intro], [axioms])) =
wenzelm@6379
   223
      class_thy
wenzelm@6379
   224
      |> Theory.add_path bclass
wenzelm@6379
   225
      |> PureThy.add_axioms_i [Thm.no_attributes (introN, intro_axm)]
wenzelm@8420
   226
      |>>> PureThy.add_axiomss_i [Thm.no_attributes (axiomsN, abs_axms)];
wenzelm@6379
   227
    val info = {super_classes = super_classes, intro = intro, axioms = axioms};
wenzelm@6379
   228
wenzelm@6379
   229
    (*store info*)
wenzelm@6379
   230
    val final_thy =
wenzelm@6379
   231
      axms_thy
wenzelm@8420
   232
      |> (#1 o PureThy.add_thms ((map #1 axms ~~ axioms) ~~ atts))
wenzelm@6379
   233
      |> Theory.parent_path
wenzelm@8420
   234
      |> (#1 o PureThy.add_thms [Thm.no_attributes (intro_name bclass, intro)])
wenzelm@6379
   235
      |> put_axclass_info class info;
wenzelm@6379
   236
  in (final_thy, {intro = intro, axioms = axioms}) end;
wenzelm@404
   237
wenzelm@15876
   238
in
wenzelm@404
   239
wenzelm@6390
   240
val add_axclass = ext_axclass Sign.intern_class Theory.read_axm Attrib.global_attribute;
wenzelm@6390
   241
val add_axclass_i = ext_axclass (K I) Theory.cert_axm (K I);
wenzelm@404
   242
wenzelm@15876
   243
end;
wenzelm@15876
   244
wenzelm@15876
   245
wenzelm@15876
   246
wenzelm@15876
   247
(** proven class instantiation **)
wenzelm@15876
   248
wenzelm@15876
   249
(* add thms to type signature *)
wenzelm@15876
   250
wenzelm@15876
   251
fun add_classrel_thms thms thy =
wenzelm@15876
   252
  let
wenzelm@15876
   253
    fun prep_thm thm =
wenzelm@15876
   254
      let
wenzelm@15876
   255
        val prop = Drule.plain_prop_of (Thm.transfer thy thm);
wenzelm@15876
   256
        val (c1, c2) = dest_classrel prop handle TERM _ =>
wenzelm@15876
   257
          raise THM ("add_classrel_thms: not a class relation", 0, [thm]);
wenzelm@15876
   258
      in (c1, c2) end;
wenzelm@15876
   259
  in Theory.add_classrel_i (map prep_thm thms) thy end;
wenzelm@15876
   260
wenzelm@15876
   261
fun add_arity_thms thms thy =
wenzelm@15876
   262
  let
wenzelm@15876
   263
    fun prep_thm thm =
wenzelm@15876
   264
      let
wenzelm@15876
   265
        val prop = Drule.plain_prop_of (Thm.transfer thy thm);
wenzelm@15876
   266
        val (t, ss, c) = dest_arity prop handle TERM _ =>
wenzelm@15876
   267
          raise THM ("add_arity_thms: not an arity", 0, [thm]);
wenzelm@15876
   268
      in (t, ss, [c]) end;
wenzelm@15876
   269
  in Theory.add_arities_i (map prep_thm thms) thy end;
wenzelm@15876
   270
wenzelm@15876
   271
wenzelm@15876
   272
(* prepare classes and arities *)
wenzelm@15876
   273
wenzelm@15876
   274
fun read_class sg c = Sign.certify_class sg (Sign.intern_class sg c);
wenzelm@15876
   275
wenzelm@15876
   276
fun test_classrel sg cc = (Type.add_classrel (Sign.pp sg) [cc] (Sign.tsig_of sg); cc);
wenzelm@15876
   277
fun cert_classrel sg = test_classrel sg o Library.pairself (Sign.certify_class sg);
wenzelm@15876
   278
fun read_classrel sg = test_classrel sg o Library.pairself (read_class sg);
wenzelm@15876
   279
wenzelm@15876
   280
fun test_arity sg ar = (Type.add_arities (Sign.pp sg) [ar] (Sign.tsig_of sg); ar);
wenzelm@15876
   281
wenzelm@15876
   282
fun prep_arity prep_tycon prep_sort prep sg (t, Ss, x) =
wenzelm@15876
   283
  test_arity sg (prep_tycon sg t, map (prep_sort sg) Ss, prep sg x);
wenzelm@15876
   284
wenzelm@15876
   285
val read_arity = prep_arity Sign.intern_tycon Sign.read_sort Sign.read_sort;
wenzelm@15876
   286
val cert_arity = prep_arity (K I) Sign.certify_sort Sign.certify_sort;
wenzelm@404
   287
wenzelm@404
   288
wenzelm@15876
   289
(* instance declarations -- tactical proof *)
wenzelm@15876
   290
wenzelm@15876
   291
local
wenzelm@423
   292
wenzelm@15876
   293
fun ext_inst_subclass prep_classrel raw_cc tac thy =
wenzelm@15876
   294
  let
wenzelm@15876
   295
    val sign = Theory.sign_of thy;
wenzelm@15876
   296
    val (c1, c2) = prep_classrel sign raw_cc;
wenzelm@15876
   297
    val txt = quote (Sign.string_of_classrel sign [c1, c2]);
wenzelm@15876
   298
    val _ = message ("Proving class inclusion " ^ txt ^ " ...");
wenzelm@15876
   299
    val th = Tactic.prove sign [] [] (mk_classrel (c1, c2)) (K tac) handle ERROR =>
wenzelm@15876
   300
      error ("The error(s) above occurred while trying to prove " ^ txt);
wenzelm@15876
   301
  in add_classrel_thms [th] thy end;
wenzelm@404
   302
wenzelm@15876
   303
fun ext_inst_arity prep_arity raw_arity tac thy =
wenzelm@15876
   304
  let
wenzelm@15876
   305
    val sign = Theory.sign_of thy;
wenzelm@15876
   306
    val arity = prep_arity sign raw_arity;
wenzelm@15876
   307
    val txt = quote (Sign.string_of_arity sign arity);
wenzelm@15876
   308
    val _ = message ("Proving type arity " ^ txt ^ " ...");
wenzelm@15876
   309
    val props = (mk_arities arity);
wenzelm@15876
   310
    val ths = Tactic.prove_multi sign [] [] props
wenzelm@15876
   311
        (fn _ => Tactic.smart_conjunction_tac (length props) THEN tac)
wenzelm@15876
   312
      handle ERROR => error ("The error(s) above occurred while trying to prove " ^ txt);
wenzelm@15876
   313
  in add_arity_thms ths thy end;
wenzelm@15876
   314
wenzelm@15876
   315
in
wenzelm@15876
   316
wenzelm@15876
   317
val add_inst_subclass = ext_inst_subclass read_classrel;
wenzelm@15876
   318
val add_inst_subclass_i = ext_inst_subclass cert_classrel;
wenzelm@15876
   319
val add_inst_arity = ext_inst_arity read_arity;
wenzelm@15876
   320
val add_inst_arity_i = ext_inst_arity cert_arity;
wenzelm@15876
   321
wenzelm@15876
   322
end;
wenzelm@404
   323
wenzelm@423
   324
wenzelm@15876
   325
(* instance declarations -- Isar proof *)
wenzelm@15876
   326
wenzelm@15876
   327
local
wenzelm@15876
   328
wenzelm@15876
   329
fun inst_proof mk_prop add_thms inst int theory =
wenzelm@15876
   330
  theory
wenzelm@15876
   331
  |> IsarThy.multi_theorem_i Drule.internalK I ProofContext.export_standard
wenzelm@15876
   332
    ("", [fn (thy, th) => (add_thms [th] thy, th)]) []
wenzelm@15876
   333
    (map (fn t => (("", []), [(t, ([], []))])) (mk_prop (Theory.sign_of theory) inst)) int;
wenzelm@15876
   334
wenzelm@15876
   335
in
wenzelm@15876
   336
wenzelm@15876
   337
val instance_subclass_proof =
wenzelm@15876
   338
  inst_proof (single oo (mk_classrel oo read_classrel)) add_classrel_thms;
wenzelm@15876
   339
val instance_subclass_proof_i =
wenzelm@15876
   340
  inst_proof (single oo (mk_classrel oo cert_classrel)) add_classrel_thms;
wenzelm@15876
   341
val instance_arity_proof = inst_proof (mk_arities oo read_arity) add_arity_thms;
wenzelm@15876
   342
val instance_arity_proof_i = inst_proof (mk_arities oo cert_arity) add_arity_thms;
wenzelm@15876
   343
wenzelm@15876
   344
end;
wenzelm@15876
   345
wenzelm@15876
   346
wenzelm@15876
   347
(* tactics and methods *)
wenzelm@6379
   348
wenzelm@14605
   349
fun intro_classes_tac facts st =
wenzelm@14605
   350
  (ALLGOALS (Method.insert_tac facts THEN'
wenzelm@14605
   351
      REPEAT_ALL_NEW (resolve_tac (class_axms (Thm.sign_of_thm st))))
wenzelm@14605
   352
    THEN Tactic.distinct_subgoals_tac) st;
wenzelm@6379
   353
wenzelm@14605
   354
fun default_intro_classes_tac [] = intro_classes_tac []
wenzelm@14605
   355
  | default_intro_classes_tac _ = Tactical.no_tac;    (*no error message!*)
wenzelm@10309
   356
wenzelm@10309
   357
fun default_tac rules ctxt facts =
wenzelm@14605
   358
  HEADGOAL (Method.some_rule_tac rules ctxt facts) ORELSE
wenzelm@14605
   359
    default_intro_classes_tac facts;
wenzelm@10309
   360
wenzelm@15801
   361
val _ = Context.add_setup [Method.add_methods
wenzelm@15801
   362
 [("intro_classes", Method.no_args (Method.METHOD intro_classes_tac),
wenzelm@15801
   363
   "back-chain introduction rules of axiomatic type classes"),
wenzelm@15801
   364
  ("default", Method.thms_ctxt_args (Method.METHOD oo default_tac), "apply some intro/elim rule")]];
wenzelm@10309
   365
wenzelm@10309
   366
wenzelm@423
   367
wenzelm@15876
   368
(** outer syntax **)
wenzelm@6379
   369
wenzelm@6719
   370
local structure P = OuterParse and K = OuterSyntax.Keyword in
wenzelm@3949
   371
wenzelm@6379
   372
val axclassP =
wenzelm@6719
   373
  OuterSyntax.command "axclass" "define axiomatic type class" K.thy_decl
wenzelm@12876
   374
    ((P.name -- Scan.optional ((P.$$$ "\\<subseteq>" || P.$$$ "<") |--
wenzelm@12876
   375
        P.!!! (P.list1 P.xname)) []) -- Scan.repeat P.spec_name
wenzelm@6379
   376
      >> (fn (cls, axs) => Toplevel.theory (#1 o add_axclass cls axs)));
wenzelm@6379
   377
wenzelm@6379
   378
val instanceP =
wenzelm@6719
   379
  OuterSyntax.command "instance" "prove type arity or subclass relation" K.thy_goal
wenzelm@12876
   380
    ((P.xname -- ((P.$$$ "\\<subseteq>" || P.$$$ "<") |-- P.xname) >> instance_subclass_proof ||
wenzelm@14605
   381
      (P.xname -- (P.$$$ "::" |-- P.arity) >> P.triple2) >> instance_arity_proof)
wenzelm@6379
   382
      >> (Toplevel.print oo Toplevel.theory_to_proof));
wenzelm@6379
   383
wenzelm@6379
   384
val _ = OuterSyntax.add_parsers [axclassP, instanceP];
wenzelm@6379
   385
wenzelm@6379
   386
end;
wenzelm@3949
   387
wenzelm@15876
   388
wenzelm@15876
   389
wenzelm@15876
   390
(** old-style instantiation proofs **)
wenzelm@15876
   391
wenzelm@15876
   392
(* axclass_tac *)
wenzelm@15876
   393
wenzelm@15876
   394
(*(1) repeatedly resolve goals of form "OFCLASS(ty, c_class)",
wenzelm@15876
   395
      try class_trivs first, then "cI" axioms
wenzelm@15876
   396
  (2) rewrite goals using user supplied definitions
wenzelm@15876
   397
  (3) repeatedly resolve goals with user supplied non-definitions*)
wenzelm@15876
   398
wenzelm@15876
   399
val is_def = Logic.is_equals o #prop o rep_thm;
wenzelm@15876
   400
wenzelm@15876
   401
fun axclass_tac thms =
wenzelm@15876
   402
  let
wenzelm@15876
   403
    val defs = List.filter is_def thms;
wenzelm@15876
   404
    val non_defs = filter_out is_def thms;
wenzelm@15876
   405
  in
wenzelm@15876
   406
    intro_classes_tac [] THEN
wenzelm@15876
   407
    TRY (rewrite_goals_tac defs) THEN
wenzelm@15876
   408
    TRY (REPEAT_FIRST (fn i => assume_tac i ORELSE resolve_tac non_defs i))
wenzelm@15876
   409
  end;
wenzelm@15876
   410
wenzelm@15876
   411
wenzelm@15876
   412
(* instance declarations *)
wenzelm@15876
   413
wenzelm@15876
   414
local
wenzelm@15876
   415
wenzelm@15876
   416
fun prove mk_prop str_of sign prop thms usr_tac =
wenzelm@15876
   417
  (Tactic.prove sign [] [] (mk_prop prop)
wenzelm@15973
   418
      (K (axclass_tac thms THEN (if_none usr_tac all_tac)))
wenzelm@15876
   419
    handle ERROR => error ("The error(s) above occurred while trying to prove " ^
wenzelm@15876
   420
     quote (str_of sign prop))) |> Drule.standard;
wenzelm@15876
   421
wenzelm@15876
   422
val prove_subclass = prove mk_classrel (fn sg => fn (c1, c2) =>
wenzelm@15876
   423
  Pretty.string_of_classrel (Sign.pp sg) [c1, c2]);
wenzelm@15876
   424
wenzelm@15876
   425
val prove_arity = prove mk_arity (fn sg => fn (t, Ss, c) =>
wenzelm@15876
   426
  Pretty.string_of_arity (Sign.pp sg) (t, Ss, [c]));
wenzelm@15876
   427
wenzelm@15876
   428
fun witnesses thy names thms =
wenzelm@15876
   429
  PureThy.get_thmss thy (map (rpair NONE) names) @ thms @ List.filter is_def (map snd (axioms_of thy));
wenzelm@15876
   430
wenzelm@15876
   431
in
wenzelm@15876
   432
wenzelm@15876
   433
fun add_inst_subclass_x raw_c1_c2 names thms usr_tac thy =
wenzelm@15876
   434
  let
wenzelm@15876
   435
    val sign = Theory.sign_of thy;
wenzelm@15876
   436
    val (c1, c2) = read_classrel sign raw_c1_c2;
wenzelm@15876
   437
  in
wenzelm@15876
   438
    message ("Proving class inclusion " ^ quote (Sign.string_of_classrel sign [c1, c2]) ^ " ...");
wenzelm@15876
   439
    thy |> add_classrel_thms [prove_subclass sign (c1, c2) (witnesses thy names thms) usr_tac]
wenzelm@15876
   440
  end;
wenzelm@15876
   441
wenzelm@15876
   442
fun add_inst_arity_x raw_arity names thms usr_tac thy =
wenzelm@15876
   443
  let
wenzelm@15876
   444
    val sign = Theory.sign_of thy;
wenzelm@15876
   445
    val (t, Ss, cs) = read_arity sign raw_arity;
wenzelm@15876
   446
    val wthms = witnesses thy names thms;
wenzelm@15876
   447
    fun prove c =
wenzelm@15876
   448
     (message ("Proving type arity " ^
wenzelm@15876
   449
        quote (Sign.string_of_arity sign (t, Ss, [c])) ^ " ...");
wenzelm@15876
   450
        prove_arity sign (t, Ss, c) wthms usr_tac);
wenzelm@15876
   451
  in add_arity_thms (map prove cs) thy end;
wenzelm@15876
   452
wenzelm@404
   453
end;
wenzelm@15876
   454
wenzelm@15876
   455
end;