src/Pure/axclass.ML
author wenzelm
Mon Oct 20 10:52:32 1997 +0200 (1997-10-20)
changeset 3938 c20fbe3cb94f
parent 3854 762606a888fe
child 3949 c60ff6d0a6b8
permissions -rw-r--r--
fixed types of add_XXX;
tuned;
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@3938
     9
sig
paulson@1498
    10
  val add_thms_as_axms: (string * thm) list -> theory -> theory
paulson@1498
    11
  val add_classrel_thms: thm list -> theory -> theory
paulson@1498
    12
  val add_arity_thms: thm list -> theory -> theory
wenzelm@3938
    13
  val add_axclass: rclass * xclass list -> (string * string) list
paulson@1498
    14
    -> theory -> theory
wenzelm@3938
    15
  val add_axclass_i: rclass * class list -> (string * term) list
paulson@1498
    16
    -> theory -> theory
wenzelm@3938
    17
  val add_inst_subclass: xclass * xclass -> string list -> thm list
wenzelm@3938
    18
    -> tactic option -> theory -> theory
wenzelm@3788
    19
  val add_inst_subclass_i: class * class -> string list -> thm list
wenzelm@3788
    20
    -> tactic option -> theory -> theory
wenzelm@3938
    21
  val add_inst_arity: xstring * xsort list * xclass list -> string list
wenzelm@3938
    22
    -> thm list -> tactic option -> theory -> theory
wenzelm@3788
    23
  val add_inst_arity_i: string * sort list * class list -> string list
wenzelm@3788
    24
    -> thm list -> tactic option -> theory -> theory
paulson@1498
    25
  val axclass_tac: theory -> thm list -> tactic
paulson@1498
    26
  val prove_subclass: theory -> class * class -> thm list
paulson@1498
    27
    -> tactic option -> thm
paulson@1498
    28
  val prove_arity: theory -> string * sort list * class -> thm list
paulson@1498
    29
    -> tactic option -> thm
paulson@1498
    30
  val goal_subclass: theory -> class * class -> thm list
paulson@1498
    31
  val goal_arity: theory -> string * sort list * class -> thm list
wenzelm@3938
    32
end;
wenzelm@404
    33
paulson@1498
    34
structure AxClass : AX_CLASS =
wenzelm@404
    35
struct
wenzelm@404
    36
wenzelm@404
    37
(** utilities **)
wenzelm@404
    38
wenzelm@404
    39
(* type vars *)
wenzelm@404
    40
wenzelm@404
    41
fun map_typ_frees f (Type (t, tys)) = Type (t, map (map_typ_frees f) tys)
wenzelm@404
    42
  | map_typ_frees f (TFree a) = f a
wenzelm@404
    43
  | map_typ_frees _ a = a;
wenzelm@404
    44
wenzelm@404
    45
val map_term_tfrees = map_term_types o map_typ_frees;
wenzelm@404
    46
wenzelm@404
    47
fun aT S = TFree ("'a", S);
wenzelm@404
    48
wenzelm@3395
    49
fun dest_varT (TFree (x, S)) = ((x, ~1), S)
wenzelm@3395
    50
  | dest_varT (TVar xi_S) = xi_S
wenzelm@3788
    51
  | dest_varT T = raise TYPE ("dest_varT", [T], []);
wenzelm@3395
    52
wenzelm@404
    53
wenzelm@886
    54
(* get axioms and theorems *)
wenzelm@404
    55
wenzelm@404
    56
fun get_ax thy name =
wenzelm@404
    57
  Some (get_axiom thy name) handle THEORY _ => None;
wenzelm@404
    58
wenzelm@404
    59
val get_axioms = mapfilter o get_ax;
wenzelm@404
    60
paulson@1498
    61
val is_def = Logic.is_equals o #prop o rep_thm;
wenzelm@886
    62
wenzelm@886
    63
fun witnesses thy axms thms =
wenzelm@1201
    64
  map (get_axiom thy) axms @ thms @ filter is_def (map snd (axioms_of thy));
