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