src/Pure/Isar/local_defs.ML
author wenzelm
Sun Jul 29 14:29:54 2007 +0200 (2007-07-29 ago)
changeset 24039 273698405054
parent 23541 f8c5e218e4d8
child 24261 dd31811bdf46
permissions -rw-r--r--
renamed Drule.add/del/merge_rules to Thm.add/del/merge_thms;
     1 (*  Title:      Pure/Isar/local_defs.ML
     2     ID:         $Id$
     3     Author:     Makarius
     4 
     5 Local definitions.
     6 *)
     7 
     8 signature LOCAL_DEFS =
     9 sig
    10   val cert_def: Proof.context -> term -> (string * typ) * term
    11   val abs_def: term -> (string * typ) * term
    12   val mk_def: Proof.context -> (string * term) list -> term list
    13   val expand: cterm list -> thm -> thm
    14   val def_export: Assumption.export
    15   val add_defs: ((string * mixfix) * ((bstring * attribute list) * term)) list -> Proof.context ->
    16     (term * (bstring * thm)) list * Proof.context
    17   val export: Proof.context -> Proof.context -> thm -> thm list * thm
    18   val trans_terms: Proof.context -> thm list -> thm
    19   val trans_props: Proof.context -> thm list -> thm
    20   val print_rules: Proof.context -> unit
    21   val defn_add: attribute
    22   val defn_del: attribute
    23   val meta_rewrite_conv: Proof.context -> conv
    24   val meta_rewrite_rule: Proof.context -> thm -> thm
    25   val unfold: Proof.context -> thm list -> thm -> thm
    26   val unfold_goals: Proof.context -> thm list -> thm -> thm
    27   val unfold_tac: Proof.context -> thm list -> tactic
    28   val fold: Proof.context -> thm list -> thm -> thm
    29   val fold_tac: Proof.context -> thm list -> tactic
    30   val derived_def: Proof.context -> bool -> term ->
    31     ((string * typ) * term) * (Proof.context -> thm -> thm)
    32 end;
    33 
    34 structure LocalDefs: LOCAL_DEFS =
    35 struct
    36 
    37 (** primitive definitions **)
    38 
    39 (* prepare defs *)
    40 
    41 fun cert_def ctxt eq =
    42   let
    43     val pp = ProofContext.pp ctxt;
    44     val display_term = quote o Pretty.string_of_term pp;
    45     fun err msg = cat_error msg ("The error(s) above occurred in definition: " ^ display_term eq);
    46     val ((lhs, _), eq') = eq
    47       |> Sign.no_vars pp
    48       |> Logic.dest_def pp Term.is_Free (Variable.is_fixed ctxt) (K true)
    49       handle TERM (msg, _) => err msg | ERROR msg => err msg;
    50   in (Term.dest_Free (Term.head_of lhs), eq') end;
    51 
    52 val abs_def = Logic.abs_def #>> Term.dest_Free;
    53 
    54 fun mk_def ctxt args =
    55   let
    56     val (xs, rhss) = split_list args;
    57     val (bind, _) = ProofContext.bind_fixes xs ctxt;
    58     val lhss = map (fn (x, rhs) => bind (Free (x, Term.fastype_of rhs))) args;
    59   in map Logic.mk_equals (lhss ~~ rhss) end;
    60 
    61 
    62 (* export defs *)
    63 
    64 val head_of_def =
    65   #1 o Term.dest_Free o Term.head_of o #1 o Logic.dest_equals o Term.strip_all_body;
    66 
    67 
    68 (*
    69   [x, x == a]
    70        :
    71       B x
    72   -----------
    73       B a
    74 *)
    75 fun expand defs =
    76   Drule.implies_intr_list defs
    77   #> Drule.generalize ([], map (head_of_def o Thm.term_of) defs)
    78   #> funpow (length defs) (fn th => Drule.reflexive_thm RS th);
    79 
    80 val expand_term = Envir.expand_term_frees o map (abs_def o Thm.term_of);
    81 
    82 fun def_export _ defs = (expand defs, expand_term defs);
    83 
    84 
    85 (* add defs *)
    86 
    87 fun add_defs defs ctxt =
    88   let
    89     val ((xs, mxs), specs) = defs |> split_list |>> split_list;
    90     val ((names, atts), rhss) = specs |> split_list |>> split_list;
    91     val names' = map2 Thm.def_name_optional xs names;
    92     val eqs = mk_def ctxt (xs ~~ rhss);
    93     val lhss = map (fst o Logic.dest_equals) eqs;
    94   in
    95     ctxt
    96     |> ProofContext.add_fixes_i (map2 (fn x => fn mx => (x, NONE, mx)) xs mxs) |> #2
    97     |> fold Variable.declare_term eqs
    98     |> ProofContext.add_assms_i def_export
    99       (map2 (fn a => fn eq => (a, [(eq, [])])) (names' ~~ atts) eqs)
   100     |>> map2 (fn lhs => fn (name, [th]) => (lhs, (name, th))) lhss
   101   end;
   102 
   103 
   104 (* specific export -- result based on educated guessing (beware of closure sizes) *)
   105 
   106 fun export inner outer =
   107   let
   108     val exp = Assumption.export false inner outer;
   109     val prems = Assumption.prems_of inner;
   110   in fn th =>
   111     let
   112       val th' = exp th;
   113       val still_fixed = map #1 (Thm.fold_terms Term.add_frees th' []);
   114       val defs = prems |> filter_out (fn prem =>
   115         (case try (head_of_def o Thm.prop_of) prem of
   116           SOME x => member (op =) still_fixed x
   117         | NONE => true));
   118     in (map Drule.abs_def defs, th') end
   119   end;
   120 
   121 
   122 (* basic transitivity reasoning -- modulo beta-eta *)
   123 
   124 local
   125 
   126 val is_trivial = Pattern.aeconv o Logic.dest_equals o Thm.prop_of;
   127 
   128 fun trans_list _ _ [] = raise Empty
   129   | trans_list trans ctxt (th :: raw_eqs) =
   130       (case filter_out is_trivial raw_eqs of
   131         [] => th
   132       | eqs =>
   133           let val ((_, th' :: eqs'), ctxt') = Variable.import_thms true (th :: eqs) ctxt
   134           in singleton (Variable.export ctxt' ctxt) (fold trans eqs' th') end);
   135 
   136 in
   137 
   138 val trans_terms = trans_list (fn eq2 => fn eq1 => eq2 COMP (eq1 COMP Drule.transitive_thm));
   139 val trans_props = trans_list (fn eq => fn th => th COMP (eq COMP Drule.equal_elim_rule1));
   140 
   141 end;
   142 
   143 
   144 
   145 (** defived definitions **)
   146 
   147 (* transformation rules *)
   148 
   149 structure Rules = GenericDataFun
   150 (
   151   type T = thm list;
   152   val empty = []
   153   val extend = I;
   154   fun merge _ = Thm.merge_thms;
   155 );
   156 
   157 fun print_rules ctxt =
   158   Pretty.writeln (Pretty.big_list "definitional transformations:"
   159     (map (ProofContext.pretty_thm ctxt) (Rules.get (Context.Proof ctxt))));
   160 
   161 val defn_add = Thm.declaration_attribute (Rules.map o Thm.add_thm);
   162 val defn_del = Thm.declaration_attribute (Rules.map o Thm.del_thm);
   163 
   164 
   165 (* meta rewrite rules *)
   166 
   167 val equals_ss =
   168   MetaSimplifier.theory_context ProtoPure.thy MetaSimplifier.empty_ss
   169     addeqcongs [Drule.equals_cong];    (*protect meta-level equality*)
   170 
   171 fun meta_rewrite_conv ctxt =
   172   MetaSimplifier.rewrite_cterm (false, false, false) (K (K NONE))
   173     (equals_ss addsimps (Rules.get (Context.Proof ctxt)));
   174 
   175 val meta_rewrite_rule = Conv.fconv_rule o meta_rewrite_conv;
   176 
   177 
   178 (* rewriting with object-level rules *)
   179 
   180 fun meta f ctxt = f o map (meta_rewrite_rule ctxt);
   181 
   182 val unfold       = meta MetaSimplifier.rewrite_rule;
   183 val unfold_goals = meta MetaSimplifier.rewrite_goals_rule;
   184 val unfold_tac   = meta MetaSimplifier.rewrite_goals_tac;
   185 val fold         = meta MetaSimplifier.fold_rule;
   186 val fold_tac     = meta MetaSimplifier.fold_goals_tac;
   187 
   188 
   189 (* derived defs -- potentially within the object-logic *)
   190 
   191 fun derived_def ctxt conditional prop =
   192   let
   193     val ((c, T), rhs) = prop
   194       |> Thm.cterm_of (ProofContext.theory_of ctxt)
   195       |> meta_rewrite_conv ctxt
   196       |> (snd o Logic.dest_equals o Thm.prop_of)
   197       |> conditional ? Logic.strip_imp_concl
   198       |> (abs_def o #2 o cert_def ctxt);
   199     fun prove ctxt' def =
   200       let
   201         val frees = Term.fold_aterms (fn Free (x, _) =>
   202           if Variable.is_fixed ctxt' x then I else insert (op =) x | _ => I) prop [];
   203       in
   204         Goal.prove ctxt' frees [] prop (K (ALLGOALS
   205           (CONVERSION (meta_rewrite_conv ctxt') THEN'
   206             MetaSimplifier.rewrite_goal_tac [def] THEN'
   207             resolve_tac [Drule.reflexive_thm])))
   208         handle ERROR msg => cat_error msg "Failed to prove definitional specification."
   209       end;
   210   in (((c, T), rhs), prove) end;
   211 
   212 end;