src/Pure/axclass.ML
author wenzelm
Tue Apr 11 16:00:01 2006 +0200 (2006-04-11)
changeset 19405 a551256aba15
parent 19398 8ad34412ea97
child 19418 03b01c9314fc
permissions -rw-r--r--
moved abstract syntax operations to logic.ML;
maintain class parameters;
added params_of_sort;
added cert/read_classrel (from sign.ML), check class parameters;
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@19405
     5
Axiomatic type classes.
wenzelm@404
     6
*)
wenzelm@404
     7
wenzelm@404
     8
signature AX_CLASS =
wenzelm@3938
     9
sig
wenzelm@6379
    10
  val print_axclasses: theory -> unit
wenzelm@19392
    11
  val get_info: theory -> class -> {super_classes: class list, intro: thm, axioms: thm list}
wenzelm@19392
    12
  val get_instances: theory ->
wenzelm@19405
    13
   {classes: unit Graph.T,
wenzelm@19405
    14
    classrel: ((class * class) * thm) list,
wenzelm@19405
    15
    arities: ((string * sort list * class) * thm) list}
wenzelm@19243
    16
  val class_intros: theory -> thm list
wenzelm@19405
    17
  val add_axclass: bstring * xstring list -> string list ->
wenzelm@19405
    18
    ((bstring * string) * Attrib.src list) list -> theory -> class * theory
wenzelm@19405
    19
  val add_axclass_i: bstring * class list -> string list ->
wenzelm@19405
    20
    ((bstring * term) * attribute list) list -> theory -> class * theory
wenzelm@19405
    21
  val params_of_sort: theory -> sort -> string list
wenzelm@19405
    22
  val cert_classrel: theory -> class * class -> class * class
wenzelm@19405
    23
  val read_classrel: theory -> xstring * xstring -> class * class
wenzelm@19405
    24
  val add_classrel: thm -> theory -> theory
wenzelm@19405
    25
  val add_arity: thm -> theory -> theory
wenzelm@19405
    26
  val prove_classrel: 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@19405
    33
(** theory data **)
wenzelm@423
    34
wenzelm@19405
    35
(* class parameters (canonical order) *)
wenzelm@423
    36
wenzelm@19405
    37
type param = string * class;
wenzelm@423
    38
wenzelm@19405
    39
fun add_param pp ((x, c): param) params =
wenzelm@19405
    40
  (case AList.lookup (op =) params x of
wenzelm@19405
    41
    NONE => (x, c) :: params
wenzelm@19405
    42
  | SOME c' => error ("Duplicate class parameter " ^ quote x ^
wenzelm@19405
    43
      " for " ^ Pretty.string_of_sort pp [c] ^
wenzelm@19405
    44
      (if c = c' then "" else " and " ^ Pretty.string_of_sort pp [c'])));
wenzelm@423
    45
wenzelm@19405
    46
fun merge_params _ ([], qs) = qs
wenzelm@19405
    47
  | merge_params pp (ps, qs) =
wenzelm@19405
    48
      fold_rev (fn q => if member (op =) ps q then I else add_param pp q) qs ps;
wenzelm@423
    49
wenzelm@423
    50
wenzelm@19392
    51
(* axclass *)
wenzelm@6379
    52
wenzelm@19243
    53
val introN = "intro";
wenzelm@19243
    54
val axiomsN = "axioms";
wenzelm@19243
    55
wenzelm@19392
    56
datatype axclass = AxClass of
wenzelm@19392
    57
 {super_classes: class list,
wenzelm@19392
    58
  intro: thm,
wenzelm@19392
    59
  axioms: thm list};
wenzelm@19392
    60
wenzelm@19392
    61
fun make_axclass (super_classes, intro, axioms) =
wenzelm@19392
    62
  AxClass {super_classes = super_classes, intro = intro, axioms = axioms};
wenzelm@19392
    63
wenzelm@19405
    64
type axclasses = axclass Symtab.table * param list;
wenzelm@19405
    65
wenzelm@19405
    66
