簡體   English   中英

在Haskell QuickCheck中測試“ DAG”又稱為非循環圖屬性

[英]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)

我正在嘗試使用QuickCheck測試“ isDAG”,能否給我一個“ isDAG”遵守的財產?

prop_isDAG :: Graph Int -> Property
prop_isDAG g = 

我相信這是准確的函數聲明,如果沒有,請隨時進行更改!


這是我為另一個功能制作的另一個示例,用於指導可能的助手尋找我要的內容:

-- | 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')

這是它遵循的QuickCheck屬性:

-- 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
prop_dag :: Property
prop_dag = forAll (dag :: Gen (DAG Int)) $ \g -> collect (length (edges g)) $ isDAG g

這就是我想要的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM