Search
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
does not. Better typechecking, generics, better metaprogramming support, etc.
At any rate, I checked in the employee database...no, you don't work here. Unless you post to ltu under an assumed name. :)
The concern about memory is real in some very small applications. C++ generates a lot of structures (e.g. for exceptions and RTTI)
which are still "there" even if you don't use them. If the compiler lets you turn these off, then this isn't such a problem, but then
you're using a C++ subset, not C++ proper.
There are a few concerns which are specific to library vendors which may apply:
Some platforms still have binary compatability issues, such as differing object/vtable layout and symbol mangling.
Upgrading libraries without requiring clients to be recompiled is harder to do in C++, and sometimes platform-dependent.
More languages provide easy ways to bind to C libraries than to bind to C++ libraries. (This is more of a concern in the open
source world.)
Apart from that, I agree with you. I don't know why most people would prefer C to C++ in this day and age, especially when you
consider that most C++ compilers are really C++ compilers with features turned off. Even if you only use C++ as "a better C", you still
win.
ABI Compatibility...
... is a real issue. While it's true that GCC 3.x is available for many platforms, in one form or another, we do have to concern
ourselves with link-time compatibility for our customers, who often aren't willing to migrate away from whatever compiler/library
combination the embedded device vendor is hawking.
In any case, having kvetched about it, I should mention that we do, in fact, still code in C++ even on our embedded devices. We just
don't enjoy it very much. :-)
C vs C++
I would usually choose C over C++. My reasons are:
I only ever use C for very small performance critical pieces of code, or to interface with specific libraries etc. Typically, C++
doesn't really add much here.
From my experience, C++ is a lot less portable than C. What I mean by this is that I have so far been unable to write any C++
code which managed to compile cleanly first time on 2 different platforms. Use of the STL seems to be the kiss of death in this
regard. Perhaps this is my own incompetence, but no other language I've used has been this tricky to port.
Related to the above: the STL seems to be single biggest reason for preferring C++ over C for the work I do. However, I rarely
program without having a Tcl library present, so I can get most of what I need from that (e.g. hashtables, dynamic Unicode-
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
aware strings, event loop, simple sockets, GUI, etc), and get a nice, compact configuration language for free!
Complexity: as mentioned above, my code is generally structured as small packages which are loaded into a Tcl interpreter to
be tied together. For these sort of small pieces of code, C++ just adds complexity without really giving much gain. C++ is a
much more complicated language than C.
In short: I'm trying to minimise the amount of code I have to write in low-level languages. C++ seems like more of a win if you're
writing most, if not all, of your code in C++. That's a situation I'd like to avoid. I'd benefit more, I think, from a nice functional
language (e.g. Ocaml, Haskell) with garbage collection, pattern matching, etc. However, I haven't had much luck integrating such a
language with Tcl, unfortunately.
Last time I tried, it was building dynamic libraries, suitable for [load]ing into the Tcl interpreter. You can build a static
customized tclsh, with whatever Haskell etc code baked in, but I'd prefer to not have to do that. I came across some code for
linking (byte-code compiled) OCaml to Perl, and was going to try and adapt it, but so far haven't got round to it. I notice that
GHC now has some preliminary support for dynamic libraries [1], so now might be the time to revisit this (although, I might
wait for it to mature a bit first).
Also, why are you mainly writing stuff using Tcl, if I may ask?
Plenty of reasons. Mostly, for the implementation/libraries, which are really very good. It's also pretty good at meta-
programming, but prefers using normal (runtime) procedures over Lisp-style macros, and defers interpretation of arguments
to individual commands, which avoids some quoting issues. (e.g., there are no special forms in Tcl, beyond the minimal
syntax defined in Tcl.n man page). There are warts, and I would personally like to see it move in a more Scheme-like
direction (Tcl 8.5/9.0 are doing so, in some ways, with big integers coming in 8.5, some proposals for lambda, and even
some preliminary talk of things like closures and continuations on the wiki), while maintaining some of it's more unique
aspects.
I sometimes gaze longingly at some particularly elegant piece of Haskell, but mainly as a means of replacing C, rather than
Tcl. My ideal language would somehow be able to scale between Tcl's very hands-off approach (interpretation as a technique
of last resort, almost), and Haskell's elegant type system.
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Tcl library docs
Tcl is a C library. The docs for the functions it exports are at http://www.tcl.tk/man/tcl8.5/TclLib/contents.htm. There
are more docs scattered around the wiki, e.g. this page on C++. The sample chapters from Practical Programming in
Tcl/Tk should be useful too.
You can either embed Tcl in your program, or you can restructure your program to be a set of Tcl-loadable packages and use
tclsh (or wish, or tclkit...) as your "main". The latter is generally the preferred approach these days, for various reasons.
Thanks.
Thanks.
Scoping. When I see some code referring to "foo", is it a member variable, or a global (or static or class) variable? Or does it
belong to some superclass? I keep writing "this->" in front of everything, just so I can keep track of it. ("But you don't need to
do that!" "I know; I want to; I wish you'd do it, too.") Yes, there are naming conventions, but they're often not followed
(especially in the older and cruftier bits of the code base), and they're different in every code base, anyway. You can do
horrible things in C with macros, but everyone knows that you use ALL_CAPS unless you're damn sure that you've made them
behave like normal variables and/or functions.
Auto-typedef-ing. The single biggest wart in the C grammar is the type name syntax and "typedef" introducing an unbounded
number of new keywords. So C++ goes and makes it worse. I like writing "struct" in front of everything. It tells me that this
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
object has complex internal state. I like writing "class" in front of everything, too, which attracts the same comments as the
above. I only use typedef when I really need an arithmetic type and I really can't use a built-in one. Indeed, in C, I often write
"foo_frob(struct foo *foo, arg1, arg2, ...)", which drives C++ programmers crazy.
The syntax. In the 90s, I remember people wondering if it was possible to parse C++ unambiguously, or if there was an
unresolvable corner case. Maybe X3J16 solved that, but it can still be a remarkable pain, especially combined with the above
two issues. By fitting into the corners of C, already a fairly densely-encoded language, it's possible to approach Perl 5 regular
expressions for clarity. And some people seem to like writing that way.
Now, it is well known that "there is no language in which it is the slightest bit difficult to write bad code", but once I've stripped a
piece of code down to the few places where I actually need virtual functions or whatever, the overhead of creating a vtable by hand in
C is generally negligible, and is dwarfed by the comments explaining why it's needed and what the invariants are, anyway.
Now, automatic constructors and destructors are rather handy, and for some mathematical-type code, operator overloading is a
wonderful syntax convenience, but if I'm doing high-level programming, I'll use a high-level language.
But other than that, the only thing I find worthwhile about C++ is the STL. Which is a very good thing, but the cost at which it was
bought... now J. Random Cruftycoder can obfuscate his wierd, special-purpose and buggy data structure by wrapping it in template
syntax and feel virtuous that he's writing "reusable code".
I don't deny that C++ is a powerful tool, able to span from low to fairly high levels, but I have seen it in the hands of idiots too often.
Embedded code
On top of my comments above, for embedded programming, I have to think hard about every single allocation: "what do I do if it
fails?" (There's an important exception for startup code, before anything data-dependent happens.)
Thus, I hate implicit allocation. I have a strictly finite amount of RAM available, and that's it. I preallocate when possible, and cleanly
back out when not.
It's not a case of "tight" memory - I usually have over a megabyte - but the fact that I can't just print "out of memory" and return to
the shell.
Not to mention, I have to provide the run-time, as well. Remote fimware upgrade is always fun, particularly when you don't have
enough non-volatile storage for two full copies. A nice simple run-time has advantages there.
Ada
I am not sure what your requirements are, but I suggest checking out Ada.
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Objective-C
The only systems which made wide-spread use of Objective-C appear to be Next and now Apple, but it is quite elegant and useful. Portable
too, as part GCC. The GnuStep framework is available for Linux and Windows, and Apple has open-sourced their lower-level libraries for
writing portable Objective-C code.
It is a thin layer on top of C, providing Smalltalk-like behaviour. Being a dynamic runtime system, it is much easier to link to other dynamic
languages (Python, Lisp, Ruby, etc.) than C++ or even C.
IMHO...
Kkaa: For a long time I have been looking to replace C/C++ with something better, but what would be a good choice? What would be a better C++
than C++?
I believe Objective Caml to be a quite reasonable replacement for C++ for 90% of the domains that I've seen C++ applied within. O'Caml offers all
of the usual benefits of the ML family such as higher-order functions, currying and partial application, algebraic data types and pattern matching,
and Hindley-Milner type inferencing. It offers key benefits of imperative languages such as mutable references and arrays. It offers all of the usual
benefits of a class-based object-oriented language such as inclusion polymorphism, multiple inheritance, method overriding, and data
encapsulation. It offers an extremely powerful module system including modules that can be parameterized by other modules so that, e.g. a
"currency" module can parameterized by a "dollar" module implemented in terms of floats or a "euro" module, also implemented in terms of floats,
so that you can't accidentally mix operations on dollars and euros but there won't be any code duplication.
Unlike many advanced alternative languages, O'Caml doesn't fetishistically abstract away from the machine: integers' range is determined by the
machine word size, whether 32 or 64 bit. There are specific 32- and 64-bit integer sizes, and arrays of these or of float values are guaranteed to
be contiguous and unboxed for efficient numerical computing and/or compatibility with C/C++ memory layout. Being a member of the ML family,
O'Caml is garbage-collected, but its GC is an extremely efficient generation-scavenging collector. If you want arbitrary-precision numbers, you can
get them, from a standard module even, but it is a distinct module.
O'Caml has a reasonable set of standard libraries, including, of course, text and binary I/O, strings, lists, hash tables, etc. Some modules go
beyond the obvious: regular expressions, threads, sockets, graphics, lazy evaluation...
O'Caml excels as an environment for developing DSLs. You can go the traditional route and use ocamlyacc and ocamllex and write your semantic
actions by hand, or you can take advantage of the camlp4 preprocessor to build your front end. camlp4 and the O'Caml back-end are well-
integrated; if you write a new front-end with camlp4 the O'Caml compiler won't parse your code again; it will deal directly with the AST that your
parser hands it, all while retaining source coordinates for error-reporting purposes.
What really grabs me about O'Caml, though, is its attention to pure pragmatics: there is an interactive REPL (Read, Eval, Print Loop, called a
"toplevel" in O'Caml terminology), a bytecode compiler coupled with a GDB-like debugger that offers "time-travel debugging" so that you can
"undo" the execution of your program when it would be helpful, and a native-code compiler coupled with a profiler for those heavy micro-
optimization tasks. There are also several foreign-function interfaces to C/C++, and lots of people have used them: there are wrappers for
database APIs, OpenGL, crypto libraries, etc.
I can't emphasize enough how important this last point is. O'Caml is the only statically-typed language I know of that is competitive with
Lisp/Scheme/Smalltalk for fun, interactive development and C/C++ for the ability to deliver small, fast native-code binaries. Highly
recommended.
Certainly
GCC 3.4.3's support for the standard is quite good, and GCC 4.0's will be even better. Also, GCC 4.0's codegen for C++ is vastly improved over
GCC 3's. Either compiler does just fine with various C++ acid tests (Boost, Crypto++, etc.)
thanks!
thanks!
MLKit
James Hague: I'd appreciate pointers to high-level language implementations designed for embedded systems (I know about Lua, but that's a
"scripting" language by design, not something you'd want to write the bulk of your code in.)
How about MLKit?
"All memory allocation directives (both allocation and de-allocation) are inferred by the compiler, which uses a number of program analyses
concerning lifetimes and storage layout. The ML Kit is unique among ML implementations in this respect... Programmers who are interested
in real-time programming can exploit the absence of garbage collection: there are no interruptions of unbounded duration at runtime."
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
By Paul Snively at Fri, 2005-04-22 14:20 | login or register to post comments
We think occam is really best suited for programming robots, because robots always have to deal with lots of
stuff at the same time. Therefore, we've also made it possible to write occam programs to control the LEGO
Mindstorms. Give it a try; you'll love it.
As occam closely models the CSP calculus, we can write clean, concise, concurrent programs that can then be
executed anywhere---on Mac OS X, Windows, *NIX, on handhelds and clusters, and yes, little robots too.
Also, Danny Dub did BIT, "A Very Compact Scheme System for Embedded Applications", and with Marc Feeley, PICBIT, a Scheme
interpreter for PIC chips (also covered in Running Scheme on a PIC microcontroller).
I'm not sure about the availability of source code with any of these.
By Anton van Straaten at Sat, 2005-04-23 19:48 | login or register to post comments
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Scheme compiler for Lego Mindstorms
Here's a Scheme compiler for Lego Mindstorms, which comes with source in Scheme. The source is only 1570 lines -- the
compiler was apparently written by three students in three days. How hard could it be to retarget for another CPU? ;)
Of course, the Transterpreter is apparently written in C, so if the target CPU has a C compiler, then porting that ought to be
easier, if the source can be obtained.
By Anton van Straaten at Sat, 2005-04-23 20:54 | login or register to post comments
Source
Apologies for not keeping up on all-things-LtU!
I should say that our intentions are to release the source, but both of the primary authors of the Transterpreter (myself and
Christian) are within 6 months of writeup for our PhDs. Supporting a source code release is not, we don't think, a good idea at
this time.
We have the infrastructure in place (version control in a place we can create accounts, etc.), but are not ready for any of the
administrative overhead at this time. We won't let it bitrot, though, we promise.
Here is why:
As a professional programmer that earns his living coding C++ applications, here is why:
a) C++ is faster. No matter what you have heard, the overall speed of C++ applications (either real or perceived) is greater than that of other
languages.
b) Templates and generic programming without too much brain twisting. Templates and type safety is very important. If C++ haven't had
templates, it would not be used.
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
templates, it would not be used.
c) Direct interfacing with C and O/S routines.
d) Plethora of good libraries.
I think of greater importance is to ask why they don't program in other languages other than C++?
Here are my reasons:
a) Java and C# can't do many intelligent things that C++ can.
b) Having everything in classes does not always make sense.
c) Python and perl have weird syntax.
d) ML and Haskell have even more weird syntax.
e) The most important reason: it is not directly visible, from the available online resources, that what I can do with C++, I can do with
LISP/ML/Haskell/O'caml/etc etc.
This last reason is the most important one. My job is about doing desktop defense application programming (radar consoles, simulation systems
etc). Here is what I want a programming language to support:
1) signals and slots, for doing model-view-controller apps.
2) networking.
3) memory mapped data structures.
4) multitasking.
5) a rich GUI.
6) value classes.
7) operator overloading.
8) XML and databases.
9) clever and efficient data structures and algorithms support.
Let me tell you, after a two year search, what I have found:
Java does not support signals and slots, but a generic observer-observable pattern which is a pain in the ass to use. Java does not support
memory-mapped data structures, nor value classes, nor operator overloading. Java interfacing with C and OpenGL is weak, to say the least.
C# is not an option, because my apps must run on Linux as well as MS Windows.
Smalltalk has an ancient GUI and tens of different flavors.
I have searched for example desktop apps in LISP/ML/Haskell/OCaml that support the above, but I have found nothing. All I find is broken links,
100 ways to code the factorial function, insane amounts of work on how continuations are the best thing since sliced bread, etc etc.
In other words, it's not the elegance of a language that counts, but rather the most cliche of arguments: what one can do with a language, in the
shortest space and time.
So after all, I am back to C++. I am using Qt, which is the best piece of software ever written, for all practical purposes, and it covers all the
needs for the types of projects I want to do. I never do any memory management, I don't need garbage collection, I have direct support for signals
and slots (although not part of C++), a very rich GUI that actually makes sense, direct support for OpenGL, and all the goodies C++ offers me, like
operator overloading, value classes (you can't tell how valuable value classes are, until Seconds and Milliseconds get confused!used value classes to
separate them and provide implicit conversions between them!), smart and efficient data structures (no need to copy stuff over and over, as in
functional languages), etc etc...
Nanny, Nanny.
I see no need to enter into deep arguments about capabilities with someone who is ignorant of the
basics.
As best as I can tell, we are in a very shallow ravine that has little to no running water.
Personally, I think anyone that can program in C++ effectively is, by definition, pretty smart - you have to be smart to figure out
and keep track of all the intracacies. Me, I'm a dumb programmer who likes to not worry about a lot details simultaneously -
which is why I don't get along well with the likes of C or C++.
Really? I find that Java stops me from shooting myself in the foot with things like accidentally passing pointers to
short-lived char* buffers, while also preventing me from easily creating well-factored, generic code.
Library implementors do need a little bit more of what it takes to create something good in Java, to be sure. Doesn't
mean that it can't be done, or that once it has been, downloading and using a Java library is harder than
downloading and using a C++ library.
Somewhat relative
I do find Java programming easier because it has built in GC and doesn't use memory pointers. However, Java's
got its own set of warts. For example, how do you determine when a type should be constructed as a class
hierarchy (limited to single inheritance) and when should it be an interface (no code inheritance)? Whichever
path you choose, you'll be fighting it all the way along. Also there's the distinction between primitive types and
object types?
May just be me, but I find that all the programs I construct are heavily reliant on the construction of Types. Get
the types right and everything else falls into it place (somewhat akin to getting a database design correct). If I
have to fight the Type system, then I find myself in a pitched battle with my software, never really being
satisfied with the tradeoffs.
Objects vs primitives
Many people are bothered at the distinction between objects vs primitive types (int, boolean, double, etc.)
in Java; it is often suggested that a major deficiency of Java is that not everything is an object (as opposed
to Smalltalk, for example, where everything is).
In C++, the problem is (at first glance), worse. Not only are the primitives not objects, but the language
happily allows you to manufacture MORE things which aren't objects--unions, structs, enums, and
pointers/arrays to such. (We'll gloss over struct vs class for now).
However--the C++ approach seems more natural to me. Why? In Java, objects and non-objects have
differing semantics and (in general) cannot be interchanged. Intrinsics live "on the stack", whereas objects
live in the heap. One object cannot be embedded in another without a level of indirection. Pre Java 5, one
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
couldn't use a primitive in an object context without an explicit cast to a class such as Integer, Boolean, etc.
Primitive types cannot be aliased without wrapping them in a containing object of some sort.
In C++, on the other hand; the polymorphic objects, the primitive types, and the non-polymorphic
aggregations all exist on the same plane. One can use any type in C++ as a template paramter; or as the
basis for overloading. All types can be allocated in any memory region, or embedded "inline" within another
object. In short, whereas the primitives and the objects are segregated in Java; in C++ they are not--
something which I thinks makes C++ more expressive.
Of course, that comes at a price--the C++ solution permits references to out-of-scope objects to be created.
By allowing internal pointers, it makes implementing GC much more difficult.
Thoughts?
I don't really see the problem in making that decision. In Java, I think of all types as being interfaces in
principle classes are really about implementation. I think possibly the reason confusion arises is that when
a class will have a single interface that won't be used anywhere else, you can save time and keystrokes and
maybe some mental overhead by simply defining the class without an explicit interface. But in that case,
the class still has an implicit interface. You could define the interface explicitly, change all references to the
classname (where used as a variable type) to use the interface instead, and the design and semantics of the
program wouldn't change.
Whichever path you choose, you'll be fighting it all the way along.
The fighting happens because you can't reliably use subclassing for implementation inheritance when you
need to implement multiple interfaces. You either need to pretend Java is C and use lower-tech reuse
mechanisms, like composition and delegation; or use the new generics stuff, Java version permitting; or
use some kind of metaprogramming, like AOP. Either way, I agree it's a fight.
By Anton van Straaten at M on, 2005-04-25 19:23 | login or register to post comments
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Good Point...
...this led me to think a little bit about some systems that I feel sit at some kind of local maximum with respect
to how I seem to measure "quality" of implementation in Java. I came up with perennial favorite Hibernate, but
also, in a similar but possibly even simpler vein, SimpleORM and Bhavaya. I'm also reminded of ActiveMQ and
JGroups. What do I find in common among these systems?
On this last point I should say that I didn't think it could get any better than Hibernate on the Java persistence
front, but both SimpleORM and Bhavaya strive to do even better, even extending to eliminating Hibernate's XML
configuration files! The real shining example, though, is ActiveMQ. On one hand it's "just" a JMS 1.1
implementation. On the other, it plugs into any J2EE 1.4 container plus several other popular containers,
supports some eight different transports, three persistence frameworks, makes non-Java code and DHTML first-
class citizens in the messaging infrastructure, supports clustering and hot deployment, and supports dynamic
discovery. Oh, and it's open source.
Technically speaking, what these systems seem to have in common is getting extreme leverage from Java's
reflection, dynamic proxies, and in some cases the availability of high-quality bytecode-generation libraries to,
as transparently as possible, minimize both the cognitive distance between task and accomplishment and the
amount of repetitive, tedious, error-prone glue code often required to bridge task and accomplishment.
There's a lesson in here, as well as in systems like Ruby on Rails, for fans of statically-typed languages that lack
reflection and/or easy in-place code generation, IMHO. In my own thinking about having a "slider" from static to
dynamic typing, I need to remember that just having that slider isn't enough, but that the space on either side
of the knob needs programmatic access to the space on the other side in order for the full potential power of the
slider to be realized.
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
By Achilleas Margaritis at M on, 2005-04-25 19:52 | login or register to post comments
Anonymous attitude
it's called an array. You may have heard of this datastructure.
I have this theory that postings seem more collegial and less snarky when posted with less anonymity. (Not sure what
causes this perception; must be some deep human factor. ;-) )
Most of us here post with our real, full names as a sign that we are prepared to stand behind our opinions.
Might I recommend this practice to you, too, Marcin?
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
By Marcin Tustin at M on, 2005-04-25 23:19 | login or register to post comments
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
This will work just fine, will be faster than any hashtable, but will take more memory (unless, of course, you
normally have very many handles open at one time). Otherwise, it will have the same functionality as a hashtable,
for example it will still hold references to objects after they have become invalid.
Feeding trolls
It won't work on architectures where sizeof(int) == sizeof(size_t):
$ cat foo.c
#include <stdlib.h>
#include <limits.h>
I don't think java will handle allocting an array twice the size of virtual memory any better.
And even on architecturs where it works (like AMD64), allocating 8GB to hold a few thousend handles
seems excessive.
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Actually, I failed to take ac
Actually, I failed to take account of the fact that this would require 16Gb (Assuming that both java
references and ints are 32 bits).
It is true that it is rather annoying to have to write your own hashtable implementation for
integers, no matter how simple it is.
Misconception
smart and efficient data structures (no need to copy stuff over and over, as in functional languages), etc etc...
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
You might be interested in this posting (from this thread, respectively).
In any case, the idea that you have to copy excessively in FPLs is a gross misconception.
He is comparing paradigms
Cheat number 1: two different algorithms are compared, not two different programming languages.
He does not primarily compare languages, rather trade-offs of paradigms - which is a useful comparison to a certain extent.
You claimed that one needed to "copy stuff over and over" in FPLs and silently implied that this was a grossly inefficient approach.
The article shows by anecdote that (1) this statement is not true, and (2) that overuse of stateful data structures for alleged
efficiency can actually backfire big time.
The non-copying style of O'caml can easily be done with C++. But that implies the data are static, i.e. they
don't change.
Sure it can be done - though not that easily, because it is much harder to achieve properly without garbage collection.
But point is, it is not done in C++, because of the wide-spread naive believe in the superior efficiency of stateful data structures.
But that implies the data are static, i.e. they don't change.
But if you add elements to those data, your data will be copied. Whereas in C++, they won't. Let's say you
have a huge data set of 10000 records, and you want to sort it. The functional way will copy the 10000
record array again and again, whereas in C++ the sorting happens in place.
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
No, the "functional way" won't copy everything over and over again, simply because it wouldn't use arrays. ;-) For most tasks,
including sorting, there are efficient algorithms that do not rely on in-place update.
Besides, all functional languages provide means to implement imperative algorithms efficiently, e.g. by providing imperative arrays.
Some of them even manage to do this without loosing the nice properties of being "pure".
Informal summary
A tree is not an array ;0)
Wrong summary
That should have been: an array is not a tree. Ah, well ;-)
Neither is a list
but all three can be used in terms of each other. More specifically, an array is an indexed list. And a list or an array can
easily be represented as a tree. From an abstract data type standpoint, any of these forms can be used to store the
data, using accessors to hide the internal representation.
Well...
I agree! . . . . . Que?
From This...
...I can only conclude that O'Caml needs better marketing, especially if you believe that Qt is the best piece of software ever written. :-) And
aren't you conveniently ignoring that Qt requires the use of a preprocessor to overcome C++ deficiencies?
I've written quite a bit about O'Camlrecently, evenso I won't rehash it here. Suffice it to say that if you don't think it has good GUI support
albeit for GTK rather than Qt, "non-copying" data structures, "value classes," OpenGL support, etc. then you really should look more closely.
As for syntax, all syntax looks weird at first, and I definitely can't help you if you think O'Caml's syntax is even weirder than Perl's!
The only point that I think we agree on is that it would be nice to see G'Caml again, but truthfully I don't even find myself thinking about that
most of the time when I'm writing in O'Caml. On the other hand, I don't find myself thinking about it much when I'm writing in C++, either, so
maybe the take-away from that is that I don't do much scientific/numerical computing and therefore don't suffer much from not overloading
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
operators.
But all of this misses the main point, which in my case is that O'Caml continues to let me write correct code vastly faster than C++ does, and
generally speaking the performance of that correct code is within epsilon of the performance of the C++ code. Even when I have to tune the
O'Caml code, it's vastly easier to do that than it is to make an arbitrary body of broken C++ code correct.
It pains me, as basically a free-market absolutist, to have to say this, but given that I keep hearing the same essential mythology about
alternatives to C++ in general, and O'Caml in particular, I have to wonder whether the solution to the imbalance won't have to consist of
someone starting a software company that simply disallows the use of C++ in their products. I would actually rather take a group of
experienced programmers, in any language, and teach them O'Caml for, say, two months after hiring them, having them create no work
product during that time, than have them hit the ground running in C++. Unless I've been fortunate enough to hire a company of Meyers,
Alexandrescus, Sutters, and Parents, I pretty much guarantee you the team will still be more productive than a C++ team would.
Fair Enough...
Achilleas Margaritis: For all practical purposes, Qt is the best library there is.
I still maintain that "all practical purposes" is far too broad, but if Qt is working for you, that's great.
Achilleas: Qt needs a pre-processor, but only because Trolltech wanted to easily convert between text (.ui files) and code;
otherwise, they would have used templates.
Assuming templates existed when they started. I dunno. I've looked at Qt many times over the years and never came away
impressed. Obviously, your milage varies. :-)
Achilleas: I would be more than happy if you post some links with good O'caml libraries in the sections I referred above.
Happy to oblige:
2. networking.
If you mean basic IETF protocol stuff, I'd suggest ocamlnet. If you're referring to process group
communication in distributed systems, let me highly recommend Ensemble. If you're talking about
CORBA, try CamlORBit.
Please see the O'Caml standard library's Bigarray module. Look for the "map_file" function. That,
coupled with O'Caml's standard marshaling functions, might do. You might also find this post helpful.
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
4. multitasking.
I refer you to O'Caml's standard "Thread," "Condition," "Event," "Mutex," and "ThreadUnix" modules.
5. a rich GUI.
6. value classes.
This is addressed quite nicely with O'Caml's module language. The example they give is using floats
to implement currencies, but to have specific currencies, e.g. dollars and euros, be incompatible
types to avoid errors.
7. operator overloading.
We saw these, and more, in G'Caml, but so far it hasn't gotten past the prototype stage. I'm still
frankly not convinced that operator overloading is a big win.
Once again, where to begin? Baire perhaps, or ExtLib, or Okasaki's Purely-Functional Data
Structures? Maybe you just want a nice Trie implementation? Or perhaps you need some nice graph
theory support?
Shouldn't it tell you something that just by following links from the O'Caml site over lunchtime that I was able to address all of your
points, and that only the operator overloading response seems the least bit lacking? Doesn't it bother you at all that the multitasking
and memory-mapped data structures questions are answered within the context of O'Caml's standard libraries, and that the "rich GUI"
question is one link away and also refers to OpenGL?
It's stuff like this that persuades me that many (most?) people who claim to have "investigated alternatives" and "found them lacking"
are lying.
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
By Achilleas Margaritis at Tue, 2005-04-26 11:42 | login or register to post comments
Conclusions
Here is my thoughts on the material from links you have given me. Please allow for a margin of corrections, since I am not
familiar with O'Caml and I need to learn the language in parallel with the examples.
signals and slots, for doing model-view-controller apps. On the (un)reality of virtual types
The link you have given me presents a typical observer-observable pattern as found in Java. Here is what I think about it:
1) it's not signals and slots, because the interface between observers and obervables is fixed. I want to have a parameterized
interface, like in C# events or Qt signals and slots.
2) it's not something used throughout all other libraries.
3) Documentation is not formal. Is it a library or a research paper?
networking. If you mean basic IETF protocol stuff, I'd suggest ocamlnet. If you're referring to process group communication in
distributed systems, let me highly recommend Ensemble. If you're talking about CORBA, try CamlORBit.
Ocamlnet is a fine library, but it offers a subset capabilities of the Java SDK/Qt. CGI is not relevant anymore, since more
advanced technologies exist. The documentation quality differs wildly from part to part: some parts are documented with
ocamldoc, others aren't.
Ensemble seems a fine library if one wants to build a communication mechanism with different protocols, but I failed to see how
it is useful in practical applications: how do I do basic stuff like open a TCP/IP port, wait for connections, how to connect, errors
reporting etc. I have to say I did not understand much of Ensemble, but I suspect this is because it is an entirely different and
irrelevant domain from my line of work. Documentation of it is in PDF format.
Camlrobit seems like a good wrapper around Corba's Orbit, but the documentation is again in a different format.
memory mapped data structures. Please see the O'Caml standard library's Bigarray module. Look for the "map_file" function.
That, coupled with O'Caml's standard marshaling functions, might do. You might also find this post helpful.
I don't see anything related to memory mapped data structures. I see information on memory mapped files though, from a guy
that wants his app to have full state persistence. What I talked about is encoding of binary messages as data structures. ADA is
the best language for this, since developed from the start with that mentality, and C++ comes closely second.
LablGTK is quite nice, and also integrates nicely with LablGL.
LablGTK is a very nice wrapping around GTK, but the problem is GTK itself: Swing and Qt are much better toolkits. GTK does not
follow the model-view-controller paradigm (from what I know so far), and the LablGTK documentation is unfinished (and yet in
another HTML style).
value classes. This is addressed quite nicely with O'Caml's module language. The example they give is using floats to implement
currencies, but to have specific currencies, e.g. dollars and euros, be incompatible types to avoid errors.
Nice example, but how are ML modules any different than C++ classes? I can do in C++ the exact same thing.
operator overloading. We saw these, and more, in G'Caml, but so far it hasn't gotten past the prototype stage. I'm still frankly
not convinced that operator overloading is a big win.
Consider the following C++ code:
Vector r = a + b - c;
Not only it takes much longer to write, but it is not as elegant as the C++ version.
Now consider a two-page algorithm like a fighter trajectory computation, and you will see why operator overloading is really
important.
Operator overloading goes hand-in-hand with value classes: Why shouldn't the Currency type (in the context of an example above)
be used with operators? it's a number anyway.
XML and databases. Boy, where to begin? OCaml-MySQL, OCaml-Sqlite, OCamlBDB, or just go straight to OCamlODBC and talk to
any database with an ODBC driver? For XML, you can start with the expat wrapper or go whole-hog to PXP. If that's still not
enough, how about CDuce or, if a new O'Caml-derived language seems like cheating, just OCaml + CDuce, which just embeds
some critical CDuce features into the O'Caml 3.08.2 compiler?
Ocaml-mysSQL / Ocaml-ODBC: same api as C, even with the lowercase keywords, and yet another documentation style.
PXP: almost the same api as Qt: document class, node class, etc etc.
CDuce: extension language. As you said, it does not count.
clever and efficient data structures and algorithms support. Once again, where to begin? Baire perhaps, or ExtLib, or Okasaki's
Purely-Functional Data Structures? Maybe you just want a nice Trie implementation? Or perhaps you need some nice graph theory
support?
Baire: In the writer's own words: Baire is a data structures library for the Caml language. Please note that Baire is an ongoing
work, even if already quite usable. Not good enough for big commercial projects. Documentation is in French and non-existent.
ExtList: exactly the same stuff as Qt or JDK.
Shouldn't it tell you something that just by following links from the O'Caml site over lunchtime that I was able to address all of
your points
Unfortunately, you did not address any of my points. There are several drawbacks in all the above links that still prohibit me for
going to my boss and suggest O'caml.
Doesn't it bother you at all that the multitasking and memory-mapped data structures questions are answered within the context
of O'Caml's standard libraries, and that the "rich GUI" question is one link away and also refers to OpenGL?
What standard libraries? all I saw is a bunch of individuals, each one doing his own thing, possibly intersecting with other people's
areas (in data structures, for example). If you would like to call these libraries "O'caml standard libraries", then perhaps I should
call the entire Sourceforge catalog as C++ standard libraries, since many of the O'caml libraries are in Sourceforge, actually. I will
not do it though: C++ has only one standard library, the STL, and Java has the JDK.
It's stuff like this that persuades me that many (most?) people who claim to have "investigated alternatives" and "found them
lacking" are lying.
Ok, it's reality time. Since you accuse me of lying (rather politely), let me tell you that you have no knowledge of the
requirements for commercial projects like defense applications or Hospital information systems. These projects are never gonna
be done on development environments like the one you suggest. The O'caml language is fine, but the development environment
is not only the language: it's the libraries, the support, the documentation, the availabity of information. So here is reality, for
you:
From one side, we have mature environments like C++ and Java with a few number of libraries developed by corporations that
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
base their existence on these libraries (Trolltech, Sun) that cover most of our needs, with consistent architecture, consisent
documentation and huge support.
From the other hand, we have (mature ?) environments like O'caml, with lots of little libraries, spread over the web, with no
immediate visibility, with support left at the hand of individuals who code away on their free time, with various architectural
designs not compatible between themselves most of the time, with incosistently presented documentation.
Furthermore, O'caml libraries are not vastly different than C++ or Java libs. I would say that they are 90% similar.
Conclusion: which one would I choose to base my defense contract on? or my Hospital information system on? or my web store?
certainly not O'caml in the way you have presented. It's too much a risk for business environments.
I have searched for example desktop apps in LISP/ML/Haskell/OCaml that support the above, but I have found nothing. All I
find is broken links, 100 ways to code the factorial function, insane amounts of work on how continuations are the best
thing since sliced bread, etc etc.
And yes, the absence of Qt bindings for OCaml is a shame -- I personally prefer Gtk to Swing, though. Ymmv.
The standard library is documented on OCaml's website. You'll find low-level networking, concurrency,
marshalling/serialization, data structures...
As you write yourself, you are obviously not familiar with OCaml, which makes this whole discussion pointless. Obviously, for
any task, you will use the tools you know best and trust. For most tasks, you will use C++, the STL and Qt. For most tasks, I
would rather use OCaml, its standard library and Gtk. Note that I have experience with C++ and STL (and only toy experience
with Qt).
P.S. :
I still do not understand what you mean by "memory-mapped data structures". Unless perhaps you want to do some low-level
DMA transfers or something similar, I believe this is a standard case of marshalling.
Thanks!
As David said, I was (as always) reacting to overly-strong claims of "I couldn't find that for O'Caml," and my reaction can
be summed up as "then you didn't try very hard." Elsewhere I've already acknowledged the marketing difficulty related to
languages other than C/C++ in certain domains. With respect to the standard libraries, my point was that the threading
support and memory-mapped file support is included in O'Caml's standard libraries, so asking about them suggests that
you (Achilleas, not David) couldn't even be bothered to look at the standard libraries. etc.
People do use O'Caml in commercial production environments. Obviously, this isn't happening on a scale that looks
impressive from a pure numbers perspectiverather akin to Apple selling computers and operating systems compared to
Microsoft selling operating systemsbut it's plain ignorant to suggest that O'Caml isn't suited for production work.
Achilleas: Ok, it's reality time. Since you accuse me of lying (rather politely), let me tell you that you have no knowledge
of the requirements for commercial projects like defense applications or Hospital information systems. These projects
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
are never gonna be done on development environments like the one you suggest.
I'm glad that you feel I was polite. In fairness, I now see that your concerns aren't actually about available features or
libraries, but rather uncertainty about or disagreement with their relative level of maturity. I would argue that those
concerns are existential: if I find a third-party C++ library, I have just as much effort to vet it as I do an O'Caml library.
I'm just as (more, actually) concerned about quality of implementation, documentation, throughput, scalability...
Now let me tell you that if you think I've never done commercial projects like defense applications or hospital
information systems, you're wrong, and if you think those systems have always been done in C/C++ or even Java, you're
wrong again. Let me also tell you that if I ever find out I've been cared for in a hospital that uses a C/C++ system, I'll sue
for criminal negligence. There's no excuse for using such radically unsafe technology in an environment in which people's
health or even lives could be at stake.
[OT] Hospitals
I'm curious: aren't hospitals running computers and embedded CPUs on which OSes are written in C ?
Note that this is not a troll. Rather a question related to hardware/OS trust (which might be better suited for a
forum topic).
By Anton van Straaten at Tue, 2005-04-26 17:06 | login or register to post comments
I'll call
That was exactly the 75% tongue-in-cheek point, very much inspired by the
anti-Supers lawsuits in "The Incredibles." :-)
I'm apparently behind on the relevant "literature"! I'll have to go find a 5 year old mentor to help get
me up to speed.
Indeed, so I'd use this Overflow module and OcamlExc to check my code for
uncaught exceptions. :-)
Hmm. I must confess this gives me deja vu. The language has fast, unchecked integers because
people demand performance (as evidenced by the other thread about C-class performance). The
performance is one factor which drives its acceptance for some real applications. But then we need
to start retrofitting solutions to the problems which were created by the pursuit of performance.
Sound familiar?
This is not particularly a critique of OCaml itself, which is obviously fulfilling many people's wants.
(Note I didn't say "needs".) Other statically typed functional languages also have usability problems
when it comes to mixing numeric data types, for example. I think I'm going to remain holed up in
my bunker with Scheme's seamless, non-overflowing, dynamically typed numeric tower for a while
longer, at least until I can get one of those famous sliders to smoosh all the numeric types into a
single one.
By Anton van Straaten at Tue, 2005-04-26 18:35 | login or register to post comments
Towering Numbers
Anton van Straaten: I'm apparently behind on the relevant "literature"! I'll have to go find a 5
year old mentor to help get me up to speed.
Oh, "The Incredibles" is far from appropriate for 5-year-olds. It is deservedly Pixar's first PG-
rated film. I personally would agree with 13-and-over.
Anton: Hmm. I must confess this gives me deja vu. The language has fast, unchecked integers
because people demand performance (as evidenced by the other thread about C-class
performance). The performance is one factor which drives its acceptance for some real
applications. But then we need to start retrofitting solutions to the problems which were
created by the pursuit of performance. Sound familiar?
Certainly. My once again somewhat tongue-in-cheek point was only that if in a certain domain
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
you can't afford silent overflow, then you can make it "unsilent." Of course, you can also use
the Big_Int or Num modules for arbitrary-precision integral or rational arithmetic. So there are
actually a range of choices here, which can be made on a per-project basis according to
requirements and what's learned from the appropriate combination of experience and profiling.
Anton: This is not particularly a critique of OCaml itself, which is obviously fulfilling many
people's wants. (Note I didn't say "needs".) Other statically typed functional languages also
have usability problems when it comes to mixing numeric data types, for example. I think I'm
going to remain holed up in my bunker with Scheme's seamless, non-overflowing, dynamically
typed numeric tower for a while longer, at least until I can get one of those famous sliders to
smoosh all the numeric types into a single one.
You're not going to hear me argue that the Scheme numeric tower isn't a thing of wonder from
a conceptual point of view, and if you care strictly about correctness over performance it is,
AFAIK, the only game in town. All I was saying was that it isn't necessarily the case that
O'Caml's overflows on its int types were silent, and therefore I could balance safety and
performance according to my needs. Having said that, I'd be very interested in seeing what a
good optimizing Scheme compiler like Stalin did with some heavy numerical code.
I think he means something like this (be warned: have not written Ada in a while, hw manipulation in Ada less so)...
main_mouse : mouse_data;
for main_mouse'address use some_intrinsic_sys_routine(16#000000ff#);
procedure initialise_driver is
main_mouse.x := 0;
main_mouse.y := 0;
-- ...
end initialise_driver;
This uses two aspects of Adas HW support. First it uses a representation clause to layout the structure we expect (for
blah use ...) and secondly it maps a record to an address (for blah'address use ...). Ada allows you to map this to a
pointer (or access type) and manipulate it safely (and back).
Ada's hw support is about giving you the ability to do hw level manipulation with some level of safety. You can with the
right code map records (structures) to physical memory, but you get static checks on the structures you manipulate (you
can fly without the checks if you really really want to, but i ain't getting in that plane ;) ).
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
I stopped using Ada a while ago, but am thinking of going back to it for a toy project. Be interesting to see what's
happening with GNAT in Gcc 4.0 and what's going on with Ada 2006.
Chris
p.s. the best reference on Ada's low level facilities is Cohens "Ada as a second language", but I couldn't find anything
online to give you a flavour. Perhaps Ehud knows of a better example?
p.p.s. the compiler doesn't have to honour representation clauses to be an Ada compiler, but something like GNAT will
unless it's just plain odd.
Thanks
Okay, thanks. That's the kind of think I hadn't seen used since... well, since I last programmed hardware "drivers"
under MS-DOS in Turbo Pascal.
mmap
I thought the original poster was referring to making use of mmap() etc from C. This provides the ability to
efficiently map data living on disk (for example) into your address space. It's used a lot for low-level database
routines etc, from what I gather. I've never used it myself (directly), and may be wildly off-mark here.
struct MessageHeader {
unsigned short id;
unsigned short length;
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
}
Is a memory mapped data structure. I can use the above in C/C++ to find out the arrived message header without the
overhead of instantiating other objects. For example:
int main()
{
SOCKET s = bla bla bla create socket;
MessageHeader header;
while ()
{
recv(s, &header, sizeof(header));
switch (header.id)
{
//bla bla bla handle message
}
}
}
The above is very important for real-time applications. It is very very important for hard-realtime applications, and
quite important for soft-realtime applications. If it was to be done in Java, I would have to instantiate a class, then read
from a buffer, then assign the data from the buffer to the instance etc etc. Too much overhead, especially when there is
a lot of messages going in and out.
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Vector r = vsub(vadd(a,b), c);
which I don't see as that much less readable.
(/ (* (- 2 3)
(+ 4 1))
(+ 11 3))
[...]
This two-dimensional effect is difficult to achieve with infix.
Yes, infix may win for small expressions. Years of experience have convinced me that prefix
wins for large expressions.
large expressions?
Well, I think there's still a reason that mathematicians don't use prefix.
In any case, how large should expressions get? Not that large, IMHO.
While it's true that they don't use it to the exclusion of everything else, it must be stressed that they do use it
extensively as in f(u(x,y),v(x,z)).
There might be a reason for the use of infix operators, but I'm not sure what that reason is. Care to enlighten?
I'm genuinely curious.
Communication device
Programming is a matter of symbolic manipulation with infix is a fairly normal technique in mathematical
notation. Helps some people (but not all) read the code easier.
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Mathematicians and their notation
Prepare for a lot of sentences beginning with "And":
Mathematicians are funny with their notation. Sure, you've got infix notation. And of course prefix notation is
common, for things like functions named 'f'. But mathematicians have all other sorts of whacked out notation
that they use when they don't feel like using prefix notation. There's postfix notation (factorial signs), topfix
notation (putting a bar over top for the conjugate), vertical infix notation ("three over five"), top-and-left-fix
notation (square roots), top-and-left infix notation (nth roots), outfix notation (absolute value), and let's not
even get into the notation used for representing functions that turn functions into other functions (integral
signs, derivatives).
And that's not the end of it. There are all sorts of ways to imply functions simply by placing symbols in different
orientations with respect to one another. Putting two variable letters together implies multiplication, as does
putting a number and letter together (with the number on the left side), as does putting anything next to a
parenthesized expression. (Unless it's a function.) And let's not forget that while one form of function notation
requires parentheses like f(n) and is spoken "f of n," another form of function notation simply places the
argument as subscript, and we end up saying "a sub n." But sometimes it's actually not a function; it's just part
of the variable name. And then of course, if the right-hand symbol is superscripted, you just get
exponentiation. And since exponentiation is not commutative, you get interesting arguments about how to
interpret x^y^z. And if the exponent is placed in parentheses, it suddenly means the nth derivative. And if the
exponent actually is a 'prime', well, that's the first derivative. Then there's combination notation, putting
numbers above one another like a fraction, except with nothing in between, and two large parentheses on both
sides.
Generally speaking, these forms of notation are used because they've been found to be more convenient.
Considering all the interesting forms of notation my text editor can't do, why would I ever want to give up infix?
It's all I have left.
nitpick
"documentation is unfinished (and yet in another HTML style)"
what's wrong with documentation in HTML? quite frankly, no PDF matches the quirkyness of going through an html manual
with Lynx. and no, i'm not kidding.
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
(direct programming on the API), and most important: doesn't restrict the programmer (what is wrong with pointer arithmetic if I need
it?). Did I mention performance (?), yeah, that too.
I guess the statement is "C++ == absolute freedom, and I value that".
Often
Often, but I admit domain specific. My programs occassionally construct graphs which easily have more than half a million nodes
in them. Lazily, insertions are only evaluated after you inspect the graph; the representation with lazy nodes explodes in these
cases. Since most of my programming is referentially transparent, and the eager normal form of a graph is complex but small I
want eager evaluation.
In short: I don't even want to be bothered by thinking over the lazy evaluation of the programs I write - I never need it since I
don't write programs with expressions which aren't evaluated.
The other two questions: yes and no ;-)
Really, that's what all of these conflicts over whose language is best come down to: if language x is so much better and more productive,
then use it. If it's as good as the claims, then it will net you a commercial advantage. It'd almost be better if other companies didn't use
language x ;)
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
You see a similar situation in the battles over the use of formal methods in software development. Now, I've been guilty of advocacy in
that particular war myself, mostly because I hate seeing people doing something one way when I know there's a better way. But, at the
end of the day, if the use of formal methods really does allow bugs to be caught sooner it will confer an economic advantage on those
companies using them. Some companies are using that advantage (see e.g. Praxis Critical Systems). Others are not. Perhaps there are
market niches for both.
New languages do get adopted (otherwise we'd all still be writing asm or FORTRAN). New techniques do (eventually) make their
way into industry practice (witness the current push towards formal verification in the VLSI world).
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Play ball!
If a company sees a competitive advantage in using a particular technique, they will use it. Or at least they should. If they're
not, they'll get crushed by someone who does (assuming said technique is all its cracked up to be).
This is exactly what I'm arguing does NOT happen, because technical improvements tend to be incremental rather than
revolutionary, and that even when the effect is quite strong, it is frequently swamped by other more visible criteria, such as
e.g. community effects.
New languages do get adopted
Yes, but not dramatically unless they can benefit from a founder effect, such as Perl with early CGI, or C with Unix, or a big
marketing push from a "big dog" (or a few) in IT e.g. Java or C++.
Whether we like it or not, if we want "better" languages to be adopted in the mainstream, we will need to think like
marketers as much as like PL theoreticians. We can't just wait for "better" features to win in the marketplace without a
concerted effort.
(Unless of course we are happy with them being our little secret... ;-) )
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
By Chris Rathman at Wed, 2005-04-27 15:03 | login or register to post comments
Haskell Examples
The first issue of The Monad.Reader has a simple memory game written with gtk2hs. A more complicated demo article is scheduled for the
third issue.
All of the nine points you mention are supported by Haskell. If you want helpful urls for each point, I would be happy to respond in greater
detail.
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
For me, a programming language is a tool. If Java makes me more productive for a certain task, I will use it. If assembly is
better, I will use that too. It just happens that I have programmed a lot in C++, so I felt I could answer the original question 'why
people still program in C++'. No language holy wars for me, please.
It seems to me that what it is needed is a language that offers 'linguistic' abstraction instead of 'executional' abstraction.
'Executional' abstraction means to abstract execution details away, whereas 'linguistic' abstraction means to abstract language
details away. If, for example, a programming language had only one type ('bit'), only one operation (assignment) and only two
possible values ('0' or '1'), but a powerful way to provide layers upon layers of linguistic abstraction, I would prefer it any day. It
seems that the solution to reuse and enhanced productivity lies in a programming language that can is able to process itself,
instead of only being able to process programs.
If it sounds like the principle behind LISP, it is because it is. But another source of inspiration is C, and specifically the C
preprocessor: by extrapolating on an ever increasing amount of clever preprocessor tricks, productivity is enchanced in
previously untold ways, both in quality and in quantity. Boost.preprocessor is one of the best actual examples amongst them.
Preprocessor ?
Correct me if I'm wrong but I tend to believe that C's (and C++'s) preprocessor, although often useful, is also one of its
biggest problems.
Every so often, when switching compiler (typically, between gcc and vc++), platform or library version, I end up having to
debug the compilation process itself, digging deep into thousands of lines of headers and macros to find out why expression
A, once compiled, ends up with syntax errors and/or different behaviours....
Also, I'm not familiar with professional C/C++ tools for code management, refactoring and analysis, but every complex macro
layer I've seen (and there's at least one such layer per toolkit involved) ends up completely breaking all the tools I'm using,
sometimes with quite annoying effects (i.e. impossibility to find symbol definitions or to generate API documentations).
Last but not least, I suspect that C/C++'s horribly slow compilation is largely due to the awful lot of string-crunching involved
by the preprocessor -- heck, I compiled larger programs about ten times faster with Turbo Pascal on my old 286.
Note that I'm not against this notion of "linguistic abstraction". I think it's interesting. I just hope I'll eventually find a
language which does it well. Note that I'm not a Lisp programmer (yet ?).
Preprocessor
"Last but not least, I suspect that C/C++'s horribly slow compilation is largely due to the awful lot of string-crunching
involved by the preprocessor -- heck, I compiled larger programs about ten times faster with Turbo Pascal on my old
286."
The preprocessor is actually very fast. It's called cpp on most systems, and you can try running it seperately. The C/C++
preprocessor is very simple, well on par with a complex regex (a little exaggeration). The biggest problem is C++ is that
it's unparseable with a context-free parser (the usual sort). The the following three lines:
int (x), y, *z;
int (x), y, new(int);
int (x), y, *z = 0;
The first declares two ints and a pointer to an int. The second line converts x to an int, discards it (noop), discards y
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
(noop), and introduces a memory leak. The second line is in fact an expression, and not a declaration line. However the
parser can't tell until it gets to the end of the line (thus your parser requires infinite lookahead). The third line could in
fact be an expression, but the rules say that it's a declaration. So your parser needs semantic info to resolve things.
Even this wouldn't be that bad -- C has it as well (tho C++ complicates things with references and what nots). The real
killer is templates. "Good C++ " (i.e. STL-style, Boost-style) use templates copiously. Templates create more ambigieties
that needs to be resolved later, thus requiring the compiler to work harder.
Compile Times
What genneth said. Keep in mind, for example, that "std::string" isn't just a class, but a typedef of a class template
based on the std::basic_string class, a type trait for char, and a standard allocator.
If you really want to kill your compile times, just start using Spirit heavily. If you really really want to kill your
compile times, use Spirit to parse XML. :-)
Fixed, thanks
By Anton van Straaten at Tue, 2005-04-26 16:31 | login or register to post comments
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
By Scott Johnson at Tue, 2005-04-26 16:47 | login or register to post comments
Well, so you found out the hard way that C is not an adequate language for dealing with symbolic computations,
in almost all cases.
And I know the answer for "why do they use C++." The answer is: alternatives, even much
better ones, require work. Unusual work. That is all.
Exactly. When using language X one has to take into account its environment as well: available libraries,
documentation, interoperability (e.g., with other languages), stability, portability, cost of maintenance, etc.
At risk of stating the obvious: even if a language excels in one particular area, it does not mean it is the overall
best choice.
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
By michaelw at Sat, 2005-06-04 19:46 | login or register to post comments
I do believe that the trade-off in performance and productivity is worthwhile. In my opinion and in my experience,
Haskell gives many times the productivity of C++.
Since I don't particularly want this to devolve into a shouting match, how can we usefully compare the two?
Interesting Point...
Achilleas Margaritis: By the way, I don't claim that C++ is better than any of the languages LtU prefers. I am into LISP lately, and I
have to say LISP is quite clever (to the point of wanting me to make a new language which has the hardware-related capabilities of C
and the linguistic capabilities of LISP).
I don't think there's an implementation yet, but you might want to keep an eye on BitC. Another strong candidate might be Cyclone,
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
designed as a safe dialect of C, but with various language features that are very familiar to functional-language programmers, albeit
more from the ML family than the Lisp family.
networking
IPv4 is standard, IPv6 is available separately, there's also a pure Haskell TCP/IP stack in House.
Yes, see the Haskell Foreign Function Interface, or darcs' FastPackedString for usage examples..
multitasking.
Control.Concurrent does
a rich GUI.
wxHaskell
gtk2hs
HOpenGL (includes GLUT)
Also bindings for Tk
value classes
operator overloading
Haskell has typeclasses, and you can define your own operators. See the HaskellDemo for a simple typeclass example.
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
clever and efficient data structures and algorithms support
Too many to list, any specific requests past what's in the standard libraries?
However
What is true, however, is that there are relatively few desktop applications written in non-C/C++, partly because many libraries -- especially
toolkits -- are C/C++-specific.
Yes, if I look at the apps I've used during the past few years, I can probably find around 20 of them written in Python, 3 or 4 in OCaml, one in C#
and one in Delphi. Compare this to hundreds or thousands written in C/C++.
This does not mean that any of these languages is inferior to C/C++. Maybe just that they need a little "push".
I think it's hilarious that most people list "type safety" as one of C++'s benefits, as if it were actually type safe.
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
The last, of course, is the tough one. The runtime (other than via a tracing garbage collector) cannot prevent double-deletion of objects
(probabilistic solutions using guard words and such are tractable); verifying that no object is deleted more than once is not amenable to static
analysis without severly restricting the type system (see ATTAPL, as usual). This is, in my mind, the strongest argument for garbage
collection; stronger than avoiding memory leaks (GC'd programs can still leak memory--even if all dead objects are collected, there is often
much live semantic garbage lying around in any large non-trivial program).
null pointers
arrays (because of their completely unsound interpretation as pointers)
delete (it's not just double deletion, it's dangling pointers in general)
all kinds of std library functions
and much more things I already repressed
Null pointers are another killer, almost as bad as dangling pointers. Since they are completely unsafe, you cannot call something like
dynamic_cast or operator new type-safe either, because they may give rise to null pointers.
I claim that it is virtually impossible to identify a safe subset of C++ that would still be workable.
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Did you mean to write, it is typesafe if you avoid libraries that use the following?
Interesting...
Achilleas Margaritis: And I don't agree that Haskell or any other language simply enhances one's productivity; it all depends on the
project. You may code an exotic algorithm in under 500 lines of code, but there are other problems that C/C++ excels in. Databases,
for example.
Given that my day job consists of keeping a C++ codebase talking to a database, and I've done the equivalent thing in half a dozen
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
different languages, I have to say that I find this a particularly inapt example: database integration, at least today, is one of the
things that C++ is most miserable at. Something like Hibernate in Java or Og or Active Records in Ruby is vastly superior to anything
I've found in the C++ world. You really want some kind of tuple type and reflection to accomplish the C++/SQL mapping, and even with
heavy use of the preprocessor and template metaprogramming, C++ just isn't competitive with frameworks in languages that actually
support such concepts. And yes, I'm keeping my eye on Boost.Interfaces and Boost.Names, even at this early stage.
Achilleas: By the way, does anybody have examples of recursive-descent parsers done with functional languages? I would like to see
how efficient they are.
Sure. See camlp4 or the tersely-named "cf" library for O'Caml, Parsec for Haskell, and I'm sure someone knows of something
equivalent for Standard ML or other languages.
Tuples in Java?
Did you really mean that? I really miss them.
Yes and No
1. That's the only reason why I use C++.
2. C was actuallly well designed. But as a high-level assembly language, not as a general-purpose, one-size-doesn't-quite-fit-anybody language.
Not so simple
I do not think any sane programmer would use a badly-designed, anachronistic,
terrible language like C++ unlike they are either totally unaware about what a
language could provide or forced by the circumstances.
I don't think it's nearly so simple as that. In lots of circumstances, you want
a good match for the hardware and predictability, in that you can roughly
understand how your code is compiled. Predictability also means that you know
when you're handing off control to a magic unknown function, like malloc in C.
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Just that there's always the chance that a 50 millisecond garbage collection
will occur at a bad time (for example, the expected frame rate for most video
games only gives you 16 milliseconds per frame of total processing time). You
can tangle yourself up impressively with C++ if you aren't careful, but you can
strip it down and keep your code transparent and straightforward if you desire.
There's something to be said for simplicity. Given new hardware (say, for an
embedded DVD player or somesuch), is it easier to write C compiler for it or a
Haskell compiler? Depends on your background, I guess, but it's easier (for me)
to see how to map C to most hardware.
Of course most of this stems from effort being put into CPUs that are designed
to run C well. If the original IBM PC had a Scheme chip in it, instead of an
8086, maybe everyone would be complaining about how we're all forced into using
Scheme :)
If the hardware instead used allocation of immutable chunks and garbage collection, you wouldn't need to broadcast changes to the
data in the cache of each CPU. Only the garbage collector could invalidate cached data.
I think this sort of approach might be faster for data that fits into the different caches (level 1, level 2, etc) because you wouldn't
need to allocate a chunk of main memory for temporary items.
This is just something that I thought up recently, but Google shows that I'm by no means the first to think along these lines. See
Cache-Conscious Copying Collectors by Henry Baker.
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
e
By Peter Van Roy at Thu, 2005-05-12 07:51 | login or register to post comments
Scheme chips
You might start by reading about the Symbolics Lisp machine; a computer which ran LISP "on the metal". (The dialect of Lisp that ran
on these was one of the precursors to Common Lisp). A Lisp Machine should have little trouble running Scheme; although a dedicated
Scheme machine (a processor optimized for running Scheme) might have better support for continuation-passing style.
At any rate, some possible hardware features which might be beneficial for dynamic languages:
* "Tag" bits to distinguish pointers from literal integers, and an instruction set that understands these tags and can act accordingly.
* For LISP-y langauges, the ability to store an entire cons node in a register (mainly for performance reasons), and the ability to
quickly extract the car and cdr
* Better hardware support for garbage collection (things like write barriers and such). Or even GC itself in hardware.
Others can list more.
One problem, of course, is that to get many of these things to work properly requires more than just modifying the CPU. Memory
devices may need modification (to store the extra metadata); bus architectures, etc. The current style of CPU architecture widely
used enjoys numerous economies of scale that would be difficult to duplicate in a radically different architecture. As a result, trying
to re-invent the Lisp Machine is probably a losing proposition. I suspect that the avenue toward improved dynamic language
performance is improved compilers and runtime environments (and possibly more dynamic-language-friendly OS's as well). Type
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
inference, when possible, is a great benefit here; as it can eliminate many instances where tag bits might be needed.
Dylan
It may be worth looking at Gwydion Dylan here. The compiler uses a 2-word architecture (one word for type tagging, one word
for data), on the basis that it can type-inference enough that it'll usually be able to eliminate one of the words from almost every
variable.
Tag bits
The simplest addition would be a tag bit to indicate which memory cells contain addresses.
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
By Anton van Straaten at Fri, 2005-05-13 10:19 | login or register to post comments
Logs: Hack The Planet ; JavaLobby ; Daily Python-URL ; Daily WTF ; PHP everywhere ; (more)
Wikis: WikiWiki ; Erlang ; Common Lisp ; Haskell ; Squeak ; Tcl ; Program Transformation
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com