src/Pure/axclass.ML
author wenzelm
Sat Mar 11 16:53:20 2006 +0100 (2006-03-11 ago)
changeset 19243 5dcb899a8486
parent 19134 47d337aefc21
child 19392 a631cd2117a8
permissions -rw-r--r--
moved read_class, read/cert_classrel/arity to sign.ML;
axclass: moved outer syntax to isar_syn.ML;
instance: moved to Tools/class_package.ML;
simplified interfaces;
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@17281
    16
  val get_info: theory -> string -> {super_classes: class list, intro: thm, axioms: thm list}
wenzelm@19243
    17
  val class_intros: theory -> thm list
wenzelm@17339
    18
  val add_axclass: bstring * xstring list -> ((bstring * string) * Attrib.src list) list ->
haftmann@18418
    19
    theory -> {intro: thm, axioms: thm list} * theory
wenzelm@18728
    20
  val add_axclass_i: bstring * class list -> ((bstring * term) * attribute list) list ->
haftmann@18418
    21
    theory -> {intro: thm, axioms: thm list} * theory
wenzelm@19243
    22
  val add_classrel: thm list -> theory -> theory
wenzelm@19243
    23
  val add_arity: thm list -> theory -> theory
wenzelm@19243
    24
  val prove_subclass: class * class -> tactic -> theory -> theory
wenzelm@19243
    25
  val prove_arity: string * sort list * sort -> tactic -> theory -> theory
wenzelm@3938
    26
end;
wenzelm@404
    27
wenzelm@15801
    28
structure AxClass: AX_CLASS =
wenzelm@404
    29
struct
wenzelm@404
    30
wenzelm@4015
    31
wenzelm@19243
    32
(** abstract syntax operations **)
wenzelm@404
    33
wenzelm@404
    34
fun aT S = TFree ("'a", S);
wenzelm@404
    35
wenzelm@3395
    36
fun dest_varT (TFree (x, S)) = ((x, ~1), S)
wenzelm@19243
    37
  | dest_varT (TVar a) = a
wenzelm@19243
    38
  | dest_varT T = raise TYPE ("AxClass.dest_varT", [T], []);
wenzelm@404
    39
wenzelm@404
    40
wenzelm@19243
    41
(* subclass propositions *)
wenzelm@423
    42
paulson@1498
    43
fun mk_classrel (c1, c2) = Logic.mk_inclass (aT [c1], c2);
wenzelm@423
    44
wenzelm@423
    45
fun dest_classrel tm =
wenzelm@423
    46
  let
wenzelm@19243
    47
    fun err () = raise TERM ("AxClass.dest_classrel", [tm]);
wenzelm@423
    48
wenzelm@3395
    49
    val (ty, c2) = Logic.dest_inclass tm handle TERM _ => err ();
wenzelm@3395
    50
    val c1 = (case dest_varT ty of (_, [c]) => c | _ => err ())
wenzelm@3395
    51
      handle TYPE _ => err ();
wenzelm@6379
    52
  in (c1, c2) end;
wenzelm@423
    53
wenzelm@423
    54
wenzelm@19243
    55
(* arity propositions *)
wenzelm@423
    56
wenzelm@14605
    57
fun mk_arity (t, Ss, c) =
wenzelm@423
    58
  let
wenzelm@14695
    59
    val tfrees = ListPair.map TFree (Term.invent_names [] "'a" (length Ss), Ss);
wenzelm@6379
    60
  in Logic.mk_inclass (Type (t, tfrees), c) end;
wenzelm@423
    61
wenzelm@14605
    62
fun mk_arities (t, Ss, S) = map (fn c => mk_arity (t, Ss, c)) S;
wenzelm@14605
    63
wenzelm@423
    64
fun dest_arity tm =
wenzelm@423
    65
  let
wenzelm@19243
    66
    fun err () = raise TERM ("AxClass.dest_arity", [tm]);
wenzelm@423
    67
wenzelm@3395
    68
    val (ty, c) = Logic.dest_inclass tm handle TERM _ => err ();
