src/Pure/axclass.ML
author wenzelm
Thu, 16 Jun 1994 12:05:53 +0200
changeset 423 a42892e72854
parent 404 dd3d3d6467db
child 449 75ac32497f09
permissions -rw-r--r--
(beta release)
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
423
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
     5
Higher level 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 =
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
     9
sig
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    10
  structure Tactical: TACTICAL
423
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    11
  local open Tactical Tactical.Thm Tactical.Thm.Sign.Syntax.Mixfix in
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    12
    val add_thms_as_axms: (string * thm) list -> theory -> theory
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    13
    val add_classrel_thms: thm list -> theory -> theory
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    14
    val add_arity_thms: thm list -> theory -> theory
404
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    15
    val add_axclass: class * class list -> (string * string) list
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    16
      -> theory -> theory
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    17
    val add_axclass_i: class * class list -> (string * term) list
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    18
      -> theory -> theory
423
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    19
    val add_sigclass: class * class list -> (string * string * mixfix) list
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    20
      -> theory -> theory
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    21
    val add_sigclass_i: class * class list -> (string * typ * mixfix) list
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    22
      -> theory -> theory
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    23
    val axclass_tac: theory -> thm list -> tactic
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    24
    val prove_classrel: theory -> class * class -> thm list
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    25
      -> tactic option -> thm
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    26
    val prove_arity: theory -> string * sort list * class -> thm list
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    27
      -> tactic option -> thm
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    28
    val add_subclass: class * class -> string list -> thm list
404
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    29
      -> tactic option -> theory -> theory
423
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    30
    val add_instance: string * sort list * class list -> string list
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    31
      -> thm list -> tactic option -> theory -> theory
