src/Pure/Isar/object_logic.ML
author haftmann
Thu May 24 08:37:37 2007 +0200 (2007-05-24)
changeset 23086 12320f6e2523
parent 22900 f8a7c10e1bd0
child 23540 77886dfbfa33
permissions -rw-r--r--
tuned Pure/General/name_space.ML
wenzelm@11897
     1
(*  Title:      Pure/Isar/object_logic.ML
wenzelm@11897
     2
    ID:         $Id$
wenzelm@11897
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@11897
     4
wenzelm@11897
     5
Specifics about common object-logics.
wenzelm@11897
     6
*)
wenzelm@11897
     7
wenzelm@11897
     8
signature OBJECT_LOGIC =
wenzelm@11897
     9
sig
wenzelm@11897
    10
  val add_judgment: bstring * string * mixfix -> theory -> theory
wenzelm@11897
    11
  val add_judgment_i: bstring * typ * mixfix -> theory -> theory
wenzelm@16449
    12
  val judgment_name: theory -> string
wenzelm@16449
    13
  val is_judgment: theory -> term -> bool
wenzelm@16449
    14
  val drop_judgment: theory -> term -> term
wenzelm@16449
    15
  val fixed_judgment: theory -> string -> term
wenzelm@18121
    16
  val ensure_propT: theory -> term -> term
wenzelm@19261
    17
  val is_elim: thm -> bool
wenzelm@18728
    18
  val declare_atomize: attribute
wenzelm@18728
    19
  val declare_rulify: attribute
wenzelm@16449
    20
  val atomize_term: theory -> term -> term
wenzelm@18783
    21
  val atomize_cterm: cterm -> thm
paulson@14743
    22
  val atomize_thm: thm -> thm
wenzelm@11897
    23
  val atomize_tac: int -> tactic
wenzelm@12829
    24
  val full_atomize_tac: int -> tactic
wenzelm@11897
    25
  val atomize_goal: int -> thm -> thm
wenzelm@18807
    26
  val rulify_term: theory -> term -> term
wenzelm@18807
    27
  val rulify_tac: int -> tactic
wenzelm@11897
    28
  val rulify: thm -> thm
wenzelm@11897
    29
  val rulify_no_asm: thm -> thm
wenzelm@18728
    30
  val rule_format: attribute
wenzelm@18728
    31
  val rule_format_no_asm: attribute
wenzelm@11897
    32
end;
wenzelm@11897
    33
wenzelm@11897
    34
structure ObjectLogic: OBJECT_LOGIC =
wenzelm@11897
    35
struct
wenzelm@11897
    36
wenzelm@11897
    37
wenzelm@18825
    38
(** theory data **)
wenzelm@11897
    39
wenzelm@16449
    40
structure ObjectLogicData = TheoryDataFun
wenzelm@22846
    41
(
wenzelm@11897
    42
  type T = string option * (thm list * thm list);
wenzelm@22846
    43
  val empty = (NONE, ([], []));
wenzelm@11897
    44
  val copy = I;
wenzelm@16449
    45
  val extend = I;
wenzelm@11897
    46
skalberg@15531
    47
  fun merge_judgment (SOME x, SOME y) =
skalberg@15531
    48
        if x = y then SOME x else error "Attempt to merge different object-logics"
wenzelm@15973
    49
    | merge_judgment (j1, j2) = if is_some j1 then j1 else j2;
wenzelm@11897
    50
wenzelm@16449
    51
  fun merge _ ((judgment1, (atomize1, rulify1)), (judgment2, (atomize2, rulify2))) =
wenzelm@11897
    52
    (merge_judgment (judgment1, judgment2),
wenzelm@11897
    53
      (Drule.merge_rules (atomize1, atomize2), Drule.merge_rules (rulify1, rulify2)));
wenzelm@22846
    54
);
wenzelm@15801
    55
wenzelm@11897
    56
wenzelm@11897
    57
wenzelm@11897
    58
(** generic treatment of judgments -- with a single argument only **)
wenzelm@11897
    59
wenzelm@18825
    60
(* add judgment *)
wenzelm@11897
    61
wenzelm@11897
    62
local
wenzelm@11897
    63
