src/Pure/axclass.ML
author wenzelm
Wed Jun 04 12:26:42 1997 +0200 (1997-06-04 ago)
changeset 3395 d8700b008944
parent 2961 842be30dc336
child 3632 17527284f100
permissions -rw-r--r--
eliminated freeze_vars;
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@3395
    45
fun dest_varT (TFree (x, S)) = ((x, ~1), S)
wenzelm@3395
    46
  | dest_varT (TVar xi_S) = xi_S
wenzelm@3395
    47
  | dest_varT T = raise_type "dest_varT" [T] [];
wenzelm@3395
    48
wenzelm@404
    49
wenzelm@886
    50
(* get axioms and theorems *)
wenzelm@404
    51
wenzelm@404
    52
fun get_ax thy name =
wenzelm@404
    53
  Some (get_axiom thy name) handle THEORY _ => None;
wenzelm@404
    54
wenzelm@404
    55
val get_axioms = mapfilter o get_ax;
wenzelm@404
    56
paulson@1498
    57
val is_def = Logic.is_equals o #prop o rep_thm;
wenzelm@886
    58
wenzelm@886
    59
fun witnesses thy axms thms =
wenzelm@1201
    60
  map (get_axiom thy) axms @ thms @ filter is_def (map snd (axioms_of thy));
wenzelm@886
    61
wenzelm@404
    62
wenzelm@404
    63
wenzelm@560
    64
(** abstract syntax operations **)
wenzelm@423
    65
wenzelm@423
    66
(* subclass relations as terms *)
wenzelm@423
    67
paulson@1498
    68
fun mk_classrel (c1, c2) = Logic.mk_inclass (aT [c1], c2);
wenzelm@423
    69
wenzelm@423
    70
fun dest_classrel tm =
wenzelm@423
    71
  let
wenzelm@423
    72
    fun err () = raise_term "dest_classrel" [tm];
wenzelm@423
    73
wenzelm@3395
    74
    val (ty, c2) = Logic.dest_inclass tm handle TERM _ => err ();
wenzelm@3395
    75
    val c1 = (case dest_varT ty of (_, [c]) => c | _ => err ())
wenzelm@3395
    76
      handle TYPE _ => err ();
wenzelm@423
    77
  in
wenzelm@423
    78
    (c1, c2)
wenzelm@423
    79
  end;
wenzelm@423
    80
wenzelm@423
    81
wenzelm@423
    82
(* arities as terms *)
wenzelm@423
    83
wenzelm@423
    84
fun mk_arity (t, ss, c) =
wenzelm@423
    85
  let
wenzelm@449
    86
    val names = tl (variantlist (replicate (length ss + 1) "'", []));
paulson@2266
    87
    val tfrees = ListPair.map TFree (names, ss);
wenzelm@423
    88
  in
paulson@1498
    89
    Logic.mk_inclass (Type (t, tfrees), c)
wenzelm@423
    90
  end;
wenzelm@423
    91
wenzelm@423
    92
fun dest_arity tm =
wenzelm@423
    93
  let
wenzelm@423
    94
    fun err () = raise_term "dest_arity" [tm];
wenzelm@423
    95
wenzelm@3395
    96
    val (ty, c) = Logic.dest_inclass tm handle TERM _ => err ();
wenzelm@3395
    97
    val (t, tvars) =
wenzelm@423
    98
      (case ty of
wenzelm@3395
    99
        Type (t, tys) => (t, map dest_varT tys handle TYPE _ => err ())
wenzelm@423
   100
      | _ => err ());
wenzelm@423
   101
    val ss =
wenzelm@3395
   102
      if null (gen_duplicates eq_fst tvars)
wenzelm@3395
   103
      then map snd tvars else err ();
wenzelm@423
   104
  in
wenzelm@423
   105
    (t, ss, c)
wenzelm@423
   106
  end;
wenzelm@423
   107
wenzelm@423
   108
wenzelm@423
   109
wenzelm@560
   110
(** add theorems as axioms **)
wenzelm@423
   111
wenzelm@423
   112
fun prep_thm_axm thy thm =
wenzelm@423
   113
  let
wenzelm@423
   114
    fun err msg = raise THM ("prep_thm_axm: " ^ msg, 0, [thm]);
