Skip to content

Instantly share code, notes, and snippets.

@camoy
Last active June 20, 2018 04:50
Show Gist options
  • Save camoy/8786dc3ce2a54cef72a1f5deeeffbd9f to your computer and use it in GitHub Desktop.
Save camoy/8786dc3ce2a54cef72a1f5deeeffbd9f to your computer and use it in GitHub Desktop.
Graph reachability in four lines of Haskell (requires set-monad package)
import qualified Data.Set.Monad as S
fix f init = until (\x -> x == f x) f init
close rstar = fix (\s -> s `S.union` (rstar s))
rstar g s = do { x <- s; (x, y) <- g; pure y }
reachable g = close (rstar g)
-- g1 = S.fromList [(1, 2), (2, 3), (3, 1), (4, 1)]
-- reachable g1 (S.singleton 1) == S.fromList [1, 2, 3]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment