Haskell Data Structures for Database Supported Application

This is my first program using Haskell. I am writing this to bring to life everything I have read about FP. The first thing I'm trying to figure out is how to model the data that I will extract from my database (in the end, I will write to the database as well). I started from 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 xy) = "(" ++ show x ++ ", " ++ show y ++ ")" main :: IO () main = do print $ Pt 1 2 

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

This code does not compile, but in fact it doesnโ€™t bother me at all - the main thing is that my types are configured 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 contain audit information and opaque identifiers. How can I take advantage of this using a system like Haskell? You can see that I created the 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 interact with Java (this application is currently written in Java).
+6
source share
1 answer

What is the best way to model data based on records?

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

A simple example: a JSValue data type representing JSON records.

How can I use a system like Haskell?

Interfaces in Haskell through class classes are a valid approach, although using newtype or another data type rather than exporting its constructors provides equally powerful abstraction properties. Like the use of the existential type type or the generalized algebraic type (GADT).

Example: see, for example, how newtype used in this example .

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

Is this even a good application for Haskell?

Seems to be completely corulent. Strong types, powerful FFI and many libraries in Hackage to help, you have a lot of technology to help get the job done.

Example: There are many database access libraries for Haskell, such as:

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

And good high-level packages for magically persistent Haskell data .

So, we have a choice and many examples to start with.

+8
source

Source: https://habr.com/ru/post/886539/


All Articles