theory Model imports Main begin datatype 'a instruction = Nop | Inc | Dec | Stop | Jump 'a | Zero 'a fun target :: "'a instruction \ 'a option" where "target (Jump t) = Some t" | "target (Zero t) = Some t" | "target _ = None" definition branches :: "nat instruction list \ nat set" where "branches p = {i. i < length p \ target (p ! i) \ None}" definition destination :: "nat instruction list \ nat \ nat" where "destination p i = the (target (p ! i))" definition wellformed :: "nat instruction list \ bool" where "wellformed p \ (\i \ branches p. destination p i < length p)" lemma finite_branches [simp]: "finite (branches p)" unfolding branches_def by (rule finite_subset[of _ "{.. branches p \ i < length p" by (simp add: branches_def) lemma branches_code [code]: "branches p = set (map fst (filter (\(i,op). target op \ None) (zip [0.. nat \ nat \ (nat \ nat) option" where "advance Nop i a = Some (Suc i, a)" | "advance Inc i a = Some (Suc i, Suc a)" | "advance Dec i a = Some (Suc i, a - 1)" | "advance Stop i a = None" | "advance (Jump t) i a = Some (t, a)" | "advance (Zero t) i a = Some (if a = 0 then t else Suc i, a)" definition source_step :: "nat instruction list \ nat \ nat \ (nat \ nat) option" where "source_step p s = (if fst s < length p then advance (p ! fst s) (fst s) (snd s) else None)" fun execute :: "('s \ 's option) \ nat \ 's \ 's" where "execute step 0 s = s" | "execute step (Suc n) s = (case step s of None \ s | Some t \ execute step n t)" end