-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtest-ctm-viz.rkt
295 lines (260 loc) · 9.5 KB
/
test-ctm-viz.rkt
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#lang fsm
;(require "main.rkt")
#|
A natural number in unary notation (nn) is either:
1. (BLANK)
2. (d nn)
|#
;; nn examples
(define ZERO (list BLANK))
(define THREE '(d d d))
(define EIGHT '(d d d d d d d d))
;; PRE: tape = (LMw) AND i=k>0 AND w in (d)*
;; POST: tape = (LMw) AND i=k+1 AND w in (d)*
(define R (make-tm '(S F)
'(d)
`(((S d) (F ,RIGHT))
((S ,BLANK) (F ,RIGHT)))
'S
'(F)))
(check-equal? (sm-showtransitions R `(,LM d d d) 1)
`((S 1 (,LM d d d))
(F 2 (,LM d d d))))
(check-equal? (sm-showtransitions R `(,LM d d d) 3)
`((S 3 (,LM d d d))
(F 4 (,LM d d d ,BLANK))))
(check-equal? (second (last (sm-showtransitions R `(,LM d d d d) 3)))
4)
;; PRE: tape = (LMw) AND i=k>0, where w in (a b)*
;; POST: tape = (LMw) AND i=k+1
(define L (make-tm '(S H)
`(d)
`(((S d) (H ,LEFT))
((S ,BLANK) (H ,LEFT)))
'S
'(H)))
;; PRE: tape = (LM w) AND i=k>0 AND tape[i]=s, where w in (d BLANK)* AND s in (d BLANK)
;; POST: tape = (LM w) AND i=k AND tape[i]=d
(define Wd (make-tm '(S H)
`(d)
`(((S d) (H d))
((S ,BLANK) (H d)))
'S
'(H)))
;; PRE: tape = (LM w) AND i=k>0 AND tape[i]=s, where w in (d BLANK)* AND s in (d BLANK)
;; POST: tape = (LM w) AND i=k AND tape[i]=BLANK
(define WB (make-tm '(S H)
`(d)
`(((S d) (H ,BLANK))
((S ,BLANK) (H ,BLANK)))
'S
'(H)))
(check-equal? (last (sm-showtransitions WB `(,LM d d) 1))
`(H 1 (,LM ,BLANK d)))
(check-equal? (last (sm-showtransitions WB `(,LM d d d d) 3))
`(H 3 (,LM d d ,BLANK d)))
;; PRE: tape = (LM w) AND i=k>0 AND w in (a b BLANK)*
;; POST: tape = (LM w) AND i>k AND tape[i]=BLANK AND tape[k+1..i-1] != BLANK
(define FBR (combine-tms (list 0
R
(cons BRANCH
(list (list 'd (list GOTO 0))
(list BLANK (list GOTO 10))))
10)
(list 'd)))
(check-equal? (ctm-run FBR `(,LM ,BLANK) 1)
`(F 2 (,LM ,BLANK ,BLANK)))
(check-equal? (ctm-run FBR `(,LM d d d d d ,BLANK d d) 2)
`(F 6 (,LM d d d d d ,BLANK d d)))
(check-equal? (ctm-run FBR `(,LM ,BLANK ,BLANK d d) 3)
`(F 5 (,LM ,BLANK ,BLANK d d ,BLANK)))
;; PRE: tape = (LM w) AND i=k>0 AND w in (d BLANK)*
;; POST: tape = (LM w) AND i>k AND tape[i]=d AND tape[k+1..i-1] != d
(define FdR (combine-tms (list 0
R
(cons BRANCH
(list (list 'd (list GOTO 10))
(list BLANK (list GOTO 0))))
10)
(list 'd)))
(define swap (combine-tms (list (list (list VAR 'i)
R
(list (list VAR 'j)
'i
L
'j)))
'(d)))
;; PRE: tape = (LM w) AND i=k>0 AND w in (d BLANK)* AND exists j<i s.t. tape[j]=BLANK
;; POST: tape = (LM w) AND i<k AND tape[i]=BLANK AND tape[i+1..|w|] != BLANK
(define FBL (combine-tms (list 0
L
(cons BRANCH
(list (list 'd (list GOTO 0))
(list BLANK (list GOTO 1))
(list LM (list GOTO 0))))
1)
'(d)))
(define SHL (combine-tms (list 0
R
`(,BRANCH (d (GOTO 20))
(,BLANK (GOTO 10)))
10
FBL
'(GOTO 30)
20
L
swap
R
'(GOTO 0)
30)
'(d)))
;; PRE: tape = (LM BLANK a BLANK b AND i=1)
;; POST: tape = (LM BLANK a b BLANK) AND i=3 if a=b=0, i=a+b+2 otherwise
#;(define ADD (combine-tms
(list FBR Wd FBR L WB)
'(d)))
(define ADD (combine-tms
(list R
`(,BRANCH (,BLANK (GOTO 100))
(d (GOTO 200)))
100 ;; a=0
R
R
`(,BRANCH (,BLANK (GOTO 500)) ;; a=b=0
(d (GOTO 300)))
200 ;; a!=0
FBR
Wd
FBR
L
WB
`(,GOTO 1000)
300 ;; a=0 and b!=0
L
SHL
FBL
SHL
`(,GOTO 1000)
500 ;; a=0 and b=0
L
`(,GOTO 1000)
1000)
'(d)))
;; a=0 and b=0
(check-equal? (rest (ctm-run ADD `(,LM ,BLANK) 1))
`(3 (@ ,BLANK ,BLANK ,BLANK ,BLANK)))
;; a=0 and b!=0
(check-equal? (rest (ctm-run ADD `(,LM ,BLANK ,BLANK ,BLANK d d) 1))
`(4 (@ ,BLANK d d ,BLANK ,BLANK ,BLANK)))
;; a!=0 and b=0
(check-equal? (rest (ctm-run ADD `(,LM ,BLANK d d d d) 1))
`(6 (@ ,BLANK d d d d ,BLANK ,BLANK)))
;; a!=0 and b!=0
(check-equal? (rest (ctm-run ADD `(,LM ,BLANK d d d ,BLANK d d) 1))
`(7 (@ ,BLANK d d d d d ,BLANK ,BLANK)))
(define ADDL '(list R
`(,BRANCH (,BLANK (GOTO 100))
(d (GOTO 200)))
100 ;; a=0
R
R
`(,BRANCH (,BLANK (GOTO 500)) ;; a=b=0
(d (GOTO 300)))
200 ;; a!=0
FBR
Wd
FBR
L
WB
`(,GOTO 1000)
300 ;; a=0 and b!=0
L
SHL
FBL
SHL
`(,GOTO 1000)
500 ;; a=0 and b=0
L
`(,GOTO 1000)
1000))
;(ctm-viz ADD ADDL `(,LM ,BLANK d d d ,BLANK d d) 1)
#;(define ADD2 (combine-tms
(list FBR Wd FBR L WB L (list BRANCH (list BLANK R R R (list BRANCH (list BLANK L)
(list 'd L SHL FBL SHL)))
(list 'd R)))
'(d)))
#;(define ADD2L (list FBR Wd FBR L WB L (list BRANCH (list BLANK R R R (list BRANCH (list BLANK L)
(list 'd L SHL FBL SHL)))
(list 'd R))))
(define ADD2 (combine-tms
(list
FBR
Wd
FBR
L
WB
L
(list BRANCH (list BLANK (list GOTO 200))
(list 'd (list GOTO 300)))
200
R
R
R
(list BRANCH (list BLANK (list GOTO 250))
(list 'd (list GOTO 400)))
250
L
(list GOTO 500)
300
R
(list GOTO 500)
400
L
SHL
FBL
SHL
500)
'(d)))
(define ADD2L '(list
FBR
Wd
FBR
L
WB
L
(list BRANCH (list BLANK (list GOTO 200))
(list 'd (list GOTO 300)))
200
R
R
R
(list BRANCH (list BLANK (list GOTO 250))
(list 'd (list GOTO 400)))
250
L
(list GOTO 500)
300
R
(list GOTO 500)
400
L
SHL
FBL
SHL
500))
;; a=0 and b=0
(check-equal? (rest (ctm-run ADD2 `(,LM ,BLANK) 1))
`(3 (@ ,BLANK ,BLANK ,BLANK ,BLANK)))
;; a=0 and b!=0
(check-equal? (rest (ctm-run ADD2 `(,LM ,BLANK ,BLANK ,BLANK d d) 1))
`(4 (@ ,BLANK d d ,BLANK ,BLANK ,BLANK)))
;; a!=0 and b=0
(check-equal? (rest (ctm-run ADD2 `(,LM ,BLANK d d d d) 1))
`(6 (@ ,BLANK d d d d ,BLANK ,BLANK)))
;; a!=0 and b!=0
(check-equal? (rest (ctm-run ADD2 `(,LM ,BLANK d d d ,BLANK d d) 1))
`(7 (@ ,BLANK d d d d d ,BLANK ,BLANK)))
;; Marcelina working on a parsing bug
;(ctm-viz ADD2 ADD2L `(,LM ,BLANK d d d ,BLANK d d) 1)
;(ctm-viz ADD2 ADD2L `(,LM ,BLANK ,BLANK d d d ,BLANK) 1)
;(ctm-viz ADD2 ADD2L `(,LM ,BLANK ,BLANK ,BLANK) 1)