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