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