Created
September 15, 2023 23:10
-
-
Save olligobber/818bd27243554a583afcf0db5c251bdc to your computer and use it in GitHub Desktop.
Bonding (xkcd.com/1188) for haskell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE RecursiveDo #-} | |
import Control.Concurrent (ThreadId, throwTo, yield, forkIO) | |
import Control.Exception (Exception, catch) | |
import Control.Monad (forever) | |
wait :: IO () | |
wait = forever yield | |
data Ball = Ball deriving Show | |
instance Exception Ball | |
type Person = ThreadId | |
person :: Person -> IO Person | |
person target = forkIO $ | |
forever $ wait `catch` (\Ball -> throwTo target Ball) | |
main :: IO () | |
main = mdo | |
parent <- person child | |
child <- person parent | |
throwTo parent Ball | |
wait |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment