Safe Haskell | None |
---|---|
Language | Haskell2010 |
lifted promoted functions
Synopsis
- data FMap p
- data p <$> q
- data p <&> q
- data Pure (t :: Type -> Type) p
- data p <*> q
- data LiftA2 p q r
- data FPair p q
- data p <:> q
- data p <$ q
- data p <* q
- data p *> q
- data FFish amb bmc a
- data ma >>= amb
- data Sequence
- data Traverse p
- data Join
- data p <|> q
- data EmptyT (t :: Type -> Type) (t1 :: Type)
- data EmptyT' (t :: Type -> Type) p
- data EmptyList (t :: Type)
- data EmptyList' (t :: k)
- data EmptyBool t b p
- data EmptyBool' b p
- data BiMap p q
- data BiFoldMap 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 Flip (p :: k1 -> k2 -> k3) (q :: k2) (r :: k1)
- data Dot (ps :: [Type -> Type]) (q :: Type)
- data RDot (ps :: [Type -> Type]) (q :: Type)
- data K (p :: k) (q :: k1)
- data Lift p q
- data Coerce (t :: Type)
- data Catch p q
- data Catch' p s
- type family DotExpandT (ps :: [Type -> Type]) (q :: Type) :: Type where ...
- type family RDotExpandT (ps :: [Type -> Type]) (q :: Type) :: Type where ...
functor
similar to <$>
>>>
pl @(FMap Succ) (Right 'a')
Present Right 'b' (FMap Succ 'b' | 'a') Val (Right 'b')
>>>
pl @(FMap Succ) (Left "Sf")
Present Left "Sf" (FMap <skipped>) Val (Left "Sf")
>>>
pz @(FMap (MkDay Id) >> Join) (Just (2020,01,01))
Val (Just 2020-01-01)
>>>
pz @(FMap (MkDay Id) >> Join) (Just (2020,01,32))
Val Nothing
>>>
pz @(FMap Succ) (Just LT)
Val (Just EQ)
>>>
pz @(FMap Pred) (Just LT)
Fail "Pred IO e=Prelude.Enum.Ordering.pred: bad argument"
>>>
pz @(FMap (ShowP Id)) (Just 10)
Val (Just "10")
>>>
pan @(FMap $ FMap $ FMap Succ) [Just "abcdefG",Nothing,Just "X"]
P FMap FMap FMap Succ 'b' | Succ 'c' | Succ 'd' | Succ 'e' | Succ 'f' | Succ 'g' | Succ 'H' | FMap <skipped> | FMap FMap Succ 'Y' | +- P FMap FMap Succ 'b' | Succ 'c' | Succ 'd' | Succ 'e' | Succ 'f' | Succ 'g' | Succ 'H' | | | `- P FMap Succ 'b' | Succ 'c' | Succ 'd' | Succ 'e' | Succ 'f' | Succ 'g' | Succ 'H' | | | +- P Succ 'b' | | | +- P Succ 'c' | | | +- P Succ 'd' | | | +- P Succ 'e' | | | +- P Succ 'f' | | | +- P Succ 'g' | | | `- P Succ 'H' | +- P FMap <skipped> | `- P FMap FMap Succ 'Y' | `- P FMap Succ 'Y' | `- P Succ 'Y' Val [Just "bcdefgH",Nothing,Just "Y"]
>>>
pan @(FMap (FromEnum > 97)) "abc"
P FMap 97 > 97 | 98 > 97 | 99 > 97 | +- False 97 > 97 | | | +- P FromEnum 97 | | | `- P '97 | +- True 98 > 97 | | | +- P FromEnum 98 | | | `- P '97 | `- True 99 > 97 | +- P FromEnum 99 | `- P '97 Val [False,True,True]
>>>
pan @(FMap (FromEnum > 97 >> Id)) "abc"
P FMap (>>) False | (>>) True | (>>) True | +- P (>>) False | | | +- False 97 > 97 | | | | | +- P FromEnum 97 | | | | | `- P '97 | | | `- P Id False | +- P (>>) True | | | +- True 98 > 97 | | | | | +- P FromEnum 98 | | | | | `- P '97 | | | `- P Id True | `- P (>>) True | +- True 99 > 97 | | | +- P FromEnum 99 | | | `- P '97 | `- P Id True Val [False,True,True]
>>>
pan @(FMap IdBool) (Just True)
P FMap IdBool | `- True IdBool Val (Just True)
>>>
pz @(FMap (Pure (Either String) Id)) [1,2,4]
Val [Right 1,Right 2,Right 4]
>>>
pl @(FMap (Pure [] Id)) (Just 10)
Present Just [10] (FMap Pure [10] | 10) Val (Just [10])
>>>
pl @(FMap (Pure SG.Sum Id)) (Just 20)
Present Just (Sum {getSum = 20}) (FMap Pure Sum {getSum = 20} | 20) Val (Just (Sum {getSum = 20}))
>>>
pz @(FMap (Coerce (SG.Sum Integer))) [Identity (-13), Identity 4, Identity 99]
Val [Sum {getSum = -13},Sum {getSum = 4},Sum {getSum = 99}]
>>>
pz @(FMap (Coerce (SG.Sum Integer))) (Just (Identity (-13)))
Val (Just (Sum {getSum = -13}))
>>>
pz @(FMap (Coerce (SG.Sum Int))) (Nothing @(Identity Int))
Val Nothing
>>>
pl @(FMap (Coerce (SG.Sum Int))) (Just (10 :: Int))
Present Just (Sum {getSum = 10}) (FMap Coerce Sum {getSum = 10} | 10) Val (Just (Sum {getSum = 10}))
>>>
pz @(Proxy Char >> FMap Succ) () ^!? acts . _Val . to typeRep
Just Char
data p <$> q infixl 4 Source #
similar to <$>
>>>
pz @(Len <$> Snd) (1,Just "abcdef")
Val (Just 6)
>>>
pz @(Len <$> (Id <> Id <> "extra" <$> Snd)) (1,Just "abcdef")
Val (Just 17)
>>>
pz @(Len <$> (Id <> Id <> "extra" <$> Snd)) (1,Right "abcdef")
Val (Right 17)
>>>
pz @(FMap $ FMap (Succ <$> Id)) (True,Just (These 12 'c'))
Val (True,Just (These 12 'd'))
>>>
pz @(FMap (Second (Succ <$> Id))) [(True, (These 12 'c'))]
Val [(True,These 12 'd')]
data p <&> q infixl 1 Source #
similar to <&>
>>>
pz @('[1,2,3] <&> Succ) ()
Val [2,3,4]
applicative
data Pure (t :: Type -> Type) p Source #
similar to pure
>>>
pz @(Pure Maybe Id) 4
Val (Just 4)
>>>
pz @(Pure [] Id) 4
Val [4]
>>>
pz @(Pure (Either String) Fst) (13,True)
Val (Right 13)
>>>
pl @(Pure Maybe Id) 'x'
Present Just 'x' (Pure Just 'x' | 'x') Val (Just 'x')
>>>
pl @(Pure (Either _) Id) 'x'
Present Right 'x' (Pure Right 'x' | 'x') Val (Right 'x')
>>>
pl @(Pure (Either _) Id >> Swap) 'x'
Present Left 'x' ((>>) Left 'x' | {Swap Left 'x' | Right 'x'}) Val (Left 'x')
>>>
pl @(Pure (Either ()) Id >> Swap) 'x'
Present Left 'x' ((>>) Left 'x' | {Swap Left 'x' | Right 'x'}) Val (Left 'x')
>>>
pl @(Pure (Either String) Id >> Swap) 123
Present Left 123 ((>>) Left 123 | {Swap Left 123 | Right 123}) Val (Left 123)
data p <*> q infixl 1 Source #
applicative bind similar to <*>
but functions have to be fully saturated: ie Len is ok but not Length
can use Proxy to delay evaluation until Pop0
>>>
pz @(MkJust '("sdf",Id) <*> MkJust 4) ()
Val (Just ("sdf",4))
>>>
pz @(MkJust Succ <*> MkJust 4) ()
Val (Just 5)
>>>
pz @('[Succ,Id,Pred] <*> "abcdef") undefined
Val "ba`cbadcbedcfedgfe"
>>>
pz @(MkJust "abc" <*> MkJust "def") () -- no function to apply so has to choose ie first one
Val (Just "abc")
>>>
pz @('[1,2] <*> "abcdef") () -- [1,2] <* "abcdef" -- ie skips rhs "abcdef" but still runs the effects
Val [1,2,1,2,1,2,1,2,1,2,1,2]
>>>
pz @(MkJust ((*) 3 Id) <*> MkJust 4) ()
Val (Just 12)
>>>
pz @(MkJust ((*) 3 Len) <*> MkJust '["aa","bb","c","d","e"]) ()
Val (Just 15)
>>>
pz @(ShowP Id <$> MkJust Succ <*> MkJust 4) ()
Val (Just "5")
>>>
pz @('["x","y"] <*> '[1,2,3]) ()
Val ["x","y","x","y","x","y"]
similar to liftA2
>>>
pan @(LiftA2 Id (MkJust 12) (MkJust "abc")) ()
P LiftA2 Id (12,"abc") | +- P MkJust Just 12 | | | `- P '12 | +- P MkJust Just "abc" | | | `- P '"abc" | `- P Id (12,"abc") Val (Just (12,"abc"))
>>>
pan @(LiftA2 Swap (MkJust 12) (MkNothing _)) ()
P LiftA2 <skipped> | +- P MkJust Just 12 | | | `- P '12 | `- P MkNothing Val Nothing
>>>
pz @(LiftA2 (ShowP Fst <> "---" <> ShowP Snd) Fst Snd) (Just 10, Just True)
Val (Just "10---True")
>>>
pz @(LiftA2 (Fst + Snd) Fst Snd) (Just 10, Just 13)
Val (Just 23)
>>>
pz @(LiftA2 Fst '["x","y"] '[1,2,3]) ()
Val ["x","x","x","y","y","y"]
>>>
pz @(LiftA2 Snd '["x","y"] '[1,2,3]) ()
Val [1,2,3,1,2,3]
>>>
pz @(LiftA2 (Pop0 Fst Snd) '[ Proxy Len ] '[ "abc", "def", "aaaaaaaaaaa"]) ()
Val [3,3,11]
>>>
pz @(LiftA2 (Fst * Snd) (FromList (ZipList _) << (10...15)) (FromList (ZipList _) << (1...10))) ()
Val (ZipList {getZipList = [10,22,36,52,70,90]})
Instances
(Traversable n, Applicative n, P p (a, b), P q x, P r x, PP p (a, b) ~ c, PP q x ~ n a, PP r x ~ n b) => P (LiftA2 p q r :: Type) x Source # | |
Show (LiftA2 p q r) Source # | |
type PP (LiftA2 p q r :: Type) x Source # | |
Defined in Predicate.Data.Lifted type PP (LiftA2 p q r :: Type) x = ExtractTFromTA (PP q x) (PP p (ExtractAFromTA (PP q x), ExtractAFromTA (PP r x))) |
runs liftA2
(,) against two values: LiftA2 is traversable and provides better debugging
>>>
pz @(FPair Fst Snd) (Just 10, Just True)
Val (Just (10,True))
>>>
pz @(FPair Fst Snd >> FMap (ShowP Fst <> "---" <> ShowP Snd)) (Just 10, Just True)
Val (Just "10---True")
>>>
pz @(FPair Fst Snd >> FMap (Fst + Snd)) (Just 10, Just 13)
Val (Just 23)
>>>
pz @(FPair (EnumFromTo Fst Snd) ('LT ... 'GT)) (10,11)
Val [(10,LT),(10,EQ),(10,GT),(11,LT),(11,EQ),(11,GT)]
>>>
pz @(FPair '[ '() ] (1 ... 5)) True
Val [((),1),((),2),((),3),((),4),((),5)]
data p <:> q infixl 6 Source #
see FPair
>>>
pz @(Fst <:> Snd) (Just 10, Just True)
Val (Just (10,True))
>>>
pz @(Fst <:> Snd) ("abc",[10,12,14])
Val [('a',10),('a',12),('a',14),('b',10),('b',12),('b',14),('c',10),('c',12),('c',14)]
>>>
pz @('[1,2] <:> "abcdef") ()
Val [(1,'a'),(1,'b'),(1,'c'),(1,'d'),(1,'e'),(1,'f'),(2,'a'),(2,'b'),(2,'c'),(2,'d'),(2,'e'),(2,'f')]
>>>
pz @(EnumFromTo Fst Snd <:> ('LT ... 'GT)) (10,11)
Val [(10,LT),(10,EQ),(10,GT),(11,LT),(11,EQ),(11,GT)]
>>>
pz @(MkJust Succ <:> MkJust 4) () -- uses Succ on (): instead use LiftA2 with Pop0 or <*> (see next 2 tests)
Fail "Succ IO e=Prelude.Enum.().succ: bad argument"
>>>
pz @(LiftA2 (Pop0 Fst Snd) (MkJust (Proxy Succ)) (MkJust 4)) ()
Val (Just 5)
>>>
pz @(MkJust Succ <*> MkJust 4) ()
Val (Just 5)
similar to <$
>>>
pz @(Fst <$ Snd) ("abc",Just 20)
Val (Just "abc")
>>>
pl @(Fst <$ Snd) (4,These "xxx" 'a')
Present These "xxx" 4 ((<$) 4) Val (These "xxx" 4)
>>>
pl @(Fst <$ Snd) (4,This 'a')
Present This 'a' ((<$) 4) Val (This 'a')
>>>
pl @(Fst <$ Snd) (4,Just 'a')
Present Just 4 ((<$) 4) Val (Just 4)
>>>
pl @(Fst <$ Snd) (4,Nothing @Int)
Present Nothing ((<$) 4) Val Nothing
>>>
pl @('True <$ Id) [1..4]
Present [True,True,True,True] ((<$) True) Val [True,True,True,True]
>>>
import Data.Functor.Compose
>>>
pl @(C "ab" <$ Id) (Compose $ Just [1..4])
Present Compose (Just "aaaa") ((<$) 'a') Val (Compose (Just "aaaa"))
>>>
pl @(Snd <$ Fst) (Just 10,'x')
Present Just 'x' ((<$) 'x') Val (Just 'x')
similar to Applicative <*
>>>
pl @(Fst <* Snd) (Just 4,Just 'a')
Present Just 4 ((<*) Just 4 | p=Just 4 | q=Just 'a') Val (Just 4)
>>>
pz @(Fst <* Snd) (Just "abc",Just 20)
Val (Just "abc")
>>>
pz @('["x","y"] <* '[1,2,3]) ()
Val ["x","x","x","y","y","y"]
similar to Applicative *>
>>>
pl @(Fst *> Snd) (Just 4,Just 'a')
Present Just 'a' ((*>) Just 4 | p=Just 4 | q=Just 'a') Val (Just 'a')
>>>
pz @('["x","y"] *> '[1,2,3]) ()
Val [1,2,3,1,2,3]
monad
similar to monad operator >=>
>>>
pz @(FFish Uncons (Snd >> Uncons) "abcdef") ()
Val (Just ('b',"cdef"))
>>>
:m + Data.Time
>>>
pz @(FFish (ReadMaybe Day Id >> FMap Fst) (MkJust Succ) "2020-02-02") ()
Val (Just 2020-02-03)
>>>
pz @(FFish Uncons (Lookup Fst "abcdef") [3,14,12]) ()
Val (Just 'd')
data ma >>= amb infixl 1 Source #
similar to monad bind operator >>=
>>>
pz @(Id >>= HeadMay) (Just "abcdef")
Val (Just 'a')
>>>
pz @(Uncons >>= (Snd >> HeadMay)) "abcdef"
Val (Just 'b')
>>>
pz @((1 ... 10) >>= EmptyBool [] Even '[Id,Id]) ()
Val [[2,2],[4,4],[6,6],[8,8],[10,10]]
>>>
pz @((1 ... 10) >>= If Even '[Id,Id] (EmptyT [] _)) ()
Val [2,2,4,4,6,6,8,8,10,10]
>>>
pz @(Lookup 0 Id >>= Lookup 1 Id) [[1,2,3]]
Val (Just 2)
>>>
pz @(Lookup 4 Id >>= Lookup 1 Id) [[1,2,3]]
Val Nothing
>>>
pz @(Lookup 0 Id >>= Lookup 5 Id) [[1,2,3]]
Val Nothing
>>>
pz @(Lookup 0 Id >>= Lookup 1 Id >>= MaybeBool Even '(Id,"is even!")) [[1,2,3]]
Val (Just (2,"is even!"))
>>>
pz @(Lookup 0 Id >>= Lookup 1 Id >>= MaybeBool Even '(Id,"is even!")) [[1,5,3]]
Val Nothing
>>>
pz @((48...55) >>= '[ '[ToEnum Char << Id,ToEnum Char << (Id+3),ToEnum Char << (Id+6)] ]) ()
Val ["036","147","258","369","47:","58;","69<","7:="]
>>>
pz @((48...55) >>= '[ Map (ToEnum Char) << '[ Id, Id+3 ,Id+6 ] ]) ()
Val ["036","147","258","369","47:","58;","69<","7:="]
similar to sequenceA
>>>
pz @Sequence [Just 10, Just 20, Just 30]
Val (Just [10,20,30])
>>>
pz @Sequence [Just 10, Just 20, Just 30, Nothing, Just 40]
Val Nothing
like traverse
>>>
pl @(Traverse (If (Gt 3) (Pure Maybe Id) (EmptyT Maybe _))) [1..5]
Present Nothing ((>>) Nothing | {Sequence Nothing | [Nothing,Nothing,Nothing,Just 4,Just 5]}) Val Nothing
>>>
pl @(Traverse (MaybeBool (Le 3) Id)) [1..5]
Present Nothing ((>>) Nothing | {Sequence Nothing | [Just 1,Just 2,Just 3,Nothing,Nothing]}) Val Nothing
>>>
pl @(Traverse (If (Gt 0) (Pure Maybe Id) (EmptyT Maybe _))) [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]}) Val (Just [1,2,3,4,5])
>>>
pl @(Traverse (If (Gt 0) (Pure Maybe Id) (MkNothing _))) [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]}) Val (Just [1,2,3,4,5])
>>>
pl @(Traverse (MaybeBool (Id >= 0) 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]}) Val (Just [1,2,3,4,5])
>>>
pl @(Traverse (MaybeBool (Id <= 3) Id)) [1..5]
Present Nothing ((>>) Nothing | {Sequence Nothing | [Just 1,Just 2,Just 3,Nothing,Nothing]}) Val Nothing
similar to join
>>>
pz @Join (Just (Just 20))
Val (Just 20)
>>>
pz @Join ["ab","cd","","ef"]
Val "abcdef"
alternative
data p <|> q infixl 3 Source #
similar to <|>
>>>
pz @(Fst <|> Snd) (Nothing,Just 20)
Val (Just 20)
>>>
pz @(Fst <|> Snd) (Just 10,Just 20)
Val (Just 10)
>>>
pz @(Fst <|> Snd) (Nothing,Nothing)
Val Nothing
>>>
pl @(Fst <|> Snd) (Just "cdef",Just "ab")
Present Just "cdef" ((<|>) Just "cdef" | p=Just "cdef" | q=Just "ab") Val (Just "cdef")
>>>
pl @(Fst <|> Snd) ("cdef","ab"::String)
Present "cdefab" ((<|>) "cdefab" | p="cdef" | q="ab") Val "cdefab"
data EmptyT (t :: Type -> Type) (t1 :: Type) Source #
similar to empty
>>>
pz @(EmptyT Maybe _) ()
Val Nothing
>>>
pz @(EmptyT [] _) ()
Val []
>>>
pz @(C "x" >> EmptyT [] String) (13,True)
Val []
>>>
pz @(Fst >> EmptyT (Either String) _) (13,True)
Val (Left "")
>>>
pz @(If 'True (MkJust 11) (EmptyT _ _)) ()
Val (Just 11)
>>>
pz @(EmptyT [] _ <> "123") ()
Val "123"
data EmptyT' (t :: Type -> Type) p Source #
similar to empty
where t
is the type of the container and t1
is a reference to the type of the items in the container
>>>
pz @(EmptyT' [] "x") ()
Val []
>>>
pz @(EmptyT' (Either String) (1 % 1)) ()
Val (Left "")
>>>
pl @(If 'True (MkJust 11) (EmptyT' _ 0)) ()
Present Just 11 (If 'True Just 11) Val (Just 11)
>>>
pz @(If 'True (MkJust 11) (EmptyT' _ (Hole Int))) ()
Val (Just 11)
>>>
pz @(If 'False (MkJust 11) (EmptyT' _ (Hole Int))) ()
Val Nothing
data EmptyList (t :: Type) Source #
creates an empty list for the given type
>>>
pz @(Id :+ EmptyList _) 99
Val [99]
data EmptyList' (t :: k) Source #
creates an empty list for the given pointer to a type t
>>>
pz @(EmptyList' 123 +: Id) 99
Val [99]
Instances
P (EmptyList' t :: Type) x Source # | |
Defined in Predicate.Data.Lifted type PP (EmptyList' t) x Source # eval :: MonadEval m => proxy (EmptyList' t) -> POpts -> x -> m (TT (PP (EmptyList' t) x)) Source # | |
Show (EmptyList' t) Source # | |
Defined in Predicate.Data.Lifted showsPrec :: Int -> EmptyList' t -> ShowS # show :: EmptyList' t -> String # showList :: [EmptyList' t] -> ShowS # | |
type PP (EmptyList' t :: Type) x Source # | |
Defined in Predicate.Data.Lifted |
Convenient method to convert a value p
to an Alternative based on a predicate b
if b
is True then pure p
else empty
>>>
pz @(EmptyBool [] (Id > 4) 'True) 24
Val [True]
>>>
pz @(EmptyBool [] (Id > 4) 'True) 1
Val []
data EmptyBool' b p Source #
Convenient method to convert a value p
to an Alternative based on a predicate b
the value of p drives the choice of Alternative to use for the empty
similar to EmptyBool
but no need to provide t
as it is inferred from the resulting type of p
if b
is True then run p
else empty
>>>
pz @(EmptyBool' (Id > 4) (MkJust (Id * 3))) 24
Val (Just 72)
>>>
pz @(EmptyBool' (Id > 4) (MkJust (Id * 3))) 2
Val Nothing
>>>
pz @(EmptyBool' (Len > 0) Head) ["Abcd","def","g"::String]
Val "Abcd"
>>>
pz @(EmptyBool' (Len > 0) Head) ([] :: [String])
Val ""
Instances
(Show (PP p a), P b a, P p a, PP p a ~ t x, PP b a ~ Bool, Alternative t) => P (EmptyBool' b p :: Type) a Source # | |
Defined in Predicate.Data.Lifted type PP (EmptyBool' b p) a Source # eval :: MonadEval m => proxy (EmptyBool' b p) -> POpts -> a -> m (TT (PP (EmptyBool' b p) a)) Source # | |
Show (EmptyBool' b p) Source # | |
Defined in Predicate.Data.Lifted showsPrec :: Int -> EmptyBool' b p -> ShowS # show :: EmptyBool' b p -> String # showList :: [EmptyBool' b p] -> ShowS # | |
type PP (EmptyBool' b p :: Type) a Source # | |
Defined in Predicate.Data.Lifted |
bifunctor
similar to bimap
>>>
pz @(BiMap Succ Head) (Left @_ @String 12) -- needs a type signature for Right
Val (Left 13)
>>>
pz @(BiMap Succ Head) (Right "xyz")
Val (Right 'x')
>>>
pz @(FMap (BiMap Succ Head)) [Right "xyz",Left 'a',Right "ab",Left 'x']
Val [Right 'x',Left 'b',Right 'a',Left 'y']
>>>
pz @(FMap (BiMap Succ Pred)) [These 12 'b', This 1, That 'd',That 'e']
Val [These 13 'a',This 2,That 'c',That 'd']
>>>
pz @(BiMap Succ Pred) (True,12,'b')
Val (True,13,'a')
>>>
pl @(FMap $ BiMap Succ (Not Id)) [This @Int @Bool 1, This 2,That True,These 4 False]
Present [This 2,This 3,That False,These 5 True] (FMap BiMap(L) Succ 2 | 1 | BiMap(L) Succ 3 | 2 | BiMap(R) Not (Id True) | BiMap(B) Succ 5 | 4 | Not (Id False)) Val [This 2,This 3,That False,These 5 True]
>>>
pl @(BiMap Succ (Not Id)) (This @Int @Bool 1)
Present This 2 (BiMap(L) Succ 2 | 1) Val (This 2)
>>>
pl @(BiMap Succ (Not Id)) (That @Int @Bool True)
Present That False (BiMap(R) Not (Id True)) Val (That False)
>>>
pl @(BiMap Succ (Not Id)) (These @Int @Bool 1 True)
Present These 2 False (BiMap(B) Succ 2 | 1 | Not (Id True)) Val (These 2 False)
>>>
pan @(FMap $ BiMap Succ (Not Id)) [This @Int @Bool 1, This 2,That True,These 4 False]
P FMap BiMap(L) Succ 2 | BiMap(L) Succ 3 | BiMap(R) Not | BiMap(B) Succ 5 | Not | +- P BiMap(L) Succ 2 | | | `- P Succ 2 | +- P BiMap(L) Succ 3 | | | `- P Succ 3 | +- P BiMap(R) Not | | | `- False Not | | | `- True Id True | `- P BiMap(B) Succ 5 | Not | +- P Succ 5 | `- True Not | `- False Id False Val [This 2,This 3,That False,These 5 True]
>>>
pl @(BiMap Succ Head) (ENone @Int @String)
Present ENone (BiMap <skipped>) Val ENone
>>>
pl @(BiMap Succ Head) (ELeft @Int @String 10)
Present ELeft 11 (BiMap(L) Succ 11 | 10) Val (ELeft 11)
>>>
pl @(BiMap Succ Head) (ERight @Int @String "xyz")
Present ERight 'x' (BiMap(R) Head 'x' | "xyz") Val (ERight 'x')
>>>
pl @(BiMap Succ Head) (EBoth @Int @String 10 "xyz")
Present EBoth 11 'x' (BiMap(B) Succ 11 | 10 | Head 'x' | "xyz") Val (EBoth 11 'x')
>>>
pan @(FMap $ BiMap Succ Head) [ENone,ELeft 10,ERight "abc",EBoth 10 "xyz"]
P FMap BiMap <skipped> | BiMap(L) Succ 11 | BiMap(R) Head 'a' | BiMap(B) Succ 11 | Head 'x' | +- P BiMap <skipped> | +- P BiMap(L) Succ 11 | | | `- P Succ 11 | +- P BiMap(R) Head 'a' | | | `- P Head 'a' | `- P BiMap(B) Succ 11 | Head 'x' | +- P Succ 11 | `- P Head 'x' Val [ENone,ELeft 11,ERight 'a',EBoth 11 'x']
a version of bifoldMap
>>>
pl @(BiFoldMap Id Id) (Right [10..13])
Present [10,11,12,13] (BiFoldMap(R) Id [10,11,12,13]) Val [10,11,12,13]
>>>
pl @(BiFoldMap Id Id) (Left [10..13])
Present [10,11,12,13] (BiFoldMap(L) Id [10,11,12,13]) Val [10,11,12,13]
>>>
pl @(BiFoldMap Id Id) (SG.Arg [1..5] [10..13])
Present [1,2,3,4,5,10,11,12,13] (BiFoldMap(B) Id [1,2,3,4,5] | Id [10,11,12,13]) Val [1,2,3,4,5,10,11,12,13]
>>>
pz @(BiFoldMap (Wrap (SG.Sum _) Id) (Wrap (SG.Sum _) Id)) (SG.Arg 1 4)
Val (Sum {getSum = 5})
>>>
pl @(BiFoldMap '( '[Id], '[]) '( '[], '[Id])) (SG.Arg "hap" "bcd")
Present (["hap"],["bcd"]) (BiFoldMap(B) '(["hap"],[]) | '([],["bcd"])) Val (["hap"],["bcd"])
>>>
pz @(BiFoldMap '(Wrap (SG.Sum _) Id, MEmptyT _) '(MEmptyT _, Id)) (SG.Arg 123 "xyz")
Val (Sum {getSum = 123},"xyz")
>>>
pl @(BiFoldMap '(Wrap (SG.Sum _) Id, MEmptyT _) '(MEmptyT _, Id)) (Left 123)
Present (Sum {getSum = 123},()) (BiFoldMap(L) '(Sum {getSum = 123},())) Val (Sum {getSum = 123},())
>>>
pz @(BiFoldMap (MEmptyT _) $ FoldMap (FoldMap (Wrap (SG.Sum _) Id))) (Right (Just [1..10]))
Val (Sum {getSum = 55})
comonad
similar to extract
>>>
pz @Extract (Nothing,Just 20)
Val (Just 20)
>>>
pz @Extract (Identity 20)
Val 20
>>>
pl @Extract (10,"hello")
Present "hello" (Extract "hello" | (10,"hello")) Val "hello"
similar to duplicate
>>>
pz @Duplicate (20,"abc")
Val (20,(20,"abc"))
function application
function application for pure functions appearing on the rhs: similar to $
>>>
:m + Text.Show.Functions
>>>
pz @(Fst $$ Snd) ((*16),4)
Val 64
>>>
pz @(Id $$ "def") ("abc"<>)
Val "abcdef"
>>>
pz @(Id $$ 12) (*13)
Val 156
>>>
pz @(Id $$ 7 $$ 3) (*)
Val 21
>>>
pz @(Id $$ 7 $$ 3) (,)
Val (7,3)
>>>
pz @(Id $$ "abc" $$ 'True) (,)
Val ("abc",True)
>>>
pz @(Id $$ "asdf" $$ 99 $$ C "A") (,,)
Val ("asdf",99,'A')
>>>
(fmap.fmap) ($ 9999) $ pz @Id (*33)
Val 329967
>>>
(fmap.fmap) ($ 9999) $ pz @(Id $$ 1 $$ 'True) (,,)
Val (1,True,9999)
>>>
(fmap.fmap.fmap) ($ 8) $ pz @'("xxx",Id) (*33)
Val ("xxx",264)
>>>
pz @('True $& 4 $& Id $$ "aa") (,,)
Val (4,True,"aa")
>>>
pz @((Id $$ "abc" $$ Wrap (SG.Sum _) 14) >> Id <> Id) These
Val (These "abcabc" (Sum {getSum = 28}))
flipped function application for expressions: similar to &
>>>
:m + Text.Show.Functions
>>>
pz @(Snd $& Fst) ((*16),4)
Val 64
>>>
pz @("def" $& Id) ("abc"<>)
Val "abcdef"
>>>
pz @('True $& 4 $& Id) (,)
Val (4,True)
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 it fails
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
data Flip (p :: k1 -> k2 -> k3) (q :: k2) (r :: k1) Source #
similar to flip
:see also FlipT
>>>
pz @(Flip Map' Id Succ) [1..5]
Val [2,3,4,5,6]
>>>
pz @(Flip '(,) 'True 2) ()
Val (2,True)
>>>
pz @(Flip ('(,,) 1) 2 Id) "ab"
Val (1,"ab",2)
data Dot (ps :: [Type -> Type]) (q :: Type) Source #
compose simple functions
>>>
pl @(Dot '[L3,L2,L1] Id) ((1,(2,9,10)),(3,4))
Present 10 (Thd 10 | (2,9,10)) Val 10
data RDot (ps :: [Type -> Type]) (q :: Type) Source #
reversed version of Dot
>>>
pl @(RDot '[L1,L2,L3] Id) ((1,(2,9,10)),(3,4))
Present 10 (Thd 10 | (2,9,10)) Val 10
>>>
pl @(RDot '[L1,L2] Id) (('a',2),(True,"zy"))
Present 2 (Snd 2 | ('a',2)) Val 2
data K (p :: k) (q :: k1) Source #
similar to const
:types dont need to match on rhs!
>>>
pl @(RDot '[L1,L2,L3,K "xxx"] Id) 12345
Present "xxx" (K '"xxx") Val "xxx"
>>>
pl @(RDot '[L1,L2,L3,K '("abc",Id)] Id) ()
Present ("abc",()) (K '("abc",())) Val ("abc",())
>>>
pl @(Dot '[K "skip",L6,Lift Dup,Lift Succ] Id) ()
Present "skip" (K '"skip") Val "skip"
>>>
pl @(L3 $ L2 $ L1 $ K Id "dud") ((1,("X",9,'a')),(3,4))
Present 'a' (Thd 'a' | ("X",9,'a')) Val 'a'
>>>
pl @((L3 $ L2 $ L1 $ K Id "dud") >> Pred) ((1,("X",9,'a')),(3,4))
Present '`' ((>>) '`' | {Pred '`' | 'a'}) Val '`'
>>>
pl @(K "ss" $ L3 $ L3 $ Fst) ()
Present "ss" (K '"ss") Val "ss"
Lift a no arg Adt to a function of one argument (for use with Dot
and RDot
)
>>>
pl @(Lift Len Snd) (True,"abcdef")
Present 6 ((>>) 6 | {Len 6 | "abcdef"}) Val 6
coerce
data Coerce (t :: Type) Source #
similar to coerce
>>>
pz @(Coerce (SG.Sum Integer)) (Identity (-13))
Val (Sum {getSum = -13})
>>>
pl @(Coerce SG.Any) True
Present Any {getAny = True} (Coerce Any {getAny = True} | True) Val (Any {getAny = True})
>>>
pl @(Coerce Bool) (SG.Any True)
Present True (Coerce True | Any {getAny = True}) Val True
>>>
pz @(Proxy 'True >> Coerce (Proxy 'False)) () ^!? acts . _Val . to typeRep
Just 'False
>>>
pz @(Proxy Int >> Coerce (Proxy (String,Char))) () ^!? acts . _Val . to typeRep
Just ([Char],Char)
>>>
import qualified GHC.Exts as GE
>>>
pz @(Proxy GE.Any >> Coerce (Proxy Int)) () ^!? acts . _Val . to typeRep
Just Int
>>>
pz @(Proxy '(_,_) >> Coerce (Proxy '(Float,Int))) () ^!? acts . _Val . to typeRep
Just ('(,) * * Float Int)
error handling
run an expression p
and on failure run q
>>>
pz @(Catch Succ (Fst >> Second (ShowP Id) >> PrintT "%s %s" Id >> 'LT)) GT
Val LT
>>>
pz @(Len > 1 && Catch (Id !! 3 == 66) 'False) [1,2]
Val False
>>>
pl @(Catch (Resplit "\\d+(") (Snd >> MEmptyP)) "123"
Present [] (Catch caught exception[Regex failed to compile]) Val []
>>>
pl @(Catch OneP 99) [10,11]
Present 99 (Catch caught exception[OneP:expected one element(2)]) Val 99
>>>
pl @(Catch OneP 99) [10]
Present 10 (Catch did not fire) Val 10
>>>
pl @(Catch OneP 'True) [False]
Present False (Catch did not fire) Val False
>>>
pl @(Catch OneP 'False) [True,True,False]
False (Catch caught exception[OneP:expected one element(3)]) Val False
>>>
pl @(Catch OneP 'True) []
True (Catch caught exception[OneP:expected one element(empty)]) Val True
run an expression p
and on failure print a custom error s
using the error string and the input value
>>>
pz @(Catch' Succ (Second (ShowP Id) >> PrintT "%s %s" Id)) GT
Fail "Succ IO e=Prelude.Enum.Ordering.succ: bad argument GT"
>>>
pz @(Catch' Succ (Second (ShowP Id) >> PrintT "%s %s" Id)) LT
Val EQ
>>>
pl @(Catch' (FailT Int "someval") (PrintT "msg=%s caught(%03d)" Id)) 44
Error msg=someval caught(044) (Catch default condition failed) Fail "msg=someval caught(044)"
>>>
pl @(Catch' OneP (Second (ShowP Id) >> PrintT "msg=%s caught(%s)" Id)) [10,12,13]
Error msg=OneP:expected one element(3) caught([10,12,13]) (Catch default condition failed) Fail "msg=OneP:expected one element(3) caught([10,12,13])"
>>>
pl @(Catch' OneP (PrintT "msg=%s caught(%s)" (Second (ShowP Id)))) [10]
Present 10 (Catch did not fire) Val 10
>>>
pl @(Catch' OneP (PrintT "msg=%s err s=%s" (Second (ShowP Id)))) [10,11]
Error msg=OneP:expected one element(2) err s=[10,11] (Catch default condition failed) Fail "msg=OneP:expected one element(2) err s=[10,11]"
type families
type family DotExpandT (ps :: [Type -> Type]) (q :: Type) :: Type where ... Source #
calculates the return type for Dot
DotExpandT '[] _ = TypeError ('Text "'[] invalid: requires at least one predicate in the list") | |
DotExpandT '[p] q = p $ q | |
DotExpandT (p ': (p1 ': ps)) q = p $ DotExpandT (p1 ': ps) q |
type family RDotExpandT (ps :: [Type -> Type]) (q :: Type) :: Type where ... Source #
calculates the return type for RDot
RDotExpandT '[] _ = TypeError ('Text "'[] invalid: requires at least one predicate in the list") | |
RDotExpandT '[p] q = p $ q | |
RDotExpandT (p ': (p1 ': ps)) q = RDotExpandT (p1 ': ps) (p $ q) |