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