src/Pure/axclass.ML
author wenzelm
Mon Apr 10 00:33:51 2006 +0200 (2006-04-10)
changeset 19392 a631cd2117a8
parent 19243 5dcb899a8486
child 19398 8ad34412ea97
permissions -rw-r--r--
add_axclass(_i): return class name only;
subclass/arity statements: require actual TVars, store raw data;
tuned;
wenzelm@404
     1
(*  Title:      Pure/axclass.ML
wenzelm@404
     2
    ID:         $Id$
wenzelm@404
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@404
     4
wenzelm@19243
     5
Axiomatic type classes: pure logical content.
wenzelm@404
     6
*)
wenzelm@404
     7
wenzelm@404
     8
signature AX_CLASS =
wenzelm@3938
     9
sig
wenzelm@19243
    10
  val mk_classrel: class * class -> term
wenzelm@19243
    11
  val dest_classrel: term -> class * class
wenzelm@19243
    12
  val mk_arity: string * sort list * class -> term
wenzelm@19243
    13
  val mk_arities: string * sort list * sort -> term list
wenzelm@19243
    14
  val dest_arity: term -> string * sort list * class
wenzelm@6379
    15
  val print_axclasses: theory -> unit
wenzelm@19392
    16
  val get_info: theory -> class -> {super_classes: class list, intro: thm, axioms: thm list}
wenzelm@19392
    17
  val get_instances: theory ->
wenzelm@19392
    18
    {classrel: unit Graph.T, subsorts: ((sort * sort) * thm) list, arities: (arity * thm) list}
wenzelm@19243
    19
  val class_intros: theory -> thm list
wenzelm@17339
    20
  val add_axclass: bstring * xstring list -> ((bstring * string) * Attrib.src list) list ->
wenzelm@19392
    21
    theory -> class * theory
wenzelm@18728
    22
  val add_axclass_i: bstring * class list -> ((bstring * term) * attribute list) list ->
wenzelm@19392
    23
    theory -> class * theory
wenzelm@19243
    24
  val add_classrel: thm list -> theory -> theory
wenzelm@19243
    25
  val add_arity: thm list -> theory -> theory
wenzelm@19243
    26
  val prove_subclass: class * class -> tactic -> theory -> theory
wenzelm@19243
    27
  val prove_arity: string * sort list * sort -> tactic -> theory -> theory
wenzelm@3938
    28
end;
wenzelm@404
    29
wenzelm@15801
    30
structure AxClass: AX_CLASS =
wenzelm@404
    31
struct
wenzelm@404
    32
wenzelm@4015
    33
wenzelm@19243
    34
(** abstract syntax operations **)
wenzelm@404
    35
wenzelm@19243
    36
(* subclass propositions *)
wenzelm@423
    37
wenzelm@19392
    38
fun mk_classrel (c1, c2) = Logic.mk_inclass (Term.aT [c1], c2);
wenzelm@423
    39
wenzelm@423
    40
fun dest_classrel tm =
wenzelm@423
    41
  let
wenzelm@19243
    42
    fun err () = raise TERM ("AxClass.dest_classrel", [tm]);
wenzelm@423
    43
wenzelm@3395
    44
    val (ty, c2) = Logic.dest_inclass tm handle TERM _ => err ();
wenzelm@19392
    45
    val c1 = (case dest_TVar ty of (_, [c]) => c | _ => err ())
wenzelm@3395
    46
      handle TYPE _ => err ();
wenzelm@6379
    47
  in (c1, c2) end;
wenzelm@423
    48
wenzelm@423
    49
wenzelm@19243
    50
(* arity propositions *)
wenzelm@423
    51
wenzelm@14605
    52
fun mk_arity (t, Ss, c) =
wenzelm@423
    53
  let
wenzelm@14695
    54
    val tfrees = ListPair.map TFree (Term.invent_names [] "'a" (length Ss), Ss);
wenzelm@6379
    55
  in Logic.mk_inclass (Type (t, tfrees), c) end;
wenzelm@423
    56
wenzelm@14605
    57
