src/Pure/axclass.ML
author wenzelm
Fri Oct 21 18:14:34 2005 +0200 (2005-10-21 ago)
changeset 17956 369e2af8ee45
parent 17928 c567e5f885bf
child 18124 a310c78298f9
permissions -rw-r--r--
Goal.prove;
     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 end;
    30 
    31 structure AxClass: AX_CLASS =
    32 struct
    33 
    34 
    35 (** utilities **)
    36 
    37 (* messages *)
    38 
    39 val quiet_mode = ref false;
    40 fun message s = if ! quiet_mode then () else writeln s;
    41 
    42 
    43 (* type vars *)
    44 
    45 fun map_typ_frees f (Type (t, tys)) = Type (t, map (map_typ_frees f) tys)
    46   | map_typ_frees f (TFree a) = f a
    47   | map_typ_frees _ a = a;
    48 
    49 val map_term_tfrees = map_term_types o map_typ_frees;
    50 
    51 fun aT S = TFree ("'a", S);
    52 
    53 fun dest_varT (TFree (x, S)) = ((x, ~1), S)
    54   | dest_varT (TVar xi_S) = xi_S
    55   | dest_varT T = raise TYPE ("dest_varT", [T], []);
    56 
    57 
    58 
    59 (** abstract syntax operations **)
    60 
    61 (* names *)
    62 
    63 val introN = "intro";
    64 val axiomsN = "axioms";
    65 
    66 
    67 (* subclass relations as terms *)
    68 
    69 fun mk_classrel (c1, c2) = Logic.mk_inclass (aT [c1], c2);
    70 
    71 fun dest_classrel tm =
    72   let
    73     fun err () = raise TERM ("dest_classrel", [tm]);
    74 
    75     val (ty, c2) = Logic.dest_inclass tm handle TERM _ => err ();
    76     val c1 = (case dest_varT ty of (_, [c]) => c | _ => err ())
    77       handle TYPE _ => err ();
    78   in (c1, c2) end;
    79 
    80 
    81 (* arities as terms *)
    82 
    83 fun mk_arity (t, Ss, c) =
    84   let
    85     val tfrees = ListPair.map TFree (Term.invent_names [] "'a" (length Ss), Ss);
    86   in Logic.mk_inclass (Type (t, tfrees), c) end;
    87 
    88 fun mk_arities (t, Ss, S) = map (fn c => mk_arity (t, Ss, c)) S;
    89 
    90 fun dest_arity tm =
    91   let
    92     fun err () = raise TERM ("dest_arity", [tm]);
    93 
    94     val (ty, c) = Logic.dest_inclass tm handle TERM _ => err ();
    95     val (t, tvars) =
    96       (case ty of
    97         Type (t, tys) => (t, map dest_varT tys handle TYPE _ => err ())
    98       | _ => err ());
    99     val ss =
   100       if null (gen_duplicates (eq_fst (op =)) tvars)
   101       then map snd tvars else err ();
   102   in (t, ss, c) end;
   103 
   104 
   105 
   106 (** axclass info **)
   107 
   108 type axclass_info =
   109   {super_classes: class list,
   110     intro: thm,
   111     axioms: thm list};
   112 
   113 structure AxclassesData = TheoryDataFun
   114 (struct
   115   val name = "Pure/axclasses";
   116   type T = axclass_info Symtab.table;
   117 
   118   val empty = Symtab.empty;
   119   val copy = I;
   120   val extend = I;
   121   fun merge _ = Symtab.merge (K true);
   122 
   123   fun print thy tab =
   124     let
   125       fun pretty_class c cs = Pretty.block
   126         (Pretty.str (Sign.extern_class thy c) :: Pretty.str " <" :: Pretty.brk 1 ::
   127           Pretty.breaks (map (Pretty.str o Sign.extern_class thy) cs));
   128 
   129       fun pretty_thms name thms = Pretty.big_list (name ^ ":")
   130         (map (Display.pretty_thm_sg thy) thms);
   131 
   132       fun pretty_axclass (name, {super_classes, intro, axioms}) = Pretty.block (Pretty.fbreaks
   133         [pretty_class name super_classes, pretty_thms introN [intro], pretty_thms axiomsN axioms]);
   134     in Pretty.writeln (Pretty.chunks (map pretty_axclass (Symtab.dest tab))) end;
   135 end);
   136 
   137 val _ = Context.add_setup [AxclassesData.init];
   138 val print_axclasses = AxclassesData.print;
   139 
   140 
   141 val lookup_info = Symtab.lookup o AxclassesData.get;
   142 
   143 fun get_info thy c =
   144   (case lookup_info thy c of
   145     NONE => error ("Unknown axclass " ^ quote c)
   146   | SOME info => info);
   147 
   148 
   149 fun class_axms thy =
   150   let val classes = Sign.classes thy in
   151     map (Thm.class_triv thy) classes @
   152     List.mapPartial (Option.map #intro o lookup_info thy) classes
   153   end;
   154 
   155 
   156 
   157 (** add axiomatic type classes **)
   158 
   159 local
   160 
   161 fun err_bad_axsort ax c =
   162   error ("Sort constraint in axiom " ^ quote ax ^ " not supersort of " ^ quote c);
   163 
   164 fun err_bad_tfrees ax =
   165   error ("More than one type variable in axiom " ^ quote ax);
   166 
   167 fun ext_axclass prep_class prep_axm prep_att (bclass, raw_super_classes) raw_axioms_atts thy =
   168   let
   169     val class = Sign.full_name thy bclass;
   170     val super_classes = map (prep_class thy) raw_super_classes;
   171     val axms = map (prep_axm thy o fst) raw_axioms_atts;
   172     val atts = map (map (prep_att thy) o snd) raw_axioms_atts;
   173 
   174     (*declare class*)
   175     val class_thy =
   176       thy |> Theory.add_classes_i [(bclass, super_classes)];
   177 
   178     (*prepare abstract axioms*)
   179     fun abs_axm ax =
   180       if null (term_tfrees ax) then
   181         Logic.mk_implies (Logic.mk_inclass (aT [], class), ax)
   182       else map_term_tfrees (K (aT [class])) ax;
   183     val abs_axms = map (abs_axm o snd) axms;
   184 
   185     fun axm_sort (name, ax) =
   186       (case term_tfrees ax of
   187         [] => []
   188       | [(_, S)] => if Sign.subsort class_thy ([class], S) then S else err_bad_axsort name class
   189       | _ => err_bad_tfrees name);
   190     val axS = Sign.certify_sort class_thy (List.concat (map axm_sort axms));
   191 
   192     val int_axm = Logic.close_form o map_term_tfrees (K (aT axS));
   193     fun inclass c = Logic.mk_inclass (aT axS, c);
   194 
   195     val intro_axm = Logic.list_implies
   196       (map inclass super_classes @ map (int_axm o #2) axms, inclass class);
   197 
   198     (*declare axioms and rule*)
   199     val (axms_thy, ([intro], [axioms])) =
   200       class_thy
   201       |> Theory.add_path (Sign.const_of_class bclass)
   202       |> PureThy.add_axioms_i [Thm.no_attributes (introN, intro_axm)]
   203       |>>> PureThy.add_axiomss_i [Thm.no_attributes (axiomsN, abs_axms)];
   204     val info = {super_classes = super_classes, intro = intro, axioms = axioms};
   205 
   206     (*store info*)
   207     val final_thy =
   208       axms_thy
   209       |> Theory.add_finals_i false [Const (Sign.const_of_class class, a_itselfT --> propT)]
   210       |> (#1 o PureThy.add_thms ((map #1 axms ~~ axioms) ~~ atts))
   211       |> Theory.restore_naming class_thy
   212       |> AxclassesData.map (Symtab.update (class, info));
   213   in (final_thy, {intro = intro, axioms = axioms}) end;
   214 
   215 in
   216 
   217 val add_axclass = ext_axclass Sign.intern_class Theory.read_axm Attrib.global_attribute;
   218 val add_axclass_i = ext_axclass (K I) Theory.cert_axm (K I);
   219 
   220 end;
   221 
   222 
   223 
   224 (** proven class instantiation **)
   225 
   226 (* add thms to type signature *)
   227 
   228 fun add_classrel_thms thms thy =
   229   let
   230     fun prep_thm thm =
   231       let
   232         val prop = Drule.plain_prop_of (Thm.transfer thy thm);
   233         val (c1, c2) = dest_classrel prop handle TERM _ =>
   234           raise THM ("add_classrel_thms: not a class relation", 0, [thm]);
   235       in (c1, c2) end;
   236   in Theory.add_classrel_i (map prep_thm thms) thy end;
   237 
   238 fun add_arity_thms thms thy =
   239   let
   240     fun prep_thm thm =
   241       let
   242         val prop = Drule.plain_prop_of (Thm.transfer thy thm);
   243         val (t, ss, c) = dest_arity prop handle TERM _ =>
   244           raise THM ("add_arity_thms: not an arity", 0, [thm]);
   245       in (t, ss, [c]) end;
   246   in Theory.add_arities_i (map prep_thm thms) thy end;
   247 
   248 
   249 (* prepare classes and arities *)
   250 
   251 fun read_class thy c = Sign.certify_class thy (Sign.intern_class thy c);
   252 
   253 fun test_classrel thy cc = (Type.add_classrel (Sign.pp thy) [cc] (Sign.tsig_of thy); cc);
   254 fun cert_classrel thy = test_classrel thy o Library.pairself (Sign.certify_class thy);
   255 fun read_classrel thy = test_classrel thy o Library.pairself (read_class thy);
   256 
   257 fun test_arity thy ar = (Type.add_arities (Sign.pp thy) [ar] (Sign.tsig_of thy); ar);
   258 
   259 fun prep_arity prep_tycon prep_sort prep thy (t, Ss, x) =
   260   test_arity thy (prep_tycon thy t, map (prep_sort thy) Ss, prep thy x);
   261 
   262 val read_arity = prep_arity Sign.intern_type Sign.read_sort Sign.read_sort;
   263 val cert_arity = prep_arity (K I) Sign.certify_sort Sign.certify_sort;
   264 
   265 
   266 (* instance declarations -- tactical proof *)
   267 
   268 local
   269 
   270 fun ext_inst_subclass prep_classrel raw_cc tac thy =
   271   let
   272     val (c1, c2) = prep_classrel thy raw_cc;
   273     val txt = quote (Sign.string_of_classrel thy [c1, c2]);
   274     val _ = message ("Proving class inclusion " ^ txt ^ " ...");
   275     val th = Goal.prove thy [] [] (mk_classrel (c1, c2)) (K tac) handle ERROR_MESSAGE msg =>
   276       error (msg ^ "\nThe error(s) above occurred while trying to prove " ^ txt);
   277   in add_classrel_thms [th] thy end;
   278 
   279 fun ext_inst_arity prep_arity raw_arity tac thy =
   280   let
   281     val arity = prep_arity thy raw_arity;
   282     val txt = quote (Sign.string_of_arity thy arity);
   283     val _ = message ("Proving type arity " ^ txt ^ " ...");
   284     val props = (mk_arities arity);
   285     val ths = Goal.prove_multi thy [] [] props
   286       (fn _ => Tactic.smart_conjunction_tac (length props) THEN tac) handle ERROR_MESSAGE msg =>
   287         error (msg ^ "\nThe error(s) above occurred while trying to prove " ^ txt);
   288   in add_arity_thms ths thy end;
   289 
   290 in
   291 
   292 val add_inst_subclass = ext_inst_subclass read_classrel;
   293 val add_inst_subclass_i = ext_inst_subclass cert_classrel;
   294 val add_inst_arity = ext_inst_arity read_arity;
   295 val add_inst_arity_i = ext_inst_arity cert_arity;
   296 
   297 end;
   298 
   299 
   300 (* instance declarations -- Isar proof *)
   301 
   302 local
   303 
   304 fun gen_instance mk_prop add_thms inst thy = thy
   305   |> ProofContext.init
   306   |> Proof.theorem_i Drule.internalK NONE (K (fold add_thms)) NONE ("", [])
   307     (map (fn t => (("", []), [(t, ([], []))])) (mk_prop thy inst));
   308 
   309 in
   310 
   311 val instance_subclass =
   312   gen_instance (single oo (mk_classrel oo read_classrel)) add_classrel_thms;
   313 val instance_subclass_i =
   314   gen_instance (single oo (mk_classrel oo cert_classrel)) add_classrel_thms;
   315 val instance_arity = gen_instance (mk_arities oo read_arity) add_arity_thms;
   316 val instance_arity_i = gen_instance (mk_arities oo cert_arity) add_arity_thms;
   317 
   318 end;
   319 
   320 
   321 (* tactics and methods *)
   322 
   323 fun intro_classes_tac facts st =
   324   (ALLGOALS (Method.insert_tac facts THEN'
   325       REPEAT_ALL_NEW (resolve_tac (class_axms (Thm.theory_of_thm st))))
   326     THEN Tactic.distinct_subgoals_tac) st;
   327 
   328 fun default_intro_classes_tac [] = intro_classes_tac []
   329   | default_intro_classes_tac _ = Tactical.no_tac;    (*no error message!*)
   330 
   331 fun default_tac rules ctxt facts =
   332   HEADGOAL (Method.some_rule_tac rules ctxt facts) ORELSE
   333     default_intro_classes_tac facts;
   334 
   335 val _ = Context.add_setup [Method.add_methods
   336  [("intro_classes", Method.no_args (Method.METHOD intro_classes_tac),
   337    "back-chain introduction rules of axiomatic type classes"),
   338   ("default", Method.thms_ctxt_args (Method.METHOD oo default_tac), "apply some intro/elim rule")]];
   339 
   340 
   341 
   342 (** outer syntax **)
   343 
   344 local structure P = OuterParse and K = OuterKeyword in
   345 
   346 val axclassP =
   347   OuterSyntax.command "axclass" "define axiomatic type class" K.thy_decl
   348     ((P.name -- Scan.optional ((P.$$$ "\\<subseteq>" || P.$$$ "<") |--
   349         P.!!! (P.list1 P.xname)) []) -- Scan.repeat P.spec_name
   350       >> (Toplevel.theory o (#1 oo uncurry add_axclass)));
   351 
   352 val instanceP =
   353   OuterSyntax.command "instance" "prove type arity or subclass relation" K.thy_goal
   354    ((P.xname -- ((P.$$$ "\\<subseteq>" || P.$$$ "<") |-- P.!!! P.xname) >> instance_subclass ||
   355       P.xname -- (P.$$$ "::" |-- P.!!! P.arity) >> (instance_arity o P.triple2))
   356     >> (Toplevel.print oo Toplevel.theory_to_proof));
   357 
   358 val _ = OuterSyntax.add_parsers [axclassP, instanceP];
   359 
   360 end;
   361 
   362 end;