src/Pure/axclass.ML
author wenzelm
Mon, 20 Oct 1997 15:20:20 +0200
changeset 3956 d59fe4579004
parent 3949 c60ff6d0a6b8
child 3988 6ff1a1e2bd21
permissions -rw-r--r--
tuned types;
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
560
6702a715281d cleaned sig;
wenzelm
parents: 487
diff changeset
     5
User interfaces for 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
1498
7b7d20e89b1b Elimination of fully-functorial style.
paulson
parents: 1237
diff changeset
    10
  val add_thms_as_axms: (string * thm) list -> theory -> theory
7b7d20e89b1b Elimination of fully-functorial style.
paulson
parents: 1237
diff changeset
    11
  val add_classrel_thms: thm list -> theory -> theory
7b7d20e89b1b Elimination of fully-functorial style.
paulson
parents: 1237
diff changeset
    12
  val add_arity_thms: thm list -> theory -> theory
3956
d59fe4579004 tuned types;
wenzelm
parents: 3949
diff changeset
    13
  val add_axclass: bclass * xclass list -> (string * string) list
1498
7b7d20e89b1b Elimination of fully-functorial style.
paulson
parents: 1237
diff changeset
    14
    -> theory -> theory
3956
d59fe4579004 tuned types;
wenzelm
parents: 3949
diff changeset
    15
  val add_axclass_i: bclass * class list -> (string * term) list
1498
7b7d20e89b1b Elimination of fully-functorial style.
paulson
parents: 1237
diff changeset
    16
    -> theory -> theory
3938
c20fbe3cb94f fixed types of add_XXX;
wenzelm
parents: 3854
diff changeset
    17
  val add_inst_subclass: xclass * xclass -> string list -> thm list
c20fbe3cb94f fixed types of add_XXX;
wenzelm
parents: 3854
diff changeset
    18
    -> tactic option -> theory -> theory
3788
51bd5c0954c6 eliminated raise_term, raise_typ;
wenzelm
parents: 3764
diff changeset
    19
  val add_inst_subclass_i: class * class -> string list -> thm list
51bd5c0954c6 eliminated raise_term, raise_typ;
wenzelm
parents: 3764
diff changeset
    20
    -> tactic option -> theory -> theory
3938
c20fbe3cb94f fixed types of add_XXX;
wenzelm
parents: 3854
diff changeset
    21
  val add_inst_arity: xstring * xsort list * xclass list -> string list
c20fbe3cb94f fixed types of add_XXX;
wenzelm
parents: 3854
diff changeset
    22
    -> thm list -> tactic option -> theory -> theory
3788
51bd5c0954c6 eliminated raise_term, raise_typ;
wenzelm
parents: 3764
diff changeset
    23
  val add_inst_arity_i: string * sort list * class list -> string list
51bd5c0954c6 eliminated raise_term, raise_typ;
wenzelm
parents: 3764
diff changeset
    24
    -> thm list -> tactic option -> theory -> theory
1498
7b7d20e89b1b Elimination of fully-functorial style.
paulson
parents: 1237
diff changeset
    25
  val axclass_tac: theory -> thm list -> tactic
7b7d20e89b1b Elimination of fully-functorial style.
paulson
parents: 1237
diff changeset
    26
  val prove_subclass: theory -> class * class -> thm list
7b7d20e89b1b Elimination of fully-functorial style.
paulson
parents: 1237
diff changeset
    27
    -> tactic option -> thm
7b7d20e89b1b Elimination of fully-functorial style.
paulson
parents: 1237
diff changeset
    28
  val prove_arity: theory -> string * sort list * class -> thm list
7b7d20e89b1b Elimination of fully-functorial style.
paulson
parents: 1237
diff changeset
    29
    -> tactic option -> thm
3949
c60ff6d0a6b8 fixed goal_XXX;
wenzelm
parents: 3938
diff changeset
    30
  val goal_subclass: theory -> xclass * xclass -> thm list
c60ff6d0a6b8 fixed goal_XXX;
wenzelm
parents: 3938
diff changeset
    31
  val goal_arity: theory -> xstring * xsort list * xclass -> thm list
3938
c20fbe3cb94f fixed types of add_XXX;
wenzelm
parents: 3854
diff changeset
    32