wenzelm@423
   115
wenzelm@1237
   116
    val {sign, hyps, prop, ...} = rep_thm thm;
wenzelm@423
   117
  in
wenzelm@423
   118
    if not (Sign.subsig (sign, sign_of thy)) then
wenzelm@423
   119
      err "theorem not of same theory"
wenzelm@1237
   120
    else if not (null (extra_shyps thm)) orelse not (null hyps) then
wenzelm@423
   121
      err "theorem may not contain hypotheses"
wenzelm@423
   122
    else prop
wenzelm@423
   123
  end;
wenzelm@423
   124
wenzelm@423
   125
(*general theorems*)
wenzelm@423
   126
fun add_thms_as_axms thms thy =
wenzelm@423
   127
  add_axioms_i (map (apsnd (prep_thm_axm thy)) thms) thy;
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@423
   139
    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@423
   152
    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@404
   173
fun ext_axclass prep_axm (class, super_classes) raw_axioms old_thy =
wenzelm@404
   174
  let
wenzelm@404
   175
    val axioms = map (prep_axm (sign_of old_thy)) raw_axioms;
wenzelm@560
   176
    val thy = add_classes [(class, super_classes)] old_thy;
wenzelm@404
   177
    val sign = sign_of thy;
wenzelm@404
   178
wenzelm@404
   179
wenzelm@404
   180
    (* prepare abstract axioms *)
wenzelm@404
   181
wenzelm@404
   182
    fun abs_axm ax =
wenzelm@404
   183
      if null (term_tfrees ax) then
paulson@1498
   184
        Logic.mk_implies (Logic.mk_inclass (aT logicS, class), ax)
wenzelm@404
   185
      else
wenzelm@404
   186
        map_term_tfrees (K (aT [class])) ax;
wenzelm@404
   187
wenzelm@404
   188
    val abs_axioms = map (apsnd abs_axm) axioms;
wenzelm@404
   189
wenzelm@404
   190
wenzelm@404
   191
    (* prepare introduction orule *)
wenzelm@404
   192
wenzelm@404
   193
    val _ =
wenzelm@404
   194
      if Sign.subsort sign ([class], logicS) then ()
wenzelm@404
   195
      else err_not_logic class;
wenzelm@404
   196
wenzelm@404
   197
    fun axm_sort (name, ax) =
wenzelm@404
   198
      (case term_tfrees ax of
wenzelm@404
   199
        [] => []
wenzelm@404
   200
      | [(_, S)] =>
wenzelm@404
   201
          if Sign.subsort sign ([class], S) then S
wenzelm@404
   202
          else err_bad_axsort name class
wenzelm@404
   203
      | _ => err_bad_tfrees name);
wenzelm@404
   204
paulson@2672
   205
    val axS = Sign.norm_sort sign (logicC :: List.concat(map axm_sort axioms))
wenzelm@404
   206
paulson@1498
   207
    val int_axm = Logic.close_form o map_term_tfrees (K (aT axS));
paulson@1498
   208
    fun inclass c = Logic.mk_inclass (aT axS, c);
wenzelm@404
   209
paulson@1498
   210
    val intro_axm = Logic.list_implies
wenzelm@404
   211
      (map inclass super_classes @ map (int_axm o snd) axioms, inclass class);
wenzelm@404
   212
  in
wenzelm@404
   213
    add_axioms_i ((class ^ "I", intro_axm) :: abs_axioms) thy
wenzelm@404
   214
  end;
wenzelm@404
   215
wenzelm@404
   216
wenzelm@404
   217
(* external interfaces *)
wenzelm@404
   218
wenzelm@404
   219
val add_axclass = ext_axclass read_axm;
wenzelm@404
   220
val add_axclass_i = ext_axclass cert_axm;
wenzelm@404
   221
wenzelm@404
   222
wenzelm@404
   223
wenzelm@423
   224
(** prove class relations and type arities **)
wenzelm@423
   225
wenzelm@423
   226
(* class_axms *)
wenzelm@404
   227
wenzelm@404
   228
fun class_axms thy =
wenzelm@404
   229
  let
wenzelm@404
   230
    val classes = Sign.classes (sign_of thy);
wenzelm@404
   231
    val intros = map (fn c => c ^ "I") classes;
