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