News for hackers. Stuff that matters.
Re:Why isn't Haskell popular in industry? by wisty on Monday August 02, 2010 @02:59AM (#1567068)
achieving anything practical using purely functional code remains extremely difficult ...

Excel is pretty much a functional language (though it is a little disabled), and people do amazing things in it.

The problem isn't that functional languages are hard. They are a bit different, but good code is often fairly functional anyway. The problem is that most of the community seems to be obsessed with showing that functional languages are both better, and more difficult, than mere procedural languages.

Take monads. The only way to "get" monads is to realize that they are just sections of the program that are imperative. But most Haskell programmers introduce them in the most incredibly obscure double-talk, just to avoid admitting that Haskell needs the ability to do imperative things in order to be useful.

Re:Why isn't Haskell popular in industry? by jrockway on Monday August 02, 2010 @04:24AM (#1567168)
Take monads. The only way to "get" monads is to realize that they are just sections of the program that are imperative.

Not really. Monadic function composition is just like regular composition, except that the programmer is given the ability to make "f of g of x" do something more than just pipe the result of g(x) into f. This can look like imperative programming, but it's still purely functional.

If you're willing to call monads Kleisli arrows instead, then you can even the same syntax to chain monadic computations as "regular" computations.

For example, write a function to add one to a number, then multiply by 4:

   f :: Num a => a -> a
   f = (*4) . (+1)
That's a normal function, with the normal function composition operator.

Now write a function to increment the state, in a stateful computation, by a number:

   inc :: Int -> State Int ()
   inc x = put . (arr (+x)) . get
Even though State is a monad or Kleisli arrow, you can treat the stateful computation as regular function composition, because it is just composition. There is no imperative programming anywhere to be seen. Although operating on some hidden state feels imperative, it's not. (Under the covers, it is a bit different than what you might be used to. Each stateful function is really a function from its arguments to another function from the current state to the result. We compose the "result" functions into one big function from state to result. This involves a bit of plumbing, but the actual implementation is only two lines of code. And it makes for a very useful abstraction in many cases. But I digress...)

Monads are just a way to make similar things, chaining computations with an arbitrary combinator, look similar in your code. We did it with State above, and there are lots of other things that work similarly. Computations that can return zero or more results, computations that can fail, computations that operate on transactional memory, etc. We use "Monad" (or "Arrow") to provide the programmer with a common syntax for interacting with each. That's all a monad is.

(IO is weird, and it is a monad, but it could also be an applicative functor, or comonad, or arrow, or a lazy list, or. Don't extrapolate your knowledge or fear of the IO monad onto monads in general.)

Re:Why isn't Haskell popular in industry? by bguthrie on Monday August 02, 2010 @04:43AM (#1567184)
I have no idea what a Kleisli arrow is, and a simple Google search points me right back in the direction of the HaskellWiki. They may be a perfectly simple concept, but if they are, then the term is not in widespread use. One of the major problems with explaining monads is that programmers who understand them frequently attempt to explain them by using terms even less well-understood than "monad". You get points for precision, but none for evangelism.

It is of no help or use to those who do not understand monads that they could also be an applicative functor, or comonad, or whatever. You may be perfectly correct, but for someone who is not already part of your community, and conversant in its terms, it does not particularly help.

Re:Why isn't Haskell popular in industry? by jrockway on Monday August 02, 2010 @04:51AM (#1567192)
A Kleisli arrow is just an arrow that works like a monad:

    -- | Kleisli arrows of a monad.

    newtype Kleisli m a b = Kleisli { runKleisli :: a -> m b }

    instance Monad m => Category (Kleisli m) where
           id = Kleisli return
           (Kleisli f) . (Kleisli g) = Kleisli (\b -> g b >>= f)
I only brought it up to show that we can use the same operator, Control.Category.., to compose functions, monads, and arrows and that they are not all that different from each other.

(It's also worth noting that every language has their own language-specific terminology. If I Google for "std::string", I am only going to find C++ results, despite the fact that strings are a generic concept. If I search for "IEnumerable", I am only going to get C# results, despite the fact that enumerations are a generic concept.

Similarly, not many languages think of programming in terms of arrows and objects and categories, so you don't see many results for "Kleisli arrow" outside of the Haskell community.

But http://en.wikipedia.org/wiki/Kleisli_category is to category theoretical monads as Kleisli arrow is to Monad in Haskell.)

Re:Why isn't Haskell popular in industry? by stonemetal on Monday August 02, 2010 @07:55AM (#1567415)
Way to make his point. Only programming language where Arrows ever come up, Haskell. Wiki page on Arrows "Generalized Monads". So far our definition of a Monad is a specialized version of the generalization of Monads.
Re:Why isn't Haskell popular in industry? by ralphc on Monday August 02, 2010 @10:44AM (#1567750)
Hear hear. I looked at Haskell years ago, liked the functional aspects, recursion, lists, etc., but monads for I/O, database, was like hitting a wall. Switched to ocaml, it had enough imperativeness for that, but I got turned off when I got to functors of modules or something like that. Worked in Common Lisp for a while, but now I'm happy in Clojure, I got back the FP but I don't need a PhD for talking to a database.
Re:Why isn't Haskell popular in industry? by jrockway on Monday August 02, 2010 @11:05AM (#1567806)
Problem is, when you use a programming language without arrows, you end up rewriting generic plumbing every time. If you want to write more code in your app to avoid using the word "monad", that's fine, but probably not rational.
Re:Why isn't Haskell popular in industry? by stonemetal on Monday August 02, 2010 @02:18PM (#1568485)
This has nothing to do with the use of monads, but how they are explained. When your explain monads to people who don't no what monads are speach starts with "It is an arrow..." You are explaining monads to yourself, not to someone who doesn't understand monads (and consequently doesn't have a clue what arrows are.)

So far as I can tell monads are abstraction of state(though calling them abstractions of function application is more correct, calling it state is more intuitive to me, and how they are represented in the type system.) From there they get used for different things: Maybe == Nullable Types, List == lists, Either == Unions, St == Mutable State, IO == hide IO. Then a function that is pure and doesn't know about what your monad does gets invoked by the monad where the hidden state changes the flow of computation. Maybe performs a null check, List calls map, Either picks the type in the union then calls the function on that, St allows you to use and update the hidden state, IO performs IO then shares the result with you.

Now there is an explanation of a monad that doesn't assume you already know what a monad is. It may not be good since it basically says monads are sticky higher order functions masquerading as a data type, but there you have it.

Re:Why isn't Haskell popular in industry? by jrockway on Monday August 02, 2010 @04:16PM (#1568895)
Honestly, I'm tired of explaining monads in every HN article about Haskell. I've written detailed explanations for newbies 100s of times. If you want to learn more about something, Google it.

Remember, you understanding Haskell will improve your understanding of the Universe. You understanding Haskell will do nothing for me. So you can see how the incentives are aligned...

Re:Why isn't Haskell popular in industry? by bguthrie on Monday August 02, 2010 @02:07PM (#1568449)
I don't disagree that every technical community has its own terms. But it's very difficult to broaden that community by restricting yourself narrowly to those terms. To me, "an arrow that works like a monad" reads like a recursive definition: if you don't understand how one works, you can't understand the other.

The net effect is that your community is going to be composed of people who made a conscious effort to break through the communication barrier. This isn't necessarily a bad thing; you end up with a small, focused group of very smart people. But it doesn't buy you many converts in the industry.

Re:Why isn't Haskell popular in industry? by tkahn6 on Monday August 02, 2010 @04:58AM (#1567198)
The main thing to take away is that:

When a function uses global state to compute a value, it's like the global state is an argument to the function.

When a function modifies global state, it's like it returns both the value it computes and the modified global state.

If you're interested more you can read http://research.microsoft.com/en-us/um/people/simonpj/papers... which I believe is the original paper introducing monads into Haskell. The first 11 pages are very approachable and give you the rational for why monads are necessary in Haskell and what they're for.

Re:Why isn't Haskell popular in industry? by cabalamat on Monday August 02, 2010 @07:54AM (#1567414)
> Monadic function composition is just like regular composition, except that the programmer is given the ability to make "f of g of x" do something more than just pipe the result of g(x) into f. This can look like imperative programming, but it's still purely functional.

> If you're willing to call monads Kleisli arrows instead, then you can even the same syntax to chain monadic computations as "regular" computations.

I have no idea what any of that is meant to mean! Are you just trying to prove wisty's assertion that "Haskell programmers introduce them in the most incredibly obscure double-talk"?

Re:Why isn't Haskell popular in industry? by jrockway on Monday August 02, 2010 @04:10PM (#1568875)
Function composition is obscure? Did you take Algebra 1?

(I used the term "Kleisli arrow" so that you could Google for something if you wanted to learn more. All you really need to know is that you can use . to pass the output of a regular function into the input of another one, and you can use . to pass the output of a monad into the input of another. Saying it that way is very imprecise, though, and the goal of people explaining things is to say them precisely so that you have some hope of eventually gaining an understanding.

You have to bootstrap understanding. Assume you know what a Kleisli arrow is. Then read the rest of the post. Now your assumption is correct!)

Re:Why isn't Haskell popular in industry? by naradaellis on Monday August 02, 2010 @03:55AM (#1567144)
Perhaps the reason that Haskellers don't describe monads as "just sections of the program that are imperative" is because that statement is _not_ true.

A monad is a very nice container abstraction - period.

The IO part of Haskell just happens to leverage monads. One of the benefits of which is an explicit marking of impure methods in the type signature, but there are others.

Monads are used in plenty of purely functional parts of Haskell.

Disclosure: been programming Haskell for only a few months now, so someone pull me up if I'm wrong. However, I have used a lot of monads so far (and they're not hard, its just a higher, more convenient level of abstraction).

edit: to address your post further. I honestly don't think that the community is the reason that Haskell isn't taking off. If you were to gather stats on the points at which would-be Haskellers give up, I think most people would leave 1) when they can't grok the syntax easily or 2) when they can't get tools or libraries going easily (it was a problem for me) and long before they get to the point of visiting the news groups and start running up against academic-type people.

Re:Why isn't Haskell popular in industry? by tkahn6 on Monday August 02, 2010 @05:01AM (#1567202)
Can you give an example of where monads are not used to interact with stateful things? That would be helpful to me.
Re:Why isn't Haskell popular in industry? by jrockway on Monday August 02, 2010 @06:01AM (#1567278)
The Maybe and List monads have nothing to do with state, and are very common in programs.

Let's use the List monad to determine someone's roommates.

    > import Control.Applicative
    > import Data.List
First, the data:

    > type Person = String
    > type Address = String

    > addresses :: [(Person, Address)]
    > addresses = [("jrockway", "123 Fake St."), ("jrockway's cat", "123 Fake St.")]

    > people :: [(Address, Person)]
    > people = uncurry (flip (,)) <$> addresses
And some helper functions around this data, a function to return all addresses for a person, and a function to return all people that live at a certain address:

    > assocFilter :: Eq a => a -> [(a,b)] -> [b]
    > assocFilter p xs = snd <$> filter ((==p) . fst) xs

    > addressesForPerson person = assocFilter person addresses
    > peopleAtAddress address = assocFilter address people
To find a person's roommate, we have to chain two computations. First, we have to find zero or more places where a person lives. Then we need to find who else lives at each of those addresses. With the List monad, this is not much code!

    > roommatesFor :: Person -> [Person]
    > roommatesFor person = do
    >     address <- addressesForPerson person
    >     peopleAtAddress address
The monadic combinator >>= (which is hidden by do) does all the looping for us, so we don't have to explicitly write it out.

BTW, you can just cut-n-paste this into a .lhs file and run it, if you want to try it out.

Re:Why isn't Haskell popular in industry? by naradaellis on Monday August 02, 2010 @05:42AM (#1567249)
I don't know how familiar you are with Haskell... but anyway:

Say you're working with the Maybe type, which is often used to represent operations that might fail, such as a map lookup, for example.

  data Maybe a = Just a | Nothing
We're using two functions defined like so (feeling unimaginative at the moment, forgive me):

  x :: String -> Maybe String
  y :: String -> Maybe String
We want to use them together. Someone who's not familiar with monads might write something like this:

  myFunction text = case x text of
                      (Just newText) -> y n
                      Nothing        -> Nothing
(If x works and returns a value, put that value into the y function. If x didn't work, just return Nothing)

It works fine, but imagine you had 3 functions that returned Maybes, it would get tiresome and messy nesting all those case statements endlessly.

  [...]
  case x text of
    Just newText -> case y newText of [...]
Not being satisfied with boilerplate, lets make an operation that will simplify this a bit:

  bind :: Maybe a -> (a -> Maybe b) -> Maybe b
  bind (Just x) f = f x
  bind Nothing  _ = Nothing
now we can define myFunction like so:

  myFunction text = (x text) `bind` y
if we want to add another operation:

  myFunction text = (x text) `bind` y `bind` z
Congratulations you've mostly made a monad. Bind is one of the monad operators (>>=), the other operators are extremely trivial to implement for the Maybe type.

This is why we say a monad is just a container with some handy operators. Maybe is the container, and bind is a way to chain together operations (without explicitly taking the value out of the container) so the operators themselves don't have to know anything about the nature of what they're dealing with.

I won't bang on anymore, this is a longer, better written tutorial[1] in the same vein as this short summary.

[1]: http://blog.sigfpe.com/2006/08/you-could-have-invented-monad...

Re:Why isn't Haskell popular in industry? by tkahn6 on Monday August 02, 2010 @05:50AM (#1567258)
I appreciate that. I am actually familiar with Haskell and the Maybe monad.

Are there use cases for Maybe where you're not interacting with some sort of IO?

Re:Why isn't Haskell popular in industry? by naradaellis on Monday August 02, 2010 @06:11AM (#1567297)
Oh yeah sure all the time, Maybe shows up a lot. It becomes more useful and less of a pain once you realise you can deal with it using monad operators. From the standard library a list operator and a map operator:

  elemIndex :: a -> [a] -> Maybe Int
  lookup :: k -> Map k a -> Maybe a
Say we have a map of lists of string, for some reason. We need to find the index of the element "foobar" in a particular key, so you could write:

  lookup key map >>= elemIndex "foobar"
(a simple example for the sake of brevity)

Not very impressive, but monads aren't anything groundbreaking after all, they're just utilities that make our lives easier. There are more complicated operations that come in handy later, but they are quite straightforward once you get a grasp of the container metaphor.

Re:Why isn't Haskell popular in industry? by tkahn6 on Monday August 02, 2010 @06:29AM (#1567311)
Ah duh. That makes sense.

Would it also make sense to use Maybe monad for converting a [Char] to an Integer?

stringToInt :: [Char] -> Maybe Integer

Re:Why isn't Haskell popular in industry? by naradaellis on Monday August 02, 2010 @07:26AM (#1567388)
Yeah it would. I think there was a Real World Haskell exercise that involved that actually.
Re:Why isn't Haskell popular in industry? by jamii on Monday August 02, 2010 @05:06AM (#1567210)
PFP uses monads to represent probability distributions.

http://web.engr.oregonstate.edu/~erwig/pfp/

Re:Why isn't Haskell popular in industry? by chc on Monday August 02, 2010 @01:38PM (#1568334)
Here's a common example of a monad being used in a non-stateful way:

  map (* 2) [1,2,3,4]
Yep, that's a function on a monad — lists are monads. The same function can be written:

  [1,2,3,4] >>= return . (* 2)
Re:Why isn't Haskell popular in industry? by ww520 on Monday August 02, 2010 @11:34AM (#1567896)
It's interesting to see Wisty's post and the replies immediately prove his point. BTW I like Haskell.
Re:Why isn't Haskell popular in industry? by BMeph on Tuesday August 10, 2010 @05:16PM (#1592978)
I disagree. His point is not merely that Haskell advocates cannot clearly explain simple interfaces like Monad (let's be honest here; this is more about Haskell folks being too researchy, and not being better skilled in the dog-and-pony-show environment of "practical" marketing), but that the lack of skill in clear explanation is due to a reluctance to admit that "Haskell needs the ability to do imperative things in order to be useful".

The thing to "get" about Monads are that they are a means to describe imperative things going on, in a way that blends in with not necessarily imperative things. Which is part of why it's so tough to explain them - we're all talking past each other regarding what "the point" of using Monads are. The point to them in Haskell is mainly twofold:

  1) Haskell users can use a Monad to describe net access, DB access, file access, and other "once done, the results are out of your control" effects in the same way as describing using (Writer) computations that keep a log of what's being done, (Error) exceptions, (Reader) environment variables, (List) functions with multiple - or no - results, and more.
It's really easy...and kind of mind-blowingly complex, all at the same time.