src/Pure/Isar/class.ML
author haftmann
Wed Nov 28 09:01:42 2007 +0100 (2007-11-28)
changeset 25485 33840a854e63
parent 25462 dad0291cb76a
child 25502 9200b36280c0
permissions -rw-r--r--
tuned interfaces of class module
haftmann@24218
     1
(*  Title:      Pure/Isar/class.ML
haftmann@24218
     2
    ID:         $Id$
haftmann@24218
     3
    Author:     Florian Haftmann, TU Muenchen
haftmann@24218
     4
haftmann@24218
     5
Type classes derived from primitive axclasses and locales.
haftmann@24218
     6
*)
haftmann@24218
     7
haftmann@24218
     8
signature CLASS =
haftmann@24218
     9
sig
haftmann@25462
    10
  (*classes*)
haftmann@25002
    11
  val class: bstring -> class list -> Element.context_i Locale.element list
haftmann@24218
    12
    -> string list -> theory -> string * Proof.context
haftmann@25002
    13
  val class_cmd: bstring -> xstring list -> Element.context Locale.element list
haftmann@24589
    14
    -> xstring list -> theory -> string * Proof.context
haftmann@25485
    15
haftmann@25311
    16
  val init: class -> theory -> Proof.context
haftmann@25485
    17
  val logical_const: string -> Markup.property list
wenzelm@25104
    18
    -> (string * mixfix) * term -> theory -> theory
haftmann@25485
    19
  val syntactic_const: string -> Syntax.mode -> Markup.property list
wenzelm@25104
    20
    -> (string * mixfix) * term -> theory -> theory
haftmann@25083
    21
  val refresh_syntax: class -> Proof.context -> Proof.context
haftmann@25485
    22
haftmann@24589
    23
  val intro_classes_tac: thm list -> tactic
haftmann@24589
    24
  val default_intro_classes_tac: thm list -> tactic
haftmann@25195
    25
  val prove_subclass: class * class -> thm list -> Proof.context
haftmann@25195
    26
    -> theory -> theory
haftmann@25485
    27
haftmann@25485
    28
  val class_prefix: string -> string
haftmann@25485
    29
  val is_class: theory -> class -> bool
haftmann@25485
    30
  val these_params: theory -> sort -> (string * (string * typ)) list
haftmann@24589
    31
  val print_classes: theory -> unit
haftmann@24423
    32
haftmann@25462
    33
  (*instances*)
haftmann@25485
    34
  val init_instantiation: arity list -> theory -> local_theory
haftmann@25485
    35
  val instantiation_instance: (local_theory -> local_theory) -> local_theory -> Proof.state
haftmann@25485
    36
  val prove_instantiation_instance: (Proof.context -> tactic) -> local_theory -> local_theory
haftmann@25485
    37
  val conclude_instantiation: local_theory -> local_theory
haftmann@25485
    38
haftmann@25485
    39
  val overloaded_const: string * typ * mixfix -> theory -> term * theory
haftmann@25485
    40
  val overloaded_def: string -> string * term -> theory -> thm * theory
haftmann@25485
    41
  val instantiation_param: Proof.context -> string -> string option
haftmann@25485
    42
  val confirm_declaration: string -> local_theory -> local_theory
haftmann@25485
    43
haftmann@25485
    44
  val unoverload: theory -> thm -> thm
haftmann@25485
    45
  val overload: theory -> thm -> thm
haftmann@25485
    46
  val unoverload_conv: theory -> conv
haftmann@25485
    47
  val overload_conv: theory -> conv
haftmann@25462
    48
  val unoverload_const: theory -> string * typ -> string
haftmann@25485
    49
  val param_of_inst: theory -> string * string -> string
haftmann@25485
    50
  val inst_of_param: theory -> string -> (string * string) option
haftmann@25462
    51
haftmann@25462
    52
  (*old axclass layer*)
haftmann@25462
    53
  val axclass_cmd: bstring * xstring list
haftmann@25462
    54
    -> ((bstring * Attrib.src list) * string list) list
haftmann@25462
    55
    -> theory -> class * theory
haftmann@25462
    56
  val classrel_cmd: xstring * xstring -> theory -> Proof.state
haftmann@25462
    57
haftmann@25462
    58
  (*old instance layer*)
haftmann@24589
    59
  val instance_arity: (theory -> theory) -> arity list -> theory -> Proof.state
haftmann@24589
    60
  val instance: arity list -> ((bstring * Attrib.src list) * term) list
haftmann@24423
    61
    -> (thm list -> theory -> theory)
haftmann@24218
    62
    -> theory -> Proof.state
haftmann@24589
    63
  val instance_cmd: (bstring * xstring list * xstring) list
haftmann@24589
    64
    -> ((bstring * Attrib.src list) * xstring) list
haftmann@24423
    65
    -> (thm list -> theory -> theory)
haftmann@24218
    66
    -> theory -> Proof.state
haftmann@24589
    67
  val prove_instance: tactic -> arity list
haftmann@24218
    68
    -> ((bstring * Attrib.src list) * term) list
haftmann@24423
    69
    -> theory -> thm list * theory
haftmann@24218
    70
end;
haftmann@24218
    71
haftmann@24218
    72
structure Class : CLASS =
haftmann@24218
    73
struct
haftmann@24218
    74
haftmann@24218
    75
(** auxiliary **)
haftmann@24218
    76
haftmann@25062
    77
val classN = "class";
haftmann@25062
    78
val introN = "intro";
haftmann@25062
    79
haftmann@25002
    80
fun prove_interpretation tac prfx_atts expr inst =
haftmann@25002
    81
  Locale.interpretation_i I prfx_atts expr inst
haftmann@24589
    82
  #> Proof.global_terminal_proof
haftmann@24589
    83
      (Method.Basic (K (Method.SIMPLE_METHOD tac), Position.none), NONE)
haftmann@24589
    84
  #> ProofContext.theory_of;
haftmann@24589
    85
haftmann@25195
    86
fun prove_interpretation_in tac after_qed (name, expr) =
haftmann@25195
    87
  Locale.interpretation_in_locale
haftmann@25195
    88
      (ProofContext.theory after_qed) (name, expr)
haftmann@25195
    89
  #> Proof.global_terminal_proof
haftmann@25195
    90
      (Method.Basic (K (Method.SIMPLE_METHOD tac), Position.none), NONE)
haftmann@25195
    91
  #> ProofContext.theory_of;
haftmann@25195
    92
wenzelm@25020
    93
fun OF_LAST thm1 thm2 = thm1 RSN (Thm.nprems_of thm2, thm2);
haftmann@24589
    94
haftmann@24589
    95
fun strip_all_ofclass thy sort =
haftmann@24589
    96
  let
wenzelm@24847
    97
    val typ = TVar ((Name.aT, 0), sort);
haftmann@24589
    98
    fun prem_inclass t =
haftmann@24589
    99
      case Logic.strip_imp_prems t
haftmann@24589
   100
       of ofcls :: _ => try Logic.dest_inclass ofcls
haftmann@24589
   101
        | [] => NONE;
haftmann@24589
   102
    fun strip_ofclass class thm =
haftmann@24589
   103
      thm OF (fst o AxClass.of_sort thy (typ, [class])) AxClass.cache;
haftmann@24589
   104
    fun strip thm = case (prem_inclass o Thm.prop_of) thm
haftmann@24589
   105
     of SOME (_, class) => thm |> strip_ofclass class |> strip
haftmann@24589
   106
      | NONE => thm;
haftmann@24589
   107
  in strip end;
haftmann@24589
   108
haftmann@25038
   109
fun get_remove_global_constraint c thy =
haftmann@25038
   110
  let
haftmann@25038
   111
    val ty = Sign.the_const_constraint thy c;
haftmann@25038
   112
  in
haftmann@25038
   113
    thy
haftmann@25038
   114
    |> Sign.add_const_constraint (c, NONE)
haftmann@25038
   115
    |> pair (c, Logic.unvarifyT ty)
haftmann@25038
   116
  end;
haftmann@25038
   117
haftmann@24589
   118
haftmann@25485
   119
(** primitive axclass and instance commands **)
haftmann@24589
   120
haftmann@24218
   121
fun axclass_cmd (class, raw_superclasses) raw_specs thy =
haftmann@24218
   122
  let
haftmann@24218
   123
    val ctxt = ProofContext.init thy;
haftmann@24218
   124
    val superclasses = map (Sign.read_class thy) raw_superclasses;
haftmann@24589
   125
    val name_atts = map ((apsnd o map) (Attrib.attribute thy) o fst)
haftmann@24589
   126
      raw_specs;
haftmann@24589
   127
    val axiomss = ProofContext.read_propp (ctxt, map (map (rpair []) o snd)
haftmann@24589
   128
          raw_specs)
haftmann@24218
   129
      |> snd
haftmann@24218
   130
      |> (map o map) fst;
haftmann@24589
   131
  in
haftmann@24589
   132
    AxClass.define_class (class, superclasses) []
haftmann@24589
   133
      (name_atts ~~ axiomss) thy
haftmann@24589
   134
  end;
haftmann@24218
   135
haftmann@24218
   136
local
haftmann@24218
   137
haftmann@24218
   138
fun gen_instance mk_prop add_thm after_qed insts thy =
haftmann@24218
   139
  let
haftmann@24218
   140
    fun after_qed' results =
haftmann@24218
   141
      ProofContext.theory ((fold o fold) add_thm results #> after_qed);
haftmann@24218
   142
  in
haftmann@24218
   143
    thy
haftmann@24218
   144
    |> ProofContext.init
haftmann@24589
   145
    |> Proof.theorem_i NONE after_qed' ((map (fn t => [(t, [])])
haftmann@24589
   146
        o maps (mk_prop thy)) insts)
haftmann@24218
   147
  end;
haftmann@24218
   148
haftmann@24218
   149
in
haftmann@24218
   150
