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