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