src/Pure/axclass.ML
author wenzelm
Fri, 19 Aug 1994 15:39:52 +0200
changeset 560 6702a715281d
parent 487 af83700cb771
child 638 7f25cc9067e7
permissions -rw-r--r--
cleaned sig; removed add_defns (now in drule.ML as add_defs); removed add_sigclass; minor internal changes;
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 =
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
560
6702a715281d cleaned sig;
wenzelm
parents: 487
diff changeset
    11
  local open Tactical Tactical.Thm in
423
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
449
75ac32497f09 various minor changes (names and comments);
wenzelm
parents: 423
diff changeset
    19
    val add_inst_subclass: class * class -> string list -> thm list
404
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    20
      -> tactic option -> theory -> theory
449
75ac32497f09 various minor changes (names and comments);
wenzelm
parents: 423
diff changeset
    21
    val add_inst_arity: string * sort list * class list -> string list
423
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    22
      -> thm list -> tactic option -> theory -> theory
474
ac1d1988d528 added functor signature constraint;
wenzelm
parents: 449
diff changeset
    23
    val axclass_tac: theory -> thm list -> tactic
ac1d1988d528 added functor signature constraint;
wenzelm
parents: 449
diff changeset
    24
    val goal_subclass: theory -> class * class -> thm list
ac1d1988d528 added functor signature constraint;
wenzelm
parents: 449
diff changeset
    25
    val goal_arity: theory -> string * sort list * class -> thm list
404
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    26
  end
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    27
end;
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    28
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    29
functor AxClassFun(structure Logic: LOGIC and Goals: GOALS and Tactic: TACTIC
474
ac1d1988d528 added functor signature constraint;
wenzelm
parents: 449
diff changeset
    30
  sharing Goals.Tactical = Tactic.Tactical): AX_CLASS =
404
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    31
struct
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    32
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    33
structure Tactical = Goals.Tactical;
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    34
structure Thm = Tactical.Thm;
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    35
structure Sign = Thm.Sign;
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    36
structure Type = Sign.Type;
487
af83700cb771 added experimental add_defns (actually should be moved somewhere else);
wenzelm
parents: 474
diff changeset
    37
structure Pretty = Sign.Syntax.Pretty;
404
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
open Logic Thm Tactical Tactic Goals;
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
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    42
(** utilities **)
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
(* type vars *)
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
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
    47
  | map_typ_frees f (TFree a) = f a
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    48
  | map_typ_frees _ a = a;
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
val map_term_tfrees = map_term_types o map_typ_frees;
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
fun aT S = TFree ("'a", S);
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    53
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    54
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    55
(* get axioms *)
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    56
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    57
fun get_ax thy name =
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    58
  Some (get_axiom thy name) handle THEORY _ => None;
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
val get_axioms = mapfilter o get_ax;
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
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    63
(* is_defn *)
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    64
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    65
fun is_defn thm =
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    66
  (case #prop (rep_thm thm) of
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    67
    Const ("==", _) $ _ $ _ => true
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
    68
  | _ => false);
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
560
6702a715281d cleaned sig;
wenzelm
parents: 487
diff changeset
    72
(** abstract syntax operations **)
423
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    73
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    74
(* subclass relations as terms *)
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    75
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    76
fun mk_classrel (c1, c2) = mk_inclass (aT [c1], c2);
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    77
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    78
fun dest_classrel tm =
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    79
  let
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    80
    fun err () = raise_term "dest_classrel" [tm];
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    81
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    82
    val (ty, c2) = dest_inclass (freeze_vars tm) handle TERM _ => err ();
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    83
    val c1 = (case ty of TFree (_, [c]) => c | _ => err ());
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    84
  in
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    85
    (c1, c2)
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    86
  end;
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    87
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    88
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    89
(* arities as terms *)
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    90
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    91
fun mk_arity (t, ss, c) =
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    92
  let
449
75ac32497f09 various minor changes (names and comments);
wenzelm
parents: 423
diff changeset
    93
    val names = tl (variantlist (replicate (length ss + 1) "'", []));
423
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    94
    val tfrees = map TFree (names ~~ ss);
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    95
  in
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    96
    mk_inclass (Type (t, tfrees), c)
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    97
  end;
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    98
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
    99
