This file is indexed.

/usr/share/acl2-8.0dfsg/books/unicode/utf8-table37.lisp is in acl2-books-source 8.0dfsg-1.

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
; Processing Unicode Files with ACL2
; Copyright (C) 2005-2006 Kookamara LLC
;
; Contact:
;
;   Kookamara LLC
;   11410 Windermere Meadows
;   Austin, TX 78759, USA
;   http://www.kookamara.com/
;
; License: (An MIT/X11-style license)
;
;   Permission is hereby granted, free of charge, to any person obtaining a
;   copy of this software and associated documentation files (the "Software"),
;   to deal in the Software without restriction, including without limitation
;   the rights to use, copy, modify, merge, publish, distribute, sublicense,
;   and/or sell copies of the Software, and to permit persons to whom the
;   Software is furnished to do so, subject to the following conditions:
;
;   The above copyright notice and this permission notice shall be included in
;   all copies or substantial portions of the Software.
;
;   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
;   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
;   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
;   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
;   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
;   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
;   DEALINGS IN THE SOFTWARE.
;
; Original author: Jared Davis <jared@kookamara.com>

(in-package "ACL2")
(include-book "uchar")
(include-book "std/typed-lists/unsigned-byte-listp" :dir :system)
(local (include-book "centaur/bitops/ihsext-basics" :dir :system))
(local (include-book "centaur/bitops/signed-byte-p" :dir :system))
(local (include-book "std/typed-lists/signed-byte-listp" :dir :system))


(local (defthm signed-byte-p-resolver
         (implies (and (integerp n)
                       (<= 1 n)
                       (integerp x)
                       (<= (- (expt 2 (1- n))) x)
                       (< x (expt 2 (1- n))))
                  (signed-byte-p n x))
         :hints(("Goal" :in-theory (enable signed-byte-p)))))

;; UTF-8 sequences are also required to satisfy the informal constraints as
;; established in Table 3-7, which we recreate below:
;;
;;  Row   Code Points          1st Byte    2nd Byte    3rd Byte    4th Byte
;;  1     U+0000..U+007F       00..7F
;;  2     U+0080..U+07FF       C2..DF      80..BF
;;  3     U+0800..U+0FFF       E0          A0..BF      80..BF
;;  4     U+1000..U+CFFF       E1..EC      80..BF      80..BF
;;  5     U+D000..U+D7FF       ED          80..9F      80..BF
;;  6     U+E000..U+FFFF       EE..EF      80..BF      80..BF
;;  7     U+10000..U+3FFFF     F0          90..BF      80..BF      80..BF
;;  8     U+40000..U+FFFFF     F1..F3      80..BF      80..BF      80..BF
;;  9     U+100000..U+10FFFF   F4          80..8F      80..BF      80..BF
;;
;; To ensure that these constraints are respected by our UTF-8 functions, we
;; formalize the constraints of this table rigorously below.
;;
;; Because we are formalizing the Unicode specification here, the thorough
;; reader should check to ensure that our formalization is in agreement with
;; the Unicode standard.  In particular, one should check that our copy of
;; Table 3-7 above is an accurate copy of Table 3-7 from the Standard, and
;; should also convince themselves that the functions below accurately capture
;; the meaning of these ranges.


;; We begin by explaining what codepoints are acceptable entries for each row.
;; Note that the code points accepted by each row are subsumed by the later
;; rows, i.e., any codepoint which is acceptable under row 1 is also acceptable
;; under row 2 by simply letting yyyyy = 00000.  We do nothing to try to
;; require least row constraints, since these are handled by Table 3-7.

