/usr/share/acl2-6.3/books/str/natstr.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 | ; 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 "digitp")
(local (include-book "arithmetic"))
(local (include-book "arithmetic-3/floor-mod/floor-mod" :dir :system))
(defsection basic-natchars
:parents (natchars)
:short "Logically simple definition that is mostly similar to @(see natchars)."
:long "<p>@(call basic-natchars) almost computes @('(natchars n)'), but when
@('n') is zero it returns @('nil') instead of @('(#\\0)').</p>"
(defund basic-natchars (n)
(declare (xargs :guard (natp n)))
(if (zp n)
nil
(cons (digit-to-char (mod n 10))
(basic-natchars (floor n 10)))))
(local (in-theory (enable basic-natchars)))
(defthm basic-natchars-when-zp
(implies (zp n)
(equal (basic-natchars n)
nil)))
(defthm true-listp-of-basic-natchars
(true-listp (basic-natchars n))
:rule-classes :type-prescription)
(defthm character-listp-of-basic-natchars
(character-listp (basic-natchars n)))
(defthm digit-listp-of-basic-natchars
(digit-listp (basic-natchars n)))
(defthm basic-natchars-under-iff
(iff (basic-natchars n)
(not (zp n))))
(defthm consp-of-basic-natchars
(equal (consp (basic-natchars n))
(if (basic-natchars n)
t
nil)))
(encapsulate
()
(local (defun my-induction (n m)
(if (or (zp n)
(zp m))
nil
(my-induction (floor n 10) (floor m 10)))))
(defthm basic-natchars-one-to-one
(equal (equal (basic-natchars n)
(basic-natchars m))
(equal (nfix n)
(nfix m)))
:hints(("Goal"
:induct (my-induction n m))))))
(defsection natchars
:parents (numbers)
:short "Convert a natural number into a list of characters."
:long "<p>For instance, @('(natchars 123)') is @('(#\\1 #\\2 #\\3)').</p>"
(local (defthm digit-list-value-of-rev-of-basic-natchars
(equal (digit-list-value (rev (basic-natchars n)))
(nfix n))
:hints(("Goal"
:induct (basic-natchars n)
:in-theory (e/d (basic-natchars)
(digit-to-char))))))
(defund natchars-aux (n acc)
(declare (type integer n)
(xargs :guard (natp n)
:verify-guards nil))
(if (zp n)
acc
(natchars-aux (the integer (truncate (the integer n) 10))
(cons (the character (code-char
(the (unsigned-byte 8)
(+ (the (unsigned-byte 8) 48)
(the (unsigned-byte 8)
(rem (the integer n) 10))))))
acc))))
(definlined natchars (n)
(declare (type integer n)
(xargs :guard (natp n)
:verify-guards nil))
(or (natchars-aux n nil) '(#\0)))
(defthm natchars-aux-redefinition
(equal (natchars-aux n acc)
(revappend (basic-natchars n) acc))
:rule-classes :definition
:hints(("Goal" :in-theory (enable natchars-aux basic-natchars))))
(verify-guards natchars-aux)
(verify-guards natchars$inline)
(local (in-theory (enable natchars)))
(defthm true-listp-of-natchars
(true-listp (natchars n))
:rule-classes :type-prescription)
(defthm character-listp-of-natchars
(character-listp (natchars n)))
(defthm digit-listp-of-natchars
(digit-listp (natchars n)))
(encapsulate
()
(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 (defthmd lemma2
(not (equal (basic-natchars n) '(#\0)))
:hints(("Goal" :in-theory (enable basic-natchars)))))
(defthm natchars-one-to-one
(equal (equal (natchars n)
(natchars m))
(equal (nfix n) (nfix m)))
:hints(("Goal"
:in-theory (disable basic-natchars-one-to-one)
:use ((:instance basic-natchars-one-to-one)
(:instance lemma2)
(:instance lemma2 (n m)))))))
(defthm digit-list-value-of-natchars
(equal (digit-list-value (natchars n))
(nfix n))))
(defsection natstr
:parents (numbers)
:short "Convert a natural number into a string."
:long "<p>For instance, @('(natstr 123)') is @('\"123\"').</p>"
(definlined natstr (n)
(declare (type integer n)
(xargs :guard (natp n)))
(implode (natchars n)))
(local (in-theory (enable natstr)))
(defthm stringp-of-natstr
(stringp (natstr n))
:rule-classes :type-prescription)
(defthm digit-listp-of-natstr
(digit-listp (explode (natstr n))))
(defthm natstr-one-to-one
(equal (equal (natstr n) (natstr m))
(equal (nfix n) (nfix m))))
(defthm digit-list-value-of-natstr
(equal (digit-list-value (explode (natstr n)))
(nfix n)))
(defthm natstr-nonempty
(not (equal (natstr n) ""))))
(defsection natstr-list
:parents (numbers)
:short "Convert a list of natural numbers into a list of strings."
(defund natstr-list (x)
(declare (xargs :guard (nat-listp x)))
(if (atom x)
nil
(cons (natstr (car x))
(natstr-list (cdr x)))))
(local (in-theory (enable natstr-list)))
(defthm natstr-list-when-atom
(implies (atom x)
(equal (natstr-list x)
nil)))
(defthm natstr-list-of-cons
(equal (natstr-list (cons a x))
(cons (natstr a)
(natstr-list x))))
(defthm string-listp-of-natstr-list
(string-listp (natstr-list x))))
(defsection revappend-natchars
:parents (numbers)
:short "More efficient version of @('(revappend (natchars n) acc).')"
:long "<p>This can be useful when building strings by consing together
characters in reverse order.</p>"
(defund revappend-natchars-aux (n acc)
(declare (type integer n)
(xargs :guard (and (natp n))))
(if (zp n)
acc
(cons (the character (code-char
(the (unsigned-byte 8)
(+ (the (unsigned-byte 8) 48)
(the (unsigned-byte 8)
(rem (the integer n) 10))))))
(revappend-natchars-aux
(the integer (truncate (the integer n) 10))
acc))))
(defthm revappend-natchars-aux-redef
(equal (revappend-natchars-aux n acc)
(append (basic-natchars n) acc))
:hints(("Goal" :in-theory (enable revappend-natchars-aux basic-natchars))))
(definline revappend-natchars (n acc)
(declare (type integer n)
(xargs :guard (natp n)
:guard-hints(("Goal" :in-theory (enable natchars)))))
(mbe :logic (revappend (natchars n) acc)
:exec (if (zp n)
(cons #\0 acc)
(revappend-natchars-aux n acc)))))
#|
;; Use a machine with lots of memory, or lower the indices below. Ideally the
;; test should not do any garbage collection.
(ccl::set-lisp-heap-gc-threshold (expt 2 33))
;; 8.5 seconds, 1.1 GB allocated
(progn (ccl::gc)
(time (loop for i fixnum from 1 to 10000000
do (explode-nonnegative-integer i 10 nil))))
;; 5.8 seconds, 1.1 GB allocated
(progn (ccl::gc)
(time (loop for i fixnum from 1 to 10000000
do (STR::natchars-aux i nil))))
;; 5.9 seconds, 1.1 GB allocated
(progn (ccl::gc)
(time (loop for i fixnum from 1 to 10000000
do (STR::natchars i))))
;; Was 44 seconds. Now 6.1 seconds with CCL patch. 1.5 GB allocated
(progn (ccl::gc)
(time (loop for i fixnum from 1 to 10000000
do (STR::natstr i))))
|#
|