-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathturing-add-new.txt
244 lines (190 loc) · 4.48 KB
/
turing-add-new.txt
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
e 1 1 + 1 1 dec ='
e 1 1 + 1 << 0 >> ='
e 1 1 + << 1 0 =
e 1 1 s + 1 0 dec ='
e 1 s 0 + 1 dec 1' ='
e s 0 0 + << 0 >> 1' ='
e 1 0 0 s + 0 1 >> ='
e 1 0 1 + 0 1 =
e 1 0 1 + 0 1 dec ='
e 1 0 1 + 0 << 0 >> ='
e 1 0 1 + << 0 0 =
e 1 0 1 s + 0 0 dec ='
e 1 0 s 0 + 0 dec 1' ='
e 1 1 0 + dec 1' 1' ='
e 1 1 0 kill 1' 1' ='
e 1 1 0 kill 1' ='
e 1 1 0 kill ='
e 1 1 0
stype Bit = &{ s: Bit }
stype DBit = &{ dec: DBit' , <<: DBit }
and DBit' = +{ >>: DBit , kill: Bit }
dbit0 :: {DBit |- DBit}
l -> dbit0 -> r =
{ case r of
dec => l.dec; l -> dbit1' -> r
| << => l.<<; l -> dbit0 -> r }
dbit1 :: {DBit |- DBit}
l -> dbit1 -> r =
{ case r of
dec => l.<<; r.>>; l -> dbit0 -> r
| << => l.<<; l -> dbit1 -> r }
dbit1' :: {DBit' |- DBit'}
l -> dbit1' -> r =
{ case l of
>> => r.>>; l -> dbit1 -> r
| kill => r.kill; l -> r }
+ :: {Bit |- DBit}
l -> + -> r =
{ case r of
dec => r.kill; l -> r
| << => l.s; l -> + -> r }
= :: {DBit |- Bit}
l -> = -> r =
{ l.dec; l -> =' -> r }
=' :: {DBit' |- Bit}
l -> =' -> r =
{ case l of
>> => l -> = -> r
| kill => l -> r }
stype Bit = &{ s: Bit , a: +{ e: 1 , 0: Bit , 1: Bit } }
c <- add <- d0,d1 =
{ d0.a;
case d0 of
e => wait d0; c <- d0
| 0 => (d1.a;
case d1 of
e => wait d1; c <- bit0 <- d0
| 0 => c' <- add <- d0,d1;
c <- bit0 <- c'
| 1 => c' <- add <- d0,d1;
c <- bit1 <- c')
| 1 => (d1.a;
case d1 of
e => wait d1; c <- bit1 <- d0
| 0 => c' <- add <- d0,d1;
c <- bit1 <- c'
| 1 => c' <- add <- d0,d1;
c'.s;
c <- bit0 <- c') }
proc eps =
{ caseR of
inc => selectR skip; eps -- bit1 }
proc bit0 =
{ caseR of
dec => selectL dec; bit1'
| skip => selectL skip; bit0'
| inc => selectR skip; bit1 }
proc bit1 =
{ caseR of
dec => selectL skip; bit0'
| skip => selectL skip; bit1'
| inc => selectL inc; bit0' }
proc bit0' =
{ caseL of
skip => selectR skip; bit0
| erase => selectR erase; <-> }
proc bit1' =
{ caseL of
skip => selectR skip; bit1
| erase => selectR erase; <-> }
proc plus =
{ caseR of
dec => selectR erase; <->
| skip => selectL inc; plus' }
proc plus' =
{ caseL of
skip => selectR skip; plus }
proc eq =
{ selectL dec; eq' }
proc eq' =
{ caseL of
skip => eq
| erase => <-> }
stype Bit = &{ s: Bit , a0: Bit -o Bit , a1: Bit -o Bit , a: Bit -o Bit }
c <- eps =
{ case c of
a0 => d0 <- recv c;
c <- bit0 <- d0
| a1 => d1 <- recv c;
c <- bit1 <- d1
| a => d <- recv c;
c <- d }
c <- bit0 <- d =
{ case c of
a0 => d0 <- recv c;
d.a;
send d d0;
c <- bit0 <- d
| a1 => d1 <- recv c;
d.a;
send d d1;
c <- bit1 <- d
| a => c' <- recv c;
c'.a0;
send c' d;
c <- c' }
c <- bit1 <- d =
{ case c of
a0 => d0 <- recv c;
d.a;
send d d0;
c <- bit1 <- d
| a1 => d1 <- recv c;
d.a;
send d d1;
d.s;
c <- bit0 <- d
| a => c' <- recv c;
c'.a1;
send c' d;
c <- c' }
case d1 of
e => wait d1;
c <- bit0 <- d0
| 0 => d0.a;
send d0 (
stype Bit = +{ e: 1, 0: Bit, 1: Bit }
stype Vis<C> = &{ e: C, 0: Bit<C> -o C, 1: Bit<C> -o C }
and Bit<C> = &{ visit: Vis<C> -o C, stop: Bit }
eps :: {Bit -|}
c <- eps =
{ case c of
visit => C <- recv c; v <- recv c;
v.e; c <- v
| stop => c <- eps }
bit0 :: {Bit -| Bit}
c <- bit0 <- d =
{ case c of
visit => C <- recv c; v <- recv c;
v.0; send d C; send v d;
c <- v
| stop => c <- bit0 <- d }
bit1 :: {Bit -| Bit}
c <- bit1 <- d =
{ case c of
visit => C <- recv c; v <- recv c;
v.1; send d C; send v d;
c <- v
| stop => c <- bit1 <- d }
succ :: {Vis<Bit> -|}
c <- succ =
{ case c of
e => c <- eps
| 0 => d <- recv c; % d : &{ visit: Vis<Bit> -o Bit , stop: Bit }
d.stop;
c <- bit1 <- d
| 1 => d <- recv c;
d.visit; send d (v <- succ);
c <- bit0 <- d }
c <- val =
{ case c of
e => send c 0; close c
| 0 => b <- recv c; % b : Vis<nat /\ 1> -o nat /\ 1
send b (v' <- val);
x <- recv b; wait b;
send c (2*x); close c
| 1 => b <- recv c; % b : Vis<nat /\ 1> -o nat /\ 1
send b (v' <- val);
x <- recv b; wait b;
send c (2*x+1); close c }