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