skalberg@15531
    64
fun new_judgment name (NONE, rules) = (SOME name, rules)
skalberg@15531
    65
  | new_judgment _ (SOME _, _) = error "Attempt to redeclare object-logic judgment";
wenzelm@11897
    66
wenzelm@16449
    67
fun gen_add_judgment add_consts (bname, T, mx) thy =
wenzelm@16449
    68
  let val c = Sign.full_name thy (Syntax.const_name bname mx) in
skalberg@14226
    69
    thy
wenzelm@16449
    70
    |> add_consts [(bname, T, mx)]
wenzelm@16449
    71
    |> (fn thy' => Theory.add_finals_i false [(Const (c, Sign.the_const_type thy' c))] thy')
wenzelm@16449
    72
    |> ObjectLogicData.map (new_judgment c)
skalberg@14226
    73
  end;
wenzelm@11897
    74
wenzelm@11897
    75
in
wenzelm@11897
    76
wenzelm@22796
    77
val add_judgment = gen_add_judgment Sign.add_consts;
wenzelm@22796
    78
val add_judgment_i = gen_add_judgment Sign.add_consts_i;
wenzelm@11897
    79
wenzelm@11897
    80
end;
wenzelm@11897
    81
wenzelm@11897
    82
wenzelm@11897
    83
(* term operations *)
wenzelm@11897
    84
wenzelm@16449
    85
fun judgment_name thy =
wenzelm@16449
    86
  (case ObjectLogicData.get thy of
skalberg@15531
    87
    (SOME name, _) => name
wenzelm@11897
    88
  | _ => raise TERM ("Unknown object-logic judgment", []));
wenzelm@11897
    89
wenzelm@16449
    90
fun is_judgment thy (Const (c, _) $ _) = c = judgment_name thy
wenzelm@11897
    91
  | is_judgment _ _ = false;
wenzelm@11897
    92
wenzelm@16449
    93
fun drop_judgment thy (Abs (x, T, t)) = Abs (x, T, drop_judgment thy t)
wenzelm@16449
    94
  | drop_judgment thy (tm as (Const (c, _) $ t)) =
wenzelm@16449
    95
      if (c = judgment_name thy handle TERM _ => false) then t else tm
wenzelm@11897
    96
  | drop_judgment _ tm = tm;
wenzelm@11897
    97
wenzelm@16449
    98
fun fixed_judgment thy x =
wenzelm@11897
    99
  let  (*be robust wrt. low-level errors*)
wenzelm@16449
   100
    val c = judgment_name thy;
wenzelm@14854
   101
    val aT = TFree ("'a", []);
wenzelm@11897
   102
    val T =
wenzelm@18939
   103
      the_default (aT --> propT) (Sign.const_type thy c)
wenzelm@11897
   104
      |> Term.map_type_tvar (fn ((x, _), S) => TFree (x, S));
wenzelm@11897
   105
    val U = Term.domain_type T handle Match => aT;
wenzelm@11897
   106
  in Const (c, T) $ Free (x, U) end;
wenzelm@11897
   107
wenzelm@18121
   108
fun ensure_propT thy t =
wenzelm@13376
   109
  let val T = Term.fastype_of t
wenzelm@16449
   110
  in if T = propT then t else Const (judgment_name thy, T --> propT) $ t end;
wenzelm@13376
   111
wenzelm@11897
   112
wenzelm@19261
   113
(* elimination rules *)
wenzelm@19261
   114
wenzelm@19261
   115
fun is_elim rule =
wenzelm@19261
   116
  let
wenzelm@19261
   117
    val thy = Thm.theory_of_thm rule;
wenzelm@19261
   118
    val concl = Thm.concl_of rule;
wenzelm@19261
   119
  in
wenzelm@19261
   120
    Term.is_Var (drop_judgment thy concl) andalso
wenzelm@19261
   121
      exists (fn prem => concl aconv Logic.strip_assums_concl prem) (Thm.prems_of rule)
wenzelm@19261
   122
  end;
wenzelm@19261
   123
wenzelm@19261
   124
wenzelm@11897
   125
wenzelm@11897
   126
(** treatment of meta-level connectives **)
wenzelm@11897
   127
