author | Christian Urban <christian.urban@kcl.ac.uk> |
Thu, 22 Feb 2024 13:38:10 +0000 | |
changeset 298 | ac5461882f3e |
parent 268 | 002b07ea0a57 |
permissions | -rw-r--r-- |
268
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
1 |
theory Abacus |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
2 |
imports Main |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
3 |
begin |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
4 |
|
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
5 |
datatype abc_inst = |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
6 |
Inc nat |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
7 |
| Dec nat nat |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
8 |
| Goto nat |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
9 |
|
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
10 |
type_synonym abc_prog = "abc_inst list" |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
11 |
|
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
12 |
type_synonym abc_state = nat |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
13 |
|
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
14 |
text {* |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
15 |
The memory of Abacus machine is defined as a list of contents, with |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
16 |
every units addressed by index into the list. |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
17 |
*} |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
18 |
type_synonym abc_lm = "nat list" |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
19 |
|
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
20 |
text {* |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
21 |
Fetching contents out of memory. Units not represented by list elements are considered |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
22 |
as having content @{text "0"}. |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
23 |
*} |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
24 |
fun abc_lm_v :: "abc_lm \<Rightarrow> nat \<Rightarrow> nat" |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
25 |
where |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
26 |
"abc_lm_v lm n = (if n < length lm then lm ! n else 0)" |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
27 |
|
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
28 |
|
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
29 |
text {* |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
30 |
Set the content of memory unit @{text "n"} to value @{text "v"}. |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
31 |
@{text "am"} is the Abacus memory before setting. |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
32 |
If address @{text "n"} is outside to scope of @{text "am"}, @{text "am"} |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
33 |
is extended so that @{text "n"} becomes in scope. |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
34 |
*} |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
35 |
|
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
36 |
fun abc_lm_s :: "abc_lm \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> abc_lm" |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
37 |
where |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
38 |
"abc_lm_s am n v = (if n < length am then am[n := v] else |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
39 |
am @ (replicate (n - length am) 0) @ [v])" |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
40 |
|
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
41 |
|
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
42 |
text {* |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
43 |
The configuration of Abaucs machines consists of its current state and its |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
44 |
current memory: |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
45 |
*} |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
46 |
|
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
47 |
type_synonym abc_conf = "abc_state \<times> abc_lm" |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
48 |
|
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
49 |
text {* |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
50 |
Fetch instruction out of Abacus program: |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
51 |
*} |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
52 |
|
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
53 |
fun abc_fetch :: "nat \<Rightarrow> abc_prog \<Rightarrow> abc_inst option" |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
54 |
where |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
55 |
"abc_fetch s p = (if s < length p then Some (p ! s) else None)" |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
56 |
|
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
57 |
text {* |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
58 |
Single step execution of Abacus machine. If no instruction is feteched, |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
59 |
configuration does not change. |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
60 |
*} |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
61 |
|
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
62 |
fun abc_step_l :: "abc_conf \<Rightarrow> abc_inst option \<Rightarrow> abc_conf" |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
63 |
where |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
64 |
"abc_step_l (s, lm) a = (case a of |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
65 |
None \<Rightarrow> (s, lm) | |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
66 |
Some (Inc n) \<Rightarrow> (let nv = abc_lm_v lm n in |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
67 |
(s + 1, abc_lm_s lm n (nv + 1))) | |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
68 |
Some (Dec n e) \<Rightarrow> (let nv = abc_lm_v lm n in |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
69 |
if (nv = 0) then (e, abc_lm_s lm n 0) |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
70 |
else (s + 1, abc_lm_s lm n (nv - 1))) | |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
71 |
Some (Goto n) \<Rightarrow> (n, lm) |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
72 |
)" |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
73 |
|
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
74 |
text {* |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
75 |
Multi-step execution of Abacus machine. |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
76 |
*} |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
77 |
fun abc_steps_l :: "abc_conf \<Rightarrow> abc_prog \<Rightarrow> nat \<Rightarrow> abc_conf" |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
78 |
where |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
79 |
"abc_steps_l (s, lm) p 0 = (s, lm)" | |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
80 |
"abc_steps_l (s, lm) p (Suc n) = |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
81 |
abc_steps_l (abc_step_l (s, lm) (abc_fetch s p)) p n" |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
82 |
|
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
83 |
fun abc_inst_shift :: "abc_inst \<Rightarrow> nat \<Rightarrow> abc_inst" |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
84 |
where |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
85 |
"abc_inst_shift (Inc m) n = Inc m" | |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
86 |
"abc_inst_shift (Dec m e) n = Dec m (e + n)" | |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
87 |
"abc_inst_shift (Goto m) n = Goto (m + n)" |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
88 |
|
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
89 |
fun abc_shift :: "abc_inst list \<Rightarrow> nat \<Rightarrow> abc_inst list" |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
90 |
where |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
91 |
"abc_shift xs n = map (\<lambda> x. abc_inst_shift x n) xs" |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
92 |
|
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
93 |
fun abc_comp :: "abc_inst list \<Rightarrow> abc_inst list \<Rightarrow> abc_inst list" (infixl ";" 99) |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
94 |
where |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
95 |
"al; bl = (let al_len = length al in al @ abc_shift bl al_len)" |
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
96 |
|
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
97 |
|
002b07ea0a57
added theorey by Jian
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
98 |
end |