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