blob: 6459591ec20d1339b214d7e455f45a9b6a766143 (
plain)
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
|
module ProofGraph (
Node(..),
Graph(..),
empty,
insert,
delete,
union
) where
import Data.Set( Set )
import qualified Data.Set as Set
import qualified Data.List as List
data Node a = Node { contents :: a
, successors :: Set (Node a) } deriving (Eq, Ord)
data Graph a = Graph { nodes :: Set (Node a) }
instance (Show a, Eq a) => Show (Node a) where
show x = let header = "ID: " ++ (show . contents $ x)
footer = if ((successors x) == Set.empty)
then "\n"
else " -> " ++ List.intercalate ", " (map (show . contents) (Set.toList . successors $ x)) ++ "\n"
in header ++ footer
instance (Show a, Eq a) => Show (Graph a) where
show x = "Graph: \n" ++ List.foldl' (++) [] (map show (Set.toList . nodes $ x)) ++ "\n"
empty :: Graph a
empty = Graph Set.empty
insert :: Ord a => Node a -> Graph a -> Graph a
insert node graph = Graph (Set.insert node (nodes graph))
delete :: Ord a => Node a -> Graph a -> Graph a
delete node graph = Graph (Set.delete node (nodes graph))
union :: Ord a => Set (Node a) -> Graph a -> Graph a
union nodeSet graph = Graph (Set.union nodeSet (nodes graph))
|