end;
404
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    33
1498
7b7d20e89b1b Elimination of fully-functorial style.
paulson
parents: 1237
diff changeset
    34
structure AxClass : AX_CLASS =
404
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    35
struct
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    36
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    37
(** utilities **)
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    38
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    39
(* type vars *)
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    40
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    41
fun map_typ_frees f (Type (t, tys)) = Type (t, map (map_typ_frees f) tys)
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    42
  | map_typ_frees f (TFree a) = f a
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    43
  | map_typ_frees _ a = a;
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    44
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    45
val map_term_tfrees = map_term_types o map_typ_frees;
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    46
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    47
fun aT S = TFree ("'a", S);
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    48
3395
d8700b008944 eliminated freeze_vars;
wenzelm
parents: 2961
diff changeset
    49
fun dest_varT (TFree (x, S)) = ((x, ~1), S)
d8700b008944 eliminated freeze_vars;
wenzelm
parents: 2961
diff changeset
    50
  | dest_varT (TVar xi_S) = xi_S
3788
51bd5c0954c6 eliminated raise_term, raise_typ;
wenzelm
parents: 3764
diff changeset
    51
  | dest_varT T = raise TYPE ("dest_varT", [T], []);
3395
d8700b008944 eliminated freeze_vars;
wenzelm
parents: 2961
diff changeset
    52
404
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    53
886
9af08725600b instance: now automatically includes defs of current thy node as witnesses;
wenzelm
parents: 638
diff changeset
    54
(* get axioms and theorems *)
404
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    55
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    56
fun get_ax thy name =
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    57
  Some (get_axiom thy name) handle THEORY _ => None;
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    58
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    59
val get_axioms = mapfilter o get_ax;
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    60
1498
7b7d20e89b1b Elimination of fully-functorial style.
paulson
parents: 1237
diff changeset
    61
val is_def = Logic.is_equals o #prop o rep_thm;
886
9af08725600b instance: now automatically includes defs of current thy node as witnesses;
wenzelm
parents: 638
diff changeset
    62
9af08725600b instance: now automatically includes defs of current thy node as witnesses;
wenzelm
parents: 638
diff changeset
    63
fun witnesses thy axms thms =
1201
de2fc8cf9b6a minor fix: instance now raises error if witness axioms don't exist;
wenzelm
parents: 886
diff changeset
    64
  map (get_axiom thy) axms @ thms @ filter is_def (map snd (axioms_of thy));
886
9af08725600b instance: now automatically includes defs of current thy node as witnesses;
wenzelm
parents: 638
diff changeset
    65
404
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    66
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    67
560
6702a715281d cleaned sig;
wenzelm
parents: 487
diff changeset
    68
(** abstract syntax operations **)
423
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    69
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    70
(* subclass relations as terms *)
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    71
1498
7b7d20e89b1b Elimination of fully-functorial style.
paulson
parents: 1237
diff changeset
    72
fun mk_classrel (c1, c2) = Logic.mk_inclass (aT [c1], c2);
423
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    73
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    74
fun dest_classrel tm =
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    75
  let
3788
51bd5c0954c6 eliminated raise_term, raise_typ;
wenzelm
parents: 3764
diff changeset
    76
    fun err () = raise TERM ("dest_classrel", [tm]);
423
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    77
3395
d8700b008944 eliminated freeze_vars;
wenzelm
parents: 2961
diff changeset
    78
    val (ty, c2) = Logic.dest_inclass tm handle TERM _ => err ();
d8700b008944 eliminated freeze_vars;
wenzelm
parents: 2961
diff changeset
    79
    val c1 = (case dest_varT ty of (_, [c]) => c | _ => err ())
d8700b008944 eliminated freeze_vars;
wenzelm
parents: 2961
diff changeset
    80
      handle TYPE _ => err ();
423
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    81
  in
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    82
    (c1, c2)
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    83
  end;
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    84
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    85
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    86
(* arities as terms *)
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    87
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    88
fun mk_arity (t, ss, c) =
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    89
  let
449
75ac32497f09 various minor changes (names and comments);
wenzelm
parents: 423
diff changeset
    90
    val names = tl (variantlist (replicate (length ss + 1) "'", []));
2266
82aef6857c5b Replaced map...~~ by ListPair.map
paulson
parents: 1498
diff changeset
    91
    val tfrees = ListPair.map TFree (names, ss);
423
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    92
  in
1498
7b7d20e89b1b Elimination of fully-functorial style.
paulson
parents: 1237
diff changeset
    93
    Logic.mk_inclass (Type (t, tfrees), c)
423
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    94
  end;
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    95
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    96
fun dest_arity tm =
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    97
  let
3788
51bd5c0954c6 eliminated raise_term, raise_typ;
wenzelm
parents: 3764
diff changeset
    98
    fun err () = raise TERM ("dest_arity", [tm]);
423
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    99
3395
d8700b008944 eliminated freeze_vars;
wenzelm
parents: 2961
diff changeset
   100
    val (ty, c) = Logic.dest_inclass tm handle TERM _ => err ();
d8700b008944 eliminated freeze_vars;
wenzelm
parents: 2961
diff changeset
   101
    val (t, tvars) =
423
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   102
      (case ty of
3395
d8700b008944 eliminated freeze_vars;
wenzelm
parents: 2961
diff changeset
   103
        Type (t, tys) => (t, map dest_varT tys handle TYPE _ => err ())
423
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   104
      | _ => err ());
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   105
    val ss =
3395
d8700b008944 eliminated freeze_vars;
wenzelm
parents: 2961
diff changeset
   106
      if null (gen_duplicates eq_fst tvars)
d8700b008944 eliminated freeze_vars;
wenzelm
parents: 2961
diff changeset
   107
      then map snd tvars else err ();
423
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   108
  in
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   109
    (t, ss, c)
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   110
  end;
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   111
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   112
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   113
560
6702a715281d cleaned sig;
wenzelm
parents: 487
diff changeset
   114
(** add theorems as axioms **)
423
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   115
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   116
fun prep_thm_axm thy thm =
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   117
  let
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   118
    fun err msg = raise THM ("prep_thm_axm: " ^ msg, 0, [thm]);
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   119
1237
45ac644b0052 adapted to new version of shyps-stuff;
wenzelm
parents: 1217
diff changeset
   120
    val {sign, hyps, prop, ...} = rep_thm thm;
423
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   121
  in
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   122
    if not (Sign.subsig (sign, sign_of thy)) then
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   123
      err "theorem not of same theory"
1237
45ac644b0052 adapted to new version of shyps-stuff;
wenzelm
parents: 1217
diff changeset
   124
    else if not (null (extra_shyps thm)) orelse not (null hyps) then
423
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   125
      err "theorem may not contain hypotheses"
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   126
    else prop
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   127
  end;
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   128
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   129
(*general theorems*)
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   130
fun add_thms_as_axms thms thy =
3764
fe7719aee219 fully qualified names: Theory.add_XXX;
wenzelm
parents: 3632
diff changeset
   131
  Theory.add_axioms_i (map (apsnd (prep_thm_axm thy)) thms) thy;
423
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   132
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   133
(*theorems expressing class relations*)
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   134
fun add_classrel_thms thms thy =
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   135
  let
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   136
    fun prep_thm thm =
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   137
      let
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   138
        val prop = prep_thm_axm thy thm;
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   139
        val (c1, c2) = dest_classrel prop handle TERM _ =>
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   140
          raise THM ("add_classrel_thms: theorem is not a class relation", 0, [thm]);
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   141
      in (c1, c2) end;
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   142
  in
3764
fe7719aee219 fully qualified names: Theory.add_XXX;
wenzelm
parents: 3632
diff changeset
   143
    Theory.add_classrel (map prep_thm thms) thy
423
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   144
  end;
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   145
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   146
(*theorems expressing arities*)
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   147
fun add_arity_thms thms thy =
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   148
  let
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   149
    fun prep_thm thm =
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   150
      let
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   151
        val prop = prep_thm_axm thy thm;
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   152
        val (t, ss, c) = dest_arity prop handle TERM _ =>
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   153
          raise THM ("add_arity_thms: theorem is not an arity", 0, [thm]);
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   154
      in (t, ss, [c]) end;
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   155
  in
3764
fe7719aee219 fully qualified names: Theory.add_XXX;
wenzelm
parents: 3632
diff changeset
   156
    Theory.add_arities (map prep_thm thms) thy
423
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   157
  end;
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   158
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   159
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   160
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   161
(** add axiomatic type classes **)
404
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   162
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   163
(* errors *)
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   164
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   165
fun err_not_logic c =
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   166
  error ("Axiomatic class " ^ quote c ^ " not subclass of \"logic\"");
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   167
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   168
fun err_bad_axsort ax c =
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   169
  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
   170
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   171
fun err_bad_tfrees ax =
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   172
  error ("More than one type variable in axiom " ^ quote ax);
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   173
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   174
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   175
(* ext_axclass *)
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   176
3788
51bd5c0954c6 eliminated raise_term, raise_typ;
wenzelm
parents: 3764
diff changeset
   177
fun ext_axclass int prep_axm (raw_class, raw_super_classes) raw_axioms old_thy =
404
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   178
  let
3938
c20fbe3cb94f fixed types of add_XXX;
wenzelm
parents: 3854
diff changeset
   179
    val old_sign = sign_of old_thy;
c20fbe3cb94f fixed types of add_XXX;
wenzelm
parents: 3854
diff changeset
   180
    val axioms = map (prep_axm old_sign) raw_axioms;
c20fbe3cb94f fixed types of add_XXX;
wenzelm
parents: 3854
diff changeset
   181
    val class = Sign.full_name old_sign raw_class;
c20fbe3cb94f fixed types of add_XXX;
wenzelm
parents: 3854
diff changeset
   182
3788
51bd5c0954c6 eliminated raise_term, raise_typ;
wenzelm
parents: 3764
diff changeset
   183
    val thy =
51bd5c0954c6 eliminated raise_term, raise_typ;
wenzelm
parents: 3764
diff changeset
   184
      (if int then Theory.add_classes else Theory.add_classes_i)
51bd5c0954c6 eliminated raise_term, raise_typ;
wenzelm
parents: 3764
diff changeset
   185
        [(raw_class, raw_super_classes)] old_thy;
404
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   186
    val sign = sign_of thy;
3938
c20fbe3cb94f fixed types of add_XXX;
wenzelm
parents: 3854
diff changeset
   187
    val super_classes =
c20fbe3cb94f fixed types of add_XXX;
wenzelm
parents: 3854
diff changeset
   188
      if int then map (Sign.intern_class sign) raw_super_classes
c20fbe3cb94f fixed types of add_XXX;
wenzelm
parents: 3854
diff changeset
   189
      else raw_super_classes;
404
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   190
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   191
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   192
    (* prepare abstract axioms *)
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   193
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   194
    fun abs_axm ax =
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   195
      if null (term_tfrees ax) then
1498
7b7d20e89b1b Elimination of fully-functorial style.
paulson
parents: 1237
diff changeset
   196
        Logic.mk_implies (Logic.mk_inclass (aT logicS, class), ax)
3788
51bd5c0954c6 eliminated raise_term, raise_typ;
wenzelm
parents: 3764
diff changeset
   197
      else map_term_tfrees (K (aT [class])) ax;
404
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   198
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   199
    val abs_axioms = map (apsnd abs_axm) axioms;
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   200
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   201
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   202
    (* prepare introduction orule *)
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   203
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   204
    val _ =
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   205
      if Sign.subsort sign ([class], logicS) then ()
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   206
      else err_not_logic class;
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   207
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   208
    fun axm_sort (name, ax) =
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   209
      (case term_tfrees ax of
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
      | [(_, S)] =>
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   212
          if Sign.subsort sign ([class], S) then S
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   213
          else err_bad_axsort name class
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   214
      | _ => err_bad_tfrees name);
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   215
3788
51bd5c0954c6 eliminated raise_term, raise_typ;
wenzelm
parents: 3764
diff changeset
   216
    val axS = Sign.norm_sort sign (logicC :: flat (map axm_sort axioms))
404
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   217
1498
7b7d20e89b1b Elimination of fully-functorial style.
paulson
parents: 1237
diff changeset
   218
    val int_axm = Logic.close_form o map_term_tfrees (K (aT axS));
7b7d20e89b1b Elimination of fully-functorial style.
paulson
parents: 1237
diff changeset
   219
    fun inclass c = Logic.mk_inclass (aT axS, c);
404
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   220
1498
7b7d20e89b1b Elimination of fully-functorial style.
paulson
parents: 1237
diff changeset
   221
    val intro_axm = Logic.list_implies
404
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   222
      (map inclass super_classes @ map (int_axm o snd) axioms, inclass class);
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   223
  in
3809
6633694439c0 fixed axiom names;
wenzelm
parents: 3788
diff changeset
   224
    Theory.add_axioms_i ((raw_class ^ "I", intro_axm) :: abs_axioms) thy
404
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   225
  end;
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   226
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   227
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   228
(* external interfaces *)
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   229
3788
51bd5c0954c6 eliminated raise_term, raise_typ;
wenzelm
parents: 3764
diff changeset
   230
val add_axclass = ext_axclass true read_axm;
51bd5c0954c6 eliminated raise_term, raise_typ;
wenzelm
parents: 3764
diff changeset
   231
val add_axclass_i = ext_axclass false cert_axm;
404
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   232
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   233
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   234
423
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   235
(** prove class relations and type arities **)
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   236
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   237
(* class_axms *)
404
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   238
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   239
fun class_axms thy =
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   240
  let
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   241
    val classes = Sign.classes (sign_of thy);
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   242
    val intros = map (fn c => c ^ "I") classes;
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   243
  in
1217
f96a04c6b352 modified prep_thm_axm to handle shyps;
wenzelm
parents: 1201
diff changeset
   244
    map (class_triv thy) classes @
f96a04c6b352 modified prep_thm_axm to handle shyps;
wenzelm
parents: 1201
diff changeset
   245
    get_axioms thy intros
404
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   246
  end;
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   247
423
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   248
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   249
(* axclass_tac *)
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   250
487
af83700cb771 added experimental add_defns (actually should be moved somewhere else);
wenzelm
parents: 474
diff changeset
   251
(*(1) repeatedly resolve goals of form "OFCLASS(ty, c_class)",
1217
f96a04c6b352 modified prep_thm_axm to handle shyps;
wenzelm
parents: 1201
diff changeset
   252
      try class_trivs first, then "cI" axioms
423
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   253
  (2) rewrite goals using user supplied definitions
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   254
  (3) repeatedly resolve goals with user supplied non-definitions*)
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   255
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   256
fun axclass_tac thy thms =
1217
f96a04c6b352 modified prep_thm_axm to handle shyps;
wenzelm
parents: 1201
diff changeset
   257
  let
f96a04c6b352 modified prep_thm_axm to handle shyps;
wenzelm
parents: 1201
diff changeset
   258
    val defs = filter is_def thms;
f96a04c6b352 modified prep_thm_axm to handle shyps;
wenzelm
parents: 1201
diff changeset
   259
    val non_defs = filter_out is_def thms;
f96a04c6b352 modified prep_thm_axm to handle shyps;
wenzelm
parents: 1201
diff changeset
   260
  in
f96a04c6b352 modified prep_thm_axm to handle shyps;
wenzelm
parents: 1201
diff changeset
   261
    TRY (REPEAT_FIRST (resolve_tac (class_axms thy))) THEN
f96a04c6b352 modified prep_thm_axm to handle shyps;
wenzelm
parents: 1201
diff changeset
   262
    TRY (rewrite_goals_tac defs) THEN
f96a04c6b352 modified prep_thm_axm to handle shyps;
wenzelm
parents: 1201
diff changeset
   263
    TRY (REPEAT_FIRST (fn i => assume_tac i ORELSE resolve_tac non_defs i))
f96a04c6b352 modified prep_thm_axm to handle shyps;
wenzelm
parents: 1201
diff changeset
   264
  end;
404
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   265
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   266
423
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   267
(* provers *)
404
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   268
423
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   269
fun prove term_of str_of thy sig_prop thms usr_tac =
404
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   270
  let
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   271
    val sign = sign_of thy;
423
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   272
    val goal = cterm_of sign (term_of sig_prop);
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   273
    val tac = axclass_tac thy thms THEN (if_none usr_tac all_tac);
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   274
  in
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   275
    prove_goalw_cterm [] goal (K [tac])
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   276
  end
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   277
  handle ERROR => error ("The error(s) above occurred while trying to prove "
3854
762606a888fe uses Sign.str_of_classrel, Sign.str_of_arity, Sign.str_of_arity;
wenzelm
parents: 3809
diff changeset
   278
    ^ quote (str_of (sign_of thy, sig_prop)));
404
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   279
638
7f25cc9067e7 prove_subclass, prove_arity now exported;
wenzelm
parents: 560
diff changeset
   280
val prove_subclass =
3854
762606a888fe uses Sign.str_of_classrel, Sign.str_of_arity, Sign.str_of_arity;
wenzelm
parents: 3809
diff changeset
   281
  prove mk_classrel (fn (sg, c1_c2) => Sign.str_of_classrel sg c1_c2);
404
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   282
423
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   283
val prove_arity =
3854
762606a888fe uses Sign.str_of_classrel, Sign.str_of_arity, Sign.str_of_arity;
wenzelm
parents: 3809
diff changeset
   284
  prove mk_arity (fn (sg, (t, Ss, c)) => Sign.str_of_arity sg (t, Ss, [c]));
404
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   285
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   286
423
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   287
449
75ac32497f09 various minor changes (names and comments);
wenzelm
parents: 423
diff changeset
   288
(** add proved subclass relations and arities **)
404
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   289
3949
c60ff6d0a6b8 fixed goal_XXX;
wenzelm
parents: 3938
diff changeset
   290
fun intrn_classrel sg c1_c2 =
c60ff6d0a6b8 fixed goal_XXX;
wenzelm
parents: 3938
diff changeset
   291
  pairself (Sign.intern_class sg) c1_c2;
c60ff6d0a6b8 fixed goal_XXX;
wenzelm
parents: 3938
diff changeset
   292
3788
51bd5c0954c6 eliminated raise_term, raise_typ;
wenzelm
parents: 3764
diff changeset
   293
fun ext_inst_subclass int raw_c1_c2 axms thms usr_tac thy =
51bd5c0954c6 eliminated raise_term, raise_typ;
wenzelm
parents: 3764
diff changeset
   294
  let
3949
c60ff6d0a6b8 fixed goal_XXX;
wenzelm
parents: 3938
diff changeset
   295
    val c1_c2 =
c60ff6d0a6b8 fixed goal_XXX;
wenzelm
parents: 3938
diff changeset
   296
      if int then intrn_classrel (sign_of thy) raw_c1_c2
c60ff6d0a6b8 fixed goal_XXX;
wenzelm
parents: 3938
diff changeset
   297
      else raw_c1_c2;
3788
51bd5c0954c6 eliminated raise_term, raise_typ;
wenzelm
parents: 3764
diff changeset
   298
  in
3854
762606a888fe uses Sign.str_of_classrel, Sign.str_of_arity, Sign.str_of_arity;
wenzelm
parents: 3809
diff changeset
   299
    writeln ("Proving class inclusion " ^
762606a888fe uses Sign.str_of_classrel, Sign.str_of_arity, Sign.str_of_arity;
wenzelm
parents: 3809
diff changeset
   300
      quote (Sign.str_of_classrel (sign_of thy) c1_c2) ^ " ...");
3788
51bd5c0954c6 eliminated raise_term, raise_typ;
wenzelm
parents: 3764
diff changeset
   301
    add_classrel_thms
51bd5c0954c6 eliminated raise_term, raise_typ;
wenzelm
parents: 3764
diff changeset
   302
      [prove_subclass thy c1_c2 (witnesses thy axms thms) usr_tac] thy
51bd5c0954c6 eliminated raise_term, raise_typ;
wenzelm
parents: 3764
diff changeset
   303
  end;
423
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   304
3788
51bd5c0954c6 eliminated raise_term, raise_typ;
wenzelm
parents: 3764
diff changeset
   305
3949
c60ff6d0a6b8 fixed goal_XXX;
wenzelm
parents: 3938
diff changeset
   306
fun intrn_arity sg intrn (t, Ss, x) =
c60ff6d0a6b8 fixed goal_XXX;
wenzelm
parents: 3938
diff changeset
   307
  (Sign.intern_tycon sg t, map (Sign.intern_sort sg) Ss, intrn sg x);
c60ff6d0a6b8 fixed goal_XXX;
wenzelm
parents: 3938
diff changeset
   308
3788
51bd5c0954c6 eliminated raise_term, raise_typ;
wenzelm
parents: 3764
diff changeset
   309
fun ext_inst_arity int (raw_t, raw_Ss, raw_cs) axms thms usr_tac thy =
423
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   310
  let
3788
51bd5c0954c6 eliminated raise_term, raise_typ;
wenzelm
parents: 3764
diff changeset
   311
    val sign = sign_of thy;
