(* Title: Pure/simplifier.ML Author: Tobias Nipkow and Markus Wenzel, TU Muenchen
Generic simplifier, suitable for most logics (see also raw_simplifier.ML for the actual meta-level rewriting engine).
*)
signature BASIC_SIMPLIFIER = sig val simp_tac: Proof.context -> int -> tactic val asm_simp_tac: Proof.context -> int -> tactic val full_simp_tac: Proof.context -> int -> tactic val asm_lr_simp_tac: Proof.context -> int -> tactic val asm_full_simp_tac: Proof.context -> int -> tactic val safe_simp_tac: Proof.context -> int -> tactic val safe_asm_simp_tac: Proof.context -> int -> tactic val safe_full_simp_tac: Proof.context -> int -> tactic val safe_asm_lr_simp_tac: Proof.context -> int -> tactic val safe_asm_full_simp_tac: Proof.context -> int -> tactic val simplify: Proof.context -> thm -> thm val asm_simplify: Proof.context -> thm -> thm val full_simplify: Proof.context -> thm -> thm val asm_lr_simplify: Proof.context -> thm -> thm val asm_full_simplify: Proof.context -> thm -> thm end;
signature SIMPLIFIER = sig
include BASIC_SIMPLIFIER
include RAW_SIMPLIFIER val attrib: (thm -> Proof.context -> Proof.context) -> attribute val simp_add: attribute val simp_del: attribute val simp_flip: attribute val cong_add: attribute val cong_del: attribute val check_simproc: Proof.context -> xstring * Position.T -> string * simproc val the_simproc: Proof.context -> string -> simproc val make_simproc: Proof.context ->
{name: string, kind: proc_kind, lhss: term list, proc: morphism -> proc, identifier: thm list} ->
simproc type ('a, 'b, 'c) simproc_spec =
{passive: bool, name: binding, kind: proc_kind, lhss: 'a list, proc: 'b, identifier: 'c} val read_simproc_spec: Proof.context ->
(string, 'b, 'c) simproc_spec -> (term, 'b, 'c) simproc_spec val define_simproc: (term, morphism -> proc, thm list) simproc_spec -> local_theory ->
simproc * local_theory val simproc_setup: (term, morphism -> proc, thm list) simproc_spec -> simproc val simproc_setup_cmd: (string, morphism -> proc, thm list) simproc_spec -> simproc val simproc_setup_command: (local_theory -> local_theory) parser val pretty_simpset: bool -> Proof.context -> Pretty.T val set_mksimps: (Proof.context -> thm -> thm list) -> Proof.context -> Proof.context val rewrite: Proof.context -> conv val asm_rewrite: Proof.context -> conv val full_rewrite: Proof.context -> conv val asm_lr_rewrite: Proof.context -> conv val asm_full_rewrite: Proof.context -> conv val cong_modifiers: Method.modifier parser list val simp_modifiers': Method.modifier parser list val simp_modifiers: Method.modifier parser list val method_setup: Method.modifier parser list -> theory -> theory val unsafe_solver_tac: Proof.context -> int -> tactic val unsafe_solver: solver val safe_solver_tac: Proof.context -> int -> tactic val safe_solver: solver end;
fun attrib f = Thm.declaration_attribute (map_ss o f);
val simp_add = attrib add_simp; val simp_del = attrib del_simp; val simp_flip = attrib flip_simp; val cong_add = attrib add_cong; val cong_del = attrib del_cong;
(** named simprocs **)
structure Simprocs = Generic_Data
( type T = simproc Name_Space.table; val empty : T = Name_Space.empty_table "simproc"; fun merge data : T = Name_Space.merge_tables data;
);
(* get simprocs *)
val get_simprocs = Simprocs.get o Context.Proof;
val the_simproc = Name_Space.get o get_simprocs; fun check_simproc ctxt = Name_Space.check (Context.Proof ctxt) (get_simprocs ctxt);
val _ = Theory.setup
(ML_Context.add_antiquotation_embedded \<^binding>\<open>simproc_setup\<close>
(fn _ => fn input => fn ctxt => let val ml = ML_Lex.tokenize_no_range;
val (decl1, ctxt1) = ML_Context.read_antiquotes proc ctxt; val (decl2, ctxt2) =
(case identifier of
NONE => (K ("", "[]"), ctxt1)
| SOME (_, thms) => ML_Thms.thm_binding "thms"false (Attrib.eval_thms ctxt1 thms) ctxt1);
fun decl' ctxt' = let val (ml_env1, ml_body1) = decl1 ctxt'; val (ml_env2, ml_body2) = decl2 ctxt' |> apply2 ml; val ml_body' =
ml "Simplifier.simproc_setup {passive = " @ ml (Bool.toString passive) @
ml ", name = " @ ml (ML_Syntax.make_binding (Binding.name_of name, Binding.pos_of name)) @
ml ", kind = " @ ml (print_proc_kind kind) @
ml ", lhss = " @ ml (ML_Syntax.print_list ML_Syntax.print_term lhss) @
ml ", proc = (" @ ml_body1 @ ml ")" @
ml ", identifier = (" @ ml_body2 @ ml ")}"; in (ml_env1 @ ml_env2, ml_body') end; in (decl', ctxt2) end));
(** congruence rule to protect foundational terms of local definitions **)
local
fun add_foundation_cong (binding, (const, target_params)) gthy = if null target_params then gthy else let val thy = Context.theory_of gthy; val cong =
list_comb (const, target_params)
|> Logic.varify_global
|> Thm.global_cterm_of thy
|> Thm.reflexive
|> Thm.close_derivation \<^here>; val cong_binding = Binding.qualify_name true binding "cong"; in
gthy
|> Attrib.generic_notes Thm.theoremK [((cong_binding, []), [([cong], [])])]
|> #2 end;
val _ = Theory.setup (Generic_Target.add_foundation_interpretation add_foundation_cong);
inend;
(** pretty_simpset **)
fun pretty_simpset verbose ctxt = let val pretty_term = Syntax.pretty_term ctxt; val pretty_thm = Thm.pretty_thm ctxt; val pretty_thm_item = Thm.pretty_thm_item ctxt;
fun pretty_simproc (name, lhss) =
Pretty.block
(Pretty.mark_str name :: Pretty.str ":" :: Pretty.fbrk ::
Pretty.fbreaks (map (Pretty.item o single o pretty_term) lhss));
fun pretty_congproc (name, {lhss, proc}) = let val prt_rule =
(casetry (Morphism.form_context' ctxt proc) @{cterm dummy} of
SOME (SOME thm) => [Pretty.fbrk, Pretty.str "rule:", Pretty.fbrk, pretty_thm thm]
| NONE => []); in
Pretty.block
(Pretty.mark_str name :: Pretty.str ":" :: Pretty.fbrk ::
Pretty.fbreaks (map (Pretty.item o single o pretty_term) lhss) @ prt_rule) end;
fun solve_all_tac solvers ctxt = let val subgoal_tac = subgoal_tac (set_solvers solvers ctxt); val solve_tac = subgoal_tac THEN_ALL_NEW (K no_tac); in DEPTH_SOLVE (solve_tac 1) end;
(*NOTE: may instantiate unknowns that appear also in other subgoals*) fun generic_simp_tac safe mode ctxt = let val loop_tac = loop_tac ctxt; val (unsafe_solvers, solvers) = solvers ctxt; val solve_tac = FIRST' (map (solver ctxt)
(rev (if safe then solvers else unsafe_solvers)));
fun simp_loop_tac i =
generic_rewrite_goal_tac mode (solve_all_tac unsafe_solvers) ctxt i THEN
(solve_tac i ORELSE TRY ((loop_tac THEN_ALL_NEW simp_loop_tac) i)); in PREFER_GOAL (simp_loop_tac 1) end;
local
fun simp rew mode ctxt thm = let val (unsafe_solvers, _) = solvers ctxt; val tacf = solve_all_tac (rev unsafe_solvers); fun prover s th = Option.map #1 (Seq.pull (tacf s th)); in rew mode prover ctxt thm end;
in
val simp_thm = simp rewrite_thm; val simp_cterm = simp rewrite_cterm;
end;
(* tactics *)
val simp_tac = generic_simp_tac false (false, false, false); val asm_simp_tac = generic_simp_tac false (false, true, false); val full_simp_tac = generic_simp_tac false (true, false, false); val asm_lr_simp_tac = generic_simp_tac false (true, true, false); val asm_full_simp_tac = generic_simp_tac false (true, true, true);
(*not totally safe: may instantiate unknowns that appear also in other subgoals*) val safe_simp_tac = generic_simp_tac true (false, false, false); val safe_asm_simp_tac = generic_simp_tac true (false, true, false); val safe_full_simp_tac = generic_simp_tac true (true, false, false); val safe_asm_lr_simp_tac = generic_simp_tac true (true, true, false); val safe_asm_full_simp_tac = generic_simp_tac true (true, true, true);
(* conversions *)
val simplify = simp_thm (false, false, false); val asm_simplify = simp_thm (false, true, false); val full_simplify = simp_thm (true, false, false); val asm_lr_simplify = simp_thm (true, true, false); val asm_full_simplify = simp_thm (true, true, true);
val rewrite = simp_cterm (false, false, false); val asm_rewrite = simp_cterm (false, true, false); val full_rewrite = simp_cterm (true, false, false); val asm_lr_rewrite = simp_cterm (true, true, false); val asm_full_rewrite = simp_cterm (true, true, true);
(** concrete syntax of attributes **)
(* add / del *)
val simpN = "simp"; val flipN = "flip" val congN = "cong"; val onlyN = "only"; val no_asmN = "no_asm"; val no_asm_useN = "no_asm_use"; val no_asm_simpN = "no_asm_simp"; val asm_lrN = "asm_lr";
(* simprocs *)
local
val add_del =
(Args.del -- Args.colon >> K del_proc ||
Scan.option (Args.add -- Args.colon) >> K add_proc)
>> (fn f => fn simproc => Morphism.entity (fn phi => Thm.declaration_attribute
(K (map_ss (f (transform_simproc phi simproc))))));
in
val simproc_att =
(Args.context -- Scan.lift add_del) :|-- (fn (ctxt, decl) =>
Scan.repeat1 (Scan.lift (Args.named_attribute (decl o #2 o check_simproc ctxt))))
>> (fn atts => Thm.declaration_attribute (fn th =>
fold (fn att => Thm.attribute_declaration (Morphism.form att) th) atts));
end;
(* conversions *)
local
fun conv_mode x =
((Args.parens (Args.$$$ no_asmN) >> K simplify ||
Args.parens (Args.$$$ no_asm_simpN) >> K asm_simplify ||
Args.parens (Args.$$$ no_asm_useN) >> K full_simplify ||
Scan.succeed asm_full_simplify) |> Scan.lift) x;
in
val simplified = conv_mode -- Attrib.thms >>
(fn (f, ths) => Thm.rule_attribute ths (fn context =>
f (Context.proof_of context |>
(if null ths then I else clear_simpset #> add_simps ths))));
end;
(* setup attributes *)
val cong_format = Scan.succeed (Thm.rule_attribute [] (Context.proof_of #> mk_cong));
val _ = Theory.setup
(Attrib.setup \<^binding>\<open>simp\<close> (Attrib.add_del simp_add simp_del) "declaration of Simplifier rewrite rule" #>
Attrib.setup \<^binding>\<open>cong\<close> (Attrib.add_del cong_add cong_del) "declaration of Simplifier congruence rule" #>
Attrib.setup \<^binding>\<open>simproc\<close> simproc_att "declaration of simplification procedures" #>
Attrib.setup \<^binding>\<open>simplified\<close> simplified "simplified rule" #>
Attrib.setup \<^binding>\<open>cong_format\<close> cong_format "internal format of Simplifier cong rule");
(** method syntax **)
val cong_modifiers =
[Args.$$$ congN -- Args.colon >> K (Method.modifier cong_add \<^here>),
Args.$$$ congN -- Args.add -- Args.colon >> K (Method.modifier cong_add \<^here>),
Args.$$$ congN -- Args.del -- Args.colon >> K (Method.modifier cong_del \<^here>)];
structure Basic_Simplifier: BASIC_SIMPLIFIER = Simplifier; open Basic_Simplifier;
¤ Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.0.25Bemerkung:
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.