src/Pure/Isar/class.ML
author haftmann
Wed Jan 07 08:03:25 2009 +0100 (2009-01-07)
changeset 29378 2821c2c5270d
parent 29360 a5be60c3674e
child 29439 83601bdadae8
permissions -rw-r--r--
proper local_theory after Class.class
haftmann@29358
     1
(*  Title:      Pure/Isar/ML
haftmann@24218
     2
    Author:     Florian Haftmann, TU Muenchen
haftmann@24218
     3
haftmann@29358
     4
Type classes derived from primitive axclasses and locales - interfaces
haftmann@24218
     5
*)
haftmann@24218
     6
haftmann@24218
     7
signature CLASS =
haftmann@24218
     8
sig
haftmann@29358
     9
  include CLASS_TARGET
haftmann@29358
    10
    (*FIXME the split in class_target.ML, theory_target.ML and
haftmann@29358
    11
      ML is artificial*)
haftmann@29358
    12
haftmann@26247
    13
  val class: bstring -> class list -> Element.context_i list
haftmann@29378
    14
    -> theory -> string * local_theory
haftmann@26247
    15
  val class_cmd: bstring -> xstring list -> Element.context list
haftmann@29378
    16
    -> theory -> string * local_theory
haftmann@29358
    17
  val prove_subclass: tactic -> class -> local_theory -> local_theory
haftmann@29358
    18
  val subclass: class -> local_theory -> Proof.state
haftmann@29358
    19
  val subclass_cmd: xstring -> local_theory -> Proof.state
haftmann@24218
    20
end;
haftmann@24218
    21
haftmann@24218
    22
structure Class : CLASS =
haftmann@24218
    23
struct
haftmann@24218
    24
haftmann@29358
    25
open Class_Target;
haftmann@28715
    26
haftmann@29358
    27
(** rule calculation **)
haftmann@24589
    28
haftmann@28715
    29
fun calculate_axiom thy sups base_sort assm_axiom param_map class =
haftmann@29360
    30
  case Old_Locale.intros thy class
haftmann@28715
    31
   of (_, []) => assm_axiom
haftmann@28715
    32
    | (_, [intro]) =>
haftmann@28715
    33
      let
haftmann@28715
    34
        fun instantiate thy sort = Thm.instantiate ([pairself (Thm.ctyp_of thy o TVar o pair (Name.aT, 0))
haftmann@28715
    35
          (base_sort, sort)], map (fn (v, (c, ty)) => pairself (Thm.cterm_of thy)
haftmann@28715
    36
            (Var ((v, 0), map_atyps (fn _ => TVar ((Name.aT, 0), sort)) ty),
haftmann@28715
    37
              Const (c, map_atyps (fn _ => TVar ((Name.aT, 0), sort)) ty))) param_map);
haftmann@29358
    38
        val axiom_premises = map_filter (fst o rules thy) sups
haftmann@28715
    39
          @ the_list assm_axiom;
haftmann@28715
    40
      in intro
haftmann@28715
    41
        |> instantiate thy [class]
haftmann@28715
    42
        |> (fn thm => thm OF axiom_premises)
haftmann@28715
    43
        |> Drule.standard'
haftmann@28715
    44
        |> Thm.close_derivation
haftmann@28715
    45
        |> SOME
haftmann@28715
    46
      end;
wenzelm@25024
    47
haftmann@28715
    48
fun calculate_rules thy phi sups base_sort param_map axiom class =
haftmann@25062
    49
  let
haftmann@25711
    50
    fun instantiate thy sort = Thm.instantiate ([pairself (Thm.ctyp_of thy o TVar o pair (Name.aT, 0))
haftmann@25711
    51
      (base_sort, sort)], map (fn (v, (c, ty)) => pairself (Thm.cterm_of thy)
haftmann@25711
    52
        (Var ((v, 0), map_atyps (fn _ => TVar ((Name.aT, 0), sort)) ty),
haftmann@25711
    53
          Const (c, map_atyps (fn _ => TVar ((Name.aT, 0), sort)) ty))) param_map);
