File: implementation.lisp

package info (click to toggle)
acl2 8.5dfsg-5
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 991,452 kB
  • sloc: lisp: 15,567,759; javascript: 22,820; cpp: 13,929; ansic: 12,092; perl: 7,150; java: 4,405; xml: 3,884; makefile: 3,507; sh: 3,187; ruby: 2,633; ml: 763; python: 746; yacc: 723; awk: 295; csh: 186; php: 171; lex: 154; tcl: 49; asm: 23; haskell: 17
file content (340 lines) | stat: -rw-r--r-- 11,753 bytes parent folder | download | duplicates (8)
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
; (value :q)
; (ccl::gc-verbose nil nil)
; (lp)
; (include-book "tmi-reductions")
; (time$ (with-output :off :all (certify-book "implementation" 1)))
; Timings on Whitehart:
; Mon Jul 16 09:26:20 2012
; 86.29 seconds realtime, 77.47 seconds runtime

(in-package "M1")
(include-book "defsys")

(defthm nst-out-bound
  (implies (natp w)
           (< (nst-out cell w) (expt 2 w)))
  :hints (("Goal" :in-theory (enable nst-out)))
  :rule-classes :linear)

(defthm current-symn-bound
  (< (current-symn tape pos) 2)
  :hints (("Goal" :in-theory (enable current-symn)))
  :rule-classes :linear)

(defun ninstr1 (st sym tm w nnil)
  (if (natp w)
      (if (zp tm)
          -1
          (if (equal tm nnil)
              -1
              (let ((cell (ncar tm w)))
                (if (and (equal st (nst-in cell w))
                         (equal sym (nsym cell w)))
                    cell
                    (ninstr1 st sym (ncdr tm w) w nnil)))))
      -1))

(defthm ninstr1-nnil-is-ninstr
  (equal (ninstr1 st sym tm w (nnil w))
         (ninstr st sym tm w))
  :hints (("Goal" :in-theory (enable ninstr))))

(in-theory (enable nst-in nsym nop nst-out ncar ncdr current-symn new-tape2))

(defsys :ld-flg nil
  :modules
  ((lessp :formals (x y)
          :input (and (natp x)
                      (natp y))
          :output (if (< x y) 1 0)
          :code (ifeq y
                      0
                      (ifeq x
                            1
                            (lessp (- x 1) (- y 1)))))
   (mod :formals (x y)
        :input (and (natp x)
                    (natp y)
                    (not (equal y 0)))
        :output (mod x y)
        :code (ifeq (lessp x y)
                    (mod (- x y) y)
                    x))
   (floor :formals (x y a)
          :input (and (natp x)
                      (natp y)
                      (not (equal y 0))
                      (natp a))
          :output (+ a (floor x y))
          :code (ifeq (lessp x y)
                      (floor (- x y) y (+ a 1))
                      a))
   (log2 :formals (x a)
         :input (and (natp x)
                     (natp a))
         :output (+ a (log2 x))
         :code (ifeq x
                     a
                     (ifeq (- x 1)
                           a
                           (log2 (floor x 2 0) (+ 1 a)))))
   (expt :formals (x n a)
         :input (and (natp x)
                     (natp n)
                     (natp a))
         :output (* a (expt x n))
         :code (ifeq n
                     a
                     (expt x (- n 1) (* x a))))
   (nst-in :formals (cell w)
           :input (and (natp cell)
                       (natp w))
           :output (nst-in cell w)
           :code (mod cell (expt 2 w 1)))

   (nsym :formals (cell w)
         :input (and (natp cell) (natp w))
         :output (nsym cell w)
         :code (mod (floor cell (expt 2 w 1) 0) 2))

   (nop :formals (cell w)
        :input (and (natp cell) (natp w))
        :output (nop cell w)
        :code (mod (floor cell (expt 2 (+ 1 w) 1) 0)
                   8))

   (nst-out :formals (cell w)
            :input (and (natp cell) (natp w))
            :output (nst-out cell w)
            :code (mod (floor cell (expt 2 (+ 4 w) 1) 0)
                       (expt 2 w 1)))

   (ncar :formals (tm w)
         :input (and (natp tm) (natp w))
         :output (ncar tm w)
         :code (mod tm (expt 2 (+ 4 (* 2 w)) 1)))

   (ncdr :formals (tm w)
         :input (and (natp tm) (natp w))
         :output (ncdr tm w)
         :code (floor tm (expt 2 (+ 4 (* 2 w)) 1) 0))

   (current-symn :formals (tape pos)
                 :input (and (natp tape)
                             (natp pos))
                 :output (current-symn tape pos)
                 :code (ifeq (- pos (log2 tape 0))
                             0
                             (mod (floor tape (expt 2 pos 1) 0)
                                  2)))

   (ninstr1 :formals (st sym tm w nnil)
            :input (and (natp st)
                        (natp sym)
                        (natp tm)
                        (natp w)
                        (equal nnil (nnil w)))
            :output (ninstr1 st sym tm w nnil)
            :code
            (ifeq tm
                  -1
                  (ifeq (- tm nnil)
                        -1
                        (ifeq (ifeq (- st (nst-in (ncar tm w) w))
                                    (- sym (nsym (ncar tm w) w))
                                    1)
                              (ncar tm w)
                              (ninstr1 st sym (ncdr tm w) w nnil)))))

   (new-tape2 :formals (op tape pos)
              :input (and (natp op)
                          (natp tape)
                          (natp pos))
              :output (mv (acl2::mv-nth 0 (new-tape2 op tape pos))
                          (acl2::mv-nth 1 (new-tape2 op tape pos)))
              :code
              (ifeq (ifeq op
                          0
                          (ifeq (- op 1)
                                0
                                1))
                    (ifeq (- pos (log2 tape 0))
                          (ifeq op
                                (mv (+ tape (expt 2 pos 1)) pos)
                                (mv (+ tape (expt 2 (+ pos 1) 1)) pos))
                          (ifeq (- (current-symn tape pos) op)
                                (mv tape pos)
                                (ifeq (current-symn tape pos)
                                      (mv (+ tape (expt 2 pos 1)) pos)
                                      (mv (- tape (expt 2 pos 1)) pos))))
                    (ifeq (- op 2)
                          (ifeq pos
                                (mv (* 2 tape) 0)
                                (mv tape (- pos 1)))

                          (ifeq (- pos (log2 tape 0))
                                (mv (+ (- tape (expt 2 pos 1))
                                       (expt 2 (+ 1 pos) 1))
                                    (+ 1 pos))
                                (mv tape (+ pos 1)))))
              :ghost-base-value (mv tape pos))

   (tmi3 :formals (st tape pos tm w nnil)
         :dcls ((declare (xargs :measure (acl2-count n))))
         :input (and (natp st)
                     (natp tape)
                     (natp pos)
                     (natp tm)
                     (natp w)
                     (equal nnil (nnil w))
                     (< st (expt 2 w)))
         :output (tmi3 st tape pos tm w n) ; the logic's tmi3 doesn't take nnil as an arg.
         :output-arity 4
         :code
         (ifeq (- (ninstr1 st (current-symn tape pos) tm w nnil) -1)
               (mv 1 st tape pos)
               (tmi3 (nst-out (ninstr1 st (current-symn tape pos) tm w nnil)
                              w)
                     (new-tape2 (nop (ninstr1 st (current-symn tape pos) tm w nnil)
                                     w)
                                tape pos)
                     tm w nnil))
         :ghost-formals (n)
         :ghost-base-test (zp n)
         :ghost-base-value (mv 0 st tape pos)
         :ghost-decr ((- n 1)))

   (main :formals (st tape pos tm w nnil)
         :input (and (natp st)
                     (natp tape)
                     (natp pos)
                     (natp tm)
                     (natp w)
                     (equal nnil (nnil w))
                     (< st (expt 2 w)))
         :output (tmi3 st tape pos tm w n)
         :output-arity 4
         :code (tmi3 st tape pos tm w nnil)
         :ghost-formals (n)
         :ghost-base-value (mv 0 st tape pos)))
  :edit-commands
  ((defun !ninstr1
     :before
     ((defthm natp-ncar
        (implies (natp tm)
                 (natp (ncar tm w)))
        :rule-classes :type-prescription)

      (defthm natp-ncdr-x
        (implies (natp tm)
                 (natp (ncdr tm w)))
        :rule-classes :type-prescription)

      (in-theory (disable ncar ncdr))

      (defthm natp-nst-in
        (implies (natp cell)
                 (natp (nst-in cell w)))
        :rule-classes :type-prescription)

; The type-prescriptions for nsym, nop, and nst-out specify NATP.

      (in-theory (disable nst-in nsym nop nst-out))

; The type-prescription for current-symn specifies NATP.

      (in-theory (disable current-symn))
      ))
   (defun !tmi3
     :before
     ((defthm integerp-ninstr1
        (implies (and (natp st)
                      (natp sym)
                      (natp tm)
                      (natp w)
                      (equal nnil (nnil w)))
                 (and (integerp (ninstr1 st sym tm w nnil))
                      (<= -1 (ninstr1 st sym tm w nnil))))
        :rule-classes
        ((:type-prescription
          :corollary
          (implies (and (natp st)
                        (natp sym)
                        (natp tm)
                        (natp w)
                        (equal nnil (nnil w)))
                   (integerp (ninstr1 st sym tm w nnil))))
         (:linear
          :corollary
          (implies (and (natp st)
                        (natp sym)
                        (natp tm)
                        (natp w)
                        (equal nnil (nnil w)))
                   (<= -1 (ninstr1 st sym tm w nnil))))
         (:rewrite
          :corollary
          (implies (and (natp st)
                        (natp sym)
                        (natp tm)
                        (natp w)
                        (equal nnil (nnil w))
                        (not (equal (ninstr1 st sym tm w nnil) -1)))
                   (and (integerp (ninstr1 st sym tm w nnil))
                        (<= 0 (ninstr1 st sym tm w nnil)))))))

      (defthm integerp-ninstr
        (implies (and (natp st)
                      (natp sym)
                      (natp tm)
                      (natp w))
                 (and (integerp (ninstr st sym tm w))
                      (<= -1 (ninstr st sym tm w))))
        :rule-classes
        ((:type-prescription
          :corollary
          (implies (and (natp st)
                        (natp sym)
                        (natp tm)
                        (natp w))
                   (integerp (ninstr st sym tm w))))
         (:linear
          :corollary
          (implies (and (natp st)
                        (natp sym)
                        (natp tm)
                        (natp w))
                   (<= -1 (ninstr st sym tm w))))
         (:rewrite
          :corollary
          (implies (and (natp st)
                        (natp sym)
                        (natp tm)
                        (natp w)
                        (not (equal (ninstr st sym tm w) -1)))
                   (and (integerp (ninstr st sym tm w))
                        (<= 0 (ninstr st sym tm w)))))))

      (defthm natp-mv-nth-0-new-tape2
        (implies (and (natp tape)
                      (natp pos))
                 (natp (acl2::mv-nth 0 (new-tape2 op tape pos))))
        :hints (("Goal" :nonlinearp t :in-theory (enable current-symn)))
        :rule-classes :type-prescription)

      (defthm natp-mv-nth-1-new-tape2
        (implies (and (natp tape)
                      (natp pos))
                 (natp (acl2::mv-nth 1 (new-tape2 op tape pos))))
        :hints (("Goal" :nonlinearp t :in-theory (enable current-symn)))
        :rule-classes :type-prescription)

      (in-theory (disable  ncar  ncdr  ninstr1  new-tape2  current-symn
                           !ncar !ncdr !ninstr1 !new-tape2 !current-symn
                           nst-in  nst-out  nop  nsym
                           !nst-in !nst-out !nop !nsym))))
   (defthm !tmi3-spec
     :hints
     (("Subgoal *1/10'" :expand (!TMI3 ST TAPE POS TM W (NNIL W) N))))))