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