mercredi 27 avril 2016

Testing 'DAG' aka acyclic graphs properties in Haskell QuickCheck

module Graph where

import Control.Monad.State
import Data.Maybe
import Data.Set as Set

-- | 'Edge' represents an edge entre two nodes.
data Edge v = Edge {source :: v, target :: v}
              deriving (Show,Eq,Ord)

data Graph v = Graph {nodes :: Set v, edges :: Set (Edge v)}
               deriving Show

-- | 'Eq' for graphs.
instance Ord v => Eq (Graph v) where
    g == g' = nodes g == nodes g' && edges g == edges g'

isValid :: Ord v => Graph v -> Bool
isValid g = (Set.map source (edges g) `Set.union` Set.map target (edges g)) `isSubsetOf` nodes g

-- | 'isDAG' tests if a graph is acyclic
isDAG :: Ord v => Graph v -> Bool
isDAG g = isValid g && all nocycle (nodes g)
    where nocycle v = all (\a -> v `notMember` reachable g a) $ Set.map target (adj g v)

I'm trying to use QuickCheck to test 'isDAG', could you give me an property that 'isDAG' abides?

prop_isDAG :: Graph Int -> Property
prop_isDAG g = 

I believe that's the accurate function declaration, if not, feel free to change it!


Here is another example that I've made for another function, to guide possible helpers on what i'm looking for:

-- | The function 'isSubgraphOf' tests if the first graph is subgraph of the second.
isSubgraphOf :: Ord v => Graph v -> Graph v -> Bool
isSubgraphOf g g' = isSubsetOf (nodes g) (nodes g') && isSubsetOf (edges g) (edges g')

And this is the QuickCheck property that it abides:

-- QuickCheck proprety to test 'isSubgraphOf'
prop_isSubgraphOf :: Graph Int -> Property
prop_isSubgraphOf g = forAll (elements $ elems $ nodes g) $ \v -> adj g v `isSubsetOf` edges g

Aucun commentaire:

Enregistrer un commentaire