src/Pure/axclass.ML
author wenzelm
Thu May 26 16:53:58 1994 +0200 (1994-05-26)
changeset 404 dd3d3d6467db
child 423 a42892e72854
permissions -rw-r--r--
axiomatic type class 'package' for Pure (alpha version);
wenzelm@404
     1
(*  Title:      Pure/axclass.ML
wenzelm@404
     2
    ID:         $Id$
wenzelm@404
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@404
     4
wenzelm@404
     5
User interface for axiomatic type classes.
wenzelm@404
     6
wenzelm@404
     7
TODO:
wenzelm@404
     8
  arity_tac: FILTER is_arity (?), assume_tac (?) (no?)
wenzelm@404
     9
*)
wenzelm@404
    10
wenzelm@404
    11
signature AX_CLASS =
wenzelm@404
    12
sig
wenzelm@404
    13
  structure Tactical: TACTICAL
wenzelm@404
    14
  local open Tactical Tactical.Thm in
wenzelm@404
    15
    val add_axclass: class * class list -> (string * string) list
wenzelm@404
    16
      -> theory -> theory
wenzelm@404
    17
    val add_axclass_i: class * class list -> (string * term) list
wenzelm@404
    18
      -> theory -> theory
wenzelm@404
    19
    val add_instance: string * sort list * class -> string list -> thm list
wenzelm@404
    20
      -> tactic option -> theory -> theory
wenzelm@404
    21
    val axclass_tac: theory -> tactic
wenzelm@404
    22
  end
wenzelm@404
    23
end;
wenzelm@404
    24
wenzelm@404
    25
functor AxClassFun(structure Logic: LOGIC and Goals: GOALS and Tactic: TACTIC
wenzelm@404
    26
  sharing Goals.Tactical = Tactic.Tactical)(*: AX_CLASS *) = (* FIXME *)
wenzelm@404
    27
struct
wenzelm@404
    28
wenzelm@404
    29
structure Tactical = Goals.Tactical;
wenzelm@404
    30
structure Thm = Tactical.Thm;
wenzelm@404
    31
structure Sign = Thm.Sign;
wenzelm@404
    32
structure Type = Sign.Type;
wenzelm@404
    33
wenzelm@404
    34
open Logic Thm Tactical Tactic Goals;
wenzelm@404
    35
wenzelm@404
    36
wenzelm@404
    37
(* FIXME -> type.ML *)
wenzelm@404
    38
wenzelm@404
    39
structure Type =
wenzelm@404
    40
struct
wenzelm@404
    41
  open Type;
wenzelm@404
    42
wenzelm@404
    43
  fun str_of_sort [c] = c
wenzelm@404
    44
    | str_of_sort cs = parents "{" "}" (commas cs);
wenzelm@404
    45
wenzelm@404
    46
  fun str_of_dom dom = parents "(" ")" (commas (map str_of_sort dom));
wenzelm@404
    47
wenzelm@404
    48
  fun str_of_arity (t, [], S) = t ^ " :: " ^ str_of_sort S
wenzelm@404
    49
    | str_of_arity (t, SS, S) =
wenzelm@404
    50
        t ^ " :: " ^ str_of_dom SS ^ " " ^ str_of_sort S;
wenzelm@404
    51
end;
wenzelm@404
    52
wenzelm@404
    53
wenzelm@404
    54
(** add constant definitions **)  (* FIXME -> drule.ML (?) *)
wenzelm@404
    55
wenzelm@404
    56
(* FIXME fake! *)
wenzelm@404
    57
val add_defns = add_axioms;
wenzelm@404
    58
val add_defns_i = add_axioms_i;
wenzelm@404
    59
wenzelm@404
    60
wenzelm@404
    61
(** utilities **)
wenzelm@404
    62
wenzelm@404
    63
(* type vars *)
wenzelm@404
    64
wenzelm@404
    65
fun map_typ_frees f (Type (t, tys)) = Type (t, map (map_typ_frees f) tys)
wenzelm@404
    66
  | map_typ_frees f (TFree a) = f a
wenzelm@404
    67
  | map_typ_frees _ a = a;
wenzelm@404
    68
wenzelm@404
    69
val map_term_tfrees = map_term_types o map_typ_frees;
wenzelm@404
    70
wenzelm@404
    71
fun aT S = TFree ("'a", S);
wenzelm@404
    72
wenzelm@404
    73
wenzelm@404
    74
(* get axioms *)
wenzelm@404
    75
wenzelm@404
    76
fun get_ax thy name =
wenzelm@404
    77
  Some (get_axiom thy name) handle THEORY _ => None;
wenzelm@404
    78
wenzelm@404
    79
val get_axioms = mapfilter o get_ax;
wenzelm@404
    80
wenzelm@404
    81
