File: bsort.lisp

package info (click to toggle)
acl2 8.3dfsg-2
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 309,408 kB
  • sloc: lisp: 3,311,842; javascript: 22,569; cpp: 9,029; ansic: 7,872; perl: 6,501; xml: 3,838; java: 3,738; makefile: 3,383; ruby: 2,633; sh: 2,489; ml: 763; python: 741; yacc: 721; awk: 260; csh: 186; php: 171; lex: 154; tcl: 49; asm: 23; haskell: 17
file content (79 lines) | stat: -rw-r--r-- 1,828 bytes parent folder | download | duplicates (6)
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
; (certify-book "bsort")

(in-package "ACL2")

(include-book "ordered-perms")
(include-book "convert-perm-to-how-many")

(defun bnext (x)
  (declare (xargs :measure (len x)))
  (cond ((endp x) x)
        ((endp (cdr x)) x)
        ((lexorder (car x) (cadr x))
         (cons (car x) (bnext (cdr x))))
        (t (cons (cadr x)
                 (bnext (cons (car x) (cddr x)))))))

(defun how-many-smaller (e x)
  (cond ((endp x) 0)
        ((equal e (car x)) (how-many-smaller e (cdr x)))
        ((lexorder (car x) e) (+ 1 (how-many-smaller e (cdr x))))
        (t (how-many-smaller e (cdr x)))))

(defun bnext-size (x)
  (cond ((endp x) 0)
        (t (+ (how-many-smaller (car x) (cdr x))
              (bnext-size (cdr x))))))

(defthm how-many-smaller-bnext
  (equal (how-many-smaller e (bnext x))
         (how-many-smaller e x)))

#|
(defun samep (x y)
  (if (endp x)
      (endp y)
    (if (endp y)
        nil
      (and (equal (car x) (car y))
           (samep (cdr x) (cdr y))))))

(defequiv samep)

(defcong samep equal (how-many-smaller e x) 2)|#

(defthm how-many-bad-pairs-bnext
  (implies (not (equal x (bnext x)))
           (< (bnext-size (bnext x))
              (bnext-size x)))
  :rule-classes :linear)

(defun bsort (x)
  (declare (xargs :measure (bnext-size x)))
  (if (equal (bnext x) x)
      x
    (bsort (bnext x))))

(defthm orderedp-when-bnext-constant
  (implies (equal (bnext x) x)
           (orderedp x)))

(defthm orderedp-bsort
  (orderedp (bsort x)))

(defthm true-listp-bnext
  (implies (true-listp x)
           (true-listp (bnext x))))

(defthm true-listp-bsort
  (implies (true-listp x)
           (true-listp (bsort x))))

(defthm how-many-bnext
  (equal (how-many e (bnext x))
         (how-many e x)))

(defthm how-many-bsort
  (equal (how-many e (bsort x))
         (how-many e x)))