(* Title: HOL/Decision_Procs/Conversions.thy Author: Stefan Berghofer *)
theory Conversions imports Main begin
ML ‹ fun tactic_of_conv cv i st = if i > Thm.nprems_of st then Seq.empty else Seq.single (Conv.gconv_rule cv i st); fun binop_conv cv cv' = Conv.combination_conv (Conv.arg_conv cv) cv'; ›
ML ‹ fun err s ct = error (s ^ ": " ^ Syntax.string_of_term_global (Thm.theory_of_cterm ct) (Thm.term_of ct)); ›
attribute_setup meta = ‹Scan.succeed (Thm.rule_attribute [] (K mk_meta_eq))› ‹convert equality to meta equality›
ML ‹ fun strip_app ct = ct |> Drule.strip_comb |>> Thm.term_of |>> dest_Const_name; fun inst cTs cts th = Thm.instantiate' (map SOME cTs) (map SOME cts) th; fun transitive' eq eq' = Thm.transitive eq (eq' (Thm.rhs_of eq)); fun type_of_eqn eqn = Thm.ctyp_of_cterm (Thm.dest_arg1 (Thm.cprop_of eqn)); fun cong1 conv ct = Thm.combination (Thm.reflexive (Thm.dest_fun ct)) (conv (Thm.dest_arg ct)); fun cong1' conv' conv ct = let val eqn = conv (Thm.dest_arg ct) in Thm.transitive (Thm.combination (Thm.reflexive (Thm.dest_fun ct)) eqn) (conv' (Thm.rhs_of eqn)) end; fun cong2 conv1 conv2 ct = Thm.combination (Thm.combination (Thm.reflexive (Thm.dest_fun2 ct)) (conv1 (Thm.dest_arg1 ct))) (conv2 (Thm.dest_arg ct)); fun cong2' conv conv1 conv2 ct = let val eqn1 = conv1 (Thm.dest_arg1 ct); val eqn2 = conv2 (Thm.dest_arg ct) in Thm.transitive (Thm.combination (Thm.combination (Thm.reflexive (Thm.dest_fun2 ct)) eqn1) eqn2) (conv (Thm.rhs_of eqn1) (Thm.rhs_of eqn2)) end; fun cong2'' conv eqn1 eqn2 = let val eqn3 = conv (Thm.rhs_of eqn1) (Thm.rhs_of eqn2) in Thm.transitive (Thm.combination (Thm.combination (Thm.reflexive (Thm.dest_fun2 (Thm.lhs_of eqn3))) eqn1) eqn2) eqn3 end; fun args1 conv ct = conv (Thm.dest_arg ct); fun args2 conv ct = conv (Thm.dest_arg1 ct) (Thm.dest_arg ct); ›
ML ‹ fun strip_numeral ct = (case strip_app ct of (🍋‹uminus›, [n]) => (case strip_app n of (🍋‹numeral›, [b]) => (🍋‹uminus›, [b]) | _ => ("", [])) | x => x); ›
lemma nat_minus1_eq: "nat (- 1) = 0" by simp
ML ‹ fun nat_conv i = (case strip_app i of (🍋‹zero_class.zero›, []) => @{thm nat_0 [meta]} | (🍋‹one_class.one›, []) => @{thm nat_one_as_int [meta, symmetric]} | (🍋‹numeral›, [b]) => inst [] [b] @{thm nat_numeral [meta]} | (🍋‹uminus›, [b]) => (case strip_app b of (🍋‹one_class.one›, []) => @{thm nat_minus1_eq [meta]} | (🍋‹numeral›, [b']) => inst [] [b'] @{thm nat_neg_numeral [meta]})); ›
ML ‹ fun expand1 a = let val numeral_1_eq_1_a = inst [a] [] @{thm numeral_One [meta, symmetric]} in fn n => case Thm.term_of n of 🍋‹one_class.one _›=> numeral_1_eq_1_a | 🍋‹uminus _ for 🍋‹one_class.one _›\› => Thm.combination (Thm.reflexive (Thm.dest_fun n)) numeral_1_eq_1_a | 🍋‹zero_class.zero _›=> Thm.reflexive n | 🍋‹numeral _ for _›=> Thm.reflexive n | 🍋‹uminus _ for 🍋‹numeral _ for _›\› => Thm.reflexive n | _ => err "expand1" n end; fun norm1_eq a = let val numeral_1_eq_1_a = inst [a] [] @{thm numeral_One [meta]} in fn eq => case Thm.term_of (Thm.rhs_of eq) of 🍋‹Num.numeral _ for 🍋‹Num.One›\› => Thm.transitive eq numeral_1_eq_1_a | 🍋‹uminus _ for 🍋‹Num.numeral _ for 🍋‹Num.One›\›\› => Thm.transitive eq (Thm.combination (Thm.reflexive (Thm.dest_fun (Thm.rhs_of eq))) numeral_1_eq_1_a) | _ => eq end; ›
ML ‹ fun plus_conv f a = let val add_0_a = inst [a] [] @{thm add_0 [meta]}; val add_0_right_a = inst [a] [] @{thm add_0_right [meta]}; val numeral_plus_numeral_a = inst [a] [] @{thm numeral_plus_numeral [meta]}; val expand1_a = expand1 a; fun conv m n = (case (strip_app m, strip_app n) of ((🍋‹zero_class.zero›, []), _) => inst [] [n] add_0_a | (_, (🍋‹zero_class.zero›, [])) => inst [] [m] add_0_right_a | ((🍋‹numeral›, [m]), (🍋‹numeral›, [n])) => transitive' (inst [] [m, n] numeral_plus_numeral_a) (cong1 (args2 add_num_conv)) | _ => cong2'' (f conv) (expand1_a m) (expand1_a n)) in f conv end; val nat_plus_conv = plus_conv I 🍋‹nat›; ›
lemma neg_numeral_plus_neg_numeral: "- Num.numeral m + - Num.numeral n = (- Num.numeral (m + n) ::'a::neg_numeral)" by simp
ML ‹ fun plus_neg_conv a = let val numeral_plus_neg_numeral_a = inst [a] [] @{thm add_neg_numeral_simps(1) [meta]}; val neg_numeral_plus_numeral_a = inst [a] [] @{thm add_neg_numeral_simps(2) [meta]}; val neg_numeral_plus_neg_numeral_a = inst [a] [] @{thm neg_numeral_plus_neg_numeral [meta]}; val sub_conv_a = sub_conv a; in fn conv => fn m => fn n => case (strip_numeral m, strip_numeral n) of ((🍋‹Num.numeral›, [m]), (🍋‹uminus›, [n])) => Thm.transitive (inst [] [m, n] numeral_plus_neg_numeral_a) (sub_conv_a m n) | ((🍋‹uminus›, [m]), (🍋‹Num.numeral›, [n])) => Thm.transitive (inst [] [m, n] neg_numeral_plus_numeral_a) (sub_conv_a n m) | ((🍋‹uminus›, [m]), (🍋‹uminus›, [n])) => transitive' (inst [] [m, n] neg_numeral_plus_neg_numeral_a) (cong1 (cong1 (args2 add_num_conv))) | _ => conv m n end; fun plus_conv' a = norm1_eq a oo plus_conv (plus_neg_conv a) a; val int_plus_conv = plus_conv' 🍋‹int›; ›
ML ‹ fun le_conv f a = let val zero_le_zero_a = inst [a] [] @{thm order_refl [of 0, THEN Eq_TrueI]}; val zero_le_numeral_a = inst [a] [] @{thm zero_le_numeral [THEN Eq_TrueI]}; val not_numeral_le_zero_a = inst [a] [] @{thm not_numeral_le_zero [THEN Eq_FalseI]}; val numeral_le_iff_a = inst [a] [] @{thm numeral_le_iff [meta]}; val expand1_a = expand1 a; fun conv m n = (case (strip_app m, strip_app n) of ((🍋‹zero_class.zero›, []), (🍋‹zero_class.zero›, [])) => zero_le_zero_a | ((🍋‹zero_class.zero›, []), (🍋‹numeral›, [n])) => inst [] [n] zero_le_numeral_a | ((🍋‹numeral›, [m]), (🍋‹zero_class.zero›, [])) => inst [] [m] not_numeral_le_zero_a | ((🍋‹numeral›, [m]), (🍋‹numeral›, [n])) => Thm.transitive (inst [] [m, n] numeral_le_iff_a) (le_num_conv m n) | _ => cong2'' (f conv) (expand1_a m) (expand1_a n)) in f conv end; val nat_le_conv = le_conv I 🍋‹nat›; ›
ML ‹ fun le_neg_conv a = let val neg_numeral_le_zero_a = inst [a] [] @{thm neg_numeral_le_zero [THEN Eq_TrueI]}; val not_zero_le_neg_numeral_a = inst [a] [] @{thm not_zero_le_neg_numeral [THEN Eq_FalseI]}; val neg_numeral_le_numeral_a = inst [a] [] @{thm neg_numeral_le_numeral [THEN Eq_TrueI]}; val not_numeral_le_neg_numeral_a = inst [a] [] @{thm not_numeral_le_neg_numeral [THEN Eq_FalseI]}; val neg_numeral_le_iff_a = inst [a] [] @{thm neg_numeral_le_iff [meta]} in fn conv => fn m => fn n => case (strip_numeral m, strip_numeral n) of ((🍋‹uminus›, [m]), (🍋‹zero_class.zero›, [])) => inst [] [m] neg_numeral_le_zero_a | ((🍋‹zero_class.zero›, []), (🍋‹uminus›, [n])) => inst [] [n] not_zero_le_neg_numeral_a | ((🍋‹Num.numeral›, [m]), (🍋‹uminus›, [n])) => inst [] [m, n] not_numeral_le_neg_numeral_a | ((🍋‹uminus›, [m]), (🍋‹Num.numeral›, [n])) => inst [] [m, n] neg_numeral_le_numeral_a | ((🍋‹uminus›, [m]), (🍋‹uminus›, [n])) => Thm.transitive (inst [] [m, n] neg_numeral_le_iff_a) (le_num_conv n m) | _ => conv m n end; fun le_conv' a = le_conv (le_neg_conv a) a; val int_le_conv = le_conv' 🍋‹int›; ›
ML ‹ fun less_conv f a = let val not_zero_less_zero_a = inst [a] [] @{thm less_irrefl [of 0, THEN Eq_FalseI]}; val zero_less_numeral_a = inst [a] [] @{thm zero_less_numeral [THEN Eq_TrueI]}; val not_numeral_less_zero_a = inst [a] [] @{thm not_numeral_less_zero [THEN Eq_FalseI]}; val numeral_less_iff_a = inst [a] [] @{thm numeral_less_iff [meta]}; val expand1_a = expand1 a; fun conv m n = (case (strip_app m, strip_app n) of ((🍋‹zero_class.zero›, []), (🍋‹zero_class.zero›, [])) => not_zero_less_zero_a | ((🍋‹zero_class.zero›, []), (🍋‹numeral›, [n])) => inst [] [n] zero_less_numeral_a | ((🍋‹numeral›, [m]), (🍋‹zero_class.zero›, [])) => inst [] [m] not_numeral_less_zero_a | ((🍋‹numeral›, [m]), (🍋‹numeral›, [n])) => Thm.transitive (inst [] [m, n] numeral_less_iff_a) (less_num_conv m n) | _ => cong2'' (f conv) (expand1_a m) (expand1_a n)) in f conv end; val nat_less_conv = less_conv I 🍋‹nat›; ›
ML ‹ fun less_neg_conv a = let val neg_numeral_less_zero_a = inst [a] [] @{thm neg_numeral_less_zero [THEN Eq_TrueI]}; val not_zero_less_neg_numeral_a = inst [a] [] @{thm not_zero_less_neg_numeral [THEN Eq_FalseI]}; val neg_numeral_less_numeral_a = inst [a] [] @{thm neg_numeral_less_numeral [THEN Eq_TrueI]}; val not_numeral_less_neg_numeral_a = inst [a] [] @{thm not_numeral_less_neg_numeral [THEN Eq_FalseI]}; val neg_numeral_less_iff_a = inst [a] [] @{thm neg_numeral_less_iff [meta]} in fn conv => fn m => fn n => case (strip_numeral m, strip_numeral n) of ((🍋‹uminus›, [m]), (🍋‹zero_class.zero›, [])) => inst [] [m] neg_numeral_less_zero_a | ((🍋‹zero_class.zero›, []), (🍋‹uminus›, [n])) => inst [] [n] not_zero_less_neg_numeral_a | ((🍋‹Num.numeral›, [m]), (🍋‹uminus›, [n])) => inst [] [m, n] not_numeral_less_neg_numeral_a | ((🍋‹uminus›, [m]), (🍋‹Num.numeral›, [n])) => inst [] [m, n] neg_numeral_less_numeral_a | ((🍋‹uminus›, [m]), (🍋‹uminus›, [n])) => Thm.transitive (inst [] [m, n] neg_numeral_less_iff_a) (less_num_conv n m) | _ => conv m n end; fun less_conv' a = less_conv (less_neg_conv a) a; val int_less_conv = less_conv' 🍋‹int›; ›
ML ‹ fun If_conv a = let val if_True = inst [a] [] @{thm if_True [meta]}; val if_False = inst [a] [] @{thm if_False [meta]} in fn p => fn x => fn y => fn ct => case strip_app ct of (🍋‹If›, [cb, cx, cy]) => let val p_eq = p cb val eq = Thm.combination (Thm.reflexive (Thm.dest_fun (Thm.dest_fun2 ct))) p_eq in case Thm.term_of (Thm.rhs_of p_eq) of 🍋‹True›=> let val x_eq = x cx; val cx = Thm.rhs_of x_eq; in Thm.transitive (Thm.combination (Thm.combination eq x_eq) (Thm.reflexive cy)) (inst [] [cx, cy] if_True) end | 🍋‹False›=> let val y_eq = y cy; val cy = Thm.rhs_of y_eq; in Thm.transitive (Thm.combination (Thm.combination eq (Thm.reflexive cx)) y_eq) (inst [] [cx, cy] if_False) end | _ => err "If_conv" (Thm.rhs_of p_eq) end end; ›
ML ‹ fun drop_conv a = let val drop_0_a = inst [a] [] @{thm drop_0 [meta]}; val drop_Cons_a = inst [a] [] @{thm drop_Cons' [meta]}; val If_conv_a = If_conv (type_of_eqn drop_0_a); fun conv n ys = (case Thm.term_of n of 🍋‹zero_class.zero _›=> inst [] [ys] drop_0_a | _ => (case strip_app ys of (🍋‹Cons›, [x, xs]) => transitive' (inst [] [n, x, xs] drop_Cons_a) (If_conv_a (args2 nat_eq_conv) Thm.reflexive (cong2' conv (args2 nat_minus_conv) Thm.reflexive)))) in conv end; ›
ML ‹ fun nth_conv a = let val nth_Cons_a = inst [a] [] @{thm nth_Cons' [meta]}; val If_conv_a = If_conv a; fun conv ys n = (case strip_app ys of (🍋‹Cons›, [x, xs]) => transitive' (inst [] [x, xs, n] nth_Cons_a) (If_conv_a (args2 nat_eq_conv) Thm.reflexive (cong2' conv Thm.reflexive (args2 nat_minus_conv)))) in conv end; ›
end
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.26 Sekunden
(vorverarbeitet am 2026-04-28)
¤
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 und die Messung sind noch experimentell.