2016-01-23 05:29:46 +03:00
|
|
|
{-# LANGUAGE NoMonomorphismRestriction, FlexibleContexts, TypeFamilies #-}
|
|
|
|
|
|
|
|
module Util (
|
|
|
|
portToPort,
|
|
|
|
iconToPort,
|
|
|
|
iconToIcon,
|
|
|
|
iconToIconEnds,
|
2016-02-05 08:53:21 +03:00
|
|
|
--iconHeadToPort,
|
2016-01-23 05:29:46 +03:00
|
|
|
iconTailToPort,
|
2016-02-05 08:53:21 +03:00
|
|
|
toNames,
|
2016-02-26 05:37:04 +03:00
|
|
|
makeSimpleEdge,
|
2016-02-05 08:53:21 +03:00
|
|
|
noEnds,
|
|
|
|
nameAndPort,
|
2016-02-08 05:01:57 +03:00
|
|
|
justName,
|
2016-02-24 07:47:08 +03:00
|
|
|
fromMaybeError,
|
2016-02-27 09:58:49 +03:00
|
|
|
mapFst,
|
2016-05-29 10:55:09 +03:00
|
|
|
printSelf,
|
2016-11-22 04:20:18 +03:00
|
|
|
eitherToMaybes,
|
|
|
|
maybeBoolToBool
|
2016-01-23 05:29:46 +03:00
|
|
|
)where
|
|
|
|
|
|
|
|
import Control.Arrow(first)
|
|
|
|
import Diagrams.Prelude(IsName, toName, Name)
|
2016-02-08 05:01:57 +03:00
|
|
|
import Data.Maybe(fromMaybe)
|
2016-02-27 09:58:49 +03:00
|
|
|
import qualified Debug.Trace
|
2016-01-23 05:29:46 +03:00
|
|
|
|
2016-02-26 05:37:04 +03:00
|
|
|
import Types(EdgeEnd(..), Edge(..), NameAndPort(..), Connection)
|
2016-01-23 05:29:46 +03:00
|
|
|
|
2016-02-18 04:31:11 +03:00
|
|
|
mapFst :: Functor f => (a -> b) -> f (a, c) -> f (b, c)
|
|
|
|
mapFst f = fmap (first f)
|
2016-01-23 05:29:46 +03:00
|
|
|
|
|
|
|
toNames :: (IsName a) => [(a, b)] -> [(Name, b)]
|
|
|
|
toNames = mapFst toName
|
|
|
|
|
2016-01-23 06:42:15 +03:00
|
|
|
noEnds :: (EdgeEnd, EdgeEnd)
|
2016-01-23 05:29:46 +03:00
|
|
|
noEnds = (EndNone, EndNone)
|
|
|
|
|
2016-02-26 05:37:04 +03:00
|
|
|
makeSimpleEdge :: Connection -> Edge
|
|
|
|
makeSimpleEdge = Edge [] noEnds
|
|
|
|
|
2016-02-05 08:53:21 +03:00
|
|
|
nameAndPort :: IsName a => a -> Int -> NameAndPort
|
|
|
|
nameAndPort n p = NameAndPort (toName n) (Just p)
|
|
|
|
|
|
|
|
justName :: IsName a => a -> NameAndPort
|
|
|
|
justName n = NameAndPort (toName n) Nothing
|
|
|
|
|
2016-01-23 05:29:46 +03:00
|
|
|
-- Edge constructors --
|
|
|
|
portToPort :: (IsName a, IsName b) => a -> Int -> b -> Int -> Edge
|
2016-02-26 05:37:04 +03:00
|
|
|
portToPort a b c d = makeSimpleEdge (nameAndPort a b, nameAndPort c d)
|
2016-01-23 05:29:46 +03:00
|
|
|
|
|
|
|
iconToPort :: (IsName a, IsName b) => a -> b -> Int -> Edge
|
2016-02-26 05:37:04 +03:00
|
|
|
iconToPort a c d = makeSimpleEdge (justName a, nameAndPort c d)
|
2016-01-23 05:29:46 +03:00
|
|
|
|
|
|
|
iconToIcon :: (IsName a, IsName b) => a -> b -> Edge
|
2016-02-26 05:37:04 +03:00
|
|
|
iconToIcon a c = makeSimpleEdge (justName a, justName c)
|
2016-01-23 05:29:46 +03:00
|
|
|
|
|
|
|
|
|
|
|
-- If there are gaps between the arrow and the icon, try switching the first two arguments
|
|
|
|
-- with the last two arguments
|
|
|
|
iconToIconEnds :: (IsName a, IsName b) => a -> EdgeEnd -> b -> EdgeEnd -> Edge
|
2016-02-26 05:37:04 +03:00
|
|
|
iconToIconEnds a b c d = Edge [] (b, d) (justName a, justName c)
|
2016-01-23 05:29:46 +03:00
|
|
|
|
2016-02-05 08:53:21 +03:00
|
|
|
-- iconHeadToPort :: (IsName a, IsName b) => a -> EdgeEnd -> b -> Int -> Edge
|
|
|
|
-- iconHeadToPort a endHead c d = Edge (justName a, nameAndPort c d) (EndNone, endHead)
|
2016-01-23 05:29:46 +03:00
|
|
|
|
2016-01-23 06:42:15 +03:00
|
|
|
iconTailToPort :: (IsName a, IsName b) => a -> EdgeEnd -> b -> Int -> Edge
|
2016-02-26 05:37:04 +03:00
|
|
|
iconTailToPort a endTail c d = Edge [] (endTail, EndNone) (justName a, nameAndPort c d)
|
2016-02-08 05:01:57 +03:00
|
|
|
|
|
|
|
fromMaybeError :: String -> Maybe a -> a
|
|
|
|
fromMaybeError s = fromMaybe (error s)
|
2016-02-27 09:58:49 +03:00
|
|
|
|
|
|
|
printSelf :: (Show a) => a -> a
|
|
|
|
printSelf a = Debug.Trace.trace (show a ++ "\n\n") a
|
2016-05-29 10:55:09 +03:00
|
|
|
|
|
|
|
eitherToMaybes :: Either a b -> (Maybe a, Maybe b)
|
|
|
|
eitherToMaybes (Left x) = (Just x, Nothing)
|
|
|
|
eitherToMaybes (Right y) = (Nothing, Just y)
|
2016-11-22 04:20:18 +03:00
|
|
|
|
|
|
|
-- | (Just True) = True, Nothing = False
|
|
|
|
maybeBoolToBool :: Maybe Bool -> Bool
|
|
|
|
maybeBoolToBool = or
|