| author | wenzelm | 
| Wed, 27 May 2020 16:09:25 +0200 | |
| changeset 71908 | 0dc67ae4a4c7 | 
| parent 70172 | c247bf924d25 | 
| child 72515 | c7038c397ae3 | 
| permissions | -rw-r--r-- | 
| 41561 | 1 | (* Title: HOL/SPARK/SPARK_Setup.thy | 
| 2 | Author: Stefan Berghofer | |
| 3 | Copyright: secunet Security Networks AG | |
| 4 | ||
| 5 | Setup for SPARK/Ada verification environment. | |
| 6 | *) | |
| 7 | ||
| 8 | theory SPARK_Setup | |
| 70172 | 9 | imports | 
| 10 | "HOL-Word.Word" | |
| 46950 
d0181abdbdac
declare command keywords via theory header, including strict checking outside Pure;
 wenzelm parents: 
42416diff
changeset | 11 | keywords | 
| 48908 
713f24d7a40f
added specific 'spark_open_vcg' and 'spark_open_siv' with formal management of corresponding source files;
 wenzelm parents: 
48891diff
changeset | 12 |   "spark_open_vcg" :: thy_load ("vcg", "fdl", "rls") and
 | 
| 56798 
939e88e79724
Discontinued old spark_open; spark_open_siv is now spark_open
 berghofe parents: 
55789diff
changeset | 13 |   "spark_open" :: thy_load ("siv", "fdl", "rls") and
 | 
| 
939e88e79724
Discontinued old spark_open; spark_open_siv is now spark_open
 berghofe parents: 
55789diff
changeset | 14 | "spark_proof_functions" "spark_types" "spark_end" :: thy_decl and | 
| 63432 | 15 | "spark_vc" :: thy_goal and | 
| 16 | "spark_status" :: diag | |
| 41561 | 17 | begin | 
| 18 | ||
| 69605 | 19 | ML_file \<open>Tools/fdl_lexer.ML\<close> | 
| 20 | ML_file \<open>Tools/fdl_parser.ML\<close> | |
| 48891 | 21 | |
| 63167 | 22 | text \<open> | 
| 41635 
f938a6022d2e
Replaced smod by standard mod operator to reflect actual behaviour
 berghofe parents: 
41561diff
changeset | 23 | SPARK version of div, see section 4.4.1.1 of SPARK Proof Manual | 
| 63167 | 24 | \<close> | 
| 41561 | 25 | |
| 26 | definition sdiv :: "int \<Rightarrow> int \<Rightarrow> int" (infixl "sdiv" 70) where | |
| 41637 | 27 | "a sdiv b = sgn a * sgn b * (\<bar>a\<bar> div \<bar>b\<bar>)" | 
| 41561 | 28 | |
| 29 | lemma sdiv_minus_dividend: "- a sdiv b = - (a sdiv b)" | |
| 41637 | 30 | by (simp add: sdiv_def sgn_if) | 
| 41561 | 31 | |
| 32 | lemma sdiv_minus_divisor: "a sdiv - b = - (a sdiv b)" | |
| 41637 | 33 | by (simp add: sdiv_def sgn_if) | 
| 41561 | 34 | |
| 63167 | 35 | text \<open> | 
| 41635 
f938a6022d2e
Replaced smod by standard mod operator to reflect actual behaviour
 berghofe parents: 
