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