因使用'minBound'而无法推断(有界a1)

arc*_*att 8 haskell types

这可能是一个愚蠢的问题,但为什么这个功能

myTest :: (Bounded a) => a
myTest = minBound :: a
Run Code Online (Sandbox Code Playgroud)

不是typecheck?

这有效

myTest' :: Int
myTest' = minBound :: Int
Run Code Online (Sandbox Code Playgroud)

它们对我来说似乎是一样的,除了必须输入前者(例如myTest :: Int)才能使它工作.

我得到的错误是

• Could not deduce (Bounded a1) arising from a use of ‘minBound’
  from the context: Bounded a
    bound by the type signature for:
               myTest :: Bounded a => a
Run Code Online (Sandbox Code Playgroud)

Laz*_*oke 9

您必须启用ScopedTypeVariablesusing {-# LANGUAGE ScopedTypeVariables #-},这允许您使用函数本身内部的函数签名中的类型变量.您还需要更改示例,如下所示:

{-# LANGUAGE ScopedTypeVariables #-}

myTest :: forall a. (Bounded a) => a
myTest = minBound :: a
Run Code Online (Sandbox Code Playgroud)

forall告诉编译器范围a.没有显式的定义具有forall默认(unscoped)行为.

否则,a函数内部与主类型签名中的不同a(a1由编译器更改).它不能推断出a1仅限于某些其他类型a有界的上下文.

第二个示例有效,因为Int它不是一个类型变量,它是一个具体的类型,这意味着无论哪个类型变量属于或不在范围内,它都引用相同的类型.

进一步阅读