File: p-formula-equiv.lsp

package info (click to toggle)
acl2 8.6%2Bdfsg-3
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 1,138,276 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,978; makefile: 3,840; ruby: 2,633; yacc: 1,126; ml: 763; awk: 295; csh: 233; lex: 197; php: 178; tcl: 49; asm: 23; haskell: 17
file content (64 lines) | stat: -rw-r--r-- 2,310 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
(in-package "ACL2")

(include-book "../top")
(include-book "../acl2s-utils/top")

(in-package "ACL2S")

;; Here's a data definition representing a simple propositional formula
(defdata p-formula
  (or bool
      var
      (list 'not p-formula)
      (list (enum '(and or implies)) p-formula p-formula)))

:q

(defpackage :simplifier (:use :cl))
(in-package :simplifier)
(import '(acl2s::p-formulap
          acl2s::nth-p-formula
          acl2s::boolp
          acl2s::varp
          acl2s::implies))
(import '(acl2s-interface::acl2s-query
          acl2s-interface::quiet-mode-on))

;; Here's an example of some arbitrary transformation on p-formulas
;; that we claim produces an equivalent p-formula
(defun simplify-nots (f)
  (assert (p-formulap f))
  (cond ((boolp f) f)
        ((varp f) f)
        ((eql (car f) 'not) ;; is (list 'not ?)
         (let ((simplified-arg (simplify-nots (second f))))
           (if (and (consp simplified-arg) (eql (car simplified-arg) 'not))
               (second simplified-arg)
             (list 'not simplified-arg))))
        (t (list (car f) (simplify-nots (second f)) (simplify-nots (third f))))))

;; Now, let's test it.
;; Turn on quiet mode to prevent a ton of extra output
(quiet-mode-on)
(print "Testing...")
;; Run 1000 tests.
(loop for n from 0 to 1000
      ;; first, use the enumerator from the defdata to generate a
      ;; random p-formula
      do (let* ((seed (random (1- (expt 2 31))))
                (f (nth-p-formula seed)))
           ;; The enumerator has a small chance of generating a
           ;; non-p-formula, so we check for that here.
           (if (not (p-formulap f))
               (format t "-")
             (let* ;; simplify the formula
                 ((simp-f (simplify-nots f))
                  ;; ask ACL2 to prove that they are equivalent
                  (res (acl2s-query `(thm (iff ,f ,simp-f)))))
               ;; If (car res) = t, then the proof failed.
               (if (car res)
                   (format t "simplify-nots returns a nonequivalent expression for f=~S~%" f)
                 ;; Otherwise, it succeeded so let's print a character to denote this.
                 (format t "."))))
           ;; Just so we see output while testing is running
           (when (eql (mod n 50) 49) (force-output))))