-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStringGroup.hs
More file actions
55 lines (43 loc) · 1.59 KB
/
StringGroup.hs
File metadata and controls
55 lines (43 loc) · 1.59 KB
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
{-# LANGUAGE TemplateHaskell #-}
module StringGroup where
import Test.QuickCheck
data SChar = P Char | N Char deriving (Eq, Ord, Show)
newtype SString = SString [SChar] deriving (Eq, Ord, Show)
instance Arbitrary SChar where
arbitrary = oneof [fmap P arbitrary, fmap N arbitrary]
instance Arbitrary SString where
arbitrary = fmap (<> mempty) $ sized $ fmap SString . vector
instance Semigroup SString where
(SString xs) <> (SString ys) = SString (reverse $ go zs [])
where
zs = xs <> ys
go [] acc = acc
go ((N x):xs) ((P y):ys)
| x == y = go xs ys
| otherwise = go xs (N x:P y:ys)
go ((P x):xs) ((N y):ys)
| x == y = go xs ys
| otherwise = go xs (P x:N y:ys)
go (x:xs) acc = go xs (x:acc)
instance Monoid SString where
mempty = SString []
positive = SString . map P
negateSChar (P x) = N x
negateSChar (N x) = P x
inverse (SString s) = SString $ reverse $ map negateSChar s
prop_associative :: SString -> SString -> SString -> Bool
prop_associative xs ys zs = (xs <> ys) <> zs == xs <> (ys <> zs)
prop_leftIdentity :: SString -> Bool
prop_leftIdentity xs = mempty <> xs == xs
prop_rightIdentity :: SString -> Bool
prop_rightIdentity xs = xs <> mempty == xs
prop_leftInverse xs = inverse xs <> xs == mempty
prop_rightInverse xs = xs <> inverse xs == mempty
return []
tests = $forAllProperties $
quickCheckWithResult (stdArgs {maxSuccess = 10000})
main = do
let x = positive "hello world!"
let y = inverse $ positive " world!"
print (x <> y)
tests