src/Pure/Isar/class_declaration.ML
author wenzelm
Mon Jun 03 17:01:28 2019 +0200 (2 months ago)
changeset 70309 f9fd15d3cfac
parent 69058 f4fb93197670
child 70387 35dd9212bf29
permissions -rw-r--r--
tuned;
haftmann@38379
     1
(*  Title:      Pure/Isar/class_declaration.ML
haftmann@24218
     2
    Author:     Florian Haftmann, TU Muenchen
haftmann@24218
     3
haftmann@38379
     4
Declaring classes and subclass relations.
haftmann@24218
     5
*)
haftmann@24218
     6
haftmann@38379
     7
signature CLASS_DECLARATION =
haftmann@24218
     8
sig
haftmann@57181
     9
  val class: binding -> class list ->
wenzelm@41585
    10
    Element.context_i list -> theory -> string * local_theory
haftmann@57181
    11
  val class_cmd: binding -> xstring list ->
wenzelm@41585
    12
    Element.context list -> theory -> string * local_theory
haftmann@57181
    13
  val prove_subclass: tactic -> class ->
wenzelm@41585
    14
    local_theory -> local_theory
haftmann@57181
    15
  val subclass: class -> local_theory -> Proof.state
haftmann@57181
    16
  val subclass_cmd: xstring -> local_theory -> Proof.state
haftmann@24218
    17
end;
haftmann@24218
    18
haftmann@38379
    19
structure Class_Declaration: CLASS_DECLARATION =
haftmann@24218
    20
struct
haftmann@24218
    21
haftmann@29665
    22
(** class definitions **)
haftmann@24218
    23
haftmann@24218
    24
local
haftmann@24218
    25
haftmann@29665
    26
(* calculating class-related rules including canonical interpretation *)
haftmann@29665
    27
haftmann@29547
    28
fun calculate thy class sups base_sort param_map assm_axiom =
haftmann@29547
    29
  let
wenzelm@70309
    30
    val thy_ctxt = Proof_Context.init_global thy;
haftmann@29547
    31
wenzelm@67699
    32
    val certT = Thm.trim_context_ctyp o Thm.global_ctyp_of thy;
wenzelm@67699
    33
    val cert = Thm.trim_context_cterm o Thm.global_cterm_of thy;
wenzelm@67699
    34
haftmann@29547
    35
    (* instantiation of canonical interpretation *)
wenzelm@67699
    36
    val a_tfree = (Name.aT, base_sort);
wenzelm@67699
    37
    val a_type = TFree a_tfree;
haftmann@29627
    38
    val param_map_const = (map o apsnd) Const param_map;