fun mk_arities (t, Ss, S) = map (fn c => mk_arity (t, Ss, c)) S;
wenzelm@14605
    58
wenzelm@423
    59
fun dest_arity tm =
wenzelm@423
    60
  let
wenzelm@19243
    61
    fun err () = raise TERM ("AxClass.dest_arity", [tm]);
wenzelm@423
    62
wenzelm@3395
    63
    val (ty, c) = Logic.dest_inclass tm handle TERM _ => err ();
wenzelm@3395
    64
    val (t, tvars) =
wenzelm@423
    65
      (case ty of
wenzelm@19392
    66
        Type (t, tys) => (t, map dest_TVar tys handle TYPE _ => err ())
wenzelm@423
    67
      | _ => err ());
wenzelm@423
    68
    val ss =
wenzelm@19243
    69
      if has_duplicates (eq_fst (op =)) tvars then err ()
wenzelm@19243
    70
      else map snd tvars;
wenzelm@6379
    71
  in (t, ss, c) end;
wenzelm@423
    72
wenzelm@423
    73
wenzelm@423
    74
wenzelm@19392
    75
(** theory data **)
wenzelm@19392
    76
wenzelm@19392
    77
(* axclass *)
wenzelm@6379
    78
wenzelm@19243
    79
val introN = "intro";
wenzelm@19243
    80
val axiomsN = "axioms";
wenzelm@19243
    81
wenzelm@19392
    82
datatype axclass = AxClass of
wenzelm@19392
    83
 {super_classes: class list,
wenzelm@19392
    84
  intro: thm,
wenzelm@19392
    85
  axioms: thm list};
wenzelm@19392
    86
wenzelm@19392
    87
fun make_axclass (super_classes, intro, axioms) =
wenzelm@19392
    88
  AxClass {super_classes = super_classes, intro = intro, axioms = axioms};
wenzelm@19392
    89
wenzelm@19392
    90
wenzelm@19392
    91
(* instances *)
wenzelm@19392
    92
wenzelm@19392
    93
datatype instances = Instances of
wenzelm@19392
    94
 {classrel: unit Graph.T,                 (*raw relation -- no closure!*)
wenzelm@19392
    95
  subsorts: ((sort * sort) * thm) list,
wenzelm@19392
    96
  arities: (arity * thm) list};
wenzelm@19392
    97
wenzelm@19392
    98
fun make_instances (classrel, subsorts, arities) =
wenzelm@19392
    99
  Instances {classrel = classrel, subsorts = subsorts, arities = arities};
wenzelm@6379
   100
wenzelm@19392
   101
fun map_instances f (Instances {classrel, subsorts, arities}) =
wenzelm@19392
   102
  make_instances (f (classrel, subsorts, arities));
wenzelm@19392
   103
wenzelm@19392
   104
fun merge_instances
wenzelm@19392
   105
   (Instances {classrel = classrel1, subsorts = subsorts1, arities = arities1},
wenzelm@19392
   106
    Instances {classrel = classrel2, subsorts = subsorts2, arities = arities2}) =
wenzelm@19392
   107
  make_instances
wenzelm@19392
   108
   (Graph.merge (K true) (classrel1, classrel2),
wenzelm@19392
   109
    merge (eq_fst op =) (subsorts1, subsorts2),
wenzelm@19392
   110
    merge (eq_fst op =) (arities1, arities2));
wenzelm@19392
   111
wenzelm@19392
   112
wenzelm@19392
   113
(* setup data *)
wenzelm@19392
   114
wenzelm@19392
   115
structure AxClassData = TheoryDataFun
wenzelm@16458
   116
