src/Pure/Isar/object_logic.ML
author wenzelm
Thu Jan 19 21:22:08 2006 +0100 (2006-01-19 ago)
changeset 18708 4b3dadb4fe33
parent 18563 1df7022eac6f
child 18728 6790126ab5f6
permissions -rw-r--r--
setup: theory -> theory;
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@11897
    17
  val declare_atomize: theory attribute
wenzelm@11897
    18
  val declare_rulify: theory attribute
wenzelm@16449
    19
  val atomize_term: theory -> term -> term
wenzelm@17902
    20
  val atomize_cterm: theory -> cterm -> thm
paulson@14743
    21
  val atomize_thm: thm -> thm
wenzelm@11897
    22
  val atomize_tac: int -> tactic
wenzelm@12829
    23
  val full_atomize_tac: int -> tactic
wenzelm@11897
    24
  val atomize_goal: int -> thm -> thm
wenzelm@11897
    25
  val rulify: thm -> thm
wenzelm@11897
    26
  val rulify_no_asm: thm -> thm
wenzelm@11897
    27
  val rule_format: 'a attribute
wenzelm@11897
    28
  val rule_format_no_asm: 'a attribute
wenzelm@11897
    29
end;
wenzelm@11897
    30
wenzelm@11897
    31
structure ObjectLogic: OBJECT_LOGIC =
wenzelm@11897
    32
struct
wenzelm@11897
    33
wenzelm@11897
    34
wenzelm@11897
    35
(** object-logic theory data **)
wenzelm@11897
    36
wenzelm@11897
    37
(* data kind 'Pure/object-logic' *)
wenzelm@11897
    38
wenzelm@16449
    39
structure ObjectLogicData = TheoryDataFun
wenzelm@16449
    40
(struct
wenzelm@18563
    41
  val name = "Pure/object_logic";
wenzelm@11897
    42
  type T = string option * (thm list * thm list);
wenzelm@11897
    43
skalberg@15531
    44
  val empty = (NONE, ([], [Drule.norm_hhf_eq]));
wenzelm@11897
    45
  val copy = I;
wenzelm@16449
    46
  val extend = I;
wenzelm@11897
    47
skalberg@15531
    48
  fun merge_judgment (SOME x, SOME y) =
skalberg@15531
    49
        if x = y then SOME x else error "Attempt to merge different object-logics"
wenzelm@15973
    50
    | merge_judgment (j1, j2) = if is_some j1 then j1 else j2;
wenzelm@11897
    51
wenzelm@16449
    52
  fun merge _ ((judgment1, (atomize1, rulify1)), (judgment2, (atomize2, rulify2))) =
wenzelm@11897
    53
    (merge_judgment (judgment1, judgment2),
wenzelm@11897
    54
      (Drule.merge_rules (atomize1, atomize2), Drule.merge_rules (rulify1, rulify2)));
wenzelm@11897
    55
wenzelm@11897
    56
  fun print _ _ = ();
wenzelm@16449
    57
end);
wenzelm@11897
    58
wenzelm@18708
    59
val _ = Context.add_setup ObjectLogicData.init;
wenzelm@15801
    60
wenzelm@11897
    61
wenzelm@11897
    62
wenzelm@11897
    63
(** generic treatment of judgments -- with a single argument only **)
wenzelm@11897
    64
wenzelm@11897
    65
(* add_judgment(_i) *)
wenzelm@11897
    66
wenzelm@11897
    67
local
wenzelm@11897
    68
skalberg@15531
    69
fun new_judgment name (NONE, rules) = (SOME name, rules)
skalberg@15531
    70
  | new_judgment _ (SOME _, _) = error "Attempt to redeclare object-logic judgment";
wenzelm@11897
    71
wenzelm@16449
    72
fun gen_add_judgment add_consts (bname, T, mx) thy =
wenzelm@16449
    73
  let val c = Sign.full_name thy (Syntax.const_name bname mx) in
skalberg@14226
    74
    thy
wenzelm@16449
    75
    |> add_consts [(bname, T, mx)]
wenzelm@16449
    76
    |> (fn thy' => Theory.add_finals_i false [(Const (c, Sign.the_const_type thy' c))] thy')
wenzelm@16449
    77
    |> ObjectLogicData.map (new_judgment c)
skalberg@14226
    78
  end;
wenzelm@11897
    79
wenzelm@11897
    80
in
wenzelm@11897
    81
wenzelm@11897
    82
val add_judgment = gen_add_judgment Theory.add_consts;
wenzelm@11897
    83
val add_judgment_i = gen_add_judgment Theory.add_consts_i;
wenzelm@11897
    84
wenzelm@11897
    85
end;
wenzelm@11897
    86
wenzelm@11897
    87
wenzelm@11897
    88
(* term operations *)
wenzelm@11897
    89
wenzelm@16449
    90
fun judgment_name thy =
wenzelm@16449
    91
  (case ObjectLogicData.get thy of
skalberg@15531
    92
    (SOME name, _) => name
wenzelm@11897
    93
  | _ => raise TERM ("Unknown object-logic judgment", []));
wenzelm@11897
    94
wenzelm@16449
    95
fun is_judgment thy (Const (c, _) $ _) = c = judgment_name thy
wenzelm@11897
    96
  | is_judgment _ _ = false;
