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
|
; Defsort - Defines a stable sort when given a comparison function
; Copyright (C) 2008 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 "ACL2")
(include-book "uniquep")
(local (include-book "std/lists/sets" :dir :system))
(local (include-book "std/lists/no-duplicatesp" :dir :system))
; DUPLICATED-MEMBERS and HONS-DUPLICATED-MEMBERS
;
; Duplicated-members, introduced below, returns all members of a list whose
; duplicity is greater than 1. It operates by sorting its argument and then
; scanning for adjacent duplicates, which is pretty fast. So, the total
; complexity is O(n log n).
;
; Hons-duplicated-members does the same thing except that it may return the
; elements in a different order. It operates by building a fast alist which
; associates each element with its duplicity, then scanning the alist for
; objects whose duplicity is greater than 1. If one believes that hons-acons
; and hons-get are effectively O(1), then its efficiency is O(n).
;
; In practice, both functions are pretty good, and hons-duplicated-members
; tends to outperform duplicated-members by slight margins in speed and memory
; consumption.
(defund collect-adjacent-duplicates (x)
(declare (xargs :guard t))
(cond ((atom x)
nil)
((atom (cdr x))
nil)
((equal (first x) (second x))
(if (atom (cddr x))
(list (first x))
(if (not (equal (first x) (third x)))
(cons (first x)
(collect-adjacent-duplicates (cddr x)))
(collect-adjacent-duplicates (cdr x)))))
(t
(collect-adjacent-duplicates (cdr x)))))
(defthm collect-adjacent-duplicates-when-not-consp
(implies (not (consp x))
(equal (collect-adjacent-duplicates x)
nil))
:hints(("Goal" :in-theory (enable collect-adjacent-duplicates))))
(defthm collect-adjacent-duplicates-when-not-consp-of-cdr
(implies (not (consp (cdr x)))
(equal (collect-adjacent-duplicates x)
nil))
:hints(("Goal" :in-theory (enable collect-adjacent-duplicates))))
(defthm collect-adjacent-duplicates-of-cons-and-cons
(equal (collect-adjacent-duplicates (cons a (cons b x)))
(if (equal a b)
(if (not (consp x))
(list a)
(if (equal a (car x))
(collect-adjacent-duplicates (cons a x))
(cons a (collect-adjacent-duplicates x))))
(collect-adjacent-duplicates (cons b x))))
:hints(("Goal" :in-theory (enable collect-adjacent-duplicates))))
(encapsulate
()
(local (defthm lemma
(implies (and (member-equal a (collect-adjacent-duplicates (cons x1 x4)))
(not (<< b a))
(<< b x1))
(member-equal a (collect-adjacent-duplicates x4)))
:hints(("Goal" :in-theory (enable collect-adjacent-duplicates)))))
(local (defthm lemma2
(implies (and (<<-ordered-p x)
(equal (duplicity a x) 1)
(not (<< b a))
(<< b (car x)))
(member-equal a (collect-adjacent-duplicates (cons b x))))
:hints(("Goal" :in-theory (enable <<-ordered-p)))))
(local (defthm lemma3
(implies (and (<<-ordered-p x)
(equal (duplicity a x) 1)
(not (<< b a))
(not (<< (car x) b)))
(member-equal a (collect-adjacent-duplicates (cons b x))))
:hints(("Goal" :in-theory (enable <<-ordered-p)))))
(local (defthm lemma4
(implies (and (<<-ordered-p x)
(< 1 (duplicity a x)))
(member-equal a (collect-adjacent-duplicates x)))
:hints(("Goal" :in-theory (enable <<-ordered-p
collect-adjacent-duplicates)))))
(local (defthm lemma5
(implies (and (<<-ordered-p x)
(not (< 1 (duplicity a x))))
(not (member-equal a (collect-adjacent-duplicates x))))
:hints(("Goal" :in-theory (enable <<-ordered-p
collect-adjacent-duplicates
duplicity)))))
(defthm member-equal-of-collect-adjacent-duplicates-when-<<-ordered-p
(implies (<<-ordered-p x)
(iff (member-equal a (collect-adjacent-duplicates x))
(< 1 (duplicity a x))))))
(encapsulate
()
(local (defthm crock0
(implies (<<-ordered-p x)
(<<-ordered-p (cdr x)))
:hints(("Goal" :in-theory (enable <<-ordered-p)))))
(local (defthm crock1
(implies (no-duplicatesp-equal x)
(equal (duplicity a x)
(if (member-equal a x)
1
0)))
:hints(("Goal" :in-theory (enable duplicity)))))
(local (defthm crock3
(implies (and (no-duplicatesp-equal (collect-adjacent-duplicates (cons b x)))
(<<-ordered-p x)
(<< a b)
(not (<< (car x) b)))
(<= (duplicity a x) 1))
:hints(("Goal" :in-theory (enable duplicity
<<-ordered-p
collect-adjacent-duplicates)))))
(defthm no-duplicatesp-equal-of-collect-adjacent-duplicates-when-<<-ordered-p
(implies (<<-ordered-p x)
(no-duplicatesp-equal (collect-adjacent-duplicates x)))
:hints(("Goal" :in-theory (enable collect-adjacent-duplicates <<-ordered-p)))))
(defund duplicated-members (x)
(declare (xargs :guard (true-listp x)))
(collect-adjacent-duplicates (<<-sort x)))
(defthm member-equal-of-duplicated-members
(iff (member-equal a (duplicated-members x))
(< 1 (duplicity a x)))
:hints(("Goal" :in-theory (enable duplicated-members))))
(defthm no-duplicatesp-equal-of-duplicated-members
(no-duplicatesp-equal (duplicated-members x))
:hints(("Goal" :in-theory (enable duplicated-members))))
(local (defthm element-p-by-duplicity-in-element-list-p
(implies (and (element-list-p x)
(< 0 (duplicity a x)))
(element-p a))))
(local (defthm element-list-p-when-subset-of-duplicated-members
(implies (and (subsetp x (duplicated-members y))
(element-list-p y))
(equal (element-list-p x)
(or (element-list-final-cdr-p t)
(true-listp x))))))
(def-listp-rule element-list-p-of-duplicated-members
(implies (element-list-p x)
(element-list-p (duplicated-members x)))
:hints (("goal" :use ((:instance element-list-p-when-subset-of-duplicated-members
(y x) (x (duplicated-members x))))
:in-theory (disable element-list-p-when-subset-of-duplicated-members)))
:tags (:duplicated-members))
(encapsulate
()
(local (defthm l0
(implies (duplicated-members x)
(not (no-duplicatesp-equal x)))
:hints(("Goal"
:in-theory (disable no-duplicatesp-equal-when-high-duplicity
member-equal-of-duplicated-members)
:use ((:instance no-duplicatesp-equal-when-high-duplicity
(a (car (duplicated-members x))))
(:instance member-equal-of-duplicated-members
(a (car (duplicated-members x)))))))))
(local (defthm l1
(implies (not (duplicated-members x))
(no-duplicatesp-equal x))
:hints(("Goal"
:in-theory (disable member-equal-of-duplicated-members)
:use ((:instance member-equal-of-duplicated-members
(a (car (duplicity-badguy x)))
(x x)))))))
(defthm duplicated-members-under-iff
(iff (duplicated-members x)
(not (no-duplicatesp-equal x)))))
;; An alternate approach to constructing the duplicated members, based upon hons,
;; is shown below. We construct an alist mapping each member of the list to its
;; duplicity, then walk the alist and see what we have found.
(defund hons-duplicity-alist-p (x)
(declare (xargs :guard t))
(if (consp x)
(and (consp (car x))
(natp (cdar x))
(hons-duplicity-alist-p (cdr x)))
(not x)))
(defthm hons-duplicity-alist-p-when-not-consp
(implies (not (consp x))
(equal (hons-duplicity-alist-p x)
(not x)))
:hints(("Goal" :in-theory (enable hons-duplicity-alist-p))))
(defthm hons-duplicity-alist-p-of-cons
(equal (hons-duplicity-alist-p (cons a x))
(and (consp a)
(natp (cdr a))
(hons-duplicity-alist-p x)))
:hints(("Goal" :in-theory (enable hons-duplicity-alist-p))))
(defthm alistp-when-hons-duplicity-alist-p
(implies (hons-duplicity-alist-p x)
(alistp x))
:hints(("Goal" :induct (len x))))
(defund hons-duplicity-alist-aux (x alist)
(declare (xargs :guard (hons-duplicity-alist-p alist)))
(if (atom x)
alist
(hons-duplicity-alist-aux
(cdr x)
(hons-acons (car x)
(+ 1 (nfix (cdr (hons-get (car x) alist))))
alist))))
(defthm hons-duplicity-alist-p-of-hons-duplicity-alist-aux
(implies (hons-duplicity-alist-p alist)
(hons-duplicity-alist-p (hons-duplicity-alist-aux x alist)))
:hints(("Goal" :in-theory (enable hons-duplicity-alist-aux))))
(defthm hons-assoc-equal-of-hons-duplicity-alist-aux
(implies (hons-duplicity-alist-p alist)
(equal (hons-assoc-equal a (hons-duplicity-alist-aux x alist))
(if (or (member-equal a x)
(hons-assoc-equal a alist))
(cons a (+ (duplicity a x)
(nfix (cdr (hons-assoc-equal a alist)))))
nil)))
:hints(("Goal"
:in-theory (enable hons-duplicity-alist-aux duplicity)
:do-not '(generalize fertilize)
:induct (hons-duplicity-alist-aux x alist))))
(defund hons-duplicity-alist (x)
(declare (xargs :guard t))
(hons-shrink-alist (hons-duplicity-alist-aux x nil) nil))
(defthm hons-duplicity-alist-p-of-hons-shrink-alist
(implies (and (hons-duplicity-alist-p alist)
(hons-duplicity-alist-p ans))
(hons-duplicity-alist-p (hons-shrink-alist alist ans)))
:hints(("Goal" :in-theory (enable hons-shrink-alist))))
(defthm hons-duplicity-alist-p-of-hons-duplicity-alist
(hons-duplicity-alist-p (hons-duplicity-alist x))
:hints(("Goal" :in-theory (enable hons-duplicity-alist))))
(local (defthm iff-of-hons-assoc-equal
(implies (alistp x)
(iff (hons-assoc-equal a x)
(member-equal a (strip-cars x))))))
(defthm no-duplicatesp-equal-of-strip-cars-of-hons-shrink-alist
(implies (alistp y)
(equal (no-duplicatesp-equal (strip-cars (hons-shrink-alist x y)))
(no-duplicatesp-equal (strip-cars y))))
:hints(("Goal" :in-theory (enable hons-shrink-alist))))
(defthm no-duplicatesp-equal-of-strip-cars-of-hons-duplicity-alist
(no-duplicatesp-equal (strip-cars (hons-duplicity-alist x)))
:hints(("Goal" :in-theory (enable hons-duplicity-alist))))
(defthm hons-assoc-equal-of-hons-shrink-alist
(equal (hons-assoc-equal a (hons-shrink-alist x y))
(or (hons-assoc-equal a y)
(hons-assoc-equal a x)))
:hints(("Goal" :in-theory (enable hons-shrink-alist))))
(defthm hons-assoc-equal-of-hons-duplicity-alist
(equal (hons-assoc-equal a (hons-duplicity-alist x))
(if (member-equal a x)
(cons a (duplicity a x))
nil))
:hints(("Goal" :in-theory (enable hons-duplicity-alist))))
(defund hons-duplicated-members-aux (alist)
;; Walks over the shrunken duplicity alist and gathers keys which occurred
;; more than once.
(declare (xargs :guard (alistp alist)))
(cond ((atom alist)
nil)
((> (nfix (cdar alist)) 1)
(cons (caar alist)
(hons-duplicated-members-aux (cdr alist))))
(t
(hons-duplicated-members-aux (cdr alist)))))
(defthm member-equal-of-strip-cars-when-member-equal-of-hons-duplicated-members-aux
(implies (member-equal a (hons-duplicated-members-aux x))
(member-equal a (strip-cars x)))
:hints(("Goal" :in-theory (enable hons-duplicated-members-aux))))
(defthm member-equal-of-hons-duplicated-members-aux
(implies (no-duplicatesp-equal (strip-cars x))
(iff (member-equal a (hons-duplicated-members-aux x))
(> (nfix (cdr (hons-assoc-equal a x))) 1)))
:hints(("Goal" :in-theory (enable hons-duplicated-members-aux))))
(defthm no-duplicatesp-equal-of-hons-duplicated-members-aux
(implies (no-duplicatesp-equal (strip-cars alist))
(no-duplicatesp-equal (hons-duplicated-members-aux alist)))
:hints(("Goal" :in-theory (enable hons-duplicated-members-aux))))
(defund hons-duplicated-members (x)
(declare (xargs :guard t))
(let* ((dalist (hons-duplicity-alist x))
(result (hons-duplicated-members-aux dalist)))
(prog2$
(flush-hons-get-hash-table-link dalist)
result)))
(defthm member-equal-of-hons-duplicated-members
(iff (member-equal a (hons-duplicated-members x))
(< 1 (duplicity a x)))
:hints(("Goal" :in-theory (enable hons-duplicated-members))))
(defthm no-duplicatesp-equal-of-hons-duplicated-members
(no-duplicatesp-equal (hons-duplicated-members x))
:hints(("Goal" :in-theory (enable hons-duplicated-members))))
#|
:q
(defconst *test1*
(append (loop for i from 1 to 10000 collect i)
(loop for i from 1 to 5000 collect i)
(loop for i from 1 to 5000 collect i)))
;; Lisp2 takes 3.31 seconds, 207 MB allocated
(time (loop for i from 1 to 100 do (hons-duplicated-members *test1*)))
;; Lisp2 takes 3.16 seconds, 278 MB allocated
(time (loop for i from 1 to 100 do (duplicated-members *test1*)))
(defconst *test2*
(append (loop for i from 1 to 10000 collect (concatenate 'string
"foo bar baz "
(coerce (explode-atom i 10) 'string)))
(loop for i from 1 to 5000 collect (concatenate 'string
"foo bar baz "
(coerce (explode-atom i 10) 'string)))
(loop for i from 1 to 5000 collect (concatenate 'string
"foo bar baz "
(coerce (explode-atom i 10) 'string)))))
;; Lisp2 takes 5.06 seconds, 208 MB allocated
(time (loop for i from 1 to 100 do (hons-duplicated-members *test2*)))
;; Lisp2 takes 6.03 seconds, 313 MB allocated
(time (loop for i from 1 to 100 do (duplicated-members *test2*)))
|#
|