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 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
|
; This book proves the correctness of a recursive static method for
; factorial on M5.
#|
; Certification Instructions.
(include-book "utilities")
(certify-book "idemo" 1)
J Moore
Here is the Java for an iterative factorial method.
class IDemo {
public static int ifact(int n){
int temp = 1;
while (0<n) {
temp = n*temp;
n = n-1;
}
return temp;
}
}
The corresponding bytecode for ifact is shown below.
Method int ifact(int)
0 iconst_1
1 istore_1
2 goto 13
5 iload_0
6 iload_1
7 imul
8 istore_1
9 iload_0
10 iconst_1
11 isub
12 istore_0
13 iload_0
14 ifgt 5
17 iload_1
18 ireturn
|#
(in-package "M5")
; Note that we have not shown a main program for our IDemo
; class. We make one up below. The initial state is only used to
; play around. We don't care about the main program when we prove
; ifact correct.
(defconst *IDemo-thread-table*
(list
(cons 0
(make-thread
(push
(make-frame
0
nil
nil
'((ICONST\_4)
(ICONST\_4)
(IADD)
(INVOKESTATIC "IDemo" "ifact" 1)
(HALT))
'UNLOCKED
"IDemo")
nil)
'SCHEDULED
nil))))
(defconst *IDemo-heap*
'((0 . (("java.lang.Class"
("<name>" . "java.lang.Object"))
("java.lang.Object"
("monitor" . 0)
("mcount" . 0)
("wait-set" . 0))))
(1 . (("java.lang.Class"
("<name>" . "ARRAY"))
("java.lang.Object"
("monitor" . 0)
("mcount" . 0)
("wait-set" . 0))))
(2 . (("java.lang.Class"
("<name>" . "java.lang.Thread"))
("java.lang.Object"
("monitor" . 0)
("mcount" . 0)
("wait-set" . 0))))
(3 . (("java.lang.Class"
("<name>" . "java.lang.String"))
("java.lang.Object"
("monitor" . 0)
("mcount" . 0)
("wait-set" . 0))))
(4 . (("java.lang.Class"
("<name>" . "java.lang.Class"))
("java.lang.Object"
("monitor" . 0)
("mcount" . 0)
("wait-set" . 0))))
(5 . (("java.lang.Class"
("<name>" . "IDemo"))
("java.lang.Object"
("monitor" . 0)
("mcount" . 0)
("wait-set" . 0))))))
(defconst *IDemo-class-table*
'(("java.lang.Object"
NIL
("monitor" "mcount" "wait-set")
NIL
NIL
(("<init>" NIL NIL (RETURN)))
(REF 0))
("ARRAY"
("java.lang.Object")
(("<array>" . *ARRAY*))
NIL
NIL
NIL
(REF 1))
("java.lang.Thread"
("java.lang.Object")
NIL
NIL
NIL
(("run" NIL NIL (RETURN))
("start" NIL NIL NIL)
("stop" NIL NIL NIL)
("<init>" NIL NIL (ALOAD\_0)
(INVOKESPECIAL "java.lang.Object" "<init>" 0)
(RETURN)))
(REF 2))
("java.lang.String"
("java.lang.Object")
("strcontents")
NIL
NIL
(("<init>" NIL NIL
(ALOAD\_0)
(INVOKESPECIAL "java.lang.Object" "<init>" 0)
(RETURN)))
(REF 3))
("java.lang.Class"
("java.lang.Object")
NIL
NIL
NIL
(("<init>" NIL NIL
(ALOAD\_0)
(INVOKESPECIAL "java.lang.Object" "<init>" 0)
(RETURN)))
(REF 4))
("IDemo"
("java.lang.Object")
NIL
NIL
NIL
(("<init>" NIL NIL
(ALOAD\_0)
(INVOKESPECIAL "java.lang.Object" "<init>" 0)
(RETURN))
("ifact" (INT) NIL
(ICONST\_1)
(ISTORE\_1)
(GOTO 11)
(ILOAD\_0)
(ILOAD\_1)
(IMUL)
(ISTORE\_1)
(ILOAD\_0)
(ICONST\_1)
(ISUB)
(ISTORE\_0)
(ILOAD\_0)
(IFGT -9)
(ILOAD\_1)
(IRETURN)))
(REF 5))))
(defconst *IDemo-state*
(make-state *IDemo-thread-table*
*IDemo-heap*
*IDemo-class-table*))
; The Mathematical Function
(defun factorial (n)
(if (zp n)
1
(* n (factorial (- n 1)))))
(defthm integerp-factorial
(integerp (factorial n))
:rule-classes :type-prescription)
; A Schedule that Runs fact to Completion
; This subroutine computes a schedule suitable for starting at the
; (ILOAD_0) at pc 13, which we consider the "top" of the loop. The
; schedule drives the machine to (but not through) the (IRETURN).
(defun ifact-loop-sched (th n)
(if (zp n)
(repeat th 3)
(append (repeat th 10)
(ifact-loop-sched th (- n 1)))))
(defun ifact-sched (th n)
(append (repeat th 4)
(ifact-loop-sched th n)
(repeat th 1)))
; Playing Around with Main
; This schedule executes main to termination.
(defun imain-sched (th)
(append (repeat th 3)
(ifact-sched th 8)))
(defthm isample-execution
(equal (top
(stack
(top-frame 0
(run (imain-sched 0)
*IDemo-state*))))
(factorial 8))
:rule-classes nil)
; Proving Fact Correct
(defun ifactorial (n temp)
(if (zp n)
temp
(ifactorial (- n 1) (int-fix (* n temp)))))
(defconst *ifact-def*
(lookup-method "ifact" "IDemo" *IDemo-class-table*))
(defun poised-at-ifact-loop (th s n)
(and (equal (status th s) 'SCHEDULED)
(equal (pc (top-frame th s)) 13)
(equal (program (top-frame th s)) (method-program *ifact-def*))
(equal n (nth 0 (locals (top-frame th s))))
(intp n)
(intp (nth 1 (locals (top-frame th s))))))
(defun ifact-loop-induction-hint (th s n)
(if (zp n)
s
(ifact-loop-induction-hint
th
(make-state
(bind
th
(make-thread
(push
(make-frame 13
(update-nth 0 (- n 1)
(update-nth 1 (int-fix
(* n
(nth 1 (locals
(top-frame th s)))))
(locals (top-frame th s))))
(stack (top-frame th s))
(method-program *ifact-def*)
(sync-flg (top-frame th s))
(cur-class (top-frame th s)))
(pop (call-stack th s)))
'scheduled
(rref th s))
(thread-table s))
(heap s)
(class-table s))
(- n 1))))
(defthm ifact-loop-is-correct
(implies (poised-at-ifact-loop th s n)
(equal
(run (ifact-loop-sched th n) s)
(modify th s
:pc 18
:locals
(if (zp n)
(locals (top-frame th s))
(update-nth 0 0
(update-nth 1
(int-fix
(ifactorial
n
(nth 1 (locals (top-frame th s)))))
(locals (top-frame th s)))))
:stack
(push (int-fix
(ifactorial
n
(nth 1 (locals (top-frame th s)))))
(stack (top-frame th s))))))
:hints (("Goal"
:induct (ifact-loop-induction-hint th s n))))
(defun poised-to-invoke-ifact (th s n)
(and (equal (status th s) 'SCHEDULED)
(equal (next-inst th s) '(invokestatic "IDemo" "ifact" 1))
(equal n (top (stack (top-frame th s))))
(intp n)
(equal (lookup-method "ifact" "IDemo" (class-table s))
*ifact-def*)))
(defthm ifact-is-correct
(implies (poised-to-invoke-ifact th s n)
(equal
(run (ifact-sched th n) s)
(modify th s
:pc (+ 3 (pc (top-frame th s)))
:stack
(push (int-fix (ifactorial n 1))
(pop (stack (top-frame th s))))))))
(in-theory (disable ifact-sched))
(defthm ifact-is-fact
(implies (and (integerp n)
(integerp temp))
(equal (int-fix (ifactorial n temp))
(int-fix (* temp (factorial n))))))
(defthm ifact-main-result
(implies (poised-to-invoke-ifact th s n)
(equal
(run (ifact-sched th n) s)
(modify th s
:pc (+ 3 (pc (top-frame th s)))
:stack
(push (int-fix (factorial n))
(pop (stack (top-frame th s))))))))
|