src/Pure/Isar/object_logic.ML
author wenzelm
Mon May 03 14:25:56 2010 +0200 (2010-05-03)
changeset 36610 bafd82950e24
parent 35626 06197484c6ad
child 37216 3165bc303f66
permissions -rw-r--r--
renamed ProofContext.init to ProofContext.init_global to emphasize that this is not the real thing;
wenzelm@11897
     1
(*  Title:      Pure/Isar/object_logic.ML
wenzelm@11897
     2
    Author:     Markus Wenzel, TU Muenchen
wenzelm@11897
     3
wenzelm@11897
     4
Specifics about common object-logics.
wenzelm@11897
     5
*)
wenzelm@11897
     6
wenzelm@11897
     7
signature OBJECT_LOGIC =
wenzelm@11897
     8
sig
wenzelm@25497
     9
  val get_base_sort: theory -> sort option
wenzelm@25497
    10
  val add_base_sort: sort -> theory -> theory
wenzelm@30344
    11
  val add_judgment: binding * typ * mixfix -> theory -> theory
wenzelm@30344
    12
  val add_judgment_cmd: binding * string * mixfix -> theory -> theory
wenzelm@16449
    13
  val judgment_name: theory -> string
wenzelm@16449
    14
  val is_judgment: theory -> term -> bool
wenzelm@16449
    15
  val drop_judgment: theory -> term -> term
wenzelm@16449
    16
  val fixed_judgment: theory -> string -> term
wenzelm@18121
    17
  val ensure_propT: theory -> term -> term
wenzelm@23586
    18
  val dest_judgment: cterm -> cterm
wenzelm@23566
    19
  val judgment_conv: conv -> conv
wenzelm@19261
    20
  val is_elim: thm -> bool
wenzelm@18728
    21
  val declare_atomize: attribute
wenzelm@18728
    22
  val declare_rulify: attribute
wenzelm@16449
    23
  val atomize_term: theory -> term -> term
wenzelm@23602
    24
  val atomize: conv
wenzelm@23602
    25
  val atomize_prems: conv
wenzelm@23602
    26
  val atomize_prems_tac: int -> tactic
wenzelm@12829
    27
  val full_atomize_tac: int -> tactic
wenzelm@18807
    28
  val rulify_term: theory -> term -> term
wenzelm@18807
    29
  val rulify_tac: int -> tactic
wenzelm@11897
    30
  val rulify: thm -> thm
wenzelm@11897
    31
  val rulify_no_asm: thm -> thm
wenzelm@18728
    32
  val rule_format: attribute
wenzelm@18728
    33
  val rule_format_no_asm: attribute
wenzelm@11897
    34
end;
wenzelm@11897
    35
wenzelm@35625
    36
structure Object_Logic: OBJECT_LOGIC =
wenzelm@11897
    37
struct
wenzelm@11897
    38
wenzelm@25497
    39
(** theory data **)
wenzelm@11897
    40
wenzelm@25497
    41
datatype data = Data of
wenzelm@25497
    42
 {base_sort: sort option,
wenzelm@25497
    43
  judgment: string option,
wenzelm@25497
    44
  atomize_rulify: thm list * thm list};
wenzelm@25497
    45
wenzelm@25497
    46
fun make_data (base_sort, judgment, atomize_rulify) =
wenzelm@25497
    47
  Data {base_sort = base_sort, judgment = judgment, atomize_rulify = atomize_rulify};
wenzelm@11897
    48
wenzelm@33522
    49
structure ObjectLogicData = Theory_Data
wenzelm@22846
    50
(
wenzelm@25497
    51
  type T = data;
wenzelm@25497
    52
  val empty = make_data (NONE, NONE, ([], []));
wenzelm@16449
    53
  val extend = I;
wenzelm@11897
    54
wenzelm@25497
    55
  fun merge_opt eq (SOME x, SOME y) =
wenzelm@25497
    56
        if eq (x, y) then SOME x else error "Attempt to merge different object-logics"
wenzelm@25497
    57
    | merge_opt _ (x, y) = if is_some x then x else y;
wenzelm@11897
    58
wenzelm@33522
    59
  fun merge
wenzelm@25497
    60
     (Data {base_sort = base_sort1, judgment = judgment1, atomize_rulify = (atomize1, rulify1)},
wenzelm@25497
    61
      Data {base_sort = base_sort2, judgment = judgment2, atomize_rulify = (atomize2, rulify2)}) =
wenzelm@25497
    62
    make_data (merge_opt (op =) (base_sort1, base_sort2), merge_opt (op =) (judgment1, judgment2),
wenzelm@24039
    63
      (Thm.merge_thms (atomize1, atomize2), Thm.merge_thms (rulify1, rulify2)));
wenzelm@22846
    64
);
wenzelm@15801
    65
wenzelm@25497
    66