wenzelm@886
    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
paulson@1498
    72
fun mk_classrel (c1, c2) = Logic.mk_inclass (aT [c1], c2);
wenzelm@423
    73
wenzelm@423
    74
fun dest_classrel tm =
wenzelm@423
    75
  let
wenzelm@3788
    76
    fun err () = raise TERM ("dest_classrel", [tm]);
wenzelm@423
    77
wenzelm@3395
    78
    val (ty, c2) = Logic.dest_inclass tm handle TERM _ => err ();
wenzelm@3395
    79
    val c1 = (case dest_varT ty of (_, [c]) => c | _ => err ())
wenzelm@3395
    80
      handle TYPE _ => err ();
wenzelm@423
    81
  in
wenzelm@423
    82
    (c1, c2)
wenzelm@423
    83
  end;
wenzelm@423
    84
wenzelm@423
    85
wenzelm@423
    86
(* arities as terms *)
wenzelm@423
    87
wenzelm@423
    88
fun mk_arity (t, ss, c) =
wenzelm@423
    89
  let
wenzelm@449
    90
    val names = tl (variantlist (replicate (length ss + 1) "'", []));
paulson@2266
    91
    val tfrees = ListPair.map TFree (names, ss);
wenzelm@423
    92
  in
paulson@1498
    93
    Logic.mk_inclass (Type (t, tfrees), c)
wenzelm@423
    94
  end;
wenzelm@423
    95
wenzelm@423
    96
fun dest_arity tm =
wenzelm@423
    97
  let
wenzelm@3788
    98
    fun err () = raise TERM ("dest_arity", [tm]);
wenzelm@423
    99
wenzelm@3395
   100
    val (ty, c) = Logic.dest_inclass tm handle TERM _ => err ();
wenzelm@3395
   101
    val (t, tvars) =
wenzelm@423
   102
      (case ty of
wenzelm@3395
   103
        Type (t, tys) => (t, map dest_varT tys handle TYPE _ => err ())
wenzelm@423
   104
      | _ => err ());
wenzelm@423
   105
    val ss =
wenzelm@3395
   106
      if null (gen_duplicates eq_fst tvars)
wenzelm@3395
   107
      then map snd tvars else err ();
wenzelm@423
   108
  in
wenzelm@423
   109
    (t, ss, c)
wenzelm@423
   110
  end;
wenzelm@423
   111
wenzelm@423
   112
wenzelm@423
   113
wenzelm@560
   114
(** add theorems as axioms **)
wenzelm@423
   115
wenzelm@423
   116
fun prep_thm_axm thy thm =
wenzelm@423
   117
  let
wenzelm@423
   118
    fun err msg = raise THM ("prep_thm_axm: " ^ msg, 0, [thm]);
wenzelm@423
   119
wenzelm@1237
   120
    val {sign, hyps, prop, ...} = rep_thm thm;
wenzelm@423
   121
  in
wenzelm@423
   122
    if not (Sign.subsig (sign, sign_of thy)) then
wenzelm@423
   123
      err "theorem not of same theory"
wenzelm@1237
   124
    else if not (null (extra_shyps thm)) orelse not (null hyps) then
wenzelm@423
   125
      err "theorem may not contain hypotheses"
wenzelm@423
   126
    else prop
wenzelm@423
   127
  end;
wenzelm@423
   128
wenzelm@423
   129
(*general theorems*)
wenzelm@423
   130
fun add_thms_as_axms thms thy =
wenzelm@3764
   131
  Theory.add_axioms_i (map (apsnd (prep_thm_axm thy)) thms) thy;
wenzelm@423
   132
wenzelm@423
   133
(*theorems expressing class relations*)
wenzelm@423
   134
fun add_classrel_thms thms thy =
wenzelm@423
   135
  let
wenzelm@423
   136
    fun prep_thm thm =
wenzelm@423
   137
      let
wenzelm@423
   138
        val prop = prep_thm_axm thy thm;
wenzelm@423
   139
        val (c1, c2) = dest_classrel prop handle TERM _ =>
