src/Pure/Tools/class_package.ML
author haftmann
Sat, 10 Feb 2007 09:26:25 +0100
changeset 22305 0e56750a092b
parent 22302 bf5bdb7f7607
child 22321 e5cddafe2629
permissions -rw-r--r--
changed representation of constants
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
18168
d35daf321b8a class_package - operational view on type classes
haftmann
parents:
diff changeset
     1
(*  Title:      Pure/Tools/class_package.ML
d35daf321b8a class_package - operational view on type classes
haftmann
parents:
diff changeset
     2
    ID:         $Id$
d35daf321b8a class_package - operational view on type classes
haftmann
parents:
diff changeset
     3
    Author:     Florian Haftmann, TU Muenchen
d35daf321b8a class_package - operational view on type classes
haftmann
parents:
diff changeset
     4
19468
0afdd5023bfc Sign.arity_sorts;
wenzelm
parents: 19456
diff changeset
     5
Type classes derived from primitive axclasses and locales.
18168
d35daf321b8a class_package - operational view on type classes
haftmann
parents:
diff changeset
     6
*)
d35daf321b8a class_package - operational view on type classes
haftmann
parents:
diff changeset
     7
d35daf321b8a class_package - operational view on type classes
haftmann
parents:
diff changeset
     8
signature CLASS_PACKAGE =
d35daf321b8a class_package - operational view on type classes
haftmann
parents:
diff changeset
     9
sig
22302
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
    10
  val fork_mixfix: bool -> bool -> mixfix -> mixfix * mixfix
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
    11
21954
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
    12
  val class: bstring -> class list -> Element.context_i Locale.element list -> theory ->
20964
77b59c3a2418 class(_i): mimic Locale.add_locale(_i);
wenzelm
parents: 20892
diff changeset
    13
    string * Proof.context
22302
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
    14
  val class_e: bstring -> string list -> Element.context Locale.element list -> theory ->
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
    15
    string * Proof.context
21954
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
    16
  val instance_arity: ((bstring * sort list) * sort) list
22302
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
    17
    -> ((bstring * Attrib.src list) * term) list
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
    18
    -> theory -> Proof.state
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
    19
  val instance_arity_e: ((bstring * string list) * string) list
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
    20
    -> ((bstring * Attrib.src list) * string) list
18575
9ccfd1d1e874 substantial additions using locales
haftmann
parents: 18550
diff changeset
    21
    -> theory -> Proof.state
21924
fe474e69e603 simplified class_package
haftmann
parents: 21894
diff changeset
    22
  val prove_instance_arity: tactic -> ((string * sort list) * sort) list
22302
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
    23
    -> ((bstring * Attrib.src list) * term) list
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
    24
    -> theory -> theory
21954
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
    25
  val instance_sort: class * sort -> theory -> Proof.state
22302
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
    26
  val instance_sort_e: string * string -> theory -> Proof.state
19150
1457d810b408 class package and codegen refinements
haftmann
parents: 19136
diff changeset
    27
  val prove_instance_sort: tactic -> class * sort -> theory -> theory
18168
d35daf321b8a class_package - operational view on type classes
haftmann
parents:
diff changeset
    28
