src/Pure/Isar/class_target.ML
author haftmann
Mon Nov 30 11:42:48 2009 +0100 (2009-11-30)
changeset 33967 e191b400a8e0
parent 33671 4b0f2599ed48
child 33969 1e7ca47c6c3d
permissions -rw-r--r--
more accurate linerarity
     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 = Theory_Data
   109 (
   110   type T = class_data Graph.T
   111   val empty = Graph.empty;
   112   val extend = I;
   113   val merge = Graph.join merge_class_data;
   114 );
   115 
   116 
   117 (* queries *)
   118 
   119 fun lookup_class_data thy class = case try (Graph.get_node (ClassData.get thy)) class
   120  of SOME (ClassData data) => SOME data
   121   | NONE => NONE;
   122 
   123 fun the_class_data thy class = case lookup_class_data thy class
   124  of NONE => error ("Undeclared class " ^ quote class)
   125   | SOME data => data;
   126 
   127 val is_class = is_some oo lookup_class_data;
   128 
   129 val ancestry = Graph.all_succs o ClassData.get;
   130 val heritage = Graph.all_preds o ClassData.get;
   131 
   132 fun these_params thy =
   133   let
   134     fun params class =
   135       let
   136         val const_typs = (#params o AxClass.get_info thy) class;
   137         val const_names = (#consts o the_class_data thy) class;
   138       in
   139         (map o apsnd)
   140           (fn c => (class, (c, (the o AList.lookup (op =) const_typs) c))) const_names
   141       end;
   142   in maps params o ancestry thy end;
   143 
   144 val base_sort = #base_sort oo the_class_data;
   145 
   146 fun rules thy class =
   147   let val { axiom, of_class, ... } = the_class_data thy class
   148   in (axiom, of_class) end;
   149 
   150 fun all_assm_intros thy =
   151   Graph.fold (fn (_, (ClassData { assm_intro, ... }, _)) => fold (insert Thm.eq_thm)
   152     (the_list assm_intro)) (ClassData.get thy) [];
   153 
   154 fun these_defs thy = maps (#defs o the_class_data thy) o ancestry thy;
   155 fun these_operations thy = maps (#operations o the_class_data thy) o ancestry thy;
   156 
   157 val base_morphism = #base_morph oo the_class_data;
   158 fun morphism thy class = base_morphism thy class
   159   $> Element.eq_morphism thy (these_defs thy [class]);
   160 val export_morphism = #export_morph oo the_class_data;
   161 
   162 fun print_classes thy =
   163   let
   164     val ctxt = ProofContext.init thy;
   165     val algebra = Sign.classes_of thy;
   166     val arities =
   167       Symtab.empty
   168       |> Symtab.fold (fn (tyco, arities) => fold (fn (class, _) =>
   169            Symtab.map_default (class, []) (insert (op =) tyco)) arities)
   170              ((#arities o Sorts.rep_algebra) algebra);
   171     val the_arities = these o Symtab.lookup arities;
   172     fun mk_arity class tyco =
   173       let
   174         val Ss = Sorts.mg_domain algebra tyco [class];
   175       in Syntax.pretty_arity ctxt (tyco, Ss, [class]) end;
   176     fun mk_param (c, ty) = Pretty.str (Sign.extern_const thy c ^ " :: "
   177       ^ setmp_CRITICAL show_sorts false (Syntax.string_of_typ ctxt o Type.strip_sorts) ty);
   178     fun mk_entry class = (Pretty.block o Pretty.fbreaks o map_filter I) [
   179       (SOME o Pretty.str) ("class " ^ Sign.extern_class thy class ^ ":"),
   180       (SOME o Pretty.block) [Pretty.str "supersort: ",
   181         (Syntax.pretty_sort ctxt o Sign.minimize_sort thy o Sign.super_classes thy) class],
   182       ((fn [] => NONE | ps => (SOME o Pretty.block o Pretty.fbreaks)
   183           (Pretty.str "parameters:" :: ps)) o map mk_param
   184         o these o Option.map #params o try (AxClass.get_info thy)) class,
   185       (SOME o Pretty.block o Pretty.breaks) [
   186         Pretty.str "instances:",
   187         Pretty.list "" "" (map (mk_arity class) (the_arities class))
   188       ]
   189     ]
   190   in
   191     (Pretty.writeln o Pretty.chunks o separate (Pretty.str "")
   192       o map mk_entry o Sorts.all_classes) algebra
   193   end;
   194 
   195 
   196 (* updaters *)
   197 
   198 fun register class sups params base_sort base_morph export_morph
   199     axiom assm_intro of_class thy =
   200   let
   201     val operations = map (fn (v_ty as (_, ty), (c, _)) =>
   202       (c, (class, (ty, Free v_ty)))) params;
   203     val add_class = Graph.new_node (class,
   204         make_class_data (((map o pairself) fst params, base_sort,
   205           base_morph, export_morph, assm_intro, of_class, axiom), ([], operations)))
   206       #> fold (curry Graph.add_edge class) sups;
   207   in ClassData.map add_class thy end;
   208 
   209 fun activate_defs class thms thy =
   210   let
   211     val eq_morph = Element.eq_morphism thy thms;
   212     fun amend cls thy = Locale.amend_registration (cls, base_morphism thy cls)
   213       (eq_morph, true) (export_morphism thy cls) thy;
   214   in fold amend (heritage thy [class]) thy end;
   215 
   216 fun register_operation class (c, (t, some_def)) thy =
   217   let
   218     val base_sort = base_sort thy class;
   219     val prep_typ = map_type_tfree
   220       (fn (v, sort) => if Name.aT = v
   221         then TFree (v, base_sort) else TVar ((v, 0), sort));
   222     val t' = map_types prep_typ t;
   223     val ty' = Term.fastype_of t';
   224   in
   225     thy
   226     |> (ClassData.map o Graph.map_node class o map_class_data o apsnd)
   227       (fn (defs, operations) =>
   228         (fold cons (the_list some_def) defs,
   229           (c, (class, (ty', t'))) :: operations))
   230     |> is_some some_def ? activate_defs class (the_list some_def)
   231   end;
   232 
   233 fun register_subclass (sub, sup) some_dep_morph some_wit export thy =
   234   let
   235     val intros = (snd o rules thy) sup :: map_filter I
   236       [Option.map (Drule.standard' o Element.conclude_witness) some_wit,
   237         (fst o rules thy) sub];
   238     val tac = EVERY (map (TRYALL o Tactic.rtac) intros);
   239     val classrel = Skip_Proof.prove_global thy [] [] (Logic.mk_classrel (sub, sup))
   240       (K tac);
   241     val diff_sort = Sign.complete_sort thy [sup]
   242       |> subtract (op =) (Sign.complete_sort thy [sub])
   243       |> filter (is_class thy);
   244     val add_dependency = case some_dep_morph
   245      of SOME dep_morph => Locale.add_dependency sub
   246           (sup, dep_morph $> Element.satisfy_morphism (the_list some_wit)) export
   247       | NONE => I
   248   in
   249     thy
   250     |> AxClass.add_classrel classrel
   251     |> ClassData.map (Graph.add_edge (sub, sup))
   252     |> activate_defs sub (these_defs thy diff_sort)
   253     |> add_dependency
   254   end;
   255 
   256 
   257 (** classes and class target **)
   258 
   259 (* class context syntax *)
   260 
   261 fun these_unchecks thy =
   262   map (fn (c, (_, (ty, t))) => (t, Const (c, ty))) o these_operations thy;
   263 
   264 fun redeclare_const thy c =
   265   let val b = Long_Name.base_name c
   266   in Sign.intern_const thy b = c ? Variable.declare_const (b, c) end;
   267 
   268 fun synchronize_class_syntax sort base_sort ctxt =
   269   let
   270     val thy = ProofContext.theory_of ctxt;
   271     val algebra = Sign.classes_of thy;
   272     val operations = these_operations thy sort;
   273     fun subst_class_typ sort = map_type_tfree (K (TVar ((Name.aT, 0), sort)));
   274     val primary_constraints =
   275       (map o apsnd) (subst_class_typ base_sort o fst o snd) operations;
   276     val secondary_constraints =
   277       (map o apsnd) (fn (class, (ty, _)) => subst_class_typ [class] ty) operations;
   278     fun improve (c, ty) = (case AList.lookup (op =) primary_constraints c
   279      of SOME ty' => (case try (Type.raw_match (ty', ty)) Vartab.empty
   280          of SOME tyenv => (case Vartab.lookup tyenv (Name.aT, 0)
   281              of SOME (_, ty' as TVar (tvar as (vi, sort))) =>
   282                   if TypeInfer.is_param vi
   283                     andalso Sorts.sort_le algebra (base_sort, sort)
   284                       then SOME (ty', TFree (Name.aT, base_sort))
   285                       else NONE
   286               | _ => NONE)
   287           | NONE => NONE)
   288       | NONE => NONE)
   289     fun subst (c, ty) = Option.map snd (AList.lookup (op =) operations c);
   290     val unchecks = these_unchecks thy sort;
   291   in
   292     ctxt
   293     |> fold (redeclare_const thy o fst) primary_constraints
   294     |> Overloading.map_improvable_syntax (K (((primary_constraints, secondary_constraints),
   295         (((improve, subst), true), unchecks)), false))
   296     |> Overloading.set_primary_constraints
   297   end;
   298 
   299 fun refresh_syntax class ctxt =
   300   let
   301     val thy = ProofContext.theory_of ctxt;
   302     val base_sort = base_sort thy class;
   303   in synchronize_class_syntax [class] base_sort ctxt end;
   304 
   305 fun redeclare_operations thy sort =
   306   fold (redeclare_const thy o fst) (these_operations thy sort);
   307 
   308 fun begin sort base_sort ctxt =
   309   ctxt
   310   |> Variable.declare_term
   311       (Logic.mk_type (TFree (Name.aT, base_sort)))
   312   |> synchronize_class_syntax sort base_sort
   313   |> Overloading.add_improvable_syntax;
   314 
   315 fun init class thy =
   316   thy
   317   |> Locale.init class
   318   |> begin [class] (base_sort thy class);
   319 
   320 
   321 (* class target *)
   322 
   323 val class_prefix = Logic.const_of_class o Long_Name.base_name;
   324 
   325 fun declare class ((c, mx), dict) thy =
   326   let
   327     val morph = morphism thy class;
   328     val b = Morphism.binding morph c;
   329     val b_def = Morphism.binding morph (Binding.suffix_name "_dict" b);
   330     val c' = Sign.full_name thy b;
   331     val dict' = Morphism.term morph dict;
   332     val ty' = Term.fastype_of dict';
   333     val def_eq = Logic.mk_equals (Const (c', ty'), dict')
   334       |> map_types Type.strip_sorts;
   335   in
   336     thy
   337     |> Sign.declare_const ((b, Type.strip_sorts ty'), mx)
   338     |> snd
   339     |> Thm.add_def false false (b_def, def_eq)
   340     |>> Thm.varifyT
   341     |-> (fn def_thm => PureThy.store_thm (b_def, def_thm)
   342       #> snd
   343       #> register_operation class (c', (dict', SOME (Thm.symmetric def_thm))))
   344     |> Sign.add_const_constraint (c', SOME ty')
   345   end;
   346 
   347 fun abbrev class prmode ((c, mx), rhs) thy =
   348   let
   349     val morph = morphism thy class;
   350     val unchecks = these_unchecks thy [class];
   351     val b = Morphism.binding morph c;
   352     val c' = Sign.full_name thy b;
   353     val rhs' = Pattern.rewrite_term thy unchecks [] rhs;
   354     val ty' = Term.fastype_of rhs';
   355     val rhs'' = map_types Logic.varifyT rhs';
   356   in
   357     thy
   358     |> Sign.add_abbrev (#1 prmode) (b, rhs'')
   359     |> snd
   360     |> Sign.add_const_constraint (c', SOME ty')
   361     |> Sign.notation true prmode [(Const (c', ty'), mx)]
   362     |> not (#1 prmode = PrintMode.input) ? register_operation class (c', (rhs', NONE))
   363   end;
   364 
   365 
   366 (* simple subclasses *)
   367 
   368 local
   369 
   370 fun gen_classrel mk_prop classrel thy =
   371   let
   372     fun after_qed results =
   373       ProofContext.theory ((fold o fold) AxClass.add_classrel results);
   374   in
   375     thy
   376     |> ProofContext.init
   377     |> Proof.theorem_i NONE after_qed [[(mk_prop thy classrel, [])]]
   378   end;
   379 
   380 in
   381 
   382 val classrel =
   383   gen_classrel (Logic.mk_classrel oo AxClass.cert_classrel);
   384 val classrel_cmd =
   385   gen_classrel (Logic.mk_classrel oo AxClass.read_classrel);
   386 
   387 end; (*local*)
   388 
   389 
   390 (** instantiation target **)
   391 
   392 (* bookkeeping *)
   393 
   394 datatype instantiation = Instantiation of {
   395   arities: string list * (string * sort) list * sort,
   396   params: ((string * string) * (string * typ)) list
   397     (*(instantiation parameter, type constructor), (local instantiation parameter, typ)*)
   398 }
   399 
   400 structure Instantiation = Proof_Data
   401 (
   402   type T = instantiation
   403   fun init _ = Instantiation { arities = ([], [], []), params = [] };
   404 );
   405 
   406 fun mk_instantiation (arities, params) =
   407   Instantiation { arities = arities, params = params };
   408 fun get_instantiation lthy = case Instantiation.get (Local_Theory.target_of lthy)
   409  of Instantiation data => data;
   410 fun map_instantiation f = (Local_Theory.target o Instantiation.map)
   411   (fn Instantiation { arities, params } => mk_instantiation (f (arities, params)));
   412 
   413 fun the_instantiation lthy = case get_instantiation lthy
   414  of { arities = ([], [], []), ... } => error "No instantiation target"
   415   | data => data;
   416 
   417 val instantiation_params = #params o get_instantiation;
   418 
   419 fun instantiation_param lthy b = instantiation_params lthy
   420   |> find_first (fn (_, (v, _)) => Binding.name_of b = v)
   421   |> Option.map (fst o fst);
   422 
   423 fun read_multi_arity thy (raw_tycos, raw_sorts, raw_sort) =
   424   let
   425     val all_arities = map (fn raw_tyco => Sign.read_arity thy
   426       (raw_tyco, raw_sorts, raw_sort)) raw_tycos;
   427     val tycos = map #1 all_arities;
   428     val (_, sorts, sort) = hd all_arities;
   429     val vs = Name.names Name.context Name.aT sorts;
   430   in (tycos, vs, sort) end;
   431 
   432 
   433 (* syntax *)
   434 
   435 fun synchronize_inst_syntax ctxt =
   436   let
   437     val Instantiation { arities = (_, _, sort), params = params } = Instantiation.get ctxt;
   438 
   439     val lookup_inst_param = AxClass.lookup_inst_param (Sign.consts_of (ProofContext.theory_of ctxt)) params;
   440     fun subst (c, ty) = case lookup_inst_param (c, ty)
   441      of SOME (v_ty as (_, ty)) => SOME (ty, Free v_ty)
   442       | NONE => NONE;
   443     val unchecks =
   444       map (fn ((c, _), v_ty as (_, ty)) => (Free v_ty, Const (c, ty))) params;
   445   in
   446     ctxt
   447     |> Overloading.map_improvable_syntax
   448          (fn (((primary_constraints, _), (((improve, _), _), _)), _) =>
   449             (((primary_constraints, []), (((improve, subst), false), unchecks)), false))
   450   end;
   451 
   452 
   453 (* target *)
   454 
   455 val sanatize_name = (*FIXME*)
   456   let
   457     fun is_valid s = Symbol.is_ascii_letter s orelse Symbol.is_ascii_digit s
   458       orelse s = "'" orelse s = "_";
   459     val is_junk = not o is_valid andf Symbol.is_regular;
   460     val junk = Scan.many is_junk;
   461     val scan_valids = Symbol.scanner "Malformed input"
   462       ((junk |--
   463         (Scan.optional (Scan.one Symbol.is_ascii_letter) "x" ^^ (Scan.many is_valid >> implode)
   464         --| junk))
   465       ::: Scan.repeat ((Scan.many1 is_valid >> implode) --| junk));
   466   in
   467     explode #> scan_valids #> implode
   468   end;
   469 
   470 fun type_name "*" = "prod"
   471   | type_name "+" = "sum"
   472   | type_name s = sanatize_name (Long_Name.base_name s);
   473 
   474 fun resort_terms pp algebra consts constraints ts =
   475   let
   476     fun matchings (Const (c_ty as (c, _))) = (case constraints c
   477          of NONE => I
   478           | SOME sorts => fold2 (curry (Sorts.meet_sort algebra))
   479               (Consts.typargs consts c_ty) sorts)
   480       | matchings _ = I
   481     val tvartab = (fold o fold_aterms) matchings ts Vartab.empty
   482       handle Sorts.CLASS_ERROR e => error (Sorts.class_error pp e);
   483     val inst = map_type_tvar
   484       (fn (vi, sort) => TVar (vi, the_default sort (Vartab.lookup tvartab vi)));
   485   in if Vartab.is_empty tvartab then NONE else SOME ((map o map_types) inst ts) end;
   486 
   487 fun init_instantiation (tycos, vs, sort) thy =
   488   let
   489     val _ = if null tycos then error "At least one arity must be given" else ();
   490     val class_params = these_params thy (filter (can (AxClass.get_info thy)) sort);
   491     fun get_param tyco (param, (_, (c, ty))) =
   492       if can (AxClass.param_of_inst thy) (c, tyco)
   493       then NONE else SOME ((c, tyco),
   494         (param ^ "_" ^ type_name tyco, map_atyps (K (Type (tyco, map TFree vs))) ty));
   495     val params = map_product get_param tycos class_params |> map_filter I;
   496     val primary_constraints = map (apsnd
   497       (map_atyps (K (TVar ((Name.aT, 0), [])))) o snd o snd) class_params;
   498     val pp = Syntax.pp_global thy;
   499     val algebra = Sign.classes_of thy
   500       |> fold (fn tyco => Sorts.add_arities pp
   501             (tyco, map (fn class => (class, map snd vs)) sort)) tycos;
   502     val consts = Sign.consts_of thy;
   503     val improve_constraints = AList.lookup (op =)
   504       (map (fn (_, (class, (c, _))) => (c, [[class]])) class_params);
   505     fun resort_check ts ctxt = case resort_terms (Syntax.pp ctxt) algebra consts improve_constraints ts
   506      of NONE => NONE
   507       | SOME ts' => SOME (ts', ctxt);
   508     val lookup_inst_param = AxClass.lookup_inst_param consts params;
   509     val typ_instance = Type.typ_instance (Sign.tsig_of thy);
   510     fun improve (c, ty) = case lookup_inst_param (c, ty)
   511      of SOME (_, ty') => if typ_instance (ty', ty) then SOME (ty, ty') else NONE
   512       | NONE => NONE;
   513   in
   514     thy
   515     |> Theory.checkpoint
   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   #> Local_Theory.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       Local_Theory.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   #> Local_Theory.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