File: meta-extract-simple-test.lisp

package info (click to toggle)
acl2 8.6%2Bdfsg-2
  • links: PTS
  • area: main
  • in suites: trixie
  • size: 1,111,420 kB
  • sloc: lisp: 17,818,294; java: 125,359; python: 28,122; javascript: 23,458; cpp: 18,851; ansic: 11,569; perl: 7,678; xml: 5,591; sh: 3,976; makefile: 3,833; ruby: 2,633; yacc: 1,126; ml: 763; awk: 295; csh: 233; lex: 197; php: 178; tcl: 49; asm: 23; haskell: 17
file content (245 lines) | stat: -rw-r--r-- 8,553 bytes parent folder | download | duplicates (5)
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
; Copyright (C) 2014, Regents of the University of Texas
; Contributed by Matt Kaufmann (original date October, 2012)
; based on earlier work by Sol Swords.
; License: A 3-clause BSD license.  See the LICENSE file distributed with ACL2.

; (certify-book "meta-extract-simple-test" 0 t :ttags :all)

; This example is based on the first one in
; clause-processors/meta-extract-user.lisp, but is simpler -- in particular, it
; does not include that book.

; For a more interesting tutorial example on meta-extract hypotheses, see
; community book
; /projects/acl2/acl2/books/demos/nth-update-nth-meta-extract.lisp.

(in-package "ACL2")

; The following is only needed for developer builds (using ACL2_DEVEL), which
; put some meta-extract functions in :program mode.
(include-book "system/meta-extract" :dir :system)
(include-book "misc/without-waterfall-parallelism" :dir :system)

(encapsulate
 ((bar (x) t))
 (local (defun bar (x) (declare (ignore x)) 1))
 (defthm bar-posp
   (posp (bar u))
   :rule-classes nil))

(defevaluator nthmeta-ev nthmeta-ev-lst
  ((typespec-check ts x)
   (if a b c)
   (equal a b)
   (not a)
   (iff a b)
   (implies a b)
   (nth n x)
   (car x)
   (binary-+ x y)
   (fix x)
   (posp x)
   (bar x)))

; Here is a trivial test of the use of meta-extract-global-fact in
; clause-processor rules.