41561diff
changeset | 36 | Correspondence between HOL's and SPARK's version of div | 
| 63167 | 37 | \<close> | 
| 41561 | 38 | |
| 39 | lemma sdiv_pos_pos: "0 \<le> a \<Longrightarrow> 0 \<le> b \<Longrightarrow> a sdiv b = a div b" | |
| 41637 | 40 | by (simp add: sdiv_def sgn_if) | 
| 41561 | 41 | |
| 42 | lemma sdiv_pos_neg: "0 \<le> a \<Longrightarrow> b < 0 \<Longrightarrow> a sdiv b = - (a div - b)" | |
| 41637 | 43 | by (simp add: sdiv_def sgn_if) | 
| 41561 | 44 | |
| 45 | lemma sdiv_neg_pos: "a < 0 \<Longrightarrow> 0 \<le> b \<Longrightarrow> a sdiv b = - (- a div b)" | |
| 41637 | 46 | by (simp add: sdiv_def sgn_if) | 
| 41561 | 47 | |
| 48 | lemma sdiv_neg_neg: "a < 0 \<Longrightarrow> b < 0 \<Longrightarrow> a sdiv b = - a div - b" | |
| 41637 | 49 | by (simp add: sdiv_def sgn_if) | 
| 41561 | 50 | |
| 51 | ||
| 63167 | 52 | text \<open> | 
| 41561 | 53 | Updating a function at a set of points. Useful for building arrays. | 
| 63167 | 54 | \<close> | 
| 41561 | 55 | |
| 56 | definition fun_upds :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a set \<Rightarrow> 'b \<Rightarrow> 'a \<Rightarrow> 'b" where
 | |
| 57 | "fun_upds f xs y z = (if z \<in> xs then y else f z)" | |
| 58 | ||
| 59 | syntax | |
| 60 |   "_updsbind" :: "['a, 'a] => updbind"             ("(2_ [:=]/ _)")
 | |
| 61 | ||
| 62 | translations | |
| 63 | "f(xs[:=]y)" == "CONST fun_upds f xs y" | |
| 64 | ||
| 65 | lemma fun_upds_in [simp]: "z \<in> xs \<Longrightarrow> (f(xs [:=] y)) z = y" | |
| 66 | by (simp add: fun_upds_def) | |
| 67 | ||
| 68 | lemma fun_upds_notin [simp]: "z \<notin> xs \<Longrightarrow> (f(xs [:=] y)) z = f z" | |
| 69 | by (simp add: fun_upds_def) | |
| 70 | ||
| 71 | lemma upds_singleton [simp]: "f({x} [:=] y) = f(x := y)"
 | |
| 72 | by (simp add: fun_eq_iff) | |
| 73 | ||
| 74 | ||
| 63167 | 75 | text \<open>Enumeration types\<close> | 
| 41561 | 76 | |
| 42416 
a8a9f4d79196
- renamed enum type class to spark_enum, to avoid confusion with
 berghofe parents: 
41637diff
changeset | 77 | class spark_enum = ord + finite + | 
| 41561 | 78 | fixes pos :: "'a \<Rightarrow> int" | 
| 79 |   assumes range_pos: "range pos = {0..<int (card (UNIV::'a set))}"
 | |
