相关疑难解决方法(0)

为什么id的类型不能专门用于(forall a.a - > a) - >(forall b.b - > b)?

在Haskell中采用简单的身份功能,

id :: forall a. a -> a
Run Code Online (Sandbox Code Playgroud)

鉴于Haskell据称支持impregicative多态性,我应该能够通过类型归属"限制" id到类型似乎是合理的(forall a. a -> a) -> (forall b. b -> b).但这不起作用:

Prelude> id :: (forall a. a -> a) -> (forall b. b -> b)

<interactive>:1:1:
    Couldn't match expected type `b -> b'
                with actual type `forall a. a -> a'
    Expected type: (forall a. a -> a) -> b -> b
      Actual type: (forall a. a -> a) -> forall a. a -> a
    In the expression: …
Run Code Online (Sandbox Code Playgroud)

polymorphism haskell impredicativetypes ascription

29
推荐指数
1
解决办法
907
查看次数

如何通过减少良好类型的函数来减少类型错误?

我正在玩van Laarhoven镜头并遇到一个问题,其中类型检查器拒绝了et-reduced形式的良好类型的功能:

{-# LANGUAGE RankNTypes #-}

import Control.Applicative

type Lens c a = forall f . Functor f => (a -> f a) -> (c -> f c)

getWith :: (a -> b) -> ((a -> Const b a) -> (c -> Const b c)) -> (c -> b)
getWith f l = getConst . l (Const . f)

get :: Lens c a -> c -> a
get lens = getWith id lens
Run Code Online (Sandbox Code Playgroud)

上面的类型检查,但如果我eta减少get

get :: Lens …
Run Code Online (Sandbox Code Playgroud)

haskell higher-rank-types

16
推荐指数
2
解决办法
523
查看次数