gpt4 book ai didi

Haskell - 表示关系(来自数据库理论)

转载 作者:行者123 更新时间:2023-12-02 15:06:01 24 4
gpt4 key购买 nike

我正在尝试写一些相当于数据库理论中的关系的东西。

-- 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

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

如何解决这个问题?

最佳答案

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

您可以创建自己的类,使用您自己的较小类型集的表示(例如您的数据类型),并提供不同的转换方法。

关于Haskell - 表示关系(来自数据库理论),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17702380/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com