4

データベース理論のリレーションに相当するものを書こうとしています。

-- A Relation has a name and a series of attributes. 
-- Each attribute might have a different type.
data Relation = Relation String [Attribute]

-- An Attribute has a name and a type
data Attribute = Attribute String Type

-- These three look like enough
data Type = String | Integer | Fixed Int

ここまでは順調ですね。

次に、アサーションを持つように属性を再定義します。強制する必要があるプロパティ

data Attribute a = Attribute String Type [Assersion a]

data Assertion a where --Using GADTs
  LessThan :: Num a => a -> Assertion a
  Element :: Eq a => [a] -> Assertion a

ここで、型パラメーターを Relation に追加する必要がありますが、これにより、すべての属性がアサーションで同じ型パラメーターを持つことになります。したがって、整数属性と文字列属性を持つことはできませんでした。

この問題を解決するにはどうすればよいですか?

4

1 に答える 1

3

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し、別のキャスト方法を提供することができます。

于 2013-08-01T00:37:33.680 に答える