~james/span

ref: 0c6f163a0c6d0b333b095d5f0b629b1709d3bff4 span/src/Model.thy -rw-r--r-- 1.9 KiB
0c6f163a — james chore(import): snapshot james.dad.town/span at 868a68649c22 14 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
theory Model
  imports Main
begin

datatype 'a instruction = Nop | Inc | Dec | Stop | Jump 'a | Zero 'a

fun target :: "'a instruction \<Rightarrow> 'a option" where
  "target (Jump t) = Some t"
| "target (Zero t) = Some t"
| "target _ = None"

definition branches :: "nat instruction list \<Rightarrow> nat set" where
  "branches p = {i. i < length p \<and> target (p ! i) \<noteq> None}"

definition destination :: "nat instruction list \<Rightarrow> nat \<Rightarrow> nat" where
  "destination p i = the (target (p ! i))"

definition wellformed :: "nat instruction list \<Rightarrow> bool" where
  "wellformed p \<longleftrightarrow> (\<forall>i \<in> branches p. destination p i < length p)"

lemma finite_branches [simp]: "finite (branches p)"
  unfolding branches_def by (rule finite_subset[of _ "{..<length p}"]) auto

lemma branch_index: "i \<in> branches p \<Longrightarrow> i < length p"
  by (simp add: branches_def)

lemma branches_code [code]:
  "branches p = set (map fst (filter (\<lambda>(i,op). target op \<noteq> None) (zip [0..<length p] p)))"
  by (auto simp: branches_def set_zip image_iff; force)

fun advance :: "nat instruction \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> (nat \<times> 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 \<Rightarrow> nat \<times> nat \<Rightarrow> (nat \<times> 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 \<Rightarrow> 's option) \<Rightarrow> nat \<Rightarrow> 's \<Rightarrow> 's" where
  "execute step 0 s = s"
| "execute step (Suc n) s = (case step s of None \<Rightarrow> s | Some t \<Rightarrow> execute step n t)"

end