fun merge_axclasses pp ((tab1, params1), (tab2, params2)) : axclasses =
wenzelm@19405
    67
  (Symtab.merge (K true) (tab1, tab2), merge_params pp (params1, params2));
wenzelm@19405
    68
wenzelm@19392
    69
wenzelm@19392
    70
(* instances *)
wenzelm@19392
    71
wenzelm@19392
    72
datatype instances = Instances of
wenzelm@19405
    73
 {classes: unit Graph.T,                 (*raw relation -- no closure!*)
wenzelm@19405
    74
  classrel: ((class * class) * thm) list,
wenzelm@19405
    75
  arities: ((string * sort list * class) * thm) list};
wenzelm@19392
    76
wenzelm@19405
    77
fun make_instances (classes, classrel, arities) =
wenzelm@19405
    78
  Instances {classes = classes, classrel = classrel, arities = arities};
wenzelm@6379
    79
wenzelm@19405
    80
fun map_instances f (Instances {classes, classrel, arities}) =
wenzelm@19405
    81
  make_instances (f (classes, classrel, arities));
wenzelm@19392
    82
wenzelm@19392
    83
fun merge_instances
wenzelm@19405
    84
   (Instances {classes = classes1, classrel = classrel1, arities = arities1},
wenzelm@19405
    85
    Instances {classes = classes2, classrel = classrel2, arities = arities2}) =
wenzelm@19392
    86
  make_instances
wenzelm@19405
    87
   (Graph.merge (K true) (classes1, classes2),
wenzelm@19405
    88
    merge (eq_fst op =) (classrel1, classrel2),
wenzelm@19392
    89
    merge (eq_fst op =) (arities1, arities2));
wenzelm@19392
    90
wenzelm@19392
    91
wenzelm@19405
    92
(* data *)
wenzelm@19392
    93
wenzelm@19392
    94
structure AxClassData = TheoryDataFun
wenzelm@16458
    95
(struct
wenzelm@19392
    96
  val name = "Pure/axclass";
wenzelm@19405
    97
  type T = axclasses * instances;
wenzelm@19405
    98
  val empty : T = ((Symtab.empty, []), make_instances (Graph.empty, [], []));
wenzelm@6546
    99
  val copy = I;
wenzelm@16458
   100
  val extend = I;
wenzelm@6379
   101
wenzelm@19405
   102
  fun merge pp ((axclasses1, instances1), (axclasses2, instances2)) =
wenzelm@19405
   103
    (merge_axclasses pp (axclasses1, axclasses2), merge_instances (instances1, instances2));
wenzelm@19392
   104
wenzelm@19405
   105
  fun print thy ((axclasses, params), _) =
wenzelm@6379
   106
    let
wenzelm@19405
   107
      val ctxt = ProofContext.init thy;
wenzelm@19405
   108
      val prt_cls = ProofContext.pretty_sort ctxt o single;
wenzelm@19405
   109
wenzelm@19405
   110
      fun pretty_class c [] = prt_cls c
wenzelm@19405
   111
        | pretty_class c cs = Pretty.block
wenzelm@19405
   112
            (prt_cls c :: Pretty.str " <" :: Pretty.brk 1 :: Pretty.commas (map prt_cls cs));
wenzelm@6379
   113
wenzelm@19405
   114
      fun pretty_axclass (class, AxClass {super_classes, intro, axioms}) =
wenzelm@19392
   115
        Pretty.block (Pretty.fbreaks
wenzelm@19405
   116
         [pretty_class class super_classes,
wenzelm@19405
   117
          Pretty.strs ("parameters:" ::
wenzelm@19405
   118
            fold (fn (x, c) => if c = class then cons x else I) params []),
wenzelm@19405
   119
          ProofContext.pretty_fact ctxt (introN, [intro]),
wenzelm@19405
   120
          ProofContext.pretty_fact ctxt (axiomsN, axioms)]);
wenzelm@19405
   121
    in
wenzelm@19405
   122
      Pretty.writeln (Pretty.chunks (map pretty_axclass (Symtab.dest axclasses)))
wenzelm@19405
   123
    end;
wenzelm@16458
   124
end);
wenzelm@6379
   125
