src/Pure/Isar/class.ML
author haftmann
Sun, 11 Jan 2009 14:18:16 +0100
changeset 29439 83601bdadae8
parent 29378 2821c2c5270d
child 29509 1ff0f3f08a7b
permissions -rw-r--r--
construct explicit class morphism
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
29358
efdfe5dfe008 rearranged target theories
haftmann
parents: 29333
diff changeset
     1
(*  Title:      Pure/Isar/ML
24218
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
     2
    Author:     Florian Haftmann, TU Muenchen
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
     3
29358
efdfe5dfe008 rearranged target theories
haftmann
parents: 29333
diff changeset
     4
Type classes derived from primitive axclasses and locales - interfaces
24218
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
     5
*)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
     6
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
     7
signature CLASS =
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
     8
sig
29358
efdfe5dfe008 rearranged target theories
haftmann
parents: 29333
diff changeset
     9
  include CLASS_TARGET
29439
83601bdadae8 construct explicit class morphism
haftmann
parents: 29378
diff changeset
    10
    (*FIXME the split into class_target.ML, theory_target.ML and
83601bdadae8 construct explicit class morphism
haftmann
parents: 29378
diff changeset
    11
      class.ML is artificial*)
29358
efdfe5dfe008 rearranged target theories
haftmann
parents: 29333
diff changeset
    12
26247
b6608fbeaae1 some theorems named explicitly
haftmann
parents: 26238
diff changeset
    13
  val class: bstring -> class list -> Element.context_i list
29378
2821c2c5270d proper local_theory after Class.class
haftmann
parents: 29360
diff changeset
    14
    -> theory -> string * local_theory
26247
b6608fbeaae1 some theorems named explicitly
haftmann
parents: 26238
diff changeset
    15
  val class_cmd: bstring -> xstring list -> Element.context list
29378
2821c2c5270d proper local_theory after Class.class
haftmann
parents: 29360
diff changeset
    16
    -> theory -> string * local_theory
29358
efdfe5dfe008 rearranged target theories
haftmann
parents: 29333
diff changeset
    17
  val prove_subclass: tactic -> class -> local_theory -> local_theory
efdfe5dfe008 rearranged target theories
haftmann
parents: 29333
diff changeset
    18
  val subclass: class -> local_theory -> Proof.state
efdfe5dfe008 rearranged target theories
haftmann
parents: 29333
diff changeset
    19
  val subclass_cmd: xstring -> local_theory -> Proof.state
24218
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    20
end;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    21
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    22
structure Class : CLASS =
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    23
struct
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
    24
29358
efdfe5dfe008 rearranged target theories
haftmann
parents: 29333
diff changeset
    25
open Class_Target;
28715
238f9966c80e class morphism stemming from locale interpretation
haftmann
parents: 28674
diff changeset
    26
29358
efdfe5dfe008 rearranged target theories
haftmann
parents: 29333
diff changeset
    27
(** rule calculation **)
24589
d3fca349736c clarified class interfaces and internals
haftmann
parents: 24435
diff changeset
    28
28715
238f9966c80e class morphism stemming from locale interpretation
haftmann
parents: 28674
diff changeset
    29
fun calculate_axiom thy sups base_sort assm_axiom param_map class =
29360
a5be60c3674e locale -> old_locale, new_locale -> locale
haftmann
parents: 29358
diff changeset
    30
  case Old_Locale.intros thy class
28715
238f9966c80e class morphism stemming from locale interpretation
haftmann
parents: 28674
diff changeset
    31
   of (_, []) => assm_axiom
238f9966c80e class morphism stemming from locale interpretation
haftmann
parents: 28674
diff changeset
    32
    | (_, [intro]) =>
238f9966c80e class morphism stemming from locale interpretation
haftmann
parents: 28674
diff changeset
    33
      let
238f9966c80e class morphism stemming from locale interpretation
haftmann
parents: 28674
diff changeset
    34
        fun instantiate thy sort = Thm.instantiate ([pairself (Thm.ctyp_of thy o TVar o pair (Name.aT, 0))
238f9966c80e class morphism stemming from locale interpretation
haftmann
parents: 28674
diff changeset
    35
          (base_sort, sort)], map (fn (v, (c, ty)) => pairself (Thm.cterm_of thy)
238f9966c80e class morphism stemming from locale interpretation
haftmann
parents: 28674
diff changeset
    36
            (Var ((v, 0), map_atyps (fn _ => TVar ((Name.aT, 0), sort)) ty),
238f9966c80e class morphism stemming from locale interpretation
haftmann
parents: 28674
diff changeset
    37
              Const (c, map_atyps (fn _ => TVar ((Name.aT, 0), sort)) ty))) param_map);
29358
efdfe5dfe008 rearranged target theories
haftmann
parents: 29333
diff changeset
    38
        val axiom_premises = map_filter (fst o rules thy) sups
28715
238f9966c80e class morphism stemming from locale interpretation
haftmann
parents: 28674
diff changeset
    39
          @ the_list assm_axiom;
238f9966c80e class morphism stemming from locale interpretation
haftmann
parents: 28674
diff changeset
    40
      in intro
238f9966c80e class morphism stemming from locale interpretation
haftmann
parents: 28674
diff changeset
    41
        |> instantiate thy [class]