haftmann@25711
    54
    val defs = these_defs thy sups;
haftmann@29360
    55
    val assm_intro = Old_Locale.intros thy class
haftmann@28715
    56
      |> fst
haftmann@28715
    57
      |> map (instantiate thy base_sort)
haftmann@28715
    58
      |> map (MetaSimplifier.rewrite_rule defs)
haftmann@28715
    59
      |> map Thm.close_derivation
haftmann@28715
    60
      |> try the_single;
haftmann@25711
    61
    val fixate = Thm.instantiate
haftmann@25711
    62
      (map (pairself (Thm.ctyp_of thy)) [(TVar ((Name.aT, 0), []), TFree (Name.aT, base_sort)),
haftmann@25711
    63
        (TVar ((Name.aT, 0), base_sort), TFree (Name.aT, base_sort))], [])
haftmann@25618
    64
    val of_class_sups = if null sups
haftmann@25711
    65
      then map (fixate o Thm.class_triv thy) base_sort
haftmann@29358
    66
      else map (fixate o snd o rules thy) sups;
haftmann@29360
    67
    val locale_dests = map Drule.standard' (Old_Locale.dests thy class);
haftmann@25711
    68
    val num_trivs = case length locale_dests
haftmann@25711
    69
     of 0 => if is_none axiom then 0 else 1
haftmann@25711
    70
      | n => n;
haftmann@25711
    71
    val pred_trivs = if num_trivs = 0 then []
haftmann@25711
    72
      else the axiom
haftmann@25711
    73
        |> Thm.prop_of
haftmann@25711
    74
        |> (map_types o map_atyps o K) (TFree (Name.aT, base_sort))
haftmann@25711
    75
        |> (Thm.assume o Thm.cterm_of thy)
haftmann@25711
    76
        |> replicate num_trivs;
