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