(defun bar-posp-cl-proc (cl hint state)
  (declare (xargs :guard t
                  :stobjs state)
           (ignore hint))
  (if (equal (meta-extract-formula 'bar-posp state)
             '(POSP (BAR U)))
      (value (list (cons '(not (posp (bar u))) cl)))
    (value (list cl))))

(defthm correctness-of-bar-posp-cl-proc
  (implies (and (pseudo-term-listp cl)
                (alistp a)
                (nthmeta-ev (meta-extract-global-fact '(:formula bar-posp)
                                                      state)
                            a)
                (nthmeta-ev (conjoin-clauses
                             (clauses-result
                              (bar-posp-cl-proc cl hint state)))
                            a))
           (nthmeta-ev (disjoin cl) a))
  :rule-classes :clause-processor)

(without-waterfall-parallelism
(defthm bar-posp-cl-proc-test
  (integerp (bar u))
  :hints (("Goal"
           :clause-processor
           (bar-posp-cl-proc clause nil state)))))

; Our first example for :meta rules is based on one in
; books/clause-processors/meta-extract-user.lisp.

; First we develop a meta rule that implements the following fact.  (We do not
; need this event.)
(defthm nth-when-symbolp
  (implies (symbolp n)
           (equal (nth n x) (car x)))
  :rule-classes nil)

(defun nth-symbolp-metafn (term mfc state)
  (declare (xargs :stobjs state))
  (case-match term
    (('nth n x)
     (if (equal (mfc-ts n mfc state :forcep nil) *ts-symbol*)
         `(car ,x)
       (prog2$ (cw "Oops, the typeset of n is ~x0~%"
                   (mfc-ts n mfc state :forcep nil))
               term)))
    (& term)))

(defthm nth-symbolp-meta
    (implies (nthmeta-ev (meta-extract-contextual-fact
                          `(:typeset ,(cadr term))
                          mfc
                          state)
                         a)
             (equal (nthmeta-ev term a)
                    (nthmeta-ev (nth-symbolp-metafn term mfc state) a)))
    :rule-classes ((:meta :trigger-fns (nth))))

(defstub foo (x) t)

(defthm foo-test
  (implies (symbolp (foo x))
           (equal (nth (foo x) y)
                  (car y)))
  :hints (("Goal" :in-theory '(nth-symbolp-meta))))

(encapsulate
 ()

 (local (include-book "arithmetic-5/top" :dir :system))

 (defun nth-symbolp-metafn-alt (term mfc state)
   (declare (xargs :stobjs state))
   (case-match term
     (('nth n x)
      (if (let ((ts-n (mfc-ts n mfc state :forcep nil)))
            (and (integerp ts-n)          ; for guard verification
                 (<= *min-type-set* ts-n) ; for guard verification
                 (<= ts-n *max-type-set*) ; for guard verification
                 (ts-subsetp ts-n *ts-symbol*)))
          `(car ,x)
        (prog2$ (cw "Oops, the typeset of n is ~x0~%"
                    (mfc-ts n mfc state :forcep nil))
                term)))
     (& term)))

 (defthm nth-symbolp-meta-alt
   (implies (nthmeta-ev (meta-extract-contextual-fact
                         `(:typeset ,(cadr term)) mfc state)
                        a)
            (equal (nthmeta-ev term a)
                   (nthmeta-ev (nth-symbolp-metafn-alt term mfc state) a)))
   :rule-classes ((:meta :trigger-fns (nth)))))

(defthm foo-test-alt
  (implies (symbolp (foo x))
           (equal (nth (foo x) y)
                  (car y)))
  :hints (("Goal" :in-theory '(nth-symbolp-meta-alt))))

; Now, a slight variant involving subtypes, implementing this fact: (We do not
; need this event.)
(defthm plus-identity
  (implies (and (acl2-numberp n)
                (not (acl2-numberp x)))
           (equal (+ n x)
                  n))
  :rule-classes nil)

(encapsulate
 ()

 (local (include-book "arithmetic-5/top" :dir :system))

 (defun plus-identity-metafn (term mfc state)
   (declare (xargs :stobjs state))
   (case-match term
     (('binary-+ n x)
      (let ((ts-n (mfc-ts n mfc state :forcep nil))
            (ts-x (mfc-ts x mfc state :forcep nil)))
        (if (and (integerp ts-n)         ; for guard verification
                 (integerp ts-x)         ; for guard verification
                 (<= *min-type-set* ts-n) ; for guard verification
                 (<= *min-type-set* ts-x) ; for guard verification
                 (<= ts-n *max-type-set*) ; for guard verification
                 (<= ts-x *max-type-set*) ; for guard verification
                 (ts= ts-n *ts-positive-integer*)
                 (ts= ts-x *ts-character*))
            n
          (prog2$ (cw "Oops, the typesets of n and x are ~x0 and ~x1~%"
                      (mfc-ts n mfc state :forcep nil)
                      (mfc-ts x mfc state :forcep nil))
                  term))))
     (& term))))

(defthm plus-identity-meta
    (implies (and (nthmeta-ev (meta-extract-contextual-fact
                               `(:typeset ,(cadr term)) mfc state)
                              a)
                  (nthmeta-ev (meta-extract-contextual-fact
                               `(:typeset ,(caddr term)) mfc state)
                              a))
             (equal (nthmeta-ev term a)
                    (nthmeta-ev (plus-identity-metafn term mfc state) a)))
    :rule-classes ((:meta :trigger-fns (binary-+))))

(defthm plus-identity-test
  (implies (and (posp (foo x))
                (characterp (foo y)))
           (equal (+ (foo x) (foo y))
                  (foo x)))
  :hints (("Goal" :in-theory '(posp plus-identity-meta))))

(encapsulate
 ()

 (local (include-book "arithmetic-5/top" :dir :system))

 (defun plus-identity-2-metafn (term mfc state)
   (declare (xargs :stobjs state))
   (case-match term
     (('binary-+ ('bar n) y)
      (cond
       ((equal (meta-extract-formula 'bar-posp state)
               '(POSP (BAR U)))
        (let ((ts-y (mfc-ts y mfc state :forcep nil)))
          (if (and (integerp ts-y)        ; for guard verification
                   (<= *min-type-set* ts-y) ; for guard verification
                   (<= ts-y *max-type-set*) ; for guard verification
                   (ts= ts-y *ts-character*))
              (cadr term)
            (prog2$ (cw "Oops, the typesets of n and y are ~x0 and ~x1~%"
                        (mfc-ts n mfc state :forcep nil)
                        (mfc-ts y mfc state :forcep nil))
                    term))))
       (t term)))
     (& term))))

(defthm plus-identity-2-meta
    (implies (and (nthmeta-ev (meta-extract-global-fact '(:formula bar-posp)
                                                      state)
                              (list (cons 'u
                                          (nthmeta-ev (cadr (cadr term))
                                                      a))))
                  (nthmeta-ev (meta-extract-contextual-fact
                               `(:typeset ,(caddr term)) mfc state)
                              a))
             (equal (nthmeta-ev term a)
                    (nthmeta-ev (plus-identity-2-metafn term mfc state) a)))
    :rule-classes ((:meta :trigger-fns (binary-+))))

(defthm plus-identity-2-test
  (implies (and (posp (bar x))
                (characterp (foo y)))
           (equal (+ (bar x) (foo y))
                  (bar x)))
  :hints (("Goal" :in-theory '(plus-identity-2-meta))))