src/Pure/Isar/local_defs.ML
author wenzelm
Tue Dec 05 22:14:42 2006 +0100 (2006-12-05)
changeset 21658 5e31241e1e3c
parent 21591 5e0f2340caa7
child 21684 e8c135b166b3
permissions -rw-r--r--
Attrib.internal: morphism;
     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 def_export: Assumption.export
    14   val add_defs: ((string * mixfix) * ((bstring * attribute list) * term)) list -> Proof.context ->
    15     (term * (bstring * thm)) list * Proof.context
    16   val export: Proof.context -> Proof.context -> thm -> thm list * thm
    17   val print_rules: Proof.context -> unit
    18   val defn_add: attribute
    19   val defn_del: attribute
    20   val meta_rewrite_rule: Proof.context -> thm -> thm
    21   val unfold: Proof.context -> thm list -> thm -> thm
    22   val unfold_goals: Proof.context -> thm list -> thm -> thm
    23   val unfold_tac: Proof.context -> thm list -> tactic
    24   val fold: Proof.context -> thm list -> thm -> thm
    25   val fold_tac: Proof.context -> thm list -> tactic
    26   val derived_def: Proof.context -> bool -> term ->
    27     ((string * typ) * term) * (Proof.context -> thm -> thm)
    28 end;
    29 
    30 structure LocalDefs: LOCAL_DEFS =
    31 struct
    32 
    33 (** primitive definitions **)
    34 
    35 (* prepare defs *)
    36 
    37 fun cert_def ctxt eq =
    38   let
    39     val pp = ProofContext.pp ctxt;
    40     val display_term = quote o Pretty.string_of_term pp;
    41     fun err msg = cat_error msg ("The error(s) above occurred in definition: " ^ display_term eq);
    42     val ((lhs, _), eq') = eq
    43       |> Sign.no_vars pp
    44       |> Logic.dest_def pp Term.is_Free (Variable.is_fixed ctxt) (K true)
    45       handle TERM (msg, _) => err msg | ERROR msg => err msg;
    46   in (Term.dest_Free (Term.head_of lhs), eq') end;
    47 
    48 val abs_def = Logic.abs_def #> apfst Term.dest_Free;
    49 
    50 fun mk_def ctxt args =
    51   let
    52     val (xs, rhss) = split_list args;
    53     val (bind, _) = ProofContext.bind_fixes xs ctxt;
    54     val lhss = map (fn (x, rhs) => bind (Free (x, Term.fastype_of rhs))) args;
    55   in map Logic.mk_equals (lhss ~~ rhss) end;
    56 
    57 
    58 (* export defs *)
    59 
    60 val head_of_def =
    61   #1 o Term.dest_Free o Term.head_of o #1 o Logic.dest_equals o Term.strip_all_body;
    62 
    63 
    64 (*
    65   [x, x == a]
    66        :
    67       B x
    68   -----------
    69       B a
    70 *)
    71 fun def_export _ defs thm =
    72   thm
    73   |> Drule.implies_intr_list defs
    74   |> Drule.generalize ([], map (head_of_def o Thm.term_of) defs)
    75   |> funpow (length defs) (fn th => Drule.reflexive_thm RS th);
    76 
    77 
    78 (* add defs *)
    79 
    80 fun add_defs defs ctxt =
    81   let
    82     val ((xs, mxs), specs) = defs |> split_list |>> split_list;
    83     val ((names, atts), rhss) = specs |> split_list |>> split_list;
    84     val names' = map2 Thm.def_name_optional xs names;
    85     val eqs = mk_def ctxt (xs ~~ rhss);
    86     val lhss = map (fst o Logic.dest_equals) eqs;
    87   in
    88     ctxt
    89     |> ProofContext.add_fixes_i (map2 (fn x => fn mx => (x, NONE, mx)) xs mxs) |> #2
    90     |> fold Variable.declare_term eqs
    91     |> ProofContext.add_assms_i def_export
    92       (map2 (fn a => fn eq => (a, [(eq, [])])) (names' ~~ atts) eqs)
    93     |>> map2 (fn lhs => fn (name, [th]) => (lhs, (name, th))) lhss
    94   end;
    95 
    96 
    97 (* export -- result based on educated guessing *)
    98 
    99 fun export inner outer th =
   100   let
   101     val th' = Assumption.export false inner outer th;
   102     val still_fixed = map #1 (Drule.fold_terms Term.add_frees th' []);
   103     val defs = Assumption.prems_of inner |> filter_out (fn prem =>
   104       (case try (head_of_def o Thm.prop_of) prem of
   105         SOME x => member (op =) still_fixed x
   106       | NONE => true));
   107   in (map Drule.abs_def defs, th') end;
   108 
   109 
   110 
   111 (** defived definitions **)
   112 
   113 (* transformation rules *)
   114 
   115 structure Rules = GenericDataFun
   116 (
   117   val name = "Pure/derived_defs";
   118   type T = thm list;
   119   val empty = []
   120   val extend = I;
   121   fun merge _ = Drule.merge_rules;
   122   fun print context rules =
   123     Pretty.writeln (Pretty.big_list "definitional transformations:"
   124       (map (ProofContext.pretty_thm (Context.proof_of context)) rules));
   125 );
   126 
   127 val _ = Context.add_setup Rules.init;
   128 
   129 val print_rules = Rules.print o Context.Proof;
   130 
   131 val defn_add = Thm.declaration_attribute (Rules.map o Drule.add_rule);
   132 val defn_del = Thm.declaration_attribute (Rules.map o Drule.del_rule);
   133 
   134 
   135 (* meta rewrite rules *)
   136 
   137 val equals_ss =
   138   MetaSimplifier.theory_context ProtoPure.thy MetaSimplifier.empty_ss
   139     addeqcongs [Drule.equals_cong];    (*protect meta-level equality*)
   140 
   141 fun meta_rewrite ctxt =
   142   MetaSimplifier.rewrite_cterm (false, false, false) (K (K NONE))
   143     (equals_ss addsimps (Rules.get (Context.Proof ctxt)));
   144 
   145 val meta_rewrite_rule = Drule.fconv_rule o meta_rewrite;
   146 
   147 fun meta_rewrite_tac ctxt i =
   148   PRIMITIVE (Drule.fconv_rule (Drule.goals_conv (equal i) (meta_rewrite ctxt)));
   149 
   150 
   151 (* rewriting with object-level rules *)
   152 
   153 fun meta f ctxt = f o map (meta_rewrite_rule ctxt);
   154 
   155 val unfold       = meta Tactic.rewrite_rule;
   156 val unfold_goals = meta Tactic.rewrite_goals_rule;
   157 val unfold_tac   = meta Tactic.rewrite_goals_tac;
   158 val fold         = meta Tactic.fold_rule;
   159 val fold_tac     = meta Tactic.fold_goals_tac;
   160 
   161 
   162 (* derived defs -- potentially within the object-logic *)
   163 
   164 fun derived_def ctxt conditional prop =
   165   let
   166     val ((c, T), rhs) = prop
   167       |> Thm.cterm_of (ProofContext.theory_of ctxt)
   168       |> meta_rewrite ctxt
   169       |> (snd o Logic.dest_equals o Thm.prop_of)
   170       |> conditional ? Logic.strip_imp_concl
   171       |> (abs_def o #2 o cert_def ctxt);
   172     fun prove ctxt' def =
   173       let
   174         val frees = Term.fold_aterms (fn Free (x, _) =>
   175           if Variable.is_fixed ctxt' x then I else insert (op =) x | _ => I) prop [];
   176       in
   177         Goal.prove ctxt' frees [] prop (K (ALLGOALS
   178           (meta_rewrite_tac ctxt' THEN'
   179             Tactic.rewrite_goal_tac [def] THEN'
   180             Tactic.resolve_tac [Drule.reflexive_thm])))
   181         handle ERROR msg => cat_error msg "Failed to prove definitional specification."
   182       end;
   183   in (((c, T), rhs), prove) end;
   184 
   185 end;