haftmann@24589
   151
val instance_arity =
haftmann@24218
   152
  gen_instance (Logic.mk_arities oo Sign.cert_arity) AxClass.add_arity;
haftmann@24589
   153
val classrel =
haftmann@24218
   154
  gen_instance (single oo (Logic.mk_classrel oo AxClass.cert_classrel))
haftmann@24218
   155
    AxClass.add_classrel I o single;
haftmann@24589
   156
val classrel_cmd =
haftmann@24589
   157
  gen_instance (single oo (Logic.mk_classrel oo AxClass.read_classrel))
haftmann@24589
   158
    AxClass.add_classrel I o single;
haftmann@24218
   159
haftmann@24218
   160
end; (*local*)
haftmann@24218
   161
haftmann@24218
   162
haftmann@25462
   163
(** basic overloading **)
haftmann@25462
   164
haftmann@25462
   165
(* bookkeeping *)
haftmann@24304
   166
haftmann@24304
   167
structure InstData = TheoryDataFun
haftmann@24304
   168
(
haftmann@24423
   169
  type T = (string * thm) Symtab.table Symtab.table * (string * string) Symtab.table;
haftmann@24423
   170
    (*constant name ~> type constructor ~> (constant name, equation),
haftmann@24423
   171
        constant name ~> (constant name, type constructor)*)
haftmann@24423
   172
  val empty = (Symtab.empty, Symtab.empty);
haftmann@24304
   173
  val copy = I;
haftmann@24304
   174
  val extend = I;
haftmann@24423
   175
  fun merge _ ((taba1, tabb1), (taba2, tabb2)) =
haftmann@24423
   176
    (Symtab.join (K (Symtab.merge (K true))) (taba1, taba2),
haftmann@24423
   177
      Symtab.merge (K true) (tabb1, tabb2));
haftmann@24304
   178
);
haftmann@24304
   179
haftmann@25462
   180
val inst_tyco = Option.map fst o try (dest_Type o the_single) oo Sign.const_typargs;
haftmann@25462
   181
haftmann@25462
   182
fun inst thy (c, tyco) =
haftmann@25462
   183
  (the o Symtab.lookup ((the o Symtab.lookup (fst (InstData.get thy))) c)) tyco;
haftmann@25462
   184
haftmann@25485
   185
val param_of_inst = fst oo inst;
haftmann@25462
   186
wenzelm@25020
   187
fun inst_thms thy = (Symtab.fold (Symtab.fold (cons o snd o snd) o snd) o fst)
haftmann@25462
   188
  (InstData.get thy) [];
haftmann@25462
   189
haftmann@25485
   190
val inst_of_param = Symtab.lookup o snd o InstData.get;
haftmann@25462
   191
haftmann@24589
   192
fun add_inst (c, tyco) inst = (InstData.map o apfst
haftmann@24589
   193
      o Symtab.map_default (c, Symtab.empty)) (Symtab.update_new (tyco, inst))
haftmann@24423
   194
  #> (InstData.map o apsnd) (Symtab.update_new (fst inst, (c, tyco)));
haftmann@24304
   195
haftmann@25485
   196
fun unoverload thy = MetaSimplifier.simplify true (inst_thms thy);
haftmann@25485
   197
fun overload thy = MetaSimplifier.simplify true (map Thm.symmetric (inst_thms thy));
haftmann@25485
   198
haftmann@25485
   199
fun unoverload_conv thy = MetaSimplifier.rewrite true (inst_thms thy);
haftmann@25485
   200
fun overload_conv thy = MetaSimplifier.rewrite true (map Thm.symmetric (inst_thms thy));
haftmann@24304
   201
haftmann@24423
   202
fun unoverload_const thy (c_ty as (c, _)) =
haftmann@24423
   203
  case AxClass.class_of_param thy c
haftmann@25462
   204
   of SOME class => (case inst_tyco thy c_ty
haftmann@25485
   205
       of SOME tyco => try (param_of_inst thy) (c, tyco) |> the_default c
haftmann@25462
   206
        | NONE => c)
haftmann@24423
   207
    | NONE => c;
haftmann@24423
   208
haftmann@25462
   209
haftmann@25462
   210
(* declaration and definition of instances of overloaded constants *)
haftmann@25462
   211
haftmann@25462
   212
fun primitive_note kind (name, thm) =
haftmann@25462
   213
  PureThy.note_thmss_i kind [((name, []), [([thm], [])])]
haftmann@25462
   214
  #>> (fn [(_, [thm])] => thm);
haftmann@25462
   215
haftmann@25485
   216
fun overloaded_const (c, ty, mx) thy =
haftmann@25462
   217
  let
haftmann@25485
   218
    val _ = if mx <> NoSyn then
haftmann@25485
   219
      error ("Illegal mixfix syntax for constant to be instantiated " ^ quote c)
haftmann@25485
   220
      else ()
haftmann@25462
   221
    val SOME class = AxClass.class_of_param thy c;
haftmann@25462
   222
    val SOME tyco = inst_tyco thy (c, ty);
haftmann@25485
   223
    val name_inst = AxClass.instance_name (tyco, class) ^ "_inst";
haftmann@25462
   224
    val c' = NameSpace.base c;
haftmann@25462
   225
    val ty' = Type.strip_sorts ty;
haftmann@25462
   226
  in
haftmann@25462
   227
    thy
haftmann@25462
   228
    |> Sign.sticky_prefix name_inst
haftmann@25462
   229
    |> Sign.no_base_names
haftmann@25462
   230
    |> Sign.declare_const [] (c', ty', NoSyn)
