src/Pure/Isar/local_defs.ML
author wenzelm
Sun Jan 29 19:23:47 2006 +0100 (2006-01-29 ago)
changeset 18840 ce16e2bad548
parent 18830 34b51dcdc570
child 18859 75248f8badc9
permissions -rw-r--r--
added attributes defn_add/del;
added (un)fold operations (from object_logic.ML);
tuned;
     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 mk_def: ProofContext.context -> (string * term) list -> term list
    11   val cert_def: ProofContext.context -> term -> string * term
    12   val abs_def: term -> (string * typ) * term
    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 unfold: ProofContext.context -> bool -> thm list -> thm -> thm
    20   val unfold_goals: ProofContext.context -> bool -> thm list -> thm -> thm
    21   val unfold_tac: ProofContext.context -> bool -> thm list -> tactic
    22   val fold: ProofContext.context -> bool -> thm list -> thm -> thm
    23   val fold_tac: ProofContext.context -> bool -> thm list -> tactic
    24   val derived_def: ProofContext.context -> term ->
    25     ((string * typ) * term) * (ProofContext.context -> term -> thm -> thm)
    26 end;
    27 
    28 structure LocalDefs: LOCAL_DEFS =
    29 struct
    30 
    31 (** primitive definitions **)
    32 
    33 (* prepare defs *)
    34 
    35 fun mk_def ctxt args =
    36   let
    37     val (xs, rhss) = split_list args;
    38     val (bind, _) = ProofContext.bind_fixes xs ctxt;
    39     val lhss = map (fn (x, rhs) => bind (Free (x, Term.fastype_of rhs))) args;
    40   in map Logic.mk_equals (lhss ~~ rhss) end;
    41 
    42 fun cert_def ctxt eq =
    43   let
    44     fun err msg = cat_error msg
    45       ("The error(s) above occurred in definition: " ^ ProofContext.string_of_term ctxt eq);
    46 
    47     val (lhs, rhs) = Logic.dest_equals (Term.strip_all_body eq)
    48       handle TERM _ => err "Not a meta-equality (==)";
    49     val (f, xs) = Term.strip_comb (Pattern.beta_eta_contract lhs);
    50     val (c, _) = Term.dest_Free f handle TERM _ =>
    51       err "Head of lhs must be a free/fixed variable";
    52 
    53     fun check_arg (Bound _) = true
    54       | check_arg (Free (x, _)) = not (ProofContext.is_fixed ctxt x)
    55       | check_arg t = (case try Logic.dest_type t of SOME (TFree _) => true | _ => false);
    56     fun close_arg (Free (x, T)) t = Term.all T $ Term.absfree (x, T, t)
    57       | close_arg (Const ("TYPE", T)) t = Term.all T $ Term.absdummy (T, t)
    58       | close_arg _ t = t;
    59 
    60     val extra_frees = Term.fold_aterms (fn v as Free (x, _) =>
    61       if ProofContext.is_fixed ctxt x orelse member (op aconv) xs v then I
    62       else insert (op =) x | _ => I) rhs [];
    63   in
    64     if not (forall check_arg xs) orelse has_duplicates (op =) xs then
    65       err "Arguments of lhs must be distinct free/bound variables"
    66     else if not (null extra_frees) then
    67       err ("Extra free variables on rhs: " ^ commas_quote extra_frees)
    68     else if Term.exists_subterm (fn t => t = f) rhs then
    69       err "Element to be defined occurs on rhs"
    70     else (c, fold_rev close_arg xs eq)
    71   end;
    72 
    73 fun abs_def eq =
    74   let
    75     val body = Term.strip_all_body eq;
    76     val vars = map Free (Term.rename_wrt_term body (Term.strip_all_vars eq));
    77     val (lhs, rhs) = Logic.dest_equals (Term.subst_bounds (vars, body));
    78     val (f, xs) = Term.strip_comb (Pattern.beta_eta_contract lhs);
    79     val eq' = Term.list_abs_free (map Term.dest_Free xs, rhs);
    80   in (Term.dest_Free f, eq') end;
    81 
    82 
    83 (* export defs *)
    84 
    85 fun head_of_def cprop =
    86   #1 (Term.strip_comb (#1 (Logic.dest_equals (Term.strip_all_body (Thm.term_of cprop)))))
    87   |> Thm.cterm_of (Thm.theory_of_cterm cprop);
    88 
    89 fun def_export _ cprops thm =
    90   thm
    91   |> Drule.implies_intr_list cprops
    92   |> Drule.forall_intr_list (map head_of_def cprops)
    93   |> Drule.forall_elim_vars 0
    94   |> RANGE (replicate (length cprops) (Tactic.rtac Drule.reflexive_thm)) 1;
    95 
    96 
    97 (* add defs *)
    98 
    99 fun add_def (x, t) ctxt =
   100   let
   101     val [eq] = mk_def ctxt [(x, t)];
   102     val x' = Term.dest_Free (fst (Logic.dest_equals eq));
   103   in
   104     ctxt
   105     |> ProofContext.add_fixes_i [(x, NONE, NoSyn)] |> snd
   106     |> ProofContext.add_assms_i def_export [(("", []), [(eq, ([], []))])]
   107     |>> (fn [(_, [th])] => (x', th))
   108   end;
   109 
   110 
   111 
   112 (** defived definitions **)
   113 
   114 (* transformation rules *)
   115 
   116 structure Data = GenericDataFun
   117 (
   118   val name = "Pure/derived_defs";
   119   type T = thm list;
   120   val empty = []
   121   val extend = I;
   122   fun merge _ = Drule.merge_rules;
   123   fun print context rules =
   124     Pretty.writeln (Pretty.big_list "definitional transformations:"
   125       (map (ProofContext.pretty_thm (Context.proof_of context)) rules));
   126 );
   127 
   128 val _ = Context.add_setup Data.init;
   129 
   130 val print_rules = Data.print;
   131 val get_rules = Data.get o Context.Proof;
   132 
   133 val defn_add = Thm.declaration_attribute (Data.map o Drule.add_rule);
   134 val defn_del = Thm.declaration_attribute (Data.map o Drule.del_rule);
   135 
   136 
   137 (* transform rewrite rules *)
   138 
   139 val equals_ss =
   140   MetaSimplifier.theory_context ProtoPure.thy MetaSimplifier.empty_ss
   141     addeqcongs [Drule.equals_cong];    (*protect meta-level equality*)
   142 
   143 fun meta_rewrite ctxt =
   144   MetaSimplifier.rewrite_cterm (false, false, false) (K (K NONE))
   145     (equals_ss addsimps (get_rules ctxt));
   146 
   147 val meta_rewrite_thm = Drule.fconv_rule o meta_rewrite;
   148 
   149 fun meta_rewrite_tac ctxt i =
   150   PRIMITIVE (Drule.fconv_rule (Drule.goals_conv (equal i) (meta_rewrite ctxt)));
   151 
   152 fun transformed f _ false = f
   153   | transformed f ctxt true = f o map (meta_rewrite_thm ctxt);
   154 
   155 val unfold       = transformed Tactic.rewrite_rule;
   156 val unfold_goals = transformed Tactic.rewrite_goals_rule;
   157 val unfold_tac   = transformed Tactic.rewrite_goals_tac;
   158 val fold         = transformed Tactic.fold_rule;
   159 val fold_tac     = transformed Tactic.fold_goals_tac;
   160 
   161 
   162 (* derived defs -- potentially within the object-logic *)
   163 
   164 fun derived_def ctxt prop =
   165   let
   166     val thy = ProofContext.theory_of ctxt;
   167     val ((c, T), rhs) = prop
   168       |> Thm.cterm_of thy
   169       |> meta_rewrite ctxt
   170       |> (snd o Logic.dest_equals o Thm.prop_of)
   171       |> Logic.strip_imp_concl
   172       |> (snd o cert_def ctxt)
   173       |> abs_def;
   174     fun prove ctxt' t def =
   175       let
   176         val thy' = ProofContext.theory_of ctxt';
   177         val prop' = Term.subst_atomic [(Free (c, T), t)] prop;
   178         val frees = Term.fold_aterms (fn Free (x, _) =>
   179           if ProofContext.is_fixed ctxt' x then I else insert (op =) x | _ => I) prop' [];
   180       in
   181         Goal.prove thy' frees [] prop' (K (ALLGOALS
   182           (meta_rewrite_tac ctxt' THEN'
   183             Tactic.rewrite_goal_tac [def] THEN'
   184             Tactic.resolve_tac [Drule.reflexive_thm])))
   185         handle ERROR msg => cat_error msg "Failed to prove definitional specification."
   186       end;
   187   in (((c, T), rhs), prove) end;
   188 
   189 
   190 end;