wenzelm@67699
    39
    val param_map_inst = param_map |> map (fn (x, (c, T)) =>
wenzelm@67699
    40
      let val T' = map_atyps (K a_type) T in ((x, T'), cert (Const (c, T'))) end);
wenzelm@67699
    41
    val const_morph =
wenzelm@67699
    42
      Element.instantiate_normalize_morphism ([], param_map_inst);
wenzelm@67699
    43
    val typ_morph =
wenzelm@67699
    44
      Element.instantiate_normalize_morphism ([(a_tfree, certT (TFree (Name.aT, [class])))], [])
wenzelm@70309
    45
    val (([raw_props], _, [(_, raw_inst_morph)], _, export_morph), _) = thy_ctxt
haftmann@29547
    46
      |> Expression.cert_goal_expression ([(class, (("", false),
ballarin@67450
    47
           (Expression.Named param_map_const, [])))], []);
wenzelm@45431
    48
    val (props, inst_morph) =
wenzelm@45431
    49
      if null param_map
haftmann@29797
    50
      then (raw_props |> map (Morphism.term typ_morph),
haftmann@29797
    51
        raw_inst_morph $> typ_morph)
haftmann@29797
    52
      else (raw_props, raw_inst_morph); (*FIXME proper handling in
haftmann@29797
    53
        locale.ML / expression.ML would be desirable*)
haftmann@29547
    54
haftmann@29547
    55
    (* witness for canonical interpretation *)
haftmann@52636
    56
    val some_prop = try the_single props;
haftmann@52636
    57
    val some_witn = Option.map (fn prop =>
wenzelm@45431
    58
      let
haftmann@38379
    59
        val sup_axioms = map_filter (fst o Class.rules thy) sups;
wenzelm@45431
    60
        val loc_intro_tac =
wenzelm@45431
    61
          (case Locale.intros_of thy class of
wenzelm@45431
    62
            (_, NONE) => all_tac
wenzelm@70309
    63
          | (_, SOME intro) => ALLGOALS (resolve_tac thy_ctxt [intro]));
haftmann@29547
    64
        val tac = loc_intro_tac
wenzelm@70309
    65
          THEN ALLGOALS (Proof_Context.fact_tac thy_ctxt (sup_axioms @ the_list assm_axiom));
wenzelm@70309
    66
      in Element.prove_witness thy_ctxt prop tac end) some_prop;
wenzelm@70309
    67
    val some_axiom = Option.map (Element.conclude_witness thy_ctxt) some_witn;
haftmann@29547
    68
haftmann@29547
    69
    (* canonical interpretation *)
haftmann@29547
    70
    val base_morph = inst_morph
wenzelm@54740
    71
      $> Morphism.binding_morphism "class_binding" (Binding.prefix false (Class.class_prefix class))
haftmann@52636
    72
      $> Element.satisfy_morphism (the_list some_witn);
haftmann@38379
    73
    val eq_morph = Element.eq_morphism thy (Class.these_defs thy sups);
haftmann@29547
    74
haftmann@29547
    75
    (* assm_intro *)
wenzelm@30344
    76
    fun prove_assm_intro thm =
haftmann@29547
    77
      let
wenzelm@70309
    78
        val ((_, [thm']), _) = Variable.import true [thm] thy_ctxt;
wenzelm@45431
    79
        val const_eq_morph =
wenzelm@45431
    80
          (case eq_morph of
wenzelm@46856
    81
            SOME eq_morph => const_morph $> eq_morph
wenzelm@45431
    82
          | NONE => const_morph);
haftmann@36674
    83
        val thm'' = Morphism.thm const_eq_morph thm';
wenzelm@54742
    84
      in
wenzelm@54742
    85
        Goal.prove_sorry_global thy [] [] (Thm.prop_of thm'')
wenzelm@54742
    86
          (fn {context = ctxt, ...} => ALLGOALS (Proof_Context.fact_tac ctxt [thm'']))
wenzelm@54742
    87
      end;
haftmann@52636
    88
    val some_assm_intro = Option.map prove_assm_intro (fst (Locale.intros_of thy class));
haftmann@29547
    89
haftmann@29547
    90
    (* of_class *)
wenzelm@67699
    91
    val of_class_prop_concl = Logic.mk_of_class (a_type, class);
wenzelm@45431
    92
    val of_class_prop =
haftmann@52636
    93
      (case some_prop of
wenzelm@45431
    94
        NONE => of_class_prop_concl
haftmann@29627
    95
      | SOME prop => Logic.mk_implies (Morphism.term const_morph
wenzelm@67699
    96
          ((map_types o map_atyps) (K a_type) prop), of_class_prop_concl));
haftmann@38379
    97
    val sup_of_classes = map (snd o Class.rules thy) sups;
wenzelm@35021
    98
    val loc_axiom_intros = map Drule.export_without_context_open (Locale.axioms_of thy class);
wenzelm@51685
    99
    val axclass_intro = #intro (Axclass.get_info thy class);
wenzelm@67699
   100
    val base_sort_trivs = Thm.of_sort (Thm.global_ctyp_of thy a_type, base_sort);
wenzelm@58957
   101
    fun tac ctxt =
wenzelm@45431
   102
      REPEAT (SOMEGOAL
wenzelm@58957
   103
        (match_tac ctxt (axclass_intro :: sup_of_classes @ loc_axiom_intros @ base_sort_trivs)
wenzelm@58963
   104
          ORELSE' assume_tac ctxt));
wenzelm@58957
   105
    val of_class = Goal.prove_sorry_global thy [] [] of_class_prop (tac o #context);
haftmann@29547
   106
haftmann@52636
   107
  in (base_morph, eq_morph, export_morph, some_axiom, some_assm_intro, of_class) end;
haftmann@29547
   108
haftmann@29632
   109
haftmann@29665
   110
(* reading and processing class specifications *)
haftmann@29665
   111
haftmann@36460
   112
fun prep_class_elems prep_decl thy sups raw_elems =
haftmann@29632
   113
  let
haftmann@29665
   114
    (* user space type system: only permits 'a type variable, improves towards 'a *)
haftmann@36460
   115
    val algebra = Sign.classes_of thy;
haftmann@36460
   116
    val inter_sort = curry (Sorts.inter_sort algebra);
wenzelm@45421
   117
    val proto_base_sort =
wenzelm@45421
   118
      if null sups then Sign.defaultS thy
haftmann@38379
   119
      else fold inter_sort (map (Class.base_sort thy) sups) [];
haftmann@58294
   120
    val is_param = member (op =) (map fst (Class.these_params thy sups));
haftmann@29665
   121
    val base_constraints = (map o apsnd)
haftmann@60346
   122
      (map_type_tfree (K (TVar ((Name.aT, 0), proto_base_sort))) o fst o fst o snd)
haftmann@38379
   123
        (Class.these_operations thy sups);
wenzelm@62952
   124
    val singleton_fixate = burrow_types (fn Ts =>
haftmann@29665
   125
      let
haftmann@58293
   126
        val tfrees = fold Term.add_tfreesT Ts [];
wenzelm@45433
   127
        val inferred_sort =
haftmann@58293
   128
          (fold o fold_atyps) (fn TVar (_, S) => inter_sort S | _ => I) Ts [];
wenzelm@45421
   129
        val fixate_sort =
wenzelm@45432
   130
          (case tfrees of
wenzelm@45432
   131
            [] => inferred_sort
wenzelm@45433
   132
          | [(a, S)] =>
wenzelm@45433
   133
              if a <> Name.aT then
wenzelm@45433
   134
                error ("No type variable other than " ^ Name.aT ^ " allowed in class specification")
wenzelm@45433
   135
              else if Sorts.sort_le algebra (S, inferred_sort) then S
wenzelm@45432
   136
              else
wenzelm@45432
   137
                error ("Type inference imposes additional sort constraint " ^
wenzelm@45432
   138
                  Syntax.string_of_sort_global thy inferred_sort ^
wenzelm@45432
   139
                  " of type parameter " ^ Name.aT ^ " of sort " ^
wenzelm@45432
   140
                  Syntax.string_of_sort_global thy S)
wenzelm@45432
   141
          | _ => error "Multiple type variables in class specification");
wenzelm@45432
   142
        val fixateT = TFree (Name.aT, fixate_sort);
wenzelm@45432
   143
      in
haftmann@58293
   144
        (map o map_atyps)
haftmann@58293
   145
          (fn T as TVar (xi, _) => if Type_Infer.is_param xi then fixateT else T | T => T) Ts
wenzelm@62952
   146
      end);
wenzelm@62952
   147
    fun unify_params ts =
haftmann@40188
   148
      let
haftmann@58294
   149
        val param_Ts = (fold o fold_aterms)
haftmann@58294
   150
          (fn Free (v, T) => if is_param v then fold_atyps (insert (op =)) T else I | _ => I) ts [];
haftmann@58319
   151
        val param_namesT = map_filter (try (fst o dest_TVar)) param_Ts;
haftmann@58319
   152
        val param_T = if null param_namesT then NONE
haftmann@58294
   153
          else SOME (case get_first (try dest_TFree) param_Ts of
haftmann@58294
   154
            SOME v_sort => TFree v_sort |
haftmann@58319
   155
            NONE => TVar (hd param_namesT, proto_base_sort));
haftmann@58294
   156
      in case param_T of
haftmann@58294
   157
        NONE => ts |
haftmann@58319
   158
        SOME T => map (subst_TVars (map (rpair T) param_namesT)) ts
haftmann@40188
   159
      end;
haftmann@29632
   160
haftmann@35120
   161
    (* preprocessing elements, retrieving base sort from type-checked elements *)
wenzelm@42402
   162
    val raw_supexpr =
ballarin@67450
   163
      (map (fn sup => (sup, (("", false), (Expression.Positional [], [])))) sups, []);
wenzelm@42402
   164
    val init_class_body =
wenzelm@42402
   165
      fold (Proof_Context.add_const_constraint o apsnd SOME) base_constraints
haftmann@38379
   166
      #> Class.redeclare_operations thy sups
wenzelm@62952
   167
      #> Context.proof_map (Syntax_Phases.term_check 0 "singleton_fixate" (K singleton_fixate));
wenzelm@47311
   168
    val ((raw_supparams, _, raw_inferred_elems, _), _) =
wenzelm@42402
   169
      Proof_Context.init_global thy
wenzelm@62952
   170
      |> Context.proof_map (Syntax_Phases.term_check 0 "unify_params" (K unify_params))
haftmann@32206
   171
      |> prep_decl raw_supexpr init_class_body raw_elems;
haftmann@38435
   172
    fun filter_element (Element.Fixes []) = NONE
haftmann@38435
   173
      | filter_element (e as Element.Fixes _) = SOME e
haftmann@38435
   174
      | filter_element (Element.Constrains []) = NONE
haftmann@38435
   175
      | filter_element (e as Element.Constrains _) = SOME e
haftmann@38435
   176
      | filter_element (Element.Assumes []) = NONE
haftmann@38435
   177
      | filter_element (e as Element.Assumes _) = SOME e
wenzelm@45421
   178
      | filter_element (Element.Defines _) =
wenzelm@45421
   179
          error ("\"defines\" element not allowed in class specification.")
wenzelm@45421
   180
      | filter_element (Element.Notes _) =
wenzelm@67671
   181
          error ("\"notes\" element not allowed in class specification.")
wenzelm@67671
   182
      | filter_element (Element.Lazy_Notes _) =
wenzelm@45421
   183
          error ("\"notes\" element not allowed in class specification.");
haftmann@38435
   184
    val inferred_elems = map_filter filter_element raw_inferred_elems;
haftmann@29665
   185
    fun fold_element_types f (Element.Fixes fxs) = fold (fn (_, SOME T, _) => f T) fxs
haftmann@29665
   186
      | fold_element_types f (Element.Constrains cnstrs) = fold (f o snd) cnstrs
haftmann@29665
   187
      | fold_element_types f (Element.Assumes assms) = fold (fold (fn (t, ts) =>
wenzelm@42402
   188
          fold_types f t #> (fold o fold_types) f ts) o snd) assms;
wenzelm@45421
   189
    val base_sort =
wenzelm@45421
   190
      if null inferred_elems then proto_base_sort
wenzelm@45421
   191
      else
wenzelm@45421
   192
        (case (fold o fold_element_types) Term.add_tfreesT inferred_elems [] of
wenzelm@45421
   193
          [] => error "No type variable in class specification"
haftmann@29665
   194
        | [(_, sort)] => sort
wenzelm@45421
   195
        | _ => error "Multiple type variables in class specification");
haftmann@32206
   196
    val supparams = map (fn ((c, T), _) =>
haftmann@32206
   197
      (c, map_atyps (K (TFree (Name.aT, base_sort))) T)) raw_supparams;
haftmann@32206
   198
    val supparam_names = map fst supparams;
haftmann@32206
   199
    fun mk_param ((c, _), _) = Free (c, (the o AList.lookup (op =) supparams) c);
haftmann@32206
   200
    val supexpr = (map (fn sup => (sup, (("", false),
ballarin@67450
   201
      (Expression.Positional (map (SOME o mk_param) (Locale.params_of thy sup)), [])))) sups,
haftmann@32206
   202
        map (fn (c, T) => (Binding.name c, SOME T, NoSyn)) supparams);
haftmann@29575
   203
haftmann@32206
   204
  in (base_sort, supparam_names, supexpr, inferred_elems) end;
haftmann@29575
   205
haftmann@29665
   206
val cert_class_elems = prep_class_elems Expression.cert_declaration;
haftmann@29665
   207
val read_class_elems = prep_class_elems Expression.cert_read_declaration;
haftmann@29575
   208
haftmann@29665
   209
fun prep_class_spec prep_class prep_class_elems thy raw_supclasses raw_elems =
haftmann@29575
   210
  let
wenzelm@46922
   211
    val thy_ctxt = Proof_Context.init_global thy;
haftmann@29665
   212
haftmann@29575
   213
    (* prepare import *)
haftmann@29575
   214
    val inter_sort = curry (Sorts.inter_sort (Sign.classes_of thy));
wenzelm@46922
   215
    val sups = Sign.minimize_sort thy (map (prep_class thy_ctxt) raw_supclasses);
wenzelm@45431
   216
    val _ =
wenzelm@45431
   217
      (case filter_out (Class.is_class thy) sups of
wenzelm@45431
   218
        [] => ()
wenzelm@45431
   219
      | no_classes => error ("No (proper) classes: " ^ commas_quote no_classes));
haftmann@38379
   220
    val raw_supparams = (map o apsnd) (snd o snd) (Class.these_params thy sups);
haftmann@32206
   221
    val raw_supparam_names = map fst raw_supparams;
wenzelm@45431
   222
    val _ =
wenzelm@45431
   223
      if has_duplicates (op =) raw_supparam_names then
wenzelm@45431
   224
        error ("Duplicate parameter(s) in superclasses: " ^
wenzelm@45431
   225
          (commas_quote (duplicates (op =) raw_supparam_names)))
haftmann@29575
   226
      else ();
haftmann@29575
   227
haftmann@29575
   228
    (* infer types and base sort *)
wenzelm@45431
   229
    val (base_sort, supparam_names, supexpr, inferred_elems) = prep_class_elems thy sups raw_elems;
haftmann@32206
   230
    val sup_sort = inter_sort base_sort sups;
haftmann@29575
   231
haftmann@29575
   232
    (* process elements as class specification *)
wenzelm@46922
   233
    val class_ctxt = Class.begin sups base_sort thy_ctxt;
wenzelm@47311
   234
    val ((_, _, syntax_elems, _), _) = class_ctxt
haftmann@29702
   235
      |> Expression.cert_declaration supexpr I inferred_elems;
wenzelm@45431
   236
    fun check_vars e vs =
wenzelm@45431
   237
      if null vs then
wenzelm@45431
   238
        error ("No type variable in part of specification element " ^
wenzelm@45431
   239
          Pretty.string_of (Pretty.chunks (Element.pretty_ctxt class_ctxt e)))
haftmann@29665
   240
      else ();
haftmann@29665
   241
    fun check_element (e as Element.Fixes fxs) =
wenzelm@45431
   242
          List.app (fn (_, SOME T, _) => check_vars e (Term.add_tfreesT T [])) fxs
haftmann@29665
   243
      | check_element (e as Element.Assumes assms) =
wenzelm@45431
   244
          List.app (fn (_, ts_pss) =>
wenzelm@45431
   245
            List.app (fn (t, _) => check_vars e (Term.add_tfrees t [])) ts_pss) assms
wenzelm@45431
   246
      | check_element _ = ();
wenzelm@45431
   247
    val _ = List.app check_element syntax_elems;
haftmann@29665
   248
    fun fork_syn (Element.Fixes xs) =
wenzelm@30344
   249
          fold_map (fn (c, ty, syn) => cons (c, syn) #> pair (c, ty, NoSyn)) xs
haftmann@29665
   250
          #>> Element.Fixes
haftmann@29665
   251
      | fork_syn x = pair x;
haftmann@29575
   252
    val (elems, global_syntax) = fold_map fork_syn syntax_elems [];
haftmann@29665
   253
haftmann@32713
   254
  in (((sups, supparam_names), (sup_sort, base_sort, supexpr)), (elems, global_syntax)) end;
haftmann@29575
   255
haftmann@29665
   256
val cert_class_spec = prep_class_spec (K I) cert_class_elems;
wenzelm@46922
   257
val read_class_spec = prep_class_spec Proof_Context.read_class read_class_elems;
haftmann@29665
   258
haftmann@29665
   259
haftmann@29665
   260
(* class establishment *)
haftmann@29575
   261
haftmann@32206
   262
fun add_consts class base_sort sups supparam_names global_syntax thy =
wenzelm@24968
   263
  let
haftmann@29816
   264
    (*FIXME simplify*)
haftmann@32206
   265
    val supconsts = supparam_names
haftmann@38379
   266
      |> AList.make (snd o the o AList.lookup (op =) (Class.these_params thy sups))
haftmann@25683
   267
      |> (map o apsnd o apsnd o map_atyps o K o TFree) (Name.aT, [class]);
haftmann@29509
   268
    val all_params = Locale.params_of thy class;
haftmann@32206
   269
    val raw_params = (snd o chop (length supparam_names)) all_params;
wenzelm@30755
   270
    fun add_const ((raw_c, raw_ty), _) thy =
haftmann@25683
   271
      let
wenzelm@30755
   272
        val b = Binding.name raw_c;
wenzelm@30344
   273
        val c = Sign.full_name thy b;
haftmann@25683
   274
        val ty = map_atyps (K (TFree (Name.aT, base_sort))) raw_ty;
haftmann@25683
   275
        val ty0 = Type.strip_sorts ty;
haftmann@25683
   276
        val ty' = map_atyps (K (TFree (Name.aT, [class]))) ty0;
wenzelm@30344
   277
        val syn = (the_default NoSyn o AList.lookup Binding.eq_name global_syntax) b;
haftmann@25683
   278
      in
haftmann@25683
   279
        thy
wenzelm@42375
   280
        |> Sign.declare_const_global ((b, ty0), syn)
haftmann@25683
   281
        |> snd
wenzelm@42494
   282
        |> pair ((Variable.check_name b, ty), (c, ty'))
haftmann@25683
   283
      end;
haftmann@28715
   284
  in
haftmann@28715
   285
    thy
haftmann@38379
   286
    |> Sign.add_path (Class.class_prefix class)
haftmann@28715
   287
    |> fold_map add_const raw_params
haftmann@28715
   288
    ||> Sign.restore_naming thy
haftmann@28715
   289
    |-> (fn params => pair (supconsts @ (map o apfst) fst params, params))
haftmann@28715
   290
  end;
haftmann@28715
   291
haftmann@32206
   292
fun adjungate_axclass bname class base_sort sups supsort supparam_names global_syntax thy =
haftmann@28715
   293
  let
haftmann@29816
   294
    (*FIXME simplify*)
haftmann@25683
   295
    fun globalize param_map = map_aterms
haftmann@25683
   296
      (fn Free (v, ty) => Const ((fst o the o AList.lookup (op =) param_map) v, ty)
haftmann@25683
   297
        | t => t);
haftmann@29509
   298
    val raw_pred = Locale.intros_of thy class
haftmann@25683
   299
      |> fst
wenzelm@35845
   300
      |> Option.map (Logic.unvarify_global o Logic.strip_imp_concl o Thm.prop_of);
wenzelm@45431
   301
    fun get_axiom thy =
wenzelm@51685
   302
      (case #axioms (Axclass.get_info thy class) of
wenzelm@45431
   303
         [] => NONE
wenzelm@45431
   304
      | [thm] => SOME thm);
wenzelm@24968
   305
  in
wenzelm@24968
   306
    thy
wenzelm@63352
   307
    |> add_consts class base_sort sups supparam_names global_syntax |-> (fn (param_map, params) =>
wenzelm@63352
   308
      Axclass.define_class (bname, supsort)
wenzelm@63352
   309
        (map (fst o snd) params)
wenzelm@63352
   310
          [(Binding.empty_atts, Option.map (globalize param_map) raw_pred |> the_list)]
wenzelm@63352
   311
      #> snd
wenzelm@63352
   312
      #> `get_axiom
wenzelm@63352
   313
      #-> (fn assm_axiom => fold (Sign.add_const_constraint o apsnd SOME o snd) params
wenzelm@63352
   314
      #> pair (param_map, params, assm_axiom)))
wenzelm@24968
   315
  end;
wenzelm@24968
   316
haftmann@57181
   317
fun gen_class prep_class_spec b raw_supclasses raw_elems thy =
haftmann@24748
   318
  let
haftmann@36635
   319
    val class = Sign.full_name thy b;
haftmann@56921
   320
    val prefix = Binding.qualify true "class";
haftmann@32206
   321
    val (((sups, supparam_names), (supsort, base_sort, supexpr)), (elems, global_syntax)) =
haftmann@32206
   322
      prep_class_spec thy raw_supclasses raw_elems;
haftmann@24218
   323
  in
haftmann@24218
   324
    thy
haftmann@57181
   325
    |> Expression.add_locale b (prefix b) supexpr elems
wenzelm@33671
   326
    |> snd |> Local_Theory.exit_global
haftmann@36635
   327
    |> adjungate_axclass b class base_sort sups supsort supparam_names global_syntax
haftmann@29526
   328
    |-> (fn (param_map, params, assm_axiom) =>
haftmann@29547
   329
       `(fn thy => calculate thy class sups base_sort param_map assm_axiom)
haftmann@52636
   330
    #-> (fn (base_morph, eq_morph, export_morph, some_axiom, some_assm_intro, of_class) =>
wenzelm@68853
   331
       Locale.add_registration_theory
wenzelm@69058
   332
         {inst = (class, base_morph),
wenzelm@68851
   333
           mixin = Option.map (rpair true) eq_morph,
wenzelm@68853
   334
           export = export_morph}
haftmann@56921
   335
    #> Class.register class sups params base_sort base_morph export_morph some_axiom some_assm_intro of_class
haftmann@56921
   336
    #> Global_Theory.store_thm (prefix (Binding.qualified_name (class ^ ".of_class.intro")), of_class)))
haftmann@56921
   337
    |> snd
haftmann@66334
   338
    |> Named_Target.init class
haftmann@25038
   339
    |> pair class
haftmann@24218
   340
  end;
haftmann@24218
   341
haftmann@24218
   342
in
haftmann@24218
   343
haftmann@29509
   344
val class = gen_class cert_class_spec;
haftmann@26518
   345
val class_cmd = gen_class read_class_spec;
haftmann@24218
   346
haftmann@24218
   347
end; (*local*)
haftmann@24218
   348
haftmann@24218
   349
wenzelm@45421
   350
haftmann@29358
   351
(** subclass relations **)
haftmann@25462
   352
haftmann@29358
   353
local
haftmann@25462
   354
haftmann@57181
   355
fun gen_subclass prep_class do_proof raw_sup lthy =
haftmann@25462
   356
  let
wenzelm@42360
   357
    val thy = Proof_Context.theory_of lthy;
haftmann@29558
   358
    val proto_sup = prep_class thy raw_sup;
wenzelm@63268
   359
    val proto_sub =
wenzelm@63268
   360
      (case Named_Target.class_of lthy of
haftmann@57182
   361
        SOME class => class
wenzelm@63268
   362
      | NONE => error "Not in a class target");
wenzelm@51685
   363
    val (sub, sup) = Axclass.cert_classrel thy (proto_sub, proto_sup);
haftmann@29509
   364
ballarin@67450
   365
    val expr = ([(sup, (("", false), (Expression.Positional [], [])))], []);
ballarin@67450
   366
    val (([props], _, deps, _, export), goal_ctxt) =
haftmann@29509
   367
      Expression.cert_goal_expression expr lthy;
haftmann@29526
   368
    val some_prop = try the_single props;
haftmann@29558
   369
    val some_dep_morph = try the_single (map snd deps);
haftmann@29558
   370
    fun after_qed some_wit =
haftmann@54866
   371
      Class.register_subclass (sub, sup) some_dep_morph some_wit export;
haftmann@29558
   372
  in do_proof after_qed some_prop goal_ctxt end;
haftmann@25485
   373
haftmann@29575
   374
fun user_proof after_qed some_prop =
haftmann@29575
   375
  Element.witness_proof (after_qed o try the_single o the_single)
haftmann@29575
   376
    [the_list some_prop];
haftmann@25485
   377
haftmann@29575
   378
fun tactic_proof tac after_qed some_prop ctxt =
haftmann@29575
   379
  after_qed (Option.map
haftmann@29575
   380
    (fn prop => Element.prove_witness ctxt prop tac) some_prop) ctxt;
haftmann@28666
   381
haftmann@29358
   382
in
haftmann@28666
   383
haftmann@57181
   384
fun prove_subclass tac = gen_subclass (K I) (tactic_proof tac);
wenzelm@54882
   385
wenzelm@54882
   386
fun subclass x = gen_subclass (K I) user_proof x;
wenzelm@54882
   387
fun subclass_cmd x =
wenzelm@54882
   388
  gen_subclass (Proof_Context.read_class o Proof_Context.init_global) user_proof x;
haftmann@25462
   389
haftmann@29358
   390
end; (*local*)
haftmann@29358
   391
haftmann@24218
   392
end;