src/Pure/Isar/class_target.ML
author haftmann
Wed Jul 15 18:20:08 2009 +0200 (2009-07-15)
changeset 32074 76d6ba08a05f
parent 31988 801aabf9f376
child 32379 a97e9caebd60
child 32800 57fcca4e7c0e
permissions -rw-r--r--
simplification of locale interfaces
     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 -> 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 -> Properties.T
    25     -> (binding * mixfix) * term -> theory -> theory
    26   val abbrev: class -> Syntax.mode -> Properties.T
    27     -> (binding * mixfix) * term -> theory -> theory
    28   val class_prefix: string -> string
    29   val refresh_syntax: class -> Proof.context -> Proof.context
    30   val redeclare_operations: theory -> sort -> Proof.context -> Proof.context
    31 
    32   (*instances*)
    33   val init_instantiation: string list * (string * sort) list * sort
    34     -> theory -> local_theory
    35   val instance_arity_cmd: xstring list * xstring list * xstring -> theory -> Proof.state
    36   val instantiation_instance: (local_theory -> local_theory)
    37     -> local_theory -> Proof.state
    38   val prove_instantiation_instance: (Proof.context -> tactic)
    39     -> local_theory -> local_theory
    40   val prove_instantiation_exit: (Proof.context -> tactic)
    41     -> local_theory -> theory
    42   val prove_instantiation_exit_result: (morphism -> 'a -> 'b)
    43     -> (Proof.context -> 'b -> tactic) -> 'a -> local_theory -> 'b * theory
    44   val conclude_instantiation: local_theory -> local_theory
    45   val instantiation_param: local_theory -> binding -> string option
    46   val confirm_declaration: binding -> local_theory -> local_theory
    47   val pretty_instantiation: local_theory -> Pretty.T
    48   val read_multi_arity: theory -> xstring list * xstring list * xstring
    49     -> string list * (string * sort) list * sort
    50   val type_name: string -> string
    51 
    52   (*subclasses*)
    53   val register_subclass: class * class -> morphism option -> Element.witness option
    54     -> morphism -> theory -> theory
    55   val classrel: class * class -> theory -> Proof.state
    56   val classrel_cmd: xstring * xstring -> theory -> Proof.state
    57 
    58   (*tactics*)
    59   val intro_classes_tac: thm list -> tactic
    60   val default_intro_tac: Proof.context -> thm list -> tactic
    61 
    62   (*old axclass layer*)
    63   val axclass_cmd: binding * xstring list
    64     -> (Attrib.binding * string list) list
    65     -> theory -> class * theory
    66 end;
    67 
    68 structure Class_Target : CLASS_TARGET =
    69 struct
    70 
    71 (** class data **)
    72 
    73 datatype class_data = ClassData of {
    74 
    75   (* static part *)
    76   consts: (string * string) list
    77     (*locale parameter ~> constant name*),
    78   base_sort: sort,
    79   base_morph: morphism
    80     (*static part of canonical morphism*),
    81   assm_intro: thm option,
    82   of_class: thm,
    83   axiom: thm option,
    84   
    85   (* dynamic part *)
    86   defs: thm list,
    87   operations: (string * (class * (typ * term))) list
    88 
    89 };
    90 
    91 fun make_class_data ((consts, base_sort, base_morph, assm_intro, of_class, axiom),
    92     (defs, operations)) =
    93   ClassData { consts = consts, base_sort = base_sort,
    94     base_morph = base_morph, assm_intro = assm_intro, of_class = of_class, axiom = axiom,
    95     defs = defs, operations = operations };
    96 fun map_class_data f (ClassData { consts, base_sort, base_morph, assm_intro,
    97     of_class, axiom, defs, operations }) =
    98   make_class_data (f ((consts, base_sort, base_morph, assm_intro, of_class, axiom),
    99     (defs, operations)));
   100 fun merge_class_data _ (ClassData { consts = consts,
   101     base_sort = base_sort, base_morph = base_morph, assm_intro = assm_intro,
   102     of_class = of_class, axiom = axiom, defs = defs1, operations = operations1 },
   103   ClassData { consts = _, base_sort = _, base_morph = _, assm_intro = _,
   104     of_class = _, axiom = _, defs = defs2, operations = operations2 }) =
   105   make_class_data ((consts, base_sort, base_morph, assm_intro, of_class, axiom),
   106     (Thm.merge_thms (defs1, defs2),
   107       AList.merge (op =) (K true) (operations1, operations2)));
   108 
   109 structure ClassData = TheoryDataFun
   110 (
   111   type T = class_data Graph.T
   112   val empty = Graph.empty;
   113   val copy = I;
   114   val extend = I;
   115   fun merge _ = Graph.join merge_class_data;
   116 );
   117 
   118 
   119 (* queries *)
   120 
   121 fun lookup_class_data thy class = case try (Graph.get_node (ClassData.get thy)) class
   122  of SOME (ClassData data) => SOME data
   123   | NONE => NONE;
   124 
   125 fun the_class_data thy class = case lookup_class_data thy class
   126  of NONE => error ("Undeclared class " ^ quote class)
   127   | SOME data => data;
   128 
   129 val is_class = is_some oo lookup_class_data;
   130 
   131 val ancestry = Graph.all_succs o ClassData.get;
   132 val heritage = Graph.all_preds o ClassData.get;
   133 
   134 fun these_params thy =
   135   let
   136     fun params class =
   137       let
   138         val const_typs = (#params o AxClass.get_info thy) class;
   139         val const_names = (#consts o the_class_data thy) class;
   140       in
   141         (map o apsnd)
   142           (fn c => (class, (c, (the o AList.lookup (op =) const_typs) c))) const_names
   143       end;
   144   in maps params o ancestry thy end;
   145 
   146 val base_sort = #base_sort oo the_class_data;
   147 
   148 fun rules thy class =
   149   let val { axiom, of_class, ... } = the_class_data thy class
   150   in (axiom, of_class) end;
   151 
   152 fun all_assm_intros thy =
   153   Graph.fold (fn (_, (ClassData { assm_intro, ... }, _)) => fold (insert Thm.eq_thm)
   154     (the_list assm_intro)) (ClassData.get thy) [];
   155 
   156 fun these_defs thy = maps (#defs o the_class_data thy) o ancestry thy;
   157 fun these_operations thy = maps (#operations o the_class_data thy) o ancestry thy;
   158 
   159 val base_morphism = #base_morph oo the_class_data;
   160 fun morphism thy class = base_morphism thy class
   161   $> Element.eq_morphism thy (these_defs thy [class]);
   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 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 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           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 eq_morph
   214       (cls, 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 = SkipProof.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 pos ((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 pos ((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 pos ((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) pos (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 = ProofDataFun
   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     |> ProofContext.init
   517     |> Instantiation.put (mk_instantiation ((tycos, vs, sort), params))
   518     |> fold (Variable.declare_typ o TFree) vs
   519     |> fold (Variable.declare_names o Free o snd) params
   520     |> (Overloading.map_improvable_syntax o apfst)
   521          (K ((primary_constraints, []), (((improve, K NONE), false), [])))
   522     |> Overloading.add_improvable_syntax
   523     |> Context.proof_map (Syntax.add_term_check 0 "resorting" resort_check)
   524     |> synchronize_inst_syntax
   525   end;
   526 
   527 fun confirm_declaration b = (map_instantiation o apsnd)
   528   (filter_out (fn (_, (c', _)) => c' = Binding.name_of b))
   529   #> LocalTheory.target synchronize_inst_syntax
   530 
   531 fun gen_instantiation_instance do_proof after_qed lthy =
   532   let
   533     val (tycos, vs, sort) = (#arities o the_instantiation) lthy;
   534     val arities_proof = maps (fn tyco => Logic.mk_arities (tyco, map snd vs, sort)) tycos;
   535     fun after_qed' results =
   536       LocalTheory.theory (fold (AxClass.add_arity o Thm.varifyT) results)
   537       #> after_qed;
   538   in
   539     lthy
   540     |> do_proof after_qed' arities_proof
   541   end;
   542 
   543 val instantiation_instance = gen_instantiation_instance (fn after_qed => fn ts =>
   544   Proof.theorem_i NONE (after_qed o map the_single) (map (fn t => [(t, [])]) ts));
   545 
   546 fun prove_instantiation_instance tac = gen_instantiation_instance (fn after_qed =>
   547   fn ts => fn lthy => after_qed (map (fn t => Goal.prove lthy [] [] t
   548     (fn {context, ...} => tac context)) ts) lthy) I;
   549 
   550 fun prove_instantiation_exit tac = prove_instantiation_instance tac
   551   #> LocalTheory.exit_global;
   552 
   553 fun prove_instantiation_exit_result f tac x lthy =
   554   let
   555     val morph = ProofContext.export_morphism lthy
   556       (ProofContext.init (ProofContext.theory_of lthy));
   557     val y = f morph x;
   558   in
   559     lthy
   560     |> prove_instantiation_exit (fn ctxt => tac ctxt y)
   561     |> pair y
   562   end;
   563 
   564 fun conclude_instantiation lthy =
   565   let
   566     val { arities, params } = the_instantiation lthy;
   567     val (tycos, vs, sort) = arities;
   568     val thy = ProofContext.theory_of lthy;
   569     val _ = map (fn tyco => if Sign.of_sort thy
   570         (Type (tyco, map TFree vs), sort)
   571       then () else error ("Missing instance proof for type " ^ quote (Sign.extern_type thy tyco)))
   572         tycos;
   573   in lthy end;
   574 
   575 fun pretty_instantiation lthy =
   576   let
   577     val { arities, params } = the_instantiation lthy;
   578     val (tycos, vs, sort) = arities;
   579     val thy = ProofContext.theory_of lthy;
   580     fun pr_arity tyco = Syntax.pretty_arity lthy (tyco, map snd vs, sort);
   581     fun pr_param ((c, _), (v, ty)) =
   582       (Pretty.block o Pretty.breaks) [Pretty.str v, Pretty.str "==",
   583         (Pretty.str o Sign.extern_const thy) c, Pretty.str "::", Syntax.pretty_typ_global thy ty];
   584   in
   585     (Pretty.block o Pretty.fbreaks)
   586       (Pretty.str "instantiation" :: map pr_arity tycos @ map pr_param params)
   587   end;
   588 
   589 
   590 (* simplified instantiation interface with no class parameter *)
   591 
   592 fun instance_arity_cmd raw_arities thy =
   593   let
   594     val (tycos, vs, sort) = read_multi_arity thy raw_arities;
   595     val sorts = map snd vs;
   596     val arities = maps (fn tyco => Logic.mk_arities (tyco, sorts, sort)) tycos;
   597     fun after_qed results = ProofContext.theory
   598       ((fold o fold) AxClass.add_arity results);
   599   in
   600     thy
   601     |> ProofContext.init
   602     |> Proof.theorem_i NONE after_qed (map (fn t => [(t, [])]) arities)
   603   end;
   604 
   605 
   606 (** tactics and methods **)
   607 
   608 fun intro_classes_tac facts st =
   609   let
   610     val thy = Thm.theory_of_thm st;
   611     val classes = Sign.all_classes thy;
   612     val class_trivs = map (Thm.class_triv thy) classes;
   613     val class_intros = map_filter (try (#intro o AxClass.get_info thy)) classes;
   614     val assm_intros = all_assm_intros thy;
   615   in
   616     Method.intros_tac (class_trivs @ class_intros @ assm_intros) facts st
   617   end;
   618 
   619 fun default_intro_tac ctxt [] =
   620       intro_classes_tac [] ORELSE Locale.intro_locales_tac true ctxt []
   621   | default_intro_tac _ _ = no_tac;
   622 
   623 fun default_tac rules ctxt facts =
   624   HEADGOAL (Method.some_rule_tac rules ctxt facts) ORELSE
   625     default_intro_tac ctxt facts;
   626 
   627 val _ = Context.>> (Context.map_theory
   628  (Method.setup (Binding.name "intro_classes") (Scan.succeed (K (METHOD intro_classes_tac)))
   629     "back-chain introduction rules of classes" #>
   630   Method.setup (Binding.name "default") (Attrib.thms >> (METHOD oo default_tac))
   631     "apply some intro/elim rule"));
   632 
   633 
   634 (** old axclass command **)
   635 
   636 fun axclass_cmd (class, raw_superclasses) raw_specs thy =
   637   let
   638     val _ = Output.legacy_feature "command \"axclass\" deprecated; use \"class\" instead.";
   639     val ctxt = ProofContext.init thy;
   640     val superclasses = map (Sign.read_class thy) raw_superclasses;
   641     val name_atts = map ((apsnd o map) (Attrib.attribute thy) o fst)
   642       raw_specs;
   643     val axiomss = ProofContext.read_propp (ctxt, map (map (rpair []) o snd)
   644           raw_specs)
   645       |> snd
   646       |> (map o map) fst;
   647   in
   648     AxClass.define_class (class, superclasses) []
   649       (name_atts ~~ axiomss) thy
   650   end;
   651 
   652 end;
   653