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