Created
March 14, 2019 08:52
-
-
Save kuribas/24f7b8abfd4384e66ad3b130f0f44797 to your computer and use it in GitHub Desktop.
Graph tie the knot
This file contains hidden or 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
| module LinkGraph where | |
| import Data.IntMap | |
| import Data.Function | |
| import Data.Maybe | |
| import Prelude hiding (lookup) | |
| data Fix f = Rec { unRec :: f (Fix f)} | |
| unsafeLinkGraph :: Functor f => IntMap (f Int) -> IntMap (Fix f) | |
| unsafeLinkGraph g = | |
| fix $ \res -> fmap (Rec . (fmap (fromJust . flip lookup res))) g | |
| isCompleteGraph :: Foldable f => IntMap (f Int) -> Bool | |
| isCompleteGraph g = all (all (isJust . flip lookup g)) g | |
| linkGraph :: (Foldable f, Functor f) => IntMap (f Int) -> Maybe (IntMap (Fix f)) | |
| linkGraph m | isCompleteGraph m = Just $ unsafeLinkGraph m | |
| linkGraph _ = Nothing |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment