src/Pure/Isar/class_target.ML
author wenzelm
Sun Nov 08 16:30:41 2009 +0100 (2009-11-08)
changeset 33519 e31a85f92ce9
parent 33173 b8ca12f6681a
child 33522 737589bb9bb8
permissions -rw-r--r--
adapted Generic_Data, Proof_Data;
tuned;
     1 (*  Title:      Pure/Isar/class_target.ML
     2     Author:     Florian Haftmann, TU Muenchen
     3 
     4 Type classes derived from primitive axclasses and locales - mechanisms.
     5 *)
     6 
     7 signature CLASS_TARGET =
     8 sig
     9   (*classes*)
    10   val register: class -> class list -> ((string * typ) * (string * typ)) list
    11     -> sort -> morphism -> morphism -> thm option -> thm option -> thm
    12     -> theory -> theory
    13 
    14   val is_class: theory -> class -> bool
    15   val base_sort: theory -> class -> sort
    16   val rules: theory -> class -> thm option * thm
    17   val these_params: theory -> sort -> (string * (class * (string * typ))) list
    18   val these_defs: theory -> sort -> thm list
    19   val these_operations: theory -> sort -> (string * (class * (typ * term))) list
    20   val print_classes: theory -> unit
    21 
    22   val begin: class list -> sort -> Proof.context -> Proof.context
    23   val init: class -> theory -> Proof.context
    24   val declare: class -> (binding * mixfix) * term -> theory -> theory
    25   val abbrev: class -> Syntax.mode -> (binding * mixfix) * term -> theory -> theory
    26   val class_prefix: string -> string
    27   val refresh_syntax: class -> Proof.context -> Proof.context
    28   val redeclare_operations: theory -> sort -> Proof.context -> Proof.context
    29 
    30   (*instances*)
    31   val init_instantiation: string list * (string * sort) list * sort
    32     -> theory -> local_theory
    33   val instance_arity_cmd: xstring list * xstring list * xstring -> theory -> Proof.state
    34   val instantiation_instance: (local_theory -> local_theory)
    35     -> local_theory -> Proof.state
    36   val prove_instantiation_instance: (Proof.context -> tactic)
    37     -> local_theory -> local_theory
    38   val prove_instantiation_exit: (Proof.context -> tactic)
    39     -> local_theory -> theory
    40   val prove_instantiation_exit_result: (morphism -> 'a -> 'b)
    41     -> (Proof.context -> 'b -> tactic) -> 'a -> local_theory -> 'b * theory
    42   val conclude_instantiation: local_theory -> local_theory
    43   val instantiation_param: local_theory -> binding -> string option
    44   val confirm_declaration: binding -> local_theory -> local_theory
    45   val pretty_instantiation: local_theory -> Pretty.T
    46   val read_multi_arity: theory -> xstring list * xstring list * xstring
    47     -> string list * (string * sort) list * sort
    48   val type_name: string -> string
    49 
    50   (*subclasses*)
    51   val register_subclass: class * class -> morphism option -> Element.witness option
    52     -> morphism -> theory -> theory
    53   val classrel: class * class -> theory -> Proof.state
    54   val classrel_cmd: xstring * xstring -> theory -> Proof.state
    55 
    56   (*tactics*)
    57   val intro_classes_tac: thm list -> tactic
    58   val default_intro_tac: Proof.context -> thm list -> tactic
    59 
    60   (*old axclass layer*)
    61   val axclass_cmd: binding * xstring list
    62     -> (Attrib.binding * string list) list
    63     -> theory -> class * theory
    64 end;
    65 
    66 structure Class_Target : CLASS_TARGET =
    67 struct
    68 
    69 (** class data **)
    70 
    71 datatype class_data = ClassData of {
    72 
    73   (* static part *)
    74   consts: (string * string) list
    75     (*locale parameter ~> constant name*),
    76   base_sort: sort,
    77   base_morph: morphism
    78     (*static part of canonical morphism*),
    79   export_morph: morphism,
    80   assm_intro: thm option,
    81   of_class: thm,
    82   axiom: thm option,
    83   
    84   (* dynamic part *)
    85   defs: thm list,
    86   operations: (string * (class * (typ * term))) list
    87 
    88 };
    89 
    90 fun make_class_data ((consts, base_sort, base_morph, export_morph, assm_intro, of_class, axiom),
    91     (defs, operations)) =
    92   ClassData { consts = consts, base_sort = base_sort,
    93     base_morph = base_morph, export_morph = export_morph, assm_intro = assm_intro,
    94     of_class = of_class, axiom = axiom, defs = defs, operations = operations };
    95 fun map_class_data f (ClassData { consts, base_sort, base_morph, export_morph, assm_intro,
    96     of_class, axiom, defs, operations }) =
    97   make_class_data (f ((consts, base_sort, base_morph, export_morph, assm_intro, of_class, axiom),
    98     (defs, operations)));
    99 fun merge_class_data _ (ClassData { consts = consts,
   100     base_sort = base_sort, base_morph = base_morph, export_morph = export_morph, assm_intro = assm_intro,
   101     of_class = of_class, axiom = axiom, defs = defs1, operations = operations1 },
   102   ClassData { consts = _, base_sort = _, base_morph = _, export_morph = _, assm_intro = _,
   103     of_class = _, axiom = _, defs = defs2, operations = operations2 }) =
   104   make_class_data ((consts, base_sort, base_morph, export_morph, assm_intro, of_class, axiom),
   105     (Thm.merge_thms (defs1, defs2),
   106       AList.merge (op =) (K true) (operations1, operations2)));
   107 
   108 structure ClassData = TheoryDataFun
   109 (
   110   type T = class_data Graph.T
   111   val empty = Graph.empty;
   112   val copy = I;
   113   val extend = I;
   114   fun merge _ = Graph.join merge_class_data;
   115 );
   116 
   117 
   118 (* queries *)
   119 
   120 fun lookup_class_data thy class = case try (Graph.get_node (ClassData.get thy)) class
   121  of SOME (ClassData data) => SOME data
   122   | NONE => NONE;
   123 
   124 fun the_class_data thy class = case lookup_class_data thy class
   125  of NONE => error ("Undeclared class " ^ quote class)
   126   | SOME data => data;
   127 
   128 val is_class = is_some oo lookup_class_data;
   129 
   130 val ancestry = Graph.all_succs o ClassData.get;
   131 val heritage = Graph.all_preds o ClassData.get;
   132 
   133 fun these_params thy =
   134   let
   135     fun params class =
   136       let
   137         val const_typs = (#params o AxClass.get_info thy) class;
   138         val const_names = (#consts o the_class_data thy) class;
   139       in
   140         (map o apsnd)
   141           (fn c => (class, (c, (the o AList.lookup (op =) const_typs) c))) const_names
   142       end;
   143   in maps params o ancestry thy end;
   144 
   145 val base_sort = #base_sort oo the_class_data;
   146 
   147 fun rules thy class =
   148   let val { axiom, of_class, ... } = the_class_data thy class
   149   in (axiom, of_class) end;
   150 
   151 fun all_assm_intros thy =
   152   Graph.fold (fn (_, (ClassData { assm_intro, ... }, _)) => fold (insert Thm.eq_thm)
   153     (the_list assm_intro)) (ClassData.get thy) [];
   154 
   155 fun these_defs thy = maps (#defs o the_class_data thy) o ancestry thy;
   156 fun these_operations thy = maps (#operations o the_class_data thy) o ancestry thy;
   157 
   158 val base_morphism = #base_morph oo the_class_data;
   159 fun morphism thy class = base_morphism thy class
   160   $> Element.eq_morphism thy (these_defs thy [class]);
   161 val export_morphism = #export_morph oo the_class_data;
   162 
   163 fun print_classes thy =
   164   let
   165     val ctxt = ProofContext.init thy;
   166     val algebra = Sign.classes_of thy;
   167     val arities =
   168       Symtab.empty
   169       |> Symtab.fold (fn (tyco, arities) => fold (fn (class, _) =>
   170            Symtab.map_default (class, []) (insert (op =) tyco)) arities)
   171              ((#arities o Sorts.rep_algebra) algebra);
   172     val the_arities = these o Symtab.lookup arities;
   173     fun mk_arity class tyco =
   174       let
   175         val Ss = Sorts.mg_domain algebra tyco [class];
   176       in Syntax.pretty_arity ctxt (tyco, Ss, [class]) end;
   177     fun mk_param (c, ty) = Pretty.str (Sign.extern_const thy c ^ " :: "
   178       ^ setmp_CRITICAL show_sorts false (Syntax.string_of_typ ctxt o Type.strip_sorts) ty);
   179     fun mk_entry class = (Pretty.block o Pretty.fbreaks o map_filter I) [
   180       (SOME o Pretty.str) ("class " ^ Sign.extern_class thy class ^ ":"),
   181       (SOME o Pretty.block) [Pretty.str "supersort: ",
   182         (Syntax.pretty_sort ctxt o Sign.minimize_sort thy o Sign.super_classes thy) class],
   183       ((fn [] => NONE | ps => (SOME o Pretty.block o Pretty.fbreaks)
   184           (Pretty.str "parameters:" :: ps)) o map mk_param
   185         o these o Option.map #params o try (AxClass.get_info thy)) class,
   186       (SOME o Pretty.block o Pretty.breaks) [
   187         Pretty.str "instances:",
   188         Pretty.list "" "" (map (mk_arity class) (the_arities class))
   189       ]
   190     ]
   191   in
   192     (Pretty.writeln o Pretty.chunks o separate (Pretty.str "")
   193       o map mk_entry o Sorts.all_classes) algebra
   194   end;
   195 
   196 
   197 (* updaters *)
   198 
   199 fun register class sups params base_sort base_morph export_morph
   200     axiom assm_intro of_class thy =
   201   let
   202     val operations = map (fn (v_ty as (_, ty), (c, _)) =>
   203       (c, (class, (ty, Free v_ty)))) params;
   204     val add_class = Graph.new_node (class,
   205         make_class_data (((map o pairself) fst params, base_sort,
   206           base_morph, export_morph, assm_intro, of_class, axiom), ([], operations)))
   207       #> fold (curry Graph.add_edge class) sups;
   208   in ClassData.map add_class thy end;
   209 
   210 fun activate_defs class thms thy =
   211   let
   212     val eq_morph = Element.eq_morphism thy thms;
   213     fun amend cls thy = Locale.amend_registration (cls, base_morphism thy cls)
   214       (eq_morph, true) (export_morphism thy cls) thy;
   215   in fold amend (heritage thy [class]) thy end;
   216 
   217 fun register_operation class (c, (t, some_def)) thy =
   218   let
   219     val base_sort = base_sort thy class;
   220     val prep_typ = map_type_tfree
   221       (fn (v, sort) => if Name.aT = v
   222         then TFree (v, base_sort) else TVar ((v, 0), sort));
   223     val t' = map_types prep_typ t;
   224     val ty' = Term.fastype_of t';
   225   in
   226     thy
   227     |> (ClassData.map o Graph.map_node class o map_class_data o apsnd)
   228       (fn (defs, operations) =>
   229         (fold cons (the_list some_def) defs,
   230           (c, (class, (ty', t'))) :: operations))
   231     |> is_some some_def ? activate_defs class (the_list some_def)
   232   end;
   233 
   234 fun register_subclass (sub, sup) some_dep_morph some_wit export thy =
   235   let
   236     val intros = (snd o rules thy) sup :: map_filter I
   237       [Option.map (Drule.standard' o Element.conclude_witness) some_wit,
   238         (fst o rules thy) sub];
   239     val tac = EVERY (map (TRYALL o Tactic.rtac) intros);
   240     val classrel = Skip_Proof.prove_global thy [] [] (Logic.mk_classrel (sub, sup))
   241       (K tac);
   242     val diff_sort = Sign.complete_sort thy [sup]
   243       |> subtract (op =) (Sign.complete_sort thy [sub])
   244       |> filter (is_class thy);
   245     val add_dependency = case some_dep_morph
   246      of SOME dep_morph => Locale.add_dependency sub
   247           (sup, dep_morph $> Element.satisfy_morphism (the_list some_wit)) export
   248       | NONE => I
   249   in
   250     thy
   251     |> AxClass.add_classrel classrel
   252     |> ClassData.map (Graph.add_edge (sub, sup))
   253     |> activate_defs sub (these_defs thy diff_sort)
   254     |> add_dependency
   255   end;
   256 
   257 
   258 (** classes and class target **)
   259 
   260 (* class context syntax *)
   261 
   262 fun these_unchecks thy =
   263   map (fn (c, (_, (ty, t))) => (t, Const (c, ty))) o these_operations thy;
   264 
   265 fun redeclare_const thy c =
   266   let val b = Long_Name.base_name c
   267   in Sign.intern_const thy b = c ? Variable.declare_const (b, c) end;
   268 
   269 fun synchronize_class_syntax sort base_sort ctxt =
   270   let
   271     val thy = ProofContext.theory_of ctxt;
   272     val algebra = Sign.classes_of thy;
   273     val operations = these_operations thy sort;
   274     fun subst_class_typ sort = map_type_tfree (K (TVar ((Name.aT, 0), sort)));
   275     val primary_constraints =
   276       (map o apsnd) (subst_class_typ base_sort o fst o snd) operations;
   277     val secondary_constraints =
   278       (map o apsnd) (fn (class, (ty, _)) => subst_class_typ [class] ty) operations;
   279     fun improve (c, ty) = (case AList.lookup (op =) primary_constraints c
   280      of SOME ty' => (case try (Type.raw_match (ty', ty)) Vartab.empty
   281          of SOME tyenv => (case Vartab.lookup tyenv (Name.aT, 0)
   282              of SOME (_, ty' as TVar (tvar as (vi, sort))) =>
   283                   if TypeInfer.is_param vi
   284                     andalso Sorts.sort_le algebra (base_sort, sort)
   285                       then SOME (ty', TFree (Name.aT, base_sort))
   286                       else NONE
   287               | _ => NONE)
   288           | NONE => NONE)
   289       | NONE => NONE)
   290     fun subst (c, ty) = Option.map snd (AList.lookup (op =) operations c);
   291     val unchecks = these_unchecks thy sort;
   292   in
   293     ctxt
   294     |> fold (redeclare_const thy o fst) primary_constraints
   295     |> Overloading.map_improvable_syntax (K (((primary_constraints, secondary_constraints),
   296         (((improve, subst), true), unchecks)), false))
   297     |> Overloading.set_primary_constraints
   298   end;
   299 
   300 fun refresh_syntax class ctxt =
   301   let
   302     val thy = ProofContext.theory_of ctxt;
   303     val base_sort = base_sort thy class;
   304   in synchronize_class_syntax [class] base_sort ctxt end;
   305 
   306 fun redeclare_operations thy sort =
   307   fold (redeclare_const thy o fst) (these_operations thy sort);
   308 
   309 fun begin sort base_sort ctxt =
   310   ctxt
   311   |> Variable.declare_term
   312       (Logic.mk_type (TFree (Name.aT, base_sort)))
   313   |> synchronize_class_syntax sort base_sort
   314   |> Overloading.add_improvable_syntax;
   315 
   316 fun init class thy =
   317   thy
   318   |> Locale.init class
   319   |> begin [class] (base_sort thy class);
   320 
   321 
   322 (* class target *)
   323 
   324 val class_prefix = Logic.const_of_class o Long_Name.base_name;
   325 
   326 fun declare class ((c, mx), dict) thy =
   327   let
   328     val morph = morphism thy class;
   329     val b = Morphism.binding morph c;
   330     val b_def = Morphism.binding morph (Binding.suffix_name "_dict" b);
   331     val c' = Sign.full_name thy b;
   332     val dict' = Morphism.term morph dict;
   333     val ty' = Term.fastype_of dict';
   334     val def_eq = Logic.mk_equals (Const (c', ty'), dict')
   335       |> map_types Type.strip_sorts;
   336   in
   337     thy
   338     |> Sign.declare_const ((b, Type.strip_sorts ty'), mx)
   339     |> snd
   340     |> Thm.add_def false false (b_def, def_eq)
   341     |>> Thm.varifyT
   342     |-> (fn def_thm => PureThy.store_thm (b_def, def_thm)
   343       #> snd
   344       #> register_operation class (c', (dict', SOME (Thm.symmetric def_thm))))
   345     |> Sign.add_const_constraint (c', SOME ty')
   346   end;
   347 
   348 fun abbrev class prmode ((c, mx), rhs) thy =
   349   let
   350     val morph = morphism thy class;
   351     val unchecks = these_unchecks thy [class];
   352     val b = Morphism.binding morph c;
   353     val c' = Sign.full_name thy b;
   354     val rhs' = Pattern.rewrite_term thy unchecks [] rhs;
   355     val ty' = Term.fastype_of rhs';
   356     val rhs'' = map_types Logic.varifyT rhs';
   357   in
   358     thy
   359     |> Sign.add_abbrev (#1 prmode) (b, rhs'')
   360     |> snd
   361     |> Sign.add_const_constraint (c', SOME ty')
   362     |> Sign.notation true prmode [(Const (c', ty'), mx)]
   363     |> not (#1 prmode = PrintMode.input) ? register_operation class (c', (rhs', NONE))
   364   end;
   365 
   366 
   367 (* simple subclasses *)
   368 
   369 local
   370 
   371 fun gen_classrel mk_prop classrel thy =
   372   let
   373     fun after_qed results =
   374       ProofContext.theory ((fold o fold) AxClass.add_classrel results);
   375   in
   376     thy
   377     |> ProofContext.init
   378     |> Proof.theorem_i NONE after_qed [[(mk_prop thy classrel, [])]]
   379   end;
   380 
   381 in
   382 
   383 val classrel =
   384   gen_classrel (Logic.mk_classrel oo AxClass.cert_classrel);
   385 val classrel_cmd =
   386   gen_classrel (Logic.mk_classrel oo AxClass.read_classrel);
   387 
   388 end; (*local*)
   389 
   390 
   391 (** instantiation target **)
   392 
   393 (* bookkeeping *)
   394 
   395 datatype instantiation = Instantiation of {
   396   arities: string list * (string * sort) list * sort,
   397   params: ((string * string) * (string * typ)) list
   398     (*(instantiation parameter, type constructor), (local instantiation parameter, typ)*)
   399 }
   400 
   401 structure Instantiation = Proof_Data
   402 (
   403   type T = instantiation
   404   fun init _ = Instantiation { arities = ([], [], []), params = [] };
   405 );
   406 
   407 fun mk_instantiation (arities, params) =
   408   Instantiation { arities = arities, params = params };
   409 fun get_instantiation lthy = case Instantiation.get (LocalTheory.target_of lthy)
   410  of Instantiation data => data;
   411 fun map_instantiation f = (LocalTheory.target o Instantiation.map)
   412   (fn Instantiation { arities, params } => mk_instantiation (f (arities, params)));
   413 
   414 fun the_instantiation lthy = case get_instantiation lthy
   415  of { arities = ([], [], []), ... } => error "No instantiation target"
   416   | data => data;
   417 
   418 val instantiation_params = #params o get_instantiation;
   419 
   420 fun instantiation_param lthy b = instantiation_params lthy
   421   |> find_first (fn (_, (v, _)) => Binding.name_of b = v)
   422   |> Option.map (fst o fst);
   423 
   424 fun read_multi_arity thy (raw_tycos, raw_sorts, raw_sort) =
   425   let
   426     val all_arities = map (fn raw_tyco => Sign.read_arity thy
   427       (raw_tyco, raw_sorts, raw_sort)) raw_tycos;
   428     val tycos = map #1 all_arities;
   429     val (_, sorts, sort) = hd all_arities;
   430     val vs = Name.names Name.context Name.aT sorts;
   431   in (tycos, vs, sort) end;
   432 
   433 
   434 (* syntax *)
   435 
   436 fun synchronize_inst_syntax ctxt =
   437   let
   438     val Instantiation { arities = (_, _, sort), params = params } = Instantiation.get ctxt;
   439 
   440     val lookup_inst_param = AxClass.lookup_inst_param (Sign.consts_of (ProofContext.theory_of ctxt)) params;
   441     fun subst (c, ty) = case lookup_inst_param (c, ty)
   442      of SOME (v_ty as (_, ty)) => SOME (ty, Free v_ty)
   443       | NONE => NONE;
   444     val unchecks =
   445       map (fn ((c, _), v_ty as (_, ty)) => (Free v_ty, Const (c, ty))) params;
   446   in
   447     ctxt
   448     |> Overloading.map_improvable_syntax
   449          (fn (((primary_constraints, _), (((improve, _), _), _)), _) =>
   450             (((primary_constraints, []), (((improve, subst), false), unchecks)), false))
   451   end;
   452 
   453 
   454 (* target *)
   455 
   456 val sanatize_name = (*FIXME*)
   457   let
   458     fun is_valid s = Symbol.is_ascii_letter s orelse Symbol.is_ascii_digit s
   459       orelse s = "'" orelse s = "_";
   460     val is_junk = not o is_valid andf Symbol.is_regular;
   461     val junk = Scan.many is_junk;
   462     val scan_valids = Symbol.scanner "Malformed input"
   463       ((junk |--
   464         (Scan.optional (Scan.one Symbol.is_ascii_letter) "x" ^^ (Scan.many is_valid >> implode)
   465         --| junk))
   466       ::: Scan.repeat ((Scan.many1 is_valid >> implode) --| junk));
   467   in
   468     explode #> scan_valids #> implode
   469   end;
   470 
   471 fun type_name "*" = "prod"
   472   | type_name "+" = "sum"
   473   | type_name s = sanatize_name (Long_Name.base_name s);
   474 
   475 fun resort_terms pp algebra consts constraints ts =
   476   let
   477     fun matchings (Const (c_ty as (c, _))) = (case constraints c
   478          of NONE => I
   479           | SOME sorts => fold2 (curry (Sorts.meet_sort algebra))
   480               (Consts.typargs consts c_ty) sorts)
   481       | matchings _ = I
   482     val tvartab = (fold o fold_aterms) matchings ts Vartab.empty
   483       handle Sorts.CLASS_ERROR e => error (Sorts.class_error pp e);
   484     val inst = map_type_tvar
   485       (fn (vi, sort) => TVar (vi, the_default sort (Vartab.lookup tvartab vi)));
   486   in if Vartab.is_empty tvartab then NONE else SOME ((map o map_types) inst ts) end;
   487 
   488 fun init_instantiation (tycos, vs, sort) thy =
   489   let
   490     val _ = if null tycos then error "At least one arity must be given" else ();
   491     val class_params = these_params thy (filter (can (AxClass.get_info thy)) sort);
   492     fun get_param tyco (param, (_, (c, ty))) =
   493       if can (AxClass.param_of_inst thy) (c, tyco)
   494       then NONE else SOME ((c, tyco),
   495         (param ^ "_" ^ type_name tyco, map_atyps (K (Type (tyco, map TFree vs))) ty));
   496     val params = map_product get_param tycos class_params |> map_filter I;
   497     val primary_constraints = map (apsnd
   498       (map_atyps (K (TVar ((Name.aT, 0), [])))) o snd o snd) class_params;
   499     val pp = Syntax.pp_global thy;
   500     val algebra = Sign.classes_of thy
   501       |> fold (fn tyco => Sorts.add_arities pp
   502             (tyco, map (fn class => (class, map snd vs)) sort)) tycos;
   503     val consts = Sign.consts_of thy;
   504     val improve_constraints = AList.lookup (op =)
   505       (map (fn (_, (class, (c, _))) => (c, [[class]])) class_params);
   506     fun resort_check ts ctxt = case resort_terms pp algebra consts improve_constraints ts
   507      of NONE => NONE
   508       | SOME ts' => SOME (ts', ctxt);
   509     val lookup_inst_param = AxClass.lookup_inst_param consts params;
   510     val typ_instance = Type.typ_instance (Sign.tsig_of thy);
   511     fun improve (c, ty) = case lookup_inst_param (c, ty)
   512      of SOME (_, ty') => if typ_instance (ty', ty) then SOME (ty, ty') else NONE
   513       | NONE => NONE;
   514   in
   515     thy
   516     |> Theory.checkpoint
   517     |> ProofContext.init
   518     |> Instantiation.put (mk_instantiation ((tycos, vs, sort), params))
   519     |> fold (Variable.declare_typ o TFree) vs
   520     |> fold (Variable.declare_names o Free o snd) params
   521     |> (Overloading.map_improvable_syntax o apfst)
   522          (K ((primary_constraints, []), (((improve, K NONE), false), [])))
   523     |> Overloading.add_improvable_syntax
   524     |> Context.proof_map (Syntax.add_term_check 0 "resorting" resort_check)
   525     |> synchronize_inst_syntax
   526   end;
   527 
   528 fun confirm_declaration b = (map_instantiation o apsnd)
   529   (filter_out (fn (_, (c', _)) => c' = Binding.name_of b))
   530   #> LocalTheory.target synchronize_inst_syntax
   531 
   532 fun gen_instantiation_instance do_proof after_qed lthy =
   533   let
   534     val (tycos, vs, sort) = (#arities o the_instantiation) lthy;
   535     val arities_proof = maps (fn tyco => Logic.mk_arities (tyco, map snd vs, sort)) tycos;
   536     fun after_qed' results =
   537       LocalTheory.theory (fold (AxClass.add_arity o Thm.varifyT) results)
   538       #> after_qed;
   539   in
   540     lthy
   541     |> do_proof after_qed' arities_proof
   542   end;
   543 
   544 val instantiation_instance = gen_instantiation_instance (fn after_qed => fn ts =>
   545   Proof.theorem_i NONE (after_qed o map the_single) (map (fn t => [(t, [])]) ts));
   546 
   547 fun prove_instantiation_instance tac = gen_instantiation_instance (fn after_qed =>
   548   fn ts => fn lthy => after_qed (map (fn t => Goal.prove lthy [] [] t
   549     (fn {context, ...} => tac context)) ts) lthy) I;
   550 
   551 fun prove_instantiation_exit tac = prove_instantiation_instance tac
   552   #> LocalTheory.exit_global;
   553 
   554 fun prove_instantiation_exit_result f tac x lthy =
   555   let
   556     val morph = ProofContext.export_morphism lthy
   557       (ProofContext.init (ProofContext.theory_of lthy));
   558     val y = f morph x;
   559   in
   560     lthy
   561     |> prove_instantiation_exit (fn ctxt => tac ctxt y)
   562     |> pair y
   563   end;
   564 
   565 fun conclude_instantiation lthy =
   566   let
   567     val { arities, params } = the_instantiation lthy;
   568     val (tycos, vs, sort) = arities;
   569     val thy = ProofContext.theory_of lthy;
   570     val _ = map (fn tyco => if Sign.of_sort thy
   571         (Type (tyco, map TFree vs), sort)
   572       then () else error ("Missing instance proof for type " ^ quote (Sign.extern_type thy tyco)))
   573         tycos;
   574   in lthy end;
   575 
   576 fun pretty_instantiation lthy =
   577   let
   578     val { arities, params } = the_instantiation lthy;
   579     val (tycos, vs, sort) = arities;
   580     val thy = ProofContext.theory_of lthy;
   581     fun pr_arity tyco = Syntax.pretty_arity lthy (tyco, map snd vs, sort);
   582     fun pr_param ((c, _), (v, ty)) =
   583       (Pretty.block o Pretty.breaks) [Pretty.str v, Pretty.str "==",
   584         (Pretty.str o Sign.extern_const thy) c, Pretty.str "::", Syntax.pretty_typ_global thy ty];
   585   in
   586     (Pretty.block o Pretty.fbreaks)
   587       (Pretty.str "instantiation" :: map pr_arity tycos @ map pr_param params)
   588   end;
   589 
   590 
   591 (* simplified instantiation interface with no class parameter *)
   592 
   593 fun instance_arity_cmd raw_arities thy =
   594   let
   595     val (tycos, vs, sort) = read_multi_arity thy raw_arities;
   596     val sorts = map snd vs;
   597     val arities = maps (fn tyco => Logic.mk_arities (tyco, sorts, sort)) tycos;
   598     fun after_qed results = ProofContext.theory
   599       ((fold o fold) AxClass.add_arity results);
   600   in
   601     thy
   602     |> ProofContext.init
   603     |> Proof.theorem_i NONE after_qed (map (fn t => [(t, [])]) arities)
   604   end;
   605 
   606 
   607 (** tactics and methods **)
   608 
   609 fun intro_classes_tac facts st =
   610   let
   611     val thy = Thm.theory_of_thm st;
   612     val classes = Sign.all_classes thy;
   613     val class_trivs = map (Thm.class_triv thy) classes;
   614     val class_intros = map_filter (try (#intro o AxClass.get_info thy)) classes;
   615     val assm_intros = all_assm_intros thy;
   616   in
   617     Method.intros_tac (class_trivs @ class_intros @ assm_intros) facts st
   618   end;
   619 
   620 fun default_intro_tac ctxt [] =
   621       intro_classes_tac [] ORELSE Locale.intro_locales_tac true ctxt []
   622   | default_intro_tac _ _ = no_tac;
   623 
   624 fun default_tac rules ctxt facts =
   625   HEADGOAL (Method.some_rule_tac rules ctxt facts) ORELSE
   626     default_intro_tac ctxt facts;
   627 
   628 val _ = Context.>> (Context.map_theory
   629  (Method.setup (Binding.name "intro_classes") (Scan.succeed (K (METHOD intro_classes_tac)))
   630     "back-chain introduction rules of classes" #>
   631   Method.setup (Binding.name "default") (Attrib.thms >> (METHOD oo default_tac))
   632     "apply some intro/elim rule"));
   633 
   634 
   635 (** old axclass command **)
   636 
   637 fun axclass_cmd (class, raw_superclasses) raw_specs thy =
   638   let
   639     val _ = Output.legacy_feature "command \"axclass\" deprecated; use \"class\" instead.";
   640     val ctxt = ProofContext.init thy;
   641     val superclasses = map (Sign.read_class thy) raw_superclasses;
   642     val name_atts = map ((apsnd o map) (Attrib.attribute thy) o fst)
   643       raw_specs;
   644     val axiomss = ProofContext.read_propp (ctxt, map (map (rpair []) o snd)
   645           raw_specs)
   646       |> snd
   647       |> (map o map) fst;
   648   in
   649     AxClass.define_class (class, superclasses) []
   650       (name_atts ~~ axiomss) thy
   651   end;
   652 
   653 end;
   654