all*_*win 1 haskell types type-constructor
我想做这样的事情:
data Bit = 0 | 1
但由于右侧必须是有效的数据构造函数(?),我必须使用类似的东西
data Bit = Zero | One
这不是特别好,因为我想使用实际值0和1.什么是我的难题的最佳解决方案?
您可能会喜欢Num我前段时间写过的这个厚颜无耻的实例:
instance Num Bool where
(+) = (/=)
(*) = (&&)
negate = id
abs = id
signum = id
fromInteger = odd
Run Code Online (Sandbox Code Playgroud)
例如,在ghci中:
> (3 + 5) * 6 :: Bool
False
Run Code Online (Sandbox Code Playgroud)
您应该能够使用类似的东西,经过适当修改以支持您data Bit = O | I而不是data Bool = False | True.