(struct
wenzelm@19392
   117
  val name = "Pure/axclass";
wenzelm@19392
   118
  type T = axclass Symtab.table * instances;
wenzelm@6379
   119
wenzelm@19392
   120
  val empty = (Symtab.empty, make_instances (Graph.empty, [], []));
wenzelm@6546
   121
  val copy = I;
wenzelm@16458
   122
  val extend = I;
wenzelm@6379
   123
wenzelm@19392
   124
  fun merge _ ((axclasses1, instances1), (axclasses2, instances2)) =
wenzelm@19392
   125
    (Symtab.merge (K true) (axclasses1, axclasses2), merge_instances (instances1, instances2));
wenzelm@19392
   126
wenzelm@19392
   127
  fun print thy (axclasses, _) =
wenzelm@6379
   128
    let
wenzelm@6379
   129
      fun pretty_class c cs = Pretty.block
wenzelm@16458
   130
        (Pretty.str (Sign.extern_class thy c) :: Pretty.str " <" :: Pretty.brk 1 ::
wenzelm@16458
   131
          Pretty.breaks (map (Pretty.str o Sign.extern_class thy) cs));
wenzelm@6379
   132
wenzelm@10008
   133
      fun pretty_thms name thms = Pretty.big_list (name ^ ":")
wenzelm@16458
   134
        (map (Display.pretty_thm_sg thy) thms);
wenzelm@6379
   135
wenzelm@19392
   136
      fun pretty_axclass (name, AxClass {super_classes, intro, axioms}) =
wenzelm@19392
   137
        Pretty.block (Pretty.fbreaks
wenzelm@19392
   138
          [pretty_class name super_classes, pretty_thms introN [intro], pretty_thms axiomsN axioms]);
wenzelm@19392
   139
    in Pretty.writeln (Pretty.chunks (map pretty_axclass (Symtab.dest axclasses))) end;
wenzelm@16458
   140
end);
wenzelm@6379
   141
wenzelm@19392
   142
val _ = Context.add_setup AxClassData.init;
wenzelm@19392
   143
val print_axclasses = AxClassData.print;
wenzelm@19392
   144
wenzelm@19392
   145
val get_instances = AxClassData.get #> (fn (_, Instances insts) => insts);
wenzelm@6379
   146
wenzelm@6379
   147
wenzelm@19392
   148
wenzelm@19392
   149
(** axclass definitions **)
wenzelm@19392
   150
wenzelm@19392
   151
(* lookup *)
wenzelm@19392
   152
wenzelm@19392
   153
val lookup_info = Symtab.lookup o #1 o AxClassData.get;
wenzelm@6379
   154
wenzelm@17281
   155
fun get_info thy c =
wenzelm@17281
   156
  (case lookup_info thy c of
wenzelm@19392
   157
    SOME (AxClass info) => info
wenzelm@19392
   158
  | NONE => error ("Unknown axclass " ^ quote c));
wenzelm@6379
   159
wenzelm@19123
   160
fun class_intros thy =
wenzelm@19392
   161
  let
wenzelm@19392
   162
    fun add_intro c =
wenzelm@19392
   163
      (case lookup_info thy c of SOME (AxClass {intro, ...}) => cons intro | _ => I);
wenzelm@19392
   164
    val classes = Sign.classes thy;
wenzelm@19392
   165
  in map (Thm.class_triv thy) classes @ fold add_intro classes [] end;
wenzelm@15876
   166
wenzelm@15876
   167
wenzelm@19392
   168
(* add_axclass(_i) *)
wenzelm@404
   169
wenzelm@15876
   170
local
wenzelm@404
   171
wenzelm@404
   172
fun err_bad_axsort ax c =
wenzelm@404
   173
  error ("Sort constraint in axiom " ^ quote ax ^ " not supersort of " ^ quote c);
wenzelm@404
   174
wenzelm@404
   175
fun err_bad_tfrees ax =
wenzelm@404
   176
  error ("More than one type variable in axiom " ^ quote ax);
wenzelm@404
   177
wenzelm@19243
   178
fun replace_tfree T = map_term_types (Term.map_atyps (fn TFree _ => T | U => U));
wenzelm@19243
   179
wenzelm@19243
   180
fun gen_axclass prep_class prep_axm prep_att
wenzelm@19243
   181
    (bclass, raw_super_classes) raw_axioms_atts thy =
wenzelm@404
   182
  let
