src/Pure/Isar/class.ML
author haftmann
Fri, 24 Aug 2007 14:14:20 +0200
changeset 24423 ae9cd0e92423
parent 24304 69d40a562ba4
child 24435 cabf8cd38258
permissions -rw-r--r--
overloaded definitions accompanied by explicit constants
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24218
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
     1
(*  Title:      Pure/Isar/class.ML
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
     2
    ID:         $Id$
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
     3
    Author:     Florian Haftmann, TU Muenchen
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
     4
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
     5
Type classes derived from primitive axclasses and locales.
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
     6
*)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
     7
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
     8
signature CLASS =
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
     9
sig
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    10
  val fork_mixfix: bool -> string option -> mixfix -> mixfix * mixfix
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    11
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    12
  val axclass_cmd: bstring * xstring list
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    13
    -> ((bstring * Attrib.src list) * string list) list -> theory -> class * theory
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    14
  val class: bstring -> class list -> Element.context_i Locale.element list
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    15
    -> string list -> theory -> string * Proof.context
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    16
  val class_cmd: bstring -> string list -> Element.context Locale.element list
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    17
    -> string list -> theory -> string * Proof.context
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24304
diff changeset
    18
  val class_of_locale: theory -> string -> class option
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24304
diff changeset
    19
  val add_const_in_class: string -> (string * term) * Syntax.mixfix
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24304
diff changeset
    20
    -> theory -> theory
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24304
diff changeset
    21
24218
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    22
  val instance_arity: arity list -> ((bstring * Attrib.src list) * term) list
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24304
diff changeset
    23
    -> (thm list -> theory -> theory)
24218
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    24
    -> theory -> Proof.state
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    25
  val instance_arity_cmd: (bstring * string list * string) list
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    26
    -> ((bstring * Attrib.src list) * string) list
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24304
diff changeset
    27
    -> (thm list -> theory -> theory)
24218
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    28
    -> theory -> Proof.state
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    29
  val prove_instance_arity: tactic -> arity list
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    30
    -> ((bstring * Attrib.src list) * term) list
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24304
diff changeset
    31
    -> theory -> thm list * theory
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24304
diff changeset
    32
  val unoverload: theory -> conv
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24304
diff changeset
    33
  val overload: theory -> conv
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24304
diff changeset
    34
  val unoverload_const: theory -> string * typ -> string
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24304
diff changeset
    35
  val inst_const: theory -> string * string -> string
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24304
diff changeset
    36
  val param_const: theory -> string -> (string * string) option
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24304
diff changeset
    37
  val intro_classes_tac: thm list -> tactic
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24304
diff changeset
    38
  val default_intro_classes_tac: thm list -> tactic
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24304
diff changeset
    39
