src/Pure/axclass.ML
author wenzelm
Tue Oct 28 17:27:10 1997 +0100 (1997-10-28 ago)
changeset 4015 92874142156b
parent 3988 6ff1a1e2bd21
child 4845 fdc7d8949d82
permissions -rw-r--r--
add_store_axioms_i;
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_classrel_thms: thm list -> theory -> theory
paulson@1498
    11
  val add_arity_thms: thm list -> theory -> theory
wenzelm@3956
    12
  val add_axclass: bclass * xclass list -> (string * string) list
paulson@1498
    13
    -> theory -> theory
wenzelm@3956
    14
  val add_axclass_i: bclass * class list -> (string * term) list
paulson@1498
    15
    -> theory -> theory
wenzelm@3938
    16
  val add_inst_subclass: xclass * xclass -> string list -> thm list
wenzelm@3938
    17
    -> tactic option -> theory -> theory
wenzelm@3788
    18
  val add_inst_subclass_i: class * class -> string list -> thm list
wenzelm@3788
    19
    -> tactic option -> theory -> theory
wenzelm@3938
    20
  val add_inst_arity: xstring * xsort list * xclass list -> string list
wenzelm@3938
    21
    -> thm list -> tactic option -> theory -> theory
wenzelm@3788
    22
  val add_inst_arity_i: string * sort list * class list -> string list
wenzelm@3788
    23
    -> thm list -> tactic option -> theory -> theory
paulson@1498
    24
  val axclass_tac: theory -> thm list -> tactic
paulson@1498
    25
  val prove_subclass: theory -> class * class -> thm list
paulson@1498
    26
    -> tactic option -> thm
paulson@1498
    27
  val prove_arity: theory -> string * sort list * class -> thm list
paulson@1498
    28
    -> tactic option -> thm
wenzelm@3949
    29
  val goal_subclass: theory -> xclass * xclass -> thm list
wenzelm@3949
    30
  val goal_arity: theory -> xstring * xsort list * xclass -> thm list
wenzelm@3938
    31
end;
wenzelm@404
    32
paulson@1498
    33
structure AxClass : AX_CLASS =
wenzelm@404
    34
struct
wenzelm@404
    35
wenzelm@4015
    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
(*theorems expressing class relations*)
wenzelm@423
   130
fun add_classrel_thms thms thy =
wenzelm@423
   131
  let
wenzelm@423
   132
    fun prep_thm thm =
wenzelm@423
   133
      let
wenzelm@423
   134
        val prop = prep_thm_axm thy thm;
wenzelm@423
   135
        val (c1, c2) = dest_classrel prop handle TERM _ =>
wenzelm@423
   136
          raise THM ("add_classrel_thms: theorem is not a class relation", 0, [thm]);
wenzelm@423
   137
      in (c1, c2) end;
wenzelm@423
   138
  in
wenzelm@3764
   139
    Theory.add_classrel (map prep_thm thms) thy
wenzelm@423
   140
  end;
wenzelm@423
   141
wenzelm@423
   142
(*theorems expressing arities*)
wenzelm@423
   143
fun add_arity_thms thms thy =
wenzelm@423
   144
  let
wenzelm@423
   145
    fun prep_thm thm =
wenzelm@423
   146
      let
wenzelm@423
   147
        val prop = prep_thm_axm thy thm;
wenzelm@423
   148
        val (t, ss, c) = dest_arity prop handle TERM _ =>
wenzelm@423
   149
          raise THM ("add_arity_thms: theorem is not an arity", 0, [thm]);
wenzelm@423
   150
      in (t, ss, [c]) end;
wenzelm@423
   151
  in
wenzelm@3764
   152
    Theory.add_arities (map prep_thm thms) thy
wenzelm@423
   153
  end;
wenzelm@423
   154
wenzelm@423
   155
wenzelm@423
   156
wenzelm@423
   157
(** add axiomatic type classes **)
wenzelm@404
   158
wenzelm@404
   159
(* errors *)
wenzelm@404
   160
wenzelm@404
   161
fun err_not_logic c =
wenzelm@404
   162
  error ("Axiomatic class " ^ quote c ^ " not subclass of \"logic\"");
wenzelm@404
   163
wenzelm@404
   164
fun err_bad_axsort ax c =
wenzelm@404
   165
  error ("Sort constraint in axiom " ^ quote ax ^ " not supersort of " ^ quote c);
wenzelm@404
   166
wenzelm@404
   167
fun err_bad_tfrees ax =
wenzelm@404
   168
  error ("More than one type variable in axiom " ^ quote ax);
wenzelm@404
   169
wenzelm@404
   170
wenzelm@404
   171
(* ext_axclass *)
wenzelm@404
   172
wenzelm@3788
   173
fun ext_axclass int prep_axm (raw_class, raw_super_classes) raw_axioms old_thy =
wenzelm@404
   174
  let
wenzelm@3938
   175
    val old_sign = sign_of old_thy;
