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
,并提供不同的转换方法。