20175
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
    29
  val assume_arities_of_sort: theory -> ((string * sort list) * sort) list -> typ * sort -> bool
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
    30
  val assume_arities_thy: theory -> ((string * sort list) * sort) list -> (theory -> 'a) -> 'a
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
    31
    (*'a must not keep any reference to theory*)
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
    32
21954
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
    33
  (* experimental class target *)
22302
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
    34
  val class_of_locale: theory -> string -> class option
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
    35
  val add_def_in_class: local_theory -> string
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
    36
    -> (string * Syntax.mixfix) * ((string * Attrib.src list) * (term * thm)) -> theory -> theory
21553
3dd27eda91b9 additional bookkeeping for class target
haftmann
parents: 21463
diff changeset
    37
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18670
diff changeset
    38
  val print_classes: theory -> unit
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
    39
  val intro_classes_tac: thm list -> tactic
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
    40
  val default_intro_classes_tac: thm list -> tactic
18168
d35daf321b8a class_package - operational view on type classes
haftmann
parents:
diff changeset
    41
end;
d35daf321b8a class_package - operational view on type classes
haftmann
parents:
diff changeset
    42
20182
79c9ff40d760 tactic for prove_instance_arity now gets definition theorems as arguments
haftmann
parents: 20175
diff changeset
    43
structure ClassPackage : CLASS_PACKAGE =
18168
d35daf321b8a class_package - operational view on type classes
haftmann
parents:
diff changeset
    44
struct
d35daf321b8a class_package - operational view on type classes
haftmann
parents:
diff changeset
    45
22302
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
    46
(** auxiliary **)
18168
d35daf321b8a class_package - operational view on type classes
haftmann
parents:
diff changeset
    47
22302
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
    48
fun fork_mixfix is_class is_loc mx =
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
    49
  let
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
    50
    val mx' = Syntax.unlocalize_mixfix mx;
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
    51
    val mx1 = if is_class orelse (is_loc andalso mx = mx') then NoSyn else mx';
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
    52
    val mx2 = if is_loc then mx else NoSyn;
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
    53
  in (mx1, mx2) end;
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
    54
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
    55
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
    56
(** AxClasses with implicit parameter handling **)
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
    57
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
    58
(* AxClass instances *)
18168
d35daf321b8a class_package - operational view on type classes
haftmann
parents:
diff changeset
    59
21954
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
    60
local
19456
b5bfd2d17dd3 more precise data structure
haftmann
parents: 19434
diff changeset
    61
21954
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
    62
fun gen_instance mk_prop add_thm after_qed insts thy =
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
    63
  let
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
    64
    fun after_qed' results =
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
    65
      ProofContext.theory ((fold o fold) add_thm results #> after_qed);
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
    66
  in
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
    67
    thy
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
    68
    |> ProofContext.init
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
    69
    |> Proof.theorem_i NONE after_qed' ((map (fn t => [(t, [])]) o maps (mk_prop thy)) insts)
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
    70
  end;
18168
d35daf321b8a class_package - operational view on type classes
haftmann
parents:
diff changeset
    71
21954
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
    72
in
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
    73
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
    74
val axclass_instance_arity =
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
    75
  gen_instance (Logic.mk_arities oo Sign.cert_arity) AxClass.add_arity;
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
    76
val axclass_instance_sort =
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
    77
  gen_instance (single oo (Logic.mk_classrel oo AxClass.cert_classrel))
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
    78
    AxClass.add_classrel I o single;
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
    79
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
    80
end; (* local *)
18575
9ccfd1d1e874 substantial additions using locales
haftmann
parents: 18550
diff changeset
    81
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 18963
diff changeset
    82
21954
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
    83
(* introducing axclasses with implicit parameter handling *)
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 18963
diff changeset
    84
21954
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
    85
fun axclass_params (name, raw_superclasses) raw_consts raw_dep_axioms thy =
21924
fe474e69e603 simplified class_package
haftmann
parents: 21894
diff changeset
    86
  let
21954
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
    87
    val superclasses = map (Sign.certify_class thy) raw_superclasses;
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
    88
    val consts = (map o apfst o apsnd) (Sign.certify_typ thy) raw_consts;
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
    89
    fun add_const ((c, ty), syn) =
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
    90
      Sign.add_consts_authentic [(c, ty, syn)] #> pair (Sign.full_name thy c, ty);
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
    91
    fun mk_axioms cs thy =
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
    92
      raw_dep_axioms thy cs
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
    93
      |> (map o apsnd o map) (Sign.cert_prop thy)
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
    94
      |> rpair thy;
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
    95
    fun add_constraint class (c, ty) =
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
    96
      Sign.add_const_constraint_i (c, SOME (Term.map_type_tfree (fn (v, _) => TFree (v, [class])) ty));
21924
fe474e69e603 simplified class_package
haftmann
parents: 21894
diff changeset
    97
  in
21954
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
    98
    thy
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
    99
    |> fold_map add_const consts
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   100
    |-> (fn cs => mk_axioms cs
22302
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   101
    #-> (fn axioms_prop => AxClass.define_class_i (name, superclasses) (map fst cs) axioms_prop
21954
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   102
    #-> (fn class => `(fn thy => AxClass.get_definition thy class)
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   103
    #-> (fn {intro, axioms, ...} => fold (add_constraint class) cs
22302
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   104
    #> pair (class, ((intro, (map snd axioms_prop, axioms)), cs))))))
21924
fe474e69e603 simplified class_package
haftmann
parents: 21894
diff changeset
   105
  end;
fe474e69e603 simplified class_package
haftmann
parents: 21894
diff changeset
   106
18168
d35daf321b8a class_package - operational view on type classes
haftmann
parents:
diff changeset
   107
21954
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   108
(* contexts with arity assumptions *)
20175
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   109
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   110
fun assume_arities_of_sort thy arities ty_sort =
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   111
  let
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   112
    val pp = Sign.pp thy;
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   113
    val algebra = Sign.classes_of thy
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   114
      |> fold (fn ((tyco, asorts), sort) =>
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   115
           Sorts.add_arities pp (tyco, map (fn class => (class, asorts)) sort)) arities;
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   116
  in Sorts.of_sort algebra ty_sort end;
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   117
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   118
fun assume_arities_thy thy arities f =
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   119
  let
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   120
    val thy_read = (fold (fn ((tyco, asorts), sort)
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   121
      => Sign.primitive_arity (tyco, asorts, sort)) arities o Theory.copy) thy
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   122
  in f thy_read end;
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   123
20455
e671d9eac6c8 removed some dictionary stuff
haftmann
parents: 20428
diff changeset
   124
21954
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   125
(* instances with implicit parameter handling *)
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 18963
diff changeset
   126
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   127
local
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   128
20175
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   129
fun gen_read_def thy prep_att read_def ((raw_name, raw_atts), raw_t) =
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 18963
diff changeset
   130
  let
19957
91ba241a1678 reduced code, better instance command
haftmann
parents: 19953
diff changeset
   131
    val (_, t) = read_def thy (raw_name, raw_t);
91ba241a1678 reduced code, better instance command
haftmann
parents: 19953
diff changeset
   132
    val ((c, ty), _) = Sign.cert_def (Sign.pp thy) t;
91ba241a1678 reduced code, better instance command
haftmann
parents: 19953
diff changeset
   133
    val atts = map (prep_att thy) raw_atts;
20175
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   134
    val insts = (Consts.typargs (Sign.consts_of thy) (c, ty))
19957
91ba241a1678 reduced code, better instance command
haftmann
parents: 19953
diff changeset
   135
    val name = case raw_name
20385
2f52b5aba086 code cleanup, instance_subsort now working
haftmann
parents: 20368
diff changeset
   136
     of "" => NONE
2f52b5aba086 code cleanup, instance_subsort now working
haftmann
parents: 20368
diff changeset
   137
      | _ => SOME raw_name;
20175
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   138
  in (c, (insts, ((name, t), atts))) end;
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   139
22302
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   140
fun read_def_e thy = gen_read_def thy Attrib.intern_src read_axm;
21954
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   141
fun read_def thy = gen_read_def thy (K I) (K I);
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   142
22302
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   143
fun gen_instance_arity prep_arity read_def do_proof raw_arities raw_defs theory =
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   144
  let
20175
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   145
    fun prep_arity' ((tyco, asorts), sort) = prep_arity theory (tyco, asorts, sort);
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   146
    val arities = map prep_arity' raw_arities;
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   147
    val arities_pair = map (fn (tyco, asorts, sort) => ((tyco, asorts), sort)) arities;
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   148
    val _ = if null arities then error "at least one arity must be given" else ();
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   149
    val _ = case (duplicates (op =) o map #1) arities
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   150
     of [] => ()
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   151
      | dupl_tycos => error ("type constructors occur more than once in arities: "
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   152
        ^ (commas o map quote) dupl_tycos);
21954
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   153
    val super_sort = (Graph.all_succs o #classes o Sorts.rep_algebra o Sign.classes_of) theory
20175
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   154
    fun get_consts_class tyco ty class =
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   155
      let
21954
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   156
        val cs = (these o Option.map snd o try (AxClass.params_of_class theory)) class;
21463
42dd50268c8b completed class parameter handling in axclass.ML
haftmann
parents: 21444
diff changeset
   157
        val subst_ty = map_type_tfree (K ty);
19957
91ba241a1678 reduced code, better instance command
haftmann
parents: 19953
diff changeset
   158
      in
21894
1a0e32ccb8bb dropped superfluos code
haftmann
parents: 21805
diff changeset
   159
        map (fn (c, ty) => (c, ((tyco, class), subst_ty ty))) cs
19957
91ba241a1678 reduced code, better instance command
haftmann
parents: 19953
diff changeset
   160
      end;
20175
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   161
    fun get_consts_sort (tyco, asorts, sort) =
19957
91ba241a1678 reduced code, better instance command
haftmann
parents: 19953
diff changeset
   162
      let
20192
956cd30ef3be renamed Name.give_names to Name.names and moved Name.alphanum to Symbol.alphanum
haftmann
parents: 20191
diff changeset
   163
        val ty = Type (tyco, map (fn (v, sort) => TVar ((v, 0), sort)) (Name.names Name.context "'a" asorts))
21954
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   164
      in maps (get_consts_class tyco ty) (super_sort sort) end;
20175
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   165
    val cs = maps get_consts_sort arities;
20385
2f52b5aba086 code cleanup, instance_subsort now working
haftmann
parents: 20368
diff changeset
   166
    fun mk_typnorm thy (ty, ty_sc) =
2f52b5aba086 code cleanup, instance_subsort now working
haftmann
parents: 20368
diff changeset
   167
      case try (Sign.typ_match thy (Logic.varifyT ty_sc, ty)) Vartab.empty
2f52b5aba086 code cleanup, instance_subsort now working
haftmann
parents: 20368
diff changeset
   168
       of SOME env => SOME (Logic.varifyT #> Envir.typ_subst_TVars env #> Logic.unvarifyT)
2f52b5aba086 code cleanup, instance_subsort now working
haftmann
parents: 20368
diff changeset
   169
        | NONE => NONE;
20175
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   170
    fun read_defs defs cs thy_read =
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   171
      let
19957
91ba241a1678 reduced code, better instance command
haftmann
parents: 19953
diff changeset
   172
        fun read raw_def cs =
91ba241a1678 reduced code, better instance command
haftmann
parents: 19953
diff changeset
   173
          let
20385
2f52b5aba086 code cleanup, instance_subsort now working
haftmann
parents: 20368
diff changeset
   174
            val (c, (inst, ((name_opt, t), atts))) = read_def thy_read raw_def;
2f52b5aba086 code cleanup, instance_subsort now working
haftmann
parents: 20368
diff changeset
   175
            val ty = Consts.instance (Sign.consts_of thy_read) (c, inst);
20175
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   176
            val ((tyco, class), ty') = case AList.lookup (op =) cs c
19957
91ba241a1678 reduced code, better instance command
haftmann
parents: 19953
diff changeset
   177
             of NONE => error ("superfluous definition for constant " ^ quote c)
19966
88bbe97ed0b0 refinement in instance command
haftmann
parents: 19957
diff changeset
   178
              | SOME class_ty => class_ty;
20385
2f52b5aba086 code cleanup, instance_subsort now working
haftmann
parents: 20368
diff changeset
   179
            val name = case name_opt
20628
b15b6f05d145 Logic.name_classrel/arities;
wenzelm
parents: 20601
diff changeset
   180
             of NONE => Thm.def_name (Logic.name_arity (tyco, [], c))
20385
2f52b5aba086 code cleanup, instance_subsort now working
haftmann
parents: 20368
diff changeset
   181
              | SOME name => name;
2f52b5aba086 code cleanup, instance_subsort now working
haftmann
parents: 20368
diff changeset
   182
            val t' = case mk_typnorm thy_read (ty', ty)
19966
88bbe97ed0b0 refinement in instance command
haftmann
parents: 19957
diff changeset
   183
             of NONE => error ("superfluous definition for constant " ^
88bbe97ed0b0 refinement in instance command
haftmann
parents: 19957
diff changeset
   184
                  quote c ^ "::" ^ Sign.string_of_typ thy_read ty)
20548
8ef25fe585a8 renamed Term.map_term_types to Term.map_types (cf. Term.fold_types);
wenzelm
parents: 20465
diff changeset
   185
              | SOME norm => map_types norm t
20175
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   186
          in (((class, tyco), ((name, t'), atts)), AList.delete (op =) c cs) end;
19957
91ba241a1678 reduced code, better instance command
haftmann
parents: 19953
diff changeset
   187
      in fold_map read defs cs end;
20175
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   188
    val (defs, _) = assume_arities_thy theory arities_pair (read_defs raw_defs cs);
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   189
    fun get_remove_contraint c thy =
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   190
      let
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   191
        val ty = Sign.the_const_constraint thy c;
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   192
      in
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   193
        thy
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   194
        |> Sign.add_const_constraint_i (c, NONE)
20385
2f52b5aba086 code cleanup, instance_subsort now working
haftmann
parents: 20368
diff changeset
   195
        |> pair (c, Logic.unvarifyT ty)
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   196
      end;
19966
88bbe97ed0b0 refinement in instance command
haftmann
parents: 19957
diff changeset
   197
    fun add_defs defs thy =
88bbe97ed0b0 refinement in instance command
haftmann
parents: 19957
diff changeset
   198
      thy
22302
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   199
      |> PureThy.add_defs_i true (map ((apsnd o map) (Attrib.attribute thy) o snd) defs)
19966
88bbe97ed0b0 refinement in instance command
haftmann
parents: 19957
diff changeset
   200
      |-> (fn thms => pair (map fst defs ~~ thms));
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   201
    fun after_qed cs thy =
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   202
      thy
19412
cc08bcabdcd2 classes: prefer thy operations;
wenzelm
parents: 19395
diff changeset
   203
      |> fold Sign.add_const_constraint_i (map (apsnd SOME) cs);
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 18963
diff changeset
   204
  in
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   205
    theory
20385
2f52b5aba086 code cleanup, instance_subsort now working
haftmann
parents: 20368
diff changeset
   206
    |> fold_map get_remove_contraint (map fst cs |> distinct (op =))
19966
88bbe97ed0b0 refinement in instance command
haftmann
parents: 19957
diff changeset
   207
    ||>> add_defs defs
21954
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   208
    |-> (fn (cs, _) => do_proof (after_qed cs) arities)
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 18963
diff changeset
   209
  end;
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 18963
diff changeset
   210
22302
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   211
fun instance_arity_e' do_proof = gen_instance_arity Sign.read_arity read_def_e do_proof;
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   212
fun instance_arity' do_proof = gen_instance_arity Sign.cert_arity read_def do_proof;
21924
fe474e69e603 simplified class_package
haftmann
parents: 21894
diff changeset
   213
fun tactic_proof tac after_qed arities =
fe474e69e603 simplified class_package
haftmann
parents: 21894
diff changeset
   214
  fold (fn arity => AxClass.prove_arity arity tac) arities
20175
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20106
diff changeset
   215
  #> after_qed;
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   216
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   217
in
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   218
21954
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   219
val instance_arity_e = instance_arity_e' axclass_instance_arity;
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   220
val instance_arity = instance_arity' axclass_instance_arity;
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   221
val prove_instance_arity = instance_arity' o tactic_proof;
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   222
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   223
end; (* local *)
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   224
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   225
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   226
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   227
(** combining locales and axclasses **)
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   228
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   229
(* theory data *)
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   230
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   231
datatype class_data = ClassData of {
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   232
  locale: string,
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   233
  consts: (string * string) list
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   234
    (*locale parameter ~> toplevel theory constant*),
22302
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   235
  witness: Element.witness list,
21954
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   236
  propnames: string list,
22302
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   237
    (*for ad-hoc instance_in*)
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   238
  primdefs: thm list
21954
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   239
    (*for experimental class target*)
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   240
};
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   241
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   242
fun rep_classdata (ClassData c) = c;
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   243
22302
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   244
fun merge_pair f1 f2 ((x1, y1), (x2, y2)) = (f1 (x1, x2), f2 (y1, y2));
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   245
21954
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   246
structure ClassData = TheoryDataFun (
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   247
  struct
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   248
    val name = "Pure/classes";
22302
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   249
    type T = class_data Graph.T * class Symtab.table (*locale name ~> class name*);
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   250
    val empty = (Graph.empty, Symtab.empty);
21954
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   251
    val copy = I;
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   252
    val extend = I;
22302
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   253
    fun merge _ = merge_pair (Graph.merge (K true)) (Symtab.merge (K true));
21954
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   254
    fun print _ _ = ();
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   255
  end
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   256
);
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   257
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   258
val _ = Context.add_setup ClassData.init;
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   259
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   260
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   261
(* queries *)
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   262
22302
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   263
val lookup_class_data = Option.map rep_classdata oo try o Graph.get_node o fst o ClassData.get;
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   264
fun class_of_locale thy = Symtab.lookup ((snd o ClassData.get) thy);
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   265
21954
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   266
fun the_class_data thy class =
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   267
  case lookup_class_data thy class
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   268
    of NONE => error ("undeclared class " ^ quote class)
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   269
     | SOME data => data;
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   270
22302
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   271
val ancestry = Graph.all_succs o fst o ClassData.get;
21954
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   272
22302
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   273
fun param_map thy = 
21954
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   274
  let
22302
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   275
    fun params thy class =
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   276
      let
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   277
        val const_typs = (#params o AxClass.get_definition thy) class;
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   278
        val const_names = (#consts o the_class_data thy) class;
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   279
      in
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   280
        (map o apsnd) (fn c => (c, (the o AList.lookup (op =) const_typs) c)) const_names
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   281
      end;
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   282
  in maps (params thy) o ancestry thy end;
21954
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   283
22302
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   284
val the_witness = #witness oo the_class_data;
21954
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   285
val the_propnames = #propnames oo the_class_data;
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   286
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   287
fun print_classes thy =
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   288
  let
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   289
    val algebra = Sign.classes_of thy;
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   290
    val arities =
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   291
      Symtab.empty
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   292
      |> Symtab.fold (fn (tyco, arities) => fold (fn (class, _) =>
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   293
           Symtab.map_default (class, []) (insert (op =) tyco)) arities)
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   294
             ((#arities o Sorts.rep_algebra) algebra);
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   295
    val the_arities = these o Symtab.lookup arities;
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   296
    fun mk_arity class tyco =
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   297
      let
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   298
        val Ss = Sorts.mg_domain algebra tyco [class];
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   299
      in Sign.pretty_arity thy (tyco, Ss, [class]) end;
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   300
    fun mk_param (c, ty) = Pretty.str (Sign.extern_const thy c ^ " :: "
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   301
      ^ setmp show_sorts false (Sign.string_of_typ thy o Type.strip_sorts) ty);
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   302
    fun mk_entry class = (Pretty.block o Pretty.fbreaks o map_filter I) [
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   303
      (SOME o Pretty.str) ("class " ^ class ^ ":"),
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   304
      (SOME o Pretty.block) [Pretty.str "supersort: ",
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   305
        (Sign.pretty_sort thy o Sign.certify_sort thy o Sign.super_classes thy) class],
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   306
      Option.map (Pretty.str o prefix "locale: " o #locale) (lookup_class_data thy class),
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   307
      ((fn [] => NONE | ps => (SOME o Pretty.block o Pretty.fbreaks) (Pretty.str "parameters:" :: ps)) o map mk_param
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   308
        o these o Option.map #params o try (AxClass.get_definition thy)) class,
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   309
      (SOME o Pretty.block o Pretty.breaks) [
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   310
        Pretty.str "instances:",
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   311
        Pretty.list "" "" (map (mk_arity class) (the_arities class))
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   312
      ]
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   313
    ]
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   314
  in
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   315
    (Pretty.writeln o Pretty.chunks o separate (Pretty.str "") o map mk_entry o Sorts.all_classes)
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   316
      algebra
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   317
  end;
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   318
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   319
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   320
(* updaters *)
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   321
22302
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   322
fun add_class_data ((class, superclasses), (locale, consts, witness, propnames)) =
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   323
  ClassData.map (fn (gr, tab) => (
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   324
    gr
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   325
    |> Graph.new_node (class, ClassData {
21954
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   326
      locale = locale,
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   327
      consts = consts,
22302
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   328
      witness = witness,
21954
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   329
      propnames = propnames,
22302
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   330
      primdefs = []})
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   331
    |> fold (curry Graph.add_edge class) superclasses,
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   332
    tab
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   333
    |> Symtab.update (locale, class)
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   334
  ));
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   335
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   336
fun add_primdef (class, thm) thy =
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   337
  (ClassData.map o apfst o Graph.map_node class)
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   338
    (fn ClassData { locale, consts, witness, propnames, primdefs } => ClassData { locale = locale,
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   339
      consts = consts, witness = witness, propnames = propnames, primdefs = thm :: primdefs });
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   340
21954
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   341
22302
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   342
(* exporting terms and theorems to global toplevel *)
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   343
(*FIXME should also be used when introducing classes*)
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   344
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   345
fun globalize thy classes =
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   346
  let
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   347
    val consts = param_map thy classes;
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   348
    val constrain_sort = curry (Sorts.inter_sort (Sign.classes_of thy)) classes;
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   349
    val subst_typ = Term.map_type_tfree (fn var as (v, sort) =>
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   350
      if v = AxClass.param_tyvarname then TFree (v, constrain_sort sort) else TFree var)
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   351
    fun subst_aterm (t as Free (v, ty)) = (case AList.lookup (op =) consts v
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   352
         of SOME (c, _) => Const (c, ty)
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   353
          | NONE => t)
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   354
      | subst_aterm t = t;
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   355
  in (subst_typ, map_types subst_typ #> Term.map_aterms subst_aterm) end;
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   356
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   357
val global_term = snd oo globalize
21954
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   358
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   359
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   360
(* tactics and methods *)
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   361
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   362
fun intro_classes_tac facts st =
22182
05ed4080cbd7 clarified code
haftmann
parents: 22101
diff changeset
   363
  let
05ed4080cbd7 clarified code
haftmann
parents: 22101
diff changeset
   364
    val thy = Thm.theory_of_thm st;
05ed4080cbd7 clarified code
haftmann
parents: 22101
diff changeset
   365
    val ctxt = ProofContext.init thy;
05ed4080cbd7 clarified code
haftmann
parents: 22101
diff changeset
   366
    val intro_classes_tac = ALLGOALS
05ed4080cbd7 clarified code
haftmann
parents: 22101
diff changeset
   367
      (Method.insert_tac facts THEN' REPEAT_ALL_NEW (resolve_tac (AxClass.class_intros thy)))
05ed4080cbd7 clarified code
haftmann
parents: 22101
diff changeset
   368
        THEN Tactic.distinct_subgoals_tac;
05ed4080cbd7 clarified code
haftmann
parents: 22101
diff changeset
   369
    val intro_locales_tac = Locale.intro_locales_tac true ctxt facts;
05ed4080cbd7 clarified code
haftmann
parents: 22101
diff changeset
   370
  in
05ed4080cbd7 clarified code
haftmann
parents: 22101
diff changeset
   371
    (intro_classes_tac THEN TRY intro_locales_tac) st
05ed4080cbd7 clarified code
haftmann
parents: 22101
diff changeset
   372
  end;
21954
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   373
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   374
fun default_intro_classes_tac [] = intro_classes_tac []
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   375
  | default_intro_classes_tac _ = Tactical.no_tac;    (*no error message!*)
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   376
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   377
fun default_tac rules ctxt facts =
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   378
  HEADGOAL (Method.some_rule_tac rules ctxt facts) ORELSE
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   379
    default_intro_classes_tac facts;
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   380
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   381
val _ = Context.add_setup (Method.add_methods
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   382
 [("intro_classes", Method.no_args (Method.METHOD intro_classes_tac),
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   383
    "back-chain introduction rules of classes"),
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   384
  ("default", Method.thms_ctxt_args (Method.METHOD oo default_tac),
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   385
    "apply some intro/elim rule")]);
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   386
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   387
22069
8668c056c507 slight cleanup
haftmann
parents: 22048
diff changeset
   388
(* FIXME workarounds for locale package *)
21954
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   389
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   390
fun prove_interpretation (prfx, atts) expr insts tac thy =
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   391
  let
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   392
    fun ad_hoc_term (Const (c, ty)) =
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   393
          let
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   394
            val p = setmp show_types true (setmp show_sorts true (setmp print_mode [] (Sign.pretty_typ thy))) ty;
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   395
            val s = c ^ "::" ^ Pretty.output p;
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   396
          in s end
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   397
      | ad_hoc_term t =
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   398
          let
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   399
            val p = setmp show_types true (setmp show_sorts true (setmp print_mode [] (Sign.pretty_term thy))) t;
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   400
            val s = Pretty.output p;
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   401
          in s end;
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   402
  in
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   403
    thy
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   404
    |> Locale.interpretation I (prfx, atts) expr (map (Option.map ad_hoc_term) insts)
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   405
    |> Proof.global_terminal_proof (Method.Basic (fn _ => Method.SIMPLE_METHOD tac), NONE)
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   406
    |> ProofContext.theory_of
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   407
  end;
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   408
22069
8668c056c507 slight cleanup
haftmann
parents: 22048
diff changeset
   409
fun prove_interpretation_in tac after_qed (name, expr) thy =
8668c056c507 slight cleanup
haftmann
parents: 22048
diff changeset
   410
  thy
8668c056c507 slight cleanup
haftmann
parents: 22048
diff changeset
   411
  |> Locale.interpretation_in_locale (ProofContext.theory after_qed) (name, expr)
8668c056c507 slight cleanup
haftmann
parents: 22048
diff changeset
   412
  |> Proof.global_terminal_proof (Method.Basic (fn _ => Method.SIMPLE_METHOD tac), NONE)
8668c056c507 slight cleanup
haftmann
parents: 22048
diff changeset
   413
  |> ProofContext.theory_of;
8668c056c507 slight cleanup
haftmann
parents: 22048
diff changeset
   414
8668c056c507 slight cleanup
haftmann
parents: 22048
diff changeset
   415
fun instance_sort' do_proof (class, sort) theory =
8668c056c507 slight cleanup
haftmann
parents: 22048
diff changeset
   416
  let
8668c056c507 slight cleanup
haftmann
parents: 22048
diff changeset
   417
    val loc_name = (#locale o the_class_data theory) class;
8668c056c507 slight cleanup
haftmann
parents: 22048
diff changeset
   418
    val loc_expr =
8668c056c507 slight cleanup
haftmann
parents: 22048
diff changeset
   419
      (Locale.Merge o map (Locale.Locale o #locale o the_class_data theory)) sort;
8668c056c507 slight cleanup
haftmann
parents: 22048
diff changeset
   420
    val const_names = (map (NameSpace.base o snd)
8668c056c507 slight cleanup
haftmann
parents: 22048
diff changeset
   421
      o maps (#consts o the_class_data theory)
22302
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   422
      o ancestry theory) [class];
22069
8668c056c507 slight cleanup
haftmann
parents: 22048
diff changeset
   423
    fun get_thms thy =
22302
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   424
      ancestry thy sort
22069
8668c056c507 slight cleanup
haftmann
parents: 22048
diff changeset
   425
      |> AList.make (the_propnames thy)
8668c056c507 slight cleanup
haftmann
parents: 22048
diff changeset
   426
      |> map (apsnd (map (NameSpace.append (Logic.const_of_class loc_name))))
8668c056c507 slight cleanup
haftmann
parents: 22048
diff changeset
   427
      |> map_filter (fn (superclass, thm_names) =>
8668c056c507 slight cleanup
haftmann
parents: 22048
diff changeset
   428
          case map_filter (try (PureThy.get_thm thy o Name)) thm_names
8668c056c507 slight cleanup
haftmann
parents: 22048
diff changeset
   429
           of [] => NONE
8668c056c507 slight cleanup
haftmann
parents: 22048
diff changeset
   430
            | thms => SOME (superclass, thms));
8668c056c507 slight cleanup
haftmann
parents: 22048
diff changeset
   431
    fun after_qed thy =
8668c056c507 slight cleanup
haftmann
parents: 22048
diff changeset
   432
      thy
8668c056c507 slight cleanup
haftmann
parents: 22048
diff changeset
   433
      |> `get_thms
8668c056c507 slight cleanup
haftmann
parents: 22048
diff changeset
   434
      |-> fold (fn (supclass, thms) => I
8668c056c507 slight cleanup
haftmann
parents: 22048
diff changeset
   435
            AxClass.prove_classrel (class, supclass)
8668c056c507 slight cleanup
haftmann
parents: 22048
diff changeset
   436
              (ALLGOALS (K (intro_classes_tac [])) THEN
8668c056c507 slight cleanup
haftmann
parents: 22048
diff changeset
   437
                (ALLGOALS o ProofContext.fact_tac) thms))
8668c056c507 slight cleanup
haftmann
parents: 22048
diff changeset
   438
  in
8668c056c507 slight cleanup
haftmann
parents: 22048
diff changeset
   439
    theory
8668c056c507 slight cleanup
haftmann
parents: 22048
diff changeset
   440
    |> do_proof after_qed (loc_name, loc_expr)
8668c056c507 slight cleanup
haftmann
parents: 22048
diff changeset
   441
  end;
8668c056c507 slight cleanup
haftmann
parents: 22048
diff changeset
   442
22074
de3586cb0ebd cleanup
haftmann
parents: 22069
diff changeset
   443
val prove_instance_sort = instance_sort' o prove_interpretation_in;
de3586cb0ebd cleanup
haftmann
parents: 22069
diff changeset
   444
22069
8668c056c507 slight cleanup
haftmann
parents: 22048
diff changeset
   445
8668c056c507 slight cleanup
haftmann
parents: 22048
diff changeset
   446
(* classes and instances *)
8668c056c507 slight cleanup
haftmann
parents: 22048
diff changeset
   447
8668c056c507 slight cleanup
haftmann
parents: 22048
diff changeset
   448
local
8668c056c507 slight cleanup
haftmann
parents: 22048
diff changeset
   449
8668c056c507 slight cleanup
haftmann
parents: 22048
diff changeset
   450
fun add_axclass (name, supsort) params axs thy =
8668c056c507 slight cleanup
haftmann
parents: 22048
diff changeset
   451
  let
8668c056c507 slight cleanup
haftmann
parents: 22048
diff changeset
   452
    val (c, thy') = thy
8668c056c507 slight cleanup
haftmann
parents: 22048
diff changeset
   453
      |> AxClass.define_class_i (name, supsort) params axs;
8668c056c507 slight cleanup
haftmann
parents: 22048
diff changeset
   454
    val {intro, axioms, ...} = AxClass.get_definition thy' c;
8668c056c507 slight cleanup
haftmann
parents: 22048
diff changeset
   455
  in ((c, (intro, axioms)), thy') end;
8668c056c507 slight cleanup
haftmann
parents: 22048
diff changeset
   456
8668c056c507 slight cleanup
haftmann
parents: 22048
diff changeset
   457
fun certify_class thy class =
8668c056c507 slight cleanup
haftmann
parents: 22048
diff changeset
   458
  tap (the_class_data thy) (Sign.certify_class thy class);
8668c056c507 slight cleanup
haftmann
parents: 22048
diff changeset
   459
8668c056c507 slight cleanup
haftmann
parents: 22048
diff changeset
   460
fun read_class thy =
8668c056c507 slight cleanup
haftmann
parents: 22048
diff changeset
   461
  certify_class thy o Sign.intern_class thy;
8668c056c507 slight cleanup
haftmann
parents: 22048
diff changeset
   462
21954
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   463
fun gen_class add_locale prep_class bname raw_supclasses raw_elems thy =
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   464
  let
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   465
    val elems = fold_rev (fn Locale.Elem e => cons e | _ => I) raw_elems [];
22302
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   466
      (*FIXME add constrains here to elements as hint for locale*)
21954
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   467
    val supclasses = map (prep_class thy) raw_supclasses;
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   468
    val supsort =
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   469
      supclasses
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   470
      |> Sign.certify_sort thy
22302
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   471
      |> (fn [] => Sign.defaultS thy | S => S);    (*FIXME Why syntax defaultS?*)
21954
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   472
    val supexpr = Locale.Merge (map (Locale.Locale o #locale o the_class_data thy) supclasses);
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   473
    val supconsts = AList.make
22302
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   474
      (the o AList.lookup (op =) (param_map thy supclasses))
21954
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   475
      ((map (fst o fst) o Locale.parameters_of_expr thy) supexpr);
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   476
    fun check_locale thy name_locale =
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   477
      let
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   478
        val tfrees =
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   479
          []
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   480
          |> fold (fold Term.add_tfrees o snd) (Locale.global_asms_of thy name_locale)
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   481
          |> fold (Term.add_tfreesT o snd o fst) (Locale.parameters_of thy name_locale);
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   482
      in case tfrees
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   483
       of [(_, _)] => ()
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   484
        (*| [(_, sort)] => error ("Additional sort constraint on class variable: "
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   485
            ^ Sign.string_of_sort thy sort) FIXME what to do about this?*)
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   486
        | [] => error ("No type variable in class specification")
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   487
        | tfrees => error ("More than one type variable in class specification: " ^
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   488
            (commas o map fst) tfrees)
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   489
      end;
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   490
    fun extract_params thy name_locale =
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   491
      Locale.parameters_of thy name_locale
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   492
      |> (map o apfst o apsnd o Term.map_type_tfree) (K (TFree (AxClass.param_tyvarname, [])))
22302
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   493
      |> (map o apsnd) (fork_mixfix false true #> fst)
21954
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   494
      |> chop (length supconsts)
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   495
      |> snd;
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   496
    fun extract_assumes name_locale params thy cs =
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   497
      let
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   498
        val consts = supconsts @ (map (fst o fst) params ~~ cs);
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   499
        (*FIXME is this type handling correct?*)
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   500
        fun subst (Free (c, ty)) =
22048
990b5077590d activated new class code
haftmann
parents: 21954
diff changeset
   501
              Const ((fst o the o AList.lookup (op =) consts) c, ty)
990b5077590d activated new class code
haftmann
parents: 21954
diff changeset
   502
          | subst t = t;
21954
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   503
        fun prep_asm ((name, atts), ts) =
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   504
          (*FIXME*)
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   505
          ((NameSpace.base name, map (Attrib.attribute thy) atts), (map o map_aterms) subst ts);
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   506
      in
22182
05ed4080cbd7 clarified code
haftmann
parents: 22101
diff changeset
   507
        Locale.global_asms_of thy name_locale
21954
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   508
        |> map prep_asm
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   509
      end;
22048
990b5077590d activated new class code
haftmann
parents: 21954
diff changeset
   510
    fun extract_axiom_names thy name_locale =
990b5077590d activated new class code
haftmann
parents: 21954
diff changeset
   511
      name_locale
22182
05ed4080cbd7 clarified code
haftmann
parents: 22101
diff changeset
   512
      |> Locale.global_asms_of thy
22048
990b5077590d activated new class code
haftmann
parents: 21954
diff changeset
   513
      |> map (NameSpace.base o fst o fst) (*FIXME*)
21954
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   514
    fun mk_const thy class (c, ty) =
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   515
      Const (c, Term.map_type_tfree (fn (v, _) => TFree (v, [class])) ty);
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   516
  in
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   517
    thy
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   518
    |> add_locale bname supexpr elems
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   519
    |-> (fn name_locale => ProofContext.theory_result (
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   520
      tap (fn thy => check_locale thy name_locale)
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   521
      #> `(fn thy => extract_params thy name_locale)
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   522
      #-> (fn params =>
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   523
        axclass_params (bname, supsort) params (extract_assumes name_locale params)
22302
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   524
      #-> (fn (name_axclass, ((_, (ax_terms, ax_axioms)), consts)) =>
22048
990b5077590d activated new class code
haftmann
parents: 21954
diff changeset
   525
        `(fn thy => extract_axiom_names thy name_locale)
990b5077590d activated new class code
haftmann
parents: 21954
diff changeset
   526
      #-> (fn axiom_names =>
990b5077590d activated new class code
haftmann
parents: 21954
diff changeset
   527
        add_class_data ((name_axclass, supclasses),
22302
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   528
          (name_locale, map (fst o fst) params ~~ map fst consts,
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   529
            map2 Element.make_witness (map Logic.mk_conjunction_list ax_terms) ax_axioms, axiom_names))
21954
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   530
      #> prove_interpretation (Logic.const_of_class bname, [])
22182
05ed4080cbd7 clarified code
haftmann
parents: 22101
diff changeset
   531
          (Locale.Locale name_locale) (map (SOME o mk_const thy name_axclass) (map snd supconsts @ consts))
21954
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   532
            ((ALLGOALS o ProofContext.fact_tac) ax_axioms)
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   533
      #> pair name_axclass
22048
990b5077590d activated new class code
haftmann
parents: 21954
diff changeset
   534
      )))))
21954
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   535
  end;
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   536
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   537
in
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   538
22182
05ed4080cbd7 clarified code
haftmann
parents: 22101
diff changeset
   539
val class_e = gen_class (Locale.add_locale true) read_class;
05ed4080cbd7 clarified code
haftmann
parents: 22101
diff changeset
   540
val class = gen_class (Locale.add_locale_i true) certify_class;
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   541
20455
e671d9eac6c8 removed some dictionary stuff
haftmann
parents: 20428
diff changeset
   542
end; (*local*)
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   543
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   544
local
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   545
21954
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   546
fun gen_instance_sort prep_class prep_sort (raw_class, raw_sort) theory =
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   547
  let
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   548
    val class = prep_class theory raw_class;
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   549
    val sort = prep_sort theory raw_sort;
22302
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   550
    val is_class = is_some o lookup_class_data theory;
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   551
  in if is_class class andalso forall is_class sort then
21954
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   552
    theory
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   553
    |> instance_sort' (Locale.interpretation_in_locale o ProofContext.theory) (class, sort)
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   554
  else case sort
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   555
   of [class'] =>
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   556
        theory
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   557
        |> axclass_instance_sort (class, class')
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   558
    | _ => error ("Exactly one class expected: " ^ Sign.string_of_sort theory sort)
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   559
  end;
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   560
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   561
in
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   562
21954
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   563
val instance_sort_e = gen_instance_sort Sign.read_class Sign.read_sort;
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   564
val instance_sort = gen_instance_sort Sign.certify_class Sign.certify_sort;
19110
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   565
4bda27adcd2e moved intro_classes from AxClass to ClassPackage
haftmann
parents: 19102
diff changeset
   566
end; (* local *)
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 18963
diff changeset
   567
19957
91ba241a1678 reduced code, better instance command
haftmann
parents: 19953
diff changeset
   568
21954
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   569
(** experimental class target **)
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   570
22302
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   571
fun add_def_in_class lthy loc ((c, syn), ((name, atts), (rhs, eq))) thy =
21954
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   572
  let
22302
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   573
    val SOME class = class_of_locale thy loc;
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   574
    val rhs' = global_term thy [class] rhs;
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   575
    val (syn', _) = fork_mixfix true true syn;
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   576
    val ty = Term.fastype_of rhs';
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   577
    fun add_const (c, ty, syn) =
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   578
      Sign.add_consts_authentic [(c, ty, syn)]
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   579
      #> pair (Sign.full_name thy c, ty);
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   580
    fun add_def ((name, atts), prop) thy =
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   581
      thy
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   582
      |> PureThy.add_defs_i false [((name, prop), map (Attrib.attribute thy) atts)]
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   583
      |>> the_single;
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   584
    (*val _ = Output.info "------ DEF ------";
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   585
    val _ = Output.info c;
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   586
    val _ = Output.info name;
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   587
    val _ = (Output.info o Sign.string_of_term thy) rhs';
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   588
    val eq' = Morphism.thm (LocalTheory.target_morphism lthy) eq;
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   589
    val _ = (Output.info o string_of_thm) eq;
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   590
    val _ = (Output.info o string_of_thm) eq';
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   591
    val witness = the_witness thy class;
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   592
    val _ = Output.info "------ WIT ------";
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   593
    fun print_wit (t, thm) = "(" ^ Sign.string_of_term thy t ^ ", " ^ Display.string_of_thm thm ^ ")"
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   594
    val _ = (Output.info o cat_lines o map (print_wit o Element.dest_witness)) witness;
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   595
    val _ = (Output.info o string_of_thm) eq';
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   596
    val eq'' = Element.satisfy_thm witness eq';
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   597
    val _ = (Output.info o string_of_thm) eq'';*)
21954
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   598
  in
22302
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   599
    thy
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   600
    (*|> add_const (c, ty, syn')
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   601
    |-> (fn c => add_def ((name, atts), Logic.mk_equals (Const c, rhs')))
bf5bdb7f7607 added experimental class target
haftmann
parents: 22222
diff changeset
   602
    |-> (fn global_def_thm => tap (fn _ => (Output.info o string_of_thm) global_def_thm))*)
21954
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   603
  end;
ffeb00290397 major restructurings
haftmann
parents: 21936
diff changeset
   604
20455
e671d9eac6c8 removed some dictionary stuff
haftmann
parents: 20428
diff changeset
   605
end;