【问题标题】:Haskell: Combining existential and universal quantifiers fails unexpectedlyHaskell:结合存在量词和全称量词意外失败
【发布时间】:2020-05-05 04:38:06
【问题描述】:

我正在尝试使用 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只接受第二个?

【问题讨论】:

  • 这很奇怪。看起来 GHC 需要知道将 a 实例化到 在它解包的点 All 构造函数的类型。但这实际上不应该是必要的。

标签: haskell ghc existential-type functional-logic-progr


【解决方案1】:

当您将模式All prfAll 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 可以在不让存在变量逃脱其范围的情况下完成这项工作。错误。

正如您所发现的,解决方案是匹配 Ex(从而提取其中的类型),然后将该类型应用于 no

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 x

【讨论】:

  • 所以重点是no :: forall a. NotPred phi a 不能与f :: forall a. phi a -&gt; Void 进行模式匹配,尽管NotPred 只是一个新类型?
  • 是的。您可以编写一个函数进行转换,但构造函数NP 只接受NotPred phi as,而不接受forall a. NotPred phi a。我建议给NP 一个记录字段——newtype NotPred phi a = NotPred { unNotPred :: phi a -&gt; Void }——并写成theorem (All no) (Ex x) = unNotPred no x
猜你喜欢
  • 2012-01-28
  • 2022-08-18
  • 2022-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-08
  • 2017-10-05
  • 2015-09-13
相关资源
最近更新 更多