Safe Haskell | None |
---|---|
Language | Haskell2010 |
extra promoted functions
Synopsis
- data Pure2 (t :: Type -> Type)
- data p <$ q
- data p <* q
- data p *> q
- data FMapFst
- data FMapSnd
- data Sequence
- data Traverse p q
- data Join
- data p <|> q
- data Extract
- data Duplicate
- data p $$ q
- data q $& p
- data Skip p
- data p |> q
- data p >| q
- data p >|> q
- data HeadDef p q
- data HeadFail msg q
- data TailDef p q
- data TailFail msg q
- data LastDef p q
- data LastFail msg q
- data InitDef p q
- data InitFail msg q
- data Coerce2 (t :: k)
- data ProxyT (t :: Type)
- data ProxyT' t
- data Prime p
- data PrimeNext p
- data Luhn p
- data Catch p q
- data Catch' p s
- data Dot (ps :: [Type -> Type]) (q :: Type)
- data RDot (ps :: [Type -> Type]) (q :: Type)
- data K (p :: k) (q :: k1)
Documentation
data Pure2 (t :: Type -> Type) Source #
lift pure over a Functor
>>>
pz @(Pure2 (Either String)) [1,2,4]
PresentT [Right 1,Right 2,Right 4]
>>>
pl @(Pure2 []) (Just 10)
Present Just [10] (Pure2 Just [10] | Just 10) PresentT (Just [10])
>>>
pl @(Pure2 SG.Sum) (Just 20)
Present Just (Sum {getSum = 20}) (Pure2 Just (Sum {getSum = 20}) | Just 20) PresentT (Just (Sum {getSum = 20}))
similar to <$
>>>
pz @(Fst Id <$ Snd Id) ("abc",Just 20)
PresentT (Just "abc")
>>>
pl @(Fst Id <$ Snd Id) (4,These "xxx" 'a')
Present These "xxx" 4 ((<$) 4) PresentT (These "xxx" 4)
>>>
pl @(Fst Id <$ Snd Id) (4,This 'a')
Present This 'a' ((<$) 4) PresentT (This 'a')
>>>
pl @(Fst Id <$ Snd Id) (4,Just 'a')
Present Just 4 ((<$) 4) PresentT (Just 4)
>>>
pl @(Fst Id <$ Snd Id) (4,Nothing @Int)
Present Nothing ((<$) 4) PresentT Nothing
>>>
pl @('True <$ Id) [1..4]
Present [True,True,True,True] ((<$) True) PresentT [True,True,True,True]
>>>
import Data.Functor.Compose
>>>
pl @(Char1 "ab" <$ Id) (Compose $ Just [1..4])
Present Compose (Just "aaaa") ((<$) 'a') PresentT (Compose (Just "aaaa"))
>>>
pl @(Snd Id <$ Fst Id) (Just 10,'x')
Present Just 'x' ((<$) 'x') PresentT (Just 'x')
similar to Applicative <*
>>>
pl @(Fst Id <* Snd Id) (Just 4,Just 'a')
Present Just 4 ((<*) Just 4 | p=Just 4 | q=Just 'a') PresentT (Just 4)
>>>
pz @(Fst Id <* Snd Id) (Just "abc",Just 20)
PresentT (Just "abc")
similar to Applicative *>
>>>
pl @(Fst Id *> Snd Id) (Just 4,Just 'a')
Present Just 'a' ((<*) Just 'a' | p=Just 'a' | q=Just 4) PresentT (Just 'a')
similar to fmap fst
>>>
pz @FMapFst (Just (13,"Asf"))
PresentT (Just 13)
>>>
pl @FMapFst (Just (1,'x'))
Present Just 1 (FMapFst) PresentT (Just 1)
>>>
pl @FMapFst [(1,'x'), (2,'y'), (3,'z')]
Present [1,2,3] (FMapFst) PresentT [1,2,3]
similar to fmap snd
>>>
pz @FMapSnd (Just ("asf",13))
PresentT (Just 13)
>>>
pl @FMapSnd (Just (1,'x'))
Present Just 'x' (FMapSnd) PresentT (Just 'x')
>>>
pl @FMapSnd (Nothing @(Char,Int))
Present Nothing (FMapSnd) PresentT Nothing
>>>
pl @FMapSnd (Right (1,'x'))
Present Right 'x' (FMapSnd) PresentT (Right 'x')
>>>
pl @FMapSnd (Left @_ @(Int,Double) "x")
Present Left "x" (FMapSnd) PresentT (Left "x")
similar to sequenceA
>>>
pz @Sequence [Just 10, Just 20, Just 30]
PresentT (Just [10,20,30])
>>>
pz @Sequence [Just 10, Just 20, Just 30, Nothing, Just 40]
PresentT Nothing
Instances
(Show (f (t a)), Show (t (f a)), Traversable t, Applicative f) => P Sequence (t (f a)) Source # | |
type PP Sequence (t (f a)) Source # | |
Defined in Predicate.Data.Extra |
like traverse
>>>
pl @(Traverse (If (Gt 3) (Pure Maybe Id) (EmptyT Maybe Id)) Id) [1..5]
Present Nothing ((>>) Nothing | {Sequence Nothing | [Nothing,Nothing,Nothing,Just 4,Just 5]}) PresentT Nothing
>>>
pl @(Traverse (MaybeBool (Le 3) Id) Id) [1..5]
Present Nothing ((>>) Nothing | {Sequence Nothing | [Just 1,Just 2,Just 3,Nothing,Nothing]}) PresentT Nothing
>>>
pl @(Traverse (If (Gt 0) (Pure Maybe Id) (EmptyT Maybe Id)) Id) [1..5]
Present Just [1,2,3,4,5] ((>>) Just [1,2,3,4,5] | {Sequence Just [1,2,3,4,5] | [Just 1,Just 2,Just 3,Just 4,Just 5]}) PresentT (Just [1,2,3,4,5])
>>>
pl @(Traverse (If (Gt 0) (Pure Maybe Id) (MkNothing _)) Id) [1..5]
Present Just [1,2,3,4,5] ((>>) Just [1,2,3,4,5] | {Sequence Just [1,2,3,4,5] | [Just 1,Just 2,Just 3,Just 4,Just 5]}) PresentT (Just [1,2,3,4,5])
>>>
pl @(Traverse (MaybeBool (Id >= 0) Id) Id) [1..5]
Present Just [1,2,3,4,5] ((>>) Just [1,2,3,4,5] | {Sequence Just [1,2,3,4,5] | [Just 1,Just 2,Just 3,Just 4,Just 5]}) PresentT (Just [1,2,3,4,5])
>>>
pl @(Traverse (MaybeBool (Id <= 3) Id) Id) [1..5]
Present Nothing ((>>) Nothing | {Sequence Nothing | [Just 1,Just 2,Just 3,Nothing,Nothing]}) PresentT Nothing
similar to join
>>>
pz @Join (Just (Just 20))
PresentT (Just 20)
>>>
pz @Join ["ab","cd","","ef"]
PresentT "abcdef"
data p <|> q infixl 3 Source #
similar to <|>
>>>
pz @(Fst Id <|> Snd Id) (Nothing,Just 20)
PresentT (Just 20)
>>>
pz @(Fst Id <|> Snd Id) (Just 10,Just 20)
PresentT (Just 10)
>>>
pz @(Fst Id <|> Snd Id) (Nothing,Nothing)
PresentT Nothing
>>>
pl @(Fst Id <|> Snd Id) (Just "cdef",Just "ab")
Present Just "cdef" ((<|>) Just "cdef" | p=Just "cdef" | q=Just "ab") PresentT (Just "cdef")
>>>
pl @(Fst Id <|> Snd Id) ("cdef","ab"::String)
Present "cdefab" ((<|>) "cdefab" | p="cdef" | q="ab") PresentT "cdefab"
similar to extract
>>>
pz @Extract (Nothing,Just 20)
PresentT (Just 20)
>>>
pz @Extract (Identity 20)
PresentT 20
>>>
pl @Extract (10,"hello")
Present "hello" (Extract "hello" | (10,"hello")) PresentT "hello"
similar to duplicate
>>>
pz @Duplicate (20,"abc")
PresentT (20,(20,"abc"))
function application for expressions: similar to $
>>>
:m + Text.Show.Functions
>>>
pz @(Fst Id $$ Snd Id) ((*16),4)
PresentT 64
>>>
pz @(Id $$ "def") ("abc"<>)
PresentT "abcdef"
flipped function application for expressions: similar to &
>>>
:m + Text.Show.Functions
>>>
pz @(Snd Id $& Fst Id) ((*16),4)
PresentT 64
>>>
pz @("def" $& Id) ("abc"<>)
PresentT "abcdef"
just run the effect ignoring the result passing the original value through for example for use with Stdout so it doesnt interfere with the 'a' on the rhs unless there is an failure
run 'p' for the effect and then run 'q' using that original value
run run 'p' and then 'q' for the effect but using the result from 'p'
data p >|> q infixr 1 Source #
run both 'p' and 'q' for their effects but ignoring the results
takes the head of a list-like object or uses the given default value
see ConsT
for other supported types eg Seq
>>>
pz @(HeadDef 444 Id) []
PresentT 444
>>>
pz @(HeadDef 444 Id) [1..5]
PresentT 1
>>>
pz @(HeadDef 444 Id) [1..5]
PresentT 1
>>>
pz @(HeadDef (Char1 "w") Id) (Seq.fromList "abcdef")
PresentT 'a'
>>>
pz @(HeadDef (Char1 "w") Id) Seq.empty
PresentT 'w'
>>>
pz @(HeadDef (MEmptyT _) Id) ([] :: [SG.Sum Int])
PresentT (Sum {getSum = 0})
>>>
pz @(HeadDef (MEmptyT String) '["abc","def","asdfadf"]) ()
PresentT "abc"
>>>
pz @(HeadDef (MEmptyT _) (Snd Id)) (123,["abc","def","asdfadf"])
PresentT "abc"
>>>
pz @(HeadDef (MEmptyT _) (Snd Id)) (123,[])
PresentT ()
>>>
pl @(HeadDef 9 (Fst Id)) ([],True)
Present 9 (JustDef Nothing) PresentT 9
>>>
pl @(HeadDef 9 (Fst Id)) ([1..5],True)
Present 1 (JustDef Just) PresentT 1
>>>
pl @(HeadDef 3 (Fst Id)) ([10..15],True)
Present 10 (JustDef Just) PresentT 10
>>>
pl @(HeadDef 12 (Fst Id) >> Le 6) ([],True)
False ((>>) False | {12 <= 6}) FalseT
>>>
pl @(HeadDef 1 (Fst Id) >> Le 6) ([],True)
True ((>>) True | {1 <= 6}) TrueT
>>>
pl @(HeadDef 10 (Fst Id) >> Le 6) ([],True)
False ((>>) False | {10 <= 6}) FalseT
>>>
pl @(HeadDef (MEmptyT _) Id) (map (:[]) ([] :: [Int]))
Present [] (JustDef Nothing) PresentT []
>>>
pl @(HeadDef (MEmptyT _) Id) (map (:[]) ([10..14] :: [Int]))
Present [10] (JustDef Just) PresentT [10]
>>>
pl @(HeadDef (Fst Id) (Snd Id)) (99,[10..14])
Present 10 (JustDef Just) PresentT 10
>>>
pl @(HeadDef (Fst Id) (Snd Id)) (99,[] :: [Int])
Present 99 (JustDef Nothing) PresentT 99
>>>
pl @(HeadDef 43 (Snd Id)) (99,[] :: [Int])
Present 43 (JustDef Nothing) PresentT 43
takes the head of a list or fails with the given message
see ConsT
for other supported types eg Seq
>>>
pz @(HeadFail "dude" Id) ["abc","def","asdfadf"]
PresentT "abc"
>>>
pz @(HeadFail "empty list" Id) []
FailT "empty list"
>>>
pl @(HeadFail "zz" (Fst Id) >> Le 6) ([],True)
Error zz ((>>) lhs failed) FailT "zz"
>>>
pl @((HeadFail "failed1" (Fst Id) >> Le 6) || 'False) ([],True)
Error failed1 (||) FailT "failed1"
>>>
pl @((Fst Id >> HeadFail "failed2" Id >> Le (6 -% 1)) || 'False) ([-9],True)
True (True || False) TrueT
>>>
pl @(HeadFail "Asdf" Id) ([] :: [()]) -- breaks otherwise
Error Asdf (JustFail Nothing) FailT "Asdf"
>>>
pl @(HeadFail (PrintF "msg=%s def" (Fst Id)) (Snd Id)) ("Abc" :: String,[]::[Int])
Error msg=Abc def (JustFail Nothing) FailT "msg=Abc def"
takes the tail of a list-like object or uses the given default value
>>>
pl @(TailDef '[9,7] (Fst Id)) ([],True)
Present [9,7] (JustDef Nothing) PresentT [9,7]
>>>
pl @(TailDef '[9,7] (Fst Id)) ([1..5],True)
Present [2,3,4,5] (JustDef Just) PresentT [2,3,4,5]
>>>
pl @(TailDef '[3] (Fst Id)) ([10..15],True)
Present [11,12,13,14,15] (JustDef Just) PresentT [11,12,13,14,15]
takes the tail of a list-like object or fails with the given message
>>>
pl @(TailFail (PrintT "a=%d b=%s" (Snd Id)) (Fst Id)) ([]::[()],(4::Int,"someval" :: String))
Error a=4 b=someval (JustFail Nothing) FailT "a=4 b=someval"
takes the last value of a list-like object or a default value
>>>
pl @(LastDef 9 (Fst Id)) ([],True)
Present 9 (JustDef Nothing) PresentT 9
>>>
pl @(LastDef 9 (Fst Id)) ([1..5],True)
Present 5 (JustDef Just) PresentT 5
>>>
pl @(LastDef 3 (Fst Id)) ([10..15],True)
Present 15 (JustDef Just) PresentT 15
>>>
pl @(LastDef 0 Id) [1..12]
Present 12 (JustDef Just) PresentT 12
>>>
pl @(LastDef 0 Id) []
Present 0 (JustDef Nothing) PresentT 0
takes the init of a list-like object or fails with the given message
takes the init of a list-like object or uses the given default value
>>>
pl @(InitDef '[9,7] (Fst Id)) ([],True)
Present [9,7] (JustDef Nothing) PresentT [9,7]
>>>
pl @(InitDef '[9,7] (Fst Id)) ([1..5],True)
Present [1,2,3,4] (JustDef Just) PresentT [1,2,3,4]
>>>
pl @(InitDef '[3] (Fst Id)) ([10..15],True)
Present [10,11,12,13,14] (JustDef Just) PresentT [10,11,12,13,14]
takes the init of a list-like object or fails with the given message
data Coerce2 (t :: k) Source #
coerce over a functor
>>>
pz @(Coerce2 (SG.Sum Integer)) [Identity (-13), Identity 4, Identity 99]
PresentT [Sum {getSum = -13},Sum {getSum = 4},Sum {getSum = 99}]
>>>
pz @(Coerce2 (SG.Sum Integer)) (Just (Identity (-13)))
PresentT (Just (Sum {getSum = -13}))
>>>
pz @(Coerce2 (SG.Sum Int)) (Nothing @(Identity Int))
PresentT Nothing
>>>
pl @(Coerce2 (SG.Sum Int)) (Just (10 :: Int))
Present Just (Sum {getSum = 10}) (Coerce2 Just (Sum {getSum = 10}) | Just 10) PresentT (Just (Sum {getSum = 10}))
a predicate on prime numbers
>>>
pz @(Prime Id) 2
TrueT
>>>
pz @(Map '(Id,Prime Id) Id) [0..12]
PresentT [(0,False),(1,False),(2,True),(3,True),(4,False),(5,True),(6,False),(7,True),(8,False),(9,False),(10,False),(11,True),(12,False)]
get the next prime number
>>>
pz @(PrimeNext Id) 6
PresentT 7
>>>
pz @(ScanN 4 (PrimeNext Id) Id) 3
PresentT [3,5,7,11,13]
Luhn predicate check on last digit
>>>
pz @(Luhn Id) [1,2,3,0]
TrueT
>>>
pz @(Luhn Id) [1,2,3,4]
FalseT
>>>
pz @(GuardSimple (Luhn Id)) [15,4,3,1,99]
FailT "(Luhn map=[90,2,3,8,6] sum=109 ret=9 | [15,4,3,1,99])"
>>>
pl @(Luhn Id) [15,4,3,1,99]
False (Luhn map=[90,2,3,8,6] sum=109 ret=9 | [15,4,3,1,99]) FalseT
run an expression 'p' and on failure run 'q'
>>>
pz @(Catch (Succ Id) (Fst Id >> Second (ShowP Id) >> PrintT "%s %s" Id >> 'LT)) GT
PresentT LT
>>>
pz @(Len > 1 && Catch (Id !! 3 == 66) 'False) [1,2]
FalseT
>>>
pl @(Catch (Resplit "\\d+(" Id) (Snd Id >> MEmptyP)) "123"
Present [] (Catch caught exception[Regex failed to compile]) PresentT []
>>>
pl @(Catch (OneP Id) 99) [10,11]
Present 99 (Catch caught exception[OneP 2 elements]) PresentT 99
>>>
pl @(Catch (OneP Id) 99) [10]
Present 10 (Catch did not fire) PresentT 10
>>>
pl @(Catch (OneP Id) 'True) [False] -- cant know that this is FalseT cos is driven by type of the list not the 'True part
Present False (Catch did not fire) PresentT False
>>>
pl @(Catch (OneP Id) 'False) [True,True,False]
False (Catch caught exception[OneP 3 elements]) FalseT
>>>
pl @(Catch (OneP Id) 'True) []
True (Catch caught exception[OneP empty]) TrueT
run an expression 'p' and on failure print a custom error 's' using the error string and the input value
>>>
pz @(Catch' (Succ Id) (Second (ShowP Id) >> PrintT "%s %s" Id)) GT
FailT "Succ IO e=Prelude.Enum.Ordering.succ: bad argument GT"
>>>
pz @(Catch' (Succ Id) (Second (ShowP Id) >> PrintT "%s %s" Id)) LT
PresentT EQ
>>>
pl @(Catch' (Failt Int "someval") (PrintT "msg=%s caught(%03d)" Id)) (44 :: Int)
Error msg=someval caught(044) (Catch default condition failed) FailT "msg=someval caught(044)"
>>>
pl @(Catch' (OneP Id) (Second (ShowP Id) >> PrintT "msg=%s caught(%s)" Id)) [10,12,13]
Error msg=OneP 3 elements caught([10,12,13]) (Catch default condition failed) FailT "msg=OneP 3 elements caught([10,12,13])"
>>>
pl @(Catch' (OneP Id) (PrintT "msg=%s caught(%s)" (Second (ShowP Id)))) [10]
Present 10 (Catch did not fire) PresentT 10
>>>
pl @(Catch' (OneP Id) (PrintT "msg=%s err s=%s" (Second (ShowP Id)))) [10,11]
Error msg=OneP 2 elements err s=[10,11] (Catch default condition failed) FailT "msg=OneP 2 elements err s=[10,11]"
data Dot (ps :: [Type -> Type]) (q :: Type) Source #
compose simple functions
>>>
pl @(Dot '[Thd,Snd,Fst] Id) ((1,(2,9,10)),(3,4))
Present 10 (Thd 10 | (2,9,10)) PresentT 10
data RDot (ps :: [Type -> Type]) (q :: Type) Source #
reversed version of Dot
>>>
pl @(RDot '[Fst,Snd,Thd] Id) ((1,(2,9,10)),(3,4))
Present 10 (Thd 10 | (2,9,10)) PresentT 10
>>>
pl @(RDot '[Fst,Snd] Id) (('a',2),(True,"zy"))
Present 2 (Snd 2 | ('a',2)) PresentT 2
data K (p :: k) (q :: k1) Source #
creates a constant expression ignoring the second argument
>>>
pl @(RDot '[Fst,Snd,Thd,K "xxx"] Id) ((1,(2,9,10)),(3,4))
Present "xxx" (K '"xxx") PresentT "xxx"
>>>
pl @(RDot '[Fst,Snd,Thd,K '("abc",Id)] Id) ((1,(2,9,10)),(3,4))
Present ("abc",((1,(2,9,10)),(3,4))) (K '("abc",((1,(2,9,10)),(3,4)))) PresentT ("abc",((1,(2,9,10)),(3,4)))
>>>
pl @(Thd $ Snd $ Fst $ K Id "dud") ((1,("W",9,'a')),(3,4))
Present 'a' (Thd 'a' | ("W",9,'a')) PresentT 'a'
>>>
pl @((Thd $ Snd $ Fst $ K Id "dud") >> Pred Id) ((1,("W",9,'a')),(3,4))
Present '`' ((>>) '`' | {Pred '`' | 'a'}) PresentT '`'