我正在尝试使用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只接受第二个?
发布于 2020-01-19 00:43:35
当您将模式All prf与All phi类型的值进行匹配时,prf将被提取为forall a. phi a类型的多态实体。在本例中,您将获得一个no :: forall a. NotPred phi a。但是,不能对此类型的对象执行模式匹配。毕竟,它是一个从类型到值的函数。您需要将它应用于一个特定的类型(称为_a),您将获得no @_a :: NotPred phi _a,现在可以匹配它来提取f :: phi _a -> Void。如果你扩展你的定义...
{-# LANGUAGE ScopedTypeVariables #-}
-- type signature with forall needed to bind the variable phi
theorem :: forall phi. All (NotPred phi) -> Not (Ex phi)
theorem prf wit = case prf of
All no -> case no @_a of -- no :: forall a. NotPred phi a
NP f -> case wit of -- f :: phi _a -> Void
Ex (x :: phi b) -> f x -- matching against Ex extracts a type variable, call it b, and then x :: phi b所以问题是,_a应该使用什么类型?好的,我们将f :: phi _a -> Void应用于x :: b (其中b是存储在wit中的类型变量),所以我们应该设置_a := b。但这是一个作用域冲突。b只能通过匹配Ex来提取,这发生在我们指定了no并提取f之后,因此f的类型不能依赖于b。因此,没有_a可以在不让existential变量脱离其作用域的情况下工作。Error。
正如您已经发现的,解决方案是在将该类型应用于no之前匹配Ex (从而提取其中的类型)。
theorem :: forall phi. All (NotPred phi) -> Not (Ex phi)
theorem prf wit = case wit of
Ex (x :: phi b) -> case prf of
All no -> case no @b of
NP f -> f x
-- or
theorem :: forall phi. All (NotPred phi) -> Not (Ex phi)
theorem (All no) (Ex x) | NP f <- no = f xhttps://stackoverflow.com/questions/59801780
复制相似问题