src/Pure/axclass.ML
author wenzelm
Tue Oct 04 19:01:37 2005 +0200 (2005-10-04 ago)
changeset 17756 d4a35f82fbb4
parent 17703 6ec36bad47ea
child 17855 64c832a03a15
permissions -rw-r--r--
minor tweaks for Poplog/ML;
     1 (*  Title:      Pure/axclass.ML
     2     ID:         $Id$
     3     Author:     Markus Wenzel, TU Muenchen
     4 
     5 Axiomatic type class package.
     6 *)
     7 
     8 signature AX_CLASS =
     9 sig
    10   val quiet_mode: bool ref
    11   val print_axclasses: theory -> unit
    12   val get_info: theory -> string -> {super_classes: class list, intro: thm, axioms: thm list}
    13   val add_axclass: bstring * xstring list -> ((bstring * string) * Attrib.src list) list ->
    14     theory -> theory * {intro: thm, axioms: thm list}
    15   val add_axclass_i: bstring * class list -> ((bstring * term) * theory attribute list) list ->
    16     theory -> theory * {intro: thm, axioms: thm list}
    17   val add_classrel_thms: thm list -> theory -> theory
    18   val add_arity_thms: thm list -> theory -> theory
    19   val add_inst_subclass: xstring * xstring -> tactic -> theory -> theory
    20   val add_inst_subclass_i: class * class -> tactic -> theory -> theory
    21   val add_inst_arity: xstring * string list * string -> tactic -> theory -> theory
    22   val add_inst_arity_i: string * sort list * sort -> tactic -> theory -> theory
    23   val instance_subclass: xstring * xstring -> theory -> Proof.state
    24   val instance_subclass_i: class * class -> theory -> Proof.state
    25   val instance_arity: xstring * string list * string -> theory -> Proof.state
    26   val instance_arity_i: string * sort list * sort -> theory -> Proof.state
    27   val intro_classes_tac: thm list -> tactic
    28   val default_intro_classes_tac: thm list -> tactic
    29   (*legacy interfaces*)
    30   val axclass_tac: thm list -> tactic
    31   val add_inst_subclass_x: xstring * xstring -> string list -> thm list
    32     -> tactic option -> theory -> theory
    33   val add_inst_arity_x: xstring * string list * string -> string list
    34     -> thm list -> tactic option -> theory -> theory
    35 end;
    36 
    37 structure AxClass: AX_CLASS =
    38 struct
    39 
    40 
    41 (** utilities **)
    42 
    43 (* messages *)
    44 
    45 val quiet_mode = ref false;
    46 fun message s = if ! quiet_mode then () else writeln s;
    47 
    48 
    49 (* type vars *)
    50 
    51 fun map_typ_frees f (Type (t, tys)) = Type (t, map (map_typ_frees f) tys)
    52   | map_typ_frees f (TFree a) = f a
    53   | map_typ_frees _ a = a;
    54 
    55 val map_term_tfrees = map_term_types o map_typ_frees;
    56 
    57 fun aT S = TFree ("'a", S);
    58 
    59 fun dest_varT (TFree (x, S)) = ((x, ~1), S)
    60   | dest_varT (TVar xi_S) = xi_S
    61   | dest_varT T = raise TYPE ("dest_varT", [T], []);
    62 
    63 
    64 
    65 (** abstract syntax operations **)
    66 
    67 (* names *)
    68 
    69 val introN = "intro";
    70 val axiomsN = "axioms";
    71 
    72 
    73 (* subclass relations as terms *)
    74 
    75 fun mk_classrel (c1, c2) = Logic.mk_inclass (aT [c1], c2);
    76 
    77 fun dest_classrel tm =
    78   let
    79     fun err () = raise TERM ("dest_classrel", [tm]);
    80 
    81     val (ty, c2) = Logic.dest_inclass tm handle TERM _ => err ();
    82     val c1 = (case dest_varT ty of (_, [c]) => c | _ => err ())
    83       handle TYPE _ => err ();
    84   in (c1, c2) end;
    85 
    86 
    87 (* arities as terms *)
    88 
    89 fun mk_arity (t, Ss, c) =
    90   let
    91     val tfrees = ListPair.map TFree (Term.invent_names [] "'a" (length Ss), Ss);
    92   in Logic.mk_inclass (Type (t, tfrees), c) end;
    93 
    94 fun mk_arities (t, Ss, S) = map (fn c => mk_arity (t, Ss, c)) S;
    95 
    96 fun dest_arity tm =
    97   let
    98     fun err () = raise TERM ("dest_arity", [tm]);
    99 
   100     val (ty, c) = Logic.dest_inclass tm handle TERM _ => err ();
   101     val (t, tvars) =
   102       (case ty of
   103         Type (t, tys) => (t, map dest_varT tys handle TYPE _ => err ())
   104       | _ => err ());
   105     val ss =
   106       if null (gen_duplicates (eq_fst (op =)) tvars)
   107       then map snd tvars else err ();
   108   in (t, ss, c) end;
   109 
   110 
   111 
   112 (** axclass info **)
   113 
   114 (* data kind 'Pure/axclasses' *)
   115 
   116 type axclass_info =
   117   {super_classes: class list,
   118     intro: thm,
   119     axioms: thm list};
   120 
   121 structure AxclassesData = TheoryDataFun
   122 (struct
   123   val name = "Pure/axclasses";
   124   type T = axclass_info Symtab.table;
   125 
   126   val empty = Symtab.empty;
   127   val copy = I;
   128   val extend = I;
   129   fun merge _ = Symtab.merge (K true);
   130 
   131   fun print thy tab =
   132     let
   133       fun pretty_class c cs = Pretty.block
   134         (Pretty.str (Sign.extern_class thy c) :: Pretty.str " <" :: Pretty.brk 1 ::
   135           Pretty.breaks (map (Pretty.str o Sign.extern_class thy) cs));
   136 
   137       fun pretty_thms name thms = Pretty.big_list (name ^ ":")
   138         (map (Display.pretty_thm_sg thy) thms);
   139 
   140       fun pretty_axclass (name, {super_classes, intro, axioms}) = Pretty.block (Pretty.fbreaks
   141         [pretty_class name super_classes, pretty_thms introN [intro], pretty_thms axiomsN axioms]);
   142     in Pretty.writeln (Pretty.chunks (map pretty_axclass (Symtab.dest tab))) end;
   143 end);
   144 
   145 val _ = Context.add_setup [AxclassesData.init];
   146 val print_axclasses = AxclassesData.print;
   147 
   148 
   149 (* get and put data *)
   150 
   151 val lookup_info = Symtab.lookup o AxclassesData.get;
   152 
   153 fun get_info thy c =
   154   (case lookup_info thy c of
   155     NONE => error ("Unknown axclass " ^ quote c)
   156   | SOME info => info);
   157 
   158 
   159 (* class_axms *)
   160 
   161 fun class_axms thy =
   162   let val classes = Sign.classes thy in
   163     map (Thm.class_triv thy) classes @
   164     List.mapPartial (Option.map #intro o lookup_info thy) classes
   165   end;
   166 
   167 
   168 
   169 (** add axiomatic type classes **)
   170 
   171 local
   172 
   173 fun err_bad_axsort ax c =
   174   error ("Sort constraint in axiom " ^ quote ax ^ " not supersort of " ^ quote c);
   175 
   176 fun err_bad_tfrees ax =
   177   error ("More than one type variable in axiom " ^ quote ax);
   178 
   179 fun ext_axclass prep_class prep_axm prep_att (bclass, raw_super_classes) raw_axioms_atts thy =
   180   let
   181     val class = Sign.full_name thy bclass;
   182     val super_classes = map (prep_class thy) raw_super_classes;
   183     val axms = map (prep_axm thy o fst) raw_axioms_atts;
   184     val atts = map (map (prep_att thy) o snd) raw_axioms_atts;
   185 
   186     (*declare class*)
   187     val class_thy =
   188       thy |> Theory.add_classes_i [(bclass, super_classes)];
   189 
   190     (*prepare abstract axioms*)
   191     fun abs_axm ax =
   192       if null (term_tfrees ax) then
   193         Logic.mk_implies (Logic.mk_inclass (aT [], class), ax)
   194       else map_term_tfrees (K (aT [class])) ax;
   195     val abs_axms = map (abs_axm o snd) axms;
   196 
   197     fun axm_sort (name, ax) =
   198       (case term_tfrees ax of
   199         [] => []
   200       | [(_, S)] => if Sign.subsort class_thy ([class], S) then S else err_bad_axsort name class
   201       | _ => err_bad_tfrees name);
   202     val axS = Sign.certify_sort class_thy (List.concat (map axm_sort axms));
   203 
   204     val int_axm = Logic.close_form o map_term_tfrees (K (aT axS));
   205     fun inclass c = Logic.mk_inclass (aT axS, c);
   206 
   207     val intro_axm = Logic.list_implies
   208       (map inclass super_classes @ map (int_axm o #2) axms, inclass class);
   209 
   210     (*declare axioms and rule*)
   211     val (axms_thy, ([intro], [axioms])) =
   212       class_thy
   213       |> Theory.add_path (Sign.const_of_class bclass)
   214       |> PureThy.add_axioms_i [Thm.no_attributes (introN, intro_axm)]
   215       |>>> PureThy.add_axiomss_i [Thm.no_attributes (axiomsN, abs_axms)];
   216     val info = {super_classes = super_classes, intro = intro, axioms = axioms};
   217 
   218     (*store info*)
   219     val final_thy =
   220       axms_thy
   221       |> Theory.add_finals_i false [Const (Sign.const_of_class class, a_itselfT --> propT)]
   222       |> (#1 o PureThy.add_thms ((map #1 axms ~~ axioms) ~~ atts))
   223       |> Theory.restore_naming class_thy
   224       |> AxclassesData.map (Symtab.update (class, info));
   225   in (final_thy, {intro = intro, axioms = axioms}) end;
   226 
   227 in
   228 
   229 val add_axclass = ext_axclass Sign.intern_class Theory.read_axm Attrib.global_attribute;
   230 val add_axclass_i = ext_axclass (K I) Theory.cert_axm (K I);
   231 
   232 end;
   233 
   234 
   235 
   236 (** proven class instantiation **)
   237 
   238 (* add thms to type signature *)
   239 
   240 fun add_classrel_thms thms thy =
   241   let
   242     fun prep_thm thm =
   243       let
   244         val prop = Drule.plain_prop_of (Thm.transfer thy thm);
   245         val (c1, c2) = dest_classrel prop handle TERM _ =>
   246           raise THM ("add_classrel_thms: not a class relation", 0, [thm]);
   247       in (c1, c2) end;
   248   in Theory.add_classrel_i (map prep_thm thms) thy end;
   249 
   250 fun add_arity_thms thms thy =
   251   let
   252     fun prep_thm thm =
   253       let
   254         val prop = Drule.plain_prop_of (Thm.transfer thy thm);
   255         val (t, ss, c) = dest_arity prop handle TERM _ =>
   256           raise THM ("add_arity_thms: not an arity", 0, [thm]);
   257       in (t, ss, [c]) end;
   258   in Theory.add_arities_i (map prep_thm thms) thy end;
   259 
   260 
   261 (* prepare classes and arities *)
   262 
   263 fun read_class thy c = Sign.certify_class thy (Sign.intern_class thy c);
   264 
   265 fun test_classrel thy cc = (Type.add_classrel (Sign.pp thy) [cc] (Sign.tsig_of thy); cc);
   266 fun cert_classrel thy = test_classrel thy o Library.pairself (Sign.certify_class thy);
   267 fun read_classrel thy = test_classrel thy o Library.pairself (read_class thy);
   268 
   269 fun test_arity thy ar = (Type.add_arities (Sign.pp thy) [ar] (Sign.tsig_of thy); ar);
   270 
   271 fun prep_arity prep_tycon prep_sort prep thy (t, Ss, x) =
   272   test_arity thy (prep_tycon thy t, map (prep_sort thy) Ss, prep thy x);
   273 
   274 val read_arity = prep_arity Sign.intern_type Sign.read_sort Sign.read_sort;
   275 val cert_arity = prep_arity (K I) Sign.certify_sort Sign.certify_sort;
   276 
   277 
   278 (* instance declarations -- tactical proof *)
   279 
   280 local
   281 
   282 fun ext_inst_subclass prep_classrel raw_cc tac thy =
   283   let
   284     val (c1, c2) = prep_classrel thy raw_cc;
   285     val txt = quote (Sign.string_of_classrel thy [c1, c2]);
   286     val _ = message ("Proving class inclusion " ^ txt ^ " ...");
   287     val th = Tactic.prove thy [] [] (mk_classrel (c1, c2)) (K tac) handle ERROR =>
   288       error ("The error(s) above occurred while trying to prove " ^ txt);
   289   in add_classrel_thms [th] thy end;
   290 
   291 fun ext_inst_arity prep_arity raw_arity tac thy =
   292   let
   293     val arity = prep_arity thy raw_arity;
   294     val txt = quote (Sign.string_of_arity thy arity);
   295     val _ = message ("Proving type arity " ^ txt ^ " ...");
   296     val props = (mk_arities arity);
   297     val ths = Tactic.prove_multi thy [] [] props
   298         (fn _ => Tactic.smart_conjunction_tac (length props) THEN tac)
   299       handle ERROR => error ("The error(s) above occurred while trying to prove " ^ txt);
   300   in add_arity_thms ths thy end;
   301 
   302 in
   303 
   304 val add_inst_subclass = ext_inst_subclass read_classrel;
   305 val add_inst_subclass_i = ext_inst_subclass cert_classrel;
   306 val add_inst_arity = ext_inst_arity read_arity;
   307 val add_inst_arity_i = ext_inst_arity cert_arity;
   308 
   309 end;
   310 
   311 
   312 (* instance declarations -- Isar proof *)
   313 
   314 local
   315 
   316 fun gen_instance mk_prop add_thms inst thy = thy
   317   |> ProofContext.init
   318   |> Proof.theorem_i Drule.internalK (K (fold add_thms)) NONE ("", [])
   319     (map (fn t => (("", []), [(t, ([], []))])) (mk_prop thy inst));
   320 
   321 in
   322 
   323 val instance_subclass =
   324   gen_instance (single oo (mk_classrel oo read_classrel)) add_classrel_thms;
   325 val instance_subclass_i =
   326   gen_instance (single oo (mk_classrel oo cert_classrel)) add_classrel_thms;
   327 val instance_arity = gen_instance (mk_arities oo read_arity) add_arity_thms;
   328 val instance_arity_i = gen_instance (mk_arities oo cert_arity) add_arity_thms;
   329 
   330 end;
   331 
   332 
   333 (* tactics and methods *)
   334 
   335 fun intro_classes_tac facts st =
   336   (ALLGOALS (Method.insert_tac facts THEN'
   337       REPEAT_ALL_NEW (resolve_tac (class_axms (Thm.theory_of_thm st))))
   338     THEN Tactic.distinct_subgoals_tac) st;
   339 
   340 fun default_intro_classes_tac [] = intro_classes_tac []
   341   | default_intro_classes_tac _ = Tactical.no_tac;    (*no error message!*)
   342 
   343 fun default_tac rules ctxt facts =
   344   HEADGOAL (Method.some_rule_tac rules ctxt facts) ORELSE
   345     default_intro_classes_tac facts;
   346 
   347 val _ = Context.add_setup [Method.add_methods
   348  [("intro_classes", Method.no_args (Method.METHOD intro_classes_tac),
   349    "back-chain introduction rules of axiomatic type classes"),
   350   ("default", Method.thms_ctxt_args (Method.METHOD oo default_tac), "apply some intro/elim rule")]];
   351 
   352 
   353 
   354 (** outer syntax **)
   355 
   356 local structure P = OuterParse and K = OuterKeyword in
   357 
   358 val axclassP =
   359   OuterSyntax.command "axclass" "define axiomatic type class" K.thy_decl
   360     ((P.name -- Scan.optional ((P.$$$ "\\<subseteq>" || P.$$$ "<") |--
   361         P.!!! (P.list1 P.xname)) []) -- Scan.repeat P.spec_name
   362       >> (Toplevel.theory o (#1 oo uncurry add_axclass)));
   363 
   364 val instanceP =
   365   OuterSyntax.command "instance" "prove type arity or subclass relation" K.thy_goal
   366    ((P.xname -- ((P.$$$ "\\<subseteq>" || P.$$$ "<") |-- P.!!! P.xname) >> instance_subclass ||
   367       P.xname -- (P.$$$ "::" |-- P.!!! P.arity) >> (instance_arity o P.triple2))
   368     >> (Toplevel.print oo Toplevel.theory_to_proof));
   369 
   370 val _ = OuterSyntax.add_parsers [axclassP, instanceP];
   371 
   372 end;
   373 
   374 
   375 
   376 (** old-style instantiation proofs **)
   377 
   378 (* axclass_tac *)
   379 
   380 (*(1) repeatedly resolve goals of form "OFCLASS(ty, c_class)",
   381       try class_trivs first, then "cI" axioms
   382   (2) rewrite goals using user supplied definitions
   383   (3) repeatedly resolve goals with user supplied non-definitions*)
   384 
   385 val is_def = Logic.is_equals o #prop o rep_thm;
   386 
   387 fun axclass_tac thms =
   388   let
   389     val defs = List.filter is_def thms;
   390     val non_defs = filter_out is_def thms;
   391   in
   392     intro_classes_tac [] THEN
   393     TRY (rewrite_goals_tac defs) THEN
   394     TRY (REPEAT_FIRST (fn i => assume_tac i ORELSE resolve_tac non_defs i))
   395   end;
   396 
   397 
   398 (* instance declarations *)
   399 
   400 local
   401 
   402 fun prove mk_prop str_of thy prop thms usr_tac =
   403   (Tactic.prove thy [] [] (mk_prop prop)
   404       (K (axclass_tac thms THEN (if_none usr_tac all_tac)))
   405     handle ERROR => error ("The error(s) above occurred while trying to prove " ^
   406      quote (str_of thy prop))) |> Drule.standard;
   407 
   408 val prove_subclass = prove mk_classrel (fn thy => fn (c1, c2) =>
   409   Pretty.string_of_classrel (Sign.pp thy) [c1, c2]);
   410 
   411 val prove_arity = prove mk_arity (fn thy => fn (t, Ss, c) =>
   412   Pretty.string_of_arity (Sign.pp thy) (t, Ss, [c]));
   413 
   414 fun witnesses thy names thms =
   415   PureThy.get_thmss thy (map Name names) @
   416   thms @
   417   List.filter is_def (map snd (axioms_of thy));
   418 
   419 in
   420 
   421 fun add_inst_subclass_x raw_c1_c2 names thms usr_tac thy =
   422   let val (c1, c2) = read_classrel thy raw_c1_c2 in
   423     message ("Proving class inclusion " ^ quote (Sign.string_of_classrel thy [c1, c2]) ^ " ...");
   424     thy |> add_classrel_thms [prove_subclass thy (c1, c2) (witnesses thy names thms) usr_tac]
   425   end;
   426 
   427 fun add_inst_arity_x raw_arity names thms usr_tac thy =
   428   let
   429     val (t, Ss, cs) = read_arity thy raw_arity;
   430     val wthms = witnesses thy names thms;
   431     fun prove c =
   432      (message ("Proving type arity " ^
   433         quote (Sign.string_of_arity thy (t, Ss, [c])) ^ " ...");
   434         prove_arity thy (t, Ss, c) wthms usr_tac);
   435   in add_arity_thms (map prove cs) thy end;
   436 
   437 end;
   438 
   439 end;