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