51bd5c0954c6 eliminated raise_term, raise_typ;
wenzelm
parents: 3764
diff changeset
   312
    val (t, Ss, cs) =
3949
c60ff6d0a6b8 fixed goal_XXX;
wenzelm
parents: 3938
diff changeset
   313
      if int then intrn_arity sign Sign.intern_sort (raw_t, raw_Ss, raw_cs)
3788
51bd5c0954c6 eliminated raise_term, raise_typ;
wenzelm
parents: 3764
diff changeset
   314
      else (raw_t, raw_Ss, raw_cs);
886
9af08725600b instance: now automatically includes defs of current thy node as witnesses;
wenzelm
parents: 638
diff changeset
   315
    val wthms = witnesses thy axms thms;
423
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   316
    fun prove c =
3854
762606a888fe uses Sign.str_of_classrel, Sign.str_of_arity, Sign.str_of_arity;
wenzelm
parents: 3809
diff changeset
   317
     (writeln ("Proving type arity " ^
762606a888fe uses Sign.str_of_classrel, Sign.str_of_arity, Sign.str_of_arity;
wenzelm
parents: 3809
diff changeset
   318
        quote (Sign.str_of_arity sign (t, Ss, [c])) ^ " ...");
762606a888fe uses Sign.str_of_classrel, Sign.str_of_arity, Sign.str_of_arity;
wenzelm
parents: 3809
diff changeset
   319
        prove_arity thy (t, Ss, c) wthms usr_tac);
423
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   320
  in
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   321
    add_arity_thms (map prove cs) thy
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   322
  end;
404
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   323
3788
51bd5c0954c6 eliminated raise_term, raise_typ;
wenzelm
parents: 3764
diff changeset
   324
val add_inst_subclass = ext_inst_subclass true;
51bd5c0954c6 eliminated raise_term, raise_typ;
wenzelm
parents: 3764
diff changeset
   325
val add_inst_subclass_i = ext_inst_subclass false;
51bd5c0954c6 eliminated raise_term, raise_typ;
wenzelm
parents: 3764
diff changeset
   326
val add_inst_arity = ext_inst_arity true;
51bd5c0954c6 eliminated raise_term, raise_typ;
wenzelm
parents: 3764
diff changeset
   327
val add_inst_arity_i = ext_inst_arity false;
51bd5c0954c6 eliminated raise_term, raise_typ;
wenzelm
parents: 3764
diff changeset
   328
404
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   329
3949
c60ff6d0a6b8 fixed goal_XXX;
wenzelm
parents: 3938
diff changeset
   330
(* make goals (for interactive use) *)
c60ff6d0a6b8 fixed goal_XXX;
wenzelm
parents: 3938
diff changeset
   331
c60ff6d0a6b8 fixed goal_XXX;
wenzelm
parents: 3938
diff changeset
   332
fun mk_goal term_of thy sig_prop =
c60ff6d0a6b8 fixed goal_XXX;
wenzelm
parents: 3938
diff changeset
   333
  goalw_cterm [] (cterm_of (sign_of thy) (term_of sig_prop));
c60ff6d0a6b8 fixed goal_XXX;
wenzelm
parents: 3938
diff changeset
   334
c60ff6d0a6b8 fixed goal_XXX;
wenzelm
parents: 3938
diff changeset
   335
fun goal_subclass thy =
c60ff6d0a6b8 fixed goal_XXX;
wenzelm
parents: 3938
diff changeset
   336
  mk_goal (mk_classrel o intrn_classrel (sign_of thy)) thy;
c60ff6d0a6b8 fixed goal_XXX;
wenzelm
parents: 3938
diff changeset
   337
c60ff6d0a6b8 fixed goal_XXX;
wenzelm
parents: 3938
diff changeset
   338
fun goal_arity thy =
c60ff6d0a6b8 fixed goal_XXX;
wenzelm
parents: 3938
diff changeset
   339
  mk_goal (mk_arity o intrn_arity (sign_of thy) Sign.intern_class) thy;
c60ff6d0a6b8 fixed goal_XXX;
wenzelm
parents: 3938
diff changeset
   340
c60ff6d0a6b8 fixed goal_XXX;
wenzelm
parents: 3938
diff changeset
   341
404
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   342
end;