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