Polymorphic C

I've been having fun recently learning to write programs in Haskell. As a lover of both Scheme and C, its functional semantics and static typing seem like a near perfect combination, and having to re-think certain algorithms that came to me so easily with mutable state has proved to be a fun challenge.

What brought me to Haskell was the fact that I was tired of faking polymorphism in C with void* passing. I just couldn't commit myself to using C++ -- it's just a personal bias -- functions on objects seems much more natural than objects with functions. Plus Haskell is the new hotness (TM).

Of course I thought wouldn't it be great if there was a C (a real C with pointers and all) that also had a ML type system. After a little digging on Google I found this: Towards an ML-style Polymorphic Type System for C; reading through the paper it seems like a very natural evolution of C.

Unfortunately it seems that the idea lives and dies in this paper. I cannot find reference to a compiler implementation anywhere.

Has anyone come across other projects that combine imperative mutable languages (like C) with ML style typing?

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Does your question have anything to do with type classes?

Nothing in the body of the message mentions them. You seem to be talking instead about parametric polymorphism.

It should have the title 'Polymorphic C'

I apologize. I had titled this post before typing it up (it was a play on C++'s earlier name of 'C with Classes'); I didn't think to rename it before hitting submit. Quite an amateur mistake.

It don't think I can go about changing the title now, and so my mistake is with us for eternity. Or perhaps I can...

Cyclone has an ML-like type system

You might want to take a look at the Cyclone language, which has been discussed many times here over the years: http://cyclone.thelanguage.org/

It is billed as a "safe C", and it introduces an ML-like type system with inference, pattern matching, etc.

Cyclone certainly has the featureset.

I seem to have overlooked it due to the fact that it doesn't list in the results when searching 'polymorphic C' in Google which is a shame.

Its lack of support for 64-bit systems is unfortunate, and they say that adding support would likely require major changes.

But despite that it seems to be a language that shares the ideas presented in the paper I reference.

Nice find.


Whats wrong with Objective-C? It seems to fit your needs. It did most of the stuff right that C++ did wrong.

Isn't id just void* in sheeps clothing?

What I find myself disliking about C is when I want to create a generic data structure (say an array that is unbounded), I either have to create one version for each type, a single version which holds the type void* or do some dirty macro tricks.

What Haskell gives is the nice feature that you can truly define a generic data structure around a type a and fill in a later on during use (not that this is unique to Haskell).

With Objective-C (and please correct me if I'm wrong), if you want to make a generic container you seem to have to fall back to the same solutions of creating one version per type, or use id as the generic type (I presume the macro tricks work here as well, but are probably even more dirty).

I'm sold on the fact that I can have language support for creating structures with generic holes in them. Holes which take the shape of the object being put in them, instead of objects which take the shape of the hole.

GNAT Ada's generics are

GNAT Ada's generics are monomorphizing polymorphism, like C++ templates, or MLTon.

It sounds like he wants statically-typed generics,

not the Smalltalk-esque generics that dynamically-types languages like Smalltalk, ObjC, and Python provide.

With regard to C++, I tend to view the objective-C approach as mostly orthogonal to the C++ approach; neither is write or wrong. (This is not to avoid criticizing C++, as it has many ugly warts; but either approach is a legit design choice).

Objective C doesn't, however, support an ML-like type system. (Or at least it didn't when I last looked at it).