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