wenzelm@423
   140
          raise THM ("add_classrel_thms: theorem is not a class relation", 0, [thm]);
wenzelm@423
   141
      in (c1, c2) end;
wenzelm@423
   142
  in
wenzelm@3764
   143
    Theory.add_classrel (map prep_thm thms) thy
wenzelm@423
   144
  end;
wenzelm@423
   145
wenzelm@423
   146
(*theorems expressing arities*)
wenzelm@423
   147
fun add_arity_thms thms thy =
wenzelm@423
   148
  let
wenzelm@423
   149
    fun prep_thm thm =
wenzelm@423
   150
      let
wenzelm@423
   151
        val prop = prep_thm_axm thy thm;
wenzelm@423
   152
        val (t, ss, c) = dest_arity prop handle TERM _ =>
wenzelm@423
   153
          raise THM ("add_arity_thms: theorem is not an arity", 0, [thm]);
wenzelm@423
   154
      in (t, ss, [c]) end;
wenzelm@423
   155
  in
wenzelm@3764
   156
    Theory.add_arities (map prep_thm thms) thy
wenzelm@423
   157
  end;
wenzelm@423
   158
wenzelm@423
   159
wenzelm@423
   160
wenzelm@423
   161
(** add axiomatic type classes **)
wenzelm@404
   162
wenzelm@404
   163
(* errors *)
wenzelm@404
   164
wenzelm@404
   165
fun err_not_logic c =
wenzelm@404
   166
  error ("Axiomatic class " ^ quote c ^ " not subclass of \"logic\"");
wenzelm@404
   167
wenzelm@404
   168
fun err_bad_axsort ax c =
wenzelm@404
   169
  error ("Sort constraint in axiom " ^ quote ax ^ " not supersort of " ^ quote c);
wenzelm@404
   170
wenzelm@404
   171
fun err_bad_tfrees ax =
wenzelm@404
   172
  error ("More than one type variable in axiom " ^ quote ax);
wenzelm@404
   173
wenzelm@404
   174
wenzelm@404
   175
(* ext_axclass *)
wenzelm@404
   176
wenzelm@3788
   177
fun ext_axclass int prep_axm (raw_class, raw_super_classes) raw_axioms old_thy =
wenzelm@404
   178
  let
wenzelm@3938
   179
    val old_sign = sign_of old_thy;
wenzelm@3938
   180
    val axioms = map (prep_axm old_sign) raw_axioms;
wenzelm@3938
   181
    val class = Sign.full_name old_sign raw_class;
wenzelm@3938
   182
wenzelm@3788
   183
    val thy =
wenzelm@3788
   184
      (if int then Theory.add_classes else Theory.add_classes_i)
wenzelm@3788
   185
        [(raw_class, raw_super_classes)] old_thy;
wenzelm@404
   186
    val sign = sign_of thy;
wenzelm@3938
   187
    val super_classes =
wenzelm@3938
   188
      if int then map (Sign.intern_class sign) raw_super_classes
wenzelm@3938
   189
      else raw_super_classes;
wenzelm@404
   190
wenzelm@404
   191
wenzelm@404
   192
    (* prepare abstract axioms *)
wenzelm@404
   193
wenzelm@404
   194
    fun abs_axm ax =
wenzelm@404
   195
      if null (term_tfrees ax) then
paulson@1498
   196
        Logic.mk_implies (Logic.mk_inclass (aT logicS, class), ax)
wenzelm@3788
   197
      else map_term_tfrees (K (aT [class])) ax;
wenzelm@404
   198
wenzelm@404
   199
    val abs_axioms = map (apsnd abs_axm) axioms;
wenzelm@404
   200
wenzelm@404
   201
wenzelm@404
   202
    (* prepare introduction orule *)
wenzelm@404
   203
wenzelm@404
   204
    val _ =
wenzelm@404
   205
      if Sign.subsort sign ([class], logicS) then ()
wenzelm@404
   206
      else err_not_logic class;
wenzelm@404
   207
wenzelm@404
   208
    fun axm_sort (name, ax) =
wenzelm@404
   209
      (case term_tfrees ax of
wenzelm@404
   210
        [] => []
wenzelm@404
   211
      | [(_, S)] =>
wenzelm@404
   212
          if Sign.subsort sign ([class], S) then S
wenzelm@404
   213
          else err_bad_axsort name class
wenzelm@404
   214
      | _ => err_bad_tfrees name);
wenzelm@404
   215
wenzelm@3788
   216
    val axS = Sign.norm_sort sign (logicC :: flat (map axm_sort axioms))
wenzelm@404
   217
paulson@1498
   218
    val int_axm = Logic.close_form o map_term_tfrees (K (aT axS));
paulson@1498
   219
    fun inclass c = Logic.mk_inclass (aT axS, c);
wenzelm@404
   220
paulson@1498
   221
    val intro_axm = Logic.list_implies
wenzelm@404
   222
      (map inclass super_classes @ map (int_axm o snd) axioms, inclass class);
wenzelm@404
   223
  in
wenzelm@3809
   224
    Theory.add_axioms_i ((raw_class ^ "I", intro_axm) :: abs_axioms) thy
wenzelm@404
   225
  end;
wenzelm@404
   226
wenzelm@404
   227
wenzelm@404
   228
(* external interfaces *)
wenzelm@404
   229
wenzelm@3788
   230
val add_axclass = ext_axclass true read_axm;
wenzelm@3788
   231
val add_axclass_i = ext_axclass false cert_axm;
wenzelm@404
   232
wenzelm@404
   233
wenzelm@404
   234
wenzelm@423
   235
(** prove class relations and type arities **)
wenzelm@423
   236
wenzelm@423
   237
(* class_axms *)
wenzelm@404
   238
wenzelm@404
   239
fun class_axms thy =
wenzelm@404
   240
  let
wenzelm@404
   241
    val classes = Sign.classes (sign_of thy);
wenzelm@404
   242
    val intros = map (fn c => c ^ "I") classes;
wenzelm@404
   243
  in
wenzelm@1217
   244
    map (class_triv thy) classes @
wenzelm@1217
   245
    get_axioms thy intros
wenzelm@404
   246
  end;
wenzelm@404
   247
wenzelm@423
   248
wenzelm@423
   249
(* axclass_tac *)
wenzelm@423
   250
wenzelm@487
   251
(*(1) repeatedly resolve goals of form "OFCLASS(ty, c_class)",
wenzelm@1217
   252
      try class_trivs first, then "cI" axioms
wenzelm@423
   253
  (2) rewrite goals using user supplied definitions
wenzelm@423
   254
  (3) repeatedly resolve goals with user supplied non-definitions*)
wenzelm@423
   255
wenzelm@423
   256
fun axclass_tac thy thms =
wenzelm@1217
   257
  let
wenzelm@1217
   258
    val defs = filter is_def thms;
wenzelm@1217
   259
    val non_defs = filter_out is_def thms;
wenzelm@1217
   260
  in
wenzelm@1217
   261
    TRY (REPEAT_FIRST (resolve_tac (class_axms thy))) THEN
wenzelm@1217
   262
    TRY (rewrite_goals_tac defs) THEN
wenzelm@1217
   263
    TRY (REPEAT_FIRST (fn i => assume_tac i ORELSE resolve_tac non_defs i))
wenzelm@1217
   264
  end;
wenzelm@404
   265
wenzelm@404
   266
wenzelm@423
   267
(* provers *)
wenzelm@404
   268
wenzelm@423
   269
fun prove term_of str_of thy sig_prop thms usr_tac =
wenzelm@404
   270
  let
wenzelm@404
   271
    val sign = sign_of thy;
wenzelm@423
   272
    val goal = cterm_of sign (term_of sig_prop);
wenzelm@423
   273
    val tac = axclass_tac thy thms THEN (if_none usr_tac all_tac);
wenzelm@423
   274
  in
