
Architecture of a Real World Haskell Application - davidkellis
http://www.onikudaki.net/blog/archives/6
======
jarrett
As the author says, there's quite a lot of imperative stuff in this design.
I'm still on the fence about whether that's the best way to build large
applications with GUIs, network access, etc.

With the imperative approach, you make liberal use of the IO monad. There's a
lot of explicit reading from and writing to mutable storage. If you're doing
this approach right, you mostly just use the IO monad for actual IO and
threading; most of the real logic of the program is kept pure. This approach
has the advantage of being familiar. And more importantly, it just plain
works. It's how I tend to do things.

Yet I can't help but wonder if there's a better way. The above approach, while
comfortable, leaves me with a bad taste in my mouth, like I'm somehow fighting
against the language. I've often heard functional reactive programming (FRP)
described as the best alternative. Though I remain skeptical. I'd be
interested in opinions about the future of FRP.

~~~
steveklabnik
As FRP becomes more and more 'normal' in JavaScript land, I think it has a
bright future.

~~~
mattgreenrocks
I think FRP is a good idea; but it seems to expose a failing of many
languages: the lack of metaprogrammability. In particular, most (non-Lisp)
languages don't let you mess with control flow much.

The result is that when writing FRP code, users have to manage two control
flows: the 'logical' control flow, which is expressed via whatever FRP library
they're using, and the actual control flow, which is the one provided by the
language they're in. Languages would need good support for source-to-source
transformation, or, alternatively, allow the compiler IR to be manipulated by
libraries.

From the little I've played with Haskell, it appears the monadic binding
operator (>>=) lets you do this, and the user writes code in an idiomatic,
imperative-like manner. It is extremely impressive that this capability can be
used to arbitrarily extend the language, while being a very small concept.

Edit: not sure if I had my terms right on the operator.

~~~
seanmcdirmid
Exactly! This is why I've turned my research away from FRP-style lifted values
to just lifting all the code over time [1], so control flow is quite normal.

[1]
[http://research.microsoft.com/pubs/211297/managedtime.pdf](http://research.microsoft.com/pubs/211297/managedtime.pdf)

~~~
mattgreenrocks
I love this. I have a feeling we'll see something like this in production one
day.

How'd you get hooked up with MS doing such cool stuff?

~~~
seanmcdirmid
Actually, MSR is supposed to do cool stuff. I got here by accident, mostly.

------
ixmatus
Not sure if "Architecture" is the right word here. More like, "How I built a
Real World Haskell Application".

When I read about how to "architect" something I'm think about how to
structure my Cabal file (a common pattern in my apps is a "library" of types,
parsers, and utilities that are jointly used by multiple executables of _the
same program_ ). I also think about structuring my tests, the naming and
structure of module directory hierarchies, maybe also how to structure the
program around the Main entry point.

I think there may also be a more interesting couple of "sub-articles" in this
article: "How I built a server daemon in Haskell" and "How I built a GUI
client in Haskell".

~~~
Spearchucker
Agreed. I get this a lot, so have definitions ready -
[http://www.bredemeyer.com/whatis.htm](http://www.bredemeyer.com/whatis.htm).

If architecture in the computing world consists of a system’s elements; the
relationships between those elements; the attributes of the elements; and the
attributes of the relationships, then architecture is _not_ a timeline,
process or stack description.

~~~
platz
also: architecture implies that you had a variety of solutions to choose from,
and that you made a choice from one of the possible solutions and are able to
reason and talk about that choice, the pros and cons, etc.

If you can't list the pros and cons relative to the other choices, I don't
really consider that architecture - that's just doing what's familiar.

~~~
tormeh
What if it's the first time you do something? You just figured something out
and thought it looked OK. Isn't that architecture?

~~~
ixmatus
Not quite, I would call that "solution".

~~~
tormeh
Also when you have code block, data flow and control flow diagrams? How well
thought out does something have to be to be architecture?

~~~
ixmatus
An Architecture, to me, is an established and idiomatic process of applying
solutions to produce a novel output.

In the case of a computer program the "architecture" should be extremely well-
thought out, idiomatic, and using established practices to produce a program
that is easy to maintain, understand, and test.

I don't believe you need diagrams but in the case of Haskell, the types (if
you design type-first) serve as one piece of the blueprint that is to become
your program, your cabal file, directory structure, documentation (both user
docs and API docs), and tests are all part of that blueprint.

------
egonschiele
> it could benefit from the lens library, but last time I tried it did have
> problems with compilation with profiling information

In my experience, template haskell does not play well with profiling. If you
were deriving lenses with TH, you could try writing them out yourself. That
might solve this issue.

------
z3phyr
It is great to see Haskell being 'used' in the 'space-science industry'
(please tell me a good word for this)

I would love to read more about mainstream functional language being used in
scientific domains like CERN, ESA, NASA or SpaceX.

~~~
outworlder
The industry appears to be very risk-averse (even though they have their
reasons), so I'd be surprised to see a modern, functional language being used
for critical tasks. However, C++ sometimes is used for those tasks, which
boggles the mind.

Then again, that might just be a wrong impression I got from reading, among
other things, "Lisping at JPL" ([http://www.flownet.com/gat/jpl-
lisp.html](http://www.flownet.com/gat/jpl-lisp.html))

~~~
maaku
> However, C++ sometimes is used for those tasks, which boggles the mind.

That's because the people making such decisions are not the ones qualified to
do so (see the Lisping at JPL article you linked to). Their logic is probably
along the lines of "if it's good enough for industry, ..." \-- that's not an
ellipsis, it is literally as far as the thought probably went.

Source: I worked at NASA for 4 years.

~~~
laureny
C++ is still a very big step forward compared to Fortran, Algol or Ada.

~~~
evincarofautumn
I was under the impression that Ada is significantly safer than C++,
particularly with the 2012 standard that adds support for contracts, and that
it mainly suffers from a lack of tooling.

~~~
taeric
The elephant in the room there is the importance of tooling. Consider,
explosions and explosive chemicals are themselves ridiculously dangerous and
for the most part to be avoided. However, with the proper tooling and use, one
can use them to accomplish some fairly amazing things.

That is, many of the "safer" languages try to get by with disallowing
explosive and dangerous constructs. With some of the more advanced tooling
that is available and good discipline, one can still do some amazing things
with them.

------
dangirsh
Very cool to see Haskell being used more in the space space. I've recently
built a command line tool in Haskell with a subset of these features for my
team's
testing:[https://github.com/dangirsh/Auto](https://github.com/dangirsh/Auto)

I agree that the existing tools are pretty limited