wenzelm@11897
   128
(* maintain rules *)
wenzelm@11897
   129
wenzelm@16449
   130
val get_atomize = #1 o #2 o ObjectLogicData.get;
wenzelm@16449
   131
val get_rulify = #2 o #2 o ObjectLogicData.get;
wenzelm@11897
   132
wenzelm@22846
   133
val add_atomize = ObjectLogicData.map o apsnd o apfst o Drule.add_rule;
wenzelm@22846
   134
val add_rulify = ObjectLogicData.map o apsnd o apsnd o Drule.add_rule;
wenzelm@11897
   135
wenzelm@22846
   136
val declare_atomize = Thm.declaration_attribute (fn th => Context.mapping (add_atomize th) I);
wenzelm@22846
   137
val declare_rulify = Thm.declaration_attribute (fn th => Context.mapping (add_rulify th) I);
wenzelm@22846
   138
wenzelm@22846
   139
val _ = Context.add_setup (add_rulify Drule.norm_hhf_eq);
wenzelm@11897
   140
wenzelm@11897
   141
wenzelm@11897
   142
(* atomize *)
wenzelm@11897
   143
wenzelm@22900
   144
fun rewrite_prems_tac rews i = PRIMITIVE (Conv.fconv_rule
wenzelm@22900
   145
  (Conv.goals_conv (Library.equal i)
wenzelm@22900
   146
    (Conv.forall_conv ~1
wenzelm@22900
   147
      (Conv.goals_conv (K true) (MetaSimplifier.rewrite true rews)))));
wenzelm@11897
   148
wenzelm@16449
   149
fun atomize_term thy =
wenzelm@16449
   150
  drop_judgment thy o MetaSimplifier.rewrite_term thy (get_atomize thy) [];
wenzelm@12729
   151
wenzelm@21708
   152
fun atomize_cterm ct = MetaSimplifier.rewrite true (get_atomize (Thm.theory_of_cterm ct)) ct;
wenzelm@17902
   153
fun atomize_thm th = rewrite_rule (get_atomize (Thm.theory_of_thm th)) th;
paulson@14743
   154
wenzelm@11897
   155
fun atomize_tac i st =
wenzelm@12807
   156
  if Logic.has_meta_prems (Thm.prop_of st) i then
wenzelm@16449
   157
    (rewrite_prems_tac (get_atomize (Thm.theory_of_thm st)) i) st
wenzelm@11897
   158
  else all_tac st;
wenzelm@11897
   159
wenzelm@12829
   160
fun full_atomize_tac i st =
wenzelm@21687
   161
  Goal.rewrite_goal_tac (get_atomize (Thm.theory_of_thm st)) i st;
wenzelm@12829
   162
wenzelm@11897
   163
fun atomize_goal i st =
skalberg@15531
   164
  (case Seq.pull (atomize_tac i st) of NONE => st | SOME (st', _) => st');
wenzelm@11897
   165
wenzelm@11897
   166
wenzelm@11897
   167
(* rulify *)
wenzelm@11897
   168
wenzelm@18807
   169
fun rulify_term thy = MetaSimplifier.rewrite_term thy (get_rulify thy) [];
wenzelm@21687
   170
fun rulify_tac i st = Goal.rewrite_goal_tac (get_rulify (Thm.theory_of_thm st)) i st;
wenzelm@18807
   171
wenzelm@11897
   172
fun gen_rulify full thm =
wenzelm@21708
   173
  MetaSimplifier.simplify full (get_rulify (Thm.theory_of_thm thm)) thm
wenzelm@20912
   174
  |> Drule.gen_all |> Thm.strip_shyps |> Drule.zero_var_indexes;
wenzelm@11897
   175
wenzelm@11897
   176
val rulify = gen_rulify true;
wenzelm@11897
   177
val rulify_no_asm = gen_rulify false;
wenzelm@11897
   178
wenzelm@18728
   179
fun rule_format x = Thm.rule_attribute (fn _ => rulify) x;
wenzelm@18728
   180
fun rule_format_no_asm x = Thm.rule_attribute (fn _ => rulify_no_asm) x;
wenzelm@11897
   181
wenzelm@11897
   182
end;