src/Pure/Isar/object_logic.ML
author wenzelm
Wed Jul 10 14:49:06 2002 +0200 (2002-07-10)
changeset 13334 27149d72bdff
parent 13197 0567f4fd1415
child 13376 59975b8417e2
permissions -rw-r--r--
added assert_judgment;
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
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
wenzelm@11897
     5
wenzelm@11897
     6
Specifics about common object-logics.
wenzelm@11897
     7
*)
wenzelm@11897
     8
wenzelm@11897
     9
signature OBJECT_LOGIC =
wenzelm@11897
    10
sig
wenzelm@11897
    11
  val add_judgment: bstring * string * mixfix -> theory -> theory
wenzelm@11897
    12
  val add_judgment_i: bstring * typ * mixfix -> theory -> theory
wenzelm@11897
    13
  val judgment_name: Sign.sg -> string
wenzelm@11897
    14
  val is_judgment: Sign.sg -> term -> bool
wenzelm@11897
    15
  val drop_judgment: Sign.sg -> term -> term
wenzelm@13334
    16
  val assert_judgment: Sign.sg -> term -> term
wenzelm@11897
    17
  val fixed_judgment: Sign.sg -> string -> term
wenzelm@11897
    18
  val declare_atomize: theory attribute
wenzelm@11897
    19
  val declare_rulify: theory attribute
wenzelm@12807
    20
  val atomize_term: Sign.sg -> term -> term
wenzelm@11897
    21
  val atomize_tac: int -> tactic
wenzelm@12829
    22
  val full_atomize_tac: int -> tactic
wenzelm@11897
    23
  val atomize_goal: int -> thm -> thm
wenzelm@11897
    24
  val rulify: thm -> thm
wenzelm@11897
    25
  val rulify_no_asm: thm -> thm
wenzelm@11897
    26
  val rule_format: 'a attribute
wenzelm@11897
    27
  val rule_format_no_asm: 'a attribute
wenzelm@11897
    28
  val setup: (theory -> theory) list
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@11897
    39
structure ObjectLogicDataArgs =
wenzelm@11897
    40
struct
wenzelm@11897
    41
  val name = "Pure/object-logic";
wenzelm@11897
    42
  type T = string option * (thm list * thm list);
wenzelm@11897
    43
wenzelm@11897
    44
  val empty = (None, ([], [Drule.norm_hhf_eq]));
wenzelm@11897
    45
  val copy = I;
wenzelm@11897
    46
  val prep_ext = I;
wenzelm@11897
    47
wenzelm@11897
    48
  fun merge_judgment (Some x, Some y) =
wenzelm@11897
    49
        if x = y then Some x else error "Attempt to merge different object-logics"
wenzelm@11897
    50
    | merge_judgment (j1, j2) = if is_some j1 then j1 else j2;
wenzelm@11897
    51
wenzelm@11897
    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@11897
    57
end;
wenzelm@11897
    58
wenzelm@11897
    59
structure ObjectLogicData = TheoryDataFun(ObjectLogicDataArgs);
wenzelm@11897
    60
wenzelm@11897
    61
wenzelm@11897
    62
(** generic treatment of judgments -- with a single argument only **)
wenzelm@11897
    63
wenzelm@11897
    64
(* add_judgment(_i) *)
wenzelm@11897
    65
wenzelm@11897
    66
local
wenzelm@11897
    67
wenzelm@11897
    68
fun new_judgment name (None, rules) = (Some name, rules)
wenzelm@11897
    69
  | new_judgment _ (Some _, _) = error "Attempt to redeclare object-logic judgment";
wenzelm@11897
    70
wenzelm@11897
    71
fun gen_add_judgment add_consts (name, T, syn) thy =
wenzelm@11897
    72
  thy
wenzelm@11897
    73
  |> add_consts [(name, T, syn)]
wenzelm@11897
    74
  |> ObjectLogicData.map (new_judgment (Sign.full_name (Theory.sign_of thy) name));
wenzelm@11897
    75
wenzelm@11897
    76
in
wenzelm@11897
    77
wenzelm@11897
    78
val add_judgment = gen_add_judgment Theory.add_consts;
wenzelm@11897
    79
val add_judgment_i = gen_add_judgment Theory.add_consts_i;
wenzelm@11897
    80
wenzelm@11897
    81
end;
wenzelm@11897
    82
wenzelm@11897
    83
wenzelm@11897
    84
(* term operations *)
wenzelm@11897
    85
wenzelm@11897
    86
fun judgment_name sg =
wenzelm@11897
    87
  (case ObjectLogicData.get_sg sg of
wenzelm@11897
    88
    (Some name, _) => name
wenzelm@11897
    89
  | _ => raise TERM ("Unknown object-logic judgment", []));
wenzelm@11897
    90
wenzelm@11897
    91
fun is_judgment sg (Const (c, _) $ _) = c = judgment_name sg
wenzelm@11897
    92
  | is_judgment _ _ = false;
wenzelm@11897
    93
wenzelm@11897
    94
fun drop_judgment sg (Abs (x, T, t)) = Abs (x, T, drop_judgment sg t)
wenzelm@12479
    95
  | drop_judgment sg (tm as (Const (c, _) $ t)) =
