Questions tagged [bifunctor]

A bifunctor is a type constructor with two covariant type parameters that can be "mapped" over. Examples include tuples and Either (a.k.a. Result, disjunction) types.

In , the canonical bifunctors implementation is the bifunctors package by Edward Kmett. It provides the Bifunctor class with several common instances, and a number of other useful modules. The basic functions for working with Bifunctor are first, second and bimap, as seen in the following ghci transcript:

> import Data.Bifunctor
> let pair = (5, "hi")
> first (*6) pair
(30,"hi")
> second (fmap toUpper) pair
(5,"HI")
> bimap (*6) (fmap toUpper) pair
(30,"HI")
24 questions
18
votes
4 answers

Traversing with a Biapplicative

I was thinking about unzipping operations and realized that one way to express them is by traversing in a Biapplicative functor. import Data.Biapplicative class Traversable2 t where traverse2 :: Biapplicative p => (a -> p b c) -> t a…
dfeuer
  • 44,398
  • 3
  • 56
  • 155
13
votes
1 answer

What are bifunctors?

I'm relatively new to Haskell and having trouble understanding the utility of bifunctors. I think I understand them in theory: say for example if I wanted to map across a type that abstracts multiple concrete types, such as Either or Maybe, I'd need…
Sophia Gold
  • 734
  • 1
  • 8
  • 18
12
votes
2 answers

Bifunctor vs. Arrow methods

There is a bit of overlap between Bifunctor and Arrow methods: class Bifunctor p where first :: (a -> a') -> p a b -> p a' b second :: (b -> b') -> p a b -> p a b' bimap :: (a -> a') -> (b -> b') -> p a b -> p a' b' class Arrow (~~>) where …
dfeuer
  • 44,398
  • 3
  • 56
  • 155
11
votes
1 answer

What would be the methods of a bi-comonad?

While musing what more useful standard class to suggest to this one class Coordinate c where createCoordinate :: x -> y -> c x y getFirst :: c x y -> x getSecond :: c x y -> y addCoordinates :: (Num x, Num y) => c x y -> c x y -> c x y it…
leftaroundabout
  • 101,764
  • 3
  • 156
  • 291
7
votes
2 answers

Is there such thing as a bidistributive? What function do I need here?

I have code (in C# actually, but this question has nothing to do with C# specifically, so I will speak of all my types in Haskell-speak) where I am working inside of an Either a b. I then bind a function with a signature that in Haskell-speak is b…
Keith Pinson
  • 7,162
  • 5
  • 54
  • 97
7
votes
3 answers

Can BiFunction references be passed to methods expecting a functional interface?

I've always only used Java 6 and am right now catching up to learn what's new in Java 8. I was reading this article here: http://www.drdobbs.com/jvm/lambda-expressions-in-java-8/240166764?pgno=2 And it says: The Java API defines several generic…
GrowinMan
  • 4,600
  • 11
  • 38
  • 57
5
votes
1 answer

Fixed points of representational bifunctors

Edward Kmett's experimental roles package offers various utilities for lifting coercions, some of which I've pasted at the end of this question. The key class in the package is class Representational (t :: k1 -> k2) where -- | An argument is…
dfeuer
  • 44,398
  • 3
  • 56
  • 155
4
votes
1 answer

Understanding of bifunctor from Category Theory for Programmers - Ch. 8

I'm going mental with Chapter 8 from Category Theory for Programmers. In section 8.3, Bartosz defines this type newtype BiComp bf fu gu a b = BiComp (bf (fu a) (gu b)) Here, if I'm understanding a bit of Haskell, bf, fu, and gu are type…
4
votes
3 answers

Type Signature Mismatch on Bifunctor instance definition

I'm learning about Haskell by working through the book Haskell Programming from First Principles, Allen & Moronuki. In the exercises for the chapter on Monad Transformers, Functor & Applicative composition, it asks the reader to write Bifunctor…
Haleemur Ali
  • 20,742
  • 3
  • 37
  • 68
4
votes
1 answer

How to derive multiple Functor instances in Haskell?

I'm defining an AST for expression, and it has three type arguments, like following: {-# language DeriveFunctor, DeriveFoldable, DeriveTraversable #-} -- | a general represetation of an expression -- , with ref info contained in r and two…
luochen1990
  • 3,124
  • 1
  • 16
  • 30
4
votes
0 answers

What is the lens equivalent of bitraverse?

Is there a way to create a lens that could do a similar thing to this function? λ> :t \f -> bitraverse f f \f -> bitraverse f f :: (Applicative f, Bitraversable t) => (b -> f d) -> t b b -> f (t d d) The specific type I'm trying to satisfy…
SimonF
  • 804
  • 1
  • 7
  • 14
3
votes
1 answer

Is this type a valid "rank-2 bifunctor"?

The rank2classes package provides a version of Functor for which the mapped functions seem to be natural transformations between type constructors. Following that idea, here's a rank-2 version of Bifunctor: {-# LANGUAGE RankNTypes #-} {-# LANGUAGE…
danidiaz
  • 24,322
  • 4
  • 41
  • 79
3
votes
1 answer

Haskell independently deciding to infer Bifunctor for my datatype?

For some reason, GHC seems to be deciding that my datatype (with two type parameters) instantiates Bifunctor for no reason at all. The funniest thing is that this is only used to tell me that there are overlapping instances of Functor for this…
3
votes
2 answers

Is there any connection between the contravarience of Hom Functor and Scala's Function1?

The Hom functor Hom(-,-) is contravariant in the first argument and covariant in the second. Can this fact somehow offer another explanation why Scala's Function1[-T1, +R] has the same property? I have seen this claim made for example here, but at…
rapt
  • 10,528
  • 29
  • 92
  • 134
3
votes
1 answer

Lifting an Iso into the first argument of a Bifunctor

Control.Lens.Iso contains many wonderful functions for lifting Isos into various type arguments of useful abstractions. For example: mapping for arbitrary Functors contramapping for Contravariant functors dimapping, lmapping and rmapping for…
frasertweedale
  • 4,801
  • 3
  • 22
  • 36
1
2