wenzelm@3395
    69
    val (t, tvars) =
wenzelm@423
    70
      (case ty of
wenzelm@3395
    71
        Type (t, tys) => (t, map dest_varT tys handle TYPE _ => err ())
wenzelm@423
    72
      | _ => err ());
wenzelm@423
    73
    val ss =
wenzelm@19243
    74
      if has_duplicates (eq_fst (op =)) tvars then err ()
wenzelm@19243
    75
      else map snd tvars;
wenzelm@6379
    76
  in (t, ss, c) end;
wenzelm@423
    77
wenzelm@423
    78
wenzelm@423
    79
wenzelm@6379
    80
(** axclass info **)
wenzelm@6379
    81
wenzelm@19243
    82
val introN = "intro";
wenzelm@19243
    83
val axiomsN = "axioms";
wenzelm@19243
    84
wenzelm@6379
    85
type axclass_info =
wenzelm@6379
    86
  {super_classes: class list,
wenzelm@6379
    87
    intro: thm,
wenzelm@6379
    88
    axioms: thm list};
wenzelm@6379
    89
wenzelm@16458
    90
structure AxclassesData = TheoryDataFun
wenzelm@16458
    91
(struct
wenzelm@6379
    92
  val name = "Pure/axclasses";
wenzelm@6379
    93
  type T = axclass_info Symtab.table;
wenzelm@6379
    94
wenzelm@6379
    95
  val empty = Symtab.empty;
wenzelm@6546
    96
  val copy = I;
wenzelm@16458
    97
  val extend = I;
wenzelm@16458
    98
  fun merge _ = Symtab.merge (K true);
wenzelm@6379
    99
wenzelm@16458
   100
  fun print thy tab =
wenzelm@6379
   101
    let
wenzelm@6379
   102
      fun pretty_class c cs = Pretty.block
wenzelm@16458
   103
        (Pretty.str (Sign.extern_class thy c) :: Pretty.str " <" :: Pretty.brk 1 ::
wenzelm@16458
   104
          Pretty.breaks (map (Pretty.str o Sign.extern_class thy) cs));
wenzelm@6379
   105
wenzelm@10008
   106
      fun pretty_thms name thms = Pretty.big_list (name ^ ":")
wenzelm@16458
   107
        (map (Display.pretty_thm_sg thy) thms);
wenzelm@6379
   108
wenzelm@6379
   109
      fun pretty_axclass (name, {super_classes, intro, axioms}) = Pretty.block (Pretty.fbreaks
wenzelm@6379
   110
        [pretty_class name super_classes, pretty_thms introN [intro], pretty_thms axiomsN axioms]);
wenzelm@8720
   111
    in Pretty.writeln (Pretty.chunks (map pretty_axclass (Symtab.dest tab))) end;
wenzelm@16458
   112
end);
wenzelm@6379
   113
wenzelm@18708
   114
val _ = Context.add_setup AxclassesData.init;
wenzelm@6379
   115
val print_axclasses = AxclassesData.print;
wenzelm@6379
   116
wenzelm@6379
   117
wenzelm@17412
   118
val lookup_info = Symtab.lookup o AxclassesData.get;
wenzelm@6379
   119
wenzelm@17281
   120
fun get_info thy c =
wenzelm@17281
   121
  (case lookup_info thy c of
skalberg@15531
   122
    NONE => error ("Unknown axclass " ^ quote c)
skalberg@15531
   123
  | SOME info => info);
wenzelm@6379
   124
wenzelm@19123
   125
fun class_intros thy =
wenzelm@16458
   126
  let val classes = Sign.classes thy in
wenzelm@16458
   127
    map (Thm.class_triv thy) classes @
