File: no-duplicatesp.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 (279 lines) | stat: -rw-r--r-- 11,106 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
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
; no-duplicatesp.lisp
; Copyright (C) 2008-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 "ACL2")
(include-book "duplicity")

(defthm no-duplicatesp-equal-when-atom
  (implies (atom x)
           (equal (no-duplicatesp-equal x)
                  t)))

(defthm no-duplicatesp-equal-of-cons
  (equal (no-duplicatesp-equal (cons a x))
         (and (not (member-equal a (double-rewrite x)))
              (no-duplicatesp-equal x))))

;; Not needed, equiv.lisp gives us a congruence rule for list-equiv
;; (defthm no-duplicatesp-equal-of-list-fix
;;   (equal (no-duplicatesp-equal (list-fix x))
;;          (no-duplicatesp-equal x)))


(defsection duplicity-badguy1
  :parents (duplicity-badguy)
  :short "@(call duplicity-badguy1) finds the first element of @('dom') whose
duplicity in @('x') exceeds 1, if such a member exists."

  (defund duplicity-badguy1 (dom x)
    (declare (xargs :guard t))
    (if (consp dom)
        (if (> (duplicity (car dom) x) 1)
            (list (car dom))
          (duplicity-badguy1 (cdr dom) x))
      nil))

  (defthm duplicity-badguy1-exists-in-list
    (implies (duplicity-badguy1 dom x)
             (member-equal (car (duplicity-badguy1 dom x)) x))
    :hints(("Goal" :in-theory (enable duplicity-badguy1))))

  (defthm duplicity-badguy1-exists-in-dom
    (implies (duplicity-badguy1 dom x)
             (member-equal (car (duplicity-badguy1 dom x)) dom))
    :hints(("Goal" :in-theory (enable duplicity-badguy1))))

  (defthm duplicity-badguy1-has-high-duplicity
    (implies (duplicity-badguy1 dom x)
             (< 1 (duplicity (car (duplicity-badguy1 dom x)) x)))
    :hints(("Goal" :in-theory (enable duplicity-badguy1))))

  (defthm duplicity-badguy1-is-complete-for-domain
    (implies (and (member-equal a dom)
                  (< 1 (duplicity a x)))
             (duplicity-badguy1 dom x))
    :hints(("Goal" :in-theory (enable duplicity-badguy1))))

  (defthm duplicity-badguy1-need-only-consider-the-list
    (implies (duplicity-badguy1 dom x)
             (duplicity-badguy1 x x))
    :hints(("Goal"
            :in-theory (disable duplicity-badguy1-exists-in-dom
                                duplicity-badguy1-exists-in-list)
            :use ((:instance duplicity-badguy1-exists-in-dom)
                  (:instance duplicity-badguy1-exists-in-list)))))

  (encapsulate
    ()
    (local (defthm lemma1
             (implies (duplicity-badguy1 dom x)
                      (duplicity-badguy1 dom (cons a x)))
             :hints(("Goal" :in-theory (enable duplicity-badguy1)))))

    (local (defthm lemma2
             (implies (and (member-equal a x)
                           (member-equal a dom))
                      (duplicity-badguy1 dom (cons a x)))
             :hints(("Goal" :in-theory (enable duplicity-badguy1)))))

    (defthm no-duplicatesp-equal-when-duplicity-badguy1
      (implies (and (not (duplicity-badguy1 dom x))
                    (subsetp-equal x dom))
               (no-duplicatesp-equal x))
      :hints(("Goal" :in-theory (enable duplicity-badguy1))))))



(defsection duplicity-badguy
  :parents (duplicity no-duplicatesp-equal-same-by-duplicity)
  :short "@(call duplicity-badguy) finds an element that occurs multiple times
in the list @('x'), if one exists."

  :long "<p>This function is central to our proof of @(see
no-duplicatesp-equal-same-by-duplicity), a pick-a-point style strategy for
proving that @(see no-duplicatesp) holds of a list by reasoning about duplicity
of an arbitrary element.</p>"

  (defund duplicity-badguy (x)
    (declare (xargs :guard t))
    (duplicity-badguy1 x x))

  (defthm duplicity-badguy-exists
    (implies (duplicity-badguy x)
             (member-equal (car (duplicity-badguy x)) x))
    :hints(("Goal" :in-theory (enable duplicity-badguy))))

  (defthm duplicity-badguy-has-high-duplicity
    (implies (duplicity-badguy x)
             (< 1 (duplicity (car (duplicity-badguy x)) x)))
    :hints(("Goal" :in-theory (e/d (duplicity-badguy)
                                   (duplicity-when-member-equal)))))

  (defthm duplicity-badguy-is-complete
    (implies (< 1 (duplicity a x))
             (duplicity-badguy x))
    :hints(("Goal"
            :in-theory (enable duplicity-badguy)
            :use ((:instance duplicity-badguy1-is-complete-for-domain
                             (dom x))))))

  (local (defthm lemma
           (implies (subsetp-equal a (cdr x))
                    (subsetp-equal a x))))

  (local (defthm lemma2
           (subsetp-equal x x)
           :hints(("Goal" :induct (len x)))))

  (local (defthm lemma3
           (implies (not (duplicity-badguy x))
                    (no-duplicatesp-equal x))
           :hints(("Goal" :in-theory (enable duplicity-badguy)))))

  (local (defthm lemma5
           (implies (duplicity-badguy x)
                    (not (no-duplicatesp-equal x)))
           :hints(("Goal"
                   :in-theory (disable no-duplicatesp-equal)
                   :use ((:instance no-duplicatesp-equal-when-high-duplicity
                                    (a (car (duplicity-badguy x)))))))))

  (defthm duplicity-badguy-under-iff
    (iff (duplicity-badguy x)
         (not (no-duplicatesp-equal x)))))



(defsection no-duplicatesp-equal-same-by-duplicity
  :parents (no-duplicatesp-equal duplicity)
  :short "Proof strategy: show that a list satisfies @(see no-duplicatesp-equal)
because it has no element whose @(see duplicity) is over 1."

  :long "<p>This is often a good way to prove @(see no-duplicatesp).  This is a
basic pick-a-point style theorem that you can (manually) <see topic='@(url
functional-instantiation-example)'>functionally instantiate</see>.</p>"

  (encapsulate
    (((duplicity-hyp) => *)
     ((duplicity-lhs) => *)
     ((duplicity-rhs) => *))

    (local (defun duplicity-hyp () nil))
    (local (defun duplicity-lhs () nil))
    (local (defun duplicity-rhs () nil))

    (defthm duplicity-constraint
      (implies (duplicity-hyp)
               (equal (duplicity a (duplicity-lhs))
                      (duplicity a (duplicity-rhs))))))

  (local (defthm lemma1
           (implies (and (duplicity-hyp)
                         (no-duplicatesp-equal (duplicity-lhs)))
                    (no-duplicatesp-equal (duplicity-rhs)))
           :hints(("goal"
                   :in-theory (disable duplicity-constraint
                                       duplicity-badguy-has-high-duplicity)
                   :use ((:instance duplicity-badguy-has-high-duplicity
                                    (x (duplicity-rhs)))
                         (:instance duplicity-constraint
                                    (a (car (duplicity-badguy (duplicity-rhs))))))))))

  (local (defthm lemma2
           (implies (and (duplicity-hyp)
                         (no-duplicatesp-equal (duplicity-rhs)))
                    (no-duplicatesp-equal (duplicity-lhs)))
           :hints(("goal"
                   :in-theory (disable duplicity-constraint
                                       duplicity-badguy-has-high-duplicity)
                   :use ((:instance duplicity-badguy-has-high-duplicity
                                    (x (duplicity-lhs)))
                         (:instance duplicity-constraint
                                    (a (car (duplicity-badguy (duplicity-lhs))))))))))

  (defthm no-duplicatesp-equal-same-by-duplicity
    (implies (duplicity-hyp)
             (equal (no-duplicatesp-equal (duplicity-lhs))
                    (no-duplicatesp-equal (duplicity-rhs))))
    :hints(("goal"
            :use ((:instance lemma1)
                  (:instance lemma2))))))



(defthm no-duplicatesp-equal-of-rev
  (equal (no-duplicatesp-equal (rev x))
         (no-duplicatesp-equal x))
  :hints(("Goal"
          :use ((:functional-instance
                 no-duplicatesp-equal-same-by-duplicity
                 (duplicity-hyp (lambda () t))
                 (duplicity-lhs (lambda () (rev x)))
                 (duplicity-rhs (lambda () x)))))))

(defthm no-duplicatesp-equal-of-append-of-rev-1
  (equal (no-duplicatesp-equal (append (rev x) y))
         (no-duplicatesp-equal (append x y)))
  :hints(("Goal"
          :use ((:functional-instance
                 no-duplicatesp-equal-same-by-duplicity
                 (duplicity-hyp (lambda () t))
                 (duplicity-lhs (lambda () (append (rev x) y)))
                 (duplicity-rhs (lambda () (append x y))))))))

(defthm no-duplicatesp-equal-of-append-of-rev-2
  (equal (no-duplicatesp-equal (append x (rev y)))
         (no-duplicatesp-equal (append x y)))
  :hints(("Goal"
          :use ((:functional-instance
                 no-duplicatesp-equal-same-by-duplicity
                 (duplicity-hyp (lambda () t))
                 (duplicity-lhs (lambda () (append x (rev y))))
                 (duplicity-rhs (lambda () (append x y))))))))

(defthm no-duplicatesp-equal-of-append-of-append
  (equal (no-duplicatesp-equal (append x y))
         (no-duplicatesp-equal (append y x)))
  :rule-classes ((:rewrite :loop-stopper ((x y))))
  :hints (("Goal" :use ((:functional-instance
                         no-duplicatesp-equal-same-by-duplicity
                         (duplicity-hyp (lambda () t))
                         (duplicity-lhs (lambda () (append x y)))
                         (duplicity-rhs (lambda () (append y x))))))))

(defthm no-duplicatesp-equal-of-flatten-of-rev
  (equal (no-duplicatesp-equal (flatten (rev x)))
         (no-duplicatesp-equal (flatten x)))
  :hints(("Goal"
          :use ((:functional-instance
                 no-duplicatesp-equal-same-by-duplicity
                 (duplicity-hyp (lambda () t))
                 (duplicity-lhs (lambda () (flatten (rev x))))
                 (duplicity-rhs (lambda () (flatten x))))))))