wenzelm@16458
   183
    val class = Sign.full_name thy bclass;
wenzelm@16458
   184
    val super_classes = map (prep_class thy) raw_super_classes;
wenzelm@16458
   185
    val axms = map (prep_axm thy o fst) raw_axioms_atts;
wenzelm@6379
   186
    val atts = map (map (prep_att thy) o snd) raw_axioms_atts;
wenzelm@404
   187
wenzelm@6379
   188
    (*declare class*)
wenzelm@6379
   189
    val class_thy =
wenzelm@6379
   190
      thy |> Theory.add_classes_i [(bclass, super_classes)];
wenzelm@404
   191
wenzelm@6379
   192
    (*prepare abstract axioms*)
wenzelm@404
   193
    fun abs_axm ax =
wenzelm@404
   194
      if null (term_tfrees ax) then
wenzelm@19392
   195
        Logic.mk_implies (Logic.mk_inclass (Term.aT [], class), ax)
wenzelm@19392
   196
      else replace_tfree (Term.aT [class]) ax;
wenzelm@17756
   197
    val abs_axms = map (abs_axm o snd) axms;
wenzelm@404
   198
wenzelm@404
   199
    fun axm_sort (name, ax) =
wenzelm@404
   200
      (case term_tfrees ax of
wenzelm@404
   201
        [] => []
wenzelm@16458
   202
      | [(_, S)] => if Sign.subsort class_thy ([class], S) then S else err_bad_axsort name class
wenzelm@404
   203
      | _ => err_bad_tfrees name);
wenzelm@16458
   204
    val axS = Sign.certify_sort class_thy (List.concat (map axm_sort axms));
wenzelm@404
   205
wenzelm@19392
   206
    val int_axm = Logic.close_form o replace_tfree (Term.aT axS);
wenzelm@19392
   207
    fun inclass c = Logic.mk_inclass (Term.aT axS, c);
wenzelm@404
   208
paulson@1498
   209
    val intro_axm = Logic.list_implies