fun dest_arity tm =
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   100
  let
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   101
    fun err () = raise_term "dest_arity" [tm];
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   102
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   103
    val (ty, c) = dest_inclass (freeze_vars tm) handle TERM _ => err ();
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   104
    val (t, tfrees) =
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   105
      (case ty of
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   106
        Type (t, tys) => (t, map (fn TFree x => x | _ => err ()) tys)
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   107
      | _ => err ());
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   108
    val ss =
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   109
      if null (gen_duplicates eq_fst tfrees)
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   110
      then map snd tfrees else err ();
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   111
  in
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   112
    (t, ss, 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
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   116
560
6702a715281d cleaned sig;
wenzelm
parents: 487
diff changeset
   117
(** add theorems as axioms **)
423
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   118
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   119
fun prep_thm_axm thy thm =
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   120
  let
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   121
    fun err msg = raise THM ("prep_thm_axm: " ^ msg, 0, [thm]);
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   122
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   123
    val {sign, hyps, prop, ...} = rep_thm thm;
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   124
  in
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   125
    if not (Sign.subsig (sign, sign_of thy)) then
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   126
      err "theorem not of same theory"
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   127
    else if not (null hyps) then
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   128
      err "theorem may not contain hypotheses"
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   129
    else prop
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   130
  end;
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   131
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   132
(*general theorems*)
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   133
fun add_thms_as_axms thms thy =
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   134
  add_axioms_i (map (apsnd (prep_thm_axm thy)) thms) thy;
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   135
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   136
(*theorems expressing class relations*)
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   137
fun add_classrel_thms thms thy =
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   138
  let
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   139
    fun prep_thm thm =
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   140
      let
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   141
        val prop = prep_thm_axm thy thm;
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   142
        val (c1, c2) = dest_classrel prop handle TERM _ =>
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   143
          raise THM ("add_classrel_thms: theorem is not a class relation", 0, [thm]);
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   144
      in (c1, c2) end;
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   145
  in
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   146
    add_classrel (map prep_thm thms) thy
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   147
  end;
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   148
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   149
(*theorems expressing arities*)
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   150
fun add_arity_thms thms thy =
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   151
  let
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   152
    fun prep_thm thm =
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   153
      let
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   154
        val prop = prep_thm_axm thy thm;
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   155
        val (t, ss, c) = dest_arity prop handle TERM _ =>
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   156
          raise THM ("add_arity_thms: theorem is not an arity", 0, [thm]);
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   157
      in (t, ss, [c]) end;
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   158
  in
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   159
    add_arities (map prep_thm thms) thy
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   160
  end;
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   161
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   162
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   163
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   164
(** add axiomatic type classes **)
404
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   165
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   166
(* errors *)
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_not_logic c =
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   169
  error ("Axiomatic class " ^ quote c ^ " not subclass of \"logic\"");
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_axsort ax c =
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   172
  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
   173
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   174
fun err_bad_tfrees ax =
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   175
  error ("More than one type variable in axiom " ^ quote ax);
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   176
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
(* ext_axclass *)
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 ext_axclass prep_axm (class, super_classes) raw_axioms old_thy =
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   181
  let
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   182
    val axioms = map (prep_axm (sign_of old_thy)) raw_axioms;
560
6702a715281d cleaned sig;
wenzelm
parents: 487
diff changeset
   183
    val thy = add_classes [(class, super_classes)] old_thy;
404
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   184
    val sign = sign_of thy;
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
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   187
    (* prepare abstract axioms *)
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
    fun abs_axm ax =
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   190
      if null (term_tfrees ax) then
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   191
        mk_implies (mk_inclass (aT logicS, class), ax)
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   192
      else
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   193
        map_term_tfrees (K (aT [class])) ax;
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   194
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   195
    val abs_axioms = map (apsnd abs_axm) axioms;
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   196
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
    (* prepare introduction orule *)
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   199
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   200
    val _ =
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   201
      if Sign.subsort sign ([class], logicS) then ()
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   202
      else err_not_logic class;
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
    fun axm_sort (name, ax) =
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   205
      (case term_tfrees ax of
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
      | [(_, S)] =>
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   208
          if Sign.subsort sign ([class], S) then S
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   209
          else err_bad_axsort name class
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   210
      | _ => err_bad_tfrees name);
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 axS = Sign.norm_sort sign (logicC :: flat (map axm_sort axioms));
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
    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
   215
    fun inclass c = mk_inclass (aT axS, c);
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   216
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   217
    val intro_axm = list_implies
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   218
      (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
   219
  in
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   220
    add_axioms_i ((class ^ "I", intro_axm) :: abs_axioms) thy
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   221
  end;
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
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   224
(* external interfaces *)
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 add_axclass = ext_axclass read_axm;
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   227
val add_axclass_i = ext_axclass cert_axm;
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
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   230
423
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   231
(** prove class relations and type arities **)
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   232
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   233
(* class_axms *)
404
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
fun class_axms thy =
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   236
  let
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   237
    val classes = Sign.classes (sign_of thy);
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   238
    val intros = map (fn c => c ^ "I") classes;
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   239
  in
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   240
    get_axioms thy intros @
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   241
    map (class_triv thy) classes
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   242
  end;
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   243
423
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   244
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   245
(* axclass_tac *)
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   246
487
af83700cb771 added experimental add_defns (actually should be moved somewhere else);
wenzelm
parents: 474
diff changeset
   247
(*(1) repeatedly resolve goals of form "OFCLASS(ty, c_class)",
423
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   248
      try "cI" axioms first and use class_triv as last resort
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   249
  (2) rewrite goals using user supplied definitions
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   250
  (3) repeatedly resolve goals with user supplied non-definitions*)
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   251
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   252
fun axclass_tac thy thms =
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   253
  TRY (REPEAT_FIRST (resolve_tac (class_axms thy))) THEN
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   254
  TRY (rewrite_goals_tac (filter is_defn thms)) THEN
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   255
  TRY (REPEAT_FIRST (resolve_tac (filter_out is_defn thms)));
404
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   256
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   257
423
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   258
(* provers *)
404
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   259
423
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   260
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
   261
  let
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   262
    val sign = sign_of thy;
423
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   263
    val goal = cterm_of sign (term_of sig_prop);
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   264
    val tac = axclass_tac thy thms THEN (if_none usr_tac all_tac);
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   265
  in
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   266
    prove_goalw_cterm [] goal (K [tac])
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   267
  end
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   268
  handle ERROR => error ("The error(s) above occurred while trying to prove "
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   269
    ^ quote (str_of sig_prop));
404
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   270
423
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   271
val prove_classrel =
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   272
  prove mk_classrel (fn (c1, c2) => c1 ^ " < " ^ c2);
404
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   273
423
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   274
val prove_arity =
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   275
  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
   276
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   277
423
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   278
(* make goals (for interactive use) *)
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   279
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   280
fun mk_goal term_of thy sig_prop =
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   281
  goalw_cterm [] (cterm_of (sign_of thy) (term_of sig_prop));
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   282
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   283
val goal_subclass = mk_goal mk_classrel;
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   284
val goal_arity = mk_goal mk_arity;
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   285
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   286
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
449
75ac32497f09 various minor changes (names and comments);
wenzelm
parents: 423
diff changeset
   290
fun add_inst_subclass (c1, c2) axms thms usr_tac thy =
423
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   291
  add_classrel_thms
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   292
  [prove_classrel thy (c1, c2) (get_axioms thy axms @ thms) usr_tac] thy;
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   293
449
75ac32497f09 various minor changes (names and comments);
wenzelm
parents: 423
diff changeset
   294
fun add_inst_arity (t, ss, cs) axms thms usr_tac thy =
423
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   295
  let
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   296
    val usr_thms = get_axioms thy axms @ thms;
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   297
    fun prove c =
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   298
      prove_arity thy (t, ss, c) usr_thms usr_tac;
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   299
  in
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   300
    add_arity_thms (map prove cs) thy
a42892e72854 (beta release)
wenzelm
parents: 404
diff changeset
   301
  end;
404
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   302
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   303
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   304
end;
dd3d3d6467db axiomatic type class 'package' for Pure (alpha version);
wenzelm
parents:
diff changeset
   305