tags:

views:

342

answers:

3

Hi

If you had the possibility of having an application that would use both Haskell and C++. What layers would you let Haskell-managed and what layers would you let C++-managed ?

Has any one ever done such an association, (surely) ?

(the Haskell site tells it's really easy because Haskell has a mode where it can be compiled in C by gcc)

At first I think I would keep all I/O operations in the C++ layers. As well as GUI management.

It is pretty vague a question, but as I am planning to learn Haskell, I was thinking about delegating some work to Haskell-code (I learn in actually coding), and I want to choose some part where I will see Haskell benefits.

+3  A: 

I have never mixed both languages but your approach feels a little upside down to me. Haskell is more apt at high-level operations while C++ can be optimized and can be most beneficial for tight loops and other performance critical code.

One of the largest benefits of Haskell is the encapsulation of IO into monads. As long as this IO isn't time critical I don't see any reason to do it in C++.

For the GUI part you are probably right. There is a plethora of Haskell GUI libraries but C++ has powerful tools such as QtCreator which simplify the tedious tasks a lot.

pmr
No, it doesn't seem upside down to you. You are clearly saying: Use Haskell for everything. Except maybe GUI and time critical operations. That's the reason you don't understand I may mix the two languages. There's a little reason that keep me in the C++ island ( though I have not learnt Haskell enough to do any comparisons), with C++ I still feel what's happening behind, speaking about hardware operation I mean.
Stephane Rolland
I don't think pmr is saying that. You asked what the relative strengths are - and pmr's answer is pretty accurate. If you feel more comfortable with an imperative style, that's fine. But it's your own feeling, not a comparison of what the languages are good at, which is what you originally asked for.
Yitz
Yeah it's a feeling, I completely agree with that, all the more so as I don't know Haskell enough. (i made pmr +1)
Stephane Rolland
+6  A: 

The benefit of Haskell is the powerful abstractions it allows you to use. You're not thinking in terms of ones and zeros and addresses and registers but computations and type properties and continuations.

The benefit of C++ is how tightly you can optimize it when necessary. You aren't thinking about high-minded monads, arrows, partial application, and composing pure functions: with C++, you can get right down to the bare metal!

There's tension between these two statements. In his paper “Structured Programming with go to statements,” Donald Knuth wrote

I have felt for a long time that a talent for programming consists largely of the ability to switch readily from microscopic to macroscopic views of things, i.e., to change levels of abstraction fluently.

Knowing how to use Haskell and C++ but also how and when to combine them well will knock down all sorts of problems.

The last big project I wrote that used FFI involved using an in-house radar modeling library written in C. Reimplementing it would have been silly, and expressing the high-level logic of the rest of the application would have been a pain. I kept the “brains” of it in Haskell and called the C library when I needed it.

You're wanting to do this as an exercise, so I'd recommend the same approach: write the smarts in Haskell. Shackling Haskell as a slave to C++ will probably end up frustrating you or making you feel as though you wasted your time. Use each language where its strengths lie.

Greg Bacon
Thanx for inverting my position. Haskell should call C++. I will try this. But it also means then that the core of the application is Haskell driven. So you are meaning that the object, functionnal, and template programming avaiable in C++ is less flexible than the possibilities offered by Haskel? Haskel will tightly stick to the fundamentals flows and logics of the application I may build ? is that what you mean?
Stephane Rolland
And thanx for Knuth's article, (fundamentals never hurt). Though it's not freely available through ACM ( well I have no account there), it can be download at CiteSeerx
Stephane Rolland
This is really quite wrong. You can optimize C++ tightly when necessary- but you absolutely can think in terms of lists, garbage collection and pattern matching in C++. You're thinking of C.
DeadMG
@DeadMG that's what I am trying to understand. At what extent do they feel like the C++ high-level abstractions may be less competitive compared to Haskell high-level abstraction. But something in what have read gives me a clue: the shortness of code. It's is claimed that Functionnal code may be shorter by a factor of 2 to 10. Even more with Erlang language.
Stephane Rolland
C++ is not noted for being particularly concise, it's true. But then, if you want to use Haskell/C++, you will have to deal with all the interop, which isn't exactly going to be trivial between two pretty unrelated languages. As for 2 to 10? I don't think it'll be THAT much.
DeadMG
@DeadMG I chose poor examples and only hinted at the elegance dimension, but please don't take me as bashing C++. The language has its definite strengths.
Greg Bacon
@DeadMG don't worry man, I am a C++ fan. But 5 years ago learning OzML had open my views, and later on I became much more well at ease with c++ functors and forthcoming c++0x lambdas. I also had been convinced by some researcher in music/maths ( Guerino Mazzola in "The Topos of Music") about the deep strength of the mathematical theory of Categories; it uses a concept called Monads, which seems to be implemented in Haskell. I want to understand this concept. I am sure I want to learn Haskell. Ofcourse interop things may be tedious in the long run, but I am still thinking to give a try.
Stephane Rolland
@Stephane The language we use [shapes our thinking](http://en.wikipedia.org/wiki/Linguistic_relativity). The imperative roots of C++ tend to bog down a program's expression in the how, and [patterns are signs of weakness in programming languages.](http://blog.plover.com/prog/design-patterns.html) Moving in the declarative direction, the scaffolding disappears, leaving the essence, which is easy to reason about. Of course, it's still possible to write beautiful programs in C++ and ugly ones in Haskell, but learning multiple paradigms will make you a better programmer.
Greg Bacon
@Stephane Rolland: If you want to learn Haskell, then go learn Haskell, then come back and ask the question. There's a difference between "How can I learn Haskell?" and "How can I interop Haskell and C++?". @gbacon: I didn't intend to suggest that you were bashing C++, merely that I felt that you gave an unfair view of it's high-level facilities, and I certainly agree that multiple paradigm is stronger.
DeadMG
@DeadMG I'm gonna learn Haskell interoping with C++. For example, I can't imagine working with multiple audio samples arrays @ 44,100Hz with Haskell as easy as in C++. But dealing with high-level abstract musical structures... well I wanted to know where I can experiment Haskell with its strength and test them. I think it's worth trying there.
Stephane Rolland
@gbacon how do you include html links in comments ? same way as in answers ?
Stephane Rolland
@gbacon many thanx for the link about linguistic relativity.
Stephane Rolland
@gbacon I will need some more time with the second link as I am a fellow follower of the Design Patterns in C++ :-) I need some time to swallow :-)))
Stephane Rolland
@Stephane Include a link in a comment as an inline Markdown link, *i.e.*, **[** *link-text* **](** *link-url* **)**
Greg Bacon
+2  A: 