wenzelm@3938
   176
    val axioms = map (prep_axm old_sign) raw_axioms;
wenzelm@3938
   177
    val class = Sign.full_name old_sign raw_class;
wenzelm@3938
   178
wenzelm@3788
   179
    val thy =
wenzelm@3788
   180
      (if int then Theory.add_classes else Theory.add_classes_i)
wenzelm@3788
   181
        [(raw_class, raw_super_classes)] old_thy;
wenzelm@404
   182
    val sign = sign_of thy;
wenzelm@3938
   183
    val super_classes =
wenzelm@3938
   184
      if int then map (Sign.intern_class sign) raw_super_classes
wenzelm@3938
   185
      else raw_super_classes;
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
paulson@1498
   192
        Logic.mk_implies (Logic.mk_inclass (aT logicS, class), ax)
wenzelm@3788
   193
      else map_term_tfrees (K (aT [class])) ax;
wenzelm@404
   194
wenzelm@404
   195
    val abs_axioms = map (apsnd abs_axm) axioms;
wenzelm@404
   196
wenzelm@404
   197
wenzelm@404
   198
    (* prepare introduction orule *)
wenzelm@404
   199
wenzelm@404
   200
    val _ =
wenzelm@404
   201
      if Sign.subsort sign ([class], logicS) then ()
wenzelm@404
   202
      else err_not_logic class;
wenzelm@404
   203
wenzelm@404
   204
    fun axm_sort (name, ax) =
wenzelm@404
   205
      (case term_tfrees ax of
wenzelm@404
   206
        [] => []
wenzelm@404
   207
      | [(_, S)] =>
wenzelm@404
   208
          if Sign.subsort sign ([class], S) then S
wenzelm@404
   209
          else err_bad_axsort name class
wenzelm@404
   210
      | _ => err_bad_tfrees name);
wenzelm@404
   211
wenzelm@3788
   212
    val axS = Sign.norm_sort sign (logicC :: flat (map axm_sort axioms))
wenzelm@404
   213
paulson@1498
   214
    val int_axm = Logic.close_form o map_term_tfrees (K (aT axS));
paulson@1498
   215
    fun inclass c = Logic.mk_inclass (aT axS, c);
wenzelm@404
   216
paulson@1498
   217
    val intro_axm = Logic.list_implies
wenzelm@404
   218
      (map inclass super_classes @ map (int_axm o snd) axioms, inclass class);
wenzelm@404
   219
  in
wenzelm@4015
   220
    PureThy.add_store_axioms_i ((raw_class ^ "I", intro_axm) :: abs_axioms) thy
wenzelm@404
   221
  end;
wenzelm@404
   222
wenzelm@404
   223
wenzelm@404
   224
(* external interfaces *)
wenzelm@404
   225
wenzelm@3788
   226
val add_axclass = ext_axclass true read_axm;
wenzelm@3788
   227
val add_axclass_i = ext_axclass false cert_axm;
wenzelm@404
   228
wenzelm@404
   229
wenzelm@404
   230
wenzelm@423
   231
(** prove class relations and type arities **)
wenzelm@423
   232
wenzelm@423
   233
(* class_axms *)
wenzelm@404
   234
wenzelm@404
   235
fun class_axms thy =
wenzelm@404
   236
  let
wenzelm@404
   237
    val classes = Sign.classes (sign_of thy);
wenzelm@404
   238
    val intros = map (fn c => c ^ "I") classes;
wenzelm@404
   239
  in
wenzelm@1217
   240
    map (class_triv thy) classes @
wenzelm@1217
   241
    get_axioms thy intros
wenzelm@404
   242
  end;
wenzelm@404
   243
wenzelm@423
   244
wenzelm@423
   245
(* axclass_tac *)
wenzelm@423
   246
wenzelm@487
   247
(*(1) repeatedly resolve goals of form "OFCLASS(ty, c_class)",
wenzelm@1217
   248
      try class_trivs first, then "cI" axioms
wenzelm@423
   249
  (2) rewrite goals using user supplied definitions
wenzelm@423
   250
  (3) repeatedly resolve goals with user supplied non-definitions*)
wenzelm@423
   251
wenzelm@423
   252
fun axclass_tac thy thms =
wenzelm@1217
   253
  let
wenzelm@1217
   254
    val defs = filter is_def thms;
wenzelm@1217
   255
    val non_defs = filter_out is_def thms;
wenzelm@1217
   256
  in
wenzelm@1217
   257
    TRY (REPEAT_FIRST (resolve_tac (class_axms thy))) THEN
wenzelm@1217
   258
    TRY (rewrite_goals_tac defs) THEN
wenzelm@1217
   259
    TRY (REPEAT_FIRST (fn i => assume_tac i ORELSE resolve_tac non_defs i))
wenzelm@1217
   260
  end;
wenzelm@404
   261
wenzelm@404
   262
wenzelm@423
   263
(* provers *)
wenzelm@404
   264
wenzelm@423
   265
fun prove term_of str_of thy sig_prop thms usr_tac =
wenzelm@404
   266
  let
wenzelm@404
   267
    val sign = sign_of thy;
wenzelm@423
   268
    val goal = cterm_of sign (term_of sig_prop);
wenzelm@423
   269
    val tac = axclass_tac thy thms THEN (if_none usr_tac all_tac);
wenzelm@423
   270
  in
wenzelm@423
   271
    prove_goalw_cterm [] goal (K [tac])
wenzelm@423
   272
  end
wenzelm@423
   273
  handle ERROR => error ("The error(s) above occurred while trying to prove "
wenzelm@3854
   274
    ^ quote (str_of (sign_of thy, sig_prop)));
wenzelm@404
   275
wenzelm@638
   276
val prove_subclass =
wenzelm@3854
   277
  prove mk_classrel (fn (sg, c1_c2) => Sign.str_of_classrel sg c1_c2);
wenzelm@404
   278
wenzelm@423
   279
val prove_arity =
wenzelm@3854
   280
  prove mk_arity (fn (sg, (t, Ss, c)) => Sign.str_of_arity sg (t, Ss, [c]));
wenzelm@404
   281
wenzelm@404
   282
wenzelm@423
   283
wenzelm@449
   284
(** add proved subclass relations and arities **)
wenzelm@404
   285
wenzelm@3949
   286
fun intrn_classrel sg c1_c2 =
wenzelm@3949
   287
  pairself (Sign.intern_class sg) c1_c2;
wenzelm@3949
   288
wenzelm@3788
   289
fun ext_inst_subclass int raw_c1_c2 axms thms usr_tac thy =
wenzelm@3788
   290
  let
wenzelm@3949
   291
    val c1_c2 =
wenzelm@3949
   292
      if int then intrn_classrel (sign_of thy) raw_c1_c2
wenzelm@3949
   293
      else raw_c1_c2;
wenzelm@3788
   294
  in
wenzelm@3854
   295
    writeln ("Proving class inclusion " ^
wenzelm@3854
   296
      quote (Sign.str_of_classrel (sign_of thy) c1_c2) ^ " ...");
wenzelm@3788
   297
    add_classrel_thms
wenzelm@3788
   298
      [prove_subclass thy c1_c2 (witnesses thy axms thms) usr_tac] thy
wenzelm@3788
   299
  end;
wenzelm@423
   300
wenzelm@3788
   301
wenzelm@3949
   302
fun intrn_arity sg intrn (t, Ss, x) =
wenzelm@3949
   303
  (Sign.intern_tycon sg t, map (Sign.intern_sort sg) Ss, intrn sg x);
wenzelm@3949
   304
wenzelm@3788
   305
fun ext_inst_arity int (raw_t, raw_Ss, raw_cs) axms thms usr_tac thy =
wenzelm@423
   306
  let
wenzelm@3788
   307
    val sign = sign_of thy;
wenzelm@3788
   308
    val (t, Ss, cs) =
wenzelm@3949
   309
      if int then intrn_arity sign Sign.intern_sort (raw_t, raw_Ss, raw_cs)
wenzelm@3788
   310
      else (raw_t, raw_Ss, raw_cs);
wenzelm@886
   311
    val wthms = witnesses thy axms thms;
wenzelm@423
   312
    fun prove c =
wenzelm@3854
   313
     (writeln ("Proving type arity " ^
wenzelm@3854
   314
        quote (Sign.str_of_arity sign (t, Ss, [c])) ^ " ...");
wenzelm@3854
   315
        prove_arity thy (t, Ss, c) wthms usr_tac);
wenzelm@423
   316
  in
wenzelm@423
   317
    add_arity_thms (map prove cs) thy
wenzelm@423
   318
  end;
wenzelm@404
   319
wenzelm@3788
   320
val add_inst_subclass = ext_inst_subclass true;
wenzelm@3788
   321
val add_inst_subclass_i = ext_inst_subclass false;
wenzelm@3788
   322
val add_inst_arity = ext_inst_arity true;
wenzelm@3788
   323
val add_inst_arity_i = ext_inst_arity false;
wenzelm@3788
   324
wenzelm@404
   325
wenzelm@3949
   326
(* make goals (for interactive use) *)
wenzelm@3949
   327
wenzelm@3949
   328
fun mk_goal term_of thy sig_prop =
wenzelm@3949
   329
  goalw_cterm [] (cterm_of (sign_of thy) (term_of sig_prop));
wenzelm@3949
   330
wenzelm@3949
   331
fun goal_subclass thy =
wenzelm@3949
   332
  mk_goal (mk_classrel o intrn_classrel (sign_of thy)) thy;
wenzelm@3949
   333
wenzelm@3949
   334
fun goal_arity thy =
wenzelm@3949
   335
  mk_goal (mk_arity o intrn_arity (sign_of thy) Sign.intern_class) thy;
wenzelm@3949
   336
wenzelm@3949
   337
wenzelm@404
   338
end;