[英]Is it possible to overload logical operators (&& e.g.) in Haskell?
我正在使用多值逻辑并尝试重载基本逻辑函数。
我没有重载Num
和Eq
运算符的问题,但我不知道如何重载&&, ||
而not
。
可能吗? 谢谢你的回答!
Haskell根本没有真正的重载(= ad-hoc-polymorphism )。 +
, *
等不是函数而是类方法 :“重载”它们更像是定义OO接口/纯抽象类的具体后代,而不是像C ++那样重载函数。
逻辑运算符OTOH只是普通函数,它们在Prelude中一劳永逸地定义。
但是,在Haskell中,中缀运算符大多被视为一种特殊的函数名,它们不是实际语法定义的一部分。 没有什么能阻止您定义具有相同目的的新的不同运算符,例如
class Booly b where
true :: b
false :: b
(&&?) :: b -> b -> b
(||?) :: b -> b -> b
infixr 3 &&?
infixr 2 ||?
instance Booly Bool where
true = True
false = False
(&&?) = (&&)
(||?) = (||)
instance Booly MVBool where
true = ...
事实上,如果新名称被模块限定符消除歧义就足够了:
import Prelude hiding ((&&), (||))
import qualified Prelude
class Booly b where
true :: b
false :: b
(&&) :: b -> b -> b
(||) :: b -> b -> b
infixr 3 &&
infixr 2 ||
instance Booly Bool where
true = True
false = False
(&&) = (Prelude.&&)
(||) = (Prelude.||)
在monkeypatching意义上,没有像Haskell那样压倒一切的东西。
也没有办法将扩展挂钩到没有构建为扩展的东西。
您可以简单地隐藏eg &&
的定义,但这样做1)不会影响其他模块中&&
的语义,2)会让人感到困惑。
所以我会使用一些简单的东西:
-- laws should be defined for the class and instances QuickChecked/proved against these laws
class Logic a where
(&.&) :: a -> a -> a
(|.|) :: a -> a -> a
...
instance Logic Bool where
(&.&) = (&&)
(|.|) = (||)
data MultiBool = False' | True' | Perhaps | CouldBe | Possibly | Unlikely
instance Logic MultiBool where
...
不,这是不可能的。 &&
的类型是Bool -> Bool -> Bool
,而Haskell不允许ad-hoc重载。 您可以隐藏声明,但是在没有限定条件的情况下,您无法在同一模块中将运算符用于布尔值和mvl值。
我建议您定义类似外观的运算符,例如&&?
为你的mvls。
您无法覆盖它们,但您可以定义自己的。
infixr 3 <&&> <||>
<&&> :: ??? -> ??? -> ???
<&&> ...
(&&)
定义为
(&&) :: Bool -> Bool -> Bool
因此,除非您不加载Prelude
或加载它,否则您不能重载该运算符。
然而,有一个类型类可以或多或少地执行您正在寻找的内容: Data.Bits
,其签名如下:
(.&.) :: Bits a => a -> a -> a
(.|.) :: Bits a => a -> a -> a
complement :: Bits a => a -> a
Data.Bits
通常用于表示按位运算。 您可以决定忽略其余的运算符(返回一些默认值)或为其分配一个有用的属性。
否则您可以定义类似的运算符。 在这种情况下,一个更好的人首先定义一个类型类:
class Logic a where
land :: a -> a -> a
lor :: a -> a -> a
lnot :: a -> a
lnand :: a -> a -> a
lnand x = lnot . land x
lnor :: a -> a -> a
lnor x = lnot . lor x
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.