This file is indexed.

/usr/share/acl2-6.3/books/str/strval.lisp is in acl2-books-source 6.3-5.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
; ACL2 String Library
; Copyright (C) 2009-2013 Centaur Technology
;
; Contact:
;   Centaur Technology Formal Verification Group
;   7600-C N. Capital of Texas Highway, Suite 300, Austin, TX 78731, USA.
;   http://www.centtech.com/
;
; This program is free software; you can redistribute it and/or modify it under
; the terms of the GNU General Public License as published by the Free Software
; Foundation; either version 2 of the License, or (at your option) any later
; version.  This program is distributed in the hope that it will be useful but
; WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
; FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
; more details.  You should have received a copy of the GNU General Public
; License along with this program; if not, write to the Free Software
; Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
;
; Original author: Jared Davis <jared@centtech.com>

(in-package "STR")
(include-book "strnatless")
(local (include-book "arithmetic"))
(local (include-book "misc/assert" :dir :system))


;; BOZO maybe use cutil::deflist for these list recognizers

(defsection octal-digitp
  :parents (numbers)
  :short "Recognizer for characters #\\0 through #\\7."
  :long "<p>@(call octal-digitp) is the octal alternative to @(see digitp).</p>"

  (definlined octal-digitp (x)
    (declare (type character x))
    (let ((code (char-code (mbe :logic (char-fix x) :exec x))))
      (declare (type (unsigned-byte 8) code))
      (and (<= (char-code #\0) code)
           (<= code (char-code #\7)))))

  (local (in-theory (enable octal-digitp)))

  (defcong ichareqv equal (octal-digitp x) 1
    :hints(("Goal" :in-theory (enable octal-digitp
                                      ichareqv
                                      downcase-char
                                      char-fix))))

  (defthm digitp-when-octal-digitp
    (implies (octal-digitp x)
             (digitp x))
    :hints(("Goal" :in-theory (enable digitp)))))


(defsection octal-digit-listp
  :parents (numbers)
  :short "Recognizes lists of @(see octal-digitp) characters."

  (defund octal-digit-listp (x)
    (declare (xargs :guard (character-listp x)))
    (if (atom x)
        t
      (and (octal-digitp (car x))
           (octal-digit-listp (cdr x)))))

  (local (in-theory (enable octal-digit-listp)))

  (defthm digit-listp-when-octal-digit-listp
    (implies (octal-digit-listp x)
             (digit-listp x))
    :hints(("Goal" :in-theory (enable digit-listp))))

  (defcong icharlisteqv equal (octal-digit-listp x) 1
    :hints(("Goal" :in-theory (enable icharlisteqv)))))



(defsection parse-octal-from-charlist
  :parents (numbers)
  :short "Parse an octal number from the beginning of a character list."
  :long "<p>This is like @(call parse-nat-from-charlist), but deals with octal
digits and returns their octal value.</p>"

  (defund parse-octal-from-charlist (x val len)
    (declare (type (integer 0 *) val len)
             (xargs :guard (and (character-listp x)
                                (natp val)
                                (natp len))))
    (cond ((atom x)
           (mv (lnfix val) (lnfix len) nil))
          ((octal-digitp (car x))
           (let ((digit-val (digit-val (car x))))
             (parse-octal-from-charlist (cdr x)
                                        (+ digit-val (* 8 (lnfix val)))
                                        (+ 1 (lnfix len)))))
          (t
           (mv (lnfix val) (lnfix len) x))))

  (local (in-theory (enable parse-octal-from-charlist)))

  (defthm natp-of-parse-octal-from-charlist
    (implies (natp val)
             (natp (mv-nth 0 (parse-octal-from-charlist x val len))))
    :rule-classes :type-prescription))


(defsection octal-digit-list-value
  :parents (numbers)
  :short "Coerces a list of octal digits into a natural number."
  :long "<p>@(call octal-digit-list-value) is like @(see digit-list-value) but
for octal numbers.</p>

<p>See also @(see parse-octal-from-charlist) for a more flexible function that
can tolerate non-octal digits after the number.</p>"

  (definlined octal-digit-list-value (x)
    (declare (xargs :guard (and (character-listp x)
                                (octal-digit-listp x))))
    (b* (((mv val ?len ?rest)
          (parse-octal-from-charlist x 0 0)))
      val))

  (local (in-theory (enable octal-digit-list-value)))

  (defthm natp-of-octal-digit-list-value
    (natp (octal-digit-list-value x))
    :rule-classes :type-prescription)

  (local (defthmd l0
           (implies (icharlisteqv x x-equiv)
                    (equal (mv-nth 0 (parse-octal-from-charlist x val len))
                           (mv-nth 0 (parse-octal-from-charlist x-equiv val len))))
           :hints(("Goal" :in-theory (enable parse-octal-from-charlist
                                             icharlisteqv)))))

  (defcong icharlisteqv equal (octal-digit-list-value x) 1
    :hints(("Goal" :use ((:instance l0 (val 0) (len 0))))))

  (local (assert! (and (equal (octal-digit-list-value (coerce "0" 'list)) #o0)
                       (equal (octal-digit-list-value (coerce "6" 'list)) #o6)
                       (equal (octal-digit-list-value (coerce "12" 'list)) #o12)
                       (equal (octal-digit-list-value (coerce "1234" 'list)) #o1234)))))




(defsection hex-digitp
  :parents (numbers)
  :short "Recognizer for characters 0-9, A-F, and a-f."
  :long "<p>@(call hex-digitp) is the hexadecimal alternative to @(see digitp).</p>"

  (definlined hex-digitp (x)
    (declare (type character x))
    (let ((code (char-code (mbe :logic (char-fix x) :exec x))))
      (declare (type (unsigned-byte 8) code))
      (or (and (<= (char-code #\0) code)
               (<= code (char-code #\9)))
          (and (<= (char-code #\a) code)
               (<= code (char-code #\f)))
          (and (<= (char-code #\A) code)
               (<= code (char-code #\F))))))

  (local (in-theory (enable hex-digitp)))

  (defcong ichareqv equal (hex-digitp x) 1
    :hints(("Goal" :in-theory (enable hex-digitp
                                      ichareqv
                                      downcase-char
                                      char-fix)))))


(defsection hex-digit-listp
  :parents (numbers)
  :short "Recognizes lists of @(see hex-digitp) characters."

  (defund hex-digit-listp (x)
    (declare (xargs :guard (character-listp x)))
    (if (atom x)
        t
      (and (hex-digitp (car x))
           (hex-digit-listp (cdr x)))))

  (local (in-theory (enable hex-digit-listp)))

  (defcong icharlisteqv equal (hex-digit-listp x) 1
    :hints(("Goal" :in-theory (enable icharlisteqv)))))


(defsection hex-digit-val
  :parents (numbers)
  :short "Coerces a @(see hex-digitp) character into an integer."
  :long "<p>@(call hex-digit-val) is the hexadecimal version of @(see
digit-val).</p>"

  (definlined hex-digit-val (x)
    (declare (xargs :guard (and (characterp x)
                                (hex-digitp x))))
    (if (mbe :logic (not (hex-digitp x))
             :exec nil)
        0
      (let ((code (char-code (mbe :logic (char-fix x)
                                  :exec x))))
        (declare (type (unsigned-byte 8) code))
        (cond ((<= (char-code #\a) code) (- code (- (char-code #\a) 10)))
              ((<= (char-code #\A) code) (- code (- (char-code #\A) 10)))
              (t                         (- code (char-code #\0)))))))

  (local (in-theory (enable hex-digit-val hex-digitp)))

  (defthm natp-of-hex-digit-val
    (natp (hex-digit-val x))
    :rule-classes :type-prescription)

  (defcong ichareqv equal (hex-digit-val x) 1
    :hints(("Goal" :in-theory (enable ichareqv
                                      downcase-char
                                      char-fix))))

  (local (assert! (and (equal (hex-digit-val #\A) #xA)
                       (equal (hex-digit-val #\B) #xB)
                       (equal (hex-digit-val #\C) #xC)
                       (equal (hex-digit-val #\D) #xD)
                       (equal (hex-digit-val #\E) #xE)
                       (equal (hex-digit-val #\F) #xF)
                       (equal (hex-digit-val #\a) #xa)
                       (equal (hex-digit-val #\b) #xb)
                       (equal (hex-digit-val #\c) #xc)
                       (equal (hex-digit-val #\d) #xd)
                       (equal (hex-digit-val #\e) #xe)
                       (equal (hex-digit-val #\f) #xf)
                       (equal (hex-digit-val #\0) #x0)
                       (equal (hex-digit-val #\1) #x1)
                       (equal (hex-digit-val #\2) #x2)
                       (equal (hex-digit-val #\3) #x3)
                       (equal (hex-digit-val #\4) #x4)
                       (equal (hex-digit-val #\5) #x5)
                       (equal (hex-digit-val #\6) #x6)
                       (equal (hex-digit-val #\7) #x7)
                       (equal (hex-digit-val #\8) #x8)
                       (equal (hex-digit-val #\9) #x9)))))

(defsection parse-hex-from-charlist
  :parents (numbers)
  :short "Parse a hexadecimal number from the beginning of a character list."
  :long "<p>This is like @(call parse-nat-from-charlist), but deals with
hex digits and returns their hexadecimal value.</p>"

  (defund parse-hex-from-charlist (x val len)
    (declare (type (integer 0 *) val len)
             (xargs :guard (and (character-listp x)
                                (natp val)
                                (natp len))))
    (cond ((atom x)
           (mv (lnfix val) (lnfix len) nil))
          ((hex-digitp (car x))
           (let ((digit-val (hex-digit-val (car x))))
             (parse-hex-from-charlist
              (cdr x)
              (the (integer 0 *) (+ (the (integer 0 *) digit-val)
                                    (the (integer 0 *)
                                      (* 16 (the (integer 0 *) (lnfix val))))))
              (the (integer 0 *) (+ 1 (the (integer 0 *) (lnfix len)))))))
          (t
           (mv (lnfix val) (lnfix len) x))))

  (local (in-theory (enable parse-hex-from-charlist)))

  (defthm natp-of-parse-hex-from-charlist
    (implies (natp val)
             (natp (mv-nth 0 (parse-hex-from-charlist x val len))))
    :rule-classes :type-prescription))


(defsection hex-digit-list-value
  :parents (numbers)
  :short "Coerces a list of hex digits into a natural number."
  :long "<p>@(call hex-digit-list-value) is like @(see digit-list-value) but
for hex numbers.</p>

<p>See also @(see parse-hex-from-charlist) for a more flexible function that
can tolerate non-hex digits after the number.</p>"

  (definlined hex-digit-list-value (x)
    (declare (xargs :guard (and (character-listp x)
                                (hex-digit-listp x))))
    (b* (((mv val ?len ?rest)
          (parse-hex-from-charlist x 0 0)))
      val))

  (local (in-theory (enable hex-digit-list-value)))

  (defthm natp-of-hex-digit-list-value
    (natp (hex-digit-list-value x))
    :rule-classes :type-prescription)

  (local (defthmd l0
           (implies (icharlisteqv x x-equiv)
                    (equal (mv-nth 0 (parse-hex-from-charlist x val len))
                           (mv-nth 0 (parse-hex-from-charlist x-equiv val len))))
           :hints(("Goal" :in-theory (enable parse-hex-from-charlist
                                             icharlisteqv)))))

  (defcong icharlisteqv equal (hex-digit-list-value x) 1
    :hints(("Goal" :use ((:instance l0 (val 0) (len 0))))))

  (local (assert! (and (equal (hex-digit-list-value (coerce "0" 'list)) #x0)
                       (equal (hex-digit-list-value (coerce "6" 'list)) #x6)
                       (equal (hex-digit-list-value (coerce "12" 'list)) #x12)
                       (equal (hex-digit-list-value (coerce "1234" 'list)) #x1234)))))


(defsection bit-digitp
  :parents (numbers)
  :short "Recognizer for characters #\\0 and #\\1."
  :long "<p>@(call bit-digitp) is the binary alternative to @(see digitp).</p>"

  (definlined bit-digitp (x)
    (declare (xargs :guard t))
    (or (eql x #\0)
        (eql x #\1)))

  (local (in-theory (enable bit-digitp)))

  (defcong ichareqv equal (bit-digitp x) 1
    :hints(("Goal" :in-theory (enable ichareqv
                                      downcase-char
                                      char-fix)))))


(defsection bit-digit-listp
  :parents (numbers)
  :short "Recognizes lists of @(see bit-digitp) characters."

  (defund bit-digit-listp (x)
    (declare (xargs :guard t))
    (if (atom x)
        t
      (and (bit-digitp (car x))
           (bit-digit-listp (cdr x)))))

  (local (in-theory (enable bit-digit-listp)))

  (defcong icharlisteqv equal (bit-digit-listp x) 1
    :hints(("Goal" :in-theory (enable icharlisteqv)))))



(defsection bitstring-p
  :parents (numbers)
  :short "Recognizes strings with only @(see bit-digitp) characters."

  (defund bitstring-p (x)
    (declare (xargs :guard t))
    ;; BOZO make a faster version of this
    ;; BOZO requiring stringp is bad for congruences
    (and (stringp x)
         (bit-digit-listp (explode x))))

  (local (in-theory (enable bitstring-p))))


(defsection parse-bits-from-charlist
  :parents (numbers)
  :short "Parse a binary number from the beginning of a character list."
  :long "<p>This is like @(call parse-nat-from-charlist), but deals with binary
digits (#\\0 and #\\1) and returns their binary value.</p>"

  (defund parse-bits-from-charlist (x val len)
    (declare (type (integer 0 *) val len)
             (xargs :guard (and (character-listp x)
                                (natp val)
                                (natp len))))
    (cond ((atom x)
           (mv (lnfix val) (lnfix len) nil))
          ((eql (car x) #\0)
           (parse-bits-from-charlist
            (cdr x)
            (mbe :logic (* 2 (nfix val))
                 :exec (the (integer 0 *) (ash val 1)))
            (the (integer 0 *) (+ 1 (the (integer 0 *) (lnfix len))))))
          ((eql (car x) #\1)
           (parse-bits-from-charlist
            (cdr x)
            (the (integer 0 *)
              (+ 1
                 (mbe :logic (* 2 (nfix val))
                      :exec (the (integer 0 *) (ash val 1)))))
            (the (integer 0 *) (+ 1 (the (integer 0 *) (lnfix len))))))
          (t
           (mv (lnfix val) (lnfix len) x))))

  (local (in-theory (enable parse-bits-from-charlist)))

  (defthm natp-of-parse-bits-from-charlist
    (implies (natp val)
             (natp (mv-nth 0 (parse-bits-from-charlist x val len))))
    :rule-classes :type-prescription))


(defsection bit-digit-list-value
  :parents (numbers)
  :short "Coerces a list of bit digits into a natural number."
  :long "<p>@(call bit-digit-list-value) is like @(see digit-list-value) but
for bit numbers.</p>

<p>See also @(see parse-bits-from-charlist) for a more flexible function that
can tolerate non-bit digits after the number.</p>"

  (definlined bit-digit-list-value (x)
    (declare (xargs :guard (and (character-listp x)
                                (bit-digit-listp x))))
    (b* (((mv val ?len ?rest)
          (parse-bits-from-charlist x 0 0)))
      val))

  (local (in-theory (enable bit-digit-list-value)))

  (defthm natp-of-bit-digit-list-value
    (natp (bit-digit-list-value x))
    :rule-classes :type-prescription)

  (local (defthmd l0
           (implies (icharlisteqv x x-equiv)
                    (equal (mv-nth 0 (parse-bits-from-charlist x val len))
                           (mv-nth 0 (parse-bits-from-charlist x-equiv val len))))
           :hints(("Goal" :in-theory (enable parse-bits-from-charlist
                                             icharlisteqv
                                             ichareqv
                                             downcase-char
                                             char-fix)))))

  (defcong icharlisteqv equal (bit-digit-list-value x) 1
    :hints(("Goal" :use ((:instance l0 (val 0) (len 0))))))

  (local (assert! (and (equal (bit-digit-list-value (coerce "0" 'list)) #b0)
                       (equal (bit-digit-list-value (coerce "1" 'list)) #b1)
                       (equal (bit-digit-list-value (coerce "01" 'list)) #b01)
                       (equal (bit-digit-list-value (coerce "0101011101" 'list))
                              #b0101011101)))))




; BOZO we could do some work to make these nicer, i.e., we should have fast
; versions of strval8, etc., and ordinary strval should have a nicer logical
; definition.

(defsection strval
  :parents (numbers)
  :short "Interpret a string as a decimal number."
  :long "<p>@(call strval) tries to interpret a string as a base-10 natural
number.  For example, @('(strval \"35\")') is 35.  If the string has any
non-decimal digit characters or is empty, we return @('nil').</p>"

  (defund strval (x)
    (declare (type string x))
    (b* (((the (integer 0 *) xl)
          (mbe :logic (len (explode x))
               :exec (length x)))
         ((mv (the (integer 0 *) val)
              (the (integer 0 *) len))
          (str::parse-nat-from-string x 0 0 0 xl)))
      (and (< 0 len)
           (int= len xl)
           val)))

  (local (in-theory (enable strval)))

  (defthm type-of-strval
    (or (natp (strval x))
        (not (strval x)))
    :rule-classes :type-prescription)

  (defcong istreqv equal (strval x) 1))


(defsection strval8
  :parents (numbers)
  :short "Interpret a string as an octal number."
  :long "<p>@(call strval8) is like @(see strval) but for octal instead of
decimal numbers.  For example, @('(strval8 \"10\")') is 8.  If the string is
empty or has any non-octal digit characters, we return @('nil').</p>"

  (defund strval8 (x)
    (declare (type string x))
    (let ((chars (explode x)))
      (and (consp chars)
           (octal-digit-listp chars)
           (octal-digit-list-value chars))))

  (local (in-theory (enable strval8)))

  (defthm type-of-strval8
    (or (natp (strval8 x))
        (not (strval8 x)))
    :rule-classes :type-prescription)

  (defcong istreqv equal (strval8 x) 1))


(defsection strval16
  :parents (numbers)
  :short "Interpret a string as a hexadecimal number."
  :long "<p>@(call strval16) is like @(see strval) but for hexadecimal instead
of decimal numbers.  For example, @('(strval16 \"10\")') is 16.  If the string
is empty or has any non-hex digit characters, we return @('nil').</p>"

  (defund strval16 (x)
    (declare (type string x))
    (let ((chars (explode x)))
      (and (consp chars)
           (hex-digit-listp chars)
           (hex-digit-list-value chars))))

  (local (in-theory (enable strval16)))

  (defthm type-of-strval16
    (or (natp (strval16 x))
        (not (strval16 x)))
    :rule-classes :type-prescription)

  (defcong istreqv equal (strval16 x) 1))


(defsection strval2
  :parents (numbers)
  :short "Interpret a string as a binary number."
  :long "<p>@(call strval2) is like @(see strval) but for binary instead of
decimal numbers.  For example, @('(strval16 \"10\")') is 2.  If the string is
empty or has any non-binary digit characters, we return @('nil').</p>"

  (defund strval2 (x)
    (declare (type string x))
    (let ((chars (explode x)))
      (and (consp chars)
           (bit-digit-listp chars)
           (bit-digit-list-value chars))))

  (local (in-theory (enable strval2)))

  (defthm type-of-strval2
    (or (natp (strval2 x))
        (not (strval2 x)))
    :rule-classes :type-prescription)

  (defcong istreqv equal (strval2 x) 1))



(local (assert! (equal (strval "") nil)))
(local (assert! (equal (strval2 "") nil)))
(local (assert! (equal (strval8 "") nil)))
(local (assert! (equal (strval16 "") nil)))

(local (assert! (equal (strval "0") 0)))
(local (assert! (equal (strval2 "0") 0)))
(local (assert! (equal (strval8 "0") 0)))
(local (assert! (equal (strval16 "0") 0)))

(local (assert! (equal (strval "1234") 1234)))
(local (assert! (equal (strval2 "0101") #b0101)))
(local (assert! (equal (strval8 "1234") #o1234)))
(local (assert! (equal (strval16 "1234") #x1234)))