haftmann@25462
   231
    |-> (fn const' as Const (c'', _) => Thm.add_def true
haftmann@25462
   232
          (Thm.def_name c', Logic.mk_equals (Const (c, ty'), const'))
haftmann@25462
   233
    #>> Thm.varifyT
haftmann@25462
   234
    #-> (fn thm => add_inst (c, tyco) (c'', thm)
haftmann@25462
   235
    #> primitive_note Thm.internalK (c', thm)
haftmann@25462
   236
    #> snd
haftmann@25462
   237
    #> Sign.restore_naming thy
haftmann@25462
   238
    #> pair (Const (c, ty))))
haftmann@25462
   239
  end;
haftmann@25462
   240
haftmann@25485
   241
fun overloaded_def name (c, t) thy =
haftmann@25462
   242
  let
haftmann@25462
   243
    val ty = Term.fastype_of t;
haftmann@25462
   244
    val SOME tyco = inst_tyco thy (c, ty);
haftmann@25462
   245
    val (c', eq) = inst thy (c, tyco);
haftmann@25462
   246
    val prop = Logic.mk_equals (Const (c', ty), t);
haftmann@25485
   247
    val name' = Thm.def_name_optional
haftmann@25485
   248
      (NameSpace.base c ^ "_" ^ NameSpace.base tyco) name;
haftmann@25462
   249
  in
haftmann@25462
   250
    thy
haftmann@25462
   251
    |> Thm.add_def false (name', prop)
haftmann@25485
   252
    |>> (fn thm =>  Drule.transitive_thm OF [eq, thm])
haftmann@25462
   253
  end;
haftmann@25462
   254
haftmann@25462
   255
haftmann@25462
   256
(* legacy *)
haftmann@24304
   257
haftmann@24304
   258
fun add_inst_def (class, tyco) (c, ty) thy =
haftmann@24304
   259
  let
wenzelm@25024
   260
    val tyco_base = Sign.base_name tyco;
wenzelm@25024
   261
    val name_inst = Sign.base_name class ^ "_" ^ tyco_base ^ "_inst";
wenzelm@25024
   262
    val c_inst_base = Sign.base_name c ^ "_" ^ tyco_base;
haftmann@24304
   263
  in
haftmann@24304
   264
    thy
haftmann@24304
   265
    |> Sign.sticky_prefix name_inst
wenzelm@25020
   266
    |> Sign.declare_const [] (c_inst_base, ty, NoSyn)
wenzelm@24968
   267
    |-> (fn const as Const (c_inst, _) =>
wenzelm@24968
   268
      PureThy.add_defs_i false
wenzelm@24968
   269
        [((Thm.def_name c_inst_base, Logic.mk_equals (const, Const (c, ty))), [])]
wenzelm@24968
   270
      #-> (fn [thm] => add_inst (c, tyco) (c_inst, Thm.symmetric thm)))
haftmann@24435
   271
    |> Sign.restore_naming thy
haftmann@24304
   272
  end;
haftmann@24304
   273
haftmann@24304
   274
fun add_inst_def' (class, tyco) (c, ty) thy =
haftmann@24423
   275
  if case Symtab.lookup (fst (InstData.get thy)) c
haftmann@24304
   276
   of NONE => true
haftmann@24304
   277
    | SOME tab => is_none (Symtab.lookup tab tyco)
haftmann@24304
   278
  then add_inst_def (class, tyco) (c, Logic.unvarifyT ty) thy
haftmann@24304
   279
  else thy;
haftmann@24304
   280
haftmann@24304
   281
fun add_def ((class, tyco), ((name, prop), atts)) thy =
haftmann@24304
   282
  let
haftmann@24589
   283
    val ((lhs as Const (c, ty), args), rhs) =
haftmann@24589
   284
      (apfst Term.strip_comb o Logic.dest_equals) prop;
haftmann@24304
   285
  in
haftmann@24304
   286
    thy
wenzelm@25020
   287
    |> PureThy.add_defs_i true [((name, prop), map (Attrib.attribute_i thy) atts)]
haftmann@25462
   288
    |-> (fn [def] => add_inst_def (class, tyco) (c, ty) #> pair def)
haftmann@24304
   289
  end;
haftmann@24304
   290
haftmann@24304
   291
haftmann@24589
   292
(** instances with implicit parameter handling **)
haftmann@24218
   293
haftmann@24218
   294
local
haftmann@24218
   295
wenzelm@24707
   296
fun gen_read_def thy prep_att parse_prop ((raw_name, raw_atts), raw_t) =
haftmann@24218
   297
  let
wenzelm@24707
   298
    val ctxt = ProofContext.init thy;
wenzelm@24707
   299
    val t = parse_prop ctxt raw_t |> Syntax.check_prop ctxt;
wenzelm@24981
   300
    val ((c, ty), _) = Sign.cert_def ctxt t;
haftmann@24218
   301
    val atts = map (prep_att thy) raw_atts;
haftmann@24218
   302
    val insts = Consts.typargs (Sign.consts_of thy) (c, ty);
haftmann@24218
   303
    val name = case raw_name
haftmann@24218
   304
     of "" => NONE
haftmann@24218
   305
      | _ => SOME raw_name;
haftmann@24218
   306
  in (c, (insts, ((name, t), atts))) end;
haftmann@24218
   307
wenzelm@24707
   308
fun read_def_cmd thy = gen_read_def thy Attrib.intern_src Syntax.parse_prop;
haftmann@24218
   309
fun read_def thy = gen_read_def thy (K I) (K I);
haftmann@24218
   310
haftmann@24589
   311
fun gen_instance prep_arity read_def do_proof raw_arities raw_defs after_qed theory =
haftmann@24218
   312
  let
haftmann@24218
   313
    val arities = map (prep_arity theory) raw_arities;
wenzelm@25020
   314
    val _ = if null arities then error "At least one arity must be given" else ();
haftmann@24218
   315
    val _ = case (duplicates (op =) o map #1) arities
haftmann@24218
   316
     of [] => ()
wenzelm@25020
   317
      | dupl_tycos => error ("Type constructors occur more than once in arities: "
wenzelm@25020
   318
          ^ commas_quote dupl_tycos);
haftmann@24218
   319
    fun get_consts_class tyco ty class =
haftmann@24218
   320
      let
wenzelm@24968
   321
        val cs = (these o try (#params o AxClass.get_info theory)) class;
haftmann@24218
   322
        val subst_ty = map_type_tfree (K ty);
haftmann@24218
   323
      in
haftmann@24304
   324
        map (fn (c, ty) => (c, ((class, tyco), subst_ty ty))) cs
haftmann@24218
   325
      end;
haftmann@24218
   326
    fun get_consts_sort (tyco, asorts, sort) =
haftmann@24218
   327
      let
haftmann@24589
   328
        val ty = Type (tyco, map (fn (v, sort) => TVar ((v, 0), sort))
wenzelm@24847
   329
          (Name.names Name.context Name.aT asorts))
wenzelm@24731
   330
      in maps (get_consts_class tyco ty) (Sign.complete_sort theory sort) end;
haftmann@24218
   331
    val cs = maps get_consts_sort arities;
haftmann@24218
   332
    fun mk_typnorm thy (ty, ty_sc) =
haftmann@24218
   333
      case try (Sign.typ_match thy (Logic.varifyT ty_sc, ty)) Vartab.empty
haftmann@24218
   334
       of SOME env => SOME (Logic.varifyT #> Envir.typ_subst_TVars env #> Logic.unvarifyT)
haftmann@24218
   335
        | NONE => NONE;
haftmann@24218
   336
    fun read_defs defs cs thy_read =
haftmann@24218
   337
      let
haftmann@24218
   338
        fun read raw_def cs =
haftmann@24218
   339
          let
haftmann@24218
   340
            val (c, (inst, ((name_opt, t), atts))) = read_def thy_read raw_def;
haftmann@24218
   341
            val ty = Consts.instance (Sign.consts_of thy_read) (c, inst);
haftmann@24304
   342
            val ((class, tyco), ty') = case AList.lookup (op =) cs c
wenzelm@25020
   343
             of NONE => error ("Illegal definition for constant " ^ quote c)
haftmann@24218
   344
              | SOME class_ty => class_ty;
haftmann@24218
   345
            val name = case name_opt
haftmann@24218
   346
             of NONE => Thm.def_name (Logic.name_arity (tyco, [], c))
haftmann@24218
   347
              | SOME name => name;
haftmann@24218
   348
            val t' = case mk_typnorm thy_read (ty', ty)
wenzelm@25020
   349
             of NONE => error ("Illegal definition for constant " ^
haftmann@24218
   350
                  quote (c ^ "::" ^ setmp show_sorts true
haftmann@24218
   351
                    (Sign.string_of_typ thy_read) ty))
haftmann@24218
   352
              | SOME norm => map_types norm t
haftmann@24218
   353
          in (((class, tyco), ((name, t'), atts)), AList.delete (op =) c cs) end;
haftmann@24218
   354
      in fold_map read defs cs end;
haftmann@24304
   355
    val (defs, other_cs) = read_defs raw_defs cs
haftmann@24218
   356
      (fold Sign.primitive_arity arities (Theory.copy theory));
haftmann@24423
   357
    fun after_qed' cs defs =
wenzelm@24766
   358
      fold Sign.add_const_constraint (map (apsnd SOME) cs)
haftmann@24423
   359
      #> after_qed defs;
haftmann@24218
   360
  in
haftmann@24218
   361
    theory
haftmann@25038
   362
    |> fold_map get_remove_global_constraint (map fst cs |> distinct (op =))
haftmann@24304
   363
    ||>> fold_map add_def defs
haftmann@24304
   364
    ||> fold (fn (c, ((class, tyco), ty)) => add_inst_def' (class, tyco) (c, ty)) other_cs
haftmann@24423
   365
    |-> (fn (cs, defs) => do_proof (after_qed' cs defs) arities defs)
haftmann@24218
   366
  end;
haftmann@24218
   367
haftmann@24423
   368
fun tactic_proof tac f arities defs =
haftmann@24218
   369
  fold (fn arity => AxClass.prove_arity arity tac) arities
haftmann@24423
   370
  #> f
haftmann@24423
   371
  #> pair defs;
haftmann@24218
   372
haftmann@24218
   373
in
haftmann@24218
   374
haftmann@24589
   375
val instance = gen_instance Sign.cert_arity read_def
haftmann@24589
   376
  (fn f => fn arities => fn defs => instance_arity f arities);
haftmann@24589
   377
val instance_cmd = gen_instance Sign.read_arity read_def_cmd
haftmann@24589
   378
  (fn f => fn arities => fn defs => instance_arity f arities);
haftmann@24589
   379
fun prove_instance tac arities defs =
haftmann@24589
   380
  gen_instance Sign.cert_arity read_def
haftmann@24589
   381
    (tactic_proof tac) arities defs (K I);
haftmann@24218
   382
haftmann@24218
   383
end; (*local*)
haftmann@24218
   384
haftmann@24218
   385
haftmann@24218
   386
haftmann@24589
   387
(** class data **)
haftmann@24218
   388
haftmann@24218
   389
datatype class_data = ClassData of {
haftmann@24218
   390
  consts: (string * string) list
haftmann@24836
   391
    (*locale parameter ~> constant name*),
haftmann@25062
   392
  base_sort: sort,
haftmann@25083
   393
  inst: term option list
haftmann@25083
   394
    (*canonical interpretation*),
haftmann@25062
   395
  morphism: morphism,
haftmann@25062
   396
    (*partial morphism of canonical interpretation*)
haftmann@24657
   397
  intro: thm,
haftmann@24657
   398
  defs: thm list,
haftmann@25368
   399
  operations: (string * (class * (typ * term))) list
haftmann@24657
   400
};
haftmann@24218
   401
haftmann@24657
   402
fun rep_class_data (ClassData d) = d;
haftmann@25062
   403
fun mk_class_data ((consts, base_sort, inst, morphism, intro),
haftmann@25368
   404
    (defs, operations)) =
haftmann@25062
   405
  ClassData { consts = consts, base_sort = base_sort, inst = inst,
haftmann@25062
   406
    morphism = morphism, intro = intro, defs = defs,
haftmann@25368
   407
    operations = operations };
haftmann@25062
   408
fun map_class_data f (ClassData { consts, base_sort, inst, morphism, intro,
haftmann@25368
   409
    defs, operations }) =
haftmann@25062
   410
  mk_class_data (f ((consts, base_sort, inst, morphism, intro),
haftmann@25368
   411
    (defs, operations)));
haftmann@25038
   412
fun merge_class_data _ (ClassData { consts = consts,
haftmann@25062
   413
    base_sort = base_sort, inst = inst, morphism = morphism, intro = intro,
haftmann@25368
   414
    defs = defs1, operations = operations1 },
haftmann@25062
   415
  ClassData { consts = _, base_sort = _, inst = _, morphism = _, intro = _,
haftmann@25368
   416
    defs = defs2, operations = operations2 }) =
haftmann@25062
   417
  mk_class_data ((consts, base_sort, inst, morphism, intro),
haftmann@24914
   418
    (Thm.merge_thms (defs1, defs2),
haftmann@25368
   419
      AList.merge (op =) (K true) (operations1, operations2)));
haftmann@24218
   420
haftmann@24218
   421
structure ClassData = TheoryDataFun
haftmann@24218
   422
(
haftmann@25038
   423
  type T = class_data Graph.T
haftmann@25038
   424
  val empty = Graph.empty;
haftmann@24218
   425
  val copy = I;
haftmann@24218
   426
  val extend = I;
haftmann@25038
   427
  fun merge _ = Graph.join merge_class_data;
haftmann@24218
   428
);
haftmann@24218
   429
haftmann@24218
   430
haftmann@24218
   431
(* queries *)
haftmann@24218
   432
haftmann@25038
   433
val lookup_class_data = Option.map rep_class_data oo try o Graph.get_node o ClassData.get;
haftmann@24218
   434
haftmann@24589
   435
fun the_class_data thy class = case lookup_class_data thy class
wenzelm@25020
   436
 of NONE => error ("Undeclared class " ^ quote class)
haftmann@24589
   437
  | SOME data => data;
haftmann@24218
   438
haftmann@25038
   439
val is_class = is_some oo lookup_class_data;
haftmann@25038
   440
haftmann@25038
   441
val ancestry = Graph.all_succs o ClassData.get;
haftmann@24218
   442
haftmann@25002
   443
fun these_params thy =
haftmann@24218
   444
  let
haftmann@24218
   445
    fun params class =
haftmann@24218
   446
      let
wenzelm@24930
   447
        val const_typs = (#params o AxClass.get_info thy) class;
haftmann@24657
   448
        val const_names = (#consts o the_class_data thy) class;
haftmann@24218
   449
      in
haftmann@24218
   450
        (map o apsnd) (fn c => (c, (the o AList.lookup (op =) const_typs) c)) const_names
haftmann@24218
   451
      end;
haftmann@24218
   452
  in maps params o ancestry thy end;
haftmann@24218
   453
haftmann@24657
   454
fun these_defs thy = maps (these o Option.map #defs o lookup_class_data thy) o ancestry thy;
haftmann@24218
   455
haftmann@25062
   456
fun morphism thy = #morphism o the_class_data thy;
haftmann@25062
   457
haftmann@24218
   458
fun these_intros thy =
haftmann@24657
   459
  Graph.fold (fn (_, (data, _)) => insert Thm.eq_thm ((#intro o rep_class_data) data))
haftmann@25038
   460
    (ClassData.get thy) [];
haftmann@24218
   461
haftmann@24836
   462
fun these_operations thy =
haftmann@24836
   463
  maps (#operations o the_class_data thy) o ancestry thy;
haftmann@24657
   464
haftmann@24218
   465
fun print_classes thy =
haftmann@24218
   466
  let
wenzelm@24920
   467
    val ctxt = ProofContext.init thy;
haftmann@24218
   468
    val algebra = Sign.classes_of thy;
haftmann@24218
   469
    val arities =
haftmann@24218
   470
      Symtab.empty
haftmann@24218
   471
      |> Symtab.fold (fn (tyco, arities) => fold (fn (class, _) =>
haftmann@24218
   472
           Symtab.map_default (class, []) (insert (op =) tyco)) arities)
haftmann@24218
   473
             ((#arities o Sorts.rep_algebra) algebra);
haftmann@24218
   474
    val the_arities = these o Symtab.lookup arities;
haftmann@24218
   475
    fun mk_arity class tyco =
haftmann@24218
   476
      let
haftmann@24218
   477
        val Ss = Sorts.mg_domain algebra tyco [class];
wenzelm@24920
   478
      in Syntax.pretty_arity ctxt (tyco, Ss, [class]) end;
haftmann@24218
   479
    fun mk_param (c, ty) = Pretty.str (Sign.extern_const thy c ^ " :: "
wenzelm@24920
   480
      ^ setmp show_sorts false (Syntax.string_of_typ ctxt o Type.strip_sorts) ty);
haftmann@24218
   481
    fun mk_entry class = (Pretty.block o Pretty.fbreaks o map_filter I) [
haftmann@25062
   482
      (SOME o Pretty.str) ("class " ^ Sign.extern_class thy class ^ ":"),
haftmann@24218
   483
      (SOME o Pretty.block) [Pretty.str "supersort: ",
wenzelm@24920
   484
        (Syntax.pretty_sort ctxt o Sign.minimize_sort thy o Sign.super_classes thy) class],
haftmann@25062
   485
      if is_class thy class then (SOME o Pretty.str)
haftmann@25062
   486
        ("locale: " ^ Locale.extern thy class) else NONE,
haftmann@25062
   487
      ((fn [] => NONE | ps => (SOME o Pretty.block o Pretty.fbreaks)
haftmann@25062
   488
          (Pretty.str "parameters:" :: ps)) o map mk_param
wenzelm@24930
   489
        o these o Option.map #params o try (AxClass.get_info thy)) class,
haftmann@24218
   490
      (SOME o Pretty.block o Pretty.breaks) [
haftmann@24218
   491
        Pretty.str "instances:",
haftmann@24218
   492
        Pretty.list "" "" (map (mk_arity class) (the_arities class))
haftmann@24218
   493
      ]
haftmann@24218
   494
    ]
haftmann@24218
   495
  in
haftmann@24589
   496
    (Pretty.writeln o Pretty.chunks o separate (Pretty.str "")
haftmann@24589
   497
      o map mk_entry o Sorts.all_classes) algebra
haftmann@24218
   498
  end;
haftmann@24218
   499
haftmann@24218
   500
haftmann@24218
   501
(* updaters *)
haftmann@24218
   502
haftmann@25163
   503
fun add_class_data ((class, superclasses), (cs, base_sort, inst, phi, intro)) thy =
haftmann@25002
   504
  let
haftmann@25368
   505
    val operations = map (fn (v_ty as (_, ty), (c, _)) =>
haftmann@25368
   506
      (c, (class, (ty, Free v_ty)))) cs;
haftmann@25002
   507
    val cs = (map o pairself) fst cs;
haftmann@25038
   508
    val add_class = Graph.new_node (class,
haftmann@25368
   509
        mk_class_data ((cs, base_sort, map (SOME o Const) inst, phi, intro), ([], operations)))
haftmann@25002
   510
      #> fold (curry Graph.add_edge class) superclasses;
haftmann@25002
   511
  in
haftmann@25038
   512
    ClassData.map add_class thy
haftmann@25002
   513
  end;
haftmann@24218
   514
haftmann@25368
   515
fun register_operation class (c, (t, some_def)) thy =
haftmann@25062
   516
  let
haftmann@25368
   517
    val base_sort = (#base_sort o the_class_data thy) class;
haftmann@25239
   518
    val prep_typ = map_atyps
haftmann@25368
   519
      (fn TVar (vi as (v, _), sort) => if Name.aT = v
haftmann@25368
   520
        then TFree (v, base_sort) else TVar (vi, sort));
haftmann@25368
   521
    val t' = map_types prep_typ t;
haftmann@25368
   522
    val ty' = Term.fastype_of t';
haftmann@25062
   523
  in
haftmann@25062
   524
    thy
haftmann@25062
   525
    |> (ClassData.map o Graph.map_node class o map_class_data o apsnd)
haftmann@25368
   526
      (fn (defs, operations) =>
haftmann@25096
   527
        (fold cons (the_list some_def) defs,
haftmann@25368
   528
          (c, (class, (ty', t'))) :: operations))
haftmann@25062
   529
  end;
haftmann@24218
   530
haftmann@24589
   531
haftmann@24589
   532
(** rule calculation, tactics and methods **)
haftmann@24589
   533
wenzelm@25024
   534
val class_prefix = Logic.const_of_class o Sign.base_name;
wenzelm@25024
   535
haftmann@25062
   536
fun calculate_morphism class cs =
haftmann@25062
   537
  let
haftmann@25062
   538
    val subst_typ = Term.map_type_tfree (fn var as (v, sort) =>
haftmann@25062
   539
      if v = Name.aT then TVar ((v, 0), [class]) else TVar ((v, 0), sort));
haftmann@25062
   540
    fun subst_aterm (t as Free (v, ty)) = (case AList.lookup (op =) cs v
haftmann@25062
   541
         of SOME (c, _) => Const (c, ty)
haftmann@25062
   542
          | NONE => t)
haftmann@25062
   543
      | subst_aterm t = t;
haftmann@25062
   544
    val subst_term = map_aterms subst_aterm #> map_types subst_typ;
haftmann@25062
   545
  in
haftmann@25209
   546
    Morphism.term_morphism subst_term
haftmann@25062
   547
    $> Morphism.typ_morphism subst_typ
haftmann@25062
   548
  end;
haftmann@25062
   549
haftmann@25038
   550
fun class_intro thy class sups =
haftmann@24589
   551
  let
haftmann@24589
   552
    fun class_elim class =
wenzelm@25020
   553
      case (#axioms o AxClass.get_info thy) class
wenzelm@25020
   554
       of [thm] => SOME (Drule.unconstrainTs thm)
haftmann@24589
   555
        | [] => NONE;
haftmann@25038
   556
    val pred_intro = case Locale.intros thy class
haftmann@24589
   557
     of ([ax_intro], [intro]) => intro |> OF_LAST ax_intro |> SOME
haftmann@24589
   558
      | ([intro], []) => SOME intro
haftmann@24589
   559
      | ([], [intro]) => SOME intro
haftmann@24589
   560
      | _ => NONE;
haftmann@24589
   561
    val pred_intro' = pred_intro
haftmann@24589
   562
      |> Option.map (fn intro => intro OF map_filter class_elim sups);
wenzelm@24930
   563
    val class_intro = (#intro o AxClass.get_info thy) class;
haftmann@24589
   564
    val raw_intro = case pred_intro'
haftmann@24589
   565
     of SOME pred_intro => class_intro |> OF_LAST pred_intro
haftmann@24589
   566
      | NONE => class_intro;
haftmann@24589
   567
    val sort = Sign.super_classes thy class;
wenzelm@24847
   568
    val typ = TVar ((Name.aT, 0), sort);
haftmann@24589
   569
    val defs = these_defs thy sups;
haftmann@24589
   570
  in
haftmann@24589
   571
    raw_intro
haftmann@24589
   572
    |> Drule.instantiate' [SOME (Thm.ctyp_of thy typ)] []
haftmann@24589
   573
    |> strip_all_ofclass thy sort
haftmann@24589
   574
    |> Thm.strip_shyps
haftmann@24589
   575
    |> MetaSimplifier.rewrite_rule defs
haftmann@24589
   576
    |> Drule.unconstrainTs
haftmann@24589
   577
  end;
haftmann@24589
   578
haftmann@24589
   579
fun class_interpretation class facts defs thy =
haftmann@24589
   580
  let
haftmann@25038
   581
    val params = these_params thy [class];
haftmann@25083
   582
    val inst = (#inst o the_class_data thy) class;
wenzelm@25020
   583
    val tac = ALLGOALS (ProofContext.fact_tac facts);
haftmann@25038
   584
    val prfx = class_prefix class;
haftmann@24589
   585
  in
haftmann@25038
   586
    thy
haftmann@25038
   587
    |> fold_map (get_remove_global_constraint o fst o snd) params
ballarin@25094
   588
    ||> prove_interpretation tac ((false, prfx), []) (Locale.Locale class)
ballarin@25094
   589
          (inst, map (fn def => (("", []), def)) defs)
haftmann@25038
   590
    |-> (fn cs => fold (Sign.add_const_constraint o apsnd SOME) cs)
haftmann@24589
   591
  end;
haftmann@24218
   592
haftmann@24218
   593
fun intro_classes_tac facts st =
haftmann@24218
   594
  let
haftmann@24218
   595
    val thy = Thm.theory_of_thm st;
haftmann@24218
   596
    val classes = Sign.all_classes thy;
haftmann@24218
   597
    val class_trivs = map (Thm.class_triv thy) classes;
haftmann@24218
   598
    val class_intros = these_intros thy;
wenzelm@24930
   599
    val axclass_intros = map_filter (try (#intro o AxClass.get_info thy)) classes;
haftmann@24218
   600
  in
haftmann@25268
   601
    Method.intros_tac (class_trivs @ class_intros @ axclass_intros) facts st
haftmann@24218
   602
  end;
haftmann@24218
   603
haftmann@24218
   604
fun default_intro_classes_tac [] = intro_classes_tac []
wenzelm@24930
   605
  | default_intro_classes_tac _ = no_tac;
haftmann@24218
   606
haftmann@24218
   607
fun default_tac rules ctxt facts =
haftmann@24218
   608
  HEADGOAL (Method.some_rule_tac rules ctxt facts) ORELSE
haftmann@24218
   609
    default_intro_classes_tac facts;
haftmann@24218
   610
haftmann@24218
   611
val _ = Context.add_setup (Method.add_methods
haftmann@24218
   612
 [("intro_classes", Method.no_args (Method.METHOD intro_classes_tac),
haftmann@24218
   613
    "back-chain introduction rules of classes"),
haftmann@24218
   614
  ("default", Method.thms_ctxt_args (Method.METHOD oo default_tac),
haftmann@24218
   615
    "apply some intro/elim rule")]);
haftmann@24218
   616
haftmann@25195
   617
fun subclass_rule thy (sub, sup) =
haftmann@25195
   618
  let
haftmann@25195
   619
    val ctxt = Locale.init sub thy;
haftmann@25195
   620
    val ctxt_thy = ProofContext.init thy;
haftmann@25195
   621
    val props =
haftmann@25195
   622
      Locale.global_asms_of thy sup
haftmann@25195
   623
      |> maps snd
haftmann@25195
   624
      |> map (ObjectLogic.ensure_propT thy);
haftmann@25195
   625
    fun tac { prems, context } =
haftmann@25195
   626
      Locale.intro_locales_tac true context prems
haftmann@25195
   627
        ORELSE ALLGOALS assume_tac;
haftmann@25195
   628
  in
haftmann@25195
   629
    Goal.prove_multi ctxt [] [] props tac
haftmann@25195
   630
    |> map (Assumption.export false ctxt ctxt_thy)
haftmann@25195
   631
    |> Variable.export ctxt ctxt_thy
haftmann@25195
   632
  end;
haftmann@25195
   633
haftmann@25195
   634
fun prove_single_subclass (sub, sup) thms ctxt thy =
haftmann@25195
   635
  let
haftmann@25195
   636
    val ctxt_thy = ProofContext.init thy;
haftmann@25195
   637
    val subclass_rule = Conjunction.intr_balanced thms
haftmann@25195
   638
      |> Assumption.export false ctxt ctxt_thy
haftmann@25195
   639
      |> singleton (Variable.export ctxt ctxt_thy);
haftmann@25195
   640
    val sub_inst = Thm.ctyp_of thy (TVar ((Name.aT, 0), [sub]));
haftmann@25195
   641
    val sub_ax = #axioms (AxClass.get_info thy sub);
haftmann@25195
   642
    val classrel =
haftmann@25195
   643
      #intro (AxClass.get_info thy sup)
haftmann@25195
   644
      |> Drule.instantiate' [SOME sub_inst] []
haftmann@25195
   645
      |> OF_LAST (subclass_rule OF sub_ax)
haftmann@25195
   646
      |> strip_all_ofclass thy (Sign.super_classes thy sup)
haftmann@25195
   647
      |> Thm.strip_shyps
haftmann@25195
   648
  in
haftmann@25195
   649
    thy
haftmann@25195
   650
    |> AxClass.add_classrel classrel
haftmann@25195
   651
    |> prove_interpretation_in (ALLGOALS (ProofContext.fact_tac thms))
haftmann@25195
   652
         I (sub, Locale.Locale sup)
haftmann@25195
   653
    |> ClassData.map (Graph.add_edge (sub, sup))
haftmann@25195
   654
  end;
haftmann@25195
   655
haftmann@25195
   656
fun prove_subclass (sub, sup) thms ctxt thy =
haftmann@25195
   657
  let
haftmann@25268
   658
    val classes = ClassData.get thy;
haftmann@25268
   659
    val is_sup = not o null o curry (Graph.irreducible_paths classes) sub;
haftmann@25268
   660
    val supclasses = Graph.all_succs classes [sup] |> filter_out is_sup;
haftmann@25195
   661
    fun transform sup' = subclass_rule thy (sup, sup') |> map (fn thm => thm OF thms);
haftmann@25195
   662
  in
haftmann@25195
   663
    thy
haftmann@25195
   664
    |> fold_rev (fn sup' => prove_single_subclass (sub, sup')
haftmann@25195
   665
         (transform sup') ctxt) supclasses
haftmann@25195
   666
 end;
haftmann@25195
   667
haftmann@24218
   668
haftmann@24589
   669
(** classes and class target **)
haftmann@24218
   670
haftmann@25002
   671
(* class context syntax *)
haftmann@24748
   672
haftmann@25083
   673
structure ClassSyntax = ProofDataFun(
haftmann@25083
   674
  type T = {
haftmann@25368
   675
    local_constraints: (string * typ) list,
haftmann@25368
   676
    global_constraints: (string * typ) list,
haftmann@25083
   677
    base_sort: sort,
haftmann@25368
   678
    operations: (string * (typ * term)) list,
haftmann@25195
   679
    unchecks: (term * term) list,
haftmann@25083
   680
    passed: bool
haftmann@25368
   681
  };
haftmann@25368
   682
  fun init _ = {
haftmann@25368
   683
    local_constraints = [],
haftmann@25368
   684
    global_constraints = [],
haftmann@25368
   685
    base_sort = [],
haftmann@25368
   686
    operations = [],
haftmann@25368
   687
    unchecks = [],
haftmann@25368
   688
    passed = true
haftmann@25368
   689
  };;
haftmann@25083
   690
);
haftmann@25083
   691
wenzelm@25344
   692
fun synchronize_syntax sups base_sort ctxt =
haftmann@24914
   693
  let
wenzelm@25344
   694
    val thy = ProofContext.theory_of ctxt;
haftmann@25368
   695
    fun subst_class_typ sort = map_atyps
haftmann@25368
   696
      (fn TFree _ => TVar ((Name.aT, 0), sort) | ty' => ty');
haftmann@25083
   697
    val operations = these_operations thy sups;
haftmann@25368
   698
    val local_constraints =
haftmann@25368
   699
      (map o apsnd) (subst_class_typ base_sort o fst o snd) operations;
haftmann@25368
   700
    val global_constraints =
haftmann@25368
   701
      (map o apsnd) (fn (class, (ty, _)) => subst_class_typ [class] ty) operations;
wenzelm@25318
   702
    fun declare_const (c, _) =
wenzelm@25318
   703
      let val b = Sign.base_name c
wenzelm@25344
   704
      in Sign.intern_const thy b = c ? Variable.declare_const (b, c) end;
haftmann@25368
   705
    val unchecks = map (fn (c, (_, (ty, t))) => (t, Const (c, ty))) operations;
haftmann@25083
   706
  in
haftmann@25083
   707
    ctxt
haftmann@25368
   708
    |> fold declare_const local_constraints
haftmann@25368
   709
    |> fold (ProofContext.add_const_constraint o apsnd SOME) local_constraints
haftmann@25368
   710
    |> ClassSyntax.put {
haftmann@25368
   711
        local_constraints = local_constraints,
haftmann@25368
   712
        global_constraints = global_constraints,
haftmann@25083
   713
        base_sort = base_sort,
haftmann@25368
   714
        operations = (map o apsnd) snd operations,
haftmann@25195
   715
        unchecks = unchecks,
haftmann@25083
   716
        passed = false
haftmann@25368
   717
      }
haftmann@25083
   718
  end;
haftmann@25083
   719
haftmann@25083
   720
fun refresh_syntax class ctxt =
haftmann@25002
   721
  let
haftmann@25002
   722
    val thy = ProofContext.theory_of ctxt;
haftmann@25062
   723
    val base_sort = (#base_sort o the_class_data thy) class;
wenzelm@25344
   724
  in synchronize_syntax [class] base_sort ctxt end;
haftmann@24914
   725
haftmann@25368
   726
val mark_passed = ClassSyntax.map
haftmann@25368
   727
  (fn { local_constraints, global_constraints, base_sort, operations, unchecks, passed } =>
haftmann@25368
   728
    { local_constraints = local_constraints, global_constraints = global_constraints,
haftmann@25368
   729
      base_sort = base_sort, operations = operations, unchecks = unchecks, passed = true });
haftmann@25083
   730
haftmann@25083
   731
fun sort_term_check ts ctxt =
haftmann@24748
   732
  let
haftmann@25368
   733
    val { local_constraints, global_constraints, base_sort, operations, passed, ... } =
haftmann@25368
   734
      ClassSyntax.get ctxt;
haftmann@25368
   735
    fun check_improve (Const (c, ty)) = (case AList.lookup (op =) local_constraints c
haftmann@25368
   736
         of SOME ty0 => (case try (Type.raw_match (ty0, ty)) Vartab.empty
haftmann@25368
   737
             of SOME tyenv => (case Vartab.lookup tyenv (Name.aT, 0)
haftmann@25368
   738
                 of SOME (_, TVar (tvar as (vi, _))) =>
haftmann@25368
   739
                      if TypeInfer.is_param vi then cons tvar else I
haftmann@25368
   740
                  | _ => I)
haftmann@25368
   741
              | NONE => I)
haftmann@25368
   742
          | NONE => I)
haftmann@25368
   743
      | check_improve _ = I;
haftmann@25368
   744
    val improvements = (fold o fold_aterms) check_improve ts [];
haftmann@25368
   745
    val ts' = (map o map_types o map_atyps) (fn ty as TVar tvar =>
haftmann@25368
   746
        if member (op =) improvements tvar
haftmann@25368
   747
          then TFree (Name.aT, base_sort) else ty | ty => ty) ts;
haftmann@25368
   748
    fun check t0 = Envir.expand_term (fn Const (c, ty) => (case AList.lookup (op =) operations c
haftmann@25368
   749
         of SOME (ty0, t) =>
haftmann@25368
   750
              if Type.typ_instance (ProofContext.tsig_of ctxt) (ty, ty0)
haftmann@25368
   751
              then SOME (ty0, check t) else NONE
haftmann@25368
   752
          | NONE => NONE)
haftmann@25368
   753
      | _ => NONE) t0;
haftmann@25368
   754
    val ts'' = map check ts';
haftmann@25368
   755
  in if eq_list (op aconv) (ts, ts'') andalso passed then NONE
haftmann@25083
   756
  else
haftmann@25083
   757
    ctxt
haftmann@25368
   758
    |> fold (ProofContext.add_const_constraint o apsnd SOME) global_constraints
haftmann@25083
   759
    |> mark_passed
haftmann@25368
   760
    |> pair ts''
haftmann@25083
   761
    |> SOME
haftmann@25083
   762
  end;
haftmann@24748
   763
haftmann@25083
   764
fun sort_term_uncheck ts ctxt =
haftmann@25002
   765
  let
haftmann@25002
   766
    val thy = ProofContext.theory_of ctxt;
haftmann@25368
   767
    val unchecks = (#unchecks o ClassSyntax.get) ctxt;
haftmann@25462
   768
    val ts' = map (Pattern.rewrite_term thy unchecks []) ts;
wenzelm@25060
   769
  in if eq_list (op aconv) (ts, ts') then NONE else SOME (ts', ctxt) end;
haftmann@25002
   770
wenzelm@25344
   771
fun init_ctxt sups base_sort ctxt =
haftmann@25083
   772
  ctxt
haftmann@25083
   773
  |> Variable.declare_term
haftmann@25083
   774
      (Logic.mk_type (TFree (Name.aT, base_sort)))
wenzelm@25344
   775
  |> synchronize_syntax sups base_sort
haftmann@25083
   776
  |> Context.proof_map (
haftmann@25083
   777
      Syntax.add_term_check 0 "class" sort_term_check
haftmann@25103
   778
      #> Syntax.add_term_uncheck 0 "class" sort_term_uncheck)
haftmann@24901
   779
haftmann@25311
   780
fun init class thy =
haftmann@25311
   781
  thy
haftmann@25311
   782
  |> Locale.init class
wenzelm@25344
   783
  |> init_ctxt [class] ((#base_sort o the_class_data thy) class);
haftmann@24914
   784
haftmann@24748
   785
haftmann@24589
   786
(* class definition *)
haftmann@24218
   787
haftmann@24218
   788
local
haftmann@24218
   789
haftmann@24748
   790
fun gen_class_spec prep_class prep_expr process_expr thy raw_supclasses raw_includes_elems =
haftmann@24218
   791
  let
haftmann@24748
   792
    val supclasses = map (prep_class thy) raw_supclasses;
haftmann@25209
   793
    val sups = filter (is_class thy) supclasses;
haftmann@25209
   794
    fun the_base_sort class = lookup_class_data thy class
haftmann@25209
   795
      |> Option.map #base_sort
haftmann@25209
   796
      |> the_default [class];
haftmann@25209
   797
    val base_sort = Sign.minimize_sort thy (maps the_base_sort supclasses);
haftmann@24748
   798
    val supsort = Sign.minimize_sort thy supclasses;
haftmann@25038
   799
    val suplocales = map Locale.Locale sups;
haftmann@24748
   800
    val (raw_elems, includes) = fold_rev (fn Locale.Elem e => apfst (cons e)
haftmann@24748
   801
      | Locale.Expr i => apsnd (cons (prep_expr thy i))) raw_includes_elems ([], []);
haftmann@24748
   802
    val supexpr = Locale.Merge suplocales;
haftmann@24748
   803
    val supparams = (map fst o Locale.parameters_of_expr thy) supexpr;
haftmann@25002
   804
    val supconsts = AList.make (the o AList.lookup (op =) (these_params thy sups))
haftmann@24748
   805
      (map fst supparams);
haftmann@24748
   806
    val mergeexpr = Locale.Merge (suplocales @ includes);
haftmann@24748
   807
    val constrain = Element.Constrains ((map o apsnd o map_atyps)
wenzelm@24847
   808
      (fn TFree (_, sort) => TFree (Name.aT, sort)) supparams);
haftmann@24748
   809
  in
haftmann@24748
   810
    ProofContext.init thy
haftmann@24748
   811
    |> Locale.cert_expr supexpr [constrain]
haftmann@24748
   812
    |> snd
wenzelm@25344
   813
    |> init_ctxt sups base_sort
haftmann@24748
   814
    |> process_expr Locale.empty raw_elems
haftmann@24748
   815
    |> fst
haftmann@25062
   816
    |> (fn elems => ((((sups, supconsts), (supsort, base_sort, mergeexpr)),
haftmann@24748
   817
          (*FIXME*) if null includes then constrain :: elems else elems)))
haftmann@24748
   818
  end;
haftmann@24748
   819
haftmann@24748
   820
val read_class_spec = gen_class_spec Sign.intern_class Locale.intern_expr Locale.read_expr;
haftmann@24748
   821
val check_class_spec = gen_class_spec (K I) (K I) Locale.cert_expr;
haftmann@24748
   822
wenzelm@24968
   823
fun define_class_params (name, raw_superclasses) raw_consts raw_dep_axioms other_consts thy =
wenzelm@24968
   824
  let
wenzelm@24968
   825
    val superclasses = map (Sign.certify_class thy) raw_superclasses;
wenzelm@24968
   826
    val consts = (map o apfst o apsnd) (Sign.certify_typ thy) raw_consts;
haftmann@25083
   827
    fun add_const ((c, ty), syn) =
haftmann@25083
   828
      Sign.declare_const [] (c, Type.strip_sorts ty, syn) #>> Term.dest_Const;
wenzelm@24968
   829
    fun mk_axioms cs thy =
wenzelm@24968
   830
      raw_dep_axioms thy cs
wenzelm@24968
   831
      |> (map o apsnd o map) (Sign.cert_prop thy)
wenzelm@24968
   832
      |> rpair thy;
haftmann@25002
   833
    fun constrain_typs class = (map o apsnd o Term.map_type_tfree)
haftmann@25002
   834
      (fn (v, _) => TFree (v, [class]))
wenzelm@24968
   835
  in
wenzelm@24968
   836
    thy
wenzelm@24968
   837
    |> Sign.add_path (Logic.const_of_class name)
wenzelm@24968
   838
    |> fold_map add_const consts
wenzelm@24968
   839
    ||> Sign.restore_naming thy
wenzelm@24968
   840
    |-> (fn cs => mk_axioms cs
wenzelm@24968
   841
    #-> (fn axioms_prop => AxClass.define_class (name, superclasses)
wenzelm@24968
   842
           (map fst cs @ other_consts) axioms_prop
haftmann@25002
   843
    #-> (fn class => `(fn _ => constrain_typs class cs)
haftmann@25002
   844
    #-> (fn cs' => `(fn thy => AxClass.get_info thy class)
haftmann@25002
   845
    #-> (fn {axioms, ...} => fold (Sign.add_const_constraint o apsnd SOME) cs'
haftmann@25002
   846
    #> pair (class, (cs', axioms)))))))
wenzelm@24968
   847
  end;
wenzelm@24968
   848
haftmann@25002
   849
fun gen_class prep_spec prep_param bname
haftmann@24748
   850
    raw_supclasses raw_includes_elems raw_other_consts thy =
haftmann@24748
   851
  let
haftmann@25038
   852
    val class = Sign.full_name thy bname;
haftmann@25062
   853
    val (((sups, supconsts), (supsort, base_sort, mergeexpr)), elems_syn) =
haftmann@24748
   854
      prep_spec thy raw_supclasses raw_includes_elems;
wenzelm@24968
   855
    val other_consts = map (tap (Sign.the_const_type thy) o prep_param thy) raw_other_consts;
haftmann@25163
   856
    fun mk_inst class cs =
haftmann@25163
   857
      (map o apsnd o Term.map_type_tfree) (fn (v, _) => TFree (v, [class])) cs;
haftmann@25062
   858
    fun fork_syntax (Element.Fixes xs) =
haftmann@25062
   859
          fold_map (fn (c, ty, syn) => cons (c, syn) #> pair (c, ty, NoSyn)) xs
haftmann@25062
   860
          #>> Element.Fixes
haftmann@25062
   861
      | fork_syntax x = pair x;
haftmann@25062
   862
    val (elems, global_syn) = fold_map fork_syntax elems_syn [];
wenzelm@25326
   863
    fun globalize (c, ty) =
haftmann@25062
   864
      ((c, Term.map_type_tfree (K (TFree (Name.aT, base_sort))) ty),
haftmann@25062
   865
        (the_default NoSyn o AList.lookup (op =) global_syn) c);
haftmann@25038
   866
    fun extract_params thy =
haftmann@24218
   867
      let
haftmann@25062
   868
        val params = map fst (Locale.parameters_of thy class);
haftmann@24218
   869
      in
haftmann@25062
   870
        (params, (map globalize o snd o chop (length supconsts)) params)
haftmann@24218
   871
      end;
haftmann@25038
   872
    fun extract_assumes params thy cs =
haftmann@24218
   873
      let
haftmann@24218
   874
        val consts = supconsts @ (map (fst o fst) params ~~ cs);
haftmann@24218
   875
        fun subst (Free (c, ty)) =
haftmann@24218
   876
              Const ((fst o the o AList.lookup (op =) consts) c, ty)
haftmann@24218
   877
          | subst t = t;
haftmann@24218
   878
        fun prep_asm ((name, atts), ts) =
wenzelm@25024
   879
          ((Sign.base_name name, map (Attrib.attribute_i thy) atts),
haftmann@24589
   880
            (map o map_aterms) subst ts);
haftmann@24218
   881
      in
haftmann@25038
   882
        Locale.global_asms_of thy class
haftmann@24218
   883
        |> map prep_asm
haftmann@24218
   884
      end;
haftmann@24218
   885
  in
haftmann@24218
   886
    thy
haftmann@24748
   887
    |> Locale.add_locale_i (SOME "") bname mergeexpr elems
haftmann@25038
   888
    |> snd
haftmann@25311
   889
    |> ProofContext.theory_of
haftmann@25311
   890
    |> `extract_params
haftmann@25311
   891
    |-> (fn (all_params, params) =>
wenzelm@24968
   892
        define_class_params (bname, supsort) params
haftmann@25038
   893
          (extract_assumes params) other_consts
haftmann@25038
   894
      #-> (fn (_, (consts, axioms)) =>
haftmann@25038
   895
        `(fn thy => class_intro thy class sups)
haftmann@24218
   896
      #-> (fn class_intro =>
haftmann@25062
   897
        PureThy.note_thmss_qualified "" (NameSpace.append class classN)
haftmann@25062
   898
          [((introN, []), [([class_intro], [])])]
haftmann@25062
   899
      #-> (fn [(_, [class_intro])] =>
haftmann@25038
   900
        add_class_data ((class, sups),
haftmann@25062
   901
          (map fst params ~~ consts, base_sort,
haftmann@25163
   902
            mk_inst class (map snd supconsts @ consts),
haftmann@25062
   903
              calculate_morphism class (supconsts @ (map (fst o fst) params ~~ consts)), class_intro))
haftmann@25038
   904
      #> class_interpretation class axioms []
haftmann@25311
   905
      ))))
haftmann@25268
   906
    |> init class
haftmann@25038
   907
    |> pair class
haftmann@24218
   908
  end;
haftmann@24218
   909
wenzelm@25326
   910
fun read_const thy = #1 o Term.dest_Const o ProofContext.read_const (ProofContext.init thy);
wenzelm@25326
   911
haftmann@24218
   912
in
haftmann@24218
   913
wenzelm@25326
   914
val class_cmd = gen_class read_class_spec read_const;
haftmann@24748
   915
val class = gen_class check_class_spec (K I);
haftmann@24218
   916
haftmann@24218
   917
end; (*local*)
haftmann@24218
   918
haftmann@24218
   919
haftmann@25485
   920
(* class target *)
haftmann@24218
   921
haftmann@25485
   922
fun logical_const class pos ((c, mx), dict) thy =
haftmann@24218
   923
  let
wenzelm@25024
   924
    val prfx = class_prefix class;
wenzelm@25024
   925
    val thy' = thy |> Sign.add_path prfx;
haftmann@25062
   926
    val phi = morphism thy' class;
wenzelm@25024
   927
haftmann@25062
   928
    val c' = Sign.full_name thy' c;
haftmann@25239
   929
    val dict' = Morphism.term phi dict;
haftmann@25239
   930
    val dict_def = map_types Logic.unvarifyT dict';
haftmann@25239
   931
    val ty' = Term.fastype_of dict_def;
haftmann@25083
   932
    val ty'' = Type.strip_sorts ty';
haftmann@25239
   933
    val def_eq = Logic.mk_equals (Const (c', ty'), dict_def);
haftmann@24218
   934
  in
wenzelm@25024
   935
    thy'
haftmann@25096
   936
    |> Sign.declare_const pos (c, ty'', mx) |> snd
haftmann@25485
   937
    |> Thm.add_def false (c, def_eq)
haftmann@25062
   938
    |>> Thm.symmetric
haftmann@25083
   939
    |-> (fn def => class_interpretation class [def] [Thm.prop_of def]
haftmann@25368
   940
          #> register_operation class (c', (dict', SOME (Thm.varifyT def))))
haftmann@24218
   941
    |> Sign.restore_naming thy
haftmann@25083
   942
    |> Sign.add_const_constraint (c', SOME ty')
haftmann@24218
   943
  end;
haftmann@24218
   944
haftmann@25485
   945
fun syntactic_const class prmode pos ((c, mx), rhs) thy =
haftmann@24836
   946
  let
wenzelm@25024
   947
    val prfx = class_prefix class;
haftmann@25096
   948
    val thy' = thy |> Sign.add_path prfx;
haftmann@25062
   949
    val phi = morphism thy class;
haftmann@25062
   950
haftmann@25096
   951
    val c' = Sign.full_name thy' c;
haftmann@25146
   952
    val rews = map (Logic.dest_equals o Thm.prop_of) (these_defs thy' [class])
haftmann@25146
   953
    val rhs' = (Pattern.rewrite_term thy rews [] o Morphism.term phi) rhs;
haftmann@25239
   954
    val ty' = Logic.unvarifyT (Term.fastype_of rhs');
haftmann@24836
   955
  in
haftmann@25096
   956
    thy'
haftmann@25146
   957
    |> Sign.add_abbrev (#1 prmode) pos (c, map_types Type.strip_sorts rhs') |> snd
haftmann@25096
   958
    |> Sign.add_const_constraint (c', SOME ty')
wenzelm@25024
   959
    |> Sign.notation true prmode [(Const (c', ty'), mx)]
haftmann@25368
   960
    |> register_operation class (c', (rhs', NONE))
haftmann@25096
   961
    |> Sign.restore_naming thy
haftmann@24836
   962
  end;
haftmann@24836
   963
haftmann@25462
   964
haftmann@25462
   965
(** instantiation target **)
haftmann@25462
   966
haftmann@25462
   967
(* bookkeeping *)
haftmann@25462
   968
haftmann@25462
   969
datatype instantiation = Instantiation of {
haftmann@25462
   970
  arities: arity list,
haftmann@25462
   971
  params: ((string * string) * (string * typ)) list
haftmann@25462
   972
}
haftmann@25462
   973
haftmann@25462
   974
structure Instantiation = ProofDataFun
haftmann@25462
   975
(
haftmann@25462
   976
  type T = instantiation
haftmann@25462
   977
  fun init _ = Instantiation { arities = [], params = [] };
haftmann@25462
   978
);
haftmann@25462
   979
haftmann@25485
   980
fun mk_instantiation (arities, params) =
haftmann@25485
   981
  Instantiation { arities = arities, params = params };
haftmann@25485
   982
fun get_instantiation ctxt = case Instantiation.get ctxt
haftmann@25485
   983
 of Instantiation data => data;
haftmann@25462
   984
fun map_instantiation f (Instantiation { arities, params }) =
haftmann@25462
   985
  mk_instantiation (f (arities, params));
haftmann@25462
   986
haftmann@25485
   987
fun the_instantiation ctxt = case get_instantiation ctxt
haftmann@25485
   988
 of { arities = [], ... } => error "No instantiation target"
haftmann@25485
   989
  | data => data;
haftmann@25462
   990
haftmann@25485
   991
val instantiation_params = #params o get_instantiation;
haftmann@25462
   992
haftmann@25485
   993
fun instantiation_param ctxt v = instantiation_params ctxt
haftmann@25462
   994
  |> find_first (fn (_, (v', _)) => v = v')
haftmann@25462
   995
  |> Option.map (fst o fst);
haftmann@25462
   996
haftmann@25485
   997
fun confirm_declaration c = (Instantiation.map o map_instantiation o apsnd)
haftmann@25485
   998
  (filter_out (fn (_, (c', _)) => c' = c));
haftmann@25485
   999
haftmann@25462
  1000
haftmann@25462
  1001
(* syntax *)
haftmann@25462
  1002
haftmann@25462
  1003
fun inst_term_check ts ctxt =
haftmann@25462
  1004
  let
haftmann@25462
  1005
    val params = instantiation_params ctxt;
haftmann@25462
  1006
    val tsig = ProofContext.tsig_of ctxt;
haftmann@25462
  1007
    val thy = ProofContext.theory_of ctxt;
haftmann@25462
  1008
haftmann@25462
  1009
    fun check_improve (Const (c, ty)) = (case inst_tyco thy (c, ty)
haftmann@25462
  1010
         of SOME tyco => (case AList.lookup (op =) params (c, tyco)
haftmann@25462
  1011
             of SOME (_, ty') => Type.typ_match tsig (ty, ty')
haftmann@25462
  1012
              | NONE => I)
haftmann@25462
  1013
          | NONE => I)
haftmann@25462
  1014
      | check_improve _ = I;
haftmann@25462
  1015
    val improvement = (fold o fold_aterms) check_improve ts Vartab.empty;
haftmann@25462
  1016
    val ts' = (map o map_types) (Envir.typ_subst_TVars improvement) ts;
haftmann@25462
  1017
    val ts'' = (map o map_aterms) (fn t as Const (c, ty) => (case inst_tyco thy (c, ty)
haftmann@25462
  1018
         of SOME tyco => (case AList.lookup (op =) params (c, tyco)
haftmann@25462
  1019
             of SOME v_ty => Free v_ty
haftmann@25462
  1020
              | NONE => t)
haftmann@25462
  1021
          | NONE => t)
haftmann@25462
  1022
      | t => t) ts';
haftmann@25462
  1023
  in if eq_list (op aconv) (ts, ts'') then NONE else SOME (ts'', ctxt) end;
haftmann@25462
  1024
haftmann@25462
  1025
fun inst_term_uncheck ts ctxt =
haftmann@25462
  1026
  let
haftmann@25462
  1027
    val params = instantiation_params ctxt;
haftmann@25462
  1028
    val ts' = (map o map_aterms) (fn t as Free (v, ty) =>
haftmann@25462
  1029
       (case get_first (fn ((c, _), (v', _)) => if v = v' then SOME c else NONE) params
haftmann@25462
  1030
         of SOME c => Const (c, ty)
haftmann@25462
  1031
          | NONE => t)
haftmann@25462
  1032
      | t => t) ts;
haftmann@25462
  1033
  in if eq_list (op aconv) (ts, ts') then NONE else SOME (ts', ctxt) end;
haftmann@25462
  1034
haftmann@25462
  1035
haftmann@25462
  1036
(* target *)
haftmann@25462
  1037
haftmann@25485
  1038
val sanatize_name = (*FIXME*)
haftmann@25485
  1039
  let
haftmann@25485
  1040
    fun is_valid s = Symbol.is_ascii_letter s orelse Symbol.is_ascii_digit s orelse s = "'";
haftmann@25485
  1041
    val is_junk = not o is_valid andf Symbol.is_regular;
haftmann@25485
  1042
    val junk = Scan.many is_junk;
haftmann@25485
  1043
    val scan_valids = Symbol.scanner "Malformed input"
haftmann@25485
  1044
      ((junk |--
haftmann@25485
  1045
        (Scan.optional (Scan.one Symbol.is_ascii_letter) "x" ^^ (Scan.many is_valid >> implode)
haftmann@25485
  1046
        --| junk))
haftmann@25485
  1047
      -- Scan.repeat ((Scan.many1 is_valid >> implode) --| junk) >> op ::);
haftmann@25485
  1048
  in
haftmann@25485
  1049
    explode #> scan_valids #> implode
haftmann@25485
  1050
  end;
haftmann@25485
  1051
haftmann@25462
  1052
haftmann@25485
  1053
fun init_instantiation arities thy =
haftmann@25462
  1054
  let
haftmann@25485
  1055
    val _ = if null arities then error "At least one arity must be given" else ();
haftmann@25485
  1056
    val _ = case (duplicates (op =) o map #1) arities
haftmann@25485
  1057
     of [] => ()
haftmann@25485
  1058
      | dupl_tycos => error ("Type constructors occur more than once in arities: "
haftmann@25485
  1059
          ^ commas_quote dupl_tycos);
haftmann@25485
  1060
    val ty_insts = map (fn (tyco, sorts, _) =>
haftmann@25485
  1061
        (tyco, Type (tyco, map TFree (Name.names Name.context Name.aT sorts))))
haftmann@25485
  1062
      arities;
haftmann@25485
  1063
    val ty_inst = the o AList.lookup (op =) ty_insts;
haftmann@25485
  1064
    fun type_name "*" = "prod"
haftmann@25485
  1065
      | type_name "+" = "sum"
haftmann@25485
  1066
      | type_name s = sanatize_name (NameSpace.base s); (*FIXME*)
haftmann@25485
  1067
    fun get_param tyco sorts (param, (c, ty)) =
haftmann@25485
  1068
      ((unoverload_const thy (c, ty), tyco),
haftmann@25485
  1069
        (param ^ "_" ^ type_name tyco,
haftmann@25485
  1070
          map_atyps (K (ty_inst tyco)) ty));
haftmann@25485
  1071
    fun get_params (tyco, sorts, sort) =
haftmann@25485
  1072
      map (get_param tyco sorts) (these_params thy sort)
haftmann@25485
  1073
    val params = maps get_params arities;
haftmann@25485
  1074
  in
haftmann@25485
  1075
    thy
haftmann@25485
  1076
    |> ProofContext.init
haftmann@25485
  1077
    |> Instantiation.put (mk_instantiation (arities, params))
haftmann@25485
  1078
    |> fold (Variable.declare_term o Logic.mk_type o snd) ty_insts
haftmann@25485
  1079
    |> fold ProofContext.add_arity arities
haftmann@25485
  1080
    |> Context.proof_map (
haftmann@25485
  1081
        Syntax.add_term_check 0 "instance" inst_term_check
haftmann@25485
  1082
        #> Syntax.add_term_uncheck 0 "instance" inst_term_uncheck)
haftmann@25485
  1083
  end;
haftmann@25485
  1084
haftmann@25485
  1085
fun gen_instantiation_instance do_proof after_qed lthy =
haftmann@25485
  1086
  let
haftmann@25462
  1087
    val ctxt = LocalTheory.target_of lthy;
haftmann@25462
  1088
    val arities = (#arities o the_instantiation) ctxt;
haftmann@25485
  1089
    val arities_proof = maps Logic.mk_arities arities;
haftmann@25462
  1090
    fun after_qed' results =
haftmann@25462
  1091
      LocalTheory.theory (fold (AxClass.add_arity o Thm.varifyT) results)
haftmann@25462
  1092
      #> after_qed;
haftmann@25462
  1093
  in
haftmann@25462
  1094
    lthy
haftmann@25462
  1095
    |> do_proof after_qed' arities_proof
haftmann@25462
  1096
  end;
haftmann@25462
  1097
haftmann@25485
  1098
val instantiation_instance = gen_instantiation_instance (fn after_qed => fn ts =>
haftmann@25462
  1099
  Proof.theorem_i NONE (after_qed o map the_single) (map (fn t => [(t, [])]) ts));
haftmann@25462
  1100
haftmann@25485
  1101
fun prove_instantiation_instance tac = gen_instantiation_instance (fn after_qed =>
haftmann@25462
  1102
  fn ts => fn lthy => after_qed (Goal.prove_multi lthy [] [] ts
haftmann@25462
  1103
    (fn {context, ...} => tac context)) lthy) I;
haftmann@25462
  1104
haftmann@25462
  1105
fun conclude_instantiation lthy =
haftmann@25462
  1106
  let
haftmann@25485
  1107
    val { arities, params } = the_instantiation lthy;
haftmann@25462
  1108
    val thy = ProofContext.theory_of lthy;
haftmann@25462
  1109
    (*val _ = map (fn (tyco, sorts, sort) =>
haftmann@25462
  1110
      if Sign.of_sort thy
haftmann@25462
  1111
        (Type (tyco, map TFree (Name.names Name.context Name.aT sorts)), sort)
haftmann@25462
  1112
      then () else error ("Missing instance proof for type " ^ quote (Sign.extern_type thy tyco)))
haftmann@25462
  1113
        arities; FIXME activate when old instance command is gone*)
haftmann@25462
  1114
    val params_of = maps (these o try (#params o AxClass.get_info thy))
haftmann@25462
  1115
      o Sign.complete_sort thy;
haftmann@25462
  1116
    val missing_params = arities
haftmann@25462
  1117
      |> maps (fn (tyco, _, sort) => params_of sort |> map (rpair tyco))
haftmann@25462
  1118
      |> filter_out (can (inst thy) o apfst fst);
haftmann@25485
  1119
    fun declare_missing ((c, ty0), tyco) thy =
haftmann@25485
  1120
    (*fun declare_missing ((c, tyco), (_, ty)) thy =*)
haftmann@25462
  1121
      let
haftmann@25462
  1122
        val SOME class = AxClass.class_of_param thy c;
haftmann@25485
  1123
        val name_inst = AxClass.instance_name (tyco, class) ^ "_inst";
haftmann@25485
  1124
        val c' = NameSpace.base c;
haftmann@25462
  1125
        val vs = Name.names Name.context Name.aT (replicate (Sign.arity_number thy tyco) []);
haftmann@25485
  1126
        val ty = map_atyps (fn _ => Type (tyco, map TFree vs)) ty0;
haftmann@25462
  1127
      in
haftmann@25462
  1128
        thy
haftmann@25462
  1129
        |> Sign.sticky_prefix name_inst
haftmann@25462
  1130
        |> Sign.no_base_names
haftmann@25485
  1131
        |> Sign.declare_const [] (c', ty, NoSyn)
haftmann@25462
  1132
        |-> (fn const' as Const (c'', _) => Thm.add_def true
haftmann@25485
  1133
              (Thm.def_name c', Logic.mk_equals (const', Const (c, ty)))
haftmann@25462
  1134
        #>> Thm.varifyT
haftmann@25462
  1135
        #-> (fn thm => add_inst (c, tyco) (c'', Thm.symmetric thm)
haftmann@25462
  1136
        #> primitive_note Thm.internalK (c', thm)
haftmann@25462
  1137
        #> snd
haftmann@25462
  1138
        #> Sign.restore_naming thy))
haftmann@25462
  1139
      end;
haftmann@25462
  1140
  in
haftmann@25462
  1141
    lthy
haftmann@25462
  1142
    |> LocalTheory.theory (fold declare_missing missing_params)
haftmann@25462
  1143
  end;
haftmann@25462
  1144
haftmann@24218
  1145
end;