archives

Algebraic Data Types in JavaScript

It is generally known that JavaScript supports a functional style of programming. But because it does not have algebraic data types, the functional programming is usually limited to some simple higher order functions applied to Arrays.

I have often build small libraries that allow working with some aspects of algebraic data types in JavaScript. This time I thought is was cool enough to write a bit about it:

Algebraic Data Types in JavaScript

It includes:

  • a very small footprint
  • unfold, map and fold over any adt
  • merging (read deforestation/fusion) of unfold, map and fold
  • user defined derived properties (think derived classes Show, Eq...)

I also show how to do the Data Types à la carte style of Wouter Swierstra with this library.

SWI-Prolog FFI Problem: Getting Prolog and C to work together on MacOS?

Hi,
I'm trying to get SWI-Prolog and C to work together on MacOS 10.4.11 (Tiger) using the Prolog's Foreign Interface. Unfortunately I'm not an expert in either Prolog, or C, so I would appreciate some help.

It seems that there are some issues with the foreign function interface specifically on MacOS. The Prolog manual has a very nice example of how to use FFI with C (http://gollem.science.uva.nl/SWI-Prolog/Manual/foreignxmp.html)... which this doesn't work for me on MacOS, but worked fine under Linux (Ubuntu 8.04 Hardy).

I don't understand what's wrong with 'strlen' initially:

$ gcc -I/opt/local/lib/swipl-5.6.15/include -fpic -c lowercase.c
lowercase.c:1: warning: -fpic is not supported; -fPIC assumed
lowercase.c: In function 'pl_lowercase':
lowercase.c:14: warning: incompatible implicit declaration of built-in function 'strlen'

... and even though we do get an object file in the end, we can't create (and register?) a library at the next step (this seems to be the MacOS-specific problem):

$ gcc -shared -o lowercase.so lowercase.o
i686-apple-darwin8-gcc-4.0.1: unrecognized option '-shared'
/usr/bin/ld: Undefined symbols:
_main
_PL_get_atom_chars
_PL_register_foreign
_PL_unify_atom_chars
_PL_warning
collect2: ld returned 1 exit status

Any suggestions? I couldn't find anything online except http://www.phil.uu.nl/~xges/HOWTO/swipldynlib.html which seems quite old? Should I go for it anyway?

Thank you,
-- Georgi

Higher-order type constructor polymorphism vs./and template style specialization

I'm wondering if there are language implementations that (elegantly?) combine what Scala (at least) refers to as "higher-order type constructor polymorphism" with the code specialization capabilities of C++ style templates?

I don't mean the notion of "specialization" sometimes used to generically describe how instantiating a template List[int] will (likely) generate new code.

I mean programmer driven template specialization.

That's pretty much the question, so now I'll make a fool of myself trying to come up with an example.

class Vector[T](size:int, init:T) extends Sequence[T]
with Indexed[T]
with Iterable[T, Vector[X]]
{
/* stuff */
}

So Iterable, say, has methods like map and friends that will now return Vector[U] for some function (T => U). And all the Iterator and Builder machinery comes out nicely typed. Yada yada yada.

Now, it would be *really* nice to somehow be able to implement bit vectors with custom code. Making up a silly keyword:

specialize class Vector[Bool](....
{
/* Do NOT want to use the normal machinery here. */
}

We don't really want a vector of 32 bit Bool values! We want some custom bitwise code over an array of bytes or unsigned ints or something. We'll also need specialized Iterators[Vector[Bool]] (hopefully inlined) and associated machinery.

But in the end we get a usable bit vector class (1) with much better space/speed for Vector[Bool] and (2) we can still write code like this.

def somefun(vi:Vector[Int]): Vector[Bool] = vi.map((_ % 2 == 0));

Just a dumb and obvious example. We could also inquire about method (or function) specializations on values yada yada yada.

Hence my question - are there language implementations that nicely combine their elegant type systems with the power of template style code specialization?

Sorry if this is too implementation oriented a question.

Scott