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