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