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