Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell data structures for DB backed application

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).
like image 784
three-cups Avatar asked Apr 23 '11 15:04

three-cups


1 Answers

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:

  • mongo
  • riak
  • berkleydb
  • cassandra
  • couchdb
  • tyrant

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.

like image 144
Don Stewart Avatar answered Nov 01 '22 16:11

Don Stewart