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
|
#|
The Universal Example
J Strother Moore, Jeff Golden, Hanbing Liu, and Sandip Ray
In this file, we establish that for any int-valued function f, and
any x, there exists an M5 schedule which induces computation of (f x) by a
"universal" M5 state. The upshot is that if one is willing to accept
a bytecode specification employing non-constructive existential
quantification of the schedule component, then we have one very simple
program which computes an entire set of interesting functions. Too
bad such a specification is not meaningful!
Here is the code array of the "universal" method:
(defconst *universal-program*
'((iconst_0)
(iconst_1)
(iadd)
(goto -2)))
Here is our specification of the "interesting set of functions":
(encapsulate (((f *) => *))
(local
(defun f (x)
(declare (ignore x))
0))
(defthm f-is-jvm-int-valued
(and (integerp (f x))
(>= (f x) (* -1 (expt 2 31)))
(<= (f x) (1- (expt 2 31))))))
It constrains f to be a function of one argument that returns a Java
int.
In the context of the encapsulation above, and definitions given below, we
proved:
(defthm universal-computes-f
(equal (top
(stack
(top-frame 0
(run (universal-schedule x)
*universal-state*))))
(f x)))
If you want to know the value of a particular such f at an n, you may
thus run the "universal program" according to the schedule generated
by the schedule-generator and then look at the top of thread zero's
active operand stack.
To recertify:
(include-book "utilities")
(certify-book "universal" 1)
Sun Jun 30 23:21:06 2002
|#
(in-package "M5")
(defconst *universal-state*
'(((0 ((0 NIL NIL
((INVOKESTATIC "Universal" "universal" 0)
(POP)
(RETURN))
JVM::UNLOCKED "Universal"))
SCHEDULED NIL))
((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>" . "Universal"))
("java.lang.Object" ("monitor" . 0)
("mcount" . 0)
("wait-set" . 0))))
(("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))
("Universal" ("java.lang.Object")
NIL NIL NIL
(("<init>" NIL NIL (ALOAD_0)
(INVOKESPECIAL "java.lang.Object" "<init>" 0)
(RETURN))
("universal" NIL NIL
(ICONST_0)
(ICONST_1)
(IADD)
(GOTO -2))
("main" (|JAVA.LANG.STRING[]|)
NIL
(INVOKESTATIC "Universal" "universal" 0)
(POP)
(RETURN)))
(REF 5)))))
; Here is the code array of the method "universal":
(defconst *universal-program*
'((ICONST_0) ;;; 0
(ICONST_1) ;;; 1
(IADD) ;;; 2
(GOTO -2))) ;;; 3
; Here is our specification of the "interesting set of functions":
(encapsulate (((f *) => *))
(local
(defun f (x)
(declare (ignore x))
0))
(defthm f-is-jvm-int-valued
(and (integerp (f x))
(>= (f x) (* -1 (expt 2 31)))
(<= (f x) (1- (expt 2 31))))
:rule-classes nil))
; We provided the constraint above in terms of (expt 2 31) to make it
; understandable to outsiders. But here is the more useful version of
; the constraint.
(defthm intp-f
(intp (f x))
:hints (("Goal" :in-theory (enable intp)
:use f-is-jvm-int-valued)))
; Here is the notion of being poised at the top of the loop in the
; universal program.
(defun poised-at-universal-loop (th s i)
(and (equal (status th s) 'SCHEDULED)
(equal (pc (top-frame th s)) 1)
(equal (program (top-frame th s)) *universal-program*)
(intp (top (stack (top-frame th s))))
(integerp i)
(<= 0 i)))
; Here is a schedule that will drive us from the top of the loop
; sufficiently to increment the top of the stack by i.
(defun universal-loop-sched (th i)
(if (zp i)
nil
(append (repeat th 3)
(universal-loop-sched th (- i 1)))))
(defun universal-loop-hint (th s i)
(if (zp i)
(list th s i)
(universal-loop-hint
th
(modify th s
:stack
(push
(int-fix (+ 1 (top (stack (top-frame th s)))))
(pop (stack (top-frame th s)))))
(- i 1))))
; These ought to be in utilities.lisp
(defthm universal-loop-behavior
(implies (poised-at-universal-loop th s i)
(equal (run (universal-loop-sched th i) s)
(if (zp i)
s
(modify
th s
:stack
(push (int-fix (+ i (top (stack (top-frame th s)))))
(pop (stack (top-frame th s))))))))
:hints (("Goal"
:induct (universal-loop-hint th s i)
:in-theory (enable zp))))
(defun poised-to-invoke-universal (th s i)
(and (equal (status th s) 'SCHEDULED)
(equal (next-inst th s) '(INVOKESTATIC "Universal" "universal" 0))
(equal (lookup-method "universal" "Universal" (class-table s))
'("universal" NIL NIL
(ICONST_0)
(ICONST_1)
(IADD)
(GOTO -2)))
(integerp i)
(<= 0 i)))
(defun universal-sched (th i)
(append (repeat th 2)
(universal-loop-sched th i)))
(defthm universal-is-correct
(implies (poised-to-invoke-universal th s i)
(equal (top
(stack
(top-frame th
(run (universal-sched th i) s))))
(int-fix i))))
(in-theory (disable universal-sched))
; So now I want to prove that if k is an int, then k = (int-fix i),
; for some natural number i. First I define the corresponding
; natural number:
(defun int-fix-nat (k)
(cond ((< k 0)
(+ (expt 2 32) k))
(t k)))
; Here I prove it is a natural.
(defthm natp-int-fix-nat
(implies (intp k)
(and (integerp (int-fix-nat k))
(<= 0 (int-fix-nat k))))
:hints (("Goal" :in-theory (enable intp))))
; Here I prove it does the job.
(defthm every-int-is-int-fix-nat
(implies (intp k)
(equal (int-fix (int-fix-nat k)) k))
:hints (("Goal" :in-theory (enable int-fix intp))))
(in-theory (disable int-fix-nat))
; Consequently, we can now prove that with a suitable schedule,
; universal computes our arbitrary int-valued function f.
(defun universal-schedule (x)
(universal-sched 0 (int-fix-nat (f x))))
(defthm universal-computes-f
(equal (top
(stack
(top-frame 0
(run (universal-schedule x)
*universal-state*))))
(f x))
:rule-classes nil
:hints (("Goal" :in-theory (disable top-frame))))
; Now I demonstrate that we can use it. Here is a concrete example of
; how universal can compute the int-fix of factorial. That value is
; what we proved of our true Java fact program.
(defun factorial (n)
(if (zp n)
1
(* n (factorial (- n 1)))))
(defthm integerp-factorial
(integerp (factorial n))
:rule-classes :type-prescription)
(defun universal-factorial-schedule (n)
(universal-sched 0 (int-fix-nat (int-fix (factorial n)))))
; Here I prove that (int-fix (factorial n)) satisfies the constraints
; on (f n).
(defthm relieve-the-contraints
(and (integerp (int-fix (factorial n)))
(>= (int-fix (factorial n)) (* -1 (expt 2 31)))
(<= (int-fix (factorial n)) (1- (expt 2 31))))
:rule-classes
((:linear :corollary
(>= (int-fix (factorial n)) (* -1 (expt 2 31))))
(:linear :corollary
(<= (int-fix (factorial n)) (1- (expt 2 31)))))
:hints (("Goal" :in-theory (enable int-fix))))
; And so now I can functionally instantiate our universal correctness
; theorem.
(defthm universal-computes-factorial
(equal (top
(stack
(top-frame 0
(run (universal-factorial-schedule n)
*universal-state*))))
(int-fix (factorial n)))
:hints (("Goal"
:use (:instance (:functional-instance
universal-computes-f
(f (lambda (n) (int-fix (factorial n))))
(universal-schedule universal-factorial-schedule))
(x n)))))
|