Haskell Language Learning Note (biapplicative)

Source: Internet
Author: User

Biapplicative
class Bifunctor p => Biapplicative p where  bipure :: a -> b -> p a b  (<<*>>) :: p (a -> b) (c -> d) -> p a c -> p b d  (*>>) :: p a b -> p c d -> p c d  a *>> b = bimap (const id) (const id) <<$>> a <<*>> b  (<<*) :: p a b -> p c d -> p a b  a <<* b = bimap const const <<$>> a <<*>> b(<<$>>) :: (a -> b) -> a -> b(<<$>>) = id

Biapplicative is a type class. It is mainly used to apply applicative on two metadata data structures.

(,) It's a biapplicative.
instance Biapplicative (,) where  bipure = (,)  (f, g) <<*>> (a, b) = (f a, g b)
Const is a biapplicative.
instance Biapplicative Const where  bipure a _ = Const a  Const f <<*>> Const x = Const (f x)
Application Biapplicative
Prelude Data.Biapplicative> ((+2),(*3)) <<*>> (3,4)(5,12)Prelude Data.Biapplicative Control.Applicative> Const (+2) <<*>> Const 3Const 5Prelude Data.Biapplicative> bimap (+) (*) <<$>> (2,3) <<*>> (3,4)(5,12)Prelude Data.Biapplicative Control.Applicative> bimap (+) (*) <<$>> Const 2 <<*>> Const 3Const 5
(<<**>>), biliftA2, biliftA3
(<<**>>) :: Biapplicative p => p a c -> p (a -> b) (c -> d) -> p b d(<<**>>) = biliftA2 (flip id) (flip id)biliftA2 :: Biapplicative w => (a -> b -> c) -> (d -> e -> f) -> w a d -> w b e -> w c fbiliftA2 f g a b = bimap f g <<$>> a <<*>> bbiliftA3 :: Biapplicative w => (a -> b -> c -> d) -> (e -> f -> g -> h) -> w a e -> w b f -> w c g -> w d hbiliftA3 f g a b c = bimap f g <<$>> a <<*>> b <<*>> c
Prelude Data.Biapplicative> (2,3) <<**>> ((2^), (3*))(4,9)Prelude Data.Biapplicative> biliftA2 (+) (*) (2,3) (4,5)(6,15)Prelude Data.Biapplicative Control.Applicative> biliftA2 (+) (*) (Const 2) (Const 3)Const 5

Haskell Language Learning Note (biapplicative)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.