wenzelm@423
   275
    prove_goalw_cterm [] goal (K [tac])
wenzelm@423
   276
  end
wenzelm@423
   277
  handle ERROR => error ("The error(s) above occurred while trying to prove "
wenzelm@3854
   278
    ^ quote (str_of (sign_of thy, sig_prop)));
wenzelm@404
   279
wenzelm@638
   280
val prove_subclass =
wenzelm@3854
   281
  prove mk_classrel (fn (sg, c1_c2) => Sign.str_of_classrel sg c1_c2);
wenzelm@404
   282
wenzelm@423
   283
val prove_arity =
wenzelm@3854
   284
  prove mk_arity (fn (sg, (t, Ss, c)) => Sign.str_of_arity sg (t, Ss, [c]));
wenzelm@404
   285
wenzelm@404
   286
wenzelm@423
   287
(* make goals (for interactive use) *)
wenzelm@423
   288
wenzelm@423
   289
fun mk_goal term_of thy sig_prop =
wenzelm@423
   290
  goalw_cterm [] (cterm_of (sign_of thy) (term_of sig_prop));
wenzelm@423
   291
wenzelm@423
   292
val goal_subclass = mk_goal mk_classrel;
wenzelm@423
   293
val goal_arity = mk_goal mk_arity;
wenzelm@423
   294
wenzelm@423
   295
wenzelm@423
   296
wenzelm@449
   297
(** add proved subclass relations and arities **)
wenzelm@404
   298
wenzelm@3788
   299
fun ext_inst_subclass int raw_c1_c2 axms thms usr_tac thy =
wenzelm@3788
   300
  let
wenzelm@3788
   301
    val intrn = if int then pairself (Sign.intern_class (sign_of thy)) else I;
wenzelm@3788
   302
    val c1_c2 = intrn raw_c1_c2;
wenzelm@3788
   303
  in
wenzelm@3854
   304
    writeln ("Proving class inclusion " ^
wenzelm@3854
   305
      quote (Sign.str_of_classrel (sign_of thy) c1_c2) ^ " ...");
wenzelm@3788
   306
    add_classrel_thms
wenzelm@3788
   307
      [prove_subclass thy c1_c2 (witnesses thy axms thms) usr_tac] thy
wenzelm@3788
   308
  end;
wenzelm@423
   309
wenzelm@3788
   310
wenzelm@3788
   311
fun ext_inst_arity int (raw_t, raw_Ss, raw_cs) axms thms usr_tac thy =
wenzelm@423
   312
  let
wenzelm@3788
   313
    val sign = sign_of thy;
wenzelm@3788
   314
    val (t, Ss, cs) =
wenzelm@3788
   315
      if int then
wenzelm@3788
   316
        (Sign.intern_tycon sign raw_t,
wenzelm@3788
   317
          map (Sign.intern_sort sign) raw_Ss,
wenzelm@3788
   318
          map (Sign.intern_class sign) raw_cs)
wenzelm@3788
   319
      else (raw_t, raw_Ss, raw_cs);
wenzelm@886
   320
    val wthms = witnesses thy axms thms;
wenzelm@423
   321
    fun prove c =
wenzelm@3854
   322
     (writeln ("Proving type arity " ^
wenzelm@3854
   323
        quote (Sign.str_of_arity sign (t, Ss, [c])) ^ " ...");
wenzelm@3854
   324
        prove_arity thy (t, Ss, c) wthms usr_tac);
wenzelm@423
   325
  in
wenzelm@423
   326
    add_arity_thms (map prove cs) thy
wenzelm@423
   327
  end;
wenzelm@404
   328
wenzelm@3788
   329
val add_inst_subclass = ext_inst_subclass true;
wenzelm@3788
   330
val add_inst_subclass_i = ext_inst_subclass false;
wenzelm@3788
   331
val add_inst_arity = ext_inst_arity true;
wenzelm@3788
   332
val add_inst_arity_i = ext_inst_arity false;
wenzelm@3788
   333
wenzelm@404
   334
wenzelm@404
   335
end;