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