(defund utf8-table37-codepoint-1? (x)
  "Return true iff x matches U+0000..U+007F, i.e., if it could be a
   codepoint for Row 1 in Table 3-7."
  (declare (xargs :guard (uchar? x)))
  (and (mbt (uchar? x))
       (in-range? (the-fixnum x) 0 #x7F)))

(defund utf8-table37-codepoint-2? (x)
  "Return true iff x matches U+0080..U+07FF, i.e., if it could be a
   codepoint for Row 2 in Table 3-7."
  (declare (xargs :guard (uchar? x)))
  (and (mbt (uchar? x))
       (in-range? (the-fixnum x) #x80 #x7FF)))

(defund utf8-table37-codepoint-3? (x)
  "Return true iff x matches U+0800..U+0FFF, i.e., if it could be a
   codepoint for Row 3 in Table 3-7."
  (declare (xargs :guard (uchar? x)))
  (and (mbt (uchar? x))
       (in-range? (the-fixnum x) #x800 #xFFF)))

(defund utf8-table37-codepoint-4? (x)
  "Return true iff x matches U+1000..U+CFFF, i.e., if it could be a
   codepoint for Row 4 in Table 3-7."
  (declare (xargs :guard (uchar? x)))
  (and (mbt (uchar? x))
       (in-range? (the-fixnum x) #x1000 #xCFFF)))

(defund utf8-table37-codepoint-5? (x)
  "Return true iff x matches U+D000..U+D7FF, i.e., if it could be a
   codepoint for Row 5 in Table 3-7."
  (declare (xargs :guard (uchar? x)))
  (and (mbt (uchar? x))
       (in-range? (the-fixnum x) #xD000 #xD7FF)))

(defund utf8-table37-codepoint-6? (x)
  "Return true iff x matches U+E000..U+FFFF, i.e., if it could be a
   codepoint for Row 6 in Table 3-7."
  (declare (xargs :guard (uchar? x)))
  (and (mbt (uchar? x))
       (in-range? (the-fixnum x) #xE000 #xFFFF)))

(defund utf8-table37-codepoint-7? (x)
  "Return true iff x matches U+10000..U+3FFFF, i.e., if it could be a
   codepoint for Row 7 in Table 3-7."
  (declare (xargs :guard (uchar? x)))
  (and (mbt (uchar? x))
       (in-range? (the-fixnum x) #x10000 #x3FFFF)))

(defund utf8-table37-codepoint-8? (x)
  "Return true iff x matches U+40000..U+FFFFF, i.e., if it could be a
   codepoint for Row 8 in Table 3-7."
  (declare (xargs :guard (uchar? x)))
  (and (mbt (uchar? x))
       (in-range? (the-fixnum x) #x40000 #xFFFFF)))

(defund utf8-table37-codepoint-9? (x)
  "Return true iff x matches U+100000..U+10FFFF, i.e., if it could be a
   codepoint for Row 9 in Table 3-7."
  (declare (xargs :guard (uchar? x)))
  (and (mbt (uchar? x))
       (in-range? (the-fixnum x) #x100000 #x10FFFF)))

(deftheory utf8-table37-codepoints
  '(utf8-table37-codepoint-1?
    utf8-table37-codepoint-2?
    utf8-table37-codepoint-3?
    utf8-table37-codepoint-4?
    utf8-table37-codepoint-5?
    utf8-table37-codepoint-6?
    utf8-table37-codepoint-7?
    utf8-table37-codepoint-8?
    utf8-table37-codepoint-9?))



;; We now enumerate the valid entries for the bytes of each row in Table 3-7.
;; For example, utf8-table37-bytes-4? ensures that x is a sequence of bytes
;; which matches row 4 in Table 3-7.

(defund utf8-table37-bytes-1? (x)
  "Return true iff x matches the bytes in Row 1 of Table 3-7."
  (declare (xargs :guard (and (unsigned-byte-listp 8 x)
                              (equal (len x) 1))))
  (and (mbt (unsigned-byte-listp 8 x))
       (mbt (equal (len x) 1))
       (<= (the-fixnum (first x)) #x7F)))

(defund utf8-table37-bytes-2? (x)
  "Return true iff x matches the bytes in Row 2 of Table 3-7."
  (declare (xargs :guard (and (unsigned-byte-listp 8 x)
                              (equal (len x) 2))))
  (and (mbt (unsigned-byte-listp 8 x))
       (mbt (equal (len x) 2))
       (in-range? (the-fixnum (first x))  #xC2 #xDF)
       (in-range? (the-fixnum (second x)) #x80 #xBF)))

(defund utf8-table37-bytes-3? (x)
  "Return true iff x matches the bytes in Row 3 of Table 3-7."
  (declare (xargs :guard (and (unsigned-byte-listp 8 x)
                              (equal (len x) 3))))
  (and (mbt (unsigned-byte-listp 8 x))
       (mbt (equal (len x) 3))
       (= (the-fixnum (first x)) #xE0)
       (in-range? (the-fixnum (second x)) #xA0 #xBF)
       (in-range? (the-fixnum (third x))  #x80 #xBF)))

(defund utf8-table37-bytes-4? (x)
  "Return true iff x matches the bytes in Row 4 of Table 3-7."
  (declare (xargs :guard (and (unsigned-byte-listp 8 x)
                              (equal (len x) 3))))
  (and (mbt (unsigned-byte-listp 8 x))
       (mbt (equal (len x) 3))
       (in-range? (the-fixnum (first x))  #xE1 #xEC)
       (in-range? (the-fixnum (second x)) #x80 #xBF)
       (in-range? (the-fixnum (third x))  #x80 #xBF)))

(defund utf8-table37-bytes-5? (x)
  "Return true iff x matches the bytes in Row 5 of Table 3-7."
  (declare (xargs :guard (and (unsigned-byte-listp 8 x)
                              (equal (len x) 3))))
  (and (mbt (unsigned-byte-listp 8 x))
       (mbt (equal (len x) 3))
       (= (the-fixnum (first x)) #xED)
       (in-range? (the-fixnum (second x)) #x80 #x9F)
       (in-range? (the-fixnum (third x))  #x80 #xBF)))

(defund utf8-table37-bytes-6? (x)
  "Return true iff x matches the bytes in Row 6 of Table 3-7."
  (declare (xargs :guard (and (unsigned-byte-listp 8 x)
                              (equal (len x) 3))))
  (and (mbt (unsigned-byte-listp 8 x))
       (mbt (equal (len x) 3))
       (in-range? (the-fixnum (first x))  #xEE #xEF)
       (in-range? (the-fixnum (second x)) #x80 #xBF)
       (in-range? (the-fixnum (third x))  #x80 #xBF)))

(defund utf8-table37-bytes-7? (x)
  "Return true iff x matches the bytes in Row 7 of Table 3-7."
  (declare (xargs :guard (and (unsigned-byte-listp 8 x)
                              (equal (len x) 4))))
  (and (mbt (unsigned-byte-listp 8 x))
       (mbt (equal (len x) 4))
       (= (the-fixnum (first x)) #xF0)
       (in-range? (the-fixnum (second x)) #x90 #xBF)
       (in-range? (the-fixnum (third x))  #x80 #xBF)
       (in-range? (the-fixnum (fourth x)) #x80 #xBF)))

(defund utf8-table37-bytes-8? (x)
  "Return true iff x matches the bytes in Row 8 of Table 3-7."
  (declare (xargs :guard (and (unsigned-byte-listp 8 x)
                              (equal (len x) 4))))
  (and (mbt (unsigned-byte-listp 8 x))
       (mbt (equal (len x) 4))
       (in-range? (the-fixnum (first x))  #xF1 #xF3)
       (in-range? (the-fixnum (second x)) #x80 #xBF)
       (in-range? (the-fixnum (third x))  #x80 #xBF)
       (in-range? (the-fixnum (fourth x)) #x80 #xBF)))

(defund utf8-table37-bytes-9? (x)
  "Return true iff x matches the bytes in Row 9 of Table 3-7."
  (declare (xargs :guard (and (unsigned-byte-listp 8 x)
                              (equal (len x) 4))))
  (and (mbt (unsigned-byte-listp 8 x))
       (mbt (equal (len x) 4))
       (= (the-fixnum (first x)) #xF4)
       (in-range? (the-fixnum (second x)) #x80 #x8F)
       (in-range? (the-fixnum (third x))  #x80 #xBF)
       (in-range? (the-fixnum (fourth x)) #x80 #xBF)))

(deftheory utf8-table37-bytes
  '(utf8-table37-bytes-1?
    utf8-table37-bytes-2?
    utf8-table37-bytes-3?
    utf8-table37-bytes-4?
    utf8-table37-bytes-5?
    utf8-table37-bytes-6?
    utf8-table37-bytes-7?
    utf8-table37-bytes-8?
    utf8-table37-bytes-9?))



;; We now merge our codepoint checking and byte-sequence checking functions, to
;; create complete checks for entire rows of Table 3-7.  For example,
;; utf8-table37-row-4? checks to ensure that a codepoint and byte sequence are
;; valid under Row 4 of Table 3-7.

(defund utf8-table37-row-1? (cp x)
  "True iff cp is a codepoint and x is a byte sequence which match the
   first row in Table 3-7."
  (declare (xargs :guard (and (uchar? cp)
                              (unsigned-byte-listp 8 x)
                              (equal (len x) 1))))
  (and (mbt (uchar? cp))
       (mbt (unsigned-byte-listp 8 x))
       (mbt (equal (len x) 1))
       (utf8-table37-codepoint-1? cp)
       (utf8-table37-bytes-1? x)))

(defund utf8-table37-row-2? (cp x)
  "True iff cp is a codepoint and x is a byte sequence which match the
   second row in Table 3-7."
  (declare (xargs :guard (and (uchar? cp)
                              (unsigned-byte-listp 8 x)
                              (equal (len x) 2))))
  (and (mbt (uchar? cp))
       (mbt (unsigned-byte-listp 8 x))
       (mbt (equal (len x) 2))
       (utf8-table37-codepoint-2? cp)
       (utf8-table37-bytes-2? x)))

(defund utf8-table37-row-3? (cp x)
  "True iff cp is a codepoint and x is a byte sequence which match the
   third row of Table 3-7."
  (declare (xargs :guard (and (uchar? cp)
                              (unsigned-byte-listp 8 x)
                              (equal (len x) 3))))
  (and (mbt (uchar? cp))
       (mbt (unsigned-byte-listp 8 x))
       (mbt (equal (len x) 3))
       (utf8-table37-codepoint-3? cp)
       (utf8-table37-bytes-3? x)))

(defund utf8-table37-row-4? (cp x)
  "True iff cp is a codepoint and x is a byte sequence which match the
   fourth row of Table 3-7."
  (declare (xargs :guard (and (uchar? cp)
                              (unsigned-byte-listp 8 x)
                              (equal (len x) 3))))
  (and (mbt (uchar? cp))
       (mbt (unsigned-byte-listp 8 x))
       (mbt (equal (len x) 3))
       (utf8-table37-codepoint-4? cp)
       (utf8-table37-bytes-4? x)))

(defund utf8-table37-row-5? (cp x)
  "True iff cp is a codepoint and x is a byte sequence which match the
   fifth row of Table 3-7."
  (declare (xargs :guard (and (uchar? cp)
                              (unsigned-byte-listp 8 x)
                              (equal (len x) 3))))
  (and (mbt (uchar? cp))
       (mbt (unsigned-byte-listp 8 x))
       (mbt (equal (len x) 3))
       (utf8-table37-codepoint-5? cp)
       (utf8-table37-bytes-5? x)))

(defund utf8-table37-row-6? (cp x)
  "True iff cp is a codepoint and x is a byte sequence which match the
   sixth row of Table 3-7."
  (declare (xargs :guard (and (uchar? cp)
                              (unsigned-byte-listp 8 x)
                              (equal (len x) 3))))
  (and (mbt (uchar? cp))
       (mbt (unsigned-byte-listp 8 x))
       (mbt (equal (len x) 3))
       (utf8-table37-codepoint-6? cp)
       (utf8-table37-bytes-6? x)))

(defund utf8-table37-row-7? (cp x)
  "True iff cp is a codepoint and x is a byte sequence which match the
   seventh row of Table 3-7."
  (declare (xargs :guard (and (uchar? cp)
                              (unsigned-byte-listp 8 x)
                              (equal (len x) 4))))
  (and (mbt (uchar? cp))
       (mbt (unsigned-byte-listp 8 x))
       (mbt (equal (len x) 4))
       (utf8-table37-codepoint-7? cp)
       (utf8-table37-bytes-7? x)))

(defund utf8-table37-row-8? (cp x)
  "True iff cp is a codepoint and x is a byte sequence which match the
   eighth row of Table 3-7."
  (declare (xargs :guard (and (uchar? cp)
                              (unsigned-byte-listp 8 x)
                              (equal (len x) 4))))
  (and (mbt (uchar? cp))
       (mbt (unsigned-byte-listp 8 x))
       (mbt (equal (len x) 4))
       (utf8-table37-codepoint-8? cp)
       (utf8-table37-bytes-8? x)))

(defund utf8-table37-row-9? (cp x)
  "True iff cp is a codepoint and x is a byte sequence which match the
   ninth row of Table 3-7."
  (declare (xargs :guard (and (uchar? cp)
                              (unsigned-byte-listp 8 x)
                              (equal (len x) 4))))
  (and (mbt (uchar? cp))
       (mbt (unsigned-byte-listp 8 x))
       (mbt (equal (len x) 4))
       (utf8-table37-codepoint-9? cp)
       (utf8-table37-bytes-9? x)))

(deftheory utf8-table37-rows
  '(utf8-table37-row-1?
    utf8-table37-row-2?
    utf8-table37-row-3?
    utf8-table37-row-4?
    utf8-table37-row-5?
    utf8-table37-row-6?
    utf8-table37-row-7?
    utf8-table37-row-8?
    utf8-table37-row-9?))

(local (defthm utf8-table37-length-lemmas
         (and (implies (utf8-table37-row-1? cp x) (equal (len x) 1))
              (implies (utf8-table37-row-2? cp x) (equal (len x) 2))
              (implies (utf8-table37-row-3? cp x) (equal (len x) 3))
              (implies (utf8-table37-row-4? cp x) (equal (len x) 3))
              (implies (utf8-table37-row-5? cp x) (equal (len x) 3))
              (implies (utf8-table37-row-6? cp x) (equal (len x) 3))
              (implies (utf8-table37-row-7? cp x) (equal (len x) 4))
              (implies (utf8-table37-row-8? cp x) (equal (len x) 4))
              (implies (utf8-table37-row-9? cp x) (equal (len x) 4)))
         :hints(("Goal" :in-theory (enable utf8-table37-rows)))))

(defund utf8-table37-ok? (cp x)
  "True iff cp is a codepoint and x is a byte sequence which match some
   row in Table 3-7."
  (declare (xargs :guard (and (uchar? cp)
                              (unsigned-byte-listp 8 x)
                              (<= 1 (len x))
                              (<= (len x) 4))))
  (mbe :logic (or (utf8-table37-row-1? cp x)
                  (utf8-table37-row-2? cp x)
                  (utf8-table37-row-3? cp x)
                  (utf8-table37-row-4? cp x)
                  (utf8-table37-row-5? cp x)
                  (utf8-table37-row-6? cp x)
                  (utf8-table37-row-7? cp x)
                  (utf8-table37-row-8? cp x)
                  (utf8-table37-row-9? cp x))
       :exec (case (len x)
               (1 (utf8-table37-row-1? cp x))
               (2 (utf8-table37-row-2? cp x))
               (3 (or (utf8-table37-row-3? cp x)
                      (utf8-table37-row-4? cp x)
                      (utf8-table37-row-5? cp x)
                      (utf8-table37-row-6? cp x)))
               (4 (or (utf8-table37-row-7? cp x)
                      (utf8-table37-row-8? cp x)
                      (utf8-table37-row-9? cp x)))
               (otherwise nil))))

(defthm len-of-bytes-when-utf8-table37-ok?
  (implies (utf8-table37-ok? cp x)
           (and (<= 1 (len x))
                (<= (len x) 4)))
  :rule-classes :linear
  :hints(("Goal" :in-theory (enable utf8-table37-ok?))))

(defthm uchar?-of-codepoint-when-utf8-table37-ok?
  (implies (utf8-table37-ok? cp x)
           (uchar? cp))
  :hints(("Goal" :in-theory (enable utf8-table37-ok?
                                    utf8-table37-rows))))

(defthm unsigned-byte-listp-of-bytes-when-utf8-table37-ok?
  (implies (utf8-table37-ok? cp x)
           (unsigned-byte-listp 8 x))
  :hints(("Goal" :in-theory (enable utf8-table37-ok?
                                    utf8-table37-rows))))