theory Layout imports Model begin definition width :: "nat instruction list \ nat set \ nat \ nat" where "width p W i = (if i \ branches p then if i \ W then 3 else 2 else 1)" definition address :: "nat instruction list \ nat set \ nat \ nat" where "address p W i = (\j nat set \ nat \ nat" where "distance p W i = (let t = destination p i in if i < t then (\j\{Suc i..j\{t.. nat set \ nat \ nat \ bool" where "fits p W i bits \ (if i < destination p i then distance p W i < 2 ^ (bits - 1) else distance p W i \ 2 ^ (bits - 1))" definition displacement :: "nat instruction list \ nat set \ nat \ int" where "displacement p W i = int (address p W (destination p i)) - int (address p W (Suc i))" definition valid :: "nat instruction list \ nat set \ bool" where "valid p W \ W \ branches p \ (\i\branches p. fits p W i (if i \ W then 16 else 8))" lemma width_positive: "0 < width p W i" by (simp add: width_def) lemma width_code [code]: "width p W i = (if i < length p \ target (p ! i) \ None then if i \ W then 3 else 2 else 1)" by (simp add: width_def branches_def) definition sizes :: "nat instruction list \ nat set \ nat list" where "sizes p W = map (\(i,op). if target op = None then 1 else if i \ W then 3 else 2) (zip [0.. sizes p W ! i = width p W i" by (simp add: sizes_def width_def branches_def) lemma width_mono: "W \ V \ width p W i \ width p V i" by (auto simp: width_def) lemma address_zero [simp]: "address p W 0 = 0" by (simp add: address_def) lemma address_suc [simp]: "address p W (Suc i) = address p W i + width p W i" by (simp add: address_def) lemma address_code [code]: "address p W i = sum_list (take i (sizes p W)) + (i - length p)" proof (induction i) case 0 then show ?case by simp next case (Suc i) show ?case proof (cases "i < length p") case True with Suc show ?thesis by (simp add: take_Suc_conv_app_nth sizes_nth) next case False with Suc show ?thesis by (simp add: width_def branches_def Suc_diff_le) qed qed lemma address_mono: "W \ V \ address p W i \ address p V i" unfolding address_def by (intro sum_mono width_mono) assumption lemma distance_mono: "W \ V \ distance p W i \ distance p V i" unfolding distance_def Let_def by (auto simp only: if_True if_False intro!: sum_mono width_mono split: if_splits) lemma fits_downward: "W \ V \ fits p V i bits \ fits p W i bits" using distance_mono[of W V p i] unfolding fits_def by auto lemma prefix_interval: fixes f :: "nat \ nat" assumes "a \ b" shows "(\jjj\{a.. {a.. {a.. destination p i" by simp have eq: "int (\jjj\{Suc i.. Suc i" by simp have eq: "int (\jjj\{destination p i.. - int (2 ^ (bits - 1)) \ displacement p W i \ displacement p W i < int (2 ^ (bits - 1))" proof - have positive: "(0::int) < int (2 ^ (bits - 1) :: nat)" by simp have nonnegative: "(0::int) \ int (distance p W i)" by simp have lower: "- int (2 ^ (bits - 1)) \ int (distance p W i)" using positive nonnegative by linarith have upper: "- int (distance p W i) < int (2 ^ (bits - 1))" using positive nonnegative by linarith show ?thesis by (cases "i < destination p i") (simp_all only: fits_def displacement_distance if_True if_False lower upper of_nat_less_iff of_nat_le_iff neg_le_iff_le simp_thms) qed lemma address_strict: assumes "i < j" shows "address p W i < address p W j" proof - have pos: "0 < (\k\{i..