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