Reputation: 4385
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
Upvotes: 6
Views: 328
Reputation: 137957
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.
Upvotes: 9