Академический Документы
Профессиональный Документы
Культура Документы
monster
The next big challenge
Simon Peyton Jones
Microsoft Research
Spring 2008
c.f. static
Summary types 1995-
2005
X := In1
X := X*X
X := X + In2*In2
A1 A2
* A3
+
B1 * B2
N-shell of atom A
Atoms accessible in N hops (but no fewer) from A
A bigger example
1-shell of atom A
N-shell of atom A
Atoms accessible in N hops (but no fewer) from A
A bigger example
2-shell of atom A
N-shell of atom A
Atoms accessible in N hops (but no fewer) from A
A bigger example
To find the N-shell of A
• Find the (N-1) shell of A
• Union the 1-shells of each of those atoms
• Delete the (N-2) shell and (N-1) shell of A
Suppose N=4
A‟s 3-shell
A
A bigger example
To find the N-shell of A
• Find the (N-1) shell of A
• Union the 1-shells of each of those atoms
• Delete the (N-2) shell and (N-1) shell of A
Suppose N=4
A‟s 3-shell
Suppose N=4
mapUnion neighbours
s1 s2
nShell g (n-1) a nShell g (n-2) a
nShell :: Graph -> Int -> Atom -> Set Atom
nShell g 0 a = unitSet a
But... nShell g 1 a = neighbours g a
nShell g n a = (mapUnion (neighbours g) s1) – s1 – s2
where
s1 = nShell g (n-1) a
s2 = nShell g (n-2) a
nShell n needs
• nShell (n-1)
• nShell (n-2)
nShell :: Graph -> Int -> Atom -> Set Atom
nShell g 0 a = unitSet a
But... nShell g 1 a = neighbours g a
nShell g n a = (mapUnion (neighbours g) s1) – s1 – s2
where
s1 = nShell g (n-1) a
s2 = nShell g (n-2) a
nShell n needs
• nShell (n-1) which needs
• nShell (n-2)
• nShell (n-3) Duplicates!
• nShell (n-2) which needs
• nShell (n-3)
• nShell (n-4)
nShell :: Graph -> Int -> Atom -> Set Atom
nShell g 0 a = unitSet a
But... nShell g 1 a = neighbours g a
nShell g n a = (mapUnion (neighbours g) s1) – s1 – s2
where
s1 = nShell g (n-1) a
s2 = nShell g (n-2) a
Plan B
(radical)
No effects
Useless
Dangerous Safe
Plan A: build on what we have
How do we know
BUT (for sure) that a
function is pure?
No effects
The type tells (nearly) all
First class control structures
Values of type (IO t) are first class
Passed to functions
Returned as results
Stored in data structures
I/O
No effects
Pure (most)
The central challenge
Cross-fertilisation Plan B
(eg STM) (radical)
No effects
Useless
Dangerous Safe
Effects matter: transactions
Multiple threads with shared, mutable state
(Ted: “there will be places where we want shared state”)
Brand leader: locks and condition variables
New kid on the block: transactional memory
atomic { withdraw( A, 4 )
; deposit (B, 4 ) }
Optimistic concurrency:
o run code without taking locks, logging changes
o check at end whether transaction has seen a consistent view of
memory
o if so, commit effects to shared memory
o if not, abort and re-run transaction
Effects matter: transactions
TM only make sense if the transacted code
o Does no input output (can‟t be rolled back)
o Mutates only transacted variables
So effects form a spectrum
Any effect No effects
Mutate Tvars only
Can only
Can do read/write Tvars
arbitrary I/O No I/O!
The central challenge
Cross-fertilisation Plan B
(eg STM) (radical)
No effects
Useless
Dangerous Safe
My claims
Mainstream languages are hamstrung by
gratuitous (ie unnecessary) effects
T = 0; for (i=0; i<N; i++) { T = T + i }
Effects are part of the fabric of computation
1,000,000
10,000
100
Geeks
1,000,000
10,000
100
The slow death
Geeks
1,000,000
10,000
The total absence
100 of death
Geeks
100
The second life?
Geeks