skeletal-set (empty) → 0.1.0.0
raw patch · 10 files changed
+780/−0 lines, 10 filesdep +basedep +containersdep +mtlsetup-changed
Dependencies added: base, containers, mtl, skeletal-set, smallcheck, tasty, tasty-discover, tasty-hunit, tasty-quickcheck, tasty-smallcheck
Files
- LICENSE +30/−0
- README.md +49/−0
- Setup.hs +2/−0
- changelog.md +12/−0
- skeletal-set.cabal +60/−0
- src/Data/SkeletalSet.hs +342/−0
- src/Data/SkeletalSet/Equivalence.hs +17/−0
- src/Data/SkeletalSet/Types.hs +17/−0
- tests/Data/SkeletalSetTest.hs +250/−0
- tests/RunTests.hs +1/−0
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2017, Global Access Internet Services GmbH++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of Pavlo Kerestey nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ README.md view
@@ -0,0 +1,49 @@+# Skeletal Set++Skeletal set is a set equipped with an equivalence relation. It is a useful data structure in cases where equivalence is chosen not to be equality. One can use it to influence the memberships of the elements more strictly than in sets, and run computations when conflicts between elements are found.++For more in-depth explanations and examples, please have a look at the+documentation on hackage.++### Copyright:+* [Global Access Internet Services GmbH](http://www.global.de)++### Authors:+* [Pavlo Kerestey](https://github.com/ptek)+* [Simon Zelazny](https://github.com/pzel)+* [Irek Jozwiak](https://github.com/irekjozwiak) - Author of the predecessor+ implementation of sets with stricter guarantees and the idea of skeletal set written in Haskell.++## License++```text+ Copyright (c) 2017, Global Access Internet Services GmbH+ + All rights reserved.+ + Redistribution and use in source and binary forms, with or without+ modification, are permitted provided that the following conditions are met:+ + * Redistributions of source code must retain the above copyright notice,+ this list of conditions and the following disclaimer.+ + * Redistributions in binary form must reproduce the above copyright+ notice, this list of conditions and the following disclaimer in the+ documentation and/or other materials provided with the distribution.+ + * Neither the name of Global Access Internet Services GmbH nor the names+ of other contributors may be used to endorse or promote products+ derived from this software without specific prior written permission.+ + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE+ ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE+ LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR+ CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF+ SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS+ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN+ CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)+ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE+ POSSIBILITY OF SUCH DAMAGE.+```
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ changelog.md view
@@ -0,0 +1,12 @@+# Revision history for skeletal-set++## 0.2.0.0 -- 2018-04-24++* Rename Setoid to Skeletal Set to better represent what this+ data structure is actually is. What we were thinking to be a setoid+ turnd out to be wrong, as we do not implement the setoid in a general+ sense of its definition.++## 0.1.0.0 -- 2017-01-17++* Initial extract of Setoid into a standalone library.
+ skeletal-set.cabal view
@@ -0,0 +1,60 @@+-- This file has been generated from package.yaml by hpack version 0.20.0.+--+-- see: https://github.com/sol/hpack+--+-- hash: cece4f190ab5fb3989e1444ed726ce512791d2156618fb401c66949ebc717d36++name: skeletal-set+version: 0.1.0.0+synopsis: Skeletal set - a set with equivalence relation different from equality+description: Skeletal Set is a set equipped with an equivalence relation. It is a useful data structure in cases where equivalence is chosen not to be equality. One can use it to influence the memberships of the elements more strictly than in sets, and run computations when conflicts between elements are found.+ You can find more deatails in `Data.SkeletalSet`+category: Data+author: Pavlo Kerestey+maintainer: kerestey@global.de+copyright: Global Access Internet Services GmbH+license: BSD3+license-file: LICENSE+build-type: Simple+cabal-version: >= 1.10++extra-source-files:+ changelog.md+ README.md++library+ hs-source-dirs:+ src+ ghc-options: -Wall+ build-depends:+ base >=4.7 && <5+ , containers >=0.5 && <0.6+ exposed-modules:+ Data.SkeletalSet+ Data.SkeletalSet.Equivalence+ Data.SkeletalSet.Types+ other-modules:+ Paths_skeletal_set+ default-language: Haskell2010++test-suite skeletal-set-test+ type: exitcode-stdio-1.0+ main-is: RunTests.hs+ hs-source-dirs:+ tests+ ghc-options: -Wall -threaded -rtsopts -with-rtsopts=-N+ build-depends:+ base >=4.7 && <5+ , containers >=0.5 && <0.6+ , mtl+ , skeletal-set+ , smallcheck+ , tasty+ , tasty-discover+ , tasty-hunit+ , tasty-quickcheck+ , tasty-smallcheck+ other-modules:+ Data.SkeletalSetTest+ Paths_skeletal_set+ default-language: Haskell2010
+ src/Data/SkeletalSet.hs view
@@ -0,0 +1,342 @@+{-# LANGUAGE MultiParamTypeClasses #-}+{-# OPTIONS_GHC -fno-warn-orphans #-}++--------------------------------------------------------------------+{- |+Module : Data.SkeletalSet+Copyright : (c) Global Access Internet Services GmbH+License : BSD3+Maintainer: Pavlo Kerestey <pavlo@kerestey.net>++A Haskell implementation of skeletal set - a set equipped with an +equivalence relation. SkeletalSet is a useful data structure when+equivalence is chosen not to be equality. This allows to influence the+membership of the elements in a set.++Here we have chosen to use a specific variant of equivalence of+transforming the elements to comparable intermediaries. Although it+does not make every equivalence relation possible, it is a practical+choice for a lot of computations.++== Usage++When manipulating collections of objects in the real world, we often+use lists/arrays. Sometimes we need to represent some properties of+the relation between the elements though, and the lists do not provide+such possibility. This library not only provides the guarantee that a+skeletal set is correct by construction, but also that the manipulations+will not change its structure.++We use it to run computations over time series of sampling data,+collections of users (who are unique by username or email) - to keep+the same structure as the one which would be used in the database with+unique indexes.++To implement equivalence we chose to use a data class `EquivalenceBy`+which provides a method of mapping an element to an intermediary,+which is then used for comparison and ultimately lead to a choice+of the members.++The type is `SkeletalSet e a` where `a` is the member type and+e is the type of equivalence intermediary. To chose the members of the+skeletal set we compare the e(quivalences) of the elements with each other.++The definition of `EquivalenceBy e a` is++@+class EquivalenceBy e a where+ eqRel :: a -> e+@++To give a simple example of how the library could be used we will+combine apples and oranges to a SkeletalSet of fruit names by colour. We+want one fruit per colour as a result and don't care if its apple or+an orange.++@+import Data.SkeletalSet (SkeletalSet)+import qualified Data.SkeletalSet as SkeletalSet++data Colour = Red | Green | Blue deriving (Eq,Ord)++instance EquivalenceBy Colour (Colour,String) where+ eqRel = fst++apples, organges, fruits :: SkeletalSet Int (Int,String)+apples = SkeletalSet.fromList [(Green,"golden delicious"), (Orange,"honeycrunch")]+oranges = SkeletalSet.fromList [(Orange,"seville"), (Red,"blood orange")]++fruits = apples `SkeletalSet.union` oranges+-- > [(Green,"golden delicious"), (Orange,"seville"), (Red,"blood orange")]+@++One can see the benefit of using a `SkeletalSet` instead of "Data.List"+because with the latter, we would have to use 'Data.List.nubBy' every+time the data is transformed.++When performing a `union`, our implementation would use `max` between+two equivalent elements to resolve the conflict. Bear in mind, that+the elements, though equivalent, might not be equal. In the example+above, ordering of @ "seville" @ is bigger than @ "golden delicious" @+thus @ ("Orange", "seville") @ is chosen in the result.++=== Friends of friends and computation on union++For another example, lets get all the users of two different services+F and G. We are not interested in the different details, but want the+instance of the users to be unique.++@+type Email = String+data User = User {+ email :: Email,+ contacts :: Int+ } deriving (Eq,Show)++instance EquivalenceBy Email User where+eqRel u = email u++usersF, usersG, allUsers :: SkeletalSet Email User+usersF <- getUsers F+usersG <- getUsers G++allUsers = SkeletalSet.unionWith mergeContactDetails usersF usersG++mergeContactDetails :: User -> User -> User+mergeContactDetails a b = User (email a) (contacts a + contacts b)+-- ... --+@++We assume that here are equivalent elements in both sets - in this+case they have the same email address. Thus we use `unionWith` to merge+the other details of the contact. Here, we could also do computations+and, for example, sum the number of friends/contacts from both+services.++Here is also one of the shortcomings of the+library. mergeContactDetails choses the email of the first+argument. Since in the context of unionWith, the emails of the first+and the second users are the same. It is not nice from the perspective+of the function itself though.++@ SkeletalSet.size allUsers @ Would give us the amount of all unique users+in both services together.++== Future Work++- There is an unproven hypothesis about a relation between skeletal sets and+ Quotient Sets. It seems, that a `SkeletalSet (a,b) (a,b,c)` is equivalent+ to a `QuotientSet a (SkeletalSet b (a,b,c))`. This means that every+ QuotientSet can actually be represented as a skeletal set.++- Performance is another issue. Current implementation uses the+ `newtype SkeletalSet x y = SkeletalSet (Map x y)` which may be inefficient.+++-}+--------------------------------------------------------------------+module Data.SkeletalSet+ ( -- * Type+ SkeletalSet+ -- * Class+ , EquivalenceBy(..)+ -- * Operators+ , (=~=)+ , (\\)+ , (∪)+ -- * Construction+ , empty+ , ø+ , singleton+ , union+ , unions+ , unionWith+ -- * Difference+ , difference+ -- * Filter+ , filter+ -- * Query+ , null+ , size+ , member+ , equivalence+ -- * Traversal+ -- ** map+ , map+ , mapResolve+ , mapM+ -- * Conversion+ , fromList+ , fromListWith+ , toList+ ) where++import qualified Data.List as List+import qualified Data.Map.Strict as Map+import Data.SkeletalSet.Equivalence+import Data.SkeletalSet.Types+import Prelude hiding (filter, lookup, map, mapM,+ mapM_, null, zip)+import qualified Prelude as P++-- | Instance Show, used for debugging+instance (Show a) =>+ Show (SkeletalSet e a) where+ show s = "{{ " ++ P.unlines (P.map show (toList s)) ++ " }}"++-- | Monoid instance for SkeletalSet+instance (Ord e, Ord a) =>+ Monoid (SkeletalSet e a) where+ mempty = empty+ mappend = union+ mconcat = unions++-- * Operators+infix 4 =~=++-- | Same as `equivalence`+(=~=)+ :: (Eq e)+ => SkeletalSet e a -> SkeletalSet e a -> Bool+(=~=) = equivalence++infix 5 \\++-- | Same as `difference`+(\\)+ :: (Ord e)+ => SkeletalSet e a -> SkeletalSet e a -> SkeletalSet e a+(\\) = difference++-- | Same as `union`+(∪)+ :: (Ord e, Ord a)+ => SkeletalSet e a -> SkeletalSet e a -> SkeletalSet e a+(∪) = union++-- * Construction+-- | An empty SkeletalSet+empty :: SkeletalSet e a+empty = SkeletalSet Map.empty++-- | Same as `empty`+ø :: SkeletalSet e a+ø = empty++-- | A SkeletalSet with a single element+singleton+ :: (EquivalenceBy e a)+ => a -> SkeletalSet e a+singleton a = SkeletalSet (Map.singleton (eqRel a) a)++-- ** Combining+-- | Combine two SkeletalSets resolving conflicts with `max` by+-- default. This makes the union operation commutative and+-- associative.+union+ :: (Ord e, Ord a)+ => SkeletalSet e a -> SkeletalSet e a -> SkeletalSet e a+union = unionWith max++-- | A generalized variant of union which accepts a function that will+-- be used when two equivalent elements are found an the conflict+-- needs to be resolved. Note that the elements are not necessarily+-- equal+unionWith+ :: (Ord e)+ => (a -> a -> a) -> SkeletalSet e a -> SkeletalSet e a -> SkeletalSet e a+unionWith f (SkeletalSet x1) (SkeletalSet x2) = SkeletalSet (Map.unionWith f x1 x2)++-- | Union several SkeletalSets into one. This uses de default union+-- variant+unions+ :: (Ord e, Ord a)+ => [SkeletalSet e a] -> SkeletalSet e a+unions = List.foldl' union empty++-- ** Difference+-- | Difference of two skeletal sets. Return elements of the first skeletal sets+-- not existing in the second set.+difference+ :: (Ord e)+ => SkeletalSet e a -> SkeletalSet e a -> SkeletalSet e a+difference (SkeletalSet x) (SkeletalSet y) = SkeletalSet (Map.difference x y)++-- ** Filter+-- | Filter a skeletal set. Return a skeletal set with elements that statisfy the+-- predicate+filter+ :: (Ord e)+ => (a -> Bool) -> SkeletalSet e a -> SkeletalSet e a+filter p (SkeletalSet s) = SkeletalSet (Map.filter p s)++-- * Query+-- | Test if SkeletalSet is empty+null :: SkeletalSet e a -> Bool+null (SkeletalSet x) = Map.null x++-- | Get the size of a skeletal set+size :: SkeletalSet e a -> Int+size (SkeletalSet x) = Map.size x++-- | Test if an element is a member of a skeletal set+member+ :: (EquivalenceBy e a, Ord e)+ => a -> SkeletalSet e a -> Bool+member e (SkeletalSet x) = Map.member (eqRel e) x++-- | Test if two SkeletalSets are equivalent i.e. if all the elements are+-- equivalent+equivalence+ :: (Eq e)+ => SkeletalSet e a -> SkeletalSet e a -> Bool+equivalence (SkeletalSet x) (SkeletalSet y) = Map.keys x == Map.keys y++-- * Traversal+-- | Map a function over elements of a skeletal set. It resolves conflict in+-- the result by chosing the maximum one+map+ :: (EquivalenceBy eb b, Ord eb, Ord b)+ => (a -> b) -> SkeletalSet ea a -> SkeletalSet eb b+map f a = fromList (P.map f (toList a))++-- | Generalized version of map, allowing to use custom function to+-- resolve a conflict if two equivalent elements are found in the+-- result+mapResolve+ :: (EquivalenceBy eb b, Ord eb)+ => (b -> b -> b) -- ^ conflict resolution function+ -> (a -> b) -- ^ map function+ -> SkeletalSet ea a -- ^ input+ -> SkeletalSet eb b -- ^ result+mapResolve r f a = fromListWith r (P.map f (toList a))++-- | Monadic variant of a map+mapM+ :: (Monad m, EquivalenceBy eb b, Ord eb, Ord b)+ => (a -> m b) -> SkeletalSet ea a -> m (SkeletalSet eb b)+mapM f xs = fromList <$> P.mapM f (toList xs)++-- * Conversion+-- ** Lists+-- | Convert skeletal set into a List+toList :: SkeletalSet e a -> [a]+toList (SkeletalSet a) = Map.elems a++-- | A default variant of fromList using `max` to resolve a conflict+-- if two equivalent elements are found. Therefore it depends on Ord+-- instance of the element+fromList+ :: (EquivalenceBy e a, Ord e, Ord a)+ => [a] -> SkeletalSet e a+fromList = fromListWith max++-- | A generalized version of fromList, which will use a supplied+-- funtion if two equivalent elements are found in the input list+fromListWith+ :: (EquivalenceBy e a, Ord e)+ => (a -> a -> a) -> [a] -> SkeletalSet e a+fromListWith f = SkeletalSet . Map.fromListWith f . P.map (\x -> (eqRel x, x))+-- O(n+(n*log n))+-- An implementation of List.foldl' (\a b -> union a (singleton b)) empty+-- would be O(n*2n)
+ src/Data/SkeletalSet/Equivalence.hs view
@@ -0,0 +1,17 @@+{-# LANGUAGE MultiParamTypeClasses #-}++--------------------------------------------------------------------------------+-- |+-- Module : Data.SkeletalSet.Equivalence+-- Copyright : (c) Global Access Internet Services GmbH 2017+-- License : BSD3+-- Maintainer : Pavlo Kerestey <pavlo@kerestey.net>+--------------------------------------------------------------------------------+module Data.SkeletalSet.Equivalence+ ( EquivalenceBy(..)+ ) where++-- | Equivalence class. It reduces the data to the part which is+-- then being tested for equality in a SkeletalSet.+class EquivalenceBy e a where+ eqRel :: a -> e
+ src/Data/SkeletalSet/Types.hs view
@@ -0,0 +1,17 @@+{-# LANGUAGE DeriveGeneric #-}++--------------------------------------------------------------------------------+-- |+-- Module : Data.SkeletalSet.Types+-- Copyright : (c) Global Access Internet Services GmbH 2017+-- License : BSD3+-- Maintainer : Pavlo Kerestey <pavlo@kerestey.net>+--------------------------------------------------------------------------------+module Data.SkeletalSet.Types where++import Data.Map.Strict (Map)+import GHC.Generics (Generic)++newtype SkeletalSet e a =+ SkeletalSet (Map e a)+ deriving (Eq, Ord, Generic)
+ tests/Data/SkeletalSetTest.hs view
@@ -0,0 +1,250 @@+{-# LANGUAGE FlexibleContexts #-}+{-# LANGUAGE FlexibleInstances #-}+{-# LANGUAGE MultiParamTypeClasses #-}+{-# LANGUAGE ScopedTypeVariables #-}+{-# LANGUAGE MonoLocalBinds #-}+{-# OPTIONS_GHC -fno-warn-orphans #-}++module Data.SkeletalSetTest where++import Control.Monad.Identity hiding (mapM)+import Data.SkeletalSet hiding (ø, (\\), (∪))+import Prelude hiding (map, mapM, filter, null)+import Test.SmallCheck.Series+import Test.Tasty+import Test.Tasty.HUnit+import Test.Tasty.SmallCheck++test_monoid_laws :: [TestTree]+test_monoid_laws =+ [ testProperty "associativity" (d3 (\a b c -> a <> (b <> c) == (a <> b) <> c))+ , testProperty "left identity" (d4 (\a -> a <> ø == a))+ , testProperty "right identity" (d4 (\a -> ø <> a == a))+ ]++test_construction :: [TestTree]+test_construction =+ [ testGroup "empty and singleton" $+ [ testCase "empty" (ø @?= ø)+ , testProperty "not empty" (\x -> st x /= ø)+ , testProperty "equal to itself" (\x -> st x == st x)+ , testProperty "different" (\x y -> x /= y ==> st x /= st y)+ , testProperty "similar" (\x y -> x == y ==> st x == st y)+ , testProperty+ "transitive"+ (d500+ (over+ similar3+ (\(x, y, z) -> (x == y) && (y == z) ==> st (x, x) == st (z, z))))+ ]+ , testGroup "general union laws" $+ [ testProperty "identity" (d3 (\a -> (a ∪ ø == a) && (ø ∪ a == a)))+ , testProperty "reflexivity" (d3 (\a -> a ∪ a == a))+ , testProperty "commutativity" (d3 (\a b -> a ∪ b == b ∪ a))+ , testProperty "associativity" (d3 (\a b c -> a ∪ (b ∪ c) == (a ∪ b) ∪ c))+ , testProperty "nonempty successor" (d3 (\a x -> a ∪ st x /= ø))+ ]+ , testGroup "unionWith" $+ [ testProperty+ "unionWith max behaves the same as union"+ (d3 (\a b -> unionWith max a b == a ∪ b))+ ]+ , testGroup "unions" $+ [ testCase "zero" (unions [ø] @?= ø)+ , testProperty "one" (\a -> unions [st a] == st a)+ , testProperty "many" (d3 (\a b -> unions (a ++ [b]) == unions a ∪ b))+ ]+ , testGroup "difference" $+ [ testProperty "identity" (d3 (\a -> (a \\ ø == a) && (ø \\ a == ø)))+ , testProperty+ "by definition"+ (d3+ (\a b ->+ all (\x -> (x `member` a) && not (x `member` b)) (toList (a \\ b))))+ ]+ , testGroup "filter" $+ [ testProperty "empty" (d3 (\a -> (filter (const True) a == (a :: TestSet))))+ , testProperty "empty" (d3 (\a -> (filter (const False) a == ø)))+ , testProperty "by definition" $+ let p = even . fst+ in d4 $ \(a :: TestSet) ->+ all (\x -> p x && (x `member` a)) (toList (filter p a))+ ]+ ]++test_queries :: [TestTree]+test_queries =+ [ testGroup "null" $+ [ testCase "empty" (null ø @?= True)+ , testProperty "singleton" (\a -> null (st a) == False)+ ]+ , testGroup "size" $+ [ testCase "empty" (size ø @?= 0)+ , testProperty "singleton" (\a -> size (st a) == 1)+ , testProperty+ "non emtpy"+ (d4 (\a -> size (a :: TestSet) == length (toList a)))+ ]+ , testGroup "member" $+ [ testProperty "empty" (\x -> member x ø == False)+ , testProperty "single element" (\x -> member x (st x) == True)+ , testProperty "many elements" (d4 (\a x -> member x (a ∪ st x) == True))+ , testProperty "many elements" (d4 (\a x -> member x (a \\ st x) == False))+ ]+ , testGroup "equivalence" $+ [ testCase "empty case" (ø =~= ø @?= True)+ , testProperty+ "empty never equivalent to nonempty"+ (\x -> not (ø =~= (st x)) && not (st x =~= ø))+ , testProperty "singleton case" (\x -> st x =~= st x)+ , testProperty+ "not equivalent if eqRel is not equal"+ (over+ different+ (\(x, y) ->+ (eqRel x :: Int) /= (eqRel y :: Int) ==> not (st x =~= st y)))+ , testProperty+ "equivalent if eqRel is equal"+ (over+ similar+ (\(x, y) -> (eqRel x :: Int) == (eqRel y :: Int) ==> st x =~= st y))+ ]+ ]++test_traversal :: [TestTree]+test_traversal =+ [ testGroup "map" $+ [ testProperty "∃ x ∈ a: ∀ y ∈ (map f a): f x == y" $+ let f (x, y) = (x * y, x + y)+ in (d4 $ forAll $ \(a :: TestSet) ->+ (`all` toList (map f a :: TestSet)) $ \y ->+ any (\x -> f x == y) (toList a))+ , testProperty "∀ x ∈ a: f x ∈ (map f a))" $+ let f (x, y) = (x * y, x + y)+ in (d4 $ forAll $ \(a :: TestSet) ->+ (`all` toList a) $ \x ->+ f x `member` (map f a :: TestSet))+ ]+ , testGroup "mapResolve" $+ [ testProperty "Is the same as map when chosing `max` as resolver" $+ let f (x,y) = (x*y, x+y)+ in d3 $ \(a :: TestSet) ->+ mapResolve max f a == (map f a :: TestSet)+ ]+ , testGroup "mapM" $+ [ testProperty "Results are equivalent to pure version. Note the ordering" $+ let f (x,y) = (x*y, x+y)+ in d3 $ \(a :: TestSet) ->+ mapM (return . f) a `mEqual` return (map f a)+ ]+ ]++test_conversion :: [TestTree]+test_conversion =+ [ testGroup "fromList" $+ [ testCase "zero" (fromList [] @?= ø)+ , testProperty "singleton" (\x -> fromList [x] =~= st x)+ , testProperty+ "via union"+ (d3 (\xs ys -> fromList (xs ++ ys) == fromList xs ∪ fromList ys))+ , testProperty+ "transitivity"+ (d3+ (\xs ys zs ->+ (fromList (xs ++ ys) ∪ fromList zs) =~=+ (fromList xs ∪ fromList (ys ++ zs))))+ ]+ , testGroup "fromListWith" $+ [ testProperty+ "fromListWith max == fromList"+ (d4+ (\xs -> fromListWith max xs == (fromList xs :: TestSet)))+ ]+ , testGroup "toList" $+ [testProperty "inverse to fromList" (d4 (toList `isInverseOf` fromList))]+ ]++instance EquivalenceBy k (k, v) where+ eqRel = fst++type TestSet = SkeletalSet Int (Int, Int)++st+ :: EquivalenceBy Int (Int, Int)+ => (Int, Int) -> SkeletalSet Int (Int, Int)+st = singleton++ø :: TestSet+ø = empty++(<>) :: TestSet -> TestSet -> TestSet+(<>) = mappend++(∪) :: TestSet -> TestSet -> TestSet+(∪) = union++(\\) :: TestSet -> TestSet -> TestSet+(\\) = difference++different :: Series m ((Int, Int), (Int, Int))+different =+ generate (\d -> [((x - 1, y), (x + 1, y)) | x <- [0 .. d], y <- [0 .. 10]])++similar :: Series m ((Int, Int), (Int, Int))+similar =+ generate+ (\d -> [((x, y), (x, z)) | x <- [0 .. d], y <- [0 .. 5], z <- [4 .. 9]])++similar3 :: Series m (Int, Int, Int)+similar3 = generate (\d -> [(k, k, k) | k <- [0 .. d]])++isInverseOf+ :: (TestSet -> a)+ -> (a -> TestSet)+ -> TestSet+ -> Bool+isInverseOf f g a = (g . f) a == a++instance (Monad m, Ord k, Ord v, Serial m v, EquivalenceBy k v) =>+ Serial m (SkeletalSet k v) where+ series = fromList <$> series++mEqual :: (Identity (TestSet))+ -> (Identity (TestSet))+ -> Bool+mEqual f g = runIdentity f == runIdentity g++d1+ :: Testable m a+ => a -> Property m+d1 = changeDepth (const 1)++d2+ :: Testable m a+ => a -> Property m+d2 = changeDepth (const 2)++d3+ :: Testable m a+ => a -> Property m+d3 = changeDepth (const 3)++d4+ :: Testable m a+ => a -> Property m+d4 = changeDepth (const 4)++d5+ :: Testable m a+ => a -> Property m+d5 = changeDepth (const 5)++d10+ :: Testable m a+ => a -> Property m+d10 = changeDepth (const 10)++d500+ :: Testable m a+ => a -> Property m+d500 = changeDepth (const 500)
+ tests/RunTests.hs view
@@ -0,0 +1,1 @@+{-# OPTIONS_GHC -F -pgmF tasty-discover #-}