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 | Lightweight pure functional monad-free type-safe concurrency.
> import Prelude hiding (read, return)
> read cont (i, is) = cont i is
> write o cont is = (x, (o, os)) where (x, os) = cont is
> return x is = (x, ())
Unlikely as it sounds, that code was motivated by
http://www.dpmms.cam.ac.uk/~martin/Research/Publications/2007/hp07.pdf
> chat alice bob = (alice_value, bob_value) where
> (alice_value, alice_output) = alice bob_output
> (bob_value, bob_output ) = bob alice_output
Example: Diffie-Hellman key exchange.
See http://en.wikipedia.org/wiki/Diffie–Hellman_key_exchange
> p = 23
> g = 5
> alice a =
> write "Hello" $
> write (g^a `mod` p) $
> read $ \b ->
> let s = b^a `mod` p in
> write "Goodbye" $
> return s
> bob b =
> read $ \message1 ->
> write (g^b `mod` p) $
> read $ \a ->
> let s = a^b `mod` p in
> read $ \message2 ->
> return s
> main = print $ chat (alice 6) (bob 15)
|
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 | Lightweight pure functional monad-free type-safe coroutines.
> import Prelude hiding (read, return)
> read cont (i, is) = cont i is
> write o cont is = (x, (o, os)) where (x, os) = cont is
> return x is = (x, ())
Unlikely as it sounds, that code was motivated by
http://www.dpmms.cam.ac.uk/~martin/Research/Publications/2007/hp07.pdf
> chat alice bob = (alice_value, bob_value) where
> (alice_value, alice_output) = alice bob_output
> (bob_value, bob_output ) = bob alice_output
Example: Diffie-Hellman key exchange.
See http://en.wikipedia.org/wiki/Diffie–Hellman_key_exchange
> p = 23
> g = 5
> alice a =
> write "Hello" $
> write (g^a `mod` p) $
> read $ \b ->
> let s = b^a `mod` p in
> write "Goodbye" $
> return s
> bob b =
> read $ \message1 ->
> write (g^b `mod` p) $
> read $ \a ->
> let s = a^b `mod` p in
> read $ \message2 ->
> return s
> main = print $ chat (alice 6) (bob 15)
|