wenzelm@19392
   126
val _ = Context.add_setup AxClassData.init;
wenzelm@19392
   127
val print_axclasses = AxClassData.print;
wenzelm@19392
   128
wenzelm@19392
   129
val get_instances = AxClassData.get #> (fn (_, Instances insts) => insts);
wenzelm@6379
   130
wenzelm@6379
   131
wenzelm@19392
   132
wenzelm@19392
   133
(** axclass definitions **)
wenzelm@19392
   134
wenzelm@19392
   135
(* lookup *)
wenzelm@19392
   136
wenzelm@19405
   137
val lookup_info = Symtab.lookup o #1 o #1 o AxClassData.get;
wenzelm@6379
   138
wenzelm@17281
   139
fun get_info thy c =
wenzelm@17281
   140
  (case lookup_info thy c of
wenzelm@19392
   141
    SOME (AxClass info) => info
wenzelm@19392
   142
  | NONE => error ("Unknown axclass " ^ quote c));
wenzelm@6379
   143
wenzelm@19123
   144
fun class_intros thy =
wenzelm@19392
   145
  let
wenzelm@19392
   146
    fun add_intro c =
wenzelm@19392
   147
      (case lookup_info thy c of SOME (AxClass {intro, ...}) => cons intro | _ => I);
wenzelm@19392
   148
    val classes = Sign.classes thy;
wenzelm@19392
   149
  in map (Thm.class_triv thy) classes @ fold add_intro classes [] end;
wenzelm@15876
   150
wenzelm@15876
   151
wenzelm@19392
   152
(* add_axclass(_i) *)
wenzelm@404
   153
wenzelm@15876
   154
local
wenzelm@404
   155
wenzelm@404
   156
fun err_bad_axsort ax c =
wenzelm@404
   157
  error ("Sort constraint in axiom " ^ quote ax ^ " not supersort of " ^ quote c);
wenzelm@404
   158
wenzelm@404
   159
fun err_bad_tfrees ax =
wenzelm@404
   160
  error ("More than one type variable in axiom " ^ quote ax);
wenzelm@404
   161
wenzelm@19243
   162
fun replace_tfree T = map_term_types (Term.map_atyps (fn TFree _ => T | U => U));
wenzelm@19243
   163
wenzelm@19243
   164
fun gen_axclass prep_class prep_axm prep_att
wenzelm@19405
   165
    (bclass, raw_super_classes) params raw_axioms_atts thy =
wenzelm@404
   166
  let
wenzelm@19405
   167
    val pp = Sign.pp thy;
wenzelm@19405
   168
wenzelm@16458
   169
    val class = Sign.full_name thy bclass;
wenzelm@16458
   170
    val super_classes = map (prep_class thy) raw_super_classes;
wenzelm@19405
   171
wenzelm@16458
   172
    val axms = map (prep_axm thy o fst) raw_axioms_atts;
wenzelm@6379
   173
    val atts = map (map (prep_att thy) o snd) raw_axioms_atts;
wenzelm@404
   174
wenzelm@6379
   175
    (*declare class*)
wenzelm@6379
   176
    val class_thy =
wenzelm@6379
   177
      thy |> Theory.add_classes_i [(bclass, super_classes)];
wenzelm@404
   178
wenzelm@6379
   179
    (*prepare abstract axioms*)
wenzelm@404
   180
    fun abs_axm ax =
wenzelm@404
   181
      if null (term_tfrees ax) then
wenzelm@19392
   182
        Logic.mk_implies (Logic.mk_inclass (Term.aT [], class), ax)
wenzelm@19392
   183
      else replace_tfree (Term.aT [class]) ax;
wenzelm@17756
   184
    val abs_axms = map (abs_axm o snd) axms;
wenzelm@404
   185
wenzelm@404
   186
    fun axm_sort (name, ax) =
wenzelm@404
   187
      (case term_tfrees ax of
wenzelm@404
   188
        [] => []
wenzelm@16458
   189
      | [(_, S)] => if Sign.subsort class_thy ([class], S) then S else err_bad_axsort name class
wenzelm@404
   190
      | _ => err_bad_tfrees name);