238f9966c80e class morphism stemming from locale interpretation
haftmann
parents: 28674
diff changeset
    42
        |> (fn thm => thm OF axiom_premises)
238f9966c80e class morphism stemming from locale interpretation
haftmann
parents: 28674
diff changeset
    43
        |> Drule.standard'
238f9966c80e class morphism stemming from locale interpretation
haftmann
parents: 28674
diff changeset
    44
        |> Thm.close_derivation
238f9966c80e class morphism stemming from locale interpretation
haftmann
parents: 28674
diff changeset
    45
        |> SOME
238f9966c80e class morphism stemming from locale interpretation
haftmann
parents: 28674
diff changeset
    46
      end;
25024
0615bb9955dd added is_class;
wenzelm
parents: 25020
diff changeset
    47
29439
83601bdadae8 construct explicit class morphism
haftmann
parents: 29378
diff changeset
    48
fun raw_morphism thy class param_map some_axiom =
83601bdadae8 construct explicit class morphism
haftmann
parents: 29378
diff changeset
    49
  let
83601bdadae8 construct explicit class morphism
haftmann
parents: 29378
diff changeset
    50
    val ctxt = ProofContext.init thy;
83601bdadae8 construct explicit class morphism
haftmann
parents: 29378
diff changeset
    51
    val some_wit = case some_axiom
83601bdadae8 construct explicit class morphism
haftmann
parents: 29378
diff changeset
    52
     of SOME axiom => SOME (Element.prove_witness ctxt
83601bdadae8 construct explicit class morphism
haftmann
parents: 29378
diff changeset
    53
          (Logic.unvarify (Thm.prop_of axiom))
83601bdadae8 construct explicit class morphism
haftmann
parents: 29378
diff changeset
    54
            (ALLGOALS (ProofContext.fact_tac [axiom])))
83601bdadae8 construct explicit class morphism
haftmann
parents: 29378
diff changeset
    55
      | NONE => NONE;
83601bdadae8 construct explicit class morphism
haftmann
parents: 29378
diff changeset
    56
    val instT = Symtab.empty |> Symtab.update ("'a", TFree ("'a", [class]));
83601bdadae8 construct explicit class morphism
haftmann
parents: 29378
diff changeset
    57
    val inst = Symtab.make ((map o apsnd) Const param_map);
83601bdadae8 construct explicit class morphism
haftmann
parents: 29378
diff changeset
    58
  in case some_wit
83601bdadae8 construct explicit class morphism
haftmann
parents: 29378
diff changeset
    59
   of SOME wit => Element.inst_morphism thy (instT, inst)
83601bdadae8 construct explicit class morphism
haftmann
parents: 29378
diff changeset
    60
        $> Morphism.binding_morphism (Binding.add_prefix false (class_prefix class))
83601bdadae8 construct explicit class morphism
haftmann
parents: 29378
diff changeset
    61
        $> Element.satisfy_morphism [wit]
83601bdadae8 construct explicit class morphism
haftmann
parents: 29378
diff changeset
    62
    | NONE => Element.inst_morphism thy (instT, inst)
83601bdadae8 construct explicit class morphism
haftmann
parents: 29378
diff changeset
    63
        $> Morphism.binding_morphism (Binding.add_prefix false (class_prefix class))
83601bdadae8 construct explicit class morphism
haftmann
parents: 29378
diff changeset
    64
  end;
83601bdadae8 construct explicit class morphism
haftmann
parents: 29378
diff changeset
    65
83601bdadae8 construct explicit class morphism
haftmann
parents: 29378
diff changeset
    66
fun calculate_rules thy morph sups base_sort param_map axiom class =
25062
af5ef0d4d655 global class syntax
haftmann
parents: 25060
diff changeset
    67
  let
25711
91cee0cefaf7 tuned primitive inferences
haftmann
parents: 25683
diff changeset
    68
    fun instantiate thy sort = Thm.instantiate ([pairself (Thm.ctyp_of thy o TVar o pair (Name.aT, 0))
91cee0cefaf7 tuned primitive inferences
haftmann
parents: 25683
diff changeset
    69
      (base_sort, sort)], map (fn (v, (c, ty)) => pairself (Thm.cterm_of thy)
91cee0cefaf7 tuned primitive inferences
haftmann
parents: 25683
diff changeset
    70
        (Var ((v, 0), map_atyps (fn _ => TVar ((Name.aT, 0), sort)) ty),
91cee0cefaf7 tuned primitive inferences
haftmann
parents: 25683
diff changeset
    71
          Const (c, map_atyps (fn _ => TVar ((Name.aT, 0), sort)) ty))) param_map);
91cee0cefaf7 tuned primitive inferences
haftmann
parents: 25683
diff changeset
    72
    val defs = these_defs thy sups;
29360
a5be60c3674e locale -> old_locale, new_locale -> locale
haftmann
parents: 29358
diff changeset
    73
    val assm_intro = Old_Locale.intros thy class
28715
238f9966c80e class morphism stemming from locale interpretation
haftmann
parents: 28674
diff changeset
    74
      |> fst
238f9966c80e class morphism stemming from locale interpretation
haftmann
parents: 28674
diff changeset
    75
      |> map (instantiate thy base_sort)
238f9966c80e class morphism stemming from locale interpretation
haftmann
parents: 28674
diff changeset
    76
      |> map (MetaSimplifier.rewrite_rule defs)
238f9966c80e class morphism stemming from locale interpretation
haftmann
parents: 28674
diff changeset
    77
      |> map Thm.close_derivation
238f9966c80e class morphism stemming from locale interpretation
haftmann
parents: 28674
diff changeset
    78
      |> try the_single;
25711
91cee0cefaf7 tuned primitive inferences
haftmann
parents: 25683
diff changeset
    79
    val fixate = Thm.instantiate
91cee0cefaf7 tuned primitive inferences
haftmann
parents: 25683
diff changeset
    80
      (map (pairself (Thm.ctyp_of thy)) [(TVar ((Name.aT, 0), []), TFree (Name.aT, base_sort)),
91cee0cefaf7 tuned primitive inferences
haftmann
parents: 25683
diff changeset
    81
        (TVar ((Name.aT, 0), base_sort), TFree (Name.aT, base_sort))], [])
25618
01f20279fea1 improved rule calculation
haftmann
parents: 25603
diff changeset
    82
    val of_class_sups = if null sups
25711
91cee0cefaf7 tuned primitive inferences
haftmann
parents: 25683
diff changeset
    83
      then map (fixate o Thm.class_triv thy) base_sort
29358
efdfe5dfe008 rearranged target theories
haftmann
parents: 29333
diff changeset
    84
      else map (fixate o snd o rules thy) sups;
29360
a5be60c3674e locale -> old_locale, new_locale -> locale
haftmann
parents: 29358
diff changeset
    85
    val locale_dests = map Drule.standard' (Old_Locale.dests thy class);
25711
91cee0cefaf7 tuned primitive inferences
haftmann
parents: 25683
diff changeset
    86
    val num_trivs = case length locale_dests
91cee0cefaf7 tuned primitive inferences
haftmann
parents: 25683
diff changeset
    87
     of 0 => if is_none axiom then 0 else 1
91cee0cefaf7 tuned primitive inferences
haftmann
parents: 25683
diff changeset
    88
      | n => n;
91cee0cefaf7 tuned primitive inferences
haftmann
parents: 25683
diff changeset
    89
    val pred_trivs = if num_trivs = 0 then []
91cee0cefaf7 tuned primitive inferences
haftmann
parents: 25683
diff changeset
    90
      else the axiom
91cee0cefaf7 tuned primitive inferences
haftmann
parents: 25683
diff changeset
    91
        |> Thm.prop_of
91cee0cefaf7 tuned primitive inferences
haftmann
parents: 25683
diff changeset
    92
        |> (map_types o map_atyps o K) (TFree (Name.aT, base_sort))
91cee0cefaf7 tuned primitive inferences
haftmann
parents: 25683
diff changeset
    93
        |> (Thm.assume o Thm.cterm_of thy)
91cee0cefaf7 tuned primitive inferences
haftmann
parents: 25683
diff changeset
    94
        |> replicate num_trivs;
28715
238f9966c80e class morphism stemming from locale interpretation
haftmann
parents: 28674
diff changeset
    95
    val axclass_intro = (#intro o AxClass.get_info thy) class;
238f9966c80e class morphism stemming from locale interpretation
haftmann
parents: 28674
diff changeset
    96
    val of_class = (fixate axclass_intro OF of_class_sups OF locale_dests OF pred_trivs)
25711
91cee0cefaf7 tuned primitive inferences
haftmann
parents: 25683
diff changeset
    97
      |> Drule.standard'
26628
63306cb94313 replaced Drule.close_derivation/Goal.close_result by Thm.close_derivation (removed obsolete compression);
wenzelm
parents: 26596
diff changeset
    98
      |> Thm.close_derivation;
28715
238f9966c80e class morphism stemming from locale interpretation
haftmann
parents: 28674
diff changeset
    99
  in (assm_intro, of_class) end;
24218
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   100
28715
238f9966c80e class morphism stemming from locale interpretation
haftmann
parents: 28674
diff changeset
   101
fun note_assm_intro class assm_intro thy =
238f9966c80e class morphism stemming from locale interpretation
haftmann
parents: 28674
diff changeset
   102
  thy
238f9966c80e class morphism stemming from locale interpretation
haftmann
parents: 28674
diff changeset
   103
  |> Sign.sticky_prefix (class_prefix class ^ "_" ^ AxClass.axiomsN)
238f9966c80e class morphism stemming from locale interpretation
haftmann
parents: 28674
diff changeset
   104
  |> PureThy.store_thm (AxClass.introN, assm_intro)
238f9966c80e class morphism stemming from locale interpretation
haftmann
parents: 28674
diff changeset
   105
  |> snd
238f9966c80e class morphism stemming from locale interpretation
haftmann
parents: 28674
diff changeset
   106
  |> Sign.restore_naming thy;
238f9966c80e class morphism stemming from locale interpretation
haftmann
parents: 28674
diff changeset
   107
24218
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   108
29358
efdfe5dfe008 rearranged target theories
haftmann
parents: 29333
diff changeset
   109
(** define classes **)
24218
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   110
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   111
local
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   112
26247
b6608fbeaae1 some theorems named explicitly
haftmann
parents: 26238
diff changeset
   113
fun gen_class_spec prep_class process_expr thy raw_supclasses raw_elems =
24218
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   114
  let
24748
ee0a0eb6b738 proper syntax during class specification
haftmann
parents: 24731
diff changeset
   115
    val supclasses = map (prep_class thy) raw_supclasses;
ee0a0eb6b738 proper syntax during class specification
haftmann
parents: 24731
diff changeset
   116
    val supsort = Sign.minimize_sort thy supclasses;
25618
01f20279fea1 improved rule calculation
haftmann
parents: 25603
diff changeset
   117
    val sups = filter (is_class thy) supsort;
26995
2511a72dd73c check for illegal merge of class parameters
haftmann
parents: 26939
diff changeset
   118
    val supparam_names = map fst (these_params thy sups);
2511a72dd73c check for illegal merge of class parameters
haftmann
parents: 26939
diff changeset
   119
    val _ = if has_duplicates (op =) supparam_names
2511a72dd73c check for illegal merge of class parameters
haftmann
parents: 26939
diff changeset
   120
      then error ("Duplicate parameter(s) in superclasses: "
2511a72dd73c check for illegal merge of class parameters
haftmann
parents: 26939
diff changeset
   121
        ^ (commas o map quote o duplicates (op =)) supparam_names)
2511a72dd73c check for illegal merge of class parameters
haftmann
parents: 26939
diff changeset
   122
      else ();
25618
01f20279fea1 improved rule calculation
haftmann
parents: 25603
diff changeset
   123
    val base_sort = if null sups then supsort else
26167
ccc9007a7164 proper merge of base sorts
haftmann
parents: 25999
diff changeset
   124
      foldr1 (Sorts.inter_sort (Sign.classes_of thy))
29358
efdfe5dfe008 rearranged target theories
haftmann
parents: 29333
diff changeset
   125
        (map (base_sort thy) sups);
29360
a5be60c3674e locale -> old_locale, new_locale -> locale
haftmann
parents: 29358
diff changeset
   126
    val suplocales = map Old_Locale.Locale sups;
a5be60c3674e locale -> old_locale, new_locale -> locale
haftmann
parents: 29358
diff changeset
   127
    val supexpr = Old_Locale.Merge suplocales;
a5be60c3674e locale -> old_locale, new_locale -> locale
haftmann
parents: 29358
diff changeset
   128
    val supparams = (map fst o Old_Locale.parameters_of_expr thy) supexpr;
a5be60c3674e locale -> old_locale, new_locale -> locale
haftmann
parents: 29358
diff changeset
   129
    val mergeexpr = Old_Locale.Merge suplocales;
24748
ee0a0eb6b738 proper syntax during class specification
haftmann
parents: 24731
diff changeset
   130
    val constrain = Element.Constrains ((map o apsnd o map_atyps)
26167
ccc9007a7164 proper merge of base sorts
haftmann
parents: 25999
diff changeset
   131
      (K (TFree (Name.aT, base_sort))) supparams);
25683
d9fefc4859be maior tuning
haftmann
parents: 25668
diff changeset
   132
    fun fork_syn (Element.Fixes xs) =
29006
abe0f11cfa4e Name.name_of -> Binding.base_name
haftmann
parents: 28965
diff changeset
   133
          fold_map (fn (c, ty, syn) => cons (Binding.base_name c, syn) #> pair (c, ty, NoSyn)) xs
25683
d9fefc4859be maior tuning
haftmann
parents: 25668
diff changeset
   134
          #>> Element.Fixes
d9fefc4859be maior tuning
haftmann
parents: 25668
diff changeset
   135
      | fork_syn x = pair x;
d9fefc4859be maior tuning
haftmann
parents: 25668
diff changeset
   136
    fun fork_syntax elems =
d9fefc4859be maior tuning
haftmann
parents: 25668
diff changeset
   137
      let
d9fefc4859be maior tuning
haftmann
parents: 25668
diff changeset
   138
        val (elems', global_syntax) = fold_map fork_syn elems [];
26247
b6608fbeaae1 some theorems named explicitly
haftmann
parents: 26238
diff changeset
   139
      in (constrain :: elems', global_syntax) end;
25683
d9fefc4859be maior tuning
haftmann
parents: 25668
diff changeset
   140
    val (elems, global_syntax) =
d9fefc4859be maior tuning
haftmann
parents: 25668
diff changeset
   141
      ProofContext.init thy
29360
a5be60c3674e locale -> old_locale, new_locale -> locale
haftmann
parents: 29358
diff changeset
   142
      |> Old_Locale.cert_expr supexpr [constrain]
25683
d9fefc4859be maior tuning
haftmann
parents: 25668
diff changeset
   143
      |> snd
29358
efdfe5dfe008 rearranged target theories
haftmann
parents: 29333
diff changeset
   144
      |> begin sups base_sort
29360
a5be60c3674e locale -> old_locale, new_locale -> locale
haftmann
parents: 29358
diff changeset
   145
      |> process_expr Old_Locale.empty raw_elems
25683
d9fefc4859be maior tuning
haftmann
parents: 25668
diff changeset
   146
      |> fst
d9fefc4859be maior tuning
haftmann
parents: 25668
diff changeset
   147
      |> fork_syntax
d9fefc4859be maior tuning
haftmann
parents: 25668
diff changeset
   148
  in (((sups, supparams), (supsort, base_sort, mergeexpr)), (elems, global_syntax)) end;
24748
ee0a0eb6b738 proper syntax during class specification
haftmann
parents: 24731
diff changeset
   149
29360
a5be60c3674e locale -> old_locale, new_locale -> locale
haftmann
parents: 29358
diff changeset
   150
val read_class_spec = gen_class_spec Sign.intern_class Old_Locale.read_expr;
a5be60c3674e locale -> old_locale, new_locale -> locale
haftmann
parents: 29358
diff changeset
   151
val check_class_spec = gen_class_spec (K I) Old_Locale.cert_expr;
24748
ee0a0eb6b738 proper syntax during class specification
haftmann
parents: 24731
diff changeset
   152
28715
238f9966c80e class morphism stemming from locale interpretation
haftmann
parents: 28674
diff changeset
   153
fun add_consts bname class base_sort sups supparams global_syntax thy =
24968
f9bafc868847 replaced Sign.add_consts_authentic by Sign.declare_const;
wenzelm
parents: 24949
diff changeset
   154
  let
25683
d9fefc4859be maior tuning
haftmann
parents: 25668
diff changeset
   155
    val supconsts = map fst supparams
26518
3db6a46d8460 improved improvements for instantiaton
haftmann
parents: 26470
diff changeset
   156
      |> AList.make (snd o the o AList.lookup (op =) (these_params thy sups))
25683
d9fefc4859be maior tuning
haftmann
parents: 25668
diff changeset
   157
      |> (map o apsnd o apsnd o map_atyps o K o TFree) (Name.aT, [class]);
29360
a5be60c3674e locale -> old_locale, new_locale -> locale
haftmann
parents: 29358
diff changeset
   158
    val all_params = map fst (Old_Locale.parameters_of thy class);
28715
238f9966c80e class morphism stemming from locale interpretation
haftmann
parents: 28674
diff changeset
   159
    val raw_params = (snd o chop (length supparams)) all_params;
25683
d9fefc4859be maior tuning
haftmann
parents: 25668
diff changeset
   160
    fun add_const (v, raw_ty) thy =
d9fefc4859be maior tuning
haftmann
parents: 25668
diff changeset
   161
      let
28965
1de908189869 cleaned up binding module and related code
haftmann
parents: 28941
diff changeset
   162
        val c = Sign.full_bname thy v;
25683
d9fefc4859be maior tuning
haftmann
parents: 25668
diff changeset
   163
        val ty = map_atyps (K (TFree (Name.aT, base_sort))) raw_ty;
d9fefc4859be maior tuning
haftmann
parents: 25668
diff changeset
   164
        val ty0 = Type.strip_sorts ty;
d9fefc4859be maior tuning
haftmann
parents: 25668
diff changeset
   165
        val ty' = map_atyps (K (TFree (Name.aT, [class]))) ty0;
d9fefc4859be maior tuning
haftmann
parents: 25668
diff changeset
   166
        val syn = (the_default NoSyn o AList.lookup (op =) global_syntax) v;
d9fefc4859be maior tuning
haftmann
parents: 25668
diff changeset
   167
      in
d9fefc4859be maior tuning
haftmann
parents: 25668
diff changeset
   168
        thy
28965
1de908189869 cleaned up binding module and related code
haftmann
parents: 28941
diff changeset
   169
        |> Sign.declare_const [] ((Binding.name v, ty0), syn)
25683
d9fefc4859be maior tuning
haftmann
parents: 25668
diff changeset
   170
        |> snd
d9fefc4859be maior tuning
haftmann
parents: 25668
diff changeset
   171
        |> pair ((v, ty), (c, ty'))
d9fefc4859be maior tuning
haftmann
parents: 25668
diff changeset
   172
      end;
28715
238f9966c80e class morphism stemming from locale interpretation
haftmann
parents: 28674
diff changeset
   173
  in
238f9966c80e class morphism stemming from locale interpretation
haftmann
parents: 28674
diff changeset
   174
    thy
238f9966c80e class morphism stemming from locale interpretation
haftmann
parents: 28674
diff changeset
   175
    |> Sign.add_path (Logic.const_of_class bname)
238f9966c80e class morphism stemming from locale interpretation
haftmann
parents: 28674
diff changeset
   176
    |> fold_map add_const raw_params
238f9966c80e class morphism stemming from locale interpretation
haftmann
parents: 28674
diff changeset
   177
    ||> Sign.restore_naming thy
238f9966c80e class morphism stemming from locale interpretation
haftmann
parents: 28674
diff changeset
   178
    |-> (fn params => pair (supconsts @ (map o apfst) fst params, params))
238f9966c80e class morphism stemming from locale interpretation
haftmann
parents: 28674
diff changeset
   179
  end;
238f9966c80e class morphism stemming from locale interpretation
haftmann
parents: 28674
diff changeset
   180
238f9966c80e class morphism stemming from locale interpretation
haftmann
parents: 28674
diff changeset
   181
fun adjungate_axclass bname class base_sort sups supsort supparams global_syntax thy =
238f9966c80e class morphism stemming from locale interpretation
haftmann
parents: 28674
diff changeset
   182
  let
25683
d9fefc4859be maior tuning
haftmann
parents: 25668
diff changeset
   183
    fun globalize param_map = map_aterms
d9fefc4859be maior tuning
haftmann
parents: 25668
diff changeset
   184
      (fn Free (v, ty) => Const ((fst o the o AList.lookup (op =) param_map) v, ty)
d9fefc4859be maior tuning
haftmann
parents: 25668
diff changeset
   185
        | t => t);
29360
a5be60c3674e locale -> old_locale, new_locale -> locale
haftmann
parents: 29358
diff changeset
   186
    val raw_pred = Old_Locale.intros thy class
25683
d9fefc4859be maior tuning
haftmann
parents: 25668
diff changeset
   187
      |> fst
d9fefc4859be maior tuning
haftmann
parents: 25668
diff changeset
   188
      |> map (Logic.unvarify o Logic.strip_imp_concl o Thm.prop_of);
d9fefc4859be maior tuning
haftmann
parents: 25668
diff changeset
   189
    fun get_axiom thy = case (#axioms o AxClass.get_info thy) class
d9fefc4859be maior tuning
haftmann
parents: 25668
diff changeset
   190
     of [] => NONE
d9fefc4859be maior tuning
haftmann
parents: 25668
diff changeset
   191
      | [thm] => SOME thm;
24968
f9bafc868847 replaced Sign.add_consts_authentic by Sign.declare_const;
wenzelm
parents: 24949
diff changeset
   192
  in
f9bafc868847 replaced Sign.add_consts_authentic by Sign.declare_const;
wenzelm
parents: 24949
diff changeset
   193
    thy
28715
238f9966c80e class morphism stemming from locale interpretation
haftmann
parents: 28674
diff changeset
   194
    |> add_consts bname class base_sort sups supparams global_syntax
25683
d9fefc4859be maior tuning
haftmann
parents: 25668
diff changeset
   195
    |-> (fn (param_map, params) => AxClass.define_class (bname, supsort)
26518
3db6a46d8460 improved improvements for instantiaton
haftmann
parents: 26470
diff changeset
   196
          (map (fst o snd) params)
28965
1de908189869 cleaned up binding module and related code
haftmann
parents: 28941
diff changeset
   197
          [((Binding.name (bname ^ "_" ^ AxClass.axiomsN), []), map (globalize param_map) raw_pred)]
25683
d9fefc4859be maior tuning
haftmann
parents: 25668
diff changeset
   198
    #> snd
d9fefc4859be maior tuning
haftmann
parents: 25668
diff changeset
   199
    #> `get_axiom
d9fefc4859be maior tuning
haftmann
parents: 25668
diff changeset
   200
    #-> (fn assm_axiom => fold (Sign.add_const_constraint o apsnd SOME o snd) params
28715
238f9966c80e class morphism stemming from locale interpretation
haftmann
parents: 28674
diff changeset
   201
    #> pair (map (Const o snd) param_map, param_map, params, assm_axiom)))
24968
f9bafc868847 replaced Sign.add_consts_authentic by Sign.declare_const;
wenzelm
parents: 24949
diff changeset
   202
  end;
f9bafc868847 replaced Sign.add_consts_authentic by Sign.declare_const;
wenzelm
parents: 24949
diff changeset
   203
26518
3db6a46d8460 improved improvements for instantiaton
haftmann
parents: 26470
diff changeset
   204
fun gen_class prep_spec bname raw_supclasses raw_elems thy =
24748
ee0a0eb6b738 proper syntax during class specification
haftmann
parents: 24731
diff changeset
   205
  let
28965
1de908189869 cleaned up binding module and related code
haftmann
parents: 28941
diff changeset
   206
    val class = Sign.full_bname thy bname;
25683
d9fefc4859be maior tuning
haftmann
parents: 25668
diff changeset
   207
    val (((sups, supparams), (supsort, base_sort, mergeexpr)), (elems, global_syntax)) =
26247
b6608fbeaae1 some theorems named explicitly
haftmann
parents: 26238
diff changeset
   208
      prep_spec thy raw_supclasses raw_elems;
28715
238f9966c80e class morphism stemming from locale interpretation
haftmann
parents: 28674
diff changeset
   209
    val supconsts = map (apsnd fst o snd) (these_params thy sups);
24218
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   210
  in
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   211
    thy
29360
a5be60c3674e locale -> old_locale, new_locale -> locale
haftmann
parents: 29358
diff changeset
   212
    |> Old_Locale.add_locale "" bname mergeexpr elems
25038
522abf8a5f87 canonical interpretation interface
haftmann
parents: 25024
diff changeset
   213
    |> snd
25311
aa750e3a581c Class.init now similiar to Locale.init
haftmann
parents: 25268
diff changeset
   214
    |> ProofContext.theory_of
26518
3db6a46d8460 improved improvements for instantiaton
haftmann
parents: 26470
diff changeset
   215
    |> adjungate_axclass bname class base_sort sups supsort supparams global_syntax
28715
238f9966c80e class morphism stemming from locale interpretation
haftmann
parents: 28674
diff changeset
   216
    |-> (fn (inst, param_map, params, assm_axiom) =>
238f9966c80e class morphism stemming from locale interpretation
haftmann
parents: 28674
diff changeset
   217
        `(fn thy => calculate_axiom thy sups base_sort assm_axiom param_map class)
238f9966c80e class morphism stemming from locale interpretation
haftmann
parents: 28674
diff changeset
   218
    #-> (fn axiom =>
238f9966c80e class morphism stemming from locale interpretation
haftmann
parents: 28674
diff changeset
   219
        prove_class_interpretation class inst
238f9966c80e class morphism stemming from locale interpretation
haftmann
parents: 28674
diff changeset
   220
          (supconsts @ map (pair class o fst o snd) params) (the_list axiom) []
29439
83601bdadae8 construct explicit class morphism
haftmann
parents: 29378
diff changeset
   221
    #> `(fn thy => raw_morphism thy class param_map axiom)
83601bdadae8 construct explicit class morphism
haftmann
parents: 29378
diff changeset
   222
    #-> (fn morph =>
83601bdadae8 construct explicit class morphism
haftmann
parents: 29378
diff changeset
   223
        `(fn thy => calculate_rules thy morph sups base_sort param_map axiom class)
28715
238f9966c80e class morphism stemming from locale interpretation
haftmann
parents: 28674
diff changeset
   224
    #-> (fn (assm_intro, of_class) =>
29358
efdfe5dfe008 rearranged target theories
haftmann
parents: 29333
diff changeset
   225
        register class sups params base_sort inst
29439
83601bdadae8 construct explicit class morphism
haftmann
parents: 29378
diff changeset
   226
          morph axiom assm_intro of_class
83601bdadae8 construct explicit class morphism
haftmann
parents: 29378
diff changeset
   227
    #> fold (note_assm_intro class) (the_list assm_intro)))))
29378
2821c2c5270d proper local_theory after Class.class
haftmann
parents: 29360
diff changeset
   228
    |> TheoryTarget.init (SOME class)
25038
522abf8a5f87 canonical interpretation interface
haftmann
parents: 25024
diff changeset
   229
    |> pair class
24218
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   230
  end;
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   231
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   232
in
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   233
26518
3db6a46d8460 improved improvements for instantiaton
haftmann
parents: 26470
diff changeset
   234
val class_cmd = gen_class read_class_spec;
3db6a46d8460 improved improvements for instantiaton
haftmann
parents: 26470
diff changeset
   235
val class = gen_class check_class_spec;
24218
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   236
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   237
end; (*local*)
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   238
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   239
29358
efdfe5dfe008 rearranged target theories
haftmann
parents: 29333
diff changeset
   240
(** subclass relations **)
25462
dad0291cb76a rudimentary instantiation target
haftmann
parents: 25368
diff changeset
   241
29358
efdfe5dfe008 rearranged target theories
haftmann
parents: 29333
diff changeset
   242
local
25462
dad0291cb76a rudimentary instantiation target
haftmann
parents: 25368
diff changeset
   243
29358
efdfe5dfe008 rearranged target theories
haftmann
parents: 29333
diff changeset
   244
fun gen_subclass prep_class do_proof raw_sup lthy =
25462
dad0291cb76a rudimentary instantiation target
haftmann
parents: 25368
diff changeset
   245
  let
29358
efdfe5dfe008 rearranged target theories
haftmann
parents: 29333
diff changeset
   246
    val thy = ProofContext.theory_of lthy;
efdfe5dfe008 rearranged target theories
haftmann
parents: 29333
diff changeset
   247
    val sup = prep_class thy raw_sup;
efdfe5dfe008 rearranged target theories
haftmann
parents: 29333
diff changeset
   248
    val sub = case TheoryTarget.peek lthy
efdfe5dfe008 rearranged target theories
haftmann
parents: 29333
diff changeset
   249
     of {is_class = false, ...} => error "Not a class context"
efdfe5dfe008 rearranged target theories
haftmann
parents: 29333
diff changeset
   250
      | {target, ...} => target;
efdfe5dfe008 rearranged target theories
haftmann
parents: 29333
diff changeset
   251
    val _ = if Sign.subsort thy ([sup], [sub])
efdfe5dfe008 rearranged target theories
haftmann
parents: 29333
diff changeset
   252
      then error ("Class " ^ Syntax.string_of_sort lthy [sup]
efdfe5dfe008 rearranged target theories
haftmann
parents: 29333
diff changeset
   253
        ^ " is subclass of class " ^ Syntax.string_of_sort lthy [sub])
efdfe5dfe008 rearranged target theories
haftmann
parents: 29333
diff changeset
   254
      else ();
efdfe5dfe008 rearranged target theories
haftmann
parents: 29333
diff changeset
   255
    val sub_params = map fst (these_params thy [sub]);
efdfe5dfe008 rearranged target theories
haftmann
parents: 29333
diff changeset
   256
    val sup_params = map fst (these_params thy [sup]);
efdfe5dfe008 rearranged target theories
haftmann
parents: 29333
diff changeset
   257
    val err_params = subtract (op =) sub_params sup_params;
efdfe5dfe008 rearranged target theories
haftmann
parents: 29333
diff changeset
   258
    val _ = if null err_params then [] else
efdfe5dfe008 rearranged target theories
haftmann
parents: 29333
diff changeset
   259
      error ("Class " ^ Syntax.string_of_sort lthy [sub] ^ " lacks parameter(s) " ^
efdfe5dfe008 rearranged target theories
haftmann
parents: 29333
diff changeset
   260
        commas_quote err_params ^ " of " ^ Syntax.string_of_sort lthy [sup]);
efdfe5dfe008 rearranged target theories
haftmann
parents: 29333
diff changeset
   261
    val sublocale_prop =
29360
a5be60c3674e locale -> old_locale, new_locale -> locale
haftmann
parents: 29358
diff changeset
   262
      Old_Locale.global_asms_of thy sup
29358
efdfe5dfe008 rearranged target theories
haftmann
parents: 29333
diff changeset
   263
      |> maps snd
efdfe5dfe008 rearranged target theories
haftmann
parents: 29333
diff changeset
   264
      |> try the_single
efdfe5dfe008 rearranged target theories
haftmann
parents: 29333
diff changeset
   265
      |> Option.map (ObjectLogic.ensure_propT thy);
efdfe5dfe008 rearranged target theories
haftmann
parents: 29333
diff changeset
   266
    fun after_qed some_thm =
efdfe5dfe008 rearranged target theories
haftmann
parents: 29333
diff changeset
   267
      LocalTheory.theory (prove_subclass_relation (sub, sup) some_thm)
efdfe5dfe008 rearranged target theories
haftmann
parents: 29333
diff changeset
   268
      #> (TheoryTarget.init (SOME sub) o ProofContext.theory_of);
26238
c30bb8182da2 generic improvable syntax for targets
haftmann
parents: 26167
diff changeset
   269
  in
29358
efdfe5dfe008 rearranged target theories
haftmann
parents: 29333
diff changeset
   270
    do_proof after_qed sublocale_prop lthy
25485
33840a854e63 tuned interfaces of class module
haftmann
parents: 25462
diff changeset
   271
  end;
33840a854e63 tuned interfaces of class module
haftmann
parents: 25462
diff changeset
   272
29358
efdfe5dfe008 rearranged target theories
haftmann
parents: 29333
diff changeset
   273
fun user_proof after_qed NONE =
efdfe5dfe008 rearranged target theories
haftmann
parents: 29333
diff changeset
   274
      Proof.theorem_i NONE (K (after_qed NONE)) [[]]
efdfe5dfe008 rearranged target theories
haftmann
parents: 29333
diff changeset
   275
  | user_proof after_qed (SOME prop) =
efdfe5dfe008 rearranged target theories
haftmann
parents: 29333
diff changeset
   276
      Proof.theorem_i NONE (after_qed o try the_single o the_single) [[(prop, [])]];
25485
33840a854e63 tuned interfaces of class module
haftmann
parents: 25462
diff changeset
   277
29358
efdfe5dfe008 rearranged target theories
haftmann
parents: 29333
diff changeset
   278
fun tactic_proof tac after_qed NONE lthy =
efdfe5dfe008 rearranged target theories
haftmann
parents: 29333
diff changeset
   279
      after_qed NONE lthy
efdfe5dfe008 rearranged target theories
haftmann
parents: 29333
diff changeset
   280
  | tactic_proof tac after_qed (SOME prop) lthy =
efdfe5dfe008 rearranged target theories
haftmann
parents: 29333
diff changeset
   281
      after_qed (SOME (Goal.prove (LocalTheory.target_of lthy) [] [] prop
efdfe5dfe008 rearranged target theories
haftmann
parents: 29333
diff changeset
   282
        (K tac))) lthy;
28666
d2dbfe3a0284 prove_instantiation_exit combinators
haftmann
parents: 28259
diff changeset
   283
29358
efdfe5dfe008 rearranged target theories
haftmann
parents: 29333
diff changeset
   284
in
28666
d2dbfe3a0284 prove_instantiation_exit combinators
haftmann
parents: 28259
diff changeset
   285
29358
efdfe5dfe008 rearranged target theories
haftmann
parents: 29333
diff changeset
   286
val subclass = gen_subclass (K I) user_proof;
efdfe5dfe008 rearranged target theories
haftmann
parents: 29333
diff changeset
   287
fun prove_subclass tac = gen_subclass (K I) (tactic_proof tac);
efdfe5dfe008 rearranged target theories
haftmann
parents: 29333
diff changeset
   288
val subclass_cmd = gen_subclass Sign.read_class user_proof;
25462
dad0291cb76a rudimentary instantiation target
haftmann
parents: 25368
diff changeset
   289
29358
efdfe5dfe008 rearranged target theories
haftmann
parents: 29333
diff changeset
   290
end; (*local*)
efdfe5dfe008 rearranged target theories
haftmann
parents: 29333
diff changeset
   291
25603
4b7a58fc168c dropped Class.prep_spec
haftmann
parents: 25597
diff changeset
   292
24218
fbf1646b267c ClassPackage renamed to Class
haftmann
parents:
diff changeset
   293
end;
25683
d9fefc4859be maior tuning
haftmann
parents: 25668
diff changeset
   294