This file is indexed.

/usr/share/acl2-6.3/books/str/char-case.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
; 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.


; char-case.lisp
;
; Original authors: Jared Davis <jared@centtech.com> and
;                   Sol Swords <sswords@centtech.com>

(in-package "STR")
(include-book "eqv")
(include-book "tools/bstar" :dir :system)
(local (include-book "arithmetic"))

(defmacro little-a ()   (char-code #\a))
(defmacro little-z ()   (char-code #\z))
(defmacro big-a ()      (char-code #\A))
(defmacro big-z ()      (char-code #\Z))
(defmacro case-delta () (- (little-a) (big-a)))



(defsection up-alpha-p
  :parents (cases acl2::upper-case-p)
  :short "Determine if a character is an upper-case letter (A-Z)."

  :long "<p>@(call up-alpha-p) determines if @('x') is an upper-case alphabetic
character.</p>

<p>ACL2 has a built-in alternative to this function, @(see acl2::upper-case-p),
but it is irritating to use because it has @(see standard-char-p) guards.  In
contrast, @('up-alpha-p') works on arbitrary characters.</p>"

  (definlined up-alpha-p (x)
    (declare (type character x))
    (b* (((the (unsigned-byte 8) code) (char-code x)))
      (and (<= (big-a) code)
           (<= code (big-z)))))

  (local (in-theory (enable up-alpha-p)))

  (defcong chareqv equal (up-alpha-p x) 1)

  ;; Rewrite ACL2's upper-case-p to up-alpha-p.  It seems simplest to just do
  ;; the proof by exhaustive testing.

  (local (defund exhaustive-test (n)
           (and (let ((x (code-char n)))
                  (equal (acl2::upper-case-p x)
                         (and (<= (big-a) (char-code x))
                              (<= (char-code x) (big-z)))))
                (or (zp n)
                    (exhaustive-test (- n 1))))))

  (local (defthm lemma1
           (implies (and (natp n)
                         (<= n 255))
                    (equal (char-code (code-char n))
                           n))))

  (local (defthm lemma2
           (implies (and (natp n)
                         (natp m)
                         (exhaustive-test n)
                         (= x (code-char m))
                         (<= m n)
                         (<= n 255))
                    (equal (acl2::upper-case-p x)
                           (and (<= (big-a) (char-code x))
                                (<= (char-code x) (big-z)))))
           :hints(("Goal"
                   :induct (exhaustive-test n)
                   :in-theory (enable exhaustive-test)))))

  (local (defthm lemma3
           (implies (characterp x)
                    (equal (acl2::upper-case-p x)
                           (and (<= (big-a) (char-code x))
                                (<= (char-code x) (big-z)))))
           :hints(("Goal"
                   :in-theory (disable lemma2)
                   :use ((:instance lemma2
                                    (n 255)
                                    (m (char-code x))))))))

  (local (defthm lemma4
           (implies (not (characterp x))
                    (not (acl2::upper-case-p x)))
           :hints(("Goal" :in-theory (enable acl2::upper-case-p)))))

  (local (defthm lemma5
           (equal (acl2::upper-case-p x)
                  (and (<= (big-a) (char-code x))
                       (<= (char-code x) (big-z))))
           :hints(("Goal" :cases ((characterp x))))))

  (defthm upper-case-p-is-up-alpha-p
    (equal (acl2::upper-case-p x)
           (up-alpha-p (double-rewrite x))))

  ;; No longer necessary since we'll rewrite upper-case-p away
  (in-theory (disable acl2::upper-case-p-char-upcase)))



(defsection down-alpha-p
  :parents (cases acl2::lower-case-p)
  :short "Determine if a character is a lower-case letter (a-z)."

  :long "<p>@(call down-alpha-p) determines if @('x') is an lower-case
alphabetic character.</p>

<p>ACL2 has a built-in alternative to this function, @(see acl2::lower-case-p),
but it is irritating to use because it has @(see standard-char-p) guards.  In
contrast, @('down-alpha-p') works on arbitrary characters.</p>"

  (definlined down-alpha-p (x)
    (declare (type character x))
    (b* (((the (unsigned-byte 8) code) (char-code x)))
      (and (<= (little-a) code)
           (<= code (little-z)))))

  (local (in-theory (enable down-alpha-p)))

  (defcong chareqv equal (down-alpha-p x) 1)

  ;; Rewrite ACL2's lower-case-p to down-alpha-p.  It seems simplest to just do
  ;; the proof by exhaustive testing.

  (local (defund exhaustive-test (n)
           (and (let ((x (code-char n)))
                  (equal (acl2::lower-case-p x)
                         (and (<= (little-a) (char-code x))
                              (<= (char-code x) (little-z)))))
                (or (zp n)
                    (exhaustive-test (- n 1))))))

  (local (defthm lemma1
           (implies (and (natp n)
                         (<= n 255))
                    (equal (char-code (code-char n))
                           n))))

  (local (defthm lemma2
           (implies (and (natp n)
                         (natp m)
                         (exhaustive-test n)
                         (= x (code-char m))
                         (<= m n)
                         (<= n 255))
                    (equal (acl2::lower-case-p x)
                           (and (<= (little-a) (char-code x))
                                (<= (char-code x) (little-z)))))
           :hints(("Goal"
                   :induct (exhaustive-test n)
                   :in-theory (enable exhaustive-test)))))

  (local (defthm lemma3
           (implies (characterp x)
                    (equal (acl2::lower-case-p x)
                           (and (<= (little-a) (char-code x))
                                (<= (char-code x) (little-z)))))
           :hints(("Goal"
                   :in-theory (disable lemma2)
                   :use ((:instance lemma2
                                    (n 255)
                                    (m (char-code x))))))))

  (local (defthm lemma4
           (implies (not (characterp x))
                    (not (acl2::lower-case-p x)))
           :hints(("Goal" :in-theory (enable acl2::lower-case-p)))))

  (local (defthm lemma5
           (equal (acl2::lower-case-p x)
                  (and (<= (little-a) (char-code x))
                       (<= (char-code x) (little-z))))
           :hints(("Goal" :cases ((characterp x))))))

  (defthm lower-case-p-is-down-alpha-p
    (equal (acl2::lower-case-p x)
           (down-alpha-p (double-rewrite x))))

  ;; No longer necessary since we'll rewrite lower-case-p away
  (in-theory (disable acl2::lower-case-p-char-downcase))

  (defthm down-alpha-p-when-up-alpha-p
    (implies (up-alpha-p x)
             (not (down-alpha-p x)))
    :hints(("Goal" :in-theory (enable up-alpha-p
                                      down-alpha-p)))))



(defsection upcase-char
  :parents (cases acl2::char-upcase)
  :short "Convert a character to upper-case."

  :long "<p>@(call upcase-char) converts lower-case characters into their
upper-case equivalents, and returns other characters unchanged.</p>

<p>ACL2 has a built-in alternative to this function, @(see acl2::char-upcase),
but it is irritating to use because it has @(see standard-char-p) guards.  In
contrast, @('upcase-char') works on arbitrary characters.</p>"

  (definlined upcase-char (x)
    (declare (type character x))
    (b* (((the (unsigned-byte 8) code) (char-code x)))
      (if (and (<= (little-a) code)
               (<= code (little-z)))
          (code-char (the (unsigned-byte 8) (- code (case-delta))))
        (mbe :logic (char-fix x) :exec x))))

  (local (in-theory (enable upcase-char)))

  (defcong chareqv equal (upcase-char x) 1)

  (defthm upcase-char-does-nothing-unless-down-alpha-p
    (implies (not (down-alpha-p x))
             (equal (upcase-char x)
                    (char-fix x)))
    :hints(("Goal" :in-theory (enable down-alpha-p))))

  (defthm upcase-char-of-upcase-char
    (equal (upcase-char (upcase-char x))
           (upcase-char x)))

  ;; Rewrite ACL2's char-upcase to upcase-char.  It seems simplest to just do
  ;; the proof by exhaustive testing.

  (local (defund exhaustive-test (n)
           (and (let ((x (code-char n)))
                  (equal (acl2::char-upcase x)
                         (if (and (<= (little-a) (char-code x))
                                  (<= (char-code x) (little-z)))
                             (code-char (- (char-code x) (case-delta)))
                           x)))
                (or (zp n)
                    (exhaustive-test (- n 1))))))

  (local (defthm lemma1
           (implies (and (natp n)
                         (<= n 255))
                    (equal (char-code (code-char n))
                           n))))

  (local (defthm lemma2
           (implies (and (natp n)
                         (natp m)
                         (exhaustive-test n)
                         (= x (code-char m))
                         (<= m n)
                         (<= n 255))
                    (equal (acl2::char-upcase x)
                           (if (and (<= (little-a) (char-code x))
                                    (<= (char-code x) (little-z)))
                               (code-char (- (char-code x) (case-delta)))
                             x)))
           :hints(("Goal"
                   :induct (exhaustive-test n)
                   :in-theory (enable exhaustive-test)))))

  (local (defthm lemma3
           (implies (characterp x)
                    (equal (acl2::char-upcase x)
                           (if (and (<= (little-a) (char-code x))
                                    (<= (char-code x) (little-z)))
                               (code-char (- (char-code x) (case-delta)))
                             x)))
           :hints(("Goal"
                   :in-theory (disable lemma2)
                   :use ((:instance lemma2
                                    (n 255)
                                    (m (char-code x))))))))

  (local (defthm lemma4
           (implies (not (characterp x))
                    (equal (acl2::char-upcase x)
                           (code-char 0)))
           :hints(("Goal" :in-theory (enable acl2::char-upcase)))))

  (local (defthm lemma5
           (equal (acl2::char-upcase x)
                  (if (characterp x)
                      (if (and (<= (little-a) (char-code x))
                               (<= (char-code x) (little-z)))
                          (code-char (- (char-code x) (case-delta)))
                        x)
                    (code-char 0)))))

  (defthm char-upcase-is-upcase-char
    (equal (acl2::char-upcase x)
           (upcase-char (double-rewrite x)))))




(defsection downcase-char
  :parents (cases acl2::char-downcase)
  :short "Convert a character to lower-case."

  :long "<p>@(call downcase-char) converts upper-case characters into their
lower-case equivalents, and returns other characters unchanged.</p>

<p>ACL2 has a built-in alternative to this function, @(see
acl2::char-downcase), but it is irritating to use because it has @(see
standard-char-p) guards.  In contrast, @('downcase-char') works on arbitrary
characters.</p>"

  (definlined downcase-char (x)
    (declare (type character x))
    (b* (((the (unsigned-byte 8) code) (char-code x)))
      (if (and (<= (big-a) code)
               (<= code (big-z)))
          (code-char (the (unsigned-byte 8) (+ code (case-delta))))
        (mbe :logic (char-fix x) :exec x))))

  (local (in-theory (enable downcase-char)))

  (defcong chareqv equal (downcase-char x) 1)

  (defthm downcase-char-does-nothing-unless-up-alpha-p
    (implies (not (up-alpha-p x))
             (equal (downcase-char x)
                    (char-fix x)))
    :hints(("Goal" :in-theory (enable up-alpha-p))))

  (defthm downcase-char-of-downcase-char
    (equal (downcase-char (downcase-char x))
           (downcase-char x)))

  (defthm downcase-char-of-upcase-char
    (equal (downcase-char (upcase-char x))
           (downcase-char x))
    :hints(("Goal" :in-theory (enable upcase-char
                                      char-fix))))

  (defthm upcase-char-of-downcase-char
    (equal (upcase-char (downcase-char x))
           (upcase-char x))
    :hints(("Goal" :in-theory (enable upcase-char
                                      char-fix))))

  ;; Rewrite ACL2's char-downcase to downcase-char.  It seems simplest to just do
  ;; the proof by exhaustive testing.

  (local (defund exhaustive-test (n)
           (and (let ((x (code-char n)))
                  (equal (acl2::char-downcase x)
                         (if (and (<= (big-a) (char-code x))
                                  (<= (char-code x) (big-z)))
                             (code-char (+ (char-code x) (case-delta)))
                           x)))
                (or (zp n)
                    (exhaustive-test (- n 1))))))

  (local (defthm lemma1
           (implies (and (natp n)
                         (<= n 255))
                    (equal (char-code (code-char n))
                           n))))

  (local (defthm lemma2
           (implies (and (natp n)
                         (natp m)
                         (exhaustive-test n)
                         (= x (code-char m))
                         (<= m n)
                         (<= n 255))
                    (equal (acl2::char-downcase x)
                           (if (and (<= (big-a) (char-code x))
                                    (<= (char-code x) (big-z)))
                               (code-char (+ (char-code x) (case-delta)))
                             x)))
           :hints(("Goal"
                   :induct (exhaustive-test n)
                   :in-theory (enable exhaustive-test)))))

  (local (defthm lemma3
           (implies (characterp x)
                    (equal (acl2::char-downcase x)
                           (if (and (<= (big-a) (char-code x))
                                    (<= (char-code x) (big-z)))
                               (code-char (+ (char-code x) (case-delta)))
                             x)))
           :hints(("Goal"
                   :in-theory (disable lemma2)
                   :use ((:instance lemma2
                                    (n 255)
                                    (m (char-code x))))))))

  (local (defthm lemma4
           (implies (not (characterp x))
                    (equal (acl2::char-downcase x)
                           (code-char 0)))
           :hints(("Goal" :in-theory (enable acl2::char-downcase)))))

  (local (defthm lemma5
           (equal (acl2::char-downcase x)
                  (if (characterp x)
                      (if (and (<= (big-a) (char-code x))
                               (<= (char-code x) (big-z)))
                          (code-char (+ (char-code x) (case-delta)))
                        x)
                    (code-char 0)))))

  (defthm char-downcase-is-downcase-char
    (equal (acl2::char-downcase x)
           (downcase-char (double-rewrite x)))))



(defsection upcase-char-str
  :parents (cases)
  :short "Convert a character into an upper-case one-element string."
  :long "<p>@(call upcase-char-str) is logically equal to:</p>

@({
 (implode (list (upcase-char c)))
})

<p>But we store these strings in a table so that they don't have to be
recomputed.  This is mainly useful to reduce the creation of temporary strings
during @(see upcase-first).</p>"

  (defun make-upcase-first-strtbl (n)
    (declare (xargs :guard (and (natp n)
                                (<= n 255))
                    :ruler-extenders :all))
    (cons (cons n (implode (list (upcase-char (code-char n)))))
          (if (zp n)
              nil
            (make-upcase-first-strtbl (- n 1)))))

  (defconst *upcase-first-strtbl*
    (compress1 '*upcase-first-strtbl*
               (cons '(:header :dimensions (256)
                               :maximum-length 257)
                     (make-upcase-first-strtbl 255))))

  (local (in-theory (disable aref1)))

  (local (defun test (n)
           (declare (xargs :ruler-extenders :all))
           (and (equal (aref1 '*upcase-first-strtbl* *upcase-first-strtbl* n)
                       (implode (list (upcase-char (code-char n)))))
                (if (zp n)
                    t
                  (test (- n 1))))))

  (local (defthm l0
           (implies (and (natp i)
                         (natp n)
                         (<= i n)
                         (<= n 255)
                         (test n))
                    (equal (aref1 '*upcase-first-strtbl* *upcase-first-strtbl* i)
                           (implode (list (upcase-char (code-char i))))))
           :hints(("Goal" :induct (test n)))))

  (local (defthm l1
           (implies (and (natp i)
                         (<= i 255))
                    (equal (aref1 '*upcase-first-strtbl* *upcase-first-strtbl* i)
                           (implode (list (upcase-char (code-char i))))))
           :hints(("Goal" :use ((:instance l0 (n 255)))))))

  (definline upcase-char-str (c)
    (declare (type character c))
    (mbe :logic (implode (list (upcase-char c)))
         :exec (aref1 '*upcase-first-strtbl* *upcase-first-strtbl* (char-code c)))))



(defsection downcase-char-str
  :parents (cases)
  :short "Convert a character into a lower-case one-element string."
  :long "<p>@(call downcase-char-str) is logically equal to:</p>

@({
 (implode (downcase-char c))
})

<p>But we store these strings in a table so that they don't have to be
recomputed.  This is mainly useful to reduce the creation of temporary strings
during @(see downcase-first).</p>"

  (defun make-downcase-first-strtbl (n)
     (declare (xargs :guard (and (natp n)
                                 (<= n 255))
                     :ruler-extenders :all))
    (cons (cons n (implode (list (downcase-char (code-char n)))))
          (if (zp n)
              nil
            (make-downcase-first-strtbl (- n 1)))))

  (defconst *downcase-first-strtbl*
    (compress1 '*downcase-first-strtbl*
               (cons '(:header :dimensions (256)
                               :maximum-length 257)
                     (make-downcase-first-strtbl 255))))

  (local (in-theory (disable aref1)))

  (local (defun test (n)
           (declare (xargs :ruler-extenders :all))
           (and (equal (aref1 '*downcase-first-strtbl* *downcase-first-strtbl* n)
                       (implode (list (downcase-char (code-char n)))))
                (if (zp n)
                    t
                  (test (- n 1))))))

  (local (defthm l0
           (implies (and (natp i)
                         (natp n)
                         (<= i n)
                         (<= n 255)
                         (test n))
                    (equal (aref1 '*downcase-first-strtbl* *downcase-first-strtbl* i)
                           (implode (list (downcase-char (code-char i))))))
           :hints(("Goal" :induct (test n)))))

  (local (defthm l1
           (implies (and (natp i)
                         (<= i 255))
                    (equal (aref1 '*downcase-first-strtbl* *downcase-first-strtbl* i)
                           (implode (list (downcase-char (code-char i))))))
           :hints(("Goal" :use ((:instance l0 (n 255)))))))

  (definline downcase-char-str (c)
    (declare (type character c))
    (mbe :logic (implode (list (downcase-char c)))
         :exec (aref1 '*downcase-first-strtbl* *downcase-first-strtbl* (char-code c)))))