This answer is more a story than a comprehensive answer, but I used a mix of Haskell, Python and C++ for my dissertation in computational linguistics, as well as several C and Java tools that I didn't write. I found it simplest to run everything as a separate process, using Python as glue code to start the Haskell, C++ and Java programs.

The C++ was a fairly simple, tight loop that counted feature occurrences. Basically all it did was math and simple I/O. I actually controlled options by having the Python glue code write out a header full of #defines and recompiling. Kind of hacky, but it worked.

The Haskell was all the intermediate processing: taking the complex output from the various C and Java parsers that I used, filtering extraneous data, and transforming it the simple format the C++ code expected. Then I took the C++ output and transformed it into LaTeX markup (among other formats).

This is an area that you would expect Python to be strong, but I found that Haskell makes manipulation of complex structures easier; Python is probably better for simple line-to-line transformations, but I was slicing and dicing parse trees and I found that I forgot the input and output types when I wrote code in Python.

Since I was using Haskell a lot like a more-structured scripting language, I ended up writing a few file I/O utilities, but beyond that, the built in libraries for tree and list manipulation sufficed.

In summary, if you have a problem like mine, I would suggest C++ for the memory-constrained, speed-critical part, Haskell for the high-level transformations, and Python to run it all.

Nathan Sanders
I had left Python for only basic scripting/system-admin features, and I went back to C++ only for applications. I may think about it with your advice. Wasnt it too much a mess dealing with three languages ? I think you used the python C low level interface, calling C++, then using Haskell with FFi as one of the SO users pointed me at... but did the whole melt well in one whole ? I mean without too much glue, or boilerplate code to match the connections ? Did you felt that your code was clean ?
Stephane Rolland
My program was a batch operation, with no GUI, so C++ and Haskell didn't talk to each other at all, except through files. I started them both using Python's subprocess.Popen so I could maximise use of the cores on my machine. So Python didn't really talk to the others either. BUT I found I didn't need much C++ either--only the central, most memory-hogging algorithm. If you need a lot of C++, or you need close co-operation between the pieces of your program, then my method may not work well.
Nathan Sanders
@Nathan, thanx for the clarification ;-)
Stephane Rolland