fun map_data f = ObjectLogicData.map (fn (Data {base_sort, judgment, atomize_rulify}) =>
wenzelm@25497
    67
  make_data (f (base_sort, judgment, atomize_rulify)));
wenzelm@25497
    68
wenzelm@25497
    69
fun get_data thy = ObjectLogicData.get thy |> (fn Data args => args);
wenzelm@25497
    70
wenzelm@11897
    71
wenzelm@11897
    72
wenzelm@11897
    73
(** generic treatment of judgments -- with a single argument only **)
wenzelm@11897
    74
wenzelm@25497
    75
(* base_sort *)
wenzelm@25497
    76
wenzelm@25497
    77
val get_base_sort = #base_sort o get_data;
wenzelm@25497
    78
wenzelm@25497
    79
fun add_base_sort S = map_data (fn (base_sort, judgment, atomize_rulify) =>
wenzelm@25497
    80
  if is_some base_sort then error "Attempt to redeclare object-logic base sort"
wenzelm@25497
    81
  else (SOME S, judgment, atomize_rulify));
wenzelm@25497
    82
wenzelm@25497
    83
wenzelm@18825
    84
(* add judgment *)
wenzelm@11897
    85
wenzelm@11897
    86
local
wenzelm@11897
    87
wenzelm@30344
    88
fun gen_add_judgment add_consts (b, T, mx) thy =
wenzelm@35129
    89
  let val c = Sign.full_name thy b in
skalberg@14226
    90
    thy
wenzelm@30344
    91
    |> add_consts [(b, T, mx)]
wenzelm@25018
    92
    |> (fn thy' => Theory.add_deps c (c, Sign.the_const_type thy' c) [] thy')
wenzelm@25497
    93
    |> map_data (fn (base_sort, judgment, atomize_rulify) =>
wenzelm@25497
    94
        if is_some judgment then error "Attempt to redeclare object-logic judgment"
wenzelm@25497
    95
        else (base_sort, SOME c, atomize_rulify))
skalberg@14226
    96
  end;
wenzelm@11897
    97
wenzelm@11897
    98
in
wenzelm@11897
    99
wenzelm@30344
   100
val add_judgment = gen_add_judgment Sign.add_consts_i;
wenzelm@30344
   101
val add_judgment_cmd = gen_add_judgment Sign.add_consts;
wenzelm@11897
   102
wenzelm@11897
   103
end;
wenzelm@11897
   104
wenzelm@11897
   105
wenzelm@23566
   106
(* judgments *)
wenzelm@11897
   107
wenzelm@16449
   108
fun judgment_name thy =
wenzelm@25497
   109
  (case #judgment (get_data thy) of
wenzelm@25497
   110
    SOME name => name
wenzelm@11897
   111
  | _ => raise TERM ("Unknown object-logic judgment", []));
wenzelm@11897
   112
wenzelm@16449
   113
fun is_judgment thy (Const (c, _) $ _) = c = judgment_name thy
wenzelm@11897
   114
  | is_judgment _ _ = false;
wenzelm@11897
   115
wenzelm@16449
   116
fun drop_judgment thy (Abs (x, T, t)) = Abs (x, T, drop_judgment thy t)
wenzelm@16449
   117
  | drop_judgment thy (tm as (Const (c, _) $ t)) =
wenzelm@16449
   118
      if (c = judgment_name thy handle TERM _ => false) then t else tm
wenzelm@11897
   119
  | drop_judgment _ tm = tm;
wenzelm@11897
   120
wenzelm@16449
   121
fun fixed_judgment thy x =
wenzelm@11897
   122
  let  (*be robust wrt. low-level errors*)
wenzelm@16449
   123
    val c = judgment_name thy;
wenzelm@24848
   124
    val aT = TFree (Name.aT, []);
wenzelm@11897
   125
    val T =
wenzelm@18939
   126
      the_default (aT --> propT) (Sign.const_type thy c)
wenzelm@11897
   127
      |> Term.map_type_tvar (fn ((x, _), S) => TFree (x, S));
wenzelm@11897
   128
    val U = Term.domain_type T handle Match => aT;
wenzelm@11897
   129
  in Const (c, T) $ Free (x, U) end;
wenzelm@11897
   130
wenzelm@18121
   131
fun ensure_propT thy t =
wenzelm@13376
   132
  let val T = Term.fastype_of t
wenzelm@16449
   133
  in if T = propT then t else Const (judgment_name thy, T --> propT) $ t end;
wenzelm@13376
   134
wenzelm@23586
   135
fun dest_judgment ct =
wenzelm@23586
   136
  if is_judgment (Thm.theory_of_cterm ct) (Thm.term_of ct)
wenzelm@23586
   137
  then Thm.dest_arg ct
wenzelm@23586
   138
  else raise CTERM ("dest_judgment", [ct]);
wenzelm@23586
   139
wenzelm@23566
   140
fun judgment_conv cv ct =
wenzelm@23566
   141
  if is_judgment (Thm.theory_of_cterm ct) (Thm.term_of ct)
