| author | wenzelm | 
| Fri, 01 Nov 2024 17:13:42 +0100 | |
| changeset 81302 | 07e1e978b093 | 
| parent 80722 | b7d051e25d9d | 
| child 82641 | d22294b20573 | 
| permissions | -rw-r--r-- | 
| 30496 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 haftmann parents: 
29302diff
changeset | 1 | (* Author: Markus Wenzel, Stefan Berghofer, and Tobias Nipkow | 
| 48560 
e0875d956a6b
replace Nat_Arith simprocs with simpler conversions that do less rearrangement of terms
 huffman parents: 
48559diff
changeset | 2 | Author: Brian Huffman | 
| 9436 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 3 | |
| 30496 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 haftmann parents: 
29302diff
changeset | 4 | Basic arithmetic for natural numbers. | 
| 9436 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 5 | *) | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 6 | |
| 30496 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 haftmann parents: 
29302diff
changeset | 7 | signature NAT_ARITH = | 
| 26101 | 8 | sig | 
| 54742 
7a86358a3c0b
proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
 wenzelm parents: 
48571diff
changeset | 9 | val cancel_diff_conv: Proof.context -> conv | 
| 
7a86358a3c0b
proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
 wenzelm parents: 
48571diff
changeset | 10 | val cancel_eq_conv: Proof.context -> conv | 
| 
7a86358a3c0b
proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
 wenzelm parents: 
48571diff
changeset | 11 | val cancel_le_conv: Proof.context -> conv | 
| 
7a86358a3c0b
proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
 wenzelm parents: 
48571diff
changeset | 12 | val cancel_less_conv: Proof.context -> conv | 
| 26101 | 13 | end; | 
| 9436 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 14 | |
| 30496 
7cdcc9dd95cb
vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
 haftmann parents: 
29302diff
changeset | 15 | structure Nat_Arith: NAT_ARITH = | 
| 9436 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 16 | struct | 
| 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 17 | |
| 48560 
e0875d956a6b
replace Nat_Arith simprocs with simpler conversions that do less rearrangement of terms
 huffman parents: 
48559diff
changeset | 18 | val norm_rules = map mk_meta_eq @{thms add_0_left add_0_right}
 | 
| 9436 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 19 | |
| 54742 
7a86358a3c0b
proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
 wenzelm parents: 
48571diff
changeset | 20 | fun move_to_front ctxt path = Conv.every_conv | 
| 70490 | 21 |     [Conv.rewr_conv (Library.foldl (op RS) (@{thm nat_arith.rule0}, path)),
 | 
| 54742 
7a86358a3c0b
proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
 wenzelm parents: 
48571diff
changeset | 22 | Conv.arg_conv (Raw_Simplifier.rewrite ctxt false norm_rules)] | 
| 9436 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 23 | |
| 80722 | 24 | fun add_atoms path \<^Const_>\<open>plus _ for x y\<close> = | 
| 70490 | 25 |       add_atoms (@{thm nat_arith.add1}::path) x #>
 | 
| 26 |       add_atoms (@{thm nat_arith.add2}::path) y
 | |
| 80722 | 27 | | add_atoms path \<^Const_>\<open>Suc for x\<close> = | 
| 70490 | 28 |       add_atoms (@{thm nat_arith.suc1}::path) x
 | 
| 80722 | 29 | | add_atoms _ \<^Const_>\<open>Groups.zero _\<close> = I | 
| 48560 
e0875d956a6b
replace Nat_Arith simprocs with simpler conversions that do less rearrangement of terms
 huffman parents: 
48559diff
changeset | 30 | | add_atoms path x = cons (x, path) | 
| 
e0875d956a6b
replace Nat_Arith simprocs with simpler conversions that do less rearrangement of terms
 huffman parents: 
48559diff
changeset | 31 | |
| 
e0875d956a6b
replace Nat_Arith simprocs with simpler conversions that do less rearrangement of terms
 huffman parents: 
48559diff
changeset | 32 | fun atoms t = add_atoms [] t [] | 
| 
e0875d956a6b
replace Nat_Arith simprocs with simpler conversions that do less rearrangement of terms
 huffman parents: 
48559diff
changeset | 33 | |
| 
e0875d956a6b
replace Nat_Arith simprocs with simpler conversions that do less rearrangement of terms
 huffman parents: 
48559diff
changeset | 34 | exception Cancel | 
| 9436 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 35 | |
| 48560 
e0875d956a6b
replace Nat_Arith simprocs with simpler conversions that do less rearrangement of terms
 huffman parents: 
48559diff
changeset | 36 | fun find_common ord xs ys = | 
| 
e0875d956a6b
replace Nat_Arith simprocs with simpler conversions that do less rearrangement of terms
 huffman parents: 
