File: Same.hs

package info (click to toggle)
haskell-graphviz 2999.17.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,488 kB
  • sloc: haskell: 12,152; makefile: 2
file content (53 lines) | stat: -rw-r--r-- 1,435 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
{-# OPTIONS_HADDOCK hide #-}

{- |
   Module      : Data.GraphViz.Attributes.Same
   Description : Consider Attributes equal on constructors.
   Copyright   : (c) Ivan Lazar Miljenovic
   License     : 3-Clause BSD-style
   Maintainer  : Ivan.Miljenovic@gmail.com

   This module is used when @a1 == a2@ should return @True@ if they
   are the same Attribute, even if they don't have the same value
   (typically for 'Set's).
-}
module Data.GraphViz.Attributes.Same
       ( SameAttr
       , SAttrs
       , toSAttr
       , unSame
       , unSameSet
       ) where

import Data.GraphViz.Attributes.Complete(Attribute, Attributes, sameAttribute)

import Data.Function(on)
import qualified Data.Set as Set
import Data.Set(Set)

-- -----------------------------------------------------------------------------

-- | Defined as a wrapper around 'Attribute' where equality is based
--   solely upon the constructor, not the contents.
newtype SameAttr = SA { getAttr :: Attribute }
                 deriving (Show, Read)

instance Eq SameAttr where
  (==) = sameAttribute `on` getAttr

instance Ord SameAttr where
  compare sa1 sa2
    | sa1 == sa2 = EQ
    | otherwise  = (compare `on` getAttr) sa1 sa2


type SAttrs = Set SameAttr

toSAttr :: Attributes -> SAttrs
toSAttr = Set.fromList . map SA

unSame :: SAttrs -> Attributes
unSame = map getAttr . Set.toList

unSameSet :: SAttrs -> Set Attribute
unSameSet = Set.mapMonotonic getAttr