src/Pure/Isar/local_defs.ML
author wenzelm
Wed Oct 11 22:55:17 2006 +0200 (2006-10-11)
changeset 20980 e4fd72aecd03
parent 20909 7132ab2b4621
child 21506 b2a673894ce5
permissions -rw-r--r--
add_defs: declare terms;
     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 find_def: Proof.context -> string -> thm option
    15   val add_defs: ((string * mixfix) * ((bstring * attribute list) * term)) list -> Proof.context ->
    16     (term * (bstring * thm)) list * Proof.context
    17   val print_rules: Context.generic -> unit
    18   val defn_add: attribute
    19   val defn_del: attribute
    20   val meta_rewrite_rule: Context.generic -> 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 (* find def *)
    79 
    80 fun find_def ctxt x =
    81   Assumption.prems_of ctxt |> find_first (fn th =>
    82     (case try (head_of_def o Thm.prop_of) th of
    83       SOME y => x = y
    84     | NONE => false));
    85 
    86 
    87 (* add defs *)
    88 
    89 fun add_defs defs ctxt =
    90   let
    91     val ((xs, mxs), specs) = defs |> split_list |>> split_list;
    92     val ((names, atts), rhss) = specs |> split_list |>> split_list;
    93     val names' = map2 Thm.def_name_optional xs names;
    94     val eqs = mk_def ctxt (xs ~~ rhss);
    95     val lhss = map (fst o Logic.dest_equals) eqs;
    96   in
    97     ctxt
    98     |> ProofContext.add_fixes_i (map2 (fn x => fn mx => (x, NONE, mx)) xs mxs) |> #2
    99     |> fold Variable.declare_term eqs
   100     |> ProofContext.add_assms_i def_export
   101       (map2 (fn a => fn eq => (a, [(eq, [])])) (names' ~~ atts) eqs)
   102     |>> map2 (fn lhs => fn (name, [th]) => (lhs, (name, th))) lhss
   103   end;
   104 
   105 
   106 
   107 (** defived definitions **)
   108 
   109 (* transformation rules *)
   110 
   111 structure Rules = GenericDataFun
   112 (
   113   val name = "Pure/derived_defs";
   114   type T = thm list;
   115   val empty = []
   116   val extend = I;
   117   fun merge _ = Drule.merge_rules;
   118   fun print context rules =
   119     Pretty.writeln (Pretty.big_list "definitional transformations:"
   120       (map (ProofContext.pretty_thm (Context.proof_of context)) rules));
   121 );
   122 
   123 val _ = Context.add_setup Rules.init;
   124 
   125 val print_rules = Rules.print;
   126 
   127 val defn_add = Thm.declaration_attribute (Rules.map o Drule.add_rule);
   128 val defn_del = Thm.declaration_attribute (Rules.map o Drule.del_rule);
   129 
   130 
   131 (* meta rewrite rules *)
   132 
   133 val equals_ss =
   134   MetaSimplifier.theory_context ProtoPure.thy MetaSimplifier.empty_ss
   135     addeqcongs [Drule.equals_cong];    (*protect meta-level equality*)
   136 
   137 fun meta_rewrite context =
   138   MetaSimplifier.rewrite_cterm (false, false, false) (K (K NONE))
   139     (equals_ss addsimps (Rules.get context));
   140 
   141 val meta_rewrite_rule = Drule.fconv_rule o meta_rewrite;
   142 
   143 fun meta_rewrite_tac ctxt i =
   144   PRIMITIVE (Drule.fconv_rule (Drule.goals_conv (equal i) (meta_rewrite (Context.Proof ctxt))));
   145 
   146 
   147 (* rewriting with object-level rules *)
   148 
   149 fun meta f ctxt = f o map (meta_rewrite_rule (Context.Proof ctxt));
   150 
   151 val unfold       = meta Tactic.rewrite_rule;
   152 val unfold_goals = meta Tactic.rewrite_goals_rule;
   153 val unfold_tac   = meta Tactic.rewrite_goals_tac;
   154 val fold         = meta Tactic.fold_rule;
   155 val fold_tac     = meta Tactic.fold_goals_tac;
   156 
   157 
   158 (* derived defs -- potentially within the object-logic *)
   159 
   160 fun derived_def ctxt conditional prop =
   161   let
   162     val ((c, T), rhs) = prop
   163       |> Thm.cterm_of (ProofContext.theory_of ctxt)
   164       |> meta_rewrite (Context.Proof ctxt)
   165       |> (snd o Logic.dest_equals o Thm.prop_of)
   166       |> K conditional ? Logic.strip_imp_concl
   167       |> (abs_def o #2 o cert_def ctxt);
   168     fun prove ctxt' def =
   169       let
   170         val frees = Term.fold_aterms (fn Free (x, _) =>
   171           if Variable.is_fixed ctxt' x then I else insert (op =) x | _ => I) prop [];
   172       in
   173         Goal.prove ctxt' frees [] prop (K (ALLGOALS
   174           (meta_rewrite_tac ctxt' THEN'
   175             Tactic.rewrite_goal_tac [def] THEN'
   176             Tactic.resolve_tac [Drule.reflexive_thm])))
   177         handle ERROR msg => cat_error msg "Failed to prove definitional specification."
   178       end;
   179   in (((c, T), rhs), prove) end;
   180 
   181 end;