24218
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    40
  val instance_class: class * class -> theory -> Proof.state
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    41
  val instance_class_cmd: string * string -> theory -> Proof.state
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    42
  val instance_sort: class * sort -> theory -> Proof.state
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    43
  val instance_sort_cmd: string * string -> theory -> Proof.state
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    44
  val prove_instance_sort: tactic -> class * sort -> theory -> theory
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    45
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    46
  val print_classes: theory -> unit
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    47
end;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    48
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    49
structure Class : CLASS =
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    50
struct
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    51
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    52
(** auxiliary **)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    53
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    54
fun fork_mixfix is_loc some_class mx =
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    55
  let
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    56
    val mx' = Syntax.unlocalize_mixfix mx;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    57
    val mx_global = if is_some some_class orelse (is_loc andalso mx = mx')
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    58
      then NoSyn else mx';
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    59
    val mx_local = if is_loc then mx else NoSyn;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    60
  in (mx_global, mx_local) end;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    61
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    62
fun axclass_cmd (class, raw_superclasses) raw_specs thy =
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    63
  let
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    64
    val ctxt = ProofContext.init thy;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    65
    val superclasses = map (Sign.read_class thy) raw_superclasses;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    66
    val name_atts = map ((apsnd o map) (Attrib.attribute thy) o fst) raw_specs;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    67
    val axiomss = ProofContext.read_propp (ctxt, map (map (rpair []) o snd) raw_specs)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    68
      |> snd
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    69
      |> (map o map) fst;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    70
  in AxClass.define_class (class, superclasses) [] (name_atts ~~ axiomss) thy end;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    71
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    72
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    73
(** axclasses with implicit parameter handling **)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    74
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    75
(* axclass instances *)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    76
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    77
local
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    78
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    79
fun gen_instance mk_prop add_thm after_qed insts thy =
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    80
  let
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    81
    fun after_qed' results =
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    82
      ProofContext.theory ((fold o fold) add_thm results #> after_qed);
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    83
  in
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    84
    thy
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    85
    |> ProofContext.init
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    86
    |> Proof.theorem_i NONE after_qed' ((map (fn t => [(t, [])]) o maps (mk_prop thy)) insts)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    87
  end;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    88
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    89
in
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    90
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    91
val axclass_instance_arity =
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    92
  gen_instance (Logic.mk_arities oo Sign.cert_arity) AxClass.add_arity;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    93
val axclass_instance_sort =
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    94
  gen_instance (single oo (Logic.mk_classrel oo AxClass.cert_classrel))
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    95
    AxClass.add_classrel I o single;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    96
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    97
end; (*local*)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    98
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    99
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   100
(* introducing axclasses with implicit parameter handling *)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   101
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   102
fun axclass_params (name, raw_superclasses) raw_consts raw_dep_axioms other_consts thy =
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   103
  let
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   104
    val superclasses = map (Sign.certify_class thy) raw_superclasses;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   105
    val consts = (map o apfst o apsnd) (Sign.certify_typ thy) raw_consts;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   106
    val prefix = Logic.const_of_class name;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   107
    fun mk_const_name c = NameSpace.map_base (NameSpace.append prefix)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   108
      (Sign.full_name thy c);
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   109
    fun add_const ((c, ty), syn) =
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   110
      Sign.add_consts_authentic [(c, ty, syn)]
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   111
      #> pair (mk_const_name c, ty);
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   112
    fun mk_axioms cs thy =
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   113
      raw_dep_axioms thy cs
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   114
      |> (map o apsnd o map) (Sign.cert_prop thy)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   115
      |> rpair thy;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   116
    fun add_constraint class (c, ty) =
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   117
      Sign.add_const_constraint_i (c, SOME
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   118
        (Term.map_type_tfree (fn (v, _) => TFree (v, [class])) ty));
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   119
  in
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   120
    thy
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   121
    |> Theory.add_path prefix
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   122
    |> fold_map add_const consts
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   123
    ||> Theory.restore_naming thy
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   124
    |-> (fn cs => mk_axioms cs
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   125
    #-> (fn axioms_prop => AxClass.define_class (name, superclasses)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   126
           (map fst cs @ other_consts) axioms_prop
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   127
    #-> (fn class => `(fn thy => AxClass.get_definition thy class)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   128
    #-> (fn {intro, axioms, ...} => fold (add_constraint class) cs
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   129
    #> pair (class, ((intro, (map Thm.prop_of axioms, axioms)), cs))))))
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   130
  end;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   131
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   132
24304
69d40a562ba4 explicit constants for overloaded definitions
haftmann
parents: 24276
diff changeset
   133
(* explicit constants for overloaded definitions *)
69d40a562ba4 explicit constants for overloaded definitions
haftmann
parents: 24276
diff changeset
   134
69d40a562ba4 explicit constants for overloaded definitions
haftmann
parents: 24276
diff changeset
   135
structure InstData = TheoryDataFun
69d40a562ba4 explicit constants for overloaded definitions
haftmann
parents: 24276
diff changeset
   136
(
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24304
diff changeset
   137
  type T = (string * thm) Symtab.table Symtab.table * (string * string) Symtab.table;
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24304
diff changeset
   138
    (*constant name ~> type constructor ~> (constant name, equation),
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24304
diff changeset
   139
        constant name ~> (constant name, type constructor)*)
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24304
diff changeset
   140
  val empty = (Symtab.empty, Symtab.empty);
24304
69d40a562ba4 explicit constants for overloaded definitions
haftmann
parents: 24276
diff changeset
   141
  val copy = I;
69d40a562ba4 explicit constants for overloaded definitions
haftmann
parents: 24276
diff changeset
   142
  val extend = I;
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24304
diff changeset
   143
  fun merge _ ((taba1, tabb1), (taba2, tabb2)) =
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24304
diff changeset
   144
    (Symtab.join (K (Symtab.merge (K true))) (taba1, taba2),
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24304
diff changeset
   145
      Symtab.merge (K true) (tabb1, tabb2));
24304
69d40a562ba4 explicit constants for overloaded definitions
haftmann
parents: 24276
diff changeset
   146
);
69d40a562ba4 explicit constants for overloaded definitions
haftmann
parents: 24276
diff changeset
   147
69d40a562ba4 explicit constants for overloaded definitions
haftmann
parents: 24276
diff changeset
   148
fun inst_thms f thy =
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24304
diff changeset
   149
  (Symtab.fold (Symtab.fold (cons o f o snd o snd) o snd) o fst) (InstData.get thy) [];
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24304
diff changeset
   150
fun add_inst (c, tyco) inst = (InstData.map o apfst o Symtab.map_default (c, Symtab.empty))
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24304
diff changeset
   151
  (Symtab.update_new (tyco, inst))
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24304
diff changeset
   152
  #> (InstData.map o apsnd) (Symtab.update_new (fst inst, (c, tyco)));
24304
69d40a562ba4 explicit constants for overloaded definitions
haftmann
parents: 24276
diff changeset
   153
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24304
diff changeset
   154
fun unoverload thy thm = MetaSimplifier.rewrite false (inst_thms I thy) thm;
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24304
diff changeset
   155
fun overload thy thm = MetaSimplifier.rewrite false (inst_thms symmetric thy) thm;
24304
69d40a562ba4 explicit constants for overloaded definitions
haftmann
parents: 24276
diff changeset
   156
69d40a562ba4 explicit constants for overloaded definitions
haftmann
parents: 24276
diff changeset
   157
fun inst_const thy (c, tyco) =
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24304
diff changeset
   158
  (fst o the o Symtab.lookup ((the o Symtab.lookup (fst (InstData.get thy))) c)) tyco;
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24304
diff changeset
   159
fun unoverload_const thy (c_ty as (c, _)) =
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24304
diff changeset
   160
  case AxClass.class_of_param thy c
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24304
diff changeset
   161
   of SOME class => (case Sign.const_typargs thy c_ty
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24304
diff changeset
   162
       of [Type (tyco, _)] =>
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24304
diff changeset
   163
            (case Symtab.lookup ((the o Symtab.lookup (fst (InstData.get thy))) c) tyco
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24304
diff changeset
   164
             of SOME (c, _) => c
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24304
diff changeset
   165
              | NONE => c)
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24304
diff changeset
   166
        | [_] => c)
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24304
diff changeset
   167
    | NONE => c;
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24304
diff changeset
   168
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24304
diff changeset
   169
val param_const = Symtab.lookup o snd o InstData.get;
24304
69d40a562ba4 explicit constants for overloaded definitions
haftmann
parents: 24276
diff changeset
   170
69d40a562ba4 explicit constants for overloaded definitions
haftmann
parents: 24276
diff changeset
   171
fun add_inst_def (class, tyco) (c, ty) thy =
69d40a562ba4 explicit constants for overloaded definitions
haftmann
parents: 24276
diff changeset
   172
  let
69d40a562ba4 explicit constants for overloaded definitions
haftmann
parents: 24276
diff changeset
   173
    val tyco_base = NameSpace.base tyco;
69d40a562ba4 explicit constants for overloaded definitions
haftmann
parents: 24276
diff changeset
   174
    val name_inst = NameSpace.base class ^ "_" ^ tyco_base ^ "_inst";
69d40a562ba4 explicit constants for overloaded definitions
haftmann
parents: 24276
diff changeset
   175
    val c_inst_base = NameSpace.base c ^ "_" ^ tyco_base;
69d40a562ba4 explicit constants for overloaded definitions
haftmann
parents: 24276
diff changeset
   176
  in
69d40a562ba4 explicit constants for overloaded definitions
haftmann
parents: 24276
diff changeset
   177
    thy
69d40a562ba4 explicit constants for overloaded definitions
haftmann
parents: 24276
diff changeset
   178
    |> Sign.sticky_prefix name_inst
69d40a562ba4 explicit constants for overloaded definitions
haftmann
parents: 24276
diff changeset
   179
    |> Sign.add_consts_i [(c_inst_base, ty, Syntax.NoSyn)]
69d40a562ba4 explicit constants for overloaded definitions
haftmann
parents: 24276
diff changeset
   180
    |> `(fn thy => Sign.full_name thy c_inst_base)
69d40a562ba4 explicit constants for overloaded definitions
haftmann
parents: 24276
diff changeset
   181
    |-> (fn c_inst => PureThy.add_defs_i true
69d40a562ba4 explicit constants for overloaded definitions
haftmann
parents: 24276
diff changeset
   182
          [((Thm.def_name c_inst_base, Logic.mk_equals (Const (c_inst, ty), Const (c, ty))), [])]
69d40a562ba4 explicit constants for overloaded definitions
haftmann
parents: 24276
diff changeset
   183
    #-> (fn [def] => add_inst (c, tyco) (c_inst, symmetric def))
69d40a562ba4 explicit constants for overloaded definitions
haftmann
parents: 24276
diff changeset
   184
    #> Sign.restore_naming thy)
69d40a562ba4 explicit constants for overloaded definitions
haftmann
parents: 24276
diff changeset
   185
  end;
69d40a562ba4 explicit constants for overloaded definitions
haftmann
parents: 24276
diff changeset
   186
69d40a562ba4 explicit constants for overloaded definitions
haftmann
parents: 24276
diff changeset
   187
fun add_inst_def' (class, tyco) (c, ty) thy =
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24304
diff changeset
   188
  if case Symtab.lookup (fst (InstData.get thy)) c
24304
69d40a562ba4 explicit constants for overloaded definitions
haftmann
parents: 24276
diff changeset
   189
   of NONE => true
69d40a562ba4 explicit constants for overloaded definitions
haftmann
parents: 24276
diff changeset
   190
    | SOME tab => is_none (Symtab.lookup tab tyco)
69d40a562ba4 explicit constants for overloaded definitions
haftmann
parents: 24276
diff changeset
   191
  then add_inst_def (class, tyco) (c, Logic.unvarifyT ty) thy
69d40a562ba4 explicit constants for overloaded definitions
haftmann
parents: 24276
diff changeset
   192
  else thy;
69d40a562ba4 explicit constants for overloaded definitions
haftmann
parents: 24276
diff changeset
   193
69d40a562ba4 explicit constants for overloaded definitions
haftmann
parents: 24276
diff changeset
   194
fun add_def ((class, tyco), ((name, prop), atts)) thy =
69d40a562ba4 explicit constants for overloaded definitions
haftmann
parents: 24276
diff changeset
   195
  let
69d40a562ba4 explicit constants for overloaded definitions
haftmann
parents: 24276
diff changeset
   196
    val ((lhs as Const (c, ty), args), rhs) = (apfst Term.strip_comb o Logic.dest_equals) prop;
69d40a562ba4 explicit constants for overloaded definitions
haftmann
parents: 24276
diff changeset
   197
    fun add_inst' def ([], (Const (c_inst, ty))) =
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24304
diff changeset
   198
          if forall (fn TFree _ => true | _ => false) (Sign.const_typargs thy (c_inst, ty))
24304
69d40a562ba4 explicit constants for overloaded definitions
haftmann
parents: 24276
diff changeset
   199
          then add_inst (c, tyco) (c_inst, def)
69d40a562ba4 explicit constants for overloaded definitions
haftmann
parents: 24276
diff changeset
   200
          else add_inst_def (class, tyco) (c, ty)
69d40a562ba4 explicit constants for overloaded definitions
haftmann
parents: 24276
diff changeset
   201
      | add_inst' _ t = add_inst_def (class, tyco) (c, ty);
69d40a562ba4 explicit constants for overloaded definitions
haftmann
parents: 24276
diff changeset
   202
  in
69d40a562ba4 explicit constants for overloaded definitions
haftmann
parents: 24276
diff changeset
   203
    thy
69d40a562ba4 explicit constants for overloaded definitions
haftmann
parents: 24276
diff changeset
   204
    |> PureThy.add_defs_i true [((name, prop), map (Attrib.attribute thy) atts)]
69d40a562ba4 explicit constants for overloaded definitions
haftmann
parents: 24276
diff changeset
   205
    |-> (fn [def] => add_inst' def (args, rhs) #> pair def)
69d40a562ba4 explicit constants for overloaded definitions
haftmann
parents: 24276
diff changeset
   206
  end;
69d40a562ba4 explicit constants for overloaded definitions
haftmann
parents: 24276
diff changeset
   207
69d40a562ba4 explicit constants for overloaded definitions
haftmann
parents: 24276
diff changeset
   208
24218
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   209
(* instances with implicit parameter handling *)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   210
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   211
local
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   212
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   213
fun gen_read_def thy prep_att read_def ((raw_name, raw_atts), raw_t) =
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   214
  let
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   215
    val (_, t) = read_def thy (raw_name, raw_t);
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   216
    val ((c, ty), _) = Sign.cert_def (Sign.pp thy) t;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   217
    val atts = map (prep_att thy) raw_atts;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   218
    val insts = Consts.typargs (Sign.consts_of thy) (c, ty);
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   219
    val name = case raw_name
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   220
     of "" => NONE
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   221
      | _ => SOME raw_name;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   222
  in (c, (insts, ((name, t), atts))) end;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   223
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   224
fun read_def_cmd thy = gen_read_def thy Attrib.intern_src Theory.read_axm;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   225
fun read_def thy = gen_read_def thy (K I) (K I);
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   226
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24304
diff changeset
   227
fun gen_instance_arity prep_arity read_def do_proof raw_arities raw_defs after_qed theory =
24218
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   228
  let
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   229
    val arities = map (prep_arity theory) raw_arities;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   230
    val _ = if null arities then error "at least one arity must be given" else ();
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   231
    val _ = case (duplicates (op =) o map #1) arities
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   232
     of [] => ()
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   233
      | dupl_tycos => error ("type constructors occur more than once in arities: "
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   234
          ^ (commas o map quote) dupl_tycos);
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   235
    val super_sort = (Graph.all_succs o #classes o Sorts.rep_algebra o Sign.classes_of) theory
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   236
    fun get_consts_class tyco ty class =
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   237
      let
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   238
        val cs = (these o Option.map snd o try (AxClass.params_of_class theory)) class;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   239
        val subst_ty = map_type_tfree (K ty);
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   240
      in
24304
69d40a562ba4 explicit constants for overloaded definitions
haftmann
parents: 24276
diff changeset
   241
        map (fn (c, ty) => (c, ((class, tyco), subst_ty ty))) cs
24218
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   242
      end;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   243
    fun get_consts_sort (tyco, asorts, sort) =
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   244
      let
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   245
        val ty = Type (tyco, map (fn (v, sort) => TVar ((v, 0), sort)) (Name.names Name.context "'a" asorts))
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   246
      in maps (get_consts_class tyco ty) (super_sort sort) end;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   247
    val cs = maps get_consts_sort arities;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   248
    fun mk_typnorm thy (ty, ty_sc) =
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   249
      case try (Sign.typ_match thy (Logic.varifyT ty_sc, ty)) Vartab.empty
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   250
       of SOME env => SOME (Logic.varifyT #> Envir.typ_subst_TVars env #> Logic.unvarifyT)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   251
        | NONE => NONE;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   252
    fun read_defs defs cs thy_read =
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   253
      let
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   254
        fun read raw_def cs =
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   255
          let
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   256
            val (c, (inst, ((name_opt, t), atts))) = read_def thy_read raw_def;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   257
            val ty = Consts.instance (Sign.consts_of thy_read) (c, inst);
24304
69d40a562ba4 explicit constants for overloaded definitions
haftmann
parents: 24276
diff changeset
   258
            val ((class, tyco), ty') = case AList.lookup (op =) cs c
24218
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   259
             of NONE => error ("illegal definition for constant " ^ quote c)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   260
              | SOME class_ty => class_ty;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   261
            val name = case name_opt
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   262
             of NONE => Thm.def_name (Logic.name_arity (tyco, [], c))
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   263
              | SOME name => name;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   264
            val t' = case mk_typnorm thy_read (ty', ty)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   265
             of NONE => error ("illegal definition for constant " ^
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   266
                  quote (c ^ "::" ^ setmp show_sorts true
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   267
                    (Sign.string_of_typ thy_read) ty))
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   268
              | SOME norm => map_types norm t
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   269
          in (((class, tyco), ((name, t'), atts)), AList.delete (op =) c cs) end;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   270
      in fold_map read defs cs end;
24304
69d40a562ba4 explicit constants for overloaded definitions
haftmann
parents: 24276
diff changeset
   271
    val (defs, other_cs) = read_defs raw_defs cs
24218
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   272
      (fold Sign.primitive_arity arities (Theory.copy theory));
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   273
    fun get_remove_contraint c thy =
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   274
      let
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   275
        val ty = Sign.the_const_constraint thy c;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   276
      in
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   277
        thy
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   278
        |> Sign.add_const_constraint_i (c, NONE)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   279
        |> pair (c, Logic.unvarifyT ty)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   280
      end;
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24304
diff changeset
   281
    fun after_qed' cs defs =
24304
69d40a562ba4 explicit constants for overloaded definitions
haftmann
parents: 24276
diff changeset
   282
      fold Sign.add_const_constraint_i (map (apsnd SOME) cs)
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24304
diff changeset
   283
      #> after_qed defs;
24218
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   284
  in
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   285
    theory
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   286
    |> fold_map get_remove_contraint (map fst cs |> distinct (op =))
24304
69d40a562ba4 explicit constants for overloaded definitions
haftmann
parents: 24276
diff changeset
   287
    ||>> fold_map add_def defs
69d40a562ba4 explicit constants for overloaded definitions
haftmann
parents: 24276
diff changeset
   288
    ||> fold (fn (c, ((class, tyco), ty)) => add_inst_def' (class, tyco) (c, ty)) other_cs
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24304
diff changeset
   289
    |-> (fn (cs, defs) => do_proof (after_qed' cs defs) arities defs)
24218
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   290
  end;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   291
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   292
fun instance_arity_cmd' do_proof = gen_instance_arity Sign.read_arity read_def_cmd do_proof;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   293
fun instance_arity' do_proof = gen_instance_arity Sign.cert_arity read_def do_proof;
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24304
diff changeset
   294
fun tactic_proof tac f arities defs =
24218
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   295
  fold (fn arity => AxClass.prove_arity arity tac) arities
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24304
diff changeset
   296
  #> f
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24304
diff changeset
   297
  #> pair defs;
24218
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   298
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   299
in
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   300
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24304
diff changeset
   301
val instance_arity_cmd = instance_arity_cmd'
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24304
diff changeset
   302
  (fn f => fn arities => fn defs => axclass_instance_arity f arities);
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24304
diff changeset
   303
val instance_arity = instance_arity'
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24304
diff changeset
   304
  (fn f => fn arities => fn defs => axclass_instance_arity f arities);
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24304
diff changeset
   305
fun prove_instance_arity tac arities defs =
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24304
diff changeset
   306
  instance_arity' (tactic_proof tac) arities defs (K I);
24218
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   307
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   308
end; (*local*)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   309
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   310
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   311
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   312
(** combining locales and axclasses **)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   313
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   314
(* theory data *)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   315
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   316
datatype class_data = ClassData of {
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   317
  locale: string,
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   318
  consts: (string * string) list
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   319
    (*locale parameter ~> toplevel theory constant*),
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   320
  v: string option,
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   321
  intro: thm
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   322
} * thm list (*derived defs*);
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   323
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   324
fun rep_classdata (ClassData c) = c;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   325
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   326
fun merge_pair f1 f2 ((x1, y1), (x2, y2)) = (f1 (x1, x2), f2 (y1, y2));
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   327
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   328
structure ClassData = TheoryDataFun
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   329
(
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   330
  type T = class_data Graph.T * class Symtab.table (*locale name ~> class name*);
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   331
  val empty = (Graph.empty, Symtab.empty);
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   332
  val copy = I;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   333
  val extend = I;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   334
  fun merge _ = merge_pair (Graph.merge (K true)) (Symtab.merge (K true));
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   335
);
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   336
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   337
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   338
(* queries *)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   339
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   340
val lookup_class_data = Option.map rep_classdata oo try o Graph.get_node o fst o ClassData.get;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   341
fun class_of_locale thy = Symtab.lookup ((snd o ClassData.get) thy);
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   342
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   343
fun the_class_data thy class =
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   344
  case lookup_class_data thy class
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   345
    of NONE => error ("undeclared class " ^ quote class)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   346
     | SOME data => data;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   347
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   348
val ancestry = Graph.all_succs o fst o ClassData.get;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   349
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   350
fun param_map thy =
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   351
  let
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   352
    fun params class =
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   353
      let
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   354
        val const_typs = (#params o AxClass.get_definition thy) class;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   355
        val const_names = (#consts o fst o the_class_data thy) class;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   356
      in
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   357
        (map o apsnd) (fn c => (c, (the o AList.lookup (op =) const_typs) c)) const_names
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   358
      end;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   359
  in maps params o ancestry thy end;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   360
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   361
fun these_defs thy = maps (these o Option.map snd o lookup_class_data thy) o ancestry thy;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   362
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   363
fun these_intros thy =
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   364
  Graph.fold (fn (_, (data, _)) => insert Thm.eq_thm ((#intro o fst o rep_classdata) data))
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   365
    ((fst o ClassData.get) thy) [];
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   366
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   367
fun print_classes thy =
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   368
  let
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   369
    val algebra = Sign.classes_of thy;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   370
    val arities =
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   371
      Symtab.empty
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   372
      |> Symtab.fold (fn (tyco, arities) => fold (fn (class, _) =>
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   373
           Symtab.map_default (class, []) (insert (op =) tyco)) arities)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   374
             ((#arities o Sorts.rep_algebra) algebra);
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   375
    val the_arities = these o Symtab.lookup arities;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   376
    fun mk_arity class tyco =
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   377
      let
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   378
        val Ss = Sorts.mg_domain algebra tyco [class];
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   379
      in Sign.pretty_arity thy (tyco, Ss, [class]) end;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   380
    fun mk_param (c, ty) = Pretty.str (Sign.extern_const thy c ^ " :: "
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   381
      ^ setmp show_sorts false (Sign.string_of_typ thy o Type.strip_sorts) ty);
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   382
    fun mk_entry class = (Pretty.block o Pretty.fbreaks o map_filter I) [
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   383
      (SOME o Pretty.str) ("class " ^ class ^ ":"),
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   384
      (SOME o Pretty.block) [Pretty.str "supersort: ",
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   385
        (Sign.pretty_sort thy o Sign.certify_sort thy o Sign.super_classes thy) class],
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   386
      Option.map (Pretty.str o prefix "locale: " o #locale o fst) (lookup_class_data thy class),
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   387
      ((fn [] => NONE | ps => (SOME o Pretty.block o Pretty.fbreaks) (Pretty.str "parameters:" :: ps)) o map mk_param
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   388
        o these o Option.map #params o try (AxClass.get_definition thy)) class,
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   389
      (SOME o Pretty.block o Pretty.breaks) [
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   390
        Pretty.str "instances:",
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   391
        Pretty.list "" "" (map (mk_arity class) (the_arities class))
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   392
      ]
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   393
    ]
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   394
  in
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   395
    (Pretty.writeln o Pretty.chunks o separate (Pretty.str "") o map mk_entry o Sorts.all_classes)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   396
      algebra
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   397
  end;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   398
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   399
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   400
(* updaters *)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   401
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   402
fun add_class_data ((class, superclasses), (locale, consts, v, intro)) =
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   403
  ClassData.map (fn (gr, tab) => (
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   404
    gr
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   405
    |> Graph.new_node (class, ClassData ({ locale = locale, consts = consts,
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   406
         v = v, intro = intro }, []))
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   407
    |> fold (curry Graph.add_edge class) superclasses,
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   408
    tab
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   409
    |> Symtab.update (locale, class)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   410
  ));
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   411
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   412
fun add_class_const_thm (class, thm) = (ClassData.map o apfst o Graph.map_node class)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   413
  (fn ClassData (data, thms) => ClassData (data, thm :: thms));
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   414
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   415
(* tactics and methods *)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   416
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   417
fun intro_classes_tac facts st =
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   418
  let
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   419
    val thy = Thm.theory_of_thm st;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   420
    val classes = Sign.all_classes thy;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   421
    val class_trivs = map (Thm.class_triv thy) classes;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   422
    val class_intros = these_intros thy;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   423
    fun add_axclass_intro class =
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   424
      case try (AxClass.get_definition thy) class of SOME {intro, ...} => cons intro | _ => I;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   425
    val axclass_intros = fold add_axclass_intro classes [];
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   426
  in
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   427
    st
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   428
    |> ((ALLGOALS (Method.insert_tac facts THEN'
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   429
          REPEAT_ALL_NEW (resolve_tac (class_trivs @ class_intros @ axclass_intros))))
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   430
            THEN Tactic.distinct_subgoals_tac)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   431
  end;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   432
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   433
fun default_intro_classes_tac [] = intro_classes_tac []
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   434
  | default_intro_classes_tac _ = Tactical.no_tac;    (*no error message!*)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   435
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   436
fun default_tac rules ctxt facts =
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   437
  HEADGOAL (Method.some_rule_tac rules ctxt facts) ORELSE
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   438
    default_intro_classes_tac facts;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   439
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   440
val _ = Context.add_setup (Method.add_methods
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   441
 [("intro_classes", Method.no_args (Method.METHOD intro_classes_tac),
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   442
    "back-chain introduction rules of classes"),
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   443
  ("default", Method.thms_ctxt_args (Method.METHOD oo default_tac),
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   444
    "apply some intro/elim rule")]);
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   445
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   446
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   447
(* tactical interfaces to locale commands *)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   448
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   449
fun prove_interpretation tac prfx_atts expr insts thy =
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   450
  thy
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   451
  |> Locale.interpretation_i I prfx_atts expr insts
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   452
  |> Proof.global_terminal_proof (Method.Basic (K (Method.SIMPLE_METHOD tac), Position.none), NONE)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   453
  |> ProofContext.theory_of;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   454
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   455
fun prove_interpretation_in tac after_qed (name, expr) thy =
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   456
  thy
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   457
  |> Locale.interpretation_in_locale (ProofContext.theory after_qed) (name, expr)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   458
  |> Proof.global_terminal_proof (Method.Basic (K (Method.SIMPLE_METHOD tac), Position.none), NONE)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   459
  |> ProofContext.theory_of;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   460
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   461
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   462
(* constructing class introduction and other rules from axclass and locale rules *)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   463
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   464
fun mk_instT class = Symtab.empty
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   465
  |> Symtab.update (AxClass.param_tyvarname, TFree (AxClass.param_tyvarname, [class]));
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   466
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   467
fun mk_inst class param_names cs =
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   468
  Symtab.empty
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   469
  |> fold2 (fn v => fn (c, ty) => Symtab.update (v, Const
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   470
       (c, Term.map_type_tfree (fn (v, _) => TFree (v, [class])) ty))) param_names cs;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   471
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   472
fun OF_LAST thm1 thm2 =
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   473
  let
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   474
    val n = (length o Logic.strip_imp_prems o prop_of) thm2;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   475
  in (thm1 RSN (n, thm2)) end;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   476
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   477
fun strip_all_ofclass thy sort =
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   478
  let
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   479
    val typ = TVar ((AxClass.param_tyvarname, 0), sort);
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   480
    fun prem_inclass t =
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   481
      case Logic.strip_imp_prems t
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   482
       of ofcls :: _ => try Logic.dest_inclass ofcls
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   483
        | [] => NONE;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   484
    fun strip_ofclass class thm =
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   485
      thm OF (fst o AxClass.of_sort thy (typ, [class])) AxClass.cache;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   486
    fun strip thm = case (prem_inclass o Thm.prop_of) thm
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   487
     of SOME (_, class) => thm |> strip_ofclass class |> strip
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   488
      | NONE => thm;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   489
  in strip end;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   490
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   491
fun class_intro thy locale class sups =
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   492
  let
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   493
    fun class_elim class =
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   494
      case (map Drule.unconstrainTs o #axioms o AxClass.get_definition thy) class
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   495
       of [thm] => SOME thm
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   496
        | [] => NONE;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   497
    val pred_intro = case Locale.intros thy locale
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   498
     of ([ax_intro], [intro]) => intro |> OF_LAST ax_intro |> SOME
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   499
      | ([intro], []) => SOME intro
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   500
      | ([], [intro]) => SOME intro
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   501
      | _ => NONE;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   502
    val pred_intro' = pred_intro
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   503
      |> Option.map (fn intro => intro OF map_filter class_elim sups);
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   504
    val class_intro = (#intro o AxClass.get_definition thy) class;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   505
    val raw_intro = case pred_intro'
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   506
     of SOME pred_intro => class_intro |> OF_LAST pred_intro
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   507
      | NONE => class_intro;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   508
    val sort = Sign.super_classes thy class;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   509
    val typ = TVar ((AxClass.param_tyvarname, 0), sort);
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   510
    val defs = these_defs thy sups;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   511
  in
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   512
    raw_intro
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   513
    |> Drule.instantiate' [SOME (Thm.ctyp_of thy typ)] []
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   514
    |> strip_all_ofclass thy sort
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   515
    |> Thm.strip_shyps
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   516
    |> MetaSimplifier.rewrite_rule defs
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   517
    |> Drule.unconstrainTs
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   518
  end;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   519
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   520
fun interpretation_in_rule thy (class1, class2) =
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   521
  let
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   522
    val (params, consts) = split_list (param_map thy [class1]);
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   523
    (*FIXME also remember this at add_class*)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   524
    fun mk_axioms class =
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   525
      let
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   526
        val name_locale = (#locale o fst o the_class_data thy) class;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   527
        val inst = mk_inst class params consts;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   528
      in
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   529
        Locale.global_asms_of thy name_locale
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   530
        |> maps snd
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   531
        |> (map o map_aterms) (fn Free (s, _) => (the o Symtab.lookup inst) s | t => t)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   532
        |> (map o map_types o map_atyps) (fn TFree _ => TFree ("'a", [class1]) | T => T)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   533
        |> map (ObjectLogic.ensure_propT thy)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   534
      end;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   535
    val (prems, concls) = pairself mk_axioms (class1, class2);
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   536
  in
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   537
    Goal.prove_global thy [] prems (Logic.mk_conjunction_list concls)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   538
      (Locale.intro_locales_tac true (ProofContext.init thy))
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   539
  end;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   540
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   541
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   542
(* classes *)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   543
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   544
local
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   545
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   546
fun read_param thy raw_t =
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   547
  let
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   548
    val t = Sign.read_term thy raw_t
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   549
  in case try dest_Const t
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   550
   of SOME (c, _) => c
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   551
    | NONE => error ("Not a constant: " ^ Sign.string_of_term thy t)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   552
  end;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   553
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   554
fun gen_class add_locale prep_class prep_param bname
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   555
    raw_supclasses raw_elems raw_other_consts thy =
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   556
  let
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   557
    (*FIXME need proper concept for reading locale statements*)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   558
    fun subst_classtyvar (_, _) =
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   559
          TFree (AxClass.param_tyvarname, [])
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   560
      | subst_classtyvar (v, sort) =
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   561
          error ("Sort constraint illegal in type class, for type variable " ^ v ^ "::" ^ Sign.string_of_sort thy sort);
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   562
    (*val subst_classtyvars = Element.map_ctxt {name = I, var = I, term = I,
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   563
      typ = Term.map_type_tfree subst_classtyvar, fact = I, attrib = I};*)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   564
    val other_consts = map (prep_param thy) raw_other_consts;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   565
    val (elems, includes) = fold_rev (fn Locale.Elem e => apfst (cons e)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   566
      | Locale.Expr i => apsnd (cons i)) raw_elems ([], []);
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   567
    val supclasses = map (prep_class thy) raw_supclasses;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   568
    val sups = filter (is_some o lookup_class_data thy) supclasses
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   569
      |> Sign.certify_sort thy;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   570
    val supsort = Sign.certify_sort thy supclasses;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   571
    val suplocales = map (Locale.Locale o #locale o fst o the_class_data thy) sups;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   572
    val supexpr = Locale.Merge (suplocales @ includes);
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   573
    val supparams = (map fst o Locale.parameters_of_expr thy)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   574
      (Locale.Merge suplocales);
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   575
    val supconsts = AList.make (the o AList.lookup (op =) (param_map thy sups))
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   576
      (map fst supparams);
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   577
    (*val elems_constrains = map
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   578
      (Element.Constrains o apsnd (Term.map_type_tfree subst_classtyvar)) supparams;*)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   579
    fun mk_tyvar (_, sort) = TFree (AxClass.param_tyvarname,
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   580
      if Sign.subsort thy (supsort, sort) then sort else error
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   581
        ("Sort " ^ Sign.string_of_sort thy sort
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   582
          ^ " is less general than permitted least general sort "
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   583
          ^ Sign.string_of_sort thy supsort));
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   584
    fun extract_params thy name_locale =
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   585
      let
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   586
        val params = Locale.parameters_of thy name_locale;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   587
        val v = case (maps typ_tfrees o map (snd o fst)) params
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   588
         of (v, _) :: _ => SOME v
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   589
          | _ => NONE;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   590
      in
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   591
        (v, (map (fst o fst) params, params
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   592
        |> (map o apfst o apsnd o Term.map_type_tfree) mk_tyvar
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   593
        |> (map o apsnd) (fork_mixfix true NONE #> fst)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   594
        |> chop (length supconsts)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   595
        |> snd))
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   596
      end;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   597
    fun extract_assumes name_locale params thy cs =
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   598
      let
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   599
        val consts = supconsts @ (map (fst o fst) params ~~ cs);
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   600
        fun subst (Free (c, ty)) =
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   601
              Const ((fst o the o AList.lookup (op =) consts) c, ty)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   602
          | subst t = t;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   603
        val super_defs = these_defs thy sups;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   604
        fun prep_asm ((name, atts), ts) =
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   605
          ((NameSpace.base name, map (Attrib.attribute thy) atts),
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   606
            (map o map_aterms) ((*MetaSimplifier.rewrite_term thy super_defs [] o *)subst) ts);
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   607
      in
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   608
        Locale.global_asms_of thy name_locale
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   609
        |> map prep_asm
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   610
      end;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   611
    fun note_intro name_axclass class_intro =
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   612
      PureThy.note_thmss_qualified "" ((Logic.const_of_class o NameSpace.base) name_axclass)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   613
        [(("intro", []), [([class_intro], [])])]
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   614
      #> snd
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   615
  in
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   616
    thy
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   617
    |> add_locale (SOME "") bname supexpr ((*elems_constrains @*) elems)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   618
    |-> (fn name_locale => ProofContext.theory_result (
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   619
      `(fn thy => extract_params thy name_locale)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   620
      #-> (fn (v, (param_names, params)) =>
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   621
        axclass_params (bname, supsort) params (extract_assumes name_locale params) other_consts
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   622
      #-> (fn (name_axclass, ((_, (ax_terms, ax_axioms)), consts)) =>
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   623
        `(fn thy => class_intro thy name_locale name_axclass sups)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   624
      #-> (fn class_intro =>
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   625
        add_class_data ((name_axclass, sups),
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   626
          (name_locale, map (fst o fst) params ~~ map fst consts, v,
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   627
            class_intro))
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   628
        (*FIXME: class_data should already contain data relevant
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   629
          for interpretation; use this later for class target*)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   630
        (*FIXME: general export_fixes which may be parametrized
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   631
          with pieces of an emerging class*)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   632
      #> note_intro name_axclass class_intro
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   633
      #> prove_interpretation ((ALLGOALS o ProofContext.fact_tac) ax_axioms)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   634
          ((false, Logic.const_of_class bname), []) (Locale.Locale name_locale)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   635
          ((mk_instT name_axclass, mk_inst name_axclass param_names (map snd supconsts @ consts)), [])
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   636
      #> pair name_axclass
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   637
      )))))
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   638
  end;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   639
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   640
in
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   641
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   642
val class_cmd = gen_class Locale.add_locale Sign.intern_class read_param;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   643
val class = gen_class Locale.add_locale_i Sign.certify_class (K I);
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   644
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   645
end; (*local*)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   646
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   647
local
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   648
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   649
fun instance_subclass (class1, class2) thy  =
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   650
  let
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   651
    val interp = interpretation_in_rule thy (class1, class2);
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   652
    val ax = #axioms (AxClass.get_definition thy class1);
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   653
    val intro = #intro (AxClass.get_definition thy class2)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   654
      |> Drule.instantiate' [SOME (Thm.ctyp_of thy
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   655
          (TVar ((AxClass.param_tyvarname, 0), [class1])))] [];
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   656
    val thm = 
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   657
      intro
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   658
      |> OF_LAST (interp OF ax)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   659
      |> strip_all_ofclass thy (Sign.super_classes thy class2);
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   660
  in
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   661
    thy |> AxClass.add_classrel thm
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   662
  end;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   663
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   664
fun instance_subsort (class, sort) thy =
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   665
  let
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   666
    val super_sort = (Graph.all_succs o #classes o Sorts.rep_algebra
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   667
      o Sign.classes_of) thy sort;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   668
    val classes = filter_out (fn class' => Sign.subsort thy ([class], [class']))
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   669
      (rev super_sort);
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   670
  in
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   671
    thy |> fold (curry instance_subclass class) classes
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   672
  end;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   673
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   674
fun instance_sort' do_proof (class, sort) theory =
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   675
  let
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   676
    val loc_name = (#locale o fst o the_class_data theory) class;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   677
    val loc_expr =
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   678
      (Locale.Merge o map (Locale.Locale o #locale o fst o the_class_data theory)) sort;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   679
  in
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   680
    theory
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   681
    |> do_proof (instance_subsort (class, sort)) (loc_name, loc_expr)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   682
  end;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   683
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   684
fun gen_instance_sort prep_class prep_sort (raw_class, raw_sort) theory =
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   685
  let
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   686
    val class = prep_class theory raw_class;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   687
    val sort = prep_sort theory raw_sort;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   688
  in
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   689
    theory
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   690
    |> instance_sort' (Locale.interpretation_in_locale o ProofContext.theory) (class, sort)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   691
  end;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   692
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   693
fun gen_instance_class prep_class (raw_class, raw_superclass) theory =
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   694
  let
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   695
    val class = prep_class theory raw_class;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   696
    val superclass = prep_class theory raw_superclass;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   697
  in
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   698
    theory
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   699
    |> axclass_instance_sort (class, superclass)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   700
  end;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   701
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   702
in
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   703
24276
7a0f71fde62c Syntax.global_read_sort;
wenzelm
parents: 24218
diff changeset
   704
val instance_sort_cmd = gen_instance_sort Sign.read_class Syntax.global_read_sort;
24218
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   705
val instance_sort = gen_instance_sort Sign.certify_class Sign.certify_sort;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   706
val prove_instance_sort = instance_sort' o prove_interpretation_in;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   707
val instance_class_cmd = gen_instance_class Sign.read_class;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   708
val instance_class = gen_instance_class Sign.certify_class;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   709
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   710
end; (*local*)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   711
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   712
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   713
(** class target **)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   714
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   715
fun export_fixes thy class =
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   716
  let
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   717
    val v = (#v o fst o the_class_data thy) class;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   718
    val constrain_sort = curry (Sorts.inter_sort (Sign.classes_of thy)) [class];
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   719
    val subst_typ = Term.map_type_tfree (fn var as (w, sort) =>
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   720
      if SOME w = v then TFree (w, constrain_sort sort) else TFree var);
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   721
    val consts = param_map thy [class];
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   722
    fun subst_aterm (t as Free (v, ty)) = (case AList.lookup (op =) consts v
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   723
         of SOME (c, _) => Const (c, ty)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   724
          | NONE => t)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   725
      | subst_aterm t = t;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   726
  in map_types subst_typ #> Term.map_aterms subst_aterm end;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   727
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   728
fun add_const_in_class class ((c, rhs), syn) thy =
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   729
  let
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   730
    val prfx = (Logic.const_of_class o NameSpace.base) class;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   731
    fun mk_name inject c =
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   732
      let
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   733
        val n1 = Sign.full_name thy c;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   734
        val n2 = NameSpace.qualifier n1;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   735
        val n3 = NameSpace.base n1;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   736
      in NameSpace.implode (n2 :: inject @ [n3]) end;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   737
    val abbr' = mk_name [prfx, prfx] c;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   738
    val rhs' = export_fixes thy class rhs;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   739
    val ty' = Term.fastype_of rhs';
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   740
    val def = (c, Logic.mk_equals (Const (mk_name [prfx] c, ty'), rhs'));
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   741
    val (syn', _) = fork_mixfix true NONE syn;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   742
    fun interpret def =
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   743
      let
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   744
        val def' = symmetric def
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   745
        val tac = (ALLGOALS o ProofContext.fact_tac) [def'];
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   746
        val name_locale = (#locale o fst o the_class_data thy) class;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   747
        val def_eq = Thm.prop_of def';
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   748
        val (params, consts) = split_list (param_map thy [class]);
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   749
      in
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   750
        prove_interpretation tac ((false, prfx), []) (Locale.Locale name_locale)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   751
          ((mk_instT class, mk_inst class params consts), [def_eq])
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   752
        #> add_class_const_thm (class, def')
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   753
      end;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   754
  in
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   755
    thy
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   756
    |> Sign.hide_consts_i true [abbr']
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   757
    |> Sign.add_path prfx
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   758
    |> Sign.add_consts_authentic [(c, ty', syn')]
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   759
    |> Sign.parent_path
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   760
    |> Sign.sticky_prefix prfx
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   761
    |> PureThy.add_defs_i false [(def, [])]
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   762
    |-> (fn [def] => interpret def)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   763
    |> Sign.restore_naming thy
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   764
  end;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   765
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   766
end;