src/Pure/axclass.ML
author wenzelm
Thu Apr 13 12:00:55 2006 +0200 (2006-04-13)
changeset 19418 03b01c9314fc
parent 19405 a551256aba15
child 19460 2b37469d52ad
permissions -rw-r--r--
reworded add_axclass(_i): canonical specification format,
purely definitional version,
always qualify intro/axioms;
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@19418
    11
  val get_info: theory -> class -> {super: sort, def: thm, 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 params_of_sort: theory -> sort -> string list
wenzelm@19405
    18
  val cert_classrel: theory -> class * class -> class * class
wenzelm@19405
    19
  val read_classrel: theory -> xstring * xstring -> class * class
wenzelm@19405
    20
  val add_classrel: thm -> theory -> theory
wenzelm@19405
    21
  val add_arity: thm -> theory -> theory
wenzelm@19405
    22
  val prove_classrel: class * class -> tactic -> theory -> theory
wenzelm@19243
    23
  val prove_arity: string * sort list * sort -> tactic -> theory -> theory
wenzelm@19418
    24
  val add_axclass: bstring * xstring list -> string list ->
wenzelm@19418
    25
    ((bstring * Attrib.src list) * string list) list -> theory -> class * theory
wenzelm@19418
    26
  val add_axclass_i: bstring * class list -> string list ->
wenzelm@19418
    27
    ((bstring * attribute list) * term list) list -> theory -> class * 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@19418
    57
 {super: sort,
wenzelm@19418
    58
  def: thm,
wenzelm@19392
    59
  intro: thm,
wenzelm@19392
    60
  axioms: thm list};
wenzelm@19392
    61
wenzelm@19418
    62
fun make_axclass (super, def, intro, axioms) =
wenzelm@19418
    63
  AxClass {super = super, def = def, intro = intro, axioms = axioms};
wenzelm@19392
    64
wenzelm@19405
    65
type axclasses = axclass Symtab.table * param list;
wenzelm@19405
    66
wenzelm@19405
    67
fun merge_axclasses pp ((tab1, params1), (tab2, params2)) : axclasses =
wenzelm@19405
    68
  (Symtab.merge (K true) (tab1, tab2), merge_params pp (params1, params2));
wenzelm@19405
    69
wenzelm@19392
    70
wenzelm@19392
    71
(* instances *)
wenzelm@19392
    72
wenzelm@19392
    73
datatype instances = Instances of
wenzelm@19405
    74
 {classes: unit Graph.T,                 (*raw relation -- no closure!*)
wenzelm@19405
    75
  classrel: ((class * class) * thm) list,
wenzelm@19405
    76
  arities: ((string * sort list * class) * thm) list};
wenzelm@19392
    77
wenzelm@19405
    78
fun make_instances (classes, classrel, arities) =
wenzelm@19405
    79
  Instances {classes = classes, classrel = classrel, arities = arities};
wenzelm@6379
    80
wenzelm@19405
    81
fun map_instances f (Instances {classes, classrel, arities}) =
wenzelm@19405
    82
  make_instances (f (classes, classrel, arities));
wenzelm@19392
    83
wenzelm@19392
    84
fun merge_instances
wenzelm@19405
    85
   (Instances {classes = classes1, classrel = classrel1, arities = arities1},
wenzelm@19405
    86
    Instances {classes = classes2, classrel = classrel2, arities = arities2}) =
wenzelm@19392
    87
  make_instances
wenzelm@19405
    88
   (Graph.merge (K true) (classes1, classes2),
wenzelm@19405
    89
    merge (eq_fst op =) (classrel1, classrel2),
wenzelm@19392
    90
    merge (eq_fst op =) (arities1, arities2));
wenzelm@19392
    91
wenzelm@19392
    92
wenzelm@19405
    93
(* data *)
wenzelm@19392
    94
wenzelm@19392
    95
structure AxClassData = TheoryDataFun
wenzelm@16458
    96
(struct
wenzelm@19392
    97
  val name = "Pure/axclass";
wenzelm@19405
    98
  type T = axclasses * instances;
wenzelm@19405
    99
  val empty : T = ((Symtab.empty, []), make_instances (Graph.empty, [], []));
wenzelm@6546
   100
  val copy = I;
wenzelm@16458
   101
  val extend = I;
wenzelm@6379
   102
wenzelm@19405
   103
  fun merge pp ((axclasses1, instances1), (axclasses2, instances2)) =
wenzelm@19405
   104
    (merge_axclasses pp (axclasses1, axclasses2), merge_instances (instances1, instances2));
wenzelm@19392
   105
wenzelm@19405
   106
  fun print thy ((axclasses, params), _) =
wenzelm@6379
   107
    let
wenzelm@19405
   108
      val ctxt = ProofContext.init thy;
wenzelm@19405
   109
      val prt_cls = ProofContext.pretty_sort ctxt o single;
wenzelm@19405
   110
wenzelm@19405
   111
      fun pretty_class c [] = prt_cls c
wenzelm@19405
   112
        | pretty_class c cs = Pretty.block
wenzelm@19405
   113
            (prt_cls c :: Pretty.str " <" :: Pretty.brk 1 :: Pretty.commas (map prt_cls cs));
wenzelm@6379
   114
wenzelm@19418
   115
      fun pretty_axclass (class, AxClass {super, def, intro, axioms}) =
wenzelm@19392
   116
        Pretty.block (Pretty.fbreaks
wenzelm@19418
   117
         [pretty_class class super,
wenzelm@19405
   118
          Pretty.strs ("parameters:" ::
wenzelm@19405
   119
            fold (fn (x, c) => if c = class then cons x else I) params []),
wenzelm@19418
   120
          ProofContext.pretty_fact ctxt ("def", [def]),
wenzelm@19405
   121
          ProofContext.pretty_fact ctxt (introN, [intro]),
wenzelm@19405
   122
          ProofContext.pretty_fact ctxt (axiomsN, axioms)]);
wenzelm@19405
   123
    in
wenzelm@19405
   124
      Pretty.writeln (Pretty.chunks (map pretty_axclass (Symtab.dest axclasses)))
wenzelm@19405
   125
    end;
wenzelm@16458
   126
end);
wenzelm@6379
   127
wenzelm@19392
   128
val _ = Context.add_setup AxClassData.init;
wenzelm@19392
   129
val print_axclasses = AxClassData.print;
wenzelm@19392
   130
wenzelm@19392
   131
val get_instances = AxClassData.get #> (fn (_, Instances insts) => insts);
wenzelm@6379
   132
wenzelm@6379
   133
wenzelm@19392
   134
(* lookup *)
wenzelm@19392
   135
wenzelm@19405
   136
val lookup_info = Symtab.lookup o #1 o #1 o AxClassData.get;
wenzelm@6379
   137
wenzelm@17281
   138
fun get_info thy c =
wenzelm@17281
   139
  (case lookup_info thy c of
wenzelm@19392
   140
    SOME (AxClass info) => info
wenzelm@19392
   141
  | NONE => error ("Unknown axclass " ^ quote c));
wenzelm@6379
   142
wenzelm@19123
   143
fun class_intros thy =
wenzelm@19392
   144
  let
wenzelm@19392
   145
    fun add_intro c =
wenzelm@19392
   146
      (case lookup_info thy c of SOME (AxClass {intro, ...}) => cons intro | _ => I);
wenzelm@19392
   147
    val classes = Sign.classes thy;
wenzelm@19392
   148
  in map (Thm.class_triv thy) classes @ fold add_intro classes [] end;
wenzelm@15876
   149
wenzelm@15876
   150
wenzelm@404
   151
wenzelm@19418
   152
(** instances **)
wenzelm@15876
   153
wenzelm@19405
   154
(* parameters *)
wenzelm@15876
   155
wenzelm@19405
   156
fun params_of_sort thy S =
wenzelm@19405
   157
  let
wenzelm@19405
   158
    val range = Graph.all_succs (Sign.classes_of thy) (Sign.certify_sort thy S);
wenzelm@19405
   159
    val params = #2 (#1 (AxClassData.get thy));
wenzelm@19405
   160
  in fold (fn (x, c) => if member (op =) range c then cons x else I) params [] end;
wenzelm@19405
   161
wenzelm@19418
   162
wenzelm@19418
   163
(* class relations *)
wenzelm@19418
   164
wenzelm@19405
   165
fun cert_classrel thy raw_rel =
wenzelm@15876
   166
  let
wenzelm@19405
   167
    val (c1, c2) = pairself (Sign.certify_class thy) raw_rel;
wenzelm@19405
   168
    val _ = Type.add_classrel (Sign.pp thy) [(c1, c2)] (Sign.tsig_of thy);
wenzelm@19405
   169
    val _ =
wenzelm@19405
   170
      (case subtract (op =) (params_of_sort thy [c1]) (params_of_sort thy [c2]) of
wenzelm@19405
   171
        [] => ()
wenzelm@19405
   172
      | xs => raise TYPE ("Class " ^ Sign.string_of_sort thy [c1] ^ " lacks parameter(s) " ^
wenzelm@19405
   173
          commas_quote xs ^ " of " ^ Sign.string_of_sort thy [c2], [], []));
wenzelm@19405
   174
  in (c1, c2) end;
wenzelm@19405
   175
wenzelm@19405
   176
fun read_classrel thy raw_rel =
wenzelm@19405
   177
  cert_classrel thy (pairself (Sign.read_class thy) raw_rel)
wenzelm@19405
   178
    handle TYPE (msg, _, _) => error msg;
wenzelm@19405
   179
wenzelm@19405
   180
wenzelm@19405
   181
(* primitive rules *)
wenzelm@19405
   182
wenzelm@19405
   183
fun add_classrel th thy =
wenzelm@19405
   184
  let
wenzelm@19405
   185
    fun err () = raise THM ("add_classrel: malformed class relation", 0, [th]);
wenzelm@19405
   186
    val prop = Drule.plain_prop_of (Thm.transfer thy th);
wenzelm@19405
   187
    val rel = Logic.dest_classrel prop handle TERM _ => err ();
wenzelm@19405
   188
    val (c1, c2) = cert_classrel thy rel handle TYPE _ => err ();
wenzelm@19392
   189
  in
wenzelm@19392
   190
    thy
wenzelm@19405
   191
    |> Theory.add_classrel_i [(c1, c2)]
wenzelm@19405
   192
    |> AxClassData.map (apsnd (map_instances (fn (classes, classrel, arities) =>
wenzelm@19418
   193
        (classes
wenzelm@19418
   194
            |> Graph.default_node (c1, ())
wenzelm@19418
   195
            |> Graph.default_node (c2, ())
wenzelm@19418
   196
            |> Graph.add_edge (c1, c2),
wenzelm@19418
   197
          ((c1, c2), th) :: classrel, arities))))
wenzelm@19392
   198
  end;
wenzelm@15876
   199
wenzelm@19405
   200
fun add_arity th thy =
wenzelm@15876
   201
  let
wenzelm@19405
   202
    val prop = Drule.plain_prop_of (Thm.transfer thy th);
wenzelm@19405
   203
    val (t, Ss, c) = Logic.dest_arity prop handle TERM _ =>
wenzelm@19405
   204
      raise THM ("add_arity: malformed type arity", 0, [th]);
wenzelm@19392
   205
  in
wenzelm@19392
   206
    thy
wenzelm@19405
   207
    |> Theory.add_arities_i [(t, Ss, [c])]
wenzelm@19405
   208
    |> AxClassData.map (apsnd (map_instances (fn (classes, classrel, arities) =>
wenzelm@19405
   209
      (classes, classrel, ((t, Ss, c), th) :: arities))))
wenzelm@19392
   210
  end;
wenzelm@404
   211
wenzelm@404
   212
wenzelm@19243
   213
(* tactical proofs *)
wenzelm@15876
   214
wenzelm@19405
   215
fun prove_classrel raw_rel tac thy =
wenzelm@15876
   216
  let
wenzelm@19405
   217
    val (c1, c2) = cert_classrel thy raw_rel;
wenzelm@19405
   218
    val th = Goal.prove thy [] [] (Logic.mk_classrel (c1, c2)) (fn _ => tac) handle ERROR msg =>
wenzelm@19243
   219
      cat_error msg ("The error(s) above occurred while trying to prove class relation " ^
wenzelm@19243
   220
        quote (Sign.string_of_classrel thy [c1, c2]));
wenzelm@19405
   221
  in add_classrel th thy end;
wenzelm@404
   222
wenzelm@19243
   223
fun prove_arity raw_arity tac thy =
wenzelm@15876
   224
  let
wenzelm@19243
   225
    val arity = Sign.cert_arity thy raw_arity;
wenzelm@19405
   226
    val props = Logic.mk_arities arity;
wenzelm@17956
   227
    val ths = Goal.prove_multi thy [] [] props
wenzelm@18678
   228
      (fn _ => Tactic.precise_conjunction_tac (length props) 1 THEN tac) handle ERROR msg =>
wenzelm@19243
   229
        cat_error msg ("The error(s) above occurred while trying to prove type arity " ^
wenzelm@19243
   230
          quote (Sign.string_of_arity thy arity));
wenzelm@19405
   231
  in fold add_arity ths thy end;
wenzelm@19392
   232
wenzelm@19418
   233
wenzelm@19418
   234
wenzelm@19418
   235
(** axclass definitions **)
wenzelm@19418
   236
wenzelm@19418
   237
(* add_axclass(_i) *)
wenzelm@19418
   238
wenzelm@19418
   239
fun gen_axclass prep_class prep_att prep_propp
wenzelm@19418
   240
    (bclass, raw_super) params raw_specs thy =
wenzelm@19418
   241
  let
wenzelm@19418
   242
    val ctxt = ProofContext.init thy;
wenzelm@19418
   243
    val pp = ProofContext.pp ctxt;
wenzelm@19418
   244
wenzelm@19418
   245
wenzelm@19418
   246
    (* prepare specification *)
wenzelm@19418
   247
wenzelm@19418
   248
    val bconst = Logic.const_of_class bclass;
wenzelm@19418
   249
    val class = Sign.full_name thy bclass;
wenzelm@19418
   250
    val super = map (prep_class thy) raw_super |> Sign.certify_sort thy;
wenzelm@19418
   251
wenzelm@19418
   252
    fun prep_axiom t =
wenzelm@19418
   253
      (case Term.add_tfrees t [] of
wenzelm@19418
   254
        [(a, S)] =>
wenzelm@19418
   255
          if Sign.subsort thy (super, S) then t
wenzelm@19418
   256
          else error ("Sort constraint of type variable " ^
wenzelm@19418
   257
            setmp show_sorts true (Pretty.string_of_typ pp) (TFree (a, S)) ^
wenzelm@19418
   258
            " needs to be weaker than " ^ Pretty.string_of_sort pp super)
wenzelm@19418
   259
      | [] => t
wenzelm@19418
   260
      | _ => error ("Multiple type variables in class axiom:\n" ^ Pretty.string_of_term pp t))
wenzelm@19418
   261
      |> map_term_types (Term.map_atyps (fn TFree _ => Term.aT [] | U => U))
wenzelm@19418
   262
      |> Logic.close_form;
wenzelm@19418
   263
wenzelm@19418
   264
    val axiomss = prep_propp (ctxt, map (map (rpair ([], [])) o snd) raw_specs)
wenzelm@19418
   265
      |> snd |> map (map (prep_axiom o fst));
wenzelm@19418
   266
    val name_atts = Attrib.map_specs (prep_att thy) raw_specs |> map fst;
wenzelm@19418
   267
wenzelm@19418
   268
wenzelm@19418
   269
    (* definition *)
wenzelm@19418
   270
wenzelm@19418
   271
    val conjs = map (curry Logic.mk_inclass (Term.aT [])) super @ List.concat axiomss;
wenzelm@19418
   272
    val class_eq =
wenzelm@19418
   273
      Logic.mk_equals (Logic.mk_inclass (Term.aT [], class), Logic.mk_conjunction_list conjs);
wenzelm@19418
   274
wenzelm@19418
   275
    val ([def], def_thy) =
wenzelm@19418
   276
      thy
wenzelm@19418
   277
      |> Theory.add_classes_i [(bclass, super)]
wenzelm@19418
   278
      |> PureThy.add_defs_i false [((Thm.def_name bconst, class_eq), [])];
wenzelm@19418
   279
    val (raw_intro, (raw_classrel, raw_axioms)) =
wenzelm@19418
   280
      (Conjunction.split_defined (length conjs) def) ||> chop (length super);
wenzelm@19418
   281
wenzelm@19418
   282
wenzelm@19418
   283
    (* facts *)
wenzelm@19418
   284
wenzelm@19418
   285
    val class_triv = Thm.class_triv def_thy class;
wenzelm@19418
   286
    val ([(_, [intro]), (_, axioms)], facts_thy) =
wenzelm@19418
   287
      def_thy
wenzelm@19418
   288
      |> PureThy.note_thmss_qualified "" bconst
wenzelm@19418
   289
        [((introN, []), [([Drule.standard raw_intro], [])]),
wenzelm@19418
   290
         ((axiomsN, []), [(map (fn th => Drule.standard (class_triv RS th)) raw_axioms, [])])]
wenzelm@19418
   291
      ||> fold (fn th => add_classrel (Drule.standard' (class_triv RS th))) raw_classrel;
wenzelm@19418
   292
wenzelm@19418
   293
wenzelm@19418
   294
    (* result *)
wenzelm@19418
   295
wenzelm@19418
   296
    val result_thy =
wenzelm@19418
   297
      facts_thy
wenzelm@19418
   298
      |> Sign.add_path bconst
wenzelm@19418
   299
      |> PureThy.note_thmss_i "" (name_atts ~~ map Thm.simple_fact (unflat axiomss axioms)) |> snd
wenzelm@19418
   300
      |> Sign.restore_naming facts_thy
wenzelm@19418
   301
      |> AxClassData.map (apfst (fn (is, ps) =>
wenzelm@19418
   302
        (Symtab.update (class, make_axclass (super, def, intro, axioms)) is,
wenzelm@19418
   303
          fold (fn x => add_param pp (x, class)) params ps)));
wenzelm@19418
   304
wenzelm@19418
   305
  in (class, result_thy) end;
wenzelm@19418
   306
wenzelm@19418
   307
val add_axclass = gen_axclass Sign.read_class Attrib.attribute ProofContext.read_propp;
wenzelm@19418
   308
val add_axclass_i = gen_axclass Sign.certify_class (K I) ProofContext.cert_propp;
wenzelm@19418
   309
wenzelm@15876
   310
end;