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