wenzelm@12479
    96
      if (c = judgment_name sg handle TERM _ => false) then t else tm
wenzelm@11897
    97
  | drop_judgment _ tm = tm;
wenzelm@11897
    98
wenzelm@13334
    99
fun assert_judgment sg (Abs (x, T, t)) = Abs (x, T, assert_judgment sg t)
wenzelm@13334
   100
  | assert_judgment sg t =
wenzelm@13334
   101
      if (is_judgment sg t handle TERM _ => true) then t
wenzelm@13334
   102
      else Const (judgment_name sg, fastype_of t --> propT) $ t;
wenzelm@13334
   103
wenzelm@11897
   104
fun fixed_judgment sg x =
wenzelm@11897
   105
  let  (*be robust wrt. low-level errors*)
wenzelm@11897
   106
    val c = judgment_name sg;
wenzelm@11897
   107
    val aT = TFree ("'a", logicS);
wenzelm@11897
   108
    val T =
wenzelm@11897
   109
      if_none (Sign.const_type sg c) (aT --> propT)
wenzelm@11897
   110
      |> Term.map_type_tvar (fn ((x, _), S) => TFree (x, S));
wenzelm@11897
   111
    val U = Term.domain_type T handle Match => aT;
wenzelm@11897
   112
  in Const (c, T) $ Free (x, U) end;
wenzelm@11897
   113
wenzelm@11897
   114
wenzelm@11897
   115
wenzelm@11897
   116
(** treatment of meta-level connectives **)
wenzelm@11897
   117
wenzelm@11897
   118
(* maintain rules *)
wenzelm@11897
   119
wenzelm@11897
   120
val get_atomize = #1 o #2 o ObjectLogicData.get_sg;
wenzelm@11897
   121
val get_rulify = #2 o #2 o ObjectLogicData.get_sg;
wenzelm@11897
   122
wenzelm@12371
   123
val add_atomize = ObjectLogicData.map o Library.apsnd o Library.apfst o Drule.add_rule;
wenzelm@12371
   124
val add_rulify = ObjectLogicData.map o Library.apsnd o Library.apsnd o Drule.add_rule;
wenzelm@11897
   125
wenzelm@12371
   126
fun declare_atomize (thy, th) = (add_atomize th thy, th);
wenzelm@12371
   127
fun declare_rulify (thy, th) = (add_rulify th thy, th);
wenzelm@11897
   128
wenzelm@11897
   129
wenzelm@11897
   130
(* atomize *)
wenzelm@11897
   131
wenzelm@11897
   132
fun rewrite_prems_tac rews i = PRIMITIVE (MetaSimplifier.fconv_rule
wenzelm@11897
   133
  (MetaSimplifier.goals_conv (Library.equal i)
wenzelm@11897
   134
    (MetaSimplifier.forall_conv
wenzelm@11897
   135
      (MetaSimplifier.goals_conv (K true) (Tactic.rewrite true rews)))));
wenzelm@11897
   136
wenzelm@12807
   137
fun atomize_term sg =
berghofe@13197
   138
  drop_judgment sg o MetaSimplifier.rewrite_term sg (get_atomize sg) [];
wenzelm@12729
   139
wenzelm@11897
   140
fun atomize_tac i st =
wenzelm@12807
   141
  if Logic.has_meta_prems (Thm.prop_of st) i then
wenzelm@11897
   142
    (rewrite_prems_tac (get_atomize (Thm.sign_of_thm st)) i) st
wenzelm@11897
   143
  else all_tac st;
wenzelm@11897
   144
wenzelm@12829
   145
fun full_atomize_tac i st =
wenzelm@12829
   146
  rewrite_goal_tac (get_atomize (Thm.sign_of_thm st)) i st;
wenzelm@12829
   147
wenzelm@11897
   148
fun atomize_goal i st =
wenzelm@11897
   149
  (case Seq.pull (atomize_tac i st) of None => st | Some (st', _) => st');
wenzelm@11897
   150
wenzelm@11897
   151
wenzelm@11897
   152
(* rulify *)
wenzelm@11897
   153
wenzelm@11897
   154
fun gen_rulify full thm =
wenzelm@11897
   155
  Tactic.simplify full (get_rulify (Thm.sign_of_thm thm)) thm
wenzelm@12725
   156
  |> Drule.gen_all |> Drule.strip_shyps_warning |> Drule.zero_var_indexes;
wenzelm@11897
   157
wenzelm@11897
   158
val rulify = gen_rulify true;
wenzelm@11897
   159
val rulify_no_asm = gen_rulify false;
wenzelm@11897
   160
wenzelm@11897
   161
fun rule_format x = Drule.rule_attribute (fn _ => rulify) x;
wenzelm@11897
   162
fun rule_format_no_asm x = Drule.rule_attribute (fn _ => rulify_no_asm) x;
wenzelm@11897
   163
wenzelm@11897
   164
wenzelm@11897
   165
wenzelm@11897
   166
(** theory setup **)
wenzelm@11897
   167
wenzelm@11897
   168
val setup = [ObjectLogicData.init];
wenzelm@11897
   169
wenzelm@11897
   170
wenzelm@11897
   171
end;