src/Pure/axclass.ML
author wenzelm
Mon Oct 06 18:57:17 1997 +0200 (1997-10-06 ago)
changeset 3788 51bd5c0954c6
parent 3764 fe7719aee219
child 3809 6633694439c0
permissions -rw-r--r--
eliminated raise_term, raise_typ;
new internal forms: add_inst_subclass_i, add_inst_arity_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 =
paulson@1498
     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
paulson@1498
    13
  val add_axclass: class * class list -> (string * string) list
paulson@1498
    14
    -> theory -> theory
paulson@1498
    15
  val add_axclass_i: class * class list -> (string * term) list
paulson@1498
    16
    -> theory -> theory
wenzelm@3788
    17
  val add_inst_subclass_i: class * class -> string list -> thm list
wenzelm@3788
    18
    -> tactic option -> theory -> theory
paulson@1498
    19
  val add_inst_subclass: class * class -> string list -> thm list
paulson@1498
    20
    -> tactic option -> theory -> theory
wenzelm@3788
    21
  val add_inst_arity_i: string * sort list * class list -> string list
wenzelm@3788
    22
    -> thm list -> tactic option -> theory -> theory
paulson@1498
    23
  val add_inst_arity: string * sort list * class list -> string list
paulson@1498
    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
paulson@1498
    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@404
   179
    val axioms = map (prep_axm (sign_of old_thy)) raw_axioms;
wenzelm@3788
   180
    val thy =
wenzelm@3788
   181
      (if int then Theory.add_classes else Theory.add_classes_i)
wenzelm@3788
   182
        [(raw_class, raw_super_classes)] old_thy;
wenzelm@404
   183
    val sign = sign_of thy;
wenzelm@3788
   184
    val class = Sign.intern_class sign raw_class;
wenzelm@3788
   185
    val super_classes = map (Sign.intern_class sign) 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@3764
   220
    Theory.add_axioms_i ((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@423
   274
    ^ quote (str_of sig_prop));
wenzelm@404
   275
wenzelm@638
   276
val prove_subclass =
wenzelm@423
   277
  prove mk_classrel (fn (c1, c2) => c1 ^ " < " ^ c2);
wenzelm@404
   278
wenzelm@423
   279
val prove_arity =
wenzelm@2961
   280
  prove mk_arity (fn (t, ss, c) => Sorts.str_of_arity (t, ss, [c]));
wenzelm@404
   281
wenzelm@404
   282
wenzelm@423
   283
(* make goals (for interactive use) *)
wenzelm@423
   284
wenzelm@423
   285
fun mk_goal term_of thy sig_prop =
wenzelm@423
   286
  goalw_cterm [] (cterm_of (sign_of thy) (term_of sig_prop));
wenzelm@423
   287
wenzelm@423
   288
val goal_subclass = mk_goal mk_classrel;
wenzelm@423
   289
val goal_arity = mk_goal mk_arity;
wenzelm@423
   290
wenzelm@423
   291
wenzelm@423
   292
wenzelm@449
   293
(** add proved subclass relations and arities **)
wenzelm@404
   294
wenzelm@3788
   295
fun ext_inst_subclass int raw_c1_c2 axms thms usr_tac thy =
wenzelm@3788
   296
  let
wenzelm@3788
   297
    val intrn = if int then pairself (Sign.intern_class (sign_of thy)) else I;
wenzelm@3788
   298
    val c1_c2 = intrn raw_c1_c2;
wenzelm@3788
   299
  in
wenzelm@3788
   300
    writeln ("Proving class inclusion " ^ quote (Sorts.str_of_classrel c1_c2) ^ " ...");
wenzelm@3788
   301
    add_classrel_thms
wenzelm@3788
   302
      [prove_subclass thy c1_c2 (witnesses thy axms thms) usr_tac] thy
wenzelm@3788
   303
  end;
wenzelm@423
   304
wenzelm@3788
   305
wenzelm@3788
   306
fun ext_inst_arity int (raw_t, raw_Ss, raw_cs) axms thms usr_tac thy =
wenzelm@423
   307
  let
wenzelm@3788
   308
    val sign = sign_of thy;
wenzelm@3788
   309
    val (t, Ss, cs) =
wenzelm@3788
   310
      if int then
wenzelm@3788
   311
        (Sign.intern_tycon sign raw_t,
wenzelm@3788
   312
          map (Sign.intern_sort sign) raw_Ss,
wenzelm@3788
   313
          map (Sign.intern_class sign) raw_cs)
wenzelm@3788
   314
      else (raw_t, raw_Ss, raw_cs);
wenzelm@886
   315
    val wthms = witnesses thy axms thms;
wenzelm@423
   316
    fun prove c =
wenzelm@3788
   317
     (writeln ("Proving type arity " ^ quote (Sorts.str_of_arity (t, Ss, [c])) ^ " ...");
wenzelm@3788
   318
      prove_arity thy (t, Ss, c) wthms usr_tac);
wenzelm@423
   319
  in
wenzelm@423
   320
    add_arity_thms (map prove cs) thy
wenzelm@423
   321
  end;
wenzelm@404
   322
wenzelm@3788
   323
val add_inst_subclass = ext_inst_subclass true;
wenzelm@3788
   324
val add_inst_subclass_i = ext_inst_subclass false;
wenzelm@3788
   325
val add_inst_arity = ext_inst_arity true;
wenzelm@3788
   326
val add_inst_arity_i = ext_inst_arity false;
wenzelm@3788
   327
wenzelm@404
   328
wenzelm@404
   329
end;