48559diff
changeset | 37 | let | 
| 
e0875d956a6b
replace Nat_Arith simprocs with simpler conversions that do less rearrangement of terms
 huffman parents: 
48559diff
changeset | 38 | fun find (xs as (x, px)::xs') (ys as (y, py)::ys') = | 
| 
e0875d956a6b
replace Nat_Arith simprocs with simpler conversions that do less rearrangement of terms
 huffman parents: 
48559diff
changeset | 39 | (case ord (x, y) of | 
| 
e0875d956a6b
replace Nat_Arith simprocs with simpler conversions that do less rearrangement of terms
 huffman parents: 
48559diff
changeset | 40 | EQUAL => (px, py) | 
| 
e0875d956a6b
replace Nat_Arith simprocs with simpler conversions that do less rearrangement of terms
 huffman parents: 
48559diff
changeset | 41 | | LESS => find xs' ys | 
| 
e0875d956a6b
replace Nat_Arith simprocs with simpler conversions that do less rearrangement of terms
 huffman parents: 
48559diff
changeset | 42 | | GREATER => find xs ys') | 
| 
e0875d956a6b
replace Nat_Arith simprocs with simpler conversions that do less rearrangement of terms
 huffman parents: 
48559diff
changeset | 43 | | find _ _ = raise Cancel | 
| 
e0875d956a6b
replace Nat_Arith simprocs with simpler conversions that do less rearrangement of terms
 huffman parents: 
48559diff
changeset | 44 | fun ord' ((x, _), (y, _)) = ord (x, y) | 
| 
e0875d956a6b
replace Nat_Arith simprocs with simpler conversions that do less rearrangement of terms
 huffman parents: 
48559diff
changeset | 45 | in | 
| 
e0875d956a6b
replace Nat_Arith simprocs with simpler conversions that do less rearrangement of terms
 huffman parents: 
48559diff
changeset | 46 | find (sort ord' xs) (sort ord' ys) | 
| 
e0875d956a6b
replace Nat_Arith simprocs with simpler conversions that do less rearrangement of terms
 huffman parents: 
48559diff
changeset | 47 | end | 
| 9436 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 48 | |
| 54742 
7a86358a3c0b
proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
 wenzelm parents: 
48571diff
changeset | 49 | fun cancel_conv rule ctxt ct = | 
| 48560 
e0875d956a6b
replace Nat_Arith simprocs with simpler conversions that do less rearrangement of terms
 huffman parents: 
48559diff
changeset | 50 | let | 
| 
e0875d956a6b
replace Nat_Arith simprocs with simpler conversions that do less rearrangement of terms
 huffman parents: 
48559diff
changeset | 51 | val ((_, lhs), rhs) = (apfst dest_comb o dest_comb) (Thm.term_of ct) | 
| 
e0875d956a6b
replace Nat_Arith simprocs with simpler conversions that do less rearrangement of terms
 huffman parents: 
48559diff
changeset | 52 | val (lpath, rpath) = find_common Term_Ord.term_ord (atoms lhs) (atoms rhs) | 
| 54742 
7a86358a3c0b
proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
 wenzelm parents: 
48571diff
changeset | 53 | val lconv = move_to_front ctxt lpath | 
| 
7a86358a3c0b
proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
 wenzelm parents: 
48571diff
changeset | 54 | val rconv = move_to_front ctxt rpath | 
| 48560 
e0875d956a6b
replace Nat_Arith simprocs with simpler conversions that do less rearrangement of terms
 huffman parents: 
48559diff
changeset | 55 | val conv1 = Conv.combination_conv (Conv.arg_conv lconv) rconv | 
| 
e0875d956a6b
replace Nat_Arith simprocs with simpler conversions that do less rearrangement of terms
 huffman parents: 
48559diff
changeset | 56 | val conv = conv1 then_conv Conv.rewr_conv rule | 
| 48571 | 57 | in conv ct end | 
| 58 |     handle Cancel => raise CTERM ("no_conversion", [])
 | |
| 9436 
62bb04ab4b01
rearranged setup of arithmetic procedures, avoiding global reference values;
 wenzelm parents: diff
changeset | 59 | |
| 62365 | 60 | val cancel_diff_conv = cancel_conv (mk_meta_eq @{thm add_diff_cancel_left [where ?'a = nat]})
 | 
| 61 | val cancel_eq_conv = cancel_conv (mk_meta_eq @{thm add_left_cancel [where ?'a = nat]})
 | |
| 62 | val cancel_le_conv = cancel_conv (mk_meta_eq @{thm add_le_cancel_left [where ?'a = nat]})
 | |
| 63 | val cancel_less_conv = cancel_conv (mk_meta_eq @{thm add_less_cancel_left [where ?'a = nat]})
 | |
| 24076 | 64 | |
| 24095 | 65 | end; |