State

Mathematically speaking…
Modeling Change
by Pavel Gladyshev
Last homework discussion
• Lee
• Ahmed
Intuitive concept of state
• World is a collection of interacting objects
– Society
– Pebbles on the beach
– Cars in traffic
• Objects & their properties change over time
• State is a snapshot of the world at an instant
• State can be modeled mathematically.
A difficulty: modeling change
• There is no implicit notion of time and change
in mathematics.
• All math definitions stay the same forever
• Time and change need to be modeled using
functions.
• Two key ideas:
1. State = function (time)
2. New state = Old state + update
Oscillation of a pendulum as a function
of time
Political views of a person as a
function of time
H set of all humans
P = {Capitalist, Communist}
R set of real numbers modeling time
views : H ´ R ® P
views(human,time)  political views of the particular
person at a moment in time
Political views of Roman Abramovich
as a function of time
P
Capitalist
views( “Roman Abramovich”, time)
Communist
R
1991
State change as a sequence of
state updates
• Sometimes it is hard to define state as a
algebraic formula of time:
– Oscillation of a pendulum with several pushes
– Positions of balls on a billiard table after a strike
– Behaviur of an interactive computer system
• In such cases, the state change over time is
calculated as a sequence of instantaneous
state updates.
Differential equation
• Newton's Law of Cooling states that the rate of
change of the temperature of an object is
proportional to the difference between its own
temperature and the ambient temperature:
dT
= k × (T - Tambient )
dt
T – temperature, t - time
slope
dT
T1 » T0 +
(t1 - t0 ) » T0 + k(T0 - Troom )(t1 - t0 )
dt
T0
T1
Troom
t0 t1
Computing Greatest Common Divisor
• gcd(a,b) – largest
number that divides
both a and b
gcd(a,b)
yes
1
function gcd(a, b)
if a = 0
return b
while b ≠ 0
if a > b
a := a − b
else
b := b − a
return a
a=0
yes
2
4
b=0
r := a
5
r := b
3
a>b
6
7
b:=b-a
yes
a:=a-b
8
halt
Computer stays halt
State
S = {(a, b,ip, r) | a Î N0 Ùb Î N0 Ùip Î N 0 ^r Î N0 Ù1£ ip £ 8}
a, b – non-negative integers
ip – instruction pointer: the number of the next
command to be executed {1,2,3,4,5,6,7,8}
r - result
Change of state (transition function)
gcd(a,b)
yes
1
a=0
2
b=0
yes
r := a
4
5
ff(
r := b
3
a>b
6
7
b:=b-a
yes
a:=a-b
8
halt
Computer stays halt
f :S ®S
gcd(a,b)
yes
1
a=0
2
b=0
yes
4
r := a
3
a>b
6
b:=b-a
7 yes
a:=ab
8
halt
5
r := b
ì (a, b, 2, r)
ï
ï (a, b, 5, r)
ï (a, b, 3, r)
ï
ï (a, b, 4, r)
ï
ïï (a, b, 7, r)
f (a, b, ip, r) = í (a, b, 6, r)
ï (a, b,8, a)
ï
ï (a, b,8, b)
ï
ï (a, b - a, 2, r)
ï (a - b, b, 2, r)
ï
ïî (a, b, 8, r)
Computer stays halt
if a ¹ 0 Ùip = 1
if a = 0 Ùip = 1
if b ¹ 0 Ùip = 2
if b = 0 Ùip = 2
if a > b Ùip = 3
if a £ b Ùip = 3
if ip = 4
if ip = 5
if ip = 6
if ip = 7
if ip = 8
Computation example: Initial state = (2,1,1,0)
ì (a, b, 2, r)
ï
ï (a, b, 5, r)
ï (a, b, 3, r)
ï
ï (a, b, 4, r)
ï
ïï (a, b, 7, r)
f (a, b, ip, r) = í (a, b, 6, r)
ï (a, b,8, a)
ï
ï (a, b,8, b)
ï
ï (a, b - a, 2, r)
ï (a - b, b, 2, r)
ï
ïî (a, b, 8, r)
if a ¹ 0 Ùip = 1
if a = 0 Ùip = 1
if b ¹ 0 Ùip = 2
if b = 0 Ùip = 2
if a > b Ùip = 3
if a £ b Ùip = 3
if ip = 4
if ip = 5
if ip = 6
if ip = 7
if ip = 8
f ((2,1,1, 0)) ® (2,1, 2, 0)
f ((2,1, 2, 0)) ® (2,1,3, 0)
f ((2,1,3, 0)) ® (2,1, 7, 0)
f ((2,1, 7, 0)) ® (1,1, 2, 0)
f ((1,1, 2, 0)) ® (1,1,3, 0)
f ((1,1,3, 0)) ® (1,1, 6, 0)
f ((1,1, 6, 0)) ® (1, 0, 2, 0)
f ((1, 0, 2, 0)) ® (1, 0,8,1)
f ((1, 0,8,1)) ® (1, 0,8,1)
f ((1, 0,8,1)) ® (1, 0,8,1)
Termination proof
• One of the key properties of a useful program
is that it does not hang when given valid input
• This is known as proof of termination: i.e.
proof that for all valid inputs the program
eventually reaches a final state
Homework
1. Think (and post in the forum) how you could
formally define a computation of f() ?
1. Think (and post in the forum) how would you
go about proving that for all initial states
of the form (a,b,1,0), where a>0, b>0, every
computation of f() reaches a state with ip=8 in
a finite number of steps?