wenzelm@11897
    97
wenzelm@16449
    98
fun drop_judgment thy (Abs (x, T, t)) = Abs (x, T, drop_judgment thy t)
wenzelm@16449
    99
  | drop_judgment thy (tm as (Const (c, _) $ t)) =
wenzelm@16449
   100
      if (c = judgment_name thy handle TERM _ => false) then t else tm
wenzelm@11897
   101
  | drop_judgment _ tm = tm;
wenzelm@11897
   102
wenzelm@16449
   103
fun fixed_judgment thy x =
wenzelm@11897
   104
  let  (*be robust wrt. low-level errors*)
wenzelm@16449
   105
    val c = judgment_name thy;
wenzelm@14854
   106
    val aT = TFree ("'a", []);
wenzelm@11897
   107
    val T =
wenzelm@16449
   108
      if_none (Sign.const_type thy c) (aT --> propT)
wenzelm@11897
   109
      |> Term.map_type_tvar (fn ((x, _), S) => TFree (x, S));
wenzelm@11897
   110
    val U = Term.domain_type T handle Match => aT;
wenzelm@11897
   111
  in Const (c, T) $ Free (x, U) end;
wenzelm@11897
   112
wenzelm@18121
   113
fun ensure_propT thy t =
wenzelm@13376
   114
  let val T = Term.fastype_of t
wenzelm@16449
   115
  in if T = propT then t else Const (judgment_name thy, T --> propT) $ t end;
wenzelm@13376
   116
wenzelm@11897
   117
wenzelm@11897
   118
wenzelm@11897
   119
(** treatment of meta-level connectives **)
wenzelm@11897
   120
wenzelm@11897
   121
(* maintain rules *)
wenzelm@11897
   122
wenzelm@16449
   123
val get_atomize = #1 o #2 o ObjectLogicData.get;
wenzelm@16449
   124
val get_rulify = #2 o #2 o ObjectLogicData.get;
wenzelm@11897
   125
wenzelm@12371
   126
val add_atomize = ObjectLogicData.map o Library.apsnd o Library.apfst o Drule.add_rule;
wenzelm@12371
   127
val add_rulify = ObjectLogicData.map o Library.apsnd o Library.apsnd o Drule.add_rule;
wenzelm@11897
   128
wenzelm@12371
   129
fun declare_atomize (thy, th) = (add_atomize th thy, th);
wenzelm@12371
   130
fun declare_rulify (thy, th) = (add_rulify th thy, th);
wenzelm@11897
   131
wenzelm@11897
   132
wenzelm@11897
   133
(* atomize *)
wenzelm@11897
   134
skalberg@15001
   135
fun rewrite_prems_tac rews i = PRIMITIVE (Drule.fconv_rule
skalberg@15001
   136
  (Drule.goals_conv (Library.equal i)
wenzelm@18254
   137
    (Drule.forall_conv ~1
skalberg@15001
   138
      (Drule.goals_conv (K true) (Tactic.rewrite true rews)))));
wenzelm@11897
   139
wenzelm@16449
   140
fun atomize_term thy =
wenzelm@16449
   141
  drop_judgment thy o MetaSimplifier.rewrite_term thy (get_atomize thy) [];
wenzelm@12729
   142
wenzelm@17902
   143
fun atomize_cterm thy = Tactic.rewrite true (get_atomize thy);
wenzelm@17902
   144
fun atomize_thm th = rewrite_rule (get_atomize (Thm.theory_of_thm th)) th;
paulson@14743
   145
wenzelm@11897
   146
fun atomize_tac i st =
wenzelm@12807
   147
  if Logic.has_meta_prems (Thm.prop_of st) i then
wenzelm@16449
   148
    (rewrite_prems_tac (get_atomize (Thm.theory_of_thm st)) i) st
wenzelm@11897
   149
  else all_tac st;
wenzelm@11897
   150
wenzelm@12829
   151
fun full_atomize_tac i st =
wenzelm@16449
   152
  rewrite_goal_tac (get_atomize (Thm.theory_of_thm st)) i st;
wenzelm@12829
   153
wenzelm@11897
   154
fun atomize_goal i st =
skalberg@15531
   155
  (case Seq.pull (atomize_tac i st) of NONE => st | SOME (st', _) => st');
wenzelm@11897
   156
wenzelm@11897
   157
wenzelm@11897
   158
(* rulify *)
wenzelm@11897
   159
wenzelm@11897
   160
fun gen_rulify full thm =
wenzelm@16449
   161
  Tactic.simplify full (get_rulify (Thm.theory_of_thm thm)) thm
wenzelm@12725
   162
  |> Drule.gen_all |> Drule.strip_shyps_warning |> Drule.zero_var_indexes;
wenzelm@11897
   163
wenzelm@11897
   164
val rulify = gen_rulify true;
wenzelm@11897
   165
val rulify_no_asm = gen_rulify false;
wenzelm@11897
   166
wenzelm@11897
   167
fun rule_format x = Drule.rule_attribute (fn _ => rulify) x;
wenzelm@11897
   168
fun rule_format_no_asm x = Drule.rule_attribute (fn _ => rulify_no_asm) x;
wenzelm@11897
   169
wenzelm@11897
   170
wenzelm@11897
   171
end;