wenzelm@404
    82
(* is_defn *)
wenzelm@404
    83
wenzelm@404
    84
fun is_defn thm =
wenzelm@404
    85
  (case #prop (rep_thm thm) of
wenzelm@404
    86
    Const ("==", _) $ _ $ _ => true
wenzelm@404
    87
  | _ => false);
wenzelm@404
    88
wenzelm@404
    89
wenzelm@404
    90
wenzelm@404
    91
(** add axiomatic type class **)
wenzelm@404
    92
wenzelm@404
    93
(* errors *)
wenzelm@404
    94
wenzelm@404
    95
fun err_not_logic c =
wenzelm@404
    96
  error ("Axiomatic class " ^ quote c ^ " not subclass of \"logic\"");
wenzelm@404
    97
wenzelm@404
    98
fun err_bad_axsort ax c =
wenzelm@404
    99
  error ("Sort constraint in axiom " ^ quote ax ^ " not supersort of " ^ quote c);
wenzelm@404
   100
wenzelm@404
   101
fun err_bad_tfrees ax =
wenzelm@404
   102
  error ("More than one type variable in axiom " ^ quote ax);
wenzelm@404
   103
wenzelm@404
   104
wenzelm@404
   105
(* ext_axclass *)
wenzelm@404
   106
wenzelm@404
   107
fun ext_axclass prep_axm (class, super_classes) raw_axioms old_thy =
wenzelm@404
   108
  let
wenzelm@404
   109
    val axioms = map (prep_axm (sign_of old_thy)) raw_axioms;
wenzelm@404
   110
    val thy = add_classes [([], class, super_classes)] old_thy;
wenzelm@404
   111
    val sign = sign_of thy;
wenzelm@404
   112
wenzelm@404
   113
wenzelm@404
   114
    (* prepare abstract axioms *)
wenzelm@404
   115
wenzelm@404
   116
    fun abs_axm ax =
wenzelm@404
   117
      if null (term_tfrees ax) then
wenzelm@404
   118
        mk_implies (mk_inclass (aT logicS, class), ax)
wenzelm@404
   119
      else
wenzelm@404
   120
        map_term_tfrees (K (aT [class])) ax;
wenzelm@404
   121
wenzelm@404
   122
    val abs_axioms = map (apsnd abs_axm) axioms;
wenzelm@404
   123
wenzelm@404
   124
wenzelm@404
   125
    (* prepare introduction orule *)
wenzelm@404
   126
wenzelm@404
   127
    val _ =
wenzelm@404
   128
      if Sign.subsort sign ([class], logicS) then ()
wenzelm@404
   129
      else err_not_logic class;
wenzelm@404
   130
wenzelm@404
   131
    fun axm_sort (name, ax) =
wenzelm@404
   132
      (case term_tfrees ax of
wenzelm@404
   133
        [] => []
wenzelm@404
   134
      | [(_, S)] =>
wenzelm@404
   135
          if Sign.subsort sign ([class], S) then S
wenzelm@404
   136
          else err_bad_axsort name class
wenzelm@404
   137
      | _ => err_bad_tfrees name);
wenzelm@404
   138
wenzelm@404
   139
    val axS = Sign.norm_sort sign (logicC :: flat (map axm_sort axioms));
wenzelm@404
   140
wenzelm@404
   141
    val int_axm = close_form o map_term_tfrees (K (aT axS));
wenzelm@404
   142
    fun inclass c = mk_inclass (aT axS, c);
wenzelm@404
   143
wenzelm@404
   144
    val intro_axm = list_implies
wenzelm@404
   145
      (map inclass super_classes @ map (int_axm o snd) axioms, inclass class);
wenzelm@404
   146
  in
wenzelm@404
   147
    add_axioms_i ((class ^ "I", intro_axm) :: abs_axioms) thy
wenzelm@404
   148
  end;
wenzelm@404
   149
wenzelm@404
   150
wenzelm@404
   151
(* external interfaces *)
wenzelm@404
   152
wenzelm@404
   153
val add_axclass = ext_axclass read_axm;
wenzelm@404
   154
val add_axclass_i = ext_axclass cert_axm;
wenzelm@404
   155
wenzelm@404
   156
wenzelm@404
   157
wenzelm@404
   158
(** add class instance **)
wenzelm@404
   159
wenzelm@404
   160
(* arities as terms *)
wenzelm@404
   161
wenzelm@404
   162
fun mk_arity (t, ss, c) =
wenzelm@404
   163
  let
wenzelm@404
   164
    val names = variantlist (replicate (length ss) "'a", []);
wenzelm@404
   165
    val tvars = map (fn a => TVar ((a, 0), logicS)) names;
wenzelm@404
   166
  in
wenzelm@404
   167
    mk_inclass (Type (t, tvars), c)
wenzelm@404
   168
  end;
wenzelm@404
   169
wenzelm@404
   170
fun dest_arity tm =
wenzelm@404
   171
  let
wenzelm@404
   172
    fun err () = raise_term "dest_arity" [tm];
wenzelm@404
   173
wenzelm@404
   174
    val (ty, c) = dest_inclass tm handle TERM _ => err ();
wenzelm@404
   175
    val (t, tvars) =
wenzelm@404
   176
      (case ty of
wenzelm@404
   177
        Type (t, tys) => (t, map (fn TVar x => x | _ => err ()) tys)
wenzelm@404
   178
      | _ => err ());
wenzelm@404
   179
    val ss =
wenzelm@404
   180
      if null (gen_duplicates eq_fst tvars)
wenzelm@404
   181
      then map snd tvars else err ();
wenzelm@404
   182
  in
wenzelm@404
   183
    (t, ss, c)
wenzelm@404
   184
  end;
wenzelm@404
   185
wenzelm@404
   186
wenzelm@404
   187
(* axclass_tac *)
wenzelm@404
   188
wenzelm@404
   189
(*repeatedly resolve subgoals of form "(| ty : c_class |)";
wenzelm@404
   190
  try "cI" axioms first and use class_triv as last resort*)
wenzelm@404
   191
wenzelm@404
   192
fun class_axms thy =
wenzelm@404
   193
  let
wenzelm@404
   194
    val classes = Sign.classes (sign_of thy);
wenzelm@404
   195
    val intros = map (fn c => c ^ "I") classes;
wenzelm@404
   196
  in
wenzelm@404
   197
    get_axioms thy intros @
wenzelm@404
   198
    map (class_triv thy) classes
wenzelm@404
   199
  end;
wenzelm@404
   200
wenzelm@404
   201
fun axclass_tac thy =
wenzelm@404
   202
  REPEAT_FIRST (resolve_tac (class_axms thy));
wenzelm@404
   203
wenzelm@404
   204
wenzelm@404
   205
(* prove_arity *)
wenzelm@404
   206
wenzelm@404
   207
(*(* FIXME *)
wenzelm@404
   208
(* FIXME state sign vs sign (!?) *)
wenzelm@404
   209
fun weaker_arity state =
wenzelm@404
   210
  all_subsort (ss, #2 (dest_arity (concl_of state)))
wenzelm@404
   211
    handle TERM _ => false;
wenzelm@404
   212
*)
wenzelm@404
   213
wenzelm@404
   214
fun prove_arity thy (arity as (t, ss, c)) thms usr_tac =
wenzelm@404
   215
  let
wenzelm@404
   216
    val sign = sign_of thy;
wenzelm@404
   217
    val all_subsort = forall2 (Sign.subsort sign);
wenzelm@404
   218
wenzelm@404
   219
    val arity_goal = cterm_of sign (mk_arity arity);
wenzelm@404
   220
wenzelm@404
   221
    val arity_tac =
wenzelm@404
   222
      axclass_tac thy THEN
wenzelm@404
   223
      TRY (rewrite_goals_tac (filter is_defn thms)) THEN
wenzelm@404
   224
      TRY (REPEAT_FIRST (resolve_tac (filter_out is_defn thms))) THEN
wenzelm@404
   225
      (if_none usr_tac all_tac);
wenzelm@404
   226
wenzelm@404
   227
    val arity_thm = prove_goalw_cterm [] arity_goal (K [arity_tac]);
wenzelm@404
   228
wenzelm@404
   229
    val (t', ss', c') = dest_arity (#prop (rep_thm arity_thm))
wenzelm@404
   230
      handle TERM _ => error "Result is not an arity";
wenzelm@404
   231
  in
wenzelm@404
   232
    if t = t' andalso all_subsort (ss, ss') andalso c = c' then ()
wenzelm@404
   233
    else error ("Proved different arity: " ^ Type.str_of_arity (t', ss', [c']))
wenzelm@404
   234
  end
wenzelm@404
   235
  handle ERROR => error ("The error(s) above occurred while trying to prove: " ^
wenzelm@404
   236
    Type.str_of_arity (t, ss, [c]));
wenzelm@404
   237
wenzelm@404
   238
wenzelm@404
   239
(* external interface *)
wenzelm@404
   240
wenzelm@404
   241
fun add_instance (t, ss, c) axms thms usr_tac thy =
wenzelm@404
   242
  (prove_arity thy (t, ss, c) (get_axioms thy axms @ thms) usr_tac;
wenzelm@404
   243
    add_arities [(t, ss, [c])] thy);
wenzelm@404
   244
wenzelm@404
   245
wenzelm@404
   246
end;
wenzelm@404
   247