wenzelm@6379
   210
      (map inclass super_classes @ map (int_axm o #2) axms, inclass class);
wenzelm@6379
   211
wenzelm@6379
   212
    (*declare axioms and rule*)
haftmann@18377
   213
    val (([intro], [axioms]), axms_thy) =
wenzelm@6379
   214
      class_thy
wenzelm@18932
   215
      |> Theory.add_path (Logic.const_of_class bclass)
wenzelm@6379
   216
      |> PureThy.add_axioms_i [Thm.no_attributes (introN, intro_axm)]
haftmann@18377
   217
      ||>> PureThy.add_axiomss_i [Thm.no_attributes (axiomsN, abs_axms)];
wenzelm@19392
   218
    val info = make_axclass (super_classes, intro, axioms);
wenzelm@6379
   219
wenzelm@6379
   220
    (*store info*)
haftmann@18377
   221
    val (_, final_thy) =
wenzelm@6379
   222
      axms_thy
wenzelm@19392
   223
      |> Theory.add_finals_i false [Const (Logic.const_of_class class, Term.a_itselfT --> propT)]
haftmann@18377
   224
      |> PureThy.add_thms ((map #1 axms ~~ axioms) ~~ atts)
haftmann@18377
   225
      ||> Theory.restore_naming class_thy
wenzelm@19392
   226
      ||> AxClassData.map (apfst (Symtab.update (class, info)));
wenzelm@19392
   227
  in (class, final_thy) end;
wenzelm@404
   228
wenzelm@15876
   229
in
wenzelm@404
   230
wenzelm@19243
   231
val add_axclass = gen_axclass Sign.intern_class Theory.read_axm Attrib.attribute;
wenzelm@19243
   232
val add_axclass_i = gen_axclass (K I) Theory.cert_axm (K I);
wenzelm@404
   233
wenzelm@15876
   234
end;
wenzelm@15876
   235
wenzelm@15876
   236
wenzelm@15876
   237
wenzelm@19392
   238
(** instantiation proofs **)
wenzelm@15876
   239
wenzelm@19392
   240
(* primitives *)
wenzelm@15876
   241
wenzelm@19243
   242
fun add_classrel ths thy =
wenzelm@15876
   243
  let
wenzelm@19392
   244
    fun add_rel (c1, c2) =
wenzelm@19392
   245
      Graph.default_node (c1, ()) #> Graph.default_node (c2, ()) #> Graph.add_edge (c1, c2);
wenzelm@19392
   246
    val rels = ths |> map (fn th =>
wenzelm@15876
   247
      let
wenzelm@19243
   248
        val prop = Drule.plain_prop_of (Thm.transfer thy th);
wenzelm@15876
   249
        val (c1, c2) = dest_classrel prop handle TERM _ =>
wenzelm@19243
   250
          raise THM ("AxClass.add_classrel: not a class relation", 0, [th]);
wenzelm@19392
   251
      in (c1, c2) end);
wenzelm@19392
   252
  in
wenzelm@19392
   253
    thy
wenzelm@19392
   254
    |> Theory.add_classrel_i rels
wenzelm@19392
   255
    |> AxClassData.map (apsnd (map_instances (fn (classrel, subsorts, arities) =>
wenzelm@19392
   256
        (classrel |> fold add_rel rels, (map (pairself single) rels ~~ ths) @ subsorts, arities))))
wenzelm@19392
   257
  end;
wenzelm@15876
   258
wenzelm@19243
   259
fun add_arity ths thy =
wenzelm@15876
   260
  let
wenzelm@19392
   261
    val ars = ths |> map (fn th =>
wenzelm@15876
   262
      let
wenzelm@19243
   263
        val prop = Drule.plain_prop_of (Thm.transfer thy th);
wenzelm@15876
   264
        val (t, ss, c) = dest_arity prop handle TERM _ =>
wenzelm@19243
   265
          raise THM ("AxClass.add_arity: not a type arity", 0, [th]);
wenzelm@19392
   266
      in (t, ss, [c]) end);
wenzelm@19392
   267
  in
wenzelm@19392
   268
    thy
wenzelm@19392
   269
    |> Theory.add_arities_i ars
wenzelm@19392
   270
    |> AxClassData.map (apsnd (map_instances (fn (classrel, subsorts, arities) =>
wenzelm@19392
   271
      (classrel, subsorts, (ars ~~ ths) @ arities))))
wenzelm@19392
   272
  end;
wenzelm@404
   273
wenzelm@404
   274
wenzelm@19243
   275
(* tactical proofs *)
wenzelm@15876
   276
wenzelm@19243
   277
fun prove_subclass raw_rel tac thy =
wenzelm@15876
   278
  let
wenzelm@19243
   279
    val (c1, c2) = Sign.cert_classrel thy raw_rel;
wenzelm@19243
   280
    val th = Goal.prove thy [] [] (mk_classrel (c1, c2)) (fn _ => tac) handle ERROR msg =>
wenzelm@19243
   281
      cat_error msg ("The error(s) above occurred while trying to prove class relation " ^
wenzelm@19243
   282
        quote (Sign.string_of_classrel thy [c1, c2]));
wenzelm@19243
   283
  in add_classrel [th] thy end;
wenzelm@404
   284
wenzelm@19243
   285
fun prove_arity raw_arity tac thy =
wenzelm@15876
   286
  let
wenzelm@19243
   287
    val arity = Sign.cert_arity thy raw_arity;
wenzelm@19243
   288
    val props = mk_arities arity;
wenzelm@17956
   289
    val ths = Goal.prove_multi thy [] [] props
wenzelm@18678
   290
      (fn _ => Tactic.precise_conjunction_tac (length props) 1 THEN tac) handle ERROR msg =>
wenzelm@19243
   291
        cat_error msg ("The error(s) above occurred while trying to prove type arity " ^
wenzelm@19243
   292
          quote (Sign.string_of_arity thy arity));
wenzelm@19243
   293
  in add_arity ths thy end;
wenzelm@15876
   294
wenzelm@19392
   295
wenzelm@15876
   296
end;