Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What functionality do you get for free with Functors or other type-classes?

I read an article which said:

Providing instances for the many standard type-classes [Functors] will immediately give you a lot of functionality for practically free

My question is: what is this functionality that you get for free (for functors or other type-classes)? I know what the definition of a functor is, but what do I get for free by defining something as a functor/other type-class. Something other than a prettier syntax. Ideally this would be general and useful functions that operate on functors/other type-classes.

My imagination (could be wrong) of what free means is functions of this sort: TypeClass x => useful x y = ..

== Edit/Additition ==

I guess I'm mainly asking about the more abstract (and brain boggling) type-classes, like the ones in this image. For less abstract classes like Ord, my object oriented intuition understands.

like image 546
user1138184 Avatar asked Jan 09 '12 07:01

user1138184


People also ask

Why are functors useful?

Functors are also important because they are a building block for applicatives and monads, which are coming in future posts.

What are Functors in Haskell?

Functor in Haskell is a kind of functional representation of different Types which can be mapped over. It is a high level concept of implementing polymorphism. According to Haskell developers, all the Types such as List, Map, Tree, etc. are the instance of the Haskell Functor.

What is a functor in programming?

In functional programming, a functor is a design pattern inspired by the definition from category theory, that allows for a generic type to apply a function inside without changing the structure of the generic type. This idea is encoded in Haskell using type class.

What is an applicative in Haskell?

In Haskell, an applicative is a parametrized type that we think of as being a container for data of that type plus two methods pure and <*> . Consider a parametrized type f a . The pure method for an applicative of type f has type. pure :: a -> f a. and can be thought of as bringing values into the applicative.


1 Answers

Functors are simple and probably not the best example. Let's look at Monads instead:

  • liftM - if something is a Monad, it is also a Functor where liftM is fmap.
  • >=>, <=<: you can compose a -> m b functions for free where m is your monad.
  • foldM, mapM, filterM... you get a bunch of utility functions that generalize existing functions to use your monad.
  • when, guard* and unless -- you also get some control functions for free.
  • join -- this is actually fairly fundamental to the definition of a monad, but you don't need to define it in Haskell since you've defined >>=.
  • transformers -- ErrorT and stuff. You can bolt error handling onto your new type, for free (give or take)!

Basically, you get a wide variety of standard functions "lifted" to use your new type as soon as you make it a Monad instance. It also becomes trivial (but alas not automatic) to make it a Functor and Applicative as well.

However, these are all "symptoms" of a more general idea. You can write interesting, nontrivial code that applies to all monads. You might find some of the functions you wrote for your type--which are useful in your particular case, for whatever reason--can be generalized to all monads. Now you can suddenly take your function and use it on parsers, and lists, and maybes and...

* As Daniel Fischer helpfully pointed out, guard requires MonadPlus rather than Monad.

like image 73
Tikhon Jelvis Avatar answered Oct 17 '22 04:10

Tikhon Jelvis