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
|
; (include-book "m1")
; (certify-book "g-invariant" 1)
(in-package "M1")
(include-book "misc/defpun" :dir :system)
(defmacro defpun (g args &rest tail)
`(acl2::defpun ,g ,args ,@tail))
(defmacro defspec (name prog inputs pre-pc post-pc annotation-alist)
(let ((Inv
(intern-in-package-of-symbol
(concatenate 'string (symbol-name name) "-INV")
'run))
(Inv-def
(intern-in-package-of-symbol
(concatenate 'string (symbol-name name) "-INV-DEF")
'run))
(Inv-opener
(intern-in-package-of-symbol
(concatenate 'string (symbol-name name) "-INV-OPENER")
'run))
(Inv-step
(intern-in-package-of-symbol
(concatenate 'string (symbol-name name) "-INV-STEP")
'run))
(Inv-run
(intern-in-package-of-symbol
(concatenate 'string (symbol-name name) "-INV-RUN")
'run))
(Correctness
(intern-in-package-of-symbol
(concatenate 'string "PARTIAL-CORRECTNESS-OF-PROGRAM-"
(symbol-name name))
'run)))
`(progn
(defpun ,Inv (,@inputs s)
(if (member (pc s)
',(strip-cars annotation-alist))
(and (equal (program s)
,prog)
(case (pc s)
,@annotation-alist))
(,Inv ,@inputs (step s))))
(defthm ,Inv-opener
(implies (and (equal pc (pc s))
(syntaxp (quotep pc))
(not
(member pc
',(strip-cars annotation-alist))))
(equal (,Inv ,@inputs s)
(,Inv ,@inputs (step s)))))
(defthm ,Inv-step
(implies (,Inv ,@inputs s)
(,Inv ,@inputs (step s))))
(defthm ,Inv-run
(implies (,Inv ,@inputs s)
(,Inv ,@inputs (run sched s)))
:rule-classes nil
:hints (("Goal" :in-theory (e/d (run)(,Inv-def)))))
(defthm ,Correctness
(let* ((sk (run sched s0)))
(implies
(and (let ((s s0)) ,(cadr (assoc pre-pc annotation-alist)))
(equal (pc s0) ,pre-pc)
(equal (locals s0) (list* ,@inputs any))
(equal (program s0) ,prog)
(equal (pc sk) ,post-pc))
(let ((s sk)) ,(cadr (assoc post-pc annotation-alist)))))
:hints (("Goal" :use
(:instance ,Inv-run
,@(pairlis$ inputs (acl2::pairlis-x2 inputs nil))
(s s0)
(sched sched))))
:rule-classes nil))))
(defun f (n)
(if (zp n)
1
(* n (f (- n 1)))))
(defconst *g*
'((PUSH 1)
(STORE 1)
(LOAD 0)
(IFLE 10)
(LOAD 0)
(LOAD 1)
(MUL)
(STORE 1)
(LOAD 0)
(PUSH 1)
(SUB)
(STORE 0)
(GOTO -10)
(LOAD 1)
(RETURN)))
(defun n (s) (nth 0 (locals s)))
(defun a (s) (nth 1 (locals s)))
(defspec g *g* (n0 a0) 0 14
((0 ; Pre-condition
(and (equal n0 (n s))
(natp n0)))
(2 ; Loop Invariant
(and (natp n0)
(natp (n s))
(natp (nth 1 (locals s)))
(<= (n s) n0)
(equal (f n0) (* (f (n s)) (a s)))))
(14 ; Post-condition
(equal (top (stack s)) (f n0)))))
(defthm corollary
(let ((sk (run sched s0)))
(implies (and (equal n0 (n s0))
(natp n0)
(equal (pc s0) 0)
(equal (locals s0) (list* n0 a0 any))
(equal (program s0) *g*)
(equal (pc sk) 14))
(equal (top (stack sk))
(f n0))))
:hints (("Goal" :use PARTIAL-CORRECTNESS-OF-PROGRAM-G)))
; Here is a proof of the half method.
(defconst *h*
; public static int half(int n){
; int a = 0;
; while (n!=0){a=a+1;n=n-2;};
; return a;
; }
'((push 0) ; 0
(store 1) ; 1
(goto 9) ; 2
(load 1) ; 3
(push 1) ; 4
(add) ; 5
(store 1) ; 6
(load 0) ; 7
(push 2) ; 8
(sub) ; 9
(store 0) ; 10
(load 0) ; 11
(ifne -9) ; 12
(load 1) ; 13
(return)) ; 14
)
(defun run-h (n)
(run (repeat 0 10000)
(make-state 0
(list n 0)
nil
*h*)))
(defspec h *h* (n0) 0 14
(; Pre-Condition:
(0 (and (equal (n s) n0)
(natp n0)))
; Loop Invariant:
(11 (and (natp n0)
(integerp (n s))
(if (and (natp (a s))
(evenp (n s)))
(equal (+ (a s) (/ (n s) 2))
(/ n0 2))
(not (evenp (n s))))
(iff (evenp n0) (evenp (n s)))))
; Post-condition:
(14 (and (evenp n0)
(equal (top (stack s)) (/ n0 2))))))
(defthm h-corollary
(implies (and (natp (n s0))
(equal (pc s0) 0)
(equal (program s0) *h*)
(equal sk (run sched s0))
(equal (pc sk) 14))
(and (evenp (n s0))
(equal (top (stack sk)) (/ (n s0) 2))))
:hints
(("Goal"
:use (:instance PARTIAL-CORRECTNESS-OF-PROGRAM-H
(n0 (n s0))
(any (cdr (locals s0)))
))))
|