n.m. has the right idea, provided a Relation is supposed to be the schema of a relation, not the relation itself. Here's an example, using Data.Typeable as the class of types, and TypeRep as the representation of a type. Additionally, I want all the types to be able to be shown for the example.
-- Use Data.Typeable as our class of types
import Data.Typeable
data Relation = Relation String [Attribute]
data Attribute = forall a. (Typeable a, Show a) => Attribute String (Proxy a) [Assertion a]
-- Requires RankNTypes
data Proxy a = Proxy
data Assertion a where
LessThan :: Ord a => a -> Assertion a
Element :: Eq a => [a] -> Assertion a
-- Requires GADTs
-- Relation
deriving instance Show Relation
attributes (Relation _ x) = x
-- Attribute
deriving instance Show Attribute
-- Requires StandaloneDeriving
name (Attribute x _ _) = x
validator :: Attribute -> forall a. (Typeable a) => a -> Maybe Bool
validator (Attribute _ proxy assertions) value =
(cast value) >>= \x -> Just $ all ((flip implementation) x) assertions
dataType :: Attribute -> TypeRep
dataType (Attribute _ proxy _) = getType proxy
-- Proxy
instance (Typeable a) => Show (Proxy a) where
show = show . getType
getType :: (Typeable a) => Proxy a -> TypeRep
getType (_ :: Proxy a) = typeOf (undefined :: a)
-- Requires ScopedTypeVariables
-- Assertion
deriving instance (Show a) => Show (Assertion a)
implementation :: Assertion a -> a -> Bool
implementation (LessThan y) x = x < y
implementation (Element y) x = any ((==) x) y
The example checks to see if a couple values would be allowed for the various attributes of a relation
-- Example
explain :: (Typeable a, Show a) => a -> Attribute -> String
explain value attribute =
case validator attribute value of
Nothing -> (show value) ++ " can't be a " ++ (name attribute) ++ " because it isn't a " ++ (show (dataType attribute))
Just False -> (show value) ++ " can't be a " ++ (name attribute) ++ " because it fails an assertion"
Just True -> (show value) ++ " can be a " ++ (name attribute)
main =
do
putStrLn $ show people
sequence [ putStrLn (explain value attribute) | value <- [150 :: Int, 700], attribute <- attributes people ]
sequence [ putStrLn (explain value attribute) | value <- ["Alice", "George"], attribute <- attributes people ]
where
people = Relation "People"
[
Attribute "Name" (Proxy :: Proxy String) [Element ["Alice", "Bob", "Eve"] ],
Attribute "Height" (Proxy :: Proxy Int) [LessThan 300] ]
Here's the output of the program:
Relation "People" [Attribute "Name" [Char] [Element
["Alice","Bob","Eve"]],Attribute "Height" Int [LessThan 300]]
150 can't be a Name because it isn't a [Char]
150 can be a Height
700 can't be a Name because it isn't a [Char]
700 can't be a Height because it fails an assertion
"Alice" can be a Name
"Alice" can't be a Height because it isn't a Int
"George" can't be a Name because it fails an assertion
"George" can't be a Height because it isn't a Int
You could make your own class, using your own representation of a smaller set of types, like your data Type, and providing a different method of casting.
data Attribute = forall a . Attribute String a [Assertion a]. Note how there's no Type any more, your Attribute can hold any type. You can add a constraint so that only a specific class of types may be put in an Attribute. Actually you have to, because you can accessaonly through methods of the class you constrain it with.a.