wenzelm@17281
   128
    List.mapPartial (Option.map #intro o lookup_info thy) classes
wenzelm@15876
   129
  end;
wenzelm@15876
   130
wenzelm@15876
   131
wenzelm@423
   132
wenzelm@423
   133
(** add axiomatic type classes **)
wenzelm@404
   134
wenzelm@15876
   135
local
wenzelm@404
   136
wenzelm@404
   137
fun err_bad_axsort ax c =
wenzelm@404
   138
  error ("Sort constraint in axiom " ^ quote ax ^ " not supersort of " ^ quote c);
wenzelm@404
   139
wenzelm@404
   140
fun err_bad_tfrees ax =
wenzelm@404
   141
  error ("More than one type variable in axiom " ^ quote ax);
wenzelm@404
   142
wenzelm@19243
   143
fun replace_tfree T = map_term_types (Term.map_atyps (fn TFree _ => T | U => U));
wenzelm@19243
   144
wenzelm@19243
   145
fun gen_axclass prep_class prep_axm prep_att
wenzelm@19243
   146
    (bclass, raw_super_classes) raw_axioms_atts thy =
wenzelm@404
   147
  let
wenzelm@16458
   148
    val class = Sign.full_name thy bclass;
wenzelm@16458
   149
    val super_classes = map (prep_class thy) raw_super_classes;
wenzelm@16458
   150
    val axms = map (prep_axm thy o fst) raw_axioms_atts;
wenzelm@6379
   151
    val atts = map (map (prep_att thy) o snd) raw_axioms_atts;
wenzelm@404
   152
wenzelm@6379
   153
    (*declare class*)
wenzelm@6379
   154
    val class_thy =
wenzelm@6379
   155
      thy |> Theory.add_classes_i [(bclass, super_classes)];
wenzelm@404
   156
wenzelm@6379
   157
    (*prepare abstract axioms*)
wenzelm@404
   158
    fun abs_axm ax =
wenzelm@404
   159
      if null (term_tfrees ax) then
wenzelm@14854
   160
        Logic.mk_implies (Logic.mk_inclass (aT [], class), ax)
wenzelm@19243
   161
      else replace_tfree (aT [class]) ax;
wenzelm@17756
   162
    val abs_axms = map (abs_axm o snd) axms;
wenzelm@404
   163
wenzelm@404
   164
    fun axm_sort (name, ax) =
wenzelm@404
   165
      (case term_tfrees ax of
wenzelm@404
   166
        [] => []
wenzelm@16458
   167
      | [(_, S)] => if Sign.subsort class_thy ([class], S) then S else err_bad_axsort name class
wenzelm@404
   168
      | _ => err_bad_tfrees name);
wenzelm@16458
   169
    val axS = Sign.certify_sort class_thy (List.concat (map axm_sort axms));
wenzelm@404
   170
wenzelm@19243
   171
    val int_axm = Logic.close_form o replace_tfree (aT axS);
paulson@1498
   172
    fun inclass c = Logic.mk_inclass (aT axS, c);
wenzelm@404
   173
paulson@1498
   174
    val intro_axm = Logic.list_implies
wenzelm@6379
   175
      (map inclass super_classes @ map (int_axm o #2) axms, inclass class);
wenzelm@6379
   176
wenzelm@6379
   177
    (*declare axioms and rule*)
haftmann@18377
   178
    val (([intro], [axioms]), axms_thy) =
wenzelm@6379
   179
      class_thy
wenzelm@18932
   180
      |> Theory.add_path (Logic.const_of_class bclass)
wenzelm@6379
   181
      |> PureThy.add_axioms_i [Thm.no_attributes (introN, intro_axm)]
haftmann@18377
   182
      ||>> PureThy.add_axiomss_i [Thm.no_attributes (axiomsN, abs_axms)];
wenzelm@6379
   183
    val info = {super_classes = super_classes, intro = intro, axioms = axioms};
wenzelm@6379
   184
wenzelm@6379
   185
    (*store info*)
haftmann@18377
   186
    val (_, final_thy) =
wenzelm@6379
   187
      axms_thy
wenzelm@18932
   188
      |> Theory.add_finals_i false [Const (Logic.const_of_class class, a_itselfT --> propT)]
haftmann@18377
   189
      |> PureThy.add_thms ((map #1 axms ~~ axioms) ~~ atts)
haftmann@18377
   190
      ||> Theory.restore_naming class_thy
haftmann@18377
   191
      ||> AxclassesData.map (Symtab.update (class, info));
haftmann@18418
   192
  in ({intro = intro, axioms = axioms}, final_thy) end;
wenzelm@404
   193
wenzelm@15876
   194
in
wenzelm@404
   195
wenzelm@19243
   196
val add_axclass = gen_axclass Sign.intern_class Theory.read_axm Attrib.attribute;
wenzelm@19243
   197
val add_axclass_i = gen_axclass (K I) Theory.cert_axm (K I);
wenzelm@404
   198
wenzelm@15876
   199
end;
wenzelm@15876
   200
wenzelm@15876
   201
wenzelm@15876
   202
wenzelm@15876
   203
(** proven class instantiation **)
wenzelm@15876
   204
wenzelm@19243
   205
(* primitive rules *)
wenzelm@15876
   206
wenzelm@19243
   207
fun add_classrel ths thy =
wenzelm@15876
   208
  let
wenzelm@19243
   209
    fun prep_thm th =
wenzelm@15876
   210
      let
wenzelm@19243
   211
        val prop = Drule.plain_prop_of (Thm.transfer thy th);
wenzelm@15876
   212
        val (c1, c2) = dest_classrel prop handle TERM _ =>
wenzelm@19243
   213
          raise THM ("AxClass.add_classrel: not a class relation", 0, [th]);
wenzelm@15876
   214
      in (c1, c2) end;
wenzelm@19243
   215
  in Theory.add_classrel_i (map prep_thm ths) thy end;
wenzelm@15876
   216
wenzelm@19243
   217
fun add_arity ths thy =
wenzelm@15876
   218
  let
wenzelm@19243
   219
    fun prep_thm th =
wenzelm@15876
   220
      let
wenzelm@19243
   221
        val prop = Drule.plain_prop_of (Thm.transfer thy th);
wenzelm@15876
   222
        val (t, ss, c) = dest_arity prop handle TERM _ =>
wenzelm@19243
   223
          raise THM ("AxClass.add_arity: not a type arity", 0, [th]);
wenzelm@15876
   224
      in (t, ss, [c]) end;
wenzelm@19243
   225
  in Theory.add_arities_i (map prep_thm ths) thy end;
wenzelm@404
   226
wenzelm@404
   227
wenzelm@19243
   228
(* tactical proofs *)
wenzelm@15876
   229
wenzelm@19243
   230
fun prove_subclass raw_rel tac thy =
wenzelm@15876
   231
  let
wenzelm@19243
   232
    val (c1, c2) = Sign.cert_classrel thy raw_rel;
wenzelm@19243
   233
    val th = Goal.prove thy [] [] (mk_classrel (c1, c2)) (fn _ => tac) handle ERROR msg =>
wenzelm@19243
   234
      cat_error msg ("The error(s) above occurred while trying to prove class relation " ^
wenzelm@19243
   235
        quote (Sign.string_of_classrel thy [c1, c2]));
wenzelm@19243
   236
  in add_classrel [th] thy end;
wenzelm@404
   237
wenzelm@19243
   238
fun prove_arity raw_arity tac thy =
wenzelm@15876
   239
  let
wenzelm@19243
   240
    val arity = Sign.cert_arity thy raw_arity;
wenzelm@19243
   241
    val props = mk_arities arity;
wenzelm@17956
   242
    val ths = Goal.prove_multi thy [] [] props
wenzelm@18678
   243
      (fn _ => Tactic.precise_conjunction_tac (length props) 1 THEN tac) handle ERROR msg =>
wenzelm@19243
   244
        cat_error msg ("The error(s) above occurred while trying to prove type arity " ^
wenzelm@19243
   245
          quote (Sign.string_of_arity thy arity));
wenzelm@19243
   246
  in add_arity ths thy end;
wenzelm@15876
   247
wenzelm@15876
   248
end;