18 subsection {* Type definition *} |
18 subsection {* Type definition *} |
19 |
19 |
20 typedef (open) 'a word = "{(0::int) ..< 2^len_of TYPE('a::len0)}" |
20 typedef (open) 'a word = "{(0::int) ..< 2^len_of TYPE('a::len0)}" |
21 morphisms uint Abs_word by auto |
21 morphisms uint Abs_word by auto |
22 |
22 |
|
23 lemma uint_nonnegative: |
|
24 "0 \<le> uint w" |
|
25 using word.uint [of w] by simp |
|
26 |
|
27 lemma uint_bounded: |
|
28 fixes w :: "'a::len0 word" |
|
29 shows "uint w < 2 ^ len_of TYPE('a)" |
|
30 using word.uint [of w] by simp |
|
31 |
|
32 lemma uint_idem: |
|
33 fixes w :: "'a::len0 word" |
|
34 shows "uint w mod 2 ^ len_of TYPE('a) = uint w" |
|
35 using uint_nonnegative uint_bounded by (rule mod_pos_pos_trivial) |
|
36 |
23 definition word_of_int :: "int \<Rightarrow> 'a\<Colon>len0 word" where |
37 definition word_of_int :: "int \<Rightarrow> 'a\<Colon>len0 word" where |
24 -- {* representation of words using unsigned or signed bins, |
38 -- {* representation of words using unsigned or signed bins, |
25 only difference in these is the type class *} |
39 only difference in these is the type class *} |
26 "word_of_int w = Abs_word (bintrunc (len_of TYPE ('a)) w)" |
40 "word_of_int k = Abs_word (k mod 2 ^ len_of TYPE('a))" |
27 |
41 |
28 lemma uint_word_of_int [code]: "uint (word_of_int w \<Colon> 'a\<Colon>len0 word) = w mod 2 ^ len_of TYPE('a)" |
42 lemma uint_word_of_int: |
29 by (auto simp add: word_of_int_def bintrunc_mod2p intro: Abs_word_inverse) |
43 "uint (word_of_int k :: 'a::len0 word) = k mod 2 ^ len_of TYPE('a)" |
30 |
44 by (auto simp add: word_of_int_def intro: Abs_word_inverse) |
31 code_datatype word_of_int |
45 |
32 |
46 lemma word_of_int_uint: |
33 subsection {* Random instance *} |
47 "word_of_int (uint w) = w" |
|
48 by (simp add: word_of_int_def uint_idem uint_inverse) |
|
49 |
|
50 lemma word_uint_eq_iff: |
|
51 "a = b \<longleftrightarrow> uint a = uint b" |
|
52 by (simp add: uint_inject) |
|
53 |
|
54 lemma word_uint_eqI: |
|
55 "uint a = uint b \<Longrightarrow> a = b" |
|
56 by (simp add: word_uint_eq_iff) |
|
57 |
|
58 |
|
59 subsection {* Basic code generation setup *} |
|
60 |
|
61 definition Word :: "int \<Rightarrow> 'a::len0 word" |
|
62 where |
|
63 [code_post]: "Word = word_of_int" |
|
64 |
|
65 lemma [code abstype]: |
|
66 "Word (uint w) = w" |
|
67 by (simp add: Word_def word_of_int_uint) |
|
68 |
|
69 declare uint_word_of_int [code abstract] |
|
70 |
|
71 instantiation word :: (len0) equal |
|
72 begin |
|
73 |
|
74 definition equal_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> bool" where |
|
75 "equal_word k l \<longleftrightarrow> HOL.equal (uint k) (uint l)" |
|
76 |
|
77 instance proof |
|
78 qed (simp add: equal equal_word_def word_uint_eq_iff) |
|
79 |
|
80 end |
34 |
81 |
35 notation fcomp (infixl "\<circ>>" 60) |
82 notation fcomp (infixl "\<circ>>" 60) |
36 notation scomp (infixl "\<circ>\<rightarrow>" 60) |
83 notation scomp (infixl "\<circ>\<rightarrow>" 60) |
37 |
84 |
38 instantiation word :: ("{len0, typerep}") random |
85 instantiation word :: ("{len0, typerep}") random |
39 begin |
86 begin |
40 |
87 |
41 definition |
88 definition |
42 "random_word i = Random.range (max i (2 ^ len_of TYPE('a))) \<circ>\<rightarrow> (\<lambda>k. Pair ( |
89 "random_word i = Random.range i \<circ>\<rightarrow> (\<lambda>k. Pair ( |
43 let j = word_of_int (Code_Numeral.int_of k) :: 'a word |
90 let j = word_of_int (Code_Numeral.int_of k) :: 'a word |
44 in (j, \<lambda>_::unit. Code_Evaluation.term_of j)))" |
91 in (j, \<lambda>_::unit. Code_Evaluation.term_of j)))" |
45 |
92 |
46 instance .. |
93 instance .. |
47 |
94 |
502 |
543 |
503 lemmas int_word_sint = td_ext_sint [THEN td_ext.eq_norm] |
544 lemmas int_word_sint = td_ext_sint [THEN td_ext.eq_norm] |
504 |
545 |
505 lemmas td_sint = word_sint.td |
546 lemmas td_sint = word_sint.td |
506 |
547 |
507 lemma word_number_of_alt: |
|
508 "number_of b = word_of_int (number_of b)" |
|
509 by (simp add: number_of_eq word_number_of_def) |
|
510 |
|
511 declare word_number_of_alt [symmetric, code_abbrev] |
|
512 |
|
513 lemma word_no_wi: "number_of = word_of_int" |
|
514 by (auto simp: word_number_of_def) |
|
515 |
|
516 lemma to_bl_def': |
548 lemma to_bl_def': |
517 "(to_bl :: 'a :: len0 word => bool list) = |
549 "(to_bl :: 'a :: len0 word => bool list) = |
518 bin_to_bl (len_of TYPE('a)) o uint" |
550 bin_to_bl (len_of TYPE('a)) o uint" |
519 by (auto simp: to_bl_def) |
551 by (auto simp: to_bl_def) |
520 |
552 |
521 lemmas word_reverse_no_def [simp] = word_reverse_def [of "number_of w"] for w |
553 lemmas word_reverse_no_def [simp] = word_reverse_def [of "numeral w"] for w |
522 |
554 |
523 lemma uints_mod: "uints n = range (\<lambda>w. w mod 2 ^ n)" |
555 lemma uints_mod: "uints n = range (\<lambda>w. w mod 2 ^ n)" |
524 by (fact uints_def [unfolded no_bintr_alt1]) |
556 by (fact uints_def [unfolded no_bintr_alt1]) |
525 |
557 |
|
558 lemma word_numeral_alt: |
|
559 "numeral b = word_of_int (numeral b)" |
|
560 by (induct b, simp_all only: numeral.simps word_of_int_homs) |
|
561 |
|
562 declare word_numeral_alt [symmetric, code_abbrev] |
|
563 |
|
564 lemma word_neg_numeral_alt: |
|
565 "neg_numeral b = word_of_int (neg_numeral b)" |
|
566 by (simp only: neg_numeral_def word_numeral_alt wi_hom_neg) |
|
567 |
|
568 declare word_neg_numeral_alt [symmetric, code_abbrev] |
|
569 |
526 lemma uint_bintrunc [simp]: |
570 lemma uint_bintrunc [simp]: |
527 "uint (number_of bin :: 'a word) = |
571 "uint (numeral bin :: 'a word) = |
528 bintrunc (len_of TYPE ('a :: len0)) (number_of bin)" |
572 bintrunc (len_of TYPE ('a :: len0)) (numeral bin)" |
529 unfolding word_number_of_alt by (rule word_ubin.eq_norm) |
573 unfolding word_numeral_alt by (rule word_ubin.eq_norm) |
|
574 |
|
575 lemma uint_bintrunc_neg [simp]: "uint (neg_numeral bin :: 'a word) = |
|
576 bintrunc (len_of TYPE ('a :: len0)) (neg_numeral bin)" |
|
577 by (simp only: word_neg_numeral_alt word_ubin.eq_norm) |
530 |
578 |
531 lemma sint_sbintrunc [simp]: |
579 lemma sint_sbintrunc [simp]: |
532 "sint (number_of bin :: 'a word) = |
580 "sint (numeral bin :: 'a word) = |
533 sbintrunc (len_of TYPE ('a :: len) - 1) (number_of bin)" |
581 sbintrunc (len_of TYPE ('a :: len) - 1) (numeral bin)" |
534 unfolding word_number_of_alt by (rule word_sbin.eq_norm) |
582 by (simp only: word_numeral_alt word_sbin.eq_norm) |
|
583 |
|
584 lemma sint_sbintrunc_neg [simp]: "sint (neg_numeral bin :: 'a word) = |
|
585 sbintrunc (len_of TYPE ('a :: len) - 1) (neg_numeral bin)" |
|
586 by (simp only: word_neg_numeral_alt word_sbin.eq_norm) |
535 |
587 |
536 lemma unat_bintrunc [simp]: |
588 lemma unat_bintrunc [simp]: |
537 "unat (number_of bin :: 'a :: len0 word) = |
589 "unat (numeral bin :: 'a :: len0 word) = |
538 nat (bintrunc (len_of TYPE('a)) (number_of bin))" |
590 nat (bintrunc (len_of TYPE('a)) (numeral bin))" |
539 unfolding unat_def nat_number_of_def |
591 by (simp only: unat_def uint_bintrunc) |
540 by (simp only: uint_bintrunc) |
592 |
|
593 lemma unat_bintrunc_neg [simp]: |
|
594 "unat (neg_numeral bin :: 'a :: len0 word) = |
|
595 nat (bintrunc (len_of TYPE('a)) (neg_numeral bin))" |
|
596 by (simp only: unat_def uint_bintrunc_neg) |
541 |
597 |
542 lemma size_0_eq: "size (w :: 'a :: len0 word) = 0 \<Longrightarrow> v = w" |
598 lemma size_0_eq: "size (w :: 'a :: len0 word) = 0 \<Longrightarrow> v = w" |
543 apply (unfold word_size) |
599 apply (unfold word_size) |
544 apply (rule word_uint.Rep_eqD) |
600 apply (rule word_uint.Rep_eqD) |
545 apply (rule box_equals) |
601 apply (rule box_equals) |
560 lemma sint_lt: "sint (x::'a::len word) < 2 ^ (len_of TYPE('a) - 1)" |
616 lemma sint_lt: "sint (x::'a::len word) < 2 ^ (len_of TYPE('a) - 1)" |
561 using word_sint.Rep [of x] by (simp add: sints_num) |
617 using word_sint.Rep [of x] by (simp add: sints_num) |
562 |
618 |
563 lemma sign_uint_Pls [simp]: |
619 lemma sign_uint_Pls [simp]: |
564 "bin_sign (uint x) = 0" |
620 "bin_sign (uint x) = 0" |
565 by (simp add: sign_Pls_ge_0 number_of_eq) |
621 by (simp add: sign_Pls_ge_0) |
566 |
622 |
567 lemma uint_m2p_neg: "uint (x::'a::len0 word) - 2 ^ len_of TYPE('a) < 0" |
623 lemma uint_m2p_neg: "uint (x::'a::len0 word) - 2 ^ len_of TYPE('a) < 0" |
568 by (simp only: diff_less_0_iff_less uint_lt2p) |
624 by (simp only: diff_less_0_iff_less uint_lt2p) |
569 |
625 |
570 lemma uint_m2p_not_non_neg: |
626 lemma uint_m2p_not_non_neg: |
579 by (fact uint_ge_0 [THEN leD, THEN linorder_antisym_conv1]) |
635 by (fact uint_ge_0 [THEN leD, THEN linorder_antisym_conv1]) |
580 |
636 |
581 lemma uint_nat: "uint w = int (unat w)" |
637 lemma uint_nat: "uint w = int (unat w)" |
582 unfolding unat_def by auto |
638 unfolding unat_def by auto |
583 |
639 |
584 lemma uint_number_of: |
640 lemma uint_numeral: |
585 "uint (number_of b :: 'a :: len0 word) = number_of b mod 2 ^ len_of TYPE('a)" |
641 "uint (numeral b :: 'a :: len0 word) = numeral b mod 2 ^ len_of TYPE('a)" |
586 unfolding word_number_of_alt |
642 unfolding word_numeral_alt |
587 by (simp only: int_word_uint) |
643 by (simp only: int_word_uint) |
588 |
644 |
589 lemma unat_number_of: |
645 lemma uint_neg_numeral: |
590 "bin_sign (number_of b) = 0 \<Longrightarrow> |
646 "uint (neg_numeral b :: 'a :: len0 word) = neg_numeral b mod 2 ^ len_of TYPE('a)" |
591 unat (number_of b::'a::len0 word) = number_of b mod 2 ^ len_of TYPE ('a)" |
647 unfolding word_neg_numeral_alt |
|
648 by (simp only: int_word_uint) |
|
649 |
|
650 lemma unat_numeral: |
|
651 "unat (numeral b::'a::len0 word) = numeral b mod 2 ^ len_of TYPE ('a)" |
592 apply (unfold unat_def) |
652 apply (unfold unat_def) |
593 apply (clarsimp simp only: uint_number_of) |
653 apply (clarsimp simp only: uint_numeral) |
594 apply (rule nat_mod_distrib [THEN trans]) |
654 apply (rule nat_mod_distrib [THEN trans]) |
595 apply (erule sign_Pls_ge_0 [THEN iffD1]) |
655 apply (rule zero_le_numeral) |
596 apply (simp_all add: nat_power_eq) |
656 apply (simp_all add: nat_power_eq) |
597 done |
657 done |
598 |
658 |
599 lemma sint_number_of: "sint (number_of b :: 'a :: len word) = (number_of b + |
659 lemma sint_numeral: "sint (numeral b :: 'a :: len word) = (numeral b + |
600 2 ^ (len_of TYPE('a) - 1)) mod 2 ^ len_of TYPE('a) - |
660 2 ^ (len_of TYPE('a) - 1)) mod 2 ^ len_of TYPE('a) - |
601 2 ^ (len_of TYPE('a) - 1)" |
661 2 ^ (len_of TYPE('a) - 1)" |
602 unfolding word_number_of_alt by (rule int_word_sint) |
662 unfolding word_numeral_alt by (rule int_word_sint) |
603 |
663 |
604 lemma word_of_int_0 [simp]: "word_of_int 0 = 0" |
664 lemma word_of_int_0 [simp, code_post]: "word_of_int 0 = 0" |
605 unfolding word_0_wi .. |
665 unfolding word_0_wi .. |
606 |
666 |
607 lemma word_of_int_1 [simp]: "word_of_int 1 = 1" |
667 lemma word_of_int_1 [simp, code_post]: "word_of_int 1 = 1" |
608 unfolding word_1_wi .. |
668 unfolding word_1_wi .. |
609 |
669 |
610 lemma word_of_int_bin [simp] : |
670 lemma word_of_int_numeral [simp] : |
611 "(word_of_int (number_of bin) :: 'a :: len0 word) = (number_of bin)" |
671 "(word_of_int (numeral bin) :: 'a :: len0 word) = (numeral bin)" |
612 unfolding word_number_of_alt .. |
672 unfolding word_numeral_alt .. |
|
673 |
|
674 lemma word_of_int_neg_numeral [simp]: |
|
675 "(word_of_int (neg_numeral bin) :: 'a :: len0 word) = (neg_numeral bin)" |
|
676 unfolding neg_numeral_def word_numeral_alt wi_hom_syms .. |
613 |
677 |
614 lemma word_int_case_wi: |
678 lemma word_int_case_wi: |
615 "word_int_case f (word_of_int i :: 'b word) = |
679 "word_int_case f (word_of_int i :: 'b word) = |
616 f (i mod 2 ^ len_of TYPE('b::len0))" |
680 f (i mod 2 ^ len_of TYPE('b::len0))" |
617 unfolding word_int_case_def by (simp add: word_uint.eq_norm) |
681 unfolding word_int_case_def by (simp add: word_uint.eq_norm) |
773 |
837 |
774 lemma to_bl_of_bin: |
838 lemma to_bl_of_bin: |
775 "to_bl (word_of_int bin::'a::len0 word) = bin_to_bl (len_of TYPE('a)) bin" |
839 "to_bl (word_of_int bin::'a::len0 word) = bin_to_bl (len_of TYPE('a)) bin" |
776 unfolding uint_bl by (clarsimp simp add: word_ubin.eq_norm word_size) |
840 unfolding uint_bl by (clarsimp simp add: word_ubin.eq_norm word_size) |
777 |
841 |
778 lemma to_bl_no_bin [simp]: |
842 lemma to_bl_numeral [simp]: |
779 "to_bl (number_of bin::'a::len0 word) = bin_to_bl (len_of TYPE('a)) (number_of bin)" |
843 "to_bl (numeral bin::'a::len0 word) = |
780 unfolding word_number_of_alt by (rule to_bl_of_bin) |
844 bin_to_bl (len_of TYPE('a)) (numeral bin)" |
|
845 unfolding word_numeral_alt by (rule to_bl_of_bin) |
|
846 |
|
847 lemma to_bl_neg_numeral [simp]: |
|
848 "to_bl (neg_numeral bin::'a::len0 word) = |
|
849 bin_to_bl (len_of TYPE('a)) (neg_numeral bin)" |
|
850 unfolding word_neg_numeral_alt by (rule to_bl_of_bin) |
781 |
851 |
782 lemma to_bl_to_bin [simp] : "bl_to_bin (to_bl w) = uint w" |
852 lemma to_bl_to_bin [simp] : "bl_to_bin (to_bl w) = uint w" |
783 unfolding uint_bl by (simp add : word_size) |
853 unfolding uint_bl by (simp add : word_size) |
784 |
854 |
785 lemma uint_bl_bin: |
855 lemma uint_bl_bin: |
801 |
871 |
802 lemma unats_uints: "unats n = nat ` uints n" |
872 lemma unats_uints: "unats n = nat ` uints n" |
803 by (auto simp add : uints_unats image_iff) |
873 by (auto simp add : uints_unats image_iff) |
804 |
874 |
805 lemmas bintr_num = word_ubin.norm_eq_iff |
875 lemmas bintr_num = word_ubin.norm_eq_iff |
806 [of "number_of a" "number_of b", symmetric, folded word_number_of_alt] for a b |
876 [of "numeral a" "numeral b", symmetric, folded word_numeral_alt] for a b |
807 lemmas sbintr_num = word_sbin.norm_eq_iff |
877 lemmas sbintr_num = word_sbin.norm_eq_iff |
808 [of "number_of a" "number_of b", symmetric, folded word_number_of_alt] for a b |
878 [of "numeral a" "numeral b", symmetric, folded word_numeral_alt] for a b |
809 |
|
810 lemmas num_of_bintr = word_ubin.Abs_norm [folded word_number_of_def] |
|
811 lemmas num_of_sbintr = word_sbin.Abs_norm [folded word_number_of_def] |
|
812 |
|
813 (* don't add these to simpset, since may want bintrunc n w to be simplified; |
|
814 may want these in reverse, but loop as simp rules, so use following *) |
|
815 |
879 |
816 lemma num_of_bintr': |
880 lemma num_of_bintr': |
817 "bintrunc (len_of TYPE('a :: len0)) (number_of a) = (number_of b) \<Longrightarrow> |
881 "bintrunc (len_of TYPE('a :: len0)) (numeral a) = (numeral b) \<Longrightarrow> |
818 number_of a = (number_of b :: 'a word)" |
882 numeral a = (numeral b :: 'a word)" |
819 unfolding bintr_num by (erule subst, simp) |
883 unfolding bintr_num by (erule subst, simp) |
820 |
884 |
821 lemma num_of_sbintr': |
885 lemma num_of_sbintr': |
822 "sbintrunc (len_of TYPE('a :: len) - 1) (number_of a) = (number_of b) \<Longrightarrow> |
886 "sbintrunc (len_of TYPE('a :: len) - 1) (numeral a) = (numeral b) \<Longrightarrow> |
823 number_of a = (number_of b :: 'a word)" |
887 numeral a = (numeral b :: 'a word)" |
824 unfolding sbintr_num by (erule subst, simp) |
888 unfolding sbintr_num by (erule subst, simp) |
825 |
889 |
826 lemma num_abs_bintr: |
890 lemma num_abs_bintr: |
827 "(number_of x :: 'a word) = |
891 "(numeral x :: 'a word) = |
828 word_of_int (bintrunc (len_of TYPE('a::len0)) (number_of x))" |
892 word_of_int (bintrunc (len_of TYPE('a::len0)) (numeral x))" |
829 by (simp only: word_ubin.Abs_norm word_number_of_alt) |
893 by (simp only: word_ubin.Abs_norm word_numeral_alt) |
830 |
894 |
831 lemma num_abs_sbintr: |
895 lemma num_abs_sbintr: |
832 "(number_of x :: 'a word) = |
896 "(numeral x :: 'a word) = |
833 word_of_int (sbintrunc (len_of TYPE('a::len) - 1) (number_of x))" |
897 word_of_int (sbintrunc (len_of TYPE('a::len) - 1) (numeral x))" |
834 by (simp only: word_sbin.Abs_norm word_number_of_alt) |
898 by (simp only: word_sbin.Abs_norm word_numeral_alt) |
835 |
899 |
836 (** cast - note, no arg for new length, as it's determined by type of result, |
900 (** cast - note, no arg for new length, as it's determined by type of result, |
837 thus in "cast w = w, the type means cast to length of w! **) |
901 thus in "cast w = w, the type means cast to length of w! **) |
838 |
902 |
839 lemma ucast_id: "ucast w = w" |
903 lemma ucast_id: "ucast w = w" |
854 done |
918 done |
855 |
919 |
856 (* for literal u(s)cast *) |
920 (* for literal u(s)cast *) |
857 |
921 |
858 lemma ucast_bintr [simp]: |
922 lemma ucast_bintr [simp]: |
859 "ucast (number_of w ::'a::len0 word) = |
923 "ucast (numeral w ::'a::len0 word) = |
860 word_of_int (bintrunc (len_of TYPE('a)) (number_of w))" |
924 word_of_int (bintrunc (len_of TYPE('a)) (numeral w))" |
861 unfolding ucast_def by simp |
925 unfolding ucast_def by simp |
|
926 (* TODO: neg_numeral *) |
862 |
927 |
863 lemma scast_sbintr [simp]: |
928 lemma scast_sbintr [simp]: |
864 "scast (number_of w ::'a::len word) = |
929 "scast (numeral w ::'a::len word) = |
865 word_of_int (sbintrunc (len_of TYPE('a) - Suc 0) (number_of w))" |
930 word_of_int (sbintrunc (len_of TYPE('a) - Suc 0) (numeral w))" |
866 unfolding scast_def by simp |
931 unfolding scast_def by simp |
867 |
932 |
868 lemma source_size: "source_size (c::'a::len0 word \<Rightarrow> _) = len_of TYPE('a)" |
933 lemma source_size: "source_size (c::'a::len0 word \<Rightarrow> _) = len_of TYPE('a)" |
869 unfolding source_size_def word_size Let_def .. |
934 unfolding source_size_def word_size Let_def .. |
870 |
935 |
1014 apply (rule word_ubin.norm_eq_iff [THEN iffD1]) |
1079 apply (rule word_ubin.norm_eq_iff [THEN iffD1]) |
1015 apply (erule bintrunc_bintrunc_ge) |
1080 apply (erule bintrunc_bintrunc_ge) |
1016 done |
1081 done |
1017 |
1082 |
1018 lemma ucast_down_no [OF refl]: |
1083 lemma ucast_down_no [OF refl]: |
1019 "uc = ucast \<Longrightarrow> is_down uc \<Longrightarrow> uc (number_of bin) = number_of bin" |
1084 "uc = ucast \<Longrightarrow> is_down uc \<Longrightarrow> uc (numeral bin) = numeral bin" |
1020 unfolding word_number_of_alt by clarify (rule ucast_down_wi) |
1085 unfolding word_numeral_alt by clarify (rule ucast_down_wi) |
1021 |
1086 |
1022 lemma ucast_down_bl [OF refl]: |
1087 lemma ucast_down_bl [OF refl]: |
1023 "uc = ucast \<Longrightarrow> is_down uc \<Longrightarrow> uc (of_bl bl) = of_bl bl" |
1088 "uc = ucast \<Longrightarrow> is_down uc \<Longrightarrow> uc (of_bl bl) = of_bl bl" |
1024 unfolding of_bl_def by clarify (erule ucast_down_wi) |
1089 unfolding of_bl_def by clarify (erule ucast_down_wi) |
1025 |
1090 |
1026 lemmas slice_def' = slice_def [unfolded word_size] |
1091 lemmas slice_def' = slice_def [unfolded word_size] |
1027 lemmas test_bit_def' = word_test_bit_def [THEN fun_cong] |
1092 lemmas test_bit_def' = word_test_bit_def [THEN fun_cong] |
1028 |
1093 |
1029 lemmas word_log_defs = word_and_def word_or_def word_xor_def word_not_def |
1094 lemmas word_log_defs = word_and_def word_or_def word_xor_def word_not_def |
1030 |
1095 |
1031 text {* Executable equality *} |
|
1032 |
|
1033 instantiation word :: (len0) equal |
|
1034 begin |
|
1035 |
|
1036 definition equal_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> bool" where |
|
1037 "equal_word k l \<longleftrightarrow> HOL.equal (uint k) (uint l)" |
|
1038 |
|
1039 instance proof |
|
1040 qed (simp add: equal equal_word_def) |
|
1041 |
|
1042 end |
|
1043 |
|
1044 |
1096 |
1045 subsection {* Word Arithmetic *} |
1097 subsection {* Word Arithmetic *} |
1046 |
1098 |
1047 lemma word_less_alt: "(a < b) = (uint a < uint b)" |
1099 lemma word_less_alt: "(a < b) = (uint a < uint b)" |
1048 unfolding word_less_def word_le_def by (simp add: less_le) |
1100 unfolding word_less_def word_le_def by (simp add: less_le) |
1055 |
1107 |
1056 lemma udvdI: |
1108 lemma udvdI: |
1057 "0 \<le> n \<Longrightarrow> uint b = n * uint a \<Longrightarrow> a udvd b" |
1109 "0 \<le> n \<Longrightarrow> uint b = n * uint a \<Longrightarrow> a udvd b" |
1058 by (auto simp: udvd_def) |
1110 by (auto simp: udvd_def) |
1059 |
1111 |
1060 lemmas word_div_no [simp] = word_div_def [of "number_of a" "number_of b"] for a b |
1112 lemmas word_div_no [simp] = word_div_def [of "numeral a" "numeral b"] for a b |
1061 |
1113 |
1062 lemmas word_mod_no [simp] = word_mod_def [of "number_of a" "number_of b"] for a b |
1114 lemmas word_mod_no [simp] = word_mod_def [of "numeral a" "numeral b"] for a b |
1063 |
1115 |
1064 lemmas word_less_no [simp] = word_less_def [of "number_of a" "number_of b"] for a b |
1116 lemmas word_less_no [simp] = word_less_def [of "numeral a" "numeral b"] for a b |
1065 |
1117 |
1066 lemmas word_le_no [simp] = word_le_def [of "number_of a" "number_of b"] for a b |
1118 lemmas word_le_no [simp] = word_le_def [of "numeral a" "numeral b"] for a b |
1067 |
1119 |
1068 lemmas word_sless_no [simp] = word_sless_def [of "number_of a" "number_of b"] for a b |
1120 lemmas word_sless_no [simp] = word_sless_def [of "numeral a" "numeral b"] for a b |
1069 |
1121 |
1070 lemmas word_sle_no [simp] = word_sle_def [of "number_of a" "number_of b"] for a b |
1122 lemmas word_sle_no [simp] = word_sle_def [of "numeral a" "numeral b"] for a b |
1071 |
|
1072 (* following two are available in class number_ring, |
|
1073 but convenient to have them here here; |
|
1074 note - the number_ring versions, numeral_0_eq_0 and numeral_1_eq_1 |
|
1075 are in the default simpset, so to use the automatic simplifications for |
|
1076 (eg) sint (number_of bin) on sint 1, must do |
|
1077 (simp add: word_1_no del: numeral_1_eq_1) |
|
1078 *) |
|
1079 lemma word_0_no: "(0::'a::len0 word) = Numeral0" |
|
1080 by (simp add: word_number_of_alt) |
|
1081 |
1123 |
1082 lemma word_1_no: "(1::'a::len0 word) = Numeral1" |
1124 lemma word_1_no: "(1::'a::len0 word) = Numeral1" |
1083 by (simp add: word_number_of_alt) |
1125 by (simp add: word_numeral_alt) |
1084 |
1126 |
1085 lemma word_m1_wi: "-1 = word_of_int -1" |
1127 lemma word_m1_wi: "-1 = word_of_int -1" |
1086 by (rule word_number_of_alt) |
1128 by (rule word_neg_numeral_alt) |
1087 |
1129 |
1088 lemma word_0_bl [simp]: "of_bl [] = 0" |
1130 lemma word_0_bl [simp]: "of_bl [] = 0" |
1089 unfolding of_bl_def by simp |
1131 unfolding of_bl_def by simp |
1090 |
1132 |
1091 lemma word_1_bl: "of_bl [True] = 1" |
1133 lemma word_1_bl: "of_bl [True] = 1" |
1193 |
1235 |
1194 lemmas uint_div_alt = word_div_def [THEN trans [OF uint_cong int_word_uint]] |
1236 lemmas uint_div_alt = word_div_def [THEN trans [OF uint_cong int_word_uint]] |
1195 lemmas uint_mod_alt = word_mod_def [THEN trans [OF uint_cong int_word_uint]] |
1237 lemmas uint_mod_alt = word_mod_def [THEN trans [OF uint_cong int_word_uint]] |
1196 |
1238 |
1197 lemma word_pred_0_n1: "word_pred 0 = word_of_int -1" |
1239 lemma word_pred_0_n1: "word_pred 0 = word_of_int -1" |
1198 unfolding word_pred_def uint_eq_0 pred_def by simp |
1240 unfolding word_pred_def uint_eq_0 by simp |
1199 |
1241 |
1200 lemma succ_pred_no [simp]: |
1242 lemma succ_pred_no [simp]: |
1201 "word_succ (number_of bin) = number_of (Int.succ bin) & |
1243 "word_succ (numeral w) = numeral w + 1" |
1202 word_pred (number_of bin) = number_of (Int.pred bin)" |
1244 "word_pred (numeral w) = numeral w - 1" |
1203 unfolding word_number_of_def Int.succ_def Int.pred_def |
1245 "word_succ (neg_numeral w) = neg_numeral w + 1" |
1204 by (simp add: word_of_int_homs) |
1246 "word_pred (neg_numeral w) = neg_numeral w - 1" |
|
1247 unfolding word_succ_p1 word_pred_m1 by simp_all |
1205 |
1248 |
1206 lemma word_sp_01 [simp] : |
1249 lemma word_sp_01 [simp] : |
1207 "word_succ -1 = 0 & word_succ 0 = 1 & word_pred 0 = -1 & word_pred 1 = 0" |
1250 "word_succ -1 = 0 & word_succ 0 = 1 & word_pred 0 = -1 & word_pred 1 = 0" |
1208 unfolding word_0_no word_1_no by simp |
1251 unfolding word_succ_p1 word_pred_m1 by simp_all |
1209 |
1252 |
1210 (* alternative approach to lifting arithmetic equalities *) |
1253 (* alternative approach to lifting arithmetic equalities *) |
1211 lemma word_of_int_Ex: |
1254 lemma word_of_int_Ex: |
1212 "\<exists>y. x = word_of_int y" |
1255 "\<exists>y. x = word_of_int y" |
1213 by (rule_tac x="uint x" in exI) simp |
1256 by (rule_tac x="uint x" in exI) simp |
1228 by (simp only: word_m1_wi word_uint.eq_norm m1mod2k) auto |
1271 by (simp only: word_m1_wi word_uint.eq_norm m1mod2k) auto |
1229 |
1272 |
1230 lemmas word_not_simps [simp] = |
1273 lemmas word_not_simps [simp] = |
1231 word_zero_le [THEN leD] word_m1_ge [THEN leD] word_n1_ge [THEN leD] |
1274 word_zero_le [THEN leD] word_m1_ge [THEN leD] word_n1_ge [THEN leD] |
1232 |
1275 |
1233 lemma word_gt_0: "0 < y = (0 ~= (y :: 'a :: len0 word))" |
1276 lemma word_gt_0: "0 < y \<longleftrightarrow> 0 \<noteq> (y :: 'a :: len0 word)" |
1234 unfolding word_less_def by auto |
1277 by (simp add: less_le) |
1235 |
1278 |
1236 lemmas word_gt_0_no [simp] = word_gt_0 [of "number_of y"] for y |
1279 lemmas word_gt_0_no [simp] = word_gt_0 [of "numeral y"] for y |
1237 |
1280 |
1238 lemma word_sless_alt: "(a <s b) = (sint a < sint b)" |
1281 lemma word_sless_alt: "(a <s b) = (sint a < sint b)" |
1239 unfolding word_sle_def word_sless_def |
1282 unfolding word_sle_def word_sless_def |
1240 by (auto simp add: less_le) |
1283 by (auto simp add: less_le) |
1241 |
1284 |
1645 by (simp add: of_nat_nat word_of_int) |
1688 by (simp add: of_nat_nat word_of_int) |
1646 |
1689 |
1647 (* note that iszero_def is only for class comm_semiring_1_cancel, |
1690 (* note that iszero_def is only for class comm_semiring_1_cancel, |
1648 which requires word length >= 1, ie 'a :: len word *) |
1691 which requires word length >= 1, ie 'a :: len word *) |
1649 lemma iszero_word_no [simp]: |
1692 lemma iszero_word_no [simp]: |
1650 "iszero (number_of bin :: 'a :: len word) = |
1693 "iszero (numeral bin :: 'a :: len word) = |
1651 iszero (bintrunc (len_of TYPE('a)) (number_of bin))" |
1694 iszero (bintrunc (len_of TYPE('a)) (numeral bin))" |
1652 using word_ubin.norm_eq_iff [where 'a='a, of "number_of bin" 0] |
1695 using word_ubin.norm_eq_iff [where 'a='a, of "numeral bin" 0] |
1653 by (simp add: iszero_def [symmetric]) |
1696 by (simp add: iszero_def [symmetric]) |
|
1697 |
|
1698 text {* Use @{text iszero} to simplify equalities between word numerals. *} |
|
1699 |
|
1700 lemmas word_eq_numeral_iff_iszero [simp] = |
|
1701 eq_numeral_iff_iszero [where 'a="'a::len word"] |
1654 |
1702 |
1655 |
1703 |
1656 subsection "Word and nat" |
1704 subsection "Word and nat" |
1657 |
1705 |
1658 lemma td_ext_unat [OF refl]: |
1706 lemma td_ext_unat [OF refl]: |
2021 "a ^ n = (word_of_int (uint a ^ n) :: 'a :: len word)" |
2069 "a ^ n = (word_of_int (uint a ^ n) :: 'a :: len word)" |
2022 by (simp add : word_of_int_power_hom [symmetric]) |
2070 by (simp add : word_of_int_power_hom [symmetric]) |
2023 |
2071 |
2024 lemma of_bl_length_less: |
2072 lemma of_bl_length_less: |
2025 "length x = k \<Longrightarrow> k < len_of TYPE('a) \<Longrightarrow> (of_bl x :: 'a :: len word) < 2 ^ k" |
2073 "length x = k \<Longrightarrow> k < len_of TYPE('a) \<Longrightarrow> (of_bl x :: 'a :: len word) < 2 ^ k" |
2026 apply (unfold of_bl_def word_less_alt word_number_of_alt) |
2074 apply (unfold of_bl_def word_less_alt word_numeral_alt) |
2027 apply safe |
2075 apply safe |
2028 apply (simp (no_asm) add: word_of_int_power_hom word_uint.eq_norm |
2076 apply (simp (no_asm) add: word_of_int_power_hom word_uint.eq_norm |
2029 del: word_of_int_bin) |
2077 del: word_of_int_numeral) |
2030 apply (simp add: mod_pos_pos_trivial) |
2078 apply (simp add: mod_pos_pos_trivial) |
2031 apply (subst mod_pos_pos_trivial) |
2079 apply (subst mod_pos_pos_trivial) |
2032 apply (rule bl_to_bin_ge0) |
2080 apply (rule bl_to_bin_ge0) |
2033 apply (rule order_less_trans) |
2081 apply (rule order_less_trans) |
2034 apply (rule bl_to_bin_lt2p) |
2082 apply (rule bl_to_bin_lt2p) |
2071 "word_of_int a OR word_of_int b = word_of_int (a OR b)" |
2119 "word_of_int a OR word_of_int b = word_of_int (a OR b)" |
2072 "word_of_int a XOR word_of_int b = word_of_int (a XOR b)" |
2120 "word_of_int a XOR word_of_int b = word_of_int (a XOR b)" |
2073 unfolding word_log_defs wils1 by simp_all |
2121 unfolding word_log_defs wils1 by simp_all |
2074 |
2122 |
2075 lemma word_no_log_defs [simp]: |
2123 lemma word_no_log_defs [simp]: |
2076 "NOT number_of a = (number_of (NOT a) :: 'a::len0 word)" |
2124 "NOT (numeral a) = word_of_int (NOT (numeral a))" |
2077 "number_of a AND number_of b = (number_of (a AND b) :: 'a word)" |
2125 "NOT (neg_numeral a) = word_of_int (NOT (neg_numeral a))" |
2078 "number_of a OR number_of b = (number_of (a OR b) :: 'a word)" |
2126 "numeral a AND numeral b = word_of_int (numeral a AND numeral b)" |
2079 "number_of a XOR number_of b = (number_of (a XOR b) :: 'a word)" |
2127 "numeral a AND neg_numeral b = word_of_int (numeral a AND neg_numeral b)" |
2080 unfolding word_no_wi word_wi_log_defs by simp_all |
2128 "neg_numeral a AND numeral b = word_of_int (neg_numeral a AND numeral b)" |
|
2129 "neg_numeral a AND neg_numeral b = word_of_int (neg_numeral a AND neg_numeral b)" |
|
2130 "numeral a OR numeral b = word_of_int (numeral a OR numeral b)" |
|
2131 "numeral a OR neg_numeral b = word_of_int (numeral a OR neg_numeral b)" |
|
2132 "neg_numeral a OR numeral b = word_of_int (neg_numeral a OR numeral b)" |
|
2133 "neg_numeral a OR neg_numeral b = word_of_int (neg_numeral a OR neg_numeral b)" |
|
2134 "numeral a XOR numeral b = word_of_int (numeral a XOR numeral b)" |
|
2135 "numeral a XOR neg_numeral b = word_of_int (numeral a XOR neg_numeral b)" |
|
2136 "neg_numeral a XOR numeral b = word_of_int (neg_numeral a XOR numeral b)" |
|
2137 "neg_numeral a XOR neg_numeral b = word_of_int (neg_numeral a XOR neg_numeral b)" |
|
2138 unfolding word_numeral_alt word_neg_numeral_alt word_wi_log_defs by simp_all |
2081 |
2139 |
2082 text {* Special cases for when one of the arguments equals 1. *} |
2140 text {* Special cases for when one of the arguments equals 1. *} |
2083 |
2141 |
2084 lemma word_bitwise_1_simps [simp]: |
2142 lemma word_bitwise_1_simps [simp]: |
2085 "NOT (1::'a::len0 word) = -2" |
2143 "NOT (1::'a::len0 word) = -2" |
2086 "(1::'a word) AND number_of b = number_of (Int.Bit1 Int.Pls AND b)" |
2144 "1 AND numeral b = word_of_int (1 AND numeral b)" |
2087 "number_of a AND (1::'a word) = number_of (a AND Int.Bit1 Int.Pls)" |
2145 "1 AND neg_numeral b = word_of_int (1 AND neg_numeral b)" |
2088 "(1::'a word) OR number_of b = number_of (Int.Bit1 Int.Pls OR b)" |
2146 "numeral a AND 1 = word_of_int (numeral a AND 1)" |
2089 "number_of a OR (1::'a word) = number_of (a OR Int.Bit1 Int.Pls)" |
2147 "neg_numeral a AND 1 = word_of_int (neg_numeral a AND 1)" |
2090 "(1::'a word) XOR number_of b = number_of (Int.Bit1 Int.Pls XOR b)" |
2148 "1 OR numeral b = word_of_int (1 OR numeral b)" |
2091 "number_of a XOR (1::'a word) = number_of (a XOR Int.Bit1 Int.Pls)" |
2149 "1 OR neg_numeral b = word_of_int (1 OR neg_numeral b)" |
|
2150 "numeral a OR 1 = word_of_int (numeral a OR 1)" |
|
2151 "neg_numeral a OR 1 = word_of_int (neg_numeral a OR 1)" |
|
2152 "1 XOR numeral b = word_of_int (1 XOR numeral b)" |
|
2153 "1 XOR neg_numeral b = word_of_int (1 XOR neg_numeral b)" |
|
2154 "numeral a XOR 1 = word_of_int (numeral a XOR 1)" |
|
2155 "neg_numeral a XOR 1 = word_of_int (neg_numeral a XOR 1)" |
2092 unfolding word_1_no word_no_log_defs by simp_all |
2156 unfolding word_1_no word_no_log_defs by simp_all |
2093 |
2157 |
2094 lemma uint_or: "uint (x OR y) = (uint x) OR (uint y)" |
2158 lemma uint_or: "uint (x OR y) = (uint x) OR (uint y)" |
2095 by (simp add: word_or_def word_wi_log_defs word_ubin.eq_norm |
2159 by (simp add: word_or_def word_wi_log_defs word_ubin.eq_norm |
2096 bin_trunc_ao(2) [symmetric]) |
2160 bin_trunc_ao(2) [symmetric]) |
2121 lemma test_bit_wi [simp]: |
2185 lemma test_bit_wi [simp]: |
2122 "(word_of_int x::'a::len0 word) !! n \<longleftrightarrow> n < len_of TYPE('a) \<and> bin_nth x n" |
2186 "(word_of_int x::'a::len0 word) !! n \<longleftrightarrow> n < len_of TYPE('a) \<and> bin_nth x n" |
2123 unfolding word_test_bit_def |
2187 unfolding word_test_bit_def |
2124 by (simp add: nth_bintr [symmetric] word_ubin.eq_norm) |
2188 by (simp add: nth_bintr [symmetric] word_ubin.eq_norm) |
2125 |
2189 |
2126 lemma test_bit_no [simp]: |
2190 lemma test_bit_numeral [simp]: |
2127 "(number_of w :: 'a::len0 word) !! n \<longleftrightarrow> |
2191 "(numeral w :: 'a::len0 word) !! n \<longleftrightarrow> |
2128 n < len_of TYPE('a) \<and> bin_nth (number_of w) n" |
2192 n < len_of TYPE('a) \<and> bin_nth (numeral w) n" |
2129 unfolding word_number_of_alt test_bit_wi .. |
2193 unfolding word_numeral_alt test_bit_wi .. |
|
2194 |
|
2195 lemma test_bit_neg_numeral [simp]: |
|
2196 "(neg_numeral w :: 'a::len0 word) !! n \<longleftrightarrow> |
|
2197 n < len_of TYPE('a) \<and> bin_nth (neg_numeral w) n" |
|
2198 unfolding word_neg_numeral_alt test_bit_wi .. |
2130 |
2199 |
2131 lemma test_bit_1 [simp]: "(1::'a::len word) !! n \<longleftrightarrow> n = 0" |
2200 lemma test_bit_1 [simp]: "(1::'a::len word) !! n \<longleftrightarrow> n = 0" |
2132 unfolding word_1_wi test_bit_wi by auto |
2201 unfolding word_1_wi test_bit_wi by auto |
2133 |
2202 |
2134 lemma nth_0 [simp]: "~ (0::'a::len0 word) !! n" |
2203 lemma nth_0 [simp]: "~ (0::'a::len0 word) !! n" |
2135 unfolding word_test_bit_def by simp |
2204 unfolding word_test_bit_def by simp |
|
2205 |
|
2206 lemma nth_minus1 [simp]: "(-1::'a::len0 word) !! n \<longleftrightarrow> n < len_of TYPE('a)" |
|
2207 unfolding word_test_bit_def by (simp add: nth_bintr) |
2136 |
2208 |
2137 (* get from commutativity, associativity etc of int_and etc |
2209 (* get from commutativity, associativity etc of int_and etc |
2138 to same for word_and etc *) |
2210 to same for word_and etc *) |
2139 |
2211 |
2140 lemmas bwsimps = |
2212 lemmas bwsimps = |
2292 |
2364 |
2293 lemma msb_word_of_int: |
2365 lemma msb_word_of_int: |
2294 "msb (word_of_int x::'a::len word) = bin_nth x (len_of TYPE('a) - 1)" |
2366 "msb (word_of_int x::'a::len word) = bin_nth x (len_of TYPE('a) - 1)" |
2295 unfolding word_msb_def by (simp add: word_sbin.eq_norm bin_sign_lem) |
2367 unfolding word_msb_def by (simp add: word_sbin.eq_norm bin_sign_lem) |
2296 |
2368 |
2297 lemma word_msb_no [simp]: |
2369 lemma word_msb_numeral [simp]: |
2298 "msb (number_of w::'a::len word) = bin_nth (number_of w) (len_of TYPE('a) - 1)" |
2370 "msb (numeral w::'a::len word) = bin_nth (numeral w) (len_of TYPE('a) - 1)" |
2299 unfolding word_number_of_alt by (rule msb_word_of_int) |
2371 unfolding word_numeral_alt by (rule msb_word_of_int) |
|
2372 |
|
2373 lemma word_msb_neg_numeral [simp]: |
|
2374 "msb (neg_numeral w::'a::len word) = bin_nth (neg_numeral w) (len_of TYPE('a) - 1)" |
|
2375 unfolding word_neg_numeral_alt by (rule msb_word_of_int) |
2300 |
2376 |
2301 lemma word_msb_0 [simp]: "\<not> msb (0::'a::len word)" |
2377 lemma word_msb_0 [simp]: "\<not> msb (0::'a::len word)" |
2302 unfolding word_msb_def by simp |
2378 unfolding word_msb_def by simp |
2303 |
2379 |
2304 lemma word_msb_1 [simp]: "msb (1::'a::len word) \<longleftrightarrow> len_of TYPE('a) = 1" |
2380 lemma word_msb_1 [simp]: "msb (1::'a::len word) \<longleftrightarrow> len_of TYPE('a) = 1" |
2418 defines "l \<equiv> len_of TYPE ('a)" |
2494 defines "l \<equiv> len_of TYPE ('a)" |
2419 shows "bin_nth (sint w) n = (if n < l - 1 then w !! n else w !! (l - 1))" |
2495 shows "bin_nth (sint w) n = (if n < l - 1 then w !! n else w !! (l - 1))" |
2420 unfolding sint_uint l_def |
2496 unfolding sint_uint l_def |
2421 by (clarsimp simp add: nth_sbintr word_test_bit_def [symmetric]) |
2497 by (clarsimp simp add: nth_sbintr word_test_bit_def [symmetric]) |
2422 |
2498 |
2423 lemma word_lsb_no [simp]: |
2499 lemma word_lsb_numeral [simp]: |
2424 "lsb (number_of bin :: 'a :: len word) = (bin_last (number_of bin) = 1)" |
2500 "lsb (numeral bin :: 'a :: len word) = (bin_last (numeral bin) = 1)" |
2425 unfolding word_lsb_alt test_bit_no by auto |
2501 unfolding word_lsb_alt test_bit_numeral by simp |
|
2502 |
|
2503 lemma word_lsb_neg_numeral [simp]: |
|
2504 "lsb (neg_numeral bin :: 'a :: len word) = (bin_last (neg_numeral bin) = 1)" |
|
2505 unfolding word_lsb_alt test_bit_neg_numeral by simp |
2426 |
2506 |
2427 lemma set_bit_word_of_int: |
2507 lemma set_bit_word_of_int: |
2428 "set_bit (word_of_int x) n b = word_of_int (bin_sc n (if b then 1 else 0) x)" |
2508 "set_bit (word_of_int x) n b = word_of_int (bin_sc n (if b then 1 else 0) x)" |
2429 unfolding word_set_bit_def |
2509 unfolding word_set_bit_def |
2430 apply (rule word_eqI) |
2510 apply (rule word_eqI) |
2431 apply (simp add: word_size bin_nth_sc_gen word_ubin.eq_norm nth_bintr) |
2511 apply (simp add: word_size bin_nth_sc_gen word_ubin.eq_norm nth_bintr) |
2432 done |
2512 done |
2433 |
2513 |
2434 lemma word_set_no [simp]: |
2514 lemma word_set_numeral [simp]: |
2435 "set_bit (number_of bin::'a::len0 word) n b = |
2515 "set_bit (numeral bin::'a::len0 word) n b = |
2436 word_of_int (bin_sc n (if b then 1 else 0) (number_of bin))" |
2516 word_of_int (bin_sc n (if b then 1 else 0) (numeral bin))" |
2437 unfolding word_number_of_alt by (rule set_bit_word_of_int) |
2517 unfolding word_numeral_alt by (rule set_bit_word_of_int) |
|
2518 |
|
2519 lemma word_set_neg_numeral [simp]: |
|
2520 "set_bit (neg_numeral bin::'a::len0 word) n b = |
|
2521 word_of_int (bin_sc n (if b then 1 else 0) (neg_numeral bin))" |
|
2522 unfolding word_neg_numeral_alt by (rule set_bit_word_of_int) |
2438 |
2523 |
2439 lemma word_set_bit_0 [simp]: |
2524 lemma word_set_bit_0 [simp]: |
2440 "set_bit 0 n b = word_of_int (bin_sc n (if b then 1 else 0) 0)" |
2525 "set_bit 0 n b = word_of_int (bin_sc n (if b then 1 else 0) 0)" |
2441 unfolding word_0_wi by (rule set_bit_word_of_int) |
2526 unfolding word_0_wi by (rule set_bit_word_of_int) |
2442 |
2527 |
2443 lemma word_set_bit_1 [simp]: |
2528 lemma word_set_bit_1 [simp]: |
2444 "set_bit 1 n b = word_of_int (bin_sc n (if b then 1 else 0) 1)" |
2529 "set_bit 1 n b = word_of_int (bin_sc n (if b then 1 else 0) 1)" |
2445 unfolding word_1_wi by (rule set_bit_word_of_int) |
2530 unfolding word_1_wi by (rule set_bit_word_of_int) |
2446 |
2531 |
2447 lemma setBit_no [simp]: |
2532 lemma setBit_no [simp]: |
2448 "setBit (number_of bin) n = word_of_int (bin_sc n 1 (number_of bin))" |
2533 "setBit (numeral bin) n = word_of_int (bin_sc n 1 (numeral bin))" |
2449 by (simp add: setBit_def) |
2534 by (simp add: setBit_def) |
2450 |
2535 |
2451 lemma clearBit_no [simp]: |
2536 lemma clearBit_no [simp]: |
2452 "clearBit (number_of bin) n = word_of_int (bin_sc n 0 (number_of bin))" |
2537 "clearBit (numeral bin) n = word_of_int (bin_sc n 0 (numeral bin))" |
2453 by (simp add: clearBit_def) |
2538 by (simp add: clearBit_def) |
2454 |
2539 |
2455 lemma to_bl_n1: |
2540 lemma to_bl_n1: |
2456 "to_bl (-1::'a::len0 word) = replicate (len_of TYPE ('a)) True" |
2541 "to_bl (-1::'a::len0 word) = replicate (len_of TYPE ('a)) True" |
2457 apply (rule word_bl.Abs_inverse') |
2542 apply (rule word_bl.Abs_inverse') |
2545 |
2630 |
2546 subsection {* Shifting, Rotating, and Splitting Words *} |
2631 subsection {* Shifting, Rotating, and Splitting Words *} |
2547 |
2632 |
2548 lemma shiftl1_wi [simp]: "shiftl1 (word_of_int w) = word_of_int (w BIT 0)" |
2633 lemma shiftl1_wi [simp]: "shiftl1 (word_of_int w) = word_of_int (w BIT 0)" |
2549 unfolding shiftl1_def |
2634 unfolding shiftl1_def |
2550 apply (simp only: word_ubin.norm_eq_iff [symmetric] word_ubin.eq_norm) |
2635 apply (simp add: word_ubin.norm_eq_iff [symmetric] word_ubin.eq_norm) |
2551 apply (subst refl [THEN bintrunc_BIT_I, symmetric]) |
2636 apply (subst refl [THEN bintrunc_BIT_I, symmetric]) |
2552 apply (subst bintrunc_bintrunc_min) |
2637 apply (subst bintrunc_bintrunc_min) |
2553 apply simp |
2638 apply simp |
2554 done |
2639 done |
2555 |
2640 |
2556 lemma shiftl1_number [simp] : |
2641 lemma shiftl1_numeral [simp]: |
2557 "shiftl1 (number_of w) = number_of (Int.Bit0 w)" |
2642 "shiftl1 (numeral w) = numeral (Num.Bit0 w)" |
2558 unfolding word_number_of_alt shiftl1_wi by simp |
2643 unfolding word_numeral_alt shiftl1_wi by simp |
|
2644 |
|
2645 lemma shiftl1_neg_numeral [simp]: |
|
2646 "shiftl1 (neg_numeral w) = neg_numeral (Num.Bit0 w)" |
|
2647 unfolding word_neg_numeral_alt shiftl1_wi by simp |
2559 |
2648 |
2560 lemma shiftl1_0 [simp] : "shiftl1 0 = 0" |
2649 lemma shiftl1_0 [simp] : "shiftl1 0 = 0" |
2561 unfolding shiftl1_def by simp |
2650 unfolding shiftl1_def by simp |
2562 |
2651 |
2563 lemma shiftl1_def_u: "shiftl1 w = word_of_int (uint w BIT 0)" |
2652 lemma shiftl1_def_u: "shiftl1 w = word_of_int (uint w BIT 0)" |
2702 zdiv_zmult2_eq [symmetric]) |
2791 zdiv_zmult2_eq [symmetric]) |
2703 done |
2792 done |
2704 |
2793 |
2705 subsubsection "shift functions in terms of lists of bools" |
2794 subsubsection "shift functions in terms of lists of bools" |
2706 |
2795 |
2707 lemmas bshiftr1_no_bin [simp] = |
2796 lemmas bshiftr1_numeral [simp] = |
2708 bshiftr1_def [where w="number_of w", unfolded to_bl_no_bin] for w |
2797 bshiftr1_def [where w="numeral w", unfolded to_bl_numeral] for w |
2709 |
2798 |
2710 lemma bshiftr1_bl: "to_bl (bshiftr1 b w) = b # butlast (to_bl w)" |
2799 lemma bshiftr1_bl: "to_bl (bshiftr1 b w) = b # butlast (to_bl w)" |
2711 unfolding bshiftr1_def by (rule word_bl.Abs_inverse) simp |
2800 unfolding bshiftr1_def by (rule word_bl.Abs_inverse) simp |
2712 |
2801 |
2713 lemma shiftl1_of_bl: "shiftl1 (of_bl bl) = of_bl (bl @ [False])" |
2802 lemma shiftl1_of_bl: "shiftl1 (of_bl bl) = of_bl (bl @ [False])" |
2883 lemma shiftl_t2n: "shiftl (w :: 'a :: len word) n = 2 ^ n * w" |
2972 lemma shiftl_t2n: "shiftl (w :: 'a :: len word) n = 2 ^ n * w" |
2884 unfolding shiftl_def |
2973 unfolding shiftl_def |
2885 by (induct n) (auto simp: shiftl1_2t) |
2974 by (induct n) (auto simp: shiftl1_2t) |
2886 |
2975 |
2887 lemma shiftr1_bintr [simp]: |
2976 lemma shiftr1_bintr [simp]: |
2888 "(shiftr1 (number_of w) :: 'a :: len0 word) = |
2977 "(shiftr1 (numeral w) :: 'a :: len0 word) = |
2889 word_of_int (bin_rest (bintrunc (len_of TYPE ('a)) (number_of w)))" |
2978 word_of_int (bin_rest (bintrunc (len_of TYPE ('a)) (numeral w)))" |
2890 unfolding shiftr1_def word_number_of_alt |
2979 unfolding shiftr1_def word_numeral_alt |
2891 by (simp add: word_ubin.eq_norm) |
2980 by (simp add: word_ubin.eq_norm) |
2892 |
2981 |
2893 lemma sshiftr1_sbintr [simp]: |
2982 lemma sshiftr1_sbintr [simp]: |
2894 "(sshiftr1 (number_of w) :: 'a :: len word) = |
2983 "(sshiftr1 (numeral w) :: 'a :: len word) = |
2895 word_of_int (bin_rest (sbintrunc (len_of TYPE ('a) - 1) (number_of w)))" |
2984 word_of_int (bin_rest (sbintrunc (len_of TYPE ('a) - 1) (numeral w)))" |
2896 unfolding sshiftr1_def word_number_of_alt |
2985 unfolding sshiftr1_def word_numeral_alt |
2897 by (simp add: word_sbin.eq_norm) |
2986 by (simp add: word_sbin.eq_norm) |
2898 |
2987 |
2899 lemma shiftr_no [simp]: |
2988 lemma shiftr_no [simp]: |
2900 "(number_of w::'a::len0 word) >> n = word_of_int |
2989 (* FIXME: neg_numeral *) |
2901 ((bin_rest ^^ n) (bintrunc (len_of TYPE('a)) (number_of w)))" |
2990 "(numeral w::'a::len0 word) >> n = word_of_int |
|
2991 ((bin_rest ^^ n) (bintrunc (len_of TYPE('a)) (numeral w)))" |
2902 apply (rule word_eqI) |
2992 apply (rule word_eqI) |
2903 apply (auto simp: nth_shiftr nth_rest_power_bin nth_bintr word_size) |
2993 apply (auto simp: nth_shiftr nth_rest_power_bin nth_bintr word_size) |
2904 done |
2994 done |
2905 |
2995 |
2906 lemma sshiftr_no [simp]: |
2996 lemma sshiftr_no [simp]: |
2907 "(number_of w::'a::len word) >>> n = word_of_int |
2997 (* FIXME: neg_numeral *) |
2908 ((bin_rest ^^ n) (sbintrunc (len_of TYPE('a) - 1) (number_of w)))" |
2998 "(numeral w::'a::len word) >>> n = word_of_int |
|
2999 ((bin_rest ^^ n) (sbintrunc (len_of TYPE('a) - 1) (numeral w)))" |
2909 apply (rule word_eqI) |
3000 apply (rule word_eqI) |
2910 apply (auto simp: nth_sshiftr nth_rest_power_bin nth_sbintr word_size) |
3001 apply (auto simp: nth_sshiftr nth_rest_power_bin nth_sbintr word_size) |
2911 apply (subgoal_tac "na + n = len_of TYPE('a) - Suc 0", simp, simp)+ |
3002 apply (subgoal_tac "na + n = len_of TYPE('a) - Suc 0", simp, simp)+ |
2912 done |
3003 done |
2913 |
3004 |
3071 apply (simp add : nat_mult_distrib nat_power_eq) |
3162 apply (simp add : nat_mult_distrib nat_power_eq) |
3072 done |
3163 done |
3073 |
3164 |
3074 lemma word_2p_lem: |
3165 lemma word_2p_lem: |
3075 "n < size w \<Longrightarrow> w < 2 ^ n = (uint (w :: 'a :: len word) < 2 ^ n)" |
3166 "n < size w \<Longrightarrow> w < 2 ^ n = (uint (w :: 'a :: len word) < 2 ^ n)" |
3076 apply (unfold word_size word_less_alt word_number_of_alt) |
3167 apply (unfold word_size word_less_alt word_numeral_alt) |
3077 apply (clarsimp simp add: word_of_int_power_hom word_uint.eq_norm |
3168 apply (clarsimp simp add: word_of_int_power_hom word_uint.eq_norm |
3078 int_mod_eq' |
3169 int_mod_eq' |
3079 simp del: word_of_int_bin) |
3170 simp del: word_of_int_numeral) |
3080 done |
3171 done |
3081 |
3172 |
3082 lemma less_mask_eq: "x < 2 ^ n \<Longrightarrow> x AND mask n = (x :: 'a :: len word)" |
3173 lemma less_mask_eq: "x < 2 ^ n \<Longrightarrow> x AND mask n = (x :: 'a :: len word)" |
3083 apply (unfold word_less_alt word_number_of_alt) |
3174 apply (unfold word_less_alt word_numeral_alt) |
3084 apply (clarsimp simp add: and_mask_mod_2p word_of_int_power_hom |
3175 apply (clarsimp simp add: and_mask_mod_2p word_of_int_power_hom |
3085 word_uint.eq_norm |
3176 word_uint.eq_norm |
3086 simp del: word_of_int_bin) |
3177 simp del: word_of_int_numeral) |
3087 apply (drule xtr8 [rotated]) |
3178 apply (drule xtr8 [rotated]) |
3088 apply (rule int_mod_le) |
3179 apply (rule int_mod_le) |
3089 apply (auto simp add : mod_pos_pos_trivial) |
3180 apply (auto simp add : mod_pos_pos_trivial) |
3090 done |
3181 done |
3091 |
3182 |
3124 |
3215 |
3125 subsubsection "Revcast" |
3216 subsubsection "Revcast" |
3126 |
3217 |
3127 lemmas revcast_def' = revcast_def [simplified] |
3218 lemmas revcast_def' = revcast_def [simplified] |
3128 lemmas revcast_def'' = revcast_def' [simplified word_size] |
3219 lemmas revcast_def'' = revcast_def' [simplified word_size] |
3129 lemmas revcast_no_def [simp] = revcast_def' [where w="number_of w", unfolded word_size] for w |
3220 lemmas revcast_no_def [simp] = revcast_def' [where w="numeral w", unfolded word_size] for w |
3130 |
3221 |
3131 lemma to_bl_revcast: |
3222 lemma to_bl_revcast: |
3132 "to_bl (revcast w :: 'a :: len0 word) = |
3223 "to_bl (revcast w :: 'a :: len0 word) = |
3133 takefill False (len_of TYPE ('a)) (to_bl w)" |
3224 takefill False (len_of TYPE ('a)) (to_bl w)" |
3134 apply (unfold revcast_def' word_size) |
3225 apply (unfold revcast_def' word_size) |
3238 |
3329 |
3239 |
3330 |
3240 subsubsection "Slices" |
3331 subsubsection "Slices" |
3241 |
3332 |
3242 lemma slice1_no_bin [simp]: |
3333 lemma slice1_no_bin [simp]: |
3243 "slice1 n (number_of w :: 'b word) = of_bl (takefill False n (bin_to_bl (len_of TYPE('b :: len0)) (number_of w)))" |
3334 "slice1 n (numeral w :: 'b word) = of_bl (takefill False n (bin_to_bl (len_of TYPE('b :: len0)) (numeral w)))" |
3244 by (simp add: slice1_def) |
3335 by (simp add: slice1_def) (* TODO: neg_numeral *) |
3245 |
3336 |
3246 lemma slice_no_bin [simp]: |
3337 lemma slice_no_bin [simp]: |
3247 "slice n (number_of w :: 'b word) = of_bl (takefill False (len_of TYPE('b :: len0) - n) |
3338 "slice n (numeral w :: 'b word) = of_bl (takefill False (len_of TYPE('b :: len0) - n) |
3248 (bin_to_bl (len_of TYPE('b :: len0)) (number_of w)))" |
3339 (bin_to_bl (len_of TYPE('b :: len0)) (numeral w)))" |
3249 by (simp add: slice_def word_size) |
3340 by (simp add: slice_def word_size) (* TODO: neg_numeral *) |
3250 |
3341 |
3251 lemma slice1_0 [simp] : "slice1 n 0 = 0" |
3342 lemma slice1_0 [simp] : "slice1 n 0 = 0" |
3252 unfolding slice1_def by simp |
3343 unfolding slice1_def by simp |
3253 |
3344 |
3254 lemma slice_0 [simp] : "slice n 0 = 0" |
3345 lemma slice_0 [simp] : "slice n 0 = 0" |
3381 |
3472 |
3382 lemmas word_split_bin' = word_split_def |
3473 lemmas word_split_bin' = word_split_def |
3383 lemmas word_cat_bin' = word_cat_def |
3474 lemmas word_cat_bin' = word_cat_def |
3384 |
3475 |
3385 lemma word_rsplit_no: |
3476 lemma word_rsplit_no: |
3386 "(word_rsplit (number_of bin :: 'b :: len0 word) :: 'a word list) = |
3477 "(word_rsplit (numeral bin :: 'b :: len0 word) :: 'a word list) = |
3387 map word_of_int (bin_rsplit (len_of TYPE('a :: len)) |
3478 map word_of_int (bin_rsplit (len_of TYPE('a :: len)) |
3388 (len_of TYPE('b), bintrunc (len_of TYPE('b)) (number_of bin)))" |
3479 (len_of TYPE('b), bintrunc (len_of TYPE('b)) (numeral bin)))" |
3389 unfolding word_rsplit_def by (simp add: word_ubin.eq_norm) |
3480 unfolding word_rsplit_def by (simp add: word_ubin.eq_norm) |
3390 |
3481 |
3391 lemmas word_rsplit_no_cl [simp] = word_rsplit_no |
3482 lemmas word_rsplit_no_cl [simp] = word_rsplit_no |
3392 [unfolded bin_rsplitl_def bin_rsplit_l [symmetric]] |
3483 [unfolded bin_rsplitl_def bin_rsplit_l [symmetric]] |
3393 |
3484 |
3578 apply safe |
3669 apply safe |
3579 apply (rule word_eqI, clarsimp simp: test_bit_cat word_size)+ |
3670 apply (rule word_eqI, clarsimp simp: test_bit_cat word_size)+ |
3580 done |
3671 done |
3581 |
3672 |
3582 lemmas word_cat_bl_no_bin [simp] = |
3673 lemmas word_cat_bl_no_bin [simp] = |
3583 word_cat_bl [where a="number_of a" |
3674 word_cat_bl [where a="numeral a" and b="numeral b", |
3584 and b="number_of b", |
3675 unfolded to_bl_numeral] |
3585 unfolded to_bl_no_bin] |
3676 for a b (* FIXME: negative numerals, 0 and 1 *) |
3586 for a b |
|
3587 |
3677 |
3588 lemmas word_split_bl_no_bin [simp] = |
3678 lemmas word_split_bl_no_bin [simp] = |
3589 word_split_bl_eq [where c="number_of c", unfolded to_bl_no_bin] for c |
3679 word_split_bl_eq [where c="numeral c", unfolded to_bl_numeral] for c |
3590 |
3680 |
3591 -- {* this odd result arises from the fact that the statement of the |
3681 text {* this odd result arises from the fact that the statement of the |
3592 result implies that the decoded words are of the same type, |
3682 result implies that the decoded words are of the same type, |
3593 and therefore of the same length, as the original word *} |
3683 and therefore of the same length, as the original word *} |
3594 |
3684 |
3595 lemma word_rsplit_same: "word_rsplit w = [w]" |
3685 lemma word_rsplit_same: "word_rsplit w = [w]" |
3596 unfolding word_rsplit_def by (simp add : bin_rsplit_all) |
3686 unfolding word_rsplit_def by (simp add : bin_rsplit_all) |
4091 |
4181 |
4092 lemma word_roti_0' [simp] : "word_roti n 0 = 0" |
4182 lemma word_roti_0' [simp] : "word_roti n 0 = 0" |
4093 unfolding word_roti_def by auto |
4183 unfolding word_roti_def by auto |
4094 |
4184 |
4095 lemmas word_rotr_dt_no_bin' [simp] = |
4185 lemmas word_rotr_dt_no_bin' [simp] = |
4096 word_rotr_dt [where w="number_of w", unfolded to_bl_no_bin] for w |
4186 word_rotr_dt [where w="numeral w", unfolded to_bl_numeral] for w |
|
4187 (* FIXME: negative numerals, 0 and 1 *) |
4097 |
4188 |
4098 lemmas word_rotl_dt_no_bin' [simp] = |
4189 lemmas word_rotl_dt_no_bin' [simp] = |
4099 word_rotl_dt [where w="number_of w", unfolded to_bl_no_bin] for w |
4190 word_rotl_dt [where w="numeral w", unfolded to_bl_numeral] for w |
|
4191 (* FIXME: negative numerals, 0 and 1 *) |
4100 |
4192 |
4101 declare word_roti_def [simp] |
4193 declare word_roti_def [simp] |
4102 |
4194 |
4103 |
4195 |
4104 subsection {* Maximum machine word *} |
4196 subsection {* Maximum machine word *} |
4117 lemma max_word_max [simp,intro!]: "n \<le> max_word" |
4209 lemma max_word_max [simp,intro!]: "n \<le> max_word" |
4118 by (cases n rule: word_int_cases) |
4210 by (cases n rule: word_int_cases) |
4119 (simp add: max_word_def word_le_def int_word_uint int_mod_eq') |
4211 (simp add: max_word_def word_le_def int_word_uint int_mod_eq') |
4120 |
4212 |
4121 lemma word_of_int_2p_len: "word_of_int (2 ^ len_of TYPE('a)) = (0::'a::len0 word)" |
4213 lemma word_of_int_2p_len: "word_of_int (2 ^ len_of TYPE('a)) = (0::'a::len0 word)" |
4122 by (subst word_uint.Abs_norm [symmetric]) |
4214 by (subst word_uint.Abs_norm [symmetric]) simp |
4123 (simp add: word_of_int_hom_syms) |
|
4124 |
4215 |
4125 lemma word_pow_0: |
4216 lemma word_pow_0: |
4126 "(2::'a::len word) ^ len_of TYPE('a) = 0" |
4217 "(2::'a::len word) ^ len_of TYPE('a) = 0" |
4127 proof - |
4218 proof - |
4128 have "word_of_int (2 ^ len_of TYPE('a)) = (0::'a word)" |
4219 have "word_of_int (2 ^ len_of TYPE('a)) = (0::'a word)" |
4333 |
4421 |
4334 lemma unat_sub: |
4422 lemma unat_sub: |
4335 "b <= a \<Longrightarrow> unat (a - b) = unat a - unat b" |
4423 "b <= a \<Longrightarrow> unat (a - b) = unat a - unat b" |
4336 by (simp add: unat_def uint_sub_if_size word_le_def nat_diff_distrib) |
4424 by (simp add: unat_def uint_sub_if_size word_le_def nat_diff_distrib) |
4337 |
4425 |
4338 lemmas word_less_sub1_numberof [simp] = word_less_sub1 [of "number_of w"] for w |
4426 lemmas word_less_sub1_numberof [simp] = word_less_sub1 [of "numeral w"] for w |
4339 lemmas word_le_sub1_numberof [simp] = word_le_sub1 [of "number_of w"] for w |
4427 lemmas word_le_sub1_numberof [simp] = word_le_sub1 [of "numeral w"] for w |
4340 |
4428 |
4341 lemma word_of_int_minus: |
4429 lemma word_of_int_minus: |
4342 "word_of_int (2^len_of TYPE('a) - i) = (word_of_int (-i)::'a::len word)" |
4430 "word_of_int (2^len_of TYPE('a) - i) = (word_of_int (-i)::'a::len word)" |
4343 proof - |
4431 proof - |
4344 have x: "2^len_of TYPE('a) - i = -i + 2^len_of TYPE('a)" by simp |
4432 have x: "2^len_of TYPE('a) - i = -i + 2^len_of TYPE('a)" by simp |
4352 lemmas word_of_int_inj = |
4440 lemmas word_of_int_inj = |
4353 word_uint.Abs_inject [unfolded uints_num, simplified] |
4441 word_uint.Abs_inject [unfolded uints_num, simplified] |
4354 |
4442 |
4355 lemma word_le_less_eq: |
4443 lemma word_le_less_eq: |
4356 "(x ::'z::len word) \<le> y = (x = y \<or> x < y)" |
4444 "(x ::'z::len word) \<le> y = (x = y \<or> x < y)" |
4357 by (auto simp add: word_less_def) |
4445 by (auto simp add: order_class.le_less) |
4358 |
4446 |
4359 lemma mod_plus_cong: |
4447 lemma mod_plus_cong: |
4360 assumes 1: "(b::int) = b'" |
4448 assumes 1: "(b::int) = b'" |
4361 and 2: "x mod b' = x' mod b'" |
4449 and 2: "x mod b' = x' mod b'" |
4362 and 3: "y mod b' = y' mod b'" |
4450 and 3: "y mod b' = y' mod b'" |