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