wenzelm@404
   232
  in
wenzelm@1217
   233
    map (class_triv thy) classes @
wenzelm@1217
   234
    get_axioms thy intros
wenzelm@404
   235
  end;
wenzelm@404
   236
wenzelm@423
   237
wenzelm@423
   238
(* axclass_tac *)
wenzelm@423
   239
wenzelm@487
   240
(*(1) repeatedly resolve goals of form "OFCLASS(ty, c_class)",
wenzelm@1217
   241
      try class_trivs first, then "cI" axioms
wenzelm@423
   242
  (2) rewrite goals using user supplied definitions
wenzelm@423
   243
  (3) repeatedly resolve goals with user supplied non-definitions*)
wenzelm@423
   244
wenzelm@423
   245
fun axclass_tac thy thms =
wenzelm@1217
   246
  let
wenzelm@1217
   247
    val defs = filter is_def thms;
wenzelm@1217
   248
    val non_defs = filter_out is_def thms;
wenzelm@1217
   249
  in
wenzelm@1217
   250
    TRY (REPEAT_FIRST (resolve_tac (class_axms thy))) THEN
wenzelm@1217
   251
    TRY (rewrite_goals_tac defs) THEN
wenzelm@1217
   252
    TRY (REPEAT_FIRST (fn i => assume_tac i ORELSE resolve_tac non_defs i))
wenzelm@1217
   253
  end;
wenzelm@404
   254
wenzelm@404
   255
wenzelm@423
   256
(* provers *)
wenzelm@404
   257
wenzelm@423
   258
fun prove term_of str_of thy sig_prop thms usr_tac =
wenzelm@404
   259
  let
wenzelm@404
   260
    val sign = sign_of thy;
wenzelm@423
   261
    val goal = cterm_of sign (term_of sig_prop);
wenzelm@423
   262
    val tac = axclass_tac thy thms THEN (if_none usr_tac all_tac);
wenzelm@423
   263
  in
wenzelm@423
   264
    prove_goalw_cterm [] goal (K [tac])
wenzelm@423
   265
  end
wenzelm@423
   266
  handle ERROR => error ("The error(s) above occurred while trying to prove "
wenzelm@423
   267
    ^ quote (str_of sig_prop));
wenzelm@404
   268
wenzelm@638
   269
val prove_subclass =
wenzelm@423
   270
  prove mk_classrel (fn (c1, c2) => c1 ^ " < " ^ c2);
wenzelm@404
   271
wenzelm@423
   272
val prove_arity =
wenzelm@2961
   273
  prove mk_arity (fn (t, ss, c) => Sorts.str_of_arity (t, ss, [c]));
wenzelm@404
   274
wenzelm@404
   275
wenzelm@423
   276
(* make goals (for interactive use) *)
wenzelm@423
   277
wenzelm@423
   278
fun mk_goal term_of thy sig_prop =
wenzelm@423
   279
  goalw_cterm [] (cterm_of (sign_of thy) (term_of sig_prop));
wenzelm@423
   280
wenzelm@423
   281
val goal_subclass = mk_goal mk_classrel;
wenzelm@423
   282
val goal_arity = mk_goal mk_arity;
wenzelm@423
   283
wenzelm@423
   284
wenzelm@423
   285
wenzelm@449
   286
(** add proved subclass relations and arities **)
wenzelm@404
   287
wenzelm@449
   288
fun add_inst_subclass (c1, c2) axms thms usr_tac thy =
wenzelm@423
   289
  add_classrel_thms
wenzelm@886
   290
  [prove_subclass thy (c1, c2) (witnesses thy axms thms) usr_tac] thy;
wenzelm@423
   291
wenzelm@449
   292
fun add_inst_arity (t, ss, cs) axms thms usr_tac thy =
wenzelm@423
   293
  let
wenzelm@886
   294
    val wthms = witnesses thy axms thms;
wenzelm@423
   295
    fun prove c =
wenzelm@886
   296
      prove_arity thy (t, ss, c) wthms usr_tac;
wenzelm@423
   297
  in
wenzelm@423
   298
    add_arity_thms (map prove cs) thy
wenzelm@423
   299
  end;
wenzelm@404
   300
wenzelm@404
   301
wenzelm@404
   302
end;