src/Pure/assumption.ML
author wenzelm
Fri Jan 10 21:37:28 2014 +0100 (2014-01-10)
changeset 54984 da70ab8531f4
parent 54883 dd04a8b654fc
child 59150 71b416020f42
permissions -rw-r--r--
more elementary management of declared hyps, below structure Assumption;
Goal.prove: insist in declared hyps;
Simplifier: declare hyps via Thm.assume_hyps;
more accurate tool context in some boundary cases;
     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 = bool -> cterm list -> (thm -> thm) * (term -> term)
    10   val assume_export: export
    11   val presume_export: export
    12   val assume: Proof.context -> cterm -> thm
    13   val assume_hyps: cterm -> Proof.context -> thm * Proof.context
    14   val all_assms_of: Proof.context -> cterm list
    15   val all_prems_of: Proof.context -> thm 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 
    52 fun assume ctxt = Raw_Simplifier.norm_hhf ctxt o Thm.assume;
    53 
    54 fun assume_hyps ct ctxt =
    55   let val (th, ctxt') = Thm.assume_hyps ct ctxt
    56   in (Raw_Simplifier.norm_hhf ctxt' th, ctxt') end;
    57 
    58 
    59 
    60 (** local context data **)
    61 
    62 datatype data = Data of
    63  {assms: (export * cterm list) list,    (*assumes: A ==> _*)
    64   prems: thm list};                     (*prems: A |- norm_hhf A*)
    65 
    66 fun make_data (assms, prems) = Data {assms = assms, prems = prems};
    67 
    68 structure Data = Proof_Data
    69 (
    70   type T = data;
    71   fun init _ = make_data ([], []);
    72 );
    73 
    74 fun map_data f = Data.map (fn Data {assms, prems} => make_data (f (assms, prems)));
    75 fun rep_data ctxt = Data.get ctxt |> (fn Data rep => rep);
    76 
    77 
    78 (* all assumptions *)
    79 
    80 val all_assumptions_of = #assms o rep_data;
    81 val all_assms_of = maps #2 o all_assumptions_of;
    82 val all_prems_of = #prems o rep_data;
    83 
    84 
    85 (* local assumptions *)
    86 
    87 fun local_assumptions_of inner outer =
    88   drop (length (all_assumptions_of outer)) (all_assumptions_of inner);
    89 
    90 val local_assms_of = maps #2 oo local_assumptions_of;
    91 
    92 fun local_prems_of inner outer =
    93   drop (length (all_prems_of outer)) (all_prems_of inner);
    94 
    95 
    96 (* add assumptions *)
    97 
    98 fun add_assms export new_asms ctxt =
    99   let val (new_prems, ctxt') = fold_map assume_hyps new_asms ctxt in
   100     ctxt'
   101     |> map_data (fn (asms, prems) => (asms @ [(export, new_asms)], prems @ new_prems))
   102     |> pair new_prems
   103   end;
   104 
   105 val add_assumes = add_assms assume_export;
   106 
   107 
   108 (* export *)
   109 
   110 fun export is_goal inner outer =
   111   Raw_Simplifier.norm_hhf_protect inner #>
   112   fold_rev (fn (e, As) => #1 (e is_goal As)) (local_assumptions_of inner outer) #>
   113   Raw_Simplifier.norm_hhf_protect outer;
   114 
   115 fun export_term inner outer =
   116   fold_rev (fn (e, As) => #2 (e false As)) (local_assumptions_of inner outer);
   117 
   118 fun export_morphism inner outer =
   119   let
   120     val thm = export false inner outer;
   121     val term = export_term inner outer;
   122     val typ = Logic.type_map term;
   123   in
   124     Morphism.morphism "Assumption.export"
   125       {binding = [], typ = [typ], term = [term], fact = [map thm]}
   126   end;
   127 
   128 end;