可能重复:
如何创建具有限制的类型
在Haskell中是否可以创建一个类型,例如"Name",它是一个String,但不包含10个字母?
如果不是,我怎么能禁止创建一个具有长名称的Person(其中Person被定义为:)data Person = Person Name.
也许它根本不重要,也许这种问题应该以不同的方式在Haskell中解决?
在这里的代码下面有趣的类型函数
{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, FlexibleContexts, TypeFamilies #-}
-- Start basic
class Add a b where
type SumTy a b
add :: a -> b -> SumTy a b
instance Add Integer Double where
type SumTy Integer Double = Double
add x y = fromIntegral x + y
instance Add Double Integer where
type SumTy Double Integer = Double
add x y = x + fromIntegral y
instance (Num a) => Add a a where
type SumTy a a …Run Code Online (Sandbox Code Playgroud)