module Diagrams.Core.Names
(
AName(..)
, Name(..), IsName(..), (.>)
, Qualifiable(..)
) where
import Control.Lens (over, Wrapped(..), Rewrapped, iso, _Unwrapping')
import Data.List (intercalate)
import qualified Data.Map as M
import Data.Semigroup
import qualified Data.Set as S
import Data.Typeable
import Diagrams.Core.Transform
class (Typeable a, Ord a, Show a) => IsName a where
toName :: a -> Name
toName = Name . (:[]) . AName
instance IsName ()
instance IsName Bool
instance IsName Char
instance IsName Int
instance IsName Float
instance IsName Double
instance IsName Integer
instance IsName String
instance IsName a => IsName [a]
instance (IsName a, IsName b) => IsName (a,b)
instance (IsName a, IsName b, IsName c) => IsName (a,b,c)
data AName where
AName :: (Typeable a, Ord a, Show a) => a -> AName
deriving (Typeable)
instance IsName AName where
toName = Name . (:[])
instance Eq AName where
(AName a1) == (AName a2) =
case cast a2 of
Nothing -> False
Just a2' -> a1 == a2'
instance Ord AName where
(AName a1) `compare` (AName a2) =
case cast a2 of
Nothing -> show (typeOf a1) `compare` show (typeOf a2)
Just a2' -> a1 `compare` a2'
instance Show AName where
show (AName a) = show a
newtype Name = Name [AName]
deriving (Eq, Ord, Semigroup, Monoid, Typeable)
instance Wrapped Name where
type Unwrapped Name = [AName]
_Wrapped' = iso (\(Name ans) -> ans) Name
instance Rewrapped Name Name
instance Show Name where
show (Name ns) = intercalate " .> " $ map show ns
instance IsName Name where
toName = id
(.>) :: (IsName a1, IsName a2) => a1 -> a2 -> Name
a1 .> a2 = toName a1 <> toName a2
class Qualifiable q where
(|>) :: IsName a => a -> q -> q
instance Qualifiable Name where
(|>) = (.>)
instance Qualifiable a => Qualifiable (TransInv a) where
(|>) n = over (_Unwrapping' TransInv) (n |>)
instance (Qualifiable a, Qualifiable b) => Qualifiable (a,b) where
n |> (a,b) = (n |> a, n |> b)
instance (Qualifiable a, Qualifiable b, Qualifiable c) => Qualifiable (a,b,c) where
n |> (a,b,c) = (n |> a, n |> b, n |> c)
instance Qualifiable a => Qualifiable [a] where
n |> as = map (n |>) as
instance (Ord a, Qualifiable a) => Qualifiable (S.Set a) where
n |> s = S.map (n |>) s
instance Qualifiable a => Qualifiable (M.Map k a) where
n |> m = fmap (n |>) m
instance Qualifiable a => Qualifiable (b -> a) where
n |> f = (n |>) . f
infixr 5 |>
infixr 5 .>