datatype machine = Machine of string
datatype network =
LAN of machine list
| Connection of machine * network
Write an ML function search that takes two arguments: a
machine and a network, checks whether the machine is part of the
network, and returns true or false:
fun lookup m [] = false | lookup m (m'::ms) = (m=m') orelse (lookup m ms) fun search m (LAN(ms)) = lookup m ms | search m (Connection(m',n)) = (m=m') orelse (search m n)
datatype bop = PLUS | DIV | EQ
datatype Expression =
Number of int
| True | False
| Binary of Expression * bop * Expression
| Define of string * Expression * Expression
| Variable of string
datatype ExpType = Integer | Boolean
exception TypeError
{P} S1 {Q} {Q} S2 {R}
------------------------
{P} S1;S2 {R}
{P AND E} S {P}
------------------------------
{P} while (E) S {P AND (NOT E)
Using the rules prove that:
{r=p-x*y} (while (x <> 0) { r:= r+y; x=x-1; }) {r=p}
Why does the proof only establish partial correctness?
{r=p-x*y AND x<>0} r:= r+y {r=p-(x+1)*y} {r=p-(x+1)*y} x=x-1; {r=p-x*y}
-------------------------------------------------------------------------
{r=p-x*y AND x<>0} r:= r+y; x=x-1; {r=p-x*y}
----------------------------------------------------------------
{r=p-x*y} (while (x <> 0) { r:= r+y; x=x-1; }) {r=p-x*y AND x=0}
The proof only establishes partial correctness because it says nothing
about whether the loop terminates.
let fun f (x) = x+x
in f (7)
end
------------------------
let fun f (x) = x+x
val x = 3
in f (7)
end
------------------------
let val x = 3
fun f (x) = x+x
in f (7)
end
------------------------
let val x = 3
fun f (y) = x+y
in f (7)
end
------------------------
let val x = 3
fun f (y) = x+y
in let val x = 5
in f (7)
end
end
------------------------
let val g = let val x = 3
fun f (y) = x+y
in f
end
in let val x = 5
in g (7)
end
end
------------------------
let val x = 3
fun f (y) = x+y
in let val x = 4
fun g (h) = h (h (7))
in g (f)
end
end
Main
P
Q
R
X
Y
In one particular execution, the following procedures are called in
the given order:
Main, X, Y, X, Y, Main, P, Q, RNone of the procedures returned yet. Draw the stack after the call to
R. Your figure should include all activation records on
the stack, and for each activation record, the name of the procedure
being called, and the activation record to which the static link
points.
sabry@cs.uoregon.edu