I am basically attempting to see if I can emulate an ORM framework within Haskell, so that if a user wants to make a database model, they would do something like this
data Car = Car {
company :: String,
model :: String,
year :: Int
} deriving (Model)
Where the table would be "Car", and the columns would be the company,model,year
To do this within Haskell, you have to use a combination of classes and generics, and this is where I am getting stuck. Using this tutorial (http://www.haskell.org/ghc/docs/7.4.1/html/users_guide/generic-programming.html), I came up with this (which was just basically copying and renaming so I can get the code working)
{-# LANGUAGE DeriveGeneric, TypeOperators, TypeSynonymInstances, FlexibleInstances #-}
module Main where
import GHC.Generics
class SModel b where
s_new :: b -> IO()
instance SModel Int where
s_new s = putStrLn s++":Int"
instance SModel Integer where
s_new s = putStrLn s++":Integer"
instance SModel String where
s_new s = putStrLn s++":String"
class Model m where
new :: m a -> IO()
instance Model U1 where
new U1 = putStrLn "unit"
instance (Model a, Model b) => Model (a :*: b) where
new (a :*: b) = do
new a
new b
instance (Model a, Model b) => Model (a :+: b) where
new (L1 x) = new x
new (R1 x) = new x
instance (Model a) => Model (M1 i c a) where
new (M1 x) = new x
instance (SModel a) => Model (K1 i a) where
new (K1 x) = s_new x
data Car = Car {
company :: String,
model :: String,
year :: Int
} deriving (Model)
The above code will produces the error
Cannot derive well-kinded instance of form `Model (Car ...)'
Class `Model' expects an argument of kind `* -> *'
In the data declaration for `Car'
And I am kind of stuck at this point, I believe I have already instantiated all of the required Generic types to cover a record
Model
in a deriving clause. Instead, you should deriveGeneric
and then provide a default instance forSModel
. See the GHC Users Guide on "Generic defaults". There's also some preliminary improved documentation at github.com/kosmikus/generic-deriving/blob/master/src/Generics/… – Carburet