我正在尝试使用 GHC 版本 8.6.5 在 Haskell 中对以下逻辑含义进行建模:
(∀ a. ¬ Φ(a)) → ¬ (∃ a: Φ(a))
我使用的定义如下:
{-# LANGUAGE RankNTypes, GADTs #-}
import Data.Void
-- Existential quantification via GADT
data Ex phi where
Ex :: forall a phi. phi a -> Ex phi
-- Universal quantification, wrapped into a newtype
newtype All phi = All (forall a. phi a)
-- Negation, as a function to Void
type Not a = a -> Void
-- Negation of a predicate, wrapped into a newtype
newtype NotPred phi a = NP (phi a -> Void)
-- The following definition does not work:
theorem :: All (NotPred phi) -> Not (Ex phi)
theorem (All (NP f)) (Ex a) = f a
在这里,GHC 拒绝执行,theorem
并显示以下错误消息:
* Couldn't match type `a' with `a0'
`a' is a rigid type variable bound by
a pattern with constructor:
Ex :: forall a (phi :: * -> *). phi a -> Ex phi,
in an equation for `theorem'
at question.hs:20:23-26
* In the first argument of `f', namely `a'
In the expression: f a
In an equation for `theorem': theorem (All (NP f)) (Ex a) = f a
* Relevant bindings include
a :: phi a (bound at question.hs:20:26)
f :: phi a0 -> Void (bound at question.hs:20:18)
我真的不明白为什么 GHC 不能匹配这两种类型。以下解决方法编译:
theorem = flip theorem' where
theorem' (Ex a) (All (NP f)) = f a
对我来说,这两种实现theorem
是等价的。为什么GHC只接受第二个?