404
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    32
  end
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    33
end;
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    34
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    35
functor AxClassFun(structure Logic: LOGIC and Goals: GOALS and Tactic: TACTIC
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    36
  sharing Goals.Tactical = Tactic.Tactical)(*: AX_CLASS *) = (* FIXME *)
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    37
struct
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
structure Tactical = Goals.Tactical;
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    40
structure Thm = Tactical.Thm;
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    41
structure Sign = Thm.Sign;
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    42
structure Type = Sign.Type;
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    43
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    44
open Logic Thm Tactical Tactic Goals;
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    45
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    46
423
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    47
(* FIXME fake! - remove *)
404
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    48
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    49
val add_defns = add_axioms;
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    50
val add_defns_i = add_axioms_i;
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    51
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    52
423
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    53
404
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    54
(** utilities **)
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
(* type vars *)
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    57
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    58
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
    59
  | map_typ_frees f (TFree a) = f a
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    60
  | map_typ_frees _ a = a;
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    61
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    62
val map_term_tfrees = map_term_types o map_typ_frees;
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    63
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    64
fun aT S = TFree ("'a", S);
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    65
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
(* get axioms *)
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    68
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    69
fun get_ax thy name =
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    70
  Some (get_axiom thy name) handle THEORY _ => None;
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    71
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    72
val get_axioms = mapfilter o get_ax;
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    73
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    74
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    75
(* is_defn *)
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    76
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    77
fun is_defn thm =
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    78
  (case #prop (rep_thm thm) of
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    79
    Const ("==", _) $ _ $ _ => true
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    80
  | _ => false);
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    81
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    82
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    83
423
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    84
(** abstract syntax operations **)    (* FIXME -> logic.ML (?) *)
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    85
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    86
(* subclass relations as terms *)
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    87
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    88
fun mk_classrel (c1, c2) = mk_inclass (aT [c1], c2);
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    89
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    90
fun dest_classrel tm =
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    91
  let
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    92
    fun err () = raise_term "dest_classrel" [tm];
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    93
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    94
    val (ty, c2) = dest_inclass (freeze_vars tm) handle TERM _ => err ();
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    95
    val c1 = (case ty of TFree (_, [c]) => c | _ => err ());
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    96
  in
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    97
    (c1, c2)
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    98
  end;
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    99
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   100
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   101
(* arities as terms *)
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   102
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   103
fun mk_arity (t, ss, c) =
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   104
  let
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   105
    val names = variantlist (replicate (length ss) "'a", []);
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   106
    val tfrees = map TFree (names ~~ ss);
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   107
  in
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   108
    mk_inclass (Type (t, tfrees), c)
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   109
  end;
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   110
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   111
fun dest_arity tm =
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   112
  let
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   113
    fun err () = raise_term "dest_arity" [tm];
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   114
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   115
    val (ty, c) = dest_inclass (freeze_vars tm) handle TERM _ => err ();
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   116
    val (t, tfrees) =
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   117
      (case ty of
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   118
        Type (t, tys) => (t, map (fn TFree x => x | _ => err ()) tys)
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   119
      | _ => err ());
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   120
    val ss =
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   121
      if null (gen_duplicates eq_fst tfrees)
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   122
      then map snd tfrees else err ();
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   123
  in
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   124
    (t, ss, c)
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   125
  end;
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   126
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   127
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   128
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   129
(** add theorems as axioms **)    (* FIXME -> drule.ML (?) *)
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   130
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   131
fun prep_thm_axm thy thm =
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   132
  let
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   133
    fun err msg = raise THM ("prep_thm_axm: " ^ msg, 0, [thm]);
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   134
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   135
    val {sign, hyps, prop, ...} = rep_thm thm;
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   136
  in
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   137
    if not (Sign.subsig (sign, sign_of thy)) then
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   138
      err "theorem not of same theory"
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   139
    else if not (null hyps) then
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   140
      err "theorem may not contain hypotheses"
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   141
    else prop
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   142
  end;
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   143
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   144
(*general theorems*)
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   145
fun add_thms_as_axms thms thy =
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   146
  add_axioms_i (map (apsnd (prep_thm_axm thy)) thms) thy;
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   147
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   148
(*theorems expressing class relations*)
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   149
fun add_classrel_thms thms thy =
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   150
  let
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   151
    fun prep_thm thm =
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   152
      let
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   153
        val prop = prep_thm_axm thy thm;
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   154
        val (c1, c2) = dest_classrel prop handle TERM _ =>
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   155
          raise THM ("add_classrel_thms: theorem is not a class relation", 0, [thm]);
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   156
      in (c1, c2) end;
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   157
  in
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   158
    add_classrel (map prep_thm thms) thy
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   159
  end;
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   160
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   161
(*theorems expressing arities*)
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   162
fun add_arity_thms thms thy =
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   163
  let
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   164
    fun prep_thm thm =
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   165
      let
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   166
        val prop = prep_thm_axm thy thm;
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   167
        val (t, ss, c) = dest_arity prop handle TERM _ =>
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   168
          raise THM ("add_arity_thms: theorem is not an arity", 0, [thm]);
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   169
      in (t, ss, [c]) end;
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   170
  in
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   171
    add_arities (map prep_thm thms) thy
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   172
  end;
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   173
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   174
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   175
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   176
(** add axiomatic type classes **)
404
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   177
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   178
(* errors *)
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   179
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   180
fun err_not_logic c =
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   181
  error ("Axiomatic class " ^ quote c ^ " not subclass of \"logic\"");
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   182
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   183
fun err_bad_axsort ax c =
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   184
  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
   185
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   186
fun err_bad_tfrees ax =
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   187
  error ("More than one type variable in axiom " ^ quote ax);
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   188
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   189
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   190
(* ext_axclass *)
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
fun ext_axclass prep_axm (class, super_classes) raw_axioms old_thy =
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   193
  let
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   194
    val axioms = map (prep_axm (sign_of old_thy)) raw_axioms;
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   195
    val thy = add_classes [([], class, super_classes)] old_thy;
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   196
    val sign = sign_of thy;
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   197
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
    (* prepare abstract 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
    fun abs_axm ax =
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   202
      if null (term_tfrees ax) then
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   203
        mk_implies (mk_inclass (aT logicS, class), ax)
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   204
      else
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   205
        map_term_tfrees (K (aT [class])) ax;
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   206
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   207
    val abs_axioms = map (apsnd abs_axm) axioms;
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   208
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   209
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   210
    (* prepare introduction orule *)
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   211
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   212
    val _ =
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   213
      if Sign.subsort sign ([class], logicS) then ()
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   214
      else err_not_logic class;
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   215
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   216
    fun axm_sort (name, ax) =
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   217
      (case term_tfrees ax of
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   218
        [] => []
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   219
      | [(_, S)] =>
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   220
          if Sign.subsort sign ([class], S) then S
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   221
          else err_bad_axsort name class
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   222
      | _ => err_bad_tfrees name);
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   223
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   224
    val axS = Sign.norm_sort sign (logicC :: flat (map axm_sort axioms));
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   225
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   226
    val int_axm = close_form o map_term_tfrees (K (aT axS));
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   227
    fun inclass c = mk_inclass (aT axS, c);
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   228
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   229
    val intro_axm = list_implies
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   230
      (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
   231
  in
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   232
    add_axioms_i ((class ^ "I", intro_axm) :: abs_axioms) thy
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   233
  end;
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   234
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   235
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   236
(* external interfaces *)
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   237
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   238
val add_axclass = ext_axclass read_axm;
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   239
val add_axclass_i = ext_axclass cert_axm;
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   240
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   241
423
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   242
(* add signature classes *)
404
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   243
423
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   244
fun ext_sigclass add_cnsts (class, super_classes) consts old_thy =
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   245
  old_thy
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   246
  |> add_axclass (class, super_classes) []
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   247
  |> add_defsort [class]
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   248
  |> add_cnsts consts
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   249
  |> add_defsort (Type.defaultS (#tsig (Sign.rep_sg (sign_of old_thy))));
404
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   250
423
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   251
val add_sigclass = ext_sigclass add_consts;
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   252
val add_sigclass_i = ext_sigclass add_consts_i;
404
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   253
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   254
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   255
423
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   256
(** prove class relations and type arities **)
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   257
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   258
(* class_axms *)
404
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   259
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   260
fun class_axms thy =
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   261
  let
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   262
    val classes = Sign.classes (sign_of thy);
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   263
    val intros = map (fn c => c ^ "I") classes;
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   264
  in
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   265
    get_axioms thy intros @
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   266
    map (class_triv thy) classes
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   267
  end;
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   268
423
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   269
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   270
(* axclass_tac *)
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   271
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   272
(*(1) repeatedly resolve goals of form "(| ty : c_class |)",
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   273
      try "cI" axioms first and use class_triv as last resort
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   274
  (2) rewrite goals using user supplied definitions
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   275
  (3) repeatedly resolve goals with user supplied non-definitions*)
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   276
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   277
fun axclass_tac thy thms =
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   278
  TRY (REPEAT_FIRST (resolve_tac (class_axms thy))) THEN
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   279
  TRY (rewrite_goals_tac (filter is_defn thms)) THEN
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   280
  TRY (REPEAT_FIRST (resolve_tac (filter_out is_defn thms)));
404
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   281
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   282
423
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   283
(* provers *)
404
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   284
423
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   285
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
   286
  let
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   287
    val sign = sign_of thy;
423
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   288
    val goal = cterm_of sign (term_of sig_prop);
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   289
    val tac = axclass_tac thy thms THEN (if_none usr_tac all_tac);
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   290
  in
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   291
    prove_goalw_cterm [] goal (K [tac])
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   292
  end
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   293
  handle ERROR => error ("The error(s) above occurred while trying to prove "
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   294
    ^ quote (str_of sig_prop));
404
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   295
423
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   296
val prove_classrel =
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   297
  prove mk_classrel (fn (c1, c2) => c1 ^ " < " ^ c2);
404
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   298
423
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   299
val prove_arity =
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   300
  prove mk_arity (fn (t, ss, c) => Type.str_of_arity (t, ss, [c]));
404
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   301
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   302
423
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   303
(* make goals (for interactive use) *)
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   304
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   305
fun mk_goal term_of thy sig_prop =
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   306
  goalw_cterm [] (cterm_of (sign_of thy) (term_of sig_prop));
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   307
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   308
val goal_subclass = mk_goal mk_classrel;
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   309
val goal_arity = mk_goal mk_arity;
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   310
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   311
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   312
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   313
(** add proved class relations and instances **)
404
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   314
423
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   315
fun add_subclass (c1, c2) axms thms usr_tac thy =
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   316
  add_classrel_thms
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   317
  [prove_classrel thy (c1, c2) (get_axioms thy axms @ thms) usr_tac] thy;
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   318
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   319
fun add_instance (t, ss, cs) axms thms usr_tac thy =
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   320
  let
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   321
    val usr_thms = get_axioms thy axms @ thms;
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   322
    fun prove c =
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   323
      prove_arity thy (t, ss, c) usr_thms usr_tac;
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   324
  in
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   325
    add_arity_thms (map prove cs) thy
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   326
  end;
404
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   327
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   328
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   329
end;
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   330