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

现在我需要将类型参数添加到关系中,但这会强制所有属性在断言上具有相同的类型参数。因此,我不能有一个整数属性和一个字符串属性。

我该如何解决这个问题?

4

1 回答 1

3

nm 有正确的想法,前提是 Relation 应该是关系的模式,而不是关系本身。这是一个示例,使用 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] ]

这是程序的输出:

关系“人”[属性“姓名”[字符][元素[“爱丽丝”,“鲍勃”,“夏娃”]],属性“身高”整数[LessThan 300]]

150 不能是名称,因为它不是 [字符]

150可以是一个高度

700 不能是名称,因为它不是 [字符]

700 不能是高度,因为它没有通过断言

“爱丽丝”可以是一个名字

“爱丽丝”不能是高度,因为它不是整数

“乔治”不能是名称,因为它没有通过断言

“乔治”不能是高度,因为它不是整数

您可以创建自己的类,使用您自己的一组较小类型的表示,如您的data Type,并提供不同的转换方法。

于 2013-08-01T00:37:33.680 回答