wenzelm@16458
   191
    val axS = Sign.certify_sort class_thy (List.concat (map axm_sort axms));
wenzelm@404
   192
wenzelm@19392
   193
    val int_axm = Logic.close_form o replace_tfree (Term.aT axS);
wenzelm@19392
   194
    fun inclass c = Logic.mk_inclass (Term.aT axS, c);
wenzelm@404
   195
paulson@1498
   196
    val intro_axm = Logic.list_implies
wenzelm@6379
   197
      (map inclass super_classes @ map (int_axm o #2) axms, inclass class);
wenzelm@6379
   198
wenzelm@6379
   199
    (*declare axioms and rule*)
haftmann@18377
   200
    val (([intro], [axioms]), axms_thy) =
wenzelm@6379
   201
      class_thy
wenzelm@18932
   202
      |> Theory.add_path (Logic.const_of_class bclass)
wenzelm@6379
   203
      |> PureThy.add_axioms_i [Thm.no_attributes (introN, intro_axm)]
haftmann@18377
   204
      ||>> PureThy.add_axiomss_i [Thm.no_attributes (axiomsN, abs_axms)];
wenzelm@19392
   205
    val info = make_axclass (super_classes, intro, axioms);
wenzelm@6379
   206
wenzelm@6379
   207
    (*store info*)
haftmann@18377
   208
    val (_, final_thy) =
wenzelm@6379
   209
      axms_thy
wenzelm@19392
   210
      |> Theory.add_finals_i false [Const (Logic.const_of_class class, Term.a_itselfT --> propT)]
haftmann@18377
   211
      |> PureThy.add_thms ((map #1 axms ~~ axioms) ~~ atts)
haftmann@18377
   212
      ||> Theory.restore_naming class_thy
wenzelm@19405
   213
      ||> AxClassData.map (apfst (fn (is, ps) =>
wenzelm@19405
   214
        (Symtab.update (class, info) is, fold (fn x => add_param pp (x, class)) params ps)));
wenzelm@19392
   215
  in (class, final_thy) end;
wenzelm@404
   216
wenzelm@15876
   217
in
wenzelm@404
   218
wenzelm@19243
   219
val add_axclass = gen_axclass Sign.intern_class Theory.read_axm Attrib.attribute;
wenzelm@19243
   220
val add_axclass_i = gen_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@19392
   226
(** instantiation proofs **)
wenzelm@15876
   227
wenzelm@19405
   228
(* parameters *)
wenzelm@15876
   229
wenzelm@19405
   230
fun params_of_sort thy S =
wenzelm@19405
   231
  let
wenzelm@19405
   232
    val range = Graph.all_succs (Sign.classes_of thy) (Sign.certify_sort thy S);
wenzelm@19405
   233
    val params = #2 (#1 (AxClassData.get thy));
wenzelm@19405
   234
  in fold (fn (x, c) => if member (op =) range c then cons x else I) params [] end;
wenzelm@19405
   235
wenzelm@19405
   236
fun cert_classrel thy raw_rel =
wenzelm@15876
   237
  let
wenzelm@19405
   238
    val (c1, c2) = pairself (Sign.certify_class thy) raw_rel;
wenzelm@19405
   239
    val _ = Type.add_classrel (Sign.pp thy) [(c1, c2)] (Sign.tsig_of thy);
wenzelm@19405
   240
    val _ =
wenzelm@19405
   241
      (case subtract (op =) (params_of_sort thy [c1]) (params_of_sort thy [c2]) of
wenzelm@19405
   242
        [] => ()
wenzelm@19405
   243
      | xs => raise TYPE ("Class " ^ Sign.string_of_sort thy [c1] ^ " lacks parameter(s) " ^
wenzelm@19405
   244
          commas_quote xs ^ " of " ^ Sign.string_of_sort thy [c2], [], []));
wenzelm@19405
   245
  in (c1, c2) end;
wenzelm@19405
   246
wenzelm@19405
   247
fun read_classrel thy raw_rel =
wenzelm@19405
   248
  cert_classrel thy (pairself (Sign.read_class thy) raw_rel)
wenzelm@19405
   249
    handle TYPE (msg, _, _) => error msg;
wenzelm@19405
   250
wenzelm@19405
   251
wenzelm@19405
   252
(* primitive rules *)
wenzelm@19405
   253
wenzelm@19405
   254
fun add_classrel th thy =
wenzelm@19405
   255
  let
wenzelm@19405
   256
    fun err () = raise THM ("add_classrel: malformed class relation", 0, [th]);
wenzelm@19405
   257
    val prop = Drule.plain_prop_of (Thm.transfer thy th);
wenzelm@19405
   258
    val rel = Logic.dest_classrel prop handle TERM _ => err ();
wenzelm@19405
   259
    val (c1, c2) = cert_classrel thy rel handle TYPE _ => err ();
wenzelm@19392
   260
  in
wenzelm@19392
   261
    thy
wenzelm@19405
   262
    |> Theory.add_classrel_i [(c1, c2)]
wenzelm@19405
   263
    |> AxClassData.map (apsnd (map_instances (fn (classes, classrel, arities) =>
wenzelm@19405
   264
      (classes
wenzelm@19405
   265
          |> Graph.default_node (c1, ())
wenzelm@19405
   266
          |> Graph.default_node (c2, ())
wenzelm@19405
   267
          |> Graph.add_edge (c1, c2),
wenzelm@19405
   268
        ((c1, c2), th) :: classrel, arities))))
wenzelm@19392
   269
  end;
wenzelm@15876
   270
wenzelm@19405
   271
fun add_arity th thy =
wenzelm@15876
   272
  let
wenzelm@19405
   273
    val prop = Drule.plain_prop_of (Thm.transfer thy th);
wenzelm@19405
   274
    val (t, Ss, c) = Logic.dest_arity prop handle TERM _ =>
wenzelm@19405
   275
      raise THM ("add_arity: malformed type arity", 0, [th]);
wenzelm@19392
   276
  in
wenzelm@19392
   277
    thy
wenzelm@19405
   278
    |> Theory.add_arities_i [(t, Ss, [c])]
wenzelm@19405
   279
    |> AxClassData.map (apsnd (map_instances (fn (classes, classrel, arities) =>
wenzelm@19405
   280
      (classes, classrel, ((t, Ss, c), th) :: arities))))
wenzelm@19392
   281
  end;
wenzelm@404
   282
wenzelm@404
   283
wenzelm@19243
   284
(* tactical proofs *)
wenzelm@15876
   285
wenzelm@19405
   286
fun prove_classrel raw_rel tac thy =
wenzelm@15876
   287
  let
wenzelm@19405
   288
    val (c1, c2) = cert_classrel thy raw_rel;
wenzelm@19405
   289
    val th = Goal.prove thy [] [] (Logic.mk_classrel (c1, c2)) (fn _ => tac) handle ERROR msg =>
wenzelm@19243
   290
      cat_error msg ("The error(s) above occurred while trying to prove class relation " ^
wenzelm@19243
   291
        quote (Sign.string_of_classrel thy [c1, c2]));
wenzelm@19405
   292
  in add_classrel th thy end;
wenzelm@404
   293
wenzelm@19243
   294
fun prove_arity raw_arity tac thy =
wenzelm@15876
   295
  let
wenzelm@19243
   296
    val arity = Sign.cert_arity thy raw_arity;
wenzelm@19405
   297
    val props = Logic.mk_arities arity;
wenzelm@17956
   298
    val ths = Goal.prove_multi thy [] [] props
wenzelm@18678
   299
      (fn _ => Tactic.precise_conjunction_tac (length props) 1 THEN tac) handle ERROR msg =>
wenzelm@19243
   300
        cat_error msg ("The error(s) above occurred while trying to prove type arity " ^
wenzelm@19243
   301
          quote (Sign.string_of_arity thy arity));
wenzelm@19405
   302
  in fold add_arity ths thy end;
wenzelm@19392
   303
wenzelm@15876
   304
end;