{-# LANGUAGE UnicodeSyntax, FlexibleContexts #-} module GraphRewriting.Layout.Coulomb where import Data.View import Data.Functor () import GraphRewriting.Graph.Types import GraphRewriting.Graph.Read import GraphRewriting.Pattern () import GraphRewriting.Layout.Position import GraphRewriting.Layout.Force coulombForce ∷ View Position n ⇒ Node -> WithGraph n Force coulombForce :: forall n. View Position n => Node -> WithGraph n Force coulombForce Node node = do Vector2 n ← (Position -> Vector2) -> n -> Vector2 forall v n field. View v n => (v -> field) -> n -> field examine Position -> Vector2 position (n -> Vector2) -> ReaderT (Graph n) Identity n -> ReaderT (Graph n) Identity Vector2 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b <$> Node -> ReaderT (Graph n) Identity n forall n (m :: * -> *). (MonadReader (Graph n) m, MonadFail m) => Node -> m n readNode Node node [Vector2] ns ← ([n] -> [Vector2]) -> ReaderT (Graph n) Identity [n] -> ReaderT (Graph n) Identity [Vector2] forall a b. (a -> b) -> ReaderT (Graph n) Identity a -> ReaderT (Graph n) Identity b forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b fmap ((n -> Vector2) -> [n] -> [Vector2] forall a b. (a -> b) -> [a] -> [b] map ((n -> Vector2) -> [n] -> [Vector2]) -> (n -> Vector2) -> [n] -> [Vector2] forall a b. (a -> b) -> a -> b $ (Position -> Vector2) -> n -> Vector2 forall v n field. View v n => (v -> field) -> n -> field examine Position -> Vector2 position) ((Node -> ReaderT (Graph n) Identity n) -> [Node] -> ReaderT (Graph n) Identity [n] forall (t :: * -> *) (m :: * -> *) a b. (Traversable t, Monad m) => (a -> m b) -> t a -> m (t b) forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b] mapM Node -> ReaderT (Graph n) Identity n forall n (m :: * -> *). (MonadReader (Graph n) m, MonadFail m) => Node -> m n readNode ([Node] -> ReaderT (Graph n) Identity [n]) -> ReaderT (Graph n) Identity [Node] -> ReaderT (Graph n) Identity [n] forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b =<< ReaderT (Graph n) Identity [Node] forall n (m :: * -> *). MonadReader (Graph n) m => m [Node] readNodeList) Force -> WithGraph n Force forall a. a -> ReaderT (Graph n) Identity a forall (m :: * -> *) a. Monad m => a -> m a return (Force -> WithGraph n Force) -> Force -> WithGraph n Force forall a b. (a -> b) -> a -> b $ [Force] -> Force fsum [Vector2 -> Vector2 -> Force repulsion Vector2 n' Vector2 n | Vector2 n' ← [Vector2] ns]