src/Pure/Isar/object_logic.ML
author wenzelm
Mon Nov 14 16:52:19 2011 +0100 (2011-11-14 ago)
changeset 45488 6d71d9e52369
parent 45375 7fe19930dfc9
child 52232 a2d4ae3e04a2
permissions -rw-r--r--
pass positions for named targets, for formal links in the document model;
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@41581
    20
  val elim_concl: thm -> term option
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@37216
    49
structure Data = 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@41493
    57
    | merge_opt _ data = merge_options data;
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@37216
    66
fun map_data f = Data.map (fn (Data {base_sort, judgment, atomize_rulify}) =>
wenzelm@25497
    67
  make_data (f (base_sort, judgment, atomize_rulify)));
wenzelm@25497
    68
wenzelm@37216
    69
fun get_data thy = Data.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@42375
    92
    |> (fn thy' => Theory.add_deps_global 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@41581
   148
fun elim_concl 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@41581
   152
    val C = drop_judgment thy concl;
wenzelm@19261
   153
  in
wenzelm@41581
   154
    if Term.is_Var C andalso
wenzelm@19261
   155
      exists (fn prem => concl aconv Logic.strip_assums_concl prem) (Thm.prems_of rule)
wenzelm@41581
   156
    then SOME C else NONE
wenzelm@19261
   157
  end;
wenzelm@19261
   158
wenzelm@19261
   159
wenzelm@11897
   160
wenzelm@11897
   161
(** treatment of meta-level connectives **)
wenzelm@11897
   162
wenzelm@11897
   163
(* maintain rules *)
wenzelm@11897
   164
wenzelm@25497
   165
val get_atomize = #1 o #atomize_rulify o get_data;
wenzelm@25497
   166
val get_rulify = #2 o #atomize_rulify o get_data;
wenzelm@11897
   167
wenzelm@25497
   168
fun add_atomize th = map_data (fn (base_sort, judgment, (atomize, rulify)) =>
wenzelm@25497
   169
  (base_sort, judgment, (Thm.add_thm th atomize, rulify)));
wenzelm@25497
   170
wenzelm@25497
   171
fun add_rulify th = map_data (fn (base_sort, judgment, (atomize, rulify)) =>
wenzelm@25497
   172
  (base_sort, judgment, (atomize, Thm.add_thm th rulify)));
wenzelm@11897
   173
wenzelm@22846
   174
val declare_atomize = Thm.declaration_attribute (fn th => Context.mapping (add_atomize th) I);
wenzelm@22846
   175
val declare_rulify = Thm.declaration_attribute (fn th => Context.mapping (add_rulify th) I);
wenzelm@22846
   176
wenzelm@28620
   177
val _ = Context.>> (Context.map_theory (fold add_rulify Drule.norm_hhf_eqs));
wenzelm@11897
   178
wenzelm@11897
   179
wenzelm@11897
   180
(* atomize *)
wenzelm@11897
   181
wenzelm@16449
   182
fun atomize_term thy =
wenzelm@41228
   183
  drop_judgment thy o Raw_Simplifier.rewrite_term thy (get_atomize thy) [];
wenzelm@12729
   184
wenzelm@23602
   185
fun atomize ct =
wenzelm@41228
   186
  Raw_Simplifier.rewrite true (get_atomize (Thm.theory_of_cterm ct)) ct;
paulson@14743
   187
wenzelm@23602
   188
fun atomize_prems ct =
wenzelm@23602
   189
  if Logic.has_meta_prems (Thm.term_of ct) then
wenzelm@26568
   190
    Conv.params_conv ~1 (K (Conv.prems_conv ~1 atomize))
wenzelm@42360
   191
      (Proof_Context.init_global (Thm.theory_of_cterm ct)) ct
wenzelm@23602
   192
  else Conv.all_conv ct;
wenzelm@11897
   193
wenzelm@23602
   194
val atomize_prems_tac = CONVERSION atomize_prems;
wenzelm@23602
   195
val full_atomize_tac = CONVERSION atomize;
wenzelm@11897
   196
wenzelm@11897
   197
wenzelm@11897
   198
(* rulify *)
wenzelm@11897
   199
wenzelm@41228
   200
fun rulify_term thy = Raw_Simplifier.rewrite_term thy (get_rulify thy) [];
wenzelm@41228
   201
fun rulify_tac i st = rewrite_goal_tac (get_rulify (Thm.theory_of_thm st)) i st;
wenzelm@18807
   202
wenzelm@11897
   203
fun gen_rulify full thm =
wenzelm@41228
   204
  Raw_Simplifier.simplify full (get_rulify (Thm.theory_of_thm thm)) thm
wenzelm@20912
   205
  |> Drule.gen_all |> Thm.strip_shyps |> Drule.zero_var_indexes;
wenzelm@11897
   206
wenzelm@11897
   207
val rulify = gen_rulify true;
wenzelm@11897
   208
val rulify_no_asm = gen_rulify false;
wenzelm@11897
   209
wenzelm@45375
   210
val rule_format = Thm.rule_attribute (K rulify);
wenzelm@45375
   211
val rule_format_no_asm = Thm.rule_attribute (K rulify_no_asm);
wenzelm@11897
   212
wenzelm@11897
   213
end;