Last active
June 20, 2018 04:50
-
-
Save camoy/8786dc3ce2a54cef72a1f5deeeffbd9f to your computer and use it in GitHub Desktop.
Graph reachability in four lines of Haskell (requires set-monad package)
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
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