src/Pure/assumption.ML
author haftmann
Tue Oct 20 16:13:01 2009 +0200 (2009-10-20)
changeset 33037 b22e44496dc2
parent 30479 fc58fb1f83ad
child 33519 e31a85f92ce9
permissions -rw-r--r--
replaced old_style infixes eq_set, subset, union, inter and variants by generic versions
wenzelm@20222
     1
(*  Title:      Pure/assumption.ML
wenzelm@20222
     2
    Author:     Makarius
wenzelm@20222
     3
wenzelm@30471
     4
Context assumptions, parameterized by export rules.
wenzelm@20222
     5
*)
wenzelm@20222
     6
wenzelm@20222
     7
signature ASSUMPTION =
wenzelm@20222
     8
sig
wenzelm@20222
     9
  type export
wenzelm@20222
    10
  val assume_export: export
wenzelm@20222
    11
  val presume_export: export
wenzelm@20222
    12
  val assume: cterm -> thm
wenzelm@30471
    13
  val all_assms_of: Proof.context -> cterm list
wenzelm@30471
    14
  val all_prems_of: Proof.context -> thm list
wenzelm@20296
    15
  val extra_hyps: Proof.context -> thm -> term list
wenzelm@30471
    16
  val local_assms_of: Proof.context -> Proof.context -> cterm list
wenzelm@30471
    17
  val local_prems_of: Proof.context -> Proof.context -> thm list
wenzelm@20296
    18
  val add_assms: export -> cterm list -> Proof.context -> thm list * Proof.context
wenzelm@20296
    19
  val add_assumes: cterm list -> Proof.context -> thm list * Proof.context
wenzelm@20296
    20
  val export: bool -> Proof.context -> Proof.context -> thm -> thm
wenzelm@21679
    21
  val export_term: Proof.context -> Proof.context -> term -> term
wenzelm@21517
    22
  val export_morphism: Proof.context -> Proof.context -> morphism
wenzelm@20222
    23
end;
wenzelm@20222
    24
wenzelm@20222
    25
structure Assumption: ASSUMPTION =
wenzelm@20222
    26
struct
wenzelm@20222
    27
wenzelm@20222
    28
(** basic rules **)
wenzelm@20222
    29
wenzelm@21679
    30
type export = bool -> cterm list -> (thm -> thm) * (term -> term);
wenzelm@20222
    31
wenzelm@20222
    32
(*
wenzelm@20222
    33
    [A]
wenzelm@20222
    34
     :
wenzelm@20222
    35
     B
wenzelm@20222
    36
  --------
wenzelm@20222
    37
  #A ==> B
wenzelm@20222
    38
*)
wenzelm@21679
    39
fun assume_export is_goal asms =
wenzelm@21679
    40
  (if is_goal then Drule.implies_intr_protected asms else Drule.implies_intr_list asms, fn t => t);
wenzelm@20222
    41
wenzelm@20222
    42
(*
wenzelm@20222
    43
    [A]
wenzelm@20222
    44
     :
wenzelm@20222
    45
     B
wenzelm@20222
    46
  -------
wenzelm@20222
    47
  A ==> B
wenzelm@20222
    48
*)
wenzelm@20222
    49
fun presume_export _ = assume_export false;
wenzelm@20222
    50
wenzelm@21605
    51
val assume = MetaSimplifier.norm_hhf o Thm.assume;
wenzelm@20222
    52
wenzelm@20222
    53
wenzelm@20222
    54
wenzelm@20222
    55
(** local context data **)
wenzelm@20222
    56
wenzelm@20222
    57
datatype data = Data of
wenzelm@30479
    58
 {assms: (export * cterm list) list,    (*assumes: A ==> _*)
wenzelm@20222
    59
  prems: thm list};                     (*prems: A |- A*)
wenzelm@20222
    60
wenzelm@20222
    61
fun make_data (assms, prems) = Data {assms = assms, prems = prems};
wenzelm@20222
    62
wenzelm@20222
    63
structure Data = ProofDataFun
wenzelm@20222
    64
(
wenzelm@20222
    65
  type T = data;
wenzelm@20222
    66
  fun init _ = make_data ([], []);
wenzelm@20222
    67
);
wenzelm@20222
    68
wenzelm@20222
    69
fun map_data f = Data.map (fn Data {assms, prems} => make_data (f (assms, prems)));
wenzelm@20222
    70
fun rep_data ctxt = Data.get ctxt |> (fn Data args => args);
wenzelm@20222
    71
wenzelm@30471
    72
wenzelm@30471
    73
(* all assumptions *)
wenzelm@30471
    74
wenzelm@30471
    75
val all_assumptions_of = #assms o rep_data;
wenzelm@30471
    76
val all_assms_of = maps #2 o all_assumptions_of;
wenzelm@30471
    77
val all_prems_of = #prems o rep_data;
wenzelm@20222
    78
wenzelm@30471
    79
fun extra_hyps ctxt th =
wenzelm@30471
    80
  subtract (op aconv) (map Thm.term_of (all_assms_of ctxt)) (Thm.hyps_of th);
wenzelm@30471
    81
wenzelm@30471
    82
(*named prems -- legacy feature*)
wenzelm@30471
    83
val _ = Context.>>
wenzelm@30471
    84
  (Context.map_theory (PureThy.add_thms_dynamic (Binding.name "prems",
wenzelm@30471
    85
    fn Context.Theory _ => [] | Context.Proof ctxt => all_prems_of ctxt)));
wenzelm@20222
    86
wenzelm@20222
    87
wenzelm@30471
    88
(* local assumptions *)
wenzelm@30471
    89
wenzelm@30471
    90
fun local_assumptions_of inner outer =
wenzelm@30471
    91
  Library.drop (length (all_assumptions_of outer), all_assumptions_of inner);
wenzelm@26392
    92
wenzelm@30471
    93
val local_assms_of = maps #2 oo local_assumptions_of;
wenzelm@30471
    94
wenzelm@30471
    95
fun local_prems_of inner outer =
wenzelm@30471
    96
  Library.drop (length (all_prems_of outer), all_prems_of inner);
wenzelm@26392
    97
wenzelm@26392
    98
wenzelm@20222
    99
(* add assumptions *)
wenzelm@20222
   100
wenzelm@20222
   101
fun add_assms export new_asms =
wenzelm@20222
   102
  let val new_prems = map assume new_asms in
wenzelm@20222
   103
    map_data (fn (asms, prems) => (asms @ [(export, new_asms)], prems @ new_prems)) #>
wenzelm@20222
   104
    pair new_prems
wenzelm@20222
   105
  end;
wenzelm@20222
   106
wenzelm@20222
   107
val add_assumes = add_assms assume_export;
wenzelm@20222
   108
wenzelm@20222
   109
wenzelm@20296
   110
(* export *)
wenzelm@20222
   111
wenzelm@20296
   112
fun export is_goal inner outer =
wenzelm@30479
   113
  MetaSimplifier.norm_hhf_protect #>
wenzelm@30479
   114
  fold_rev (fn (e, As) => #1 (e is_goal As)) (local_assumptions_of inner outer) #>
wenzelm@30479
   115
  MetaSimplifier.norm_hhf_protect;
wenzelm@20222
   116
wenzelm@21679
   117
fun export_term inner outer =
wenzelm@30471
   118
  fold_rev (fn (e, As) => #2 (e false As)) (local_assumptions_of inner outer);
wenzelm@21679
   119
wenzelm@21517
   120
fun export_morphism inner outer =
wenzelm@21517
   121
  let
wenzelm@21517
   122
    val thm = export false inner outer;
wenzelm@21679
   123
    val term = export_term inner outer;
wenzelm@21517
   124
    val typ = Logic.type_map term;
wenzelm@29605
   125
  in Morphism.morphism {binding = I, typ = typ, term = term, fact = map thm} end;
wenzelm@21517
   126
wenzelm@20222
   127
end;