File: hex.lisp

package info (click to toggle)
acl2 7.2dfsg-3
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 198,968 kB
  • ctags: 182,300
  • sloc: lisp: 2,415,261; ansic: 5,675; perl: 5,577; xml: 3,576; sh: 3,255; cpp: 2,835; makefile: 2,440; ruby: 2,402; python: 778; ml: 763; yacc: 709; csh: 355; php: 171; lex: 162; tcl: 44; java: 24; asm: 23; haskell: 17
file content (932 lines) | stat: -rw-r--r-- 37,556 bytes parent folder | download | duplicates (3)
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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
; ACL2 String Library
; Copyright (C) 2009-2014 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/
;
; 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@centtech.com>

(in-package "STR")
(include-book "ieqv")
(include-book "std/basic/defs" :dir :system)
(include-book "std/util/deflist" :dir :system)
(include-book "ihs/basic-definitions" :dir :system)
(local (include-book "arithmetic"))
(local (include-book "misc/assert" :dir :system))
(local (include-book "centaur/bitops/ihs-extensions" :dir :system))

(local (defthm unsigned-byte-p-8-of-char-code
         (unsigned-byte-p 8 (char-code x))))

(local (in-theory (disable unsigned-byte-p
                           digit-to-char)))

(local (defthm unsigned-byte-p-8-of-new-nibble-on-the-front
         (implies (and (unsigned-byte-p 4 a)
                       (unsigned-byte-p n b))
                  (unsigned-byte-p (+ 4 n) (+ b (ash a n))))
         :hints(("Goal" :in-theory (acl2::enable* acl2::ihsext-recursive-redefs
                                                  acl2::ihsext-inductions)))))

(local (defthm unsigned-byte-p-8-of-new-nibble-on-the-front-alt
         (implies (and (unsigned-byte-p 4 a)
                       (unsigned-byte-p n b))
                  (unsigned-byte-p (+ 4 n) (+ (ash a n) b)))
         :hints(("Goal" :in-theory (acl2::enable* acl2::ihsext-recursive-redefs
                                                  acl2::ihsext-inductions)))))

(local (defthm shift-left-of-sum-of-integers
         (implies (and (natp n)
                       (integerp a)
                       (integerp b))
                  (equal (ash (+ a b) n)
                         (+ (ash a n)
                            (ash b n))))
         :hints(("Goal" :in-theory (acl2::enable* acl2::ihsext-recursive-redefs
                                                  acl2::ihsext-inductions)))))

(local (defthm logtail-decreases
         (implies (and (posp n)
                       (posp x))
                  (< (acl2::logtail n x) x))
         :rule-classes ((:rewrite) (:linear))
         :hints(("Goal" :in-theory (acl2::enable* acl2::ihsext-recursive-redefs
                                                  acl2::ihsext-inductions
                                                  acl2::logcons)))))

(defsection hex
  :parents (numbers)
  :short "Functions for working with hexadecimal numbers in strings.")

(local (xdoc::set-default-parents hex))

