[Haskell-cafe] relational data representation in memory using haskell?

Salvatore Insalaco kirby81 at gmail.com
Thu May 22 02:16:54 EDT 2008

2008/5/22 Marc Weber <marco-oweber at gmx.de>:
> I'd like to illustrate two different ideas using a small example:
> (A)
>        data CD = CD { title :: String, tracks :: [ Track ] }
>        data Track = Track { track :: String, cd :: CD }
>        data PDB = PDB { cds :: Set CD, tracks :: Set Track }
> because it's not using foreign ids but kind of pointers I'll call this
> the pointer method

This doesn't look like a relational structure at all in Haskell.
Let's take the CD and Track relations. In a relational database you
have something like:
CD (1, 'Querying about you')
Track (1, 'Inserting some love', 1)
Track (2, 'Updating my feelings', 1)
Track (3, 'Deleting my hopes', 1)

In an imperative language you can do something similar in memory using
objects (you can in haskell to with IORefs and so on, but let's stay
on "data"). You get something like:

0x000 CD('Querying about you')
0x004 Track('Inserting some love, 0x004)

In Haskell when you say:
>        data Track = Track { track :: String, cd :: CD }

You are not storing in Track a reference, a pointer or something
similar to a CD, you are storing a *value* (low level you probably
have a pointer, but you have not pointer semantics). As you noticed,
you cannot "update" the CD title without changing each Track. That's a
way to store information, and a good way too, but it's not a
relational structure by any extent.

If you want to use this structure for your relational data you need two things:
1) Something that will convert from a value-based representation of
data to something relational (aka ORM in the OO world... a FRM? VRM?).
2) A relational storage (internal or external).

If you want to use "normal" Haskell ADT, are you sure that a
relational storage is what you want? Keeping that in memory doesn't
give you some advantages of relational databases (e.g. uniform
representation), and the impedance between the functional and the
relational world is not easy to manage.

Maybe I misunderstood what you are trying to accomplish, and you only
want to do a generic data structure with fast lookups on the content
of the items? Or do you really need relational semantics?


More information about the Haskell-Cafe mailing list