这是toy-example.hs:

{-# LANGUAGE ImpredicativeTypes #-}

import Control.Arrow

data From = From (forall a. Arrow a => a Int Char -> a [Int] String)

data Fine = Fine (forall a. Arrow a => a Int Char -> a () String)

data Broken = Broken (Maybe (forall a. Arrow a => a Int Char -> a () String))

fine :: From -> Fine
fine (From f) = Fine g
  where g :: forall a. Arrow a => a Int Char -> a () String
        g x = f x <<< arr (const [1..5])

broken :: From -> Broken
broken (From f) = Broken (Just g) -- line 17
  where g :: forall a. Arrow a => a Int Char -> a () String
        g x = f x <<< arr (const [1..5])

这是ghci的想法:
GHCi, version 7.0.3: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading package ffi-1.0 ... linking ... done.
Prelude> :l toy-example.hs
[1 of 1] Compiling Main             ( toy-example.hs, interpreted )

toy-example.hs:17:32:
    Couldn't match expected type `forall (a :: * -> * -> *).
                                  Arrow a =>
                                  a Int Char -> a () String'
                with actual type `a0 Int Char -> a0 () String'
    In the first argument of `Just', namely `g'
    In the first argument of `Broken', namely `(Just g)'
    In the expression: Broken (Just g)
Failed, modules loaded: none.

为什么fine类型检查而broken不检查?

如何获取broken进行类型检查?

(在我的真实代码中,如果需要的话,我可以将类型参数a添加到Broken中,而不是在构造函数中对其进行通用量化,但是如果可能的话,我想避免这种情况。)

编辑:如果我将Broken的定义更改为
data Broken = Broken (forall a. Arrow a => Maybe (a Int Char -> a () String))

然后broken类型检查。耶!

但是如果我再添加以下功能
munge :: Broken -> String
munge (Broken Nothing) = "something"  -- line 23
munge (Broken (Just f)) = f chr ()

然后我收到错误消息
toy-example.hs:23:15:
    Ambiguous type variable `a0' in the constraint:
      (Arrow a0) arising from a pattern
    Probable fix: add a type signature that fixes these type variable(s)
    In the pattern: Nothing
    In the pattern: Broken Nothing
    In an equation for `munge': munge (Broken Nothing) = "something"

如何获得munge进行类型检查?

第2次编辑:在我的真实程序中,我已经用Broken (Maybe ...)BrokenNothing构造函数(已经有其他构造函数)替换了BrokenJust ...构造函数,但是我很好奇在这种情况下模式匹配应该如何工作。

最佳答案

ImpredicativeTypes使您处于不稳定状态,无论如何都应从GHC版本更改为版本-他们正在努力寻找一种可以兼顾功率,易用性和易于实现性的隐性表示。

在这种特殊情况下,尝试将量化类型放入Maybe内(这是未明确定义为以这种方式运行的数据类型)确实很棘手,因此建议您使用自定义构造函数,而不要像您提到的那样。

我认为您可以通过在RHS上重新解构munge的参数来修正Broken的问题,例如:

munge (Broken x@(Just _)) = fromJust x chr ()

不过,这很丑陋。

10-06 02:43