1
1
mirror of https://github.com/github/semantic.git synced 2024-12-27 00:44:57 +03:00
semantic/src/Data/Graph.hs

40 lines
1.5 KiB
Haskell
Raw Normal View History

2018-05-11 19:09:21 +03:00
{-# LANGUAGE GeneralizedNewtypeDeriving, TypeFamilies, UndecidableInstances #-}
2018-05-11 19:07:05 +03:00
module Data.Graph
( Graph(..)
2018-05-11 19:09:37 +03:00
, Class.overlay
, Class.connect
, Class.vertex
2018-05-11 19:10:27 +03:00
, simplify
2018-05-11 19:07:05 +03:00
) where
import qualified Algebra.Graph as G
2018-05-11 19:09:21 +03:00
import qualified Algebra.Graph.Class as Class
2018-05-11 19:10:59 +03:00
import Prologue
2018-05-11 19:07:05 +03:00
newtype Graph vertex = Graph (G.Graph vertex)
2018-05-11 19:09:21 +03:00
deriving (Eq, Foldable, Functor, Class.Graph, Show, Class.ToGraph, Traversable)
2018-05-11 19:10:27 +03:00
2018-05-11 19:12:05 +03:00
simplify :: Ord vertex => Graph vertex -> Graph vertex
2018-05-11 19:10:27 +03:00
simplify (Graph graph) = Graph (G.simplify graph)
2018-05-11 19:10:59 +03:00
2018-05-11 19:12:05 +03:00
instance Semigroup (Graph vertex) where
2018-05-11 19:10:59 +03:00
(<>) = Class.overlay
2018-05-11 19:11:03 +03:00
2018-05-11 19:12:05 +03:00
instance Monoid (Graph vertex) where
2018-05-11 19:11:03 +03:00
mempty = Class.empty
mappend = (<>)
2018-05-11 19:11:49 +03:00
2018-05-11 19:12:05 +03:00
instance Ord vertex => Ord (Graph vertex) where
2018-05-11 19:11:49 +03:00
compare (Graph G.Empty) (Graph G.Empty) = EQ
compare (Graph G.Empty) _ = LT
compare _ (Graph G.Empty) = GT
compare (Graph (G.Vertex a)) (Graph (G.Vertex b)) = compare a b
compare (Graph (G.Vertex _)) _ = LT
compare _ (Graph (G.Vertex _)) = GT
compare (Graph (G.Overlay a1 a2)) (Graph (G.Overlay b1 b2)) = (compare `on` Graph) a1 b1 <> (compare `on` Graph) a2 b2
compare (Graph (G.Overlay _ _)) _ = LT
compare _ (Graph (G.Overlay _ _)) = GT
compare (Graph (G.Connect a1 a2)) (Graph (G.Connect b1 b2)) = (compare `on` Graph) a1 b1 <> (compare `on` Graph) a2 b2