6

This is my first program using Haskell. I'm writing it to put into practice all that I've read about FP. The first thing I'm trying to figure out is how to model the data that I'll be pulling out of my DB (eventually I'll be writing to the DB as well). I started with my users table and wrote something like this

module Model (User) where

class Audited a where  
  creationDate :: a -> Integer
  lastUpdatedDate :: a -> Integer
  creationUser :: a -> User
  lastUpdatedUser :: a -> User

class Identified a where
  id :: a -> Integer

data User = User {userId :: Integer}

instance Identified User where
  id u = userId u

and

module Main (main) where
import Model (User)

data Point = Pt {pointx, pointy :: Float}

instance Show Point where
  show (Pt x y) = "(" ++ show x ++ ", " ++ show y ++ ")"

main :: IO ()
main = do 
  print $ Pt 1 2

(The Point stuff is just me testing... this is my very first Haskell code ever)

This code does not compile, but I'm not really concerned about that yet -- the big thing is getting my types set up in a good way.

Here is a list of questions that I have

  • What is the best way to model record-based data in Haskell?
  • Most of my tables have audit information and opaque IDs. How can I take advantage of this using the Haskell type system? You can see that I created Audited and Identified classes. Is this a good approach?
  • Is this even a good application for Haskell? I was considering using Clojure as it could interoperate with Java (this application is currently written in Java).
Don Stewart
  • 137,316
  • 36
  • 365
  • 468
three-cups
  • 4,375
  • 3
  • 31
  • 41
  • 1
    BTW, it's not a good idea to define a function `id`. Usually, `id` is the identity function defined as `id x = x` and thus quite important. – fuz Apr 23 '11 at 19:04
  • Hello. I see you are using Haskell with a DB, and also code in Scala. Can you take a look at my question please? How to deal with a database when you have immutable objects like we generally do in FP? Do you use mutable classes for data access? http://stackoverflow.com/questions/12882099/how-to-update-object-in-mongo-with-an-immutable-salat-case-class – Sebastien Lorber Oct 20 '12 at 22:21

1 Answers1

9

What is the best way to model record-based data?

As an algebraic data type with possibly (Haskell) record components.

Simple example: the JSValue data type, representing JSON records.

How can I take advantage of the Haskell type system?

Interfaces in Haskell via type classes are a valid approach, though using a newtype or other data type, and not exporting its constructors provides equally strong abstraction properties. As would using an existential type or generalized algebraic data type (GADT).

Example: Look at e.g. how newtype is used in this example.

Example: newtype as used to add type safety, and abstraction, to the PCRE library.

Is this even a good application for Haskell?

Seems perfectly cromulent. Strong types, powerful FFI, and plenty of libraries on Hackage to help means you have plenty of technology to help get the job done.

Example: there are many, many database accessor libraries for Haskell, such as:

and the venerable hdbc, which is also documented in RWH.

And nice high level packages for magically persistening Haskell data.

So there's lots of choice, and plenty of examples to get started from.

Don Stewart
  • 137,316
  • 36
  • 365
  • 468
  • Thanks for the answer. I'm a Haskell nube. It would be really awesome if you could give a couple short examples of what you're talking about. – three-cups Apr 23 '11 at 17:20