Skip to content

Instantly share code, notes, and snippets.

@kuribas
Created March 14, 2019 08:52
Show Gist options
  • Select an option

  • Save kuribas/24f7b8abfd4384e66ad3b130f0f44797 to your computer and use it in GitHub Desktop.

Select an option

Save kuribas/24f7b8abfd4384e66ad3b130f0f44797 to your computer and use it in GitHub Desktop.
Graph tie the knot
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