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