Relation が関係自体ではなく、関係のスキーマであると想定されている場合、nm は正しい考えを持っています。以下は、型のクラスとして Data.Typeable を使用し、型の表現として TypeRep を使用する例です。さらに、例としてすべてのタイプを表示できるようにしたいと考えています。
-- 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
この例では、リレーションのさまざまな属性に対していくつかの値が許可されるかどうかを確認します
-- 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] ]
プログラムの出力は次のとおりです。
Relation "People" [Attribute "Name" [Char] [Element ["Alice","Bob","Eve"]],Attribute "Height" Int [LessThan 300]]
150 は [Char] ではないため、名前にすることはできません
150 は高さにすることができます
700 は [Char] ではないため、名前にすることはできません
700 はアサーションに失敗するため、Height にすることはできません
「アリス」は名前にできる
"Alice" は Int ではないため、Height にすることはできません
"George" はアサーションに失敗するため、名前にすることはできません
"George" は Int ではないため、Height にすることはできません
のような小さな型セットの独自の表現を使用して独自のクラスを作成data Type
し、別のキャスト方法を提供することができます。