Haskell's effect on my C++ : exploit the type system
vallettaventures.com
vallettaventures.com
> Although the newcomer C# was only introduced ten years
> ago, there is little in the language to reflect the
> thirty years of research into programming languages since
> C++ was introduced.
Not a huge fan of C#, but he couldn't have picked a worse example for language stagnation.The people that develop F# and those who develop a large part of Haskell drink coffee from the same coffee machine.
struct Rectangle { double x,y, width, length; };
int main() { Rectangle r = {0., 0., 10., 10. }; return 0; }
This even compiles in C++03. If you really insist on private members, you will need initializer lists and some changed member names.
The small example error has been worked around with for years by simply placing the constant on the left side of the comparison. Not pretty, but effective.
I agree with the general point, though. A functional style can make C++ much more efficient. It is also the only way to do template meta-programming and everybody who tries to go into that direction can benefit from learning Haskell.
http://blog.garillot.net/post/15147165154/every-single-fresh...
In C++11, the language is unaltered from the perspective of a PL researcher, its type systems unchanged, but wildly improved from the perspective of a programmer. Initializer lists, auto, lambdas, and for-each loops were added, all of which make the language more compact in ways which most programmers will appreciate. Futures will do for multi-threading in C++ what RAII did for resource management.
Programmers are not computer scientists. From the perspective a programmer, the big three languages -- C++, Java, Python -- are quite alive and changing in ways which allow us to write better, more interesting programs. Even if they are built on thirty year old type systems.
It is a joy to use to.
[1]: http://stackoverflow.com/questions/2349378/new-programming-j...
To the point that, I've started writing several libraries that enable in C++ several of the features I miss most in Haskell. In particular, I've missed the natural syntax of higher order functions, e.g., currying and functional composition, as well as the pithy expressiveness of Prelude vs the STL. With C++11, we can have these things, and while Boost gives us a wealth of functionality, it's hardly the most natural (and lightweight) library.
fc (functional composition) (https://github.com/jdduke/fc) and fpcpp (functional programming with C++)(https://github.com/jdduke/fpcpp) are my recent (very much work-in-progress) efforts towards this end.
With fc, one can naturally compose functions, lambdas and function objects, e.g., given composable functions f, g, h, write auto fgh = f + g + h; or, auto fgh = compose(f, compose (h,g));, and then call it as fgh(a,b,c);
fpcpp enables syntax like
let pi = [](unsigned samples) -> double {
typedef std::pair<double,double> point;
let dxs = map([](const point& p) { return p.first*p.first + p.second*p.second; },
zip(take(samples, rand_range(-1.0,1.0)),
take(samples, rand_range(-1.0,1.0))));
return 4.0 * filter([](double d) { return d <= 1.0; }, dxs).size() / dxs.size();
}
EXPECT_NEAR(pi(10000), 3.14);
In writing fc and fpcpp, I've actually become less and less concerned for the future of C++; having used a good number of the C++11 features available, I've regained a good deal of my passion for C++ programming. Really, C++ needs a more expansive and natural standard library, iterators are powerful but are NOT the answer.Part of the beauty of Haskell is the aggressive effort to define each function on the most general type it applies to (and then specialize to concrete types during compilation, a la C++, when performance is requested via a pragma), leading to a rich hierarchy of tiny type classes (type classes are similar to C++ abstract classes)
int main() {
string s[] = { "a", "b", "c" };
// will print "abc"
cout << accumulate(s, s+3, string(""), [](string l, string r) { return l+r; });
return 0;
}
I think a closer C++ equivalent for typeclasses are specialized template functions--for example, if I define: template<InputIterator> string accumulate<string>(InputIterator first,
InputIterator last, string init);
then my call to accumulate(s, s+3, string("")) will use that specialization.> if (a < 0) a = 0;
> has been replaced with
> const int a = MAX(0,f());
Going by MAX capitalization, it must be a macro, in which case the code is wrong. I am guessing this is a synthetic example, but still - try and get the basics right.
#define max(a,b) \
({ typeof (a) _a = (a); \
typeof (b) _b = (b); \
_a > _b ? _a : _b; })Of course, people still misuse macros.
If it's a case where you can retrieve the value essentially free w/o side effects (as opposed to the result of a computation), one idiomatic alternative is:
const int a = foo >= 0 ? foo : 0; int a = f();
if (a < 0) a = 0;
has been replaced with
const int a = MAX(0,f());
I believe the latter will generally result in 2 calls to f() (unless it returns less than zero), which could cause undesired behavior or just be inefficient. Of course, it depends on what f() does.Assuming MAX(0,f()) expands to something like this:
if( f() < 0 )
a = 0
else
a = f() template<class T>
const T& max(const T& a, const T& b) {
return a < b ? b : a;
}
template<class T, class Predicate>
const T& max(const T& a, const T& b, Predicate p) {
return p(a, b) ? b : a;
}
And your definition of MAX() is wrong, not just because of multiple evaluation of the arguments. The “conventionally wrong” MAX() would be this: #define MAX(a, b) ((a) < (b) ? (b) : (a))That said, for me, your code would be clear, as not being in all caps, I would assume it's a function and therefore doesn't have the same weakness.
As someone else pointed out, there's no way you can look at MAX(0,f()) and know that it's enforced that f() is only used once within the macro.
C++11's strongly-typed enums look like:
enum class Enumeration { Val1, Val2};
enum class Enum2 : unsigned int {Val1, Val2};
A strongly-typed typedef could use similar "declare a class deriving from a native type" syntax: class Celcius : double; // something like this?
typedef Fahrenheit : double; // or maybe this?
Celcius celcius = 0f;
Fahrenheit fahrenheit = celcius; // ERRORSo if you want to grow as an engineer and computer scientist, learn these languages. You'll learn a lot fast, and not just about one programming paradigm (FP) but about complexity and how to manage it. You'll just learn a lot more about software because the way to learn software is to write it and one person can actually accomplish significant things in these languages. Java and C++ demote the individual contributor: "commodity" developers crank out classes and a more senior "architect" figures out how to bolt them together.
That said, these languages won't improve your performance in typical Java and C++ jobs. Six months of practice for exposure might give you some new ideas and make your code and performance better. Beyond that, they might diminish it. Why? Because you start hating these languages (Java and C++) and the accidental complexity they throw at you, which starts to look foreign and unnecessary. You get to a point where your own code (if you use the idioms of these languages, and you probably should, because it's not just about you) starts to look like someone else's. When you start hating your own code, disengagement sets in and it gets ugly fast. I can "hack" Java and C++ but I certainly wouldn't do a major project in them at this point.
My Java and C++ are uninspired now that I know better languages. Often, the only way I can write decent Java is to write it first in Scala (which I like, a lot) and transliterate the solution. Whether this produces "good" Java I don't even know.
Also, I really believe that C++ is the 21st-century COBOL. Java was gunning for that distinction, but Scala and Clojure gave it an assembly-like status, as the lowest-level language on the JVM (which will remain important for at least 15 years because of Scala and Clojure).
There are very few people who have just learnt functional programming. I have met a few in academia, and they tend to be as "bad" as the pure Java/C++ programmers, but in a different way. They have trouble understanding how their code will boil down to CPU instructions, and so have trouble with performance, particularly (unsuprisingly) mutating algorithms, which are vital for performance in some situations.
After I have defined a high-performance algorithm, they often want to modify it to make it "more beautiful" (which means functional) and in the process destroy the performance.
Also, I had quite a lot of exposure to Haskell, and I haven't found C++ hate. I got some Haskell hate, because doing some things (mutating algorithms and bit fiddling) were really, really hard, while I found most of the things I loved from Haskell I could do in C++, with care.
I work on low-level algorithm design in A.I., where performance is everything and there are many interlocking, low-level and complex algorithms and data structures. I know that other problems might well have very different characteristics. For example, I have no real ideal how Haskell vs C++ would compare for the average web app for example (or of course, something like Ruby/Python which seem to be much more popular).
After I have defined a high-performance algorithm, they often want to modify it to make it "more beautiful" (which means functional) and in the process destroy the performance.
That's annoying. They should understand the concept of interface as separate from implementation. Implementations are allowed to be (and sometimes should be) ugly if performance concerns merit it.
Besides, mutable state, intelligently used, isn't "ugly". A lot of programs are simpler and more attractive when mutable state is used. Mutable state just needs to be used with taste.
What really pisses me off about that behavior pattern is when the tinkering is unnecessary. If it's working code, then who cares? I'm a hard-core FP advocate but I write stateful programs all the time. I try to wrap them behind interfaces that are as functional as possible. I will use state in controlled ways when appropriate; I just don't want to impose it on other people.
I work on low-level algorithm design in A.I., where performance is everything and there are many interlocking, low-level and complex algorithms and data structures.
I think what makes C++ appropriate for your purposes is that once code is written to spec, it doesn't need to be maintained (except for performance optimizations). I'm guessing that (except possibly for some STL usage) you're actually C.
Where C++ and Java really drop the ball (legacy disasters) is when code has to be read more times than it is written. If your goal is to write fast software one time and, once it's working code, never need to look at it again, C++ is probably appropriate.
One small comment - what makes C++ appropriate for my usage is mainly templates. Lots and lots of templates.
It is very useful to be able to easily compile an algorithm for different types. If compiling a whole 10,000+ line algorithm 4 times for char/short/int/long inputs with templates and doing run-time branching between the algorithms (but not in the algorithms) saves 15% CPU time, then that is well worth the work. I don't know (well) any other language that offers that. Obviously if you introduce some kind of abstract base class / interface like Java, you will immediately lose the benefit of going int -> short -> char, when a class is alway at least... 12 bytes? 16?)
In C doing that requires a lot of macros and rapidly gets unusable, in Java it requires introducing lots of interfaces and hoping things get inlined.
What I like about C++ (compared again to Java/C, not Haskell), is that I can without fear of cost (other than compile time) introduce another layer of abstraction. When benchmarking code this is amazing, I can easily swap in a vector, or a deque, or an optimised fixed-size stack array.
In Java it is common for people to 'un-Java' code, introducing arrays of primitives instead of arrays of classes for example.
I don't know how well Haskel does at that kind of thing.
Have you looked at Ocaml and Scala? Those might be close enough to suit your needs, and I think it's only a matter of time before Scala (or something like it) starts outperforming C++ on parallel problems.
What I'll say for C++ is that, in the style of development you're doing, it's definitely faster. The problem I have with C++ is all the use of it that occurs where it's not appropriate and actually makes performance worse because per unit time, programmers can accomplish less in it.
It's clearly possible for expert programmers to optimize the hell out of C++ in a way that just doesn't exist in GC languages, but I don't think average-case code is better, especially if it's framed as an economic problem and programmers of equivalent skill are involved and given the exact same amount of time to do it. Under those constraints, for a large problem, I would bet on Ocaml and Scala winning just because the programmers would have more time to spend on optimization. For a small problem, though, I'd bet on C++.
In particular, the key feature of C++ templates (as opposed to other similar systems of polymorphism) is that they abstract over not just code but data representation. This is what CJefferson was getting at and it's orthogonal to macros. Ocaml and Scala don't offer it. It's about data, not code, so you'll never be able to gloss over it with extra parallelism.
In C++ a std::pair<double,double> really is just two doubles, 16 bytes. It can be passed into and returned from functions in registers. If I have a singly-linked list of them, each list cell is 24 bytes (16 for the pair, 8 for the next pointer.) In any other (mainstream) language, the pair is really a pair of pointers to boxed doubles which reside in the heap, and the list cell holds a pointer to that. The overhead of this is unacceptable in many situations, and these situations are where C++ still thrives. (cf. the Eigen linear algebra library for an example of "doing it right.")
Most languages have given up on this feature because of the drawbacks (code bloat) and the unified runtime representation of data (basically everything is a void*) makes many things easier (garbage collection, serialization, etc.) (I'm not super familiar with C# but I understand the struct types address this somewhat, but not without their own drawbacks.) But thanks to LLVM (written in C++ btw) people are pushing this area of language development forward (Rust, Deca, and others) in an attempt to fix the (many) problems of C++ while preserving its strengths.
Oh and BTW: A tank with wings is called an A-10 warthog :)
It is certainly plausible. But a few things need to change
(i) JVM applications tend to be memory heavy, even if you can approach 75% of the run time speed of a C or a C++ application you would typically need a lot more memory. In my expeience it has been 8 times or more, YMMV.
(ii) The other problem is that all the exciting native SIMD instructions are mostly out of reach. This can be a drag especially when your code uses log and exponentiation a lot. Without SIMD they are terribly expensive.
(iii) The kind of things that I use C++ for is heavy in array operations, in particular manipulating sparse arrays. In these applications the loop bounds are not constant and it is impossible to ensure at compile time that the array indexing will not go out of bounds. There Java's runtime bounds checking does slow it down. A way to override the safety would help.
(iv) The other good thing about C++ is the ability to delay evaluation via the use of templates, I am talking about expression templates. Its not pretty by any one's standards but with suitable libraries you can get the job done.
> I don't think any Lisps are fast enough to be interesting to you
Under favorable circumstances Lisps can easily give C++ a run for its money, even on numeric code. For that take a look at Stalin. It's unbelieveable how much one can optimize, even without type annotations.
There are few new curly brace languages out there, that are trying to simplify generic programming. D is the more mature among these and the new exciting ones are Clay, Deca and Rust. All have been discussed on HN. Exciting times ahead.
A multithreaded runtime for OCaMl will be realy cool. I do not need that threads be surfaced as an API but the possibility that parallelism exposed by the functional style can be exploited by the runtime (perhaps based on options used to start the runtime).
Yes there is F# but mono I hear is not that great on Linux, though I have not tried it myself.
OCaMl / Stalin: An old and civil discussion https://groups.google.com/group/comp.lang.scheme/browse_thre...
Clay: http://claylabs.com/clay/ and recently discussed here http://news.ycombinator.com/item?id=3474722
Deca: discussed here http://news.ycombinator.com/item?id=3413936 and here http://news.ycombinator.com/item?id=3413936
Rust and D I think do not need championing :)
This. I would love to see a multithreaded Ocaml.
What a great metaphor. Hilarious, great imagery, and it's even mostly true!
Thanks for that, I'll remember it.
I think that is apt for C++, too. It is not sexy as in 'objects all the way down', 'functions all the way down', 'consign all the way down', or whatever else rocks one's boat, but it is sturdy and does get the job done.