(define hex-digitp (x)
  :short "Recognizer for hexadecimal digit characters: 0-9, A-F, a-f."
  :returns bool
  :long "<p>ACL2 provides @(see digit-char-p) which is more flexible and can
recognize numeric characters in other bases.  @(call hex-digitp) only
recognizes base-16 digits, but is much faster, at least on CCL.  Here is an
experiment you can run in raw lisp, with times reported in CCL on an AMD
FX-8350.</p>

@({
  (defconstant *chars*
    (loop for i from 0 to 255 collect (code-char i)))

  ;; 19.216 seconds
  (time (loop for i fixnum from 1 to 10000000 do
              (loop for c in *chars* do (digit-char-p c 16))))

  ;; 4.711 seconds
  (time (loop for i fixnum from 1 to 10000000 do
              (loop for c in *chars* do (str::hex-digitp c))))
})"
  :inline t
  (mbe :logic (let ((code (char-code (char-fix x))))
                (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)))))
       :exec (and (characterp x)
                  (b* (((the (unsigned-byte 8) code) (char-code (the character x))))
                    ;; The ASCII ranges break down to: [48...57] or [65...70] or [97...102]
                    (if (<= code 70)
                        (if (<= code 57)
                            (<= 48 code)
                          (<= 65 code))
                      (and (<= code 102)
                           (<= 97 code))))))
  ///
  (defcong ichareqv equal (hex-digitp x) 1
    :hints(("Goal" :in-theory (enable ichareqv
                                      downcase-char
                                      char-fix))))

  (defthm characterp-when-hex-digitp
    (implies (hex-digitp char)
             (characterp char))
    :rule-classes :compound-recognizer)

  (local (defun test (n)
           (let ((x (code-char n)))
             (and (iff (hex-digitp x)
                       (member x '(#\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9
                                   #\a #\b #\c #\d #\e #\f
                                   #\A #\B #\C #\D #\E #\F)))
                  (or (zp n)
                      (test (- n 1)))))))

  (local (defthm l0
           (implies (and (test n)
                         (natp i)
                         (natp n)
                         (<= i n))
                    (let ((x (code-char i)))
                      (iff (hex-digitp x)
                           (member x '(#\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9
                                       #\a #\b #\c #\d #\e #\f
                                       #\A #\B #\C #\D #\E #\F)))))))

  (defthmd hex-digitp-cases
    (iff (hex-digitp x)
         (member x '(#\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9
                     #\a #\b #\c #\d #\e #\f
                     #\A #\B #\C #\D #\E #\F)))
    :hints(("Goal"
            :in-theory (e/d (member) (l0))
            :use ((:instance l0 (n 255) (i (char-code x))))))))


(define nonzero-hex-digitp (x)
  :short "Recognizer for non-zero hexadecimal digit characters: 1-9, A-F, a-f."
  :returns bool
  :inline t
  (mbe :logic (let ((code (char-code (char-fix x))))
                (or (and (<= (char-code #\1) code) (<= code (char-code #\9)))
                    (and (<= (char-code #\a) code) (<= code (char-code #\f)))
                    (and (<= (char-code #\A) code) (<= code (char-code #\F)))))
       :exec (and (characterp x)
                  (b* (((the (unsigned-byte 8) code) (char-code (the character x))))
                    (if (<= code 70)
                        (if (<= code 57)
                            (<= 49 code) ;; <-- 49 instead of 48, to exclude 0
                          (<= 65 code))
                      (and (<= code 102)
                           (<= 97 code))))))
  ///
  (defcong ichareqv equal (nonzero-hex-digitp x) 1
    :hints(("Goal" :in-theory (enable ichareqv
                                      downcase-char
                                      char-fix))))
  (defthm hex-digitp-when-nonzero-hex-digitp
    (implies (nonzero-hex-digitp x)
             (hex-digitp x))
    :hints(("Goal" :in-theory (enable hex-digitp)))))


(define hex-digit-val
  :short "Coerces a @(see hex-digitp) character into a number."
  ((x hex-digitp))
  :split-types t
  :returns (val natp :rule-classes :type-prescription)
  :long "<p>For instance, @('(hex-digit-val #\\a)') is 10.  For any non-@(see
         hex-digitp), 0 is returned.</p>"
  :inline t
  (mbe :logic
       (b* (((unless (hex-digitp x))
             0)
            (code (char-code x))
            ((when (<= (char-code #\a) code))
             (- code (- (char-code #\a) 10)))
            ((when (<= (char-code #\A) code))
             (- code (- (char-code #\A) 10))))
         (- code (char-code #\0)))
       :exec
       (b* (((the (unsigned-byte 8) code) (char-code (the character x)))
            ((when (<= 97 code))
             (the (unsigned-byte 8) (- code 87)))
            ((when (<= 65 code))
             (the (unsigned-byte 8) (- code 55))))
         (the (unsigned-byte 8) (- code 48))))
  :prepwork
  ((local (in-theory (enable hex-digitp 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))))
  (defcong ichareqv equal (hex-digit-val x) 1
    :hints(("Goal" :in-theory (enable ichareqv downcase-char))))
  (defthm hex-digit-val-upper-bound
    (< (hex-digit-val x) 16)
    :rule-classes ((:rewrite) (:linear)))
  (defthm unsigned-byte-p-of-hex-digit-val
    (unsigned-byte-p 4 (hex-digit-val x)))
  (defthm equal-of-hex-digit-val-and-hex-digit-val
    (implies (and (hex-digitp x)
                  (hex-digitp y))
             (equal (equal (hex-digit-val x) (hex-digit-val y))
                    (ichareqv x y)))
    :hints(("Goal" :in-theory (enable hex-digitp-cases))))
  (defthm hex-digit-val-of-digit-to-char
    (implies (and (natp n)
                  (< n 16))
             (equal (hex-digit-val (digit-to-char n))
                    n))
    :hints(("Goal" :in-theory (enable digit-to-char)))))


(std::deflist hex-digit-listp (x)
  :short "Recognizes lists of @(see hex-digitp) characters."
  (hex-digitp x)
  ///
  (defcong icharlisteqv equal (hex-digit-listp x) 1
    :hints(("Goal" :in-theory (enable icharlisteqv))))
  (defthm character-listp-when-hex-digit-listp
    (implies (hex-digit-listp x)
             (equal (character-listp x)
                    (true-listp x)))
    :rule-classes ((:rewrite :backchain-limit-lst 1))))


(define hex-digit-list-value1
  :parents (hex-digit-list-value)
  ((x hex-digit-listp)
   (val :type unsigned-byte))
  :guard-debug t
  (if (consp x)
      (hex-digit-list-value1
       (cdr x)
       (the unsigned-byte
            (+ (the (unsigned-byte 4) (hex-digit-val (car x)))
               (the unsigned-byte (ash (mbe :logic (lnfix val)
                                            :exec val)
                                       4)))))
    (lnfix val)))

(define hex-digit-list-value
  :short "Coerces a @(see hex-digit-listp) into a natural number."
  ((x hex-digit-listp))
  :returns (value natp :rule-classes :type-prescription)
  :long "<p>For instance, @('(hex-digit-list-value '(#\1 #\F))') is 31.  See
         also @(see parse-hex-from-charlist) for a more flexible function that
         can tolerate non-hexadecimal characters after the number.</p>"
  :inline t
  :verify-guards nil
  (mbe :logic (if (consp x)
                  (+ (ash (hex-digit-val (car x)) (* 4 (1- (len x))))
                     (hex-digit-list-value (cdr x)))
                0)
       :exec (hex-digit-list-value1 x 0))
  ///
  (defcong icharlisteqv equal (hex-digit-list-value x) 1
    :hints(("Goal" :in-theory (e/d (icharlisteqv)))))
  (defthm unsigned-byte-p-of-hex-digit-list-value
    (unsigned-byte-p (* 4 (len x)) (hex-digit-list-value x)))
  (defthm hex-digit-list-value-upper-bound
    (< (hex-digit-list-value x)
       (expt 2 (* 4 (len x))))
    :rule-classes ((:rewrite) (:linear))
    :hints(("Goal"
            :in-theory (e/d (unsigned-byte-p)
                            (unsigned-byte-p-of-hex-digit-list-value))
            :use ((:instance unsigned-byte-p-of-hex-digit-list-value)))))
  (defthm hex-digit-list-value-upper-bound-free
    (implies (equal n (len x))
             (< (hex-digit-list-value x) (expt 2 (* 4 n)))))
  (defthm hex-digit-list-value1-removal
    (implies (natp val)
             (equal (hex-digit-list-value1 x val)
                    (+ (hex-digit-list-value x)
                       (ash (nfix val) (* 4 (len x))))))
    :hints(("Goal"
            :in-theory (enable hex-digit-list-value1)
            :induct (hex-digit-list-value1 x val))))
  (verify-guards hex-digit-list-value$inline)
  (defthm hex-digit-list-value-of-append
    (equal (hex-digit-list-value (append x (list a)))
           (+ (ash (hex-digit-list-value x) 4)
              (hex-digit-val a))))
  (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)))))

(define skip-leading-hex-digits (x)
  :short "Skip over any leading hex digit characters at the start of a character list."
  :returns (tail character-listp :hyp (character-listp x))
  (cond ((atom x)             nil)
        ((hex-digitp (car x)) (skip-leading-hex-digits (cdr x)))
        (t                    x))
  ///
  (defcong charlisteqv charlisteqv (skip-leading-hex-digits x) 1
    :hints(("Goal" :in-theory (enable charlisteqv))))
  (defcong icharlisteqv icharlisteqv (skip-leading-hex-digits x) 1
    :hints(("Goal" :in-theory (enable icharlisteqv))))
  (defthm len-of-skip-leading-hex-digits
    (implies (hex-digitp (car x))
             (< (len (skip-leading-hex-digits x))
                (len x)))))

(define take-leading-hex-digits (x)
  :short "Collect any leading hex digit characters from the start of a character
          list."
  :returns (head character-listp :hyp (character-listp x))
  (cond ((atom x)             nil)
        ((hex-digitp (car x)) (cons (car x) (take-leading-hex-digits (cdr x))))
        (t                    nil))
  ///
  ;; Unlike decimal/binary/octal versions, here we don't get an EQUAL
  ;; congruence when given ichareqvlists, because we might have, e.g., #\a
  ;; versus #\A.  So, we end up with two different congruences.
  (defcong charlisteqv equal (take-leading-hex-digits x) 1
    :hints(("Goal" :in-theory (enable charlisteqv
                                      chareqv))))
  (defcong icharlisteqv icharlisteqv (take-leading-hex-digits x) 1
    :hints(("Goal" :in-theory (enable icharlisteqv))))
  (defthm hex-digit-listp-of-take-leading-hex-digits
    (hex-digit-listp (take-leading-hex-digits x)))
  (defthm bound-of-len-of-take-leading-hex-digits
    (<= (len (take-leading-hex-digits x)) (len x))
    :rule-classes :linear)
  (defthm equal-of-take-leading-hex-digits-and-length
    (equal (equal (len (take-leading-hex-digits x)) (len x))
           (hex-digit-listp x)))
  (defthm take-leading-hex-digits-when-hex-digit-listp
    (implies (hex-digit-listp x)
             (equal (take-leading-hex-digits x)
                    (list-fix x))))
  (defthm consp-of-take-leading-hex-digits
    (equal (consp (take-leading-hex-digits x))
           (hex-digitp (car x)))))

(define hex-digit-string-p-aux
  :parents (hex-digit-string-p)
  ((x  stringp             :type string)
   (n  natp                :type unsigned-byte)
   (xl (eql xl (length x)) :type unsigned-byte))
  :guard (<= n xl)
  :measure (nfix (- (nfix xl) (nfix n)))
  :split-types t
  :verify-guards nil
  :enabled t
  (mbe :logic
       (hex-digit-listp (nthcdr n (explode x)))
       :exec
       (if (eql n xl)
           t
         (and (hex-digitp (char x n))
              (hex-digit-string-p-aux x
                                      (the unsigned-byte (+ 1 n))
                                      xl))))
  ///
  (verify-guards hex-digit-string-p-aux
    :hints(("Goal" :in-theory (enable hex-digit-listp)))))

(define hex-digit-string-p
  :short "Recognizer for strings whose characters are hexadecimal digits."
  ((x :type string))
  :returns bool
  :long "<p>Corner case: this accepts the empty string since all of its
characters are hex digits.</p>

<p>Logically this is defined in terms of @(see hex-digit-listp).  But in the
execution, we use a @(see char)-based function that avoids exploding the
string.  This provides much better performance, e.g., on an AMD FX-8350 with
CCL:</p>

@({
    ;; 0.97 seconds, no garbage
    (let ((x \"deadbeef\"))
      (time$ (loop for i fixnum from 1 to 10000000 do
                   (str::hex-digit-string-p x))))

    ;; 1.74 seconds, 1.28 GB allocated
    (let ((x \"deadbeef\"))
      (time$ (loop for i fixnum from 1 to 10000000 do
                   (str::hex-digit-listp (explode x)))))
})"
  :inline t
  :enabled t
  (mbe :logic (hex-digit-listp (explode x))
       :exec (hex-digit-string-p-aux x 0 (length x)))
  ///
  (defcong istreqv equal (hex-digit-string-p x) 1))


(define hex-digit-to-char ((n :type (unsigned-byte 4)))
  :short "Convert a number from 0-15 into a hex character."
  :long "<p>ACL2 has a built-in version of this function, @(see digit-to-char),
but @('hex-digit-to-char') is faster:</p>

@({
  (defconstant *codes*
    (loop for i from 0 to 15 collect i))

  ;; .217 seconds, no garbage
  (time (loop for i fixnum from 1 to 10000000 do
              (loop for c in *codes* do (str::hex-digit-to-char c))))

  ;; .881 seconds, no garbage
  (time (loop for i fixnum from 1 to 10000000 do
              (loop for c in *codes* do (digit-to-char c))))

})"

  :guard-hints(("Goal" :in-theory (enable unsigned-byte-p
                                          digit-to-char)))
  :inline t
  :enabled t
  (mbe :logic (digit-to-char n)
       :exec
       (if (< n 10)
           (code-char (the (unsigned-byte 8) (+ 48 n)))
         ;; Naively this is (code-char A) + N-10
         ;; But we merge (code-char A) == 65 and -10 together to get 55.
         (code-char (the (unsigned-byte 8) (+ 55 n))))))

(define basic-natchars16
  :parents (natchars16)
  :short "Logically simple definition that is similar to @(see natchars16)."
  ((n natp))
  :returns (chars hex-digit-listp)
  :long "<p>This <i>almost</i> computes @('(natchars16 n)'), but when @('n') is
zero it returns @('nil') instead of @('(#\\0)').  You would normally never call
this function directly, but it is convenient for reasoning about @(see
natchars16).</p>"
  (if (zp n)
      nil
    (cons (hex-digit-to-char (logand n #xF))
          (basic-natchars16 (ash n -4))))
  :prepwork
  ((local (defthm l0
            (implies (and (< a 16)
                          (< b 16)
                          (natp a)
                          (natp b))
                     (equal (equal (digit-to-char a) (digit-to-char b))
                            (equal a b)))
            :hints(("Goal" :in-theory (enable digit-to-char)))))
   (local (defthm l1
            (implies (and (< a 16)
                          (natp a))
                     (hex-digitp (digit-to-char a)))
            :hints(("Goal" :in-theory (enable digit-to-char)))))
   (local (in-theory (disable digit-to-char))))
  ///
  (defthm basic-natchars16-when-zp
    (implies (zp n)
             (equal (basic-natchars16 n)
                    nil)))
  (defthm true-listp-of-basic-natchars16
    (true-listp (basic-natchars16 n))
    :rule-classes :type-prescription)
  (defthm character-listp-of-basic-natchars16
    (character-listp (basic-natchars16 n)))
  (defthm basic-natchars16-under-iff
    (iff (basic-natchars16 n)
         (not (zp n))))
  (defthm consp-of-basic-natchars16
    (equal (consp (basic-natchars16 n))
           (if (basic-natchars16 n) t nil)))
  (local (defun my-induction (n m)
           (if (or (zp n)
                   (zp m))
               nil
             (my-induction (ash n -4) (ash m -4)))))
  (local (defthm c0
           (implies (and (integerp x)
                         (natp n))
                    (equal (logior (ash (acl2::logtail n x) n) (acl2::loghead n x))
                           x))
           :hints(("Goal"
                   :in-theory (acl2::enable* acl2::ihsext-recursive-redefs
                                             acl2::ihsext-inductions)))))
  (local (defthm c1
           (implies (and (equal (acl2::logtail k n) (acl2::logtail k m))
                         (equal (acl2::loghead k n) (acl2::loghead k m))
                         (integerp n)
                         (integerp m)
                         (natp k))
                    (equal (equal n m)
                           t))
           :hints(("Goal"
                   :in-theory (disable c0)
                   :use ((:instance c0 (x n) (n k))
                         (:instance c0 (x m) (n k)))))))
  (defthm basic-natchars16-one-to-one
    (equal (equal (basic-natchars16 n)
                  (basic-natchars16 m))
           (equal (nfix n)
                  (nfix m)))
    :hints(("Goal" :induct (my-induction n m)))))

(define natchars16-aux ((n natp) acc)
  :parents (natchars16)
  :verify-guards nil
  :enabled t
  (mbe :logic
       (revappend (basic-natchars16 n) acc)
       :exec
       (if (zp n)
           acc
         (natchars16-aux
          (the unsigned-byte (ash (the unsigned-byte n) -4))
          (cons (hex-digit-to-char
                 (the (unsigned-byte 4) (logand (the unsigned-byte n) #xF)))
                acc))))
  ///
  (verify-guards natchars16-aux
    :hints(("Goal" :in-theory (enable basic-natchars16)))))

(define natchars16
  :short "Convert a natural number into a list of hexadecimal bits."
  ((n natp))
  :returns (chars hex-digit-listp)
  :long "<p>For instance, @('(natchars16 31)') is @('(#\\1 #\\F)').</p>

<p>This is like ACL2's built-in function @(see explode-nonnegative-integer),
except that it doesn't deal with accumulators and is limited to base-16 numbers.
These simplifications lead to particularly nice rules, e.g., about @(see
hex-digit-list-value), and somewhat better performance:</p>

@({
  ;; Times reported by an AMD FX-8350, Linux, 64-bit CCL:

  ;; .732 seconds, 942 MB allocated
  (progn (gc$)
         (time (loop for i fixnum from 1 to 10000000 do
                     (str::natchars16 i))))

  ;; 3.71 seconds, 942 MB allocated
  (progn (gc$)
         (time (loop for i fixnum from 1 to 10000000 do
            (explode-nonnegative-integer i 16 nil))))
})"
  :inline t
  (or (natchars16-aux n nil) '(#\0))
  ///

  (defthm true-listp-of-natchars16
    (and (true-listp (natchars16 n))
         (consp (natchars16 n)))
    :rule-classes :type-prescription)
  (defthm character-listp-of-natchars16
    (character-listp (natchars16 n)))
  (local (defthm lemma1
           (equal (equal (rev x) (list y))
                  (and (consp x)
                       (not (consp (cdr x)))
                       (equal (car x) y)))
           :hints(("Goal" :in-theory (enable rev)))))
  (local (defthm crock
           (IMPLIES (AND (EQUAL (ACL2::LOGTAIL n x) 0)
                         (EQUAL (ACL2::LOGHEAD n x) 0)
                         (natp n))
                    (zip x))
           :rule-classes :forward-chaining
           :hints(("Goal" :in-theory (acl2::enable* acl2::ihsext-inductions
                                                    acl2::ihsext-recursive-redefs
                                                    acl2::zip
                                                    acl2::zp)))))
  (local (defthm crock2
           (implies (AND (EQUAL (ACL2::LOGTAIL n x) 0)
                         (NOT (ZP x))
                         (natp n))
                    (< 0 (acl2::loghead n x)))
           :hints(("Goal" :in-theory (acl2::enable* acl2::ihsext-inductions
                                                    acl2::ihsext-recursive-redefs)))))
  (local (defthm crock3
           (equal (equal (digit-to-char n) #\0)
                  (or (zp n)
                      (< 15 n)))
           :hints(("Goal" :in-theory (enable digit-to-char)))))
  (local (defthmd lemma2
           (not (equal (basic-natchars16 n) '(#\0)))
           :hints(("Goal" :in-theory (acl2::enable* basic-natchars16
                                                    acl2::ihsext-recursive-redefs)))))
  (defthm natchars16-one-to-one
    (equal (equal (natchars16 n) (natchars16 m))
           (equal (nfix n) (nfix m)))
    :hints(("Goal"
            :in-theory (disable basic-natchars16-one-to-one)
            :use ((:instance basic-natchars16-one-to-one)
                  (:instance lemma2)
                  (:instance lemma2 (n m))))))
  (local (defthm c0
           (implies (and (integerp x)
                         (natp n))
                    (equal (+ (acl2::loghead n x) (ash (acl2::logtail n x) n))
                           x))
           :hints(("Goal"
                   :in-theory (acl2::enable* acl2::ihsext-recursive-redefs
                                             acl2::ihsext-inductions)))))
  (local (defthm hex-digit-list-value-of-rev-of-basic-natchars16
           (equal (hex-digit-list-value (rev (basic-natchars16 n)))
                  (nfix n))
           :hints(("Goal"
                   :induct (basic-natchars16 n)
                   :in-theory (acl2::enable* basic-natchars16
                                             acl2::ihsext-recursive-redefs
                                             acl2::logcons)))))
  (defthm hex-digit-list-value-of-natchars16
    (equal (hex-digit-list-value (natchars16 n))
           (nfix n))))

(define revappend-natchars16-aux ((n natp) (acc))
  :parents (revappend-natchars16)
  :enabled t
  :verify-guards nil
  (mbe :logic
       (append (basic-natchars16 n) acc)
       :exec
       (if (zp n)
           acc
         (cons (hex-digit-to-char (the (unsigned-byte 4)
                                       (logand (the unsigned-byte n) #xF)))
               (revappend-natchars16-aux
                (the unsigned-byte (ash (the unsigned-byte n) -4))
                acc))))
  ///
  (verify-guards revappend-natchars16-aux
    :hints(("Goal" :in-theory (enable basic-natchars16)))))

(define revappend-natchars16
  :short "More efficient version of @('(revappend (natchars16 n) acc).')"
  ((n natp)
   (acc))
  :returns (new-acc)
  :long "<p>This strange operation can be useful when building strings by consing
         together characters in reverse order.</p>"
  :inline t
  :enabled t
  :prepwork ((local (in-theory (enable natchars16))))
  (mbe :logic (revappend (natchars16 n) acc)
       :exec (if (zp n)
                 (cons #\0 acc)
               (revappend-natchars16-aux n acc))))

(define natstr16
  :short "Convert a natural number into a string with its hex digits."
  ((n natp))
  :returns (str stringp :rule-classes :type-prescription)
  :long "<p>For instance, @('(natstr16 31)') is @('\"1F\"').</p>"
  :inline t
  (implode (natchars16 n))
  ///
  (defthm hex-digit-listp-of-natstr
    (hex-digit-listp (explode (natstr16 n))))
  (defthm natstr16-one-to-one
    (equal (equal (natstr16 n) (natstr16 m))
           (equal (nfix n) (nfix m))))
  (defthm hex-digit-list-value-of-natstr
    (equal (hex-digit-list-value (explode (natstr16 n)))
           (nfix n)))
  (defthm natstr16-nonempty
    (not (equal (natstr16 n) ""))))

(define natstr16-list
  :short "Convert a list of natural numbers into a list of hex digit strings."
  ((x nat-listp))
  :returns (strs string-listp)
  (if (atom x)
      nil
    (cons (natstr16 (car x))
          (natstr16-list (cdr x))))
  ///
  (defthm natstr16-list-when-atom
    (implies (atom x)
             (equal (natstr16-list x)
                    nil)))
  (defthm natstr16-list-of-cons
    (equal (natstr16-list (cons a x))
           (cons (natstr16 a)
                 (natstr16-list x)))))


(define natsize16-aux
  :parents (natsize16)
  ((n natp))
  :returns (size natp :rule-classes :type-prescription)
  (if (zp n)
      0
    (+ 1 (natsize16-aux (ash n -4))))
  :prepwork ((local (in-theory (enable natchars16))))
  ///
  ;; BOZO perhaps eventually reimplement this using integer-length.  Here are
  ;; some fledgling steps toward that... natsize16 is probably something like:
  ;;   (+ 1 (ash (+ -1 (integer-length x)) -2)))

  ;; (local (defthm c1
  ;;          (equal (acl2::logtail 2 x)
  ;;                 (acl2::logcdr (acl2::logcdr x)))
  ;;          :hints(("Goal"
  ;;                  :expand ((acl2::logtail 2 x))
  ;;                  :in-theory (acl2::enable* acl2::ihsext-recursive-redefs
  ;;                                            acl2::logtail**)))))

  ;; (local (defthm c2
  ;;          (equal (acl2::logtail 4 x)
  ;;                 (acl2::logcdr (acl2::logcdr (acl2::logcdr (acl2::logcdr x)))))
  ;;          :hints(("Goal"
  ;;                  :expand ((acl2::logtail 4 x))
  ;;                  :in-theory (acl2::enable* acl2::ihsext-recursive-redefs
  ;;                                            acl2::logtail**)))))

  ;; (local (defthm natsize16-aux-redef
  ;;          (equal (natsize16-aux n)
  ;;                 (if (zp n)
  ;;                     0
  ;;                   (+ 1 (natsize16-aux
  ;;                         (acl2::logcdr (acl2::logcdr (acl2::logcdr (acl2::logcdr n))))))))
  ;;          :rule-classes ((:definition :clique (natsize16-aux)
  ;;                          :controller-alist ((natsize16-aux t))))))

  ;; (local (in-theory (disable natsize16-aux)))

  ;; (DEFTHM ACL2::INTEGER-LENGTH**
  ;;   (EQUAL (INTEGER-LENGTH I)
  ;;          (COND ((ZIP I) 0)
  ;;                ((EQUAL I -1) 0)
  ;;                (T (1+ (INTEGER-LENGTH (ACL2::LOGCDR I))))))
  ;;   :HINTS (("goal" :BY ACL2::INTEGER-LENGTH*))
  ;;   :RULE-CLASSES
  ;;   ((:DEFINITION :CLIQUE (INTEGER-LENGTH)
  ;;     :CONTROLLER-ALIST ((INTEGER-LENGTH T)))))

  (defthm natsize16-aux-redef
    (equal (natsize16-aux n)
           (len (basic-natchars16 n)))
    :hints(("Goal" :in-theory (enable basic-natchars16)))))

(define natsize16
  :short "Number of characters in the hexadecimal representation of a natural."
  ((x natp))
  :returns (size posp :rule-classes :type-prescription)
  :inline t
  (mbe :logic (len (natchars16 x))
       :exec
       (if (zp x)
           1
         (natsize16-aux x)))
  :prepwork ((local (in-theory (enable natchars16)))))

(define parse-hex-from-charlist
  :parents (numbers)
  :short "Parse a hexadecimal number from the beginning of a character list."
  ((x   character-listp "Characters to read from.")
   (val natp            "Accumulator for the value of the hex digits we have read
                         so far; typically 0 to start with.")
   (len natp            "Accumulator for the number of hex digits we have read;
                         typically 0 to start with."))
  :returns (mv (val  "Value of the initial hex digits as a natural number.")
               (len  "Number of initial hex digits we read.")
               (rest "The rest of @('x'), past the leading hex digits."))
  :split-types t
  (declare (type unsigned-byte val len))
  :long "<p>This is like @(call parse-nat-from-charlist), but deals with hex
         digits and returns their hexadecimal value.</p>"
  (cond ((atom x)
         (mv (lnfix val) (lnfix len) nil))
        ((hex-digitp (car x))
         (parse-hex-from-charlist
          (cdr x)
          (the unsigned-byte
               (+ (the unsigned-byte (hex-digit-val (car x)))
                  (the unsigned-byte (ash (the unsigned-byte (lnfix val)) 4))))
          (the unsigned-byte (+ 1 (the unsigned-byte (lnfix len))))))
        (t
         (mv (lnfix val) (lnfix len) x)))
  ///
  (defthm val-of-parse-hex-from-charlist
      (equal (mv-nth 0 (parse-hex-from-charlist x val len))
             (+ (hex-digit-list-value (take-leading-hex-digits x))
                (ash (nfix val) (* 4 (len (take-leading-hex-digits x))))))
      :hints(("Goal" :in-theory (enable take-leading-hex-digits
                                        hex-digit-list-value))))
  (defthm len-of-parse-hex-from-charlist
    (equal (mv-nth 1 (parse-hex-from-charlist x val len))
           (+ (nfix len) (len (take-leading-hex-digits x))))
    :hints(("Goal" :in-theory (enable take-leading-hex-digits))))

  (defthm rest-of-parse-hex-from-charlist
    (equal (mv-nth 2 (parse-hex-from-charlist x val len))
           (skip-leading-hex-digits x))
    :hints(("Goal" :in-theory (enable skip-leading-hex-digits)))))

(define parse-hex-from-string
  :short "Parse a hexadecimal number from a string, at some offset."
  ((x   stringp "The string to parse.")
   (val natp    "Accumulator for the value we have parsed so far; typically 0 to
                 start with.")
   (len natp    "Accumulator for the number of hex digits we have parsed so far;
                 typically 0 to start with.")
   (n   natp    "Offset into @('x') where we should begin parsing.  Must be a valid
                 index into the string, i.e., @('0 <= n < (length x)').")
   (xl  (eql xl (length x)) "Pre-computed length of @('x')."))
  :guard (<= n xl)
  :returns
  (mv (val "The value of the hex digits we parsed."
           natp :rule-classes :type-prescription)
      (len "The number of hex digits we parsed."
           natp :rule-classes :type-prescription))
  :split-types t
  (declare (type string x)
           (type unsigned-byte val len n xl))
  :verify-guards nil
  :enabled t
  :long "<p>This function is flexible but very complicated.  See @(see strval16)
for a very simple alternative that may do what you want.</p>

<p>The final @('val') and @('len') are guaranteed to be natural numbers;
failure is indicated by a return @('len') of zero.</p>

<p>Because of leading zeroes, the @('len') may be much larger than you would
expect based on @('val') alone.  The @('len') argument is generally useful if
you want to continue parsing through the string, i.e., the @('n') you started
with plus the @('len') you got out will be the next position in the string
after the number.</p>

<p>See also @(see parse-hex-from-charlist) for a simpler function that reads a
number from the start of a character list.  This function also serves as part
of our logical definition.</p>"

  (mbe :logic
       (b* (((mv val len ?rest)
             (parse-hex-from-charlist (nthcdr n (explode x)) val len)))
         (mv val len))
       :exec
       (b* (((when (eql n xl))
             (mv val len))
            ((the character char) (char (the string x)
                                        (the unsigned-byte n)))
            ((when (hex-digitp char))
             (parse-hex-from-string
              (the string x)
              (the unsigned-byte
                   (+ (the unsigned-byte (hex-digit-val char))
                      (the unsigned-byte (ash (the unsigned-byte val) 4))))
              (the unsigned-byte (+ 1 (the unsigned-byte len)))
              (the unsigned-byte (+ 1 n))
              (the unsigned-byte xl))))
         (mv val len)))
  ///
  ;; Minor speed hint
  (local (in-theory (disable BOUND-OF-LEN-OF-TAKE-LEADING-HEX-DIGITS
                             ACL2::RIGHT-SHIFT-TO-LOGTAIL
                             HEX-DIGIT-LISTP-OF-CDR-WHEN-HEX-DIGIT-LISTP)))

  (verify-guards parse-hex-from-string
    :hints(("Goal" :in-theory (enable parse-hex-from-charlist
                                      take-leading-hex-digits
                                      hex-digit-list-value
                                      )))))


(define strval16
  :short "Interpret a string as a hexadecimal number."
  ((x stringp))
  :returns (value? (or (natp value?)
                       (not value?))
                   :rule-classes :type-prescription)
  :long "<p>For example, @('(strval16 \"1F\")') is 31.  If the string is empty
or has any non hexadecimal digit characters (0-9, A-F, a-f), we return
@('nil').</p>"
  :split-types t
  (declare (type string x))
  (mbe :logic
       (let ((chars (explode x)))
         (and (consp chars)
              (hex-digit-listp chars)
              (hex-digit-list-value chars)))
       :exec
       (b* (((the unsigned-byte xl) (length x))
            ((mv (the unsigned-byte val) (the unsigned-byte len))
             (parse-hex-from-string x 0 0 0 xl)))
         (and (not (eql 0 len))
              (eql len xl)
              val)))
  ///
  (defcong istreqv equal (strval16 x) 1)
  (local (assert! (equal (strval16 "") nil)))
  (local (assert! (equal (strval16 "0") 0)))
  (local (assert! (equal (strval16 "1234") #x1234))))