wenzelm@23566
   142
  then Conv.arg_conv cv ct
wenzelm@23566
   143
  else raise CTERM ("judgment_conv", [ct]);
wenzelm@23566
   144
wenzelm@11897
   145
wenzelm@19261
   146
(* elimination rules *)
wenzelm@19261
   147
wenzelm@19261
   148
fun is_elim rule =
wenzelm@19261
   149
  let
wenzelm@19261
   150
    val thy = Thm.theory_of_thm rule;
wenzelm@19261
   151
    val concl = Thm.concl_of rule;
wenzelm@19261
   152
  in
wenzelm@19261
   153
    Term.is_Var (drop_judgment thy concl) andalso
wenzelm@19261
   154
      exists (fn prem => concl aconv Logic.strip_assums_concl prem) (Thm.prems_of rule)
wenzelm@19261
   155
  end;
wenzelm@19261
   156
wenzelm@19261
   157
wenzelm@11897
   158
wenzelm@11897
   159
(** treatment of meta-level connectives **)
wenzelm@11897
   160
wenzelm@11897
   161
(* maintain rules *)
wenzelm@11897
   162
wenzelm@25497
   163
val get_atomize = #1 o #atomize_rulify o get_data;
wenzelm@25497
   164
val get_rulify = #2 o #atomize_rulify o get_data;
wenzelm@11897
   165
wenzelm@25497
   166
fun add_atomize th = map_data (fn (base_sort, judgment, (atomize, rulify)) =>
wenzelm@25497
   167
  (base_sort, judgment, (Thm.add_thm th atomize, rulify)));
wenzelm@25497
   168
wenzelm@25497
   169
fun add_rulify th = map_data (fn (base_sort, judgment, (atomize, rulify)) =>
wenzelm@25497
   170
  (base_sort, judgment, (atomize, Thm.add_thm th rulify)));
wenzelm@11897
   171
wenzelm@22846
   172
val declare_atomize = Thm.declaration_attribute (fn th => Context.mapping (add_atomize th) I);
wenzelm@22846
   173
val declare_rulify = Thm.declaration_attribute (fn th => Context.mapping (add_rulify th) I);
wenzelm@22846
   174
wenzelm@28620
   175
val _ = Context.>> (Context.map_theory (fold add_rulify Drule.norm_hhf_eqs));
wenzelm@11897
   176
wenzelm@11897
   177
wenzelm@11897
   178
(* atomize *)
wenzelm@11897
   179
wenzelm@16449
   180
fun atomize_term thy =
wenzelm@16449
   181
  drop_judgment thy o MetaSimplifier.rewrite_term thy (get_atomize thy) [];
wenzelm@12729
   182
wenzelm@23602
   183
fun atomize ct =
wenzelm@23602
   184
  MetaSimplifier.rewrite true (get_atomize (Thm.theory_of_cterm ct)) ct;
paulson@14743
   185
wenzelm@23602
   186
fun atomize_prems ct =
wenzelm@23602
   187
  if Logic.has_meta_prems (Thm.term_of ct) then
wenzelm@26568
   188
    Conv.params_conv ~1 (K (Conv.prems_conv ~1 atomize))
wenzelm@36610
   189
      (ProofContext.init_global (Thm.theory_of_cterm ct)) ct
wenzelm@23602
   190
  else Conv.all_conv ct;
wenzelm@11897
   191
wenzelm@23602
   192
val atomize_prems_tac = CONVERSION atomize_prems;
wenzelm@23602
   193
val full_atomize_tac = CONVERSION atomize;
wenzelm@11897
   194
wenzelm@11897
   195
wenzelm@11897
   196
(* rulify *)
wenzelm@11897
   197
wenzelm@18807
   198
fun rulify_term thy = MetaSimplifier.rewrite_term thy (get_rulify thy) [];
wenzelm@23540
   199
fun rulify_tac i st = MetaSimplifier.rewrite_goal_tac (get_rulify (Thm.theory_of_thm st)) i st;
wenzelm@18807
   200
wenzelm@11897
   201
fun gen_rulify full thm =
wenzelm@21708
   202
  MetaSimplifier.simplify full (get_rulify (Thm.theory_of_thm thm)) thm
wenzelm@20912
   203
  |> Drule.gen_all |> Thm.strip_shyps |> Drule.zero_var_indexes;
wenzelm@11897
   204
wenzelm@11897
   205
val rulify = gen_rulify true;
wenzelm@11897
   206
val rulify_no_asm = gen_rulify false;
wenzelm@11897
   207
wenzelm@18728
   208
fun rule_format x = Thm.rule_attribute (fn _ => rulify) x;
wenzelm@18728
   209
fun rule_format_no_asm x = Thm.rule_attribute (fn _ => rulify_no_asm) x;
wenzelm@11897
   210
wenzelm@11897
   211
end;