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
|
; 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/
;
; 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 "misc/definline" :dir :system) ;; bozo
(local (include-book "std/lists/nthcdr" :dir :system))
(local (include-book "arithmetic"))
(defsection ichar<
:parents (ordering)
:short "Case-insensitive character less-than test."
:long "<p>@(call ichar<) determines if @('x') is \"smaller\" than @('y'), but
ignoring case. Our approach is basically to downcase upper-case letters and
then compare the resulting character codes.</p>
<p>Something subtle about this is that, in the ASCII character ordering, the
upper-case characters do not immediately preceede the lower-case ones. That
is, upper-case Z is at 90, but lower-case a does not start until 97. So, in
our approach, the 6 intervening characters (the square brackets, backslash,
hat, underscore, and backtick) are considered \"smaller\" than letters, even
though in regular ASCII ordering they are \"larger\" than the upper-case
letters.</p>"
(definlined ichar< (x y)
(declare (type character x)
(type character y)
(xargs :guard-hints(("Goal" :in-theory (enable downcase-char)))))
(mbe :logic
(< (char-code (downcase-char x))
(char-code (downcase-char y)))
:exec
(let* ((xc (the (unsigned-byte 8) (char-code (the character x))))
(yc (the (unsigned-byte 8) (char-code (the character y))))
(xc-fix (if (and (<= (big-a) (the (unsigned-byte 8) xc))
(<= (the (unsigned-byte 8) xc) (big-z)))
(the (unsigned-byte 8) (+ (the (unsigned-byte 8) xc) 32))
(the (unsigned-byte 8) xc)))
(yc-fix (if (and (<= (big-a) (the (unsigned-byte 8) yc))
(<= (the (unsigned-byte 8) yc) (big-z)))
(the (unsigned-byte 8) (+ (the (unsigned-byte 8) yc) 32))
(the (unsigned-byte 8) yc))))
(< (the (unsigned-byte 8) xc-fix)
(the (unsigned-byte 8) yc-fix)))))
(local (in-theory (enable ichar<)))
(defthm ichar<-irreflexive
(not (ichar< x x)))
(defthm ichar<-antisymmetric
(implies (ichar< x y)
(not (ichar< y x))))
(defthm ichar<-transitive
(implies (and (ichar< x y)
(ichar< y z))
(ichar< x z)))
(defthm ichar<-transitive-two
(implies (and (ichar< y z)
(ichar< x y))
(ichar< x z)))
(defthm ichar<-trichotomy-weak
(implies (and (not (ichar< x y))
(not (ichar< y x)))
(equal (ichareqv x y)
t))
:hints(("Goal" :in-theory (enable ichar<
ichareqv
downcase-char
char-fix))))
(defcong ichareqv equal (ichar< x y) 1
:hints(("Goal" :in-theory (enable ichar<
ichareqv
downcase-char
char-fix))))
(defcong ichareqv equal (ichar< x y) 2
:hints(("Goal" :in-theory (enable ichar<
ichareqv
downcase-char
char-fix))))
(defthm ichar<-trichotomy-strong
(equal (ichar< x y)
(and (not (ichareqv x y))
(not (ichar< y x))))
:hints(("Goal"
:in-theory (e/d (ichareqv downcase-char char-fix)
(ichar<-trichotomy-weak))
:use ((:instance ichar<-trichotomy-weak))))
:rule-classes ((:rewrite :loop-stopper ((x y))))))
(defsection icharlist<
:parents (ordering)
:short "Case-insensitive character-list less-than test."
:long "<p>@(call icharlist<) determines whether the character list @('x')
preceeds @('y') in alphabetical order without regards to case. The characters
are compared with @(see ichar<) and shorter strings are considered smaller than
longer strings.</p>"
(defund icharlist< (x y)
(declare (xargs :guard (and (character-listp x)
(character-listp y))
:verify-guards nil))
(mbe :logic (cond ((atom y)
nil)
((atom x)
t)
((ichar< (car x) (car y))
t)
((ichar< (car y) (car x))
nil)
(t
(icharlist< (cdr x) (cdr y))))
:exec
(cond ((atom y)
nil)
((atom x)
t)
(t
(let* ((xc (the (unsigned-byte 8) (char-code (the character (car x)))))
(yc (the (unsigned-byte 8) (char-code (the character (car y)))))
(xc-fix (if (and (<= (big-a) (the (unsigned-byte 8) xc))
(<= (the (unsigned-byte 8) xc) (big-z)))
(the (unsigned-byte 8) (+ (the (unsigned-byte 8) xc) 32))
(the (unsigned-byte 8) xc)))
(yc-fix (if (and (<= (big-a) (the (unsigned-byte 8) yc))
(<= (the (unsigned-byte 8) yc) (big-z)))
(the (unsigned-byte 8) (+ (the (unsigned-byte 8) yc) 32))
(the (unsigned-byte 8) yc))))
(cond ((< (the (unsigned-byte 8) xc-fix) (the (unsigned-byte 8) yc-fix))
t)
((< (the (unsigned-byte 8) yc-fix) (the (unsigned-byte 8) xc-fix))
nil)
(t
(icharlist< (cdr x) (cdr y)))))))))
(local (in-theory (enable icharlist<)))
(verify-guards icharlist<
:hints(("Goal" :in-theory (e/d (ichar< downcase-char)
(ichar<-trichotomy-strong)))))
(defthm icharlist<-irreflexive
(equal (icharlist< x x)
nil))
(defthm icharlist<-antisymmetric
(implies (icharlist< x y)
(not (icharlist< y x))))
(defthm icharlist<-transitive
(implies (and (icharlist< x y)
(icharlist< y z))
(icharlist< x z)))
(defthm icharlist<-trichotomy-weak
(implies (and (not (icharlist< x y))
(not (icharlist< y x)))
(equal (icharlisteqv x y)
t)))
(defcong icharlisteqv equal (icharlist< x y) 1)
(defcong icharlisteqv equal (icharlist< x y) 2)
(defthm icharlist<-trichotomy-strong
(equal (icharlist< x y)
(and (not (icharlisteqv x y))
(not (icharlist< y x))))
:rule-classes ((:rewrite :loop-stopper ((x y))))))
(defsection istr<
:parents (ordering)
:short "Case-insensitive string less-than test."
:long "<p>@(call icharlist<) determines whether the string @('x') preceeds
@('y') in alphabetical order without regards to case. The characters are
compared with @(see ichar<) and shorter strings are considered smaller than
longer strings.</p>
<p>Logically, this is identical to:</p>
@({
(icharlist< (explode x) (explode y))
})
<p>But we use a more efficient implementation which avoids coercing the strings
into lists.</p>
<p>NOTE: for reasoning, we leave this function enabled and prefer to work with
@(see icharlist<) of the explodes as our normal form.</p>"
(defund istr<-aux (x y n xl yl)
(declare (type string x)
(type string y)
(type integer n)
(type integer xl)
(type integer yl)
(xargs :guard (and (stringp x)
(stringp y)
(natp n)
(<= n (length x))
(<= n (length y))
(equal xl (length x))
(equal yl (length y)))
:verify-guards nil
:measure (nfix (- (nfix xl) (nfix n)))))
(mbe :logic
(cond ((zp (- (nfix yl) (nfix n)))
nil)
((zp (- (nfix xl) (nfix n)))
t)
((ichar< (char x n) (char y n))
t)
((ichar< (char y n) (char x n))
nil)
(t
(istr<-aux x y (+ (nfix n) 1) xl yl)))
:exec
(cond ((= (the integer n) (the integer yl))
nil)
((= (the integer n) (the integer xl))
t)
(t
(let* ((xc (the (unsigned-byte 8)
(char-code (the character
(char (the string x) (the integer n))))))
(yc (the (unsigned-byte 8)
(char-code (the character
(char (the string y) (the integer n))))))
(xc-fix (if (and (<= (big-a) (the (unsigned-byte 8) xc))
(<= (the (unsigned-byte 8) xc) (big-z)))
(the (unsigned-byte 8) (+ (the (unsigned-byte 8) xc) 32))
(the (unsigned-byte 8) xc)))
(yc-fix (if (and (<= (big-a) (the (unsigned-byte 8) yc))
(<= (the (unsigned-byte 8) yc) (big-z)))
(the (unsigned-byte 8) (+ (the (unsigned-byte 8) yc) 32))
(the (unsigned-byte 8) yc))))
(cond ((< (the (unsigned-byte 8) xc-fix) (the (unsigned-byte 8) yc-fix))
t)
((< (the (unsigned-byte 8) yc-fix) (the (unsigned-byte 8) xc-fix))
nil)
(t
(istr<-aux (the string x)
(the string y)
(the integer (+ (the integer n) 1))
(the integer xl)
(the integer yl)))))))))
(definline istr< (x y)
(declare (type string x)
(type string y)
(xargs :verify-guards nil))
(mbe :logic
(icharlist< (explode x) (explode y))
:exec
(istr<-aux (the string x)
(the string y)
(the integer 0)
(the integer (length (the string x)))
(the integer (length (the string y))))))
(local (defthm crock
(equal (< a a)
nil)))
(local (defthm crock2
(implies (< a b)
(equal (< b a)
nil))))
(verify-guards istr<-aux
:hints(("Goal" :in-theory (e/d (ichar< downcase-char)))))
(defthm istr<-aux-correct
(implies (and (stringp x)
(stringp y)
(natp n)
(<= n (length x))
(<= n (length y))
(equal xl (length x))
(equal yl (length y)))
(equal (istr<-aux x y n xl yl)
(icharlist< (nthcdr n (coerce x 'list))
(nthcdr n (coerce y 'list)))))
:hints(("Goal" :in-theory (enable istr<-aux icharlist<))))
(verify-guards istr<$inline)
(defcong istreqv equal (istr< x y) 1)
(defcong istreqv equal (istr< x y) 2))
|