haftmann@28715
    77
    val axclass_intro = (#intro o AxClass.get_info thy) class;
haftmann@28715
    78
    val of_class = (fixate axclass_intro OF of_class_sups OF locale_dests OF pred_trivs)
haftmann@25711
    79
      |> Drule.standard'
wenzelm@26628
    80
      |> Thm.close_derivation;
haftmann@28715
    81
  in (assm_intro, of_class) end;
haftmann@24218
    82
haftmann@28715
    83
fun note_assm_intro class assm_intro thy =
haftmann@28715
    84
  thy
haftmann@28715
    85
  |> Sign.sticky_prefix (class_prefix class ^ "_" ^ AxClass.axiomsN)
haftmann@28715
    86
  |> PureThy.store_thm (AxClass.introN, assm_intro)
haftmann@28715
    87
  |> snd
haftmann@28715
    88
  |> Sign.restore_naming thy;
haftmann@28715
    89
haftmann@24218
    90
haftmann@29358
    91
(** define classes **)
haftmann@24218
    92
haftmann@24218
    93
local
haftmann@24218
    94
haftmann@26247
    95
fun gen_class_spec prep_class process_expr thy raw_supclasses raw_elems =
haftmann@24218
    96
  let
haftmann@24748
    97
    val supclasses = map (prep_class thy) raw_supclasses;
haftmann@24748
    98
    val supsort = Sign.minimize_sort thy supclasses;
haftmann@25618
    99
    val sups = filter (is_class thy) supsort;
haftmann@26995
   100
    val supparam_names = map fst (these_params thy sups);
haftmann@26995
   101
    val _ = if has_duplicates (op =) supparam_names
haftmann@26995
   102
      then error ("Duplicate parameter(s) in superclasses: "
haftmann@26995
   103
        ^ (commas o map quote o duplicates (op =)) supparam_names)
haftmann@26995
   104
      else ();
haftmann@25618
   105
    val base_sort = if null sups then supsort else
haftmann@26167
   106
      foldr1 (Sorts.inter_sort (Sign.classes_of thy))
haftmann@29358
   107
        (map (base_sort thy) sups);
haftmann@29360
   108
    val suplocales = map Old_Locale.Locale sups;
haftmann@29360
   109
    val supexpr = Old_Locale.Merge suplocales;
haftmann@29360
   110
    val supparams = (map fst o Old_Locale.parameters_of_expr thy) supexpr;
haftmann@29360
   111
    val mergeexpr = Old_Locale.Merge suplocales;
haftmann@24748
   112
    val constrain = Element.Constrains ((map o apsnd o map_atyps)
haftmann@26167
   113
      (K (TFree (Name.aT, base_sort))) supparams);
haftmann@25683
   114
    fun fork_syn (Element.Fixes xs) =
haftmann@29006
   115
          fold_map (fn (c, ty, syn) => cons (Binding.base_name c, syn) #> pair (c, ty, NoSyn)) xs
haftmann@25683
   116
          #>> Element.Fixes
haftmann@25683
   117
      | fork_syn x = pair x;
haftmann@25683
   118
    fun fork_syntax elems =
haftmann@25683
   119
      let
haftmann@25683
   120
        val (elems', global_syntax) = fold_map fork_syn elems [];
haftmann@26247
   121
      in (constrain :: elems', global_syntax) end;
haftmann@25683
   122
    val (elems, global_syntax) =
haftmann@25683
   123
      ProofContext.init thy
haftmann@29360
   124
      |> Old_Locale.cert_expr supexpr [constrain]
haftmann@25683
   125
      |> snd
haftmann@29358
   126
      |> begin sups base_sort
haftmann@29360
   127
      |> process_expr Old_Locale.empty raw_elems
haftmann@25683
   128
      |> fst
haftmann@25683
   129
      |> fork_syntax
haftmann@25683
   130
  in (((sups, supparams), (supsort, base_sort, mergeexpr)), (elems, global_syntax)) end;
haftmann@24748
   131
haftmann@29360
   132
val read_class_spec = gen_class_spec Sign.intern_class Old_Locale.read_expr;
haftmann@29360
   133
val check_class_spec = gen_class_spec (K I) Old_Locale.cert_expr;
haftmann@24748
   134
haftmann@28715
   135
fun add_consts bname class base_sort sups supparams global_syntax thy =
wenzelm@24968
   136
  let
haftmann@25683
   137
    val supconsts = map fst supparams
haftmann@26518
   138
      |> AList.make (snd o the o AList.lookup (op =) (these_params thy sups))
haftmann@25683
   139
      |> (map o apsnd o apsnd o map_atyps o K o TFree) (Name.aT, [class]);
haftmann@29360
   140
    val all_params = map fst (Old_Locale.parameters_of thy class);
haftmann@28715
   141
    val raw_params = (snd o chop (length supparams)) all_params;
haftmann@25683
   142
    fun add_const (v, raw_ty) thy =
haftmann@25683
   143
      let
haftmann@28965
   144
        val c = Sign.full_bname thy v;
haftmann@25683
   145
        val ty = map_atyps (K (TFree (Name.aT, base_sort))) raw_ty;
haftmann@25683
   146
        val ty0 = Type.strip_sorts ty;
haftmann@25683
   147
        val ty' = map_atyps (K (TFree (Name.aT, [class]))) ty0;
haftmann@25683
   148
        val syn = (the_default NoSyn o AList.lookup (op =) global_syntax) v;
haftmann@25683
   149
      in
haftmann@25683
   150
        thy
haftmann@28965
   151
        |> Sign.declare_const [] ((Binding.name v, ty0), syn)
haftmann@25683
   152
        |> snd
haftmann@25683
   153
        |> pair ((v, ty), (c, ty'))
haftmann@25683
   154
      end;
haftmann@28715
   155
  in
haftmann@28715
   156
    thy
haftmann@28715
   157
    |> Sign.add_path (Logic.const_of_class bname)
haftmann@28715
   158
    |> fold_map add_const raw_params
haftmann@28715
   159
    ||> Sign.restore_naming thy
haftmann@28715
   160
    |-> (fn params => pair (supconsts @ (map o apfst) fst params, params))
haftmann@28715
   161
  end;
haftmann@28715
   162
haftmann@28715
   163
fun adjungate_axclass bname class base_sort sups supsort supparams global_syntax thy =
haftmann@28715
   164
  let
haftmann@25683
   165
    fun globalize param_map = map_aterms
haftmann@25683
   166
      (fn Free (v, ty) => Const ((fst o the o AList.lookup (op =) param_map) v, ty)
haftmann@25683
   167
        | t => t);
haftmann@29360
   168
    val raw_pred = Old_Locale.intros thy class
haftmann@25683
   169
      |> fst
haftmann@25683
   170
      |> map (Logic.unvarify o Logic.strip_imp_concl o Thm.prop_of);
haftmann@25683
   171
    fun get_axiom thy = case (#axioms o AxClass.get_info thy) class
haftmann@25683
   172
     of [] => NONE
haftmann@25683
   173
      | [thm] => SOME thm;
wenzelm@24968
   174
  in
wenzelm@24968
   175
    thy
haftmann@28715
   176
    |> add_consts bname class base_sort sups supparams global_syntax
haftmann@25683
   177
    |-> (fn (param_map, params) => AxClass.define_class (bname, supsort)
haftmann@26518
   178
          (map (fst o snd) params)
haftmann@28965
   179
          [((Binding.name (bname ^ "_" ^ AxClass.axiomsN), []), map (globalize param_map) raw_pred)]
haftmann@25683
   180
    #> snd
haftmann@25683
   181
    #> `get_axiom
haftmann@25683
   182
    #-> (fn assm_axiom => fold (Sign.add_const_constraint o apsnd SOME o snd) params
haftmann@28715
   183
    #> pair (map (Const o snd) param_map, param_map, params, assm_axiom)))
wenzelm@24968
   184
  end;
wenzelm@24968
   185
haftmann@26518
   186
fun gen_class prep_spec bname raw_supclasses raw_elems thy =
haftmann@24748
   187
  let
haftmann@28965
   188
    val class = Sign.full_bname thy bname;
haftmann@25683
   189
    val (((sups, supparams), (supsort, base_sort, mergeexpr)), (elems, global_syntax)) =
haftmann@26247
   190
      prep_spec thy raw_supclasses raw_elems;
haftmann@28715
   191
    val supconsts = map (apsnd fst o snd) (these_params thy sups);
haftmann@24218
   192
  in
haftmann@24218
   193
    thy
haftmann@29360
   194
    |> Old_Locale.add_locale "" bname mergeexpr elems
haftmann@25038
   195
    |> snd
haftmann@25311
   196
    |> ProofContext.theory_of
haftmann@26518
   197
    |> adjungate_axclass bname class base_sort sups supsort supparams global_syntax
haftmann@28715
   198
    |-> (fn (inst, param_map, params, assm_axiom) =>
haftmann@28715
   199
        `(fn thy => calculate_axiom thy sups base_sort assm_axiom param_map class)
haftmann@28715
   200
    #-> (fn axiom =>
haftmann@28715
   201
        prove_class_interpretation class inst
haftmann@28715
   202
          (supconsts @ map (pair class o fst o snd) params) (the_list axiom) []
haftmann@28715
   203
    #-> (fn morphism =>
haftmann@28715
   204
        `(fn thy => activate_class_morphism thy class inst morphism)
haftmann@28715
   205
    #-> (fn phi =>
haftmann@28715
   206
        `(fn thy => calculate_rules thy phi sups base_sort param_map axiom class)
haftmann@28715
   207
    #-> (fn (assm_intro, of_class) =>
haftmann@29358
   208
        register class sups params base_sort inst
haftmann@29358
   209
          morphism axiom assm_intro of_class
haftmann@28715
   210
    #> fold (note_assm_intro class) (the_list assm_intro))))))
haftmann@29378
   211
    |> TheoryTarget.init (SOME class)
haftmann@25038
   212
    |> pair class
haftmann@24218
   213
  end;
haftmann@24218
   214
haftmann@24218
   215
in
haftmann@24218
   216
haftmann@26518
   217
val class_cmd = gen_class read_class_spec;
haftmann@26518
   218
val class = gen_class check_class_spec;
haftmann@24218
   219
haftmann@24218
   220
end; (*local*)
haftmann@24218
   221
haftmann@24218
   222
haftmann@29358
   223
(** subclass relations **)
haftmann@25462
   224
haftmann@29358
   225
local
haftmann@25462
   226
haftmann@29358
   227
fun gen_subclass prep_class do_proof raw_sup lthy =
haftmann@25462
   228
  let
haftmann@29358
   229
    val thy = ProofContext.theory_of lthy;
haftmann@29358
   230
    val sup = prep_class thy raw_sup;
haftmann@29358
   231
    val sub = case TheoryTarget.peek lthy
haftmann@29358
   232
     of {is_class = false, ...} => error "Not a class context"
haftmann@29358
   233
      | {target, ...} => target;
haftmann@29358
   234
    val _ = if Sign.subsort thy ([sup], [sub])
haftmann@29358
   235
      then error ("Class " ^ Syntax.string_of_sort lthy [sup]
haftmann@29358
   236
        ^ " is subclass of class " ^ Syntax.string_of_sort lthy [sub])
haftmann@29358
   237
      else ();
haftmann@29358
   238
    val sub_params = map fst (these_params thy [sub]);
haftmann@29358
   239
    val sup_params = map fst (these_params thy [sup]);
haftmann@29358
   240
    val err_params = subtract (op =) sub_params sup_params;
haftmann@29358
   241
    val _ = if null err_params then [] else
haftmann@29358
   242
      error ("Class " ^ Syntax.string_of_sort lthy [sub] ^ " lacks parameter(s) " ^
haftmann@29358
   243
        commas_quote err_params ^ " of " ^ Syntax.string_of_sort lthy [sup]);
haftmann@29358
   244
    val sublocale_prop =
haftmann@29360
   245
      Old_Locale.global_asms_of thy sup
haftmann@29358
   246
      |> maps snd
haftmann@29358
   247
      |> try the_single
haftmann@29358
   248
      |> Option.map (ObjectLogic.ensure_propT thy);
haftmann@29358
   249
    fun after_qed some_thm =
haftmann@29358
   250
      LocalTheory.theory (prove_subclass_relation (sub, sup) some_thm)
haftmann@29358
   251
      #> (TheoryTarget.init (SOME sub) o ProofContext.theory_of);
haftmann@26238
   252
  in
haftmann@29358
   253
    do_proof after_qed sublocale_prop lthy
haftmann@25485
   254
  end;
haftmann@25485
   255
haftmann@29358
   256
fun user_proof after_qed NONE =
haftmann@29358
   257
      Proof.theorem_i NONE (K (after_qed NONE)) [[]]
haftmann@29358
   258
  | user_proof after_qed (SOME prop) =
haftmann@29358
   259
      Proof.theorem_i NONE (after_qed o try the_single o the_single) [[(prop, [])]];
haftmann@25485
   260
haftmann@29358
   261
fun tactic_proof tac after_qed NONE lthy =
haftmann@29358
   262
      after_qed NONE lthy
haftmann@29358
   263
  | tactic_proof tac after_qed (SOME prop) lthy =
haftmann@29358
   264
      after_qed (SOME (Goal.prove (LocalTheory.target_of lthy) [] [] prop
haftmann@29358
   265
        (K tac))) lthy;
haftmann@28666
   266
haftmann@29358
   267
in
haftmann@28666
   268
haftmann@29358
   269
val subclass = gen_subclass (K I) user_proof;
haftmann@29358
   270
fun prove_subclass tac = gen_subclass (K I) (tactic_proof tac);
haftmann@29358
   271
val subclass_cmd = gen_subclass Sign.read_class user_proof;
haftmann@25462
   272
haftmann@29358
   273
end; (*local*)
haftmann@29358
   274
haftmann@25603
   275
haftmann@24218
   276
end;
haftmann@25683
   277