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