| 80 | and less_pos: "(x < y) = (pos x < pos y)" | |
| 81 | and less_eq_pos: "(x \<le> y) = (pos x \<le> pos y)" | |
| 82 | begin | |
| 83 | ||
| 84 | definition "val = inv pos" | |
| 85 | ||
| 86 | definition "succ x = val (pos x + 1)" | |
| 87 | ||
| 88 | definition "pred x = val (pos x - 1)" | |
| 89 | ||
| 90 | lemma inj_pos: "inj pos" | |
| 91 | using finite_UNIV | |
| 92 | by (rule eq_card_imp_inj_on) (simp add: range_pos) | |
| 93 | ||
| 94 | lemma val_pos: "val (pos x) = x" | |
| 95 | unfolding val_def using inj_pos | |
| 96 | by (rule inv_f_f) | |
| 97 | ||
| 98 | lemma pos_val: "z \<in> range pos \<Longrightarrow> pos (val z) = z" | |
| 99 | unfolding val_def | |
| 100 | by (rule f_inv_into_f) | |
| 101 | ||
| 102 | subclass linorder | |
| 103 | proof | |
| 104 | fix x::'a and y show "(x < y) = (x \<le> y \<and> \<not> y \<le> x)" | |
| 105 | by (simp add: less_pos less_eq_pos less_le_not_le) | |
| 106 | next | |
| 107 | fix x::'a show "x \<le> x" by (simp add: less_eq_pos) | |
| 108 | next | |
| 109 | fix x::'a and y z assume "x \<le> y" and "y \<le> z" | |
| 110 | then show "x \<le> z" by (simp add: less_eq_pos) | |
| 111 | next | |
| 112 | fix x::'a and y assume "x \<le> y" and "y \<le> x" | |
| 113 | with inj_pos show "x = y" | |
| 114 | by (auto dest: injD simp add: less_eq_pos) | |
| 115 | next | |
| 116 | fix x::'a and y show "x \<le> y \<or> y \<le> x" | |
| 117 | by (simp add: less_eq_pos linear) | |
| 118 | qed | |
| 119 | ||
| 120 | definition "first_el = val 0" | |
| 121 | ||
| 122 | definition "last_el = val (int (card (UNIV::'a set)) - 1)" | |
| 123 | ||
| 124 | lemma first_el_smallest: "first_el \<le> x" | |
| 125 | proof - | |
| 126 | have "pos x \<in> range pos" by (rule rangeI) | |
| 127 | then have "pos (val 0) \<le> pos x" | |
| 128 | by (simp add: range_pos pos_val) | |
| 129 | then show ?thesis by (simp add: first_el_def less_eq_pos) | |
| 130 | qed | |
| 131 | ||
| 132 | lemma last_el_greatest: "x \<le> last_el" | |
| 133 | proof - | |
| 134 | have "pos x \<in> range pos" by (rule rangeI) | |
| 135 | then have "pos x \<le> pos (val (int (card (UNIV::'a set)) - 1))" | |
| 136 | by (simp add: range_pos pos_val) | |
| 137 | then show ?thesis by (simp add: last_el_def less_eq_pos) | |
| 138 | qed | |
| 139 | ||
| 140 | lemma pos_succ: | |
| 141 | assumes "x \<noteq> last_el" | |
| 142 | shows "pos (succ x) = pos x + 1" | |
| 143 | proof - | |
| 144 | have "x \<le> last_el" by (rule last_el_greatest) | |
| 145 | with assms have "x < last_el" by simp | |
| 146 | then have "pos x < pos last_el" | |
| 147 | by (simp add: less_pos) | |
| 148 | with rangeI [of pos x] | |
| 149 | have "pos x + 1 \<in> range pos" | |
| 150 | by (simp add: range_pos last_el_def pos_val) | |
| 151 | then show ?thesis | |
| 152 | by (simp add: succ_def pos_val) | |
| 153 | qed | |
| 154 | ||
| 155 | lemma pos_pred: | |
| 156 | assumes "x \<noteq> first_el" | |
| 157 | shows "pos (pred x) = pos x - 1" | |
| 158 | proof - | |
| 159 | have "first_el \<le> x" by (rule first_el_smallest) | |
| 160 | with assms have "first_el < x" by simp | |
| 161 | then have "pos first_el < pos x" | |
| 162 | by (simp add: less_pos) | |
| 163 | with rangeI [of pos x] | |
| 164 | have "pos x - 1 \<in> range pos" | |
| 165 | by (simp add: range_pos first_el_def pos_val) | |
| 166 | then show ?thesis | |
| 167 | by (simp add: pred_def pos_val) | |
| 168 | qed | |
| 169 | ||
| 170 | lemma succ_val: "x \<in> range pos \<Longrightarrow> succ (val x) = val (x + 1)" | |
| 171 | by (simp add: succ_def pos_val) | |
| 172 | ||
| 173 | lemma pred_val: "x \<in> range pos \<Longrightarrow> pred (val x) = val (x - 1)" | |
| 174 | by (simp add: pred_def pos_val) | |
| 175 | ||
| 176 | end | |
| 177 | ||
| 178 | lemma interval_expand: | |
| 179 |   "x < y \<Longrightarrow> (z::int) \<in> {x..<y} = (z = x \<or> z \<in> {x+1..<y})"
 | |
| 180 | by auto | |
| 181 | ||
| 182 | ||
| 63167 | 183 | text \<open>Load the package\<close> | 
| 41561 | 184 | |
| 69605 | 185 | ML_file \<open>Tools/spark_vcs.ML\<close> | 
| 186 | ML_file \<open>Tools/spark_commands.ML\<close> | |
| 41561 | 187 | |
| 188 | end |