-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFilterableDataRecordExample.hs
37 lines (28 loc) · 1.23 KB
/
FilterableDataRecordExample.hs
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
{-# LANGUAGE DeriveGeneric, FlexibleInstances #-}
import Control.Applicative
import Control.Monad.Identity
import FilterableDataRecord
import GHC.Generics
data Record f = Record { a :: f Bool , b :: f Int, c :: f String } deriving (Generic)
-- testing part
instance Show (Record Identity) where
show (Record (Identity a) (Identity b) (Identity c)) = "Record " ++ show a ++ " " ++ show b ++ " " ++ show c
instance Show (Record Predicate) where
show (Record a b c) = "Record (" ++ show a ++ ") (" ++ show b ++ ") (" ++ show c ++ ")"
x <≡> y = x $ Identity y
x <=> y = x $ makePredicate ("Only " ++ show y) $ (==) y
x <∑> y = x $ makePredicate ("OneOf " ++ show y) $ (`elem` y)
x <≠> y = x $ makePredicate ("Except " ++ show y) $ (/=) y
x <∀> () = x $ makePredicate ("Any") $ const True
v = [
Record <≡> True <≡> 1 <≡> "2",
Record <≡> True <≡> 2 <≡> "1",
Record <≡> False <≡> 3 <≡> "2",
Record <≡> True <≡> 4 <≡> "2"]
f = [
Record <=> True <=> 1 <∀> (),
Record <=> True <∀> () <=> "2",
Record <∀> () <∀> () <=> "2",
Record <∀> () <≠> 3 <∀> (),
Record <∀> () <∑>[2,3] <∀> ()]
main = mapM_ print $ (\f v -> (show f, show v, applyPredicate f v)) <$> f <*> v