File: monoidLaw.hs

package info (click to toggle)
haskell-sql-words 0.1.6.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 76 kB
  • sloc: haskell: 238; makefile: 5
file content (65 lines) | stat: -rw-r--r-- 1,583 bytes parent folder | download | duplicates (4)
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
{-# OPTIONS -fno-warn-orphans #-}

import Language.SQL.Keyword
  (Keyword (Sequence), DString, (<++>))

import Data.Monoid (Monoid, mempty, (<>))
import Data.String (fromString)
import Test.QuickCheck (Arbitrary (..), Testable)
import Test.QuickCheck.Simple (Test, qcTest, defaultMain)


prop :: Testable prop => String -> prop -> Test
prop = qcTest

leftId :: (Eq a, Monoid a) => a -> Bool
leftId a = mempty <> a == a

rightId :: (Eq a, Monoid a) => a -> Bool
rightId a = a <> mempty == a

assoc :: (Eq a, Monoid a) => a -> a -> a -> Bool
assoc a b c = (a <> b) <> c == a <> (b <> c)

dsLeftId :: DString -> Bool
dsLeftId =  leftId

dsRightId :: DString -> Bool
dsRightId =  rightId

dsAssoc :: DString -> DString -> DString -> Bool
dsAssoc =  assoc

instance Arbitrary DString where
  arbitrary = fmap read arbitrary

kwLeftId :: Keyword -> Bool
kwLeftId =  leftId

kwRightId :: Keyword -> Bool
kwRightId =  rightId

kwAssoc :: Keyword -> Keyword -> Keyword -> Bool
kwAssoc =  assoc

instance Arbitrary Keyword where
  arbitrary = fmap fromString arbitrary

concatCommutative :: DString -> DString -> Bool
concatCommutative x y =
  Sequence x <++> Sequence y
  ==
  Sequence (x <> y)

tests :: [Test]
tests =  [ prop "DString left Id"  dsLeftId
         , prop "DString right Id" dsRightId
         , prop "DString associativity" dsAssoc
         , prop "Keyword left Id"  kwLeftId
         , prop "Keyword right Id" kwRightId
         , prop "Keyword associativity" kwAssoc
         , prop "concat commutative" concatCommutative
         ]

main :: IO ()
main = defaultMain tests