gpt4 book ai didi

haskell - Control.Arrow : Why "let (a,b) = (first,second)" fails?

转载 作者:行者123 更新时间:2023-12-04 08:41:53 25 4
gpt4 key购买 nike

我想要的是写这样的东西:
let (a,b) = if *condition* then (first, second) else (second, first)
我发现我什至不能写这个:
let (a,b) = (first,second)
它失败并出现错误:

 <interactive>:7:5:                                                                                                                          
Could not deduce (Arrow a0)
from the context (Arrow a)
bound by the inferred type for `a':
Arrow a => a b c -> a (b, d) (c, d)
at <interactive>:7:5-26
The type variable `a0' is ambiguous
When checking that `a' has the inferred type
a :: forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (b, d) (c, d)
Probable cause: the inferred type is ambiguous

<interactive>:7:5:
Could not deduce (Arrow a0)
from the context (Arrow a)
bound by the inferred type for `b':
Arrow a => a b c -> a (d, b) (d, c)
at <interactive>:7:5-26
The type variable `a0' is ambiguous
When checking that `b' has the inferred type
b :: forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (d, b) (d, c)
Probable cause: the inferred type is ambiguous

最佳答案

很快,您将尝试构建 GHC 无法推断的 Impredicative 类型。你可以做:

λ Control.Arrow > let (a,b) = (first, second) :: Arrow a => (a b b -> a (b, b) (b, b), a b b -> a (b, b) (b, b))
λ Control.Arrow > :t a
a :: Arrow a => a b b -> a (b, b) (b, b)
λ Control.Arrow > :t b
b :: Arrow a => a b b -> a (b, b) (b, b)

或者
:set -XImpredicativeTypes 
λ Control.Arrow > let (a,b) = (first, second) :: (Arrow a => a b b -> a (b, b) (b, b), Arrow a => a b b -> a (b, b) (b, b))
λ Control.Arrow > :t a
a :: Arrow a => a b b -> a (b, b) (b, b)
λ Control.Arrow > :t b
b :: Arrow a => a b b -> a (b, b) (b, b)

但你不能这样做:
λ Control.Arrow > let (a,b) = (first, second) :: (Arrow a, Arrow a') => (a b b -> a (b, b) (b, b), a' b b -> a' (b, b) (b, b))

为了隔离问题,这有效:
λ Control.Arrow > let p = (first, second) :: (Arrow a, Arrow a') => (a b b -> a (b, b) (b, b), a' b b -> a' (b, b) (b, b));
λ Control.Arrow > :t p
p :: (Arrow a', Arrow a) =>
(a b b -> a (b, b) (b, b), a' b b -> a' (b, b) (b, b))

但是当您尝试将其绑定(bind)到模式时:
λ Control.Arrow > let (a, b) = p

它失败。约束在配对类型之外,并且对于配对的其他部分是多余的,因为
λ Control.Arrow > :set -XImpredicativeTypes 
λ Control.Arrow > let p = (first, second) :: (Arrow a => a b b -> a (b, b) (b, b), Arrow a => a b b -> a (b, b) (b, b))
λ Control.Arrow > let (a, b) = p

作品。

简单的例子:
λ Prelude Data.Monoid > :t (mappend, ())
(mappend, ()) :: Monoid a => (a -> a -> a, ())
λ Prelude Data.Monoid > let (a, b) = (mappend, ())

<interactive>:12:5:
No instance for (Monoid a0)
arising from the ambiguity check for ‘b’
The type variable ‘a0’ is ambiguous
When checking that ‘b’ has the inferred type ‘()’
Probable cause: the inferred type is ambiguous

一个必须携带约束,但没有 a类型为 () ,即 Monoid a => ()是模棱两可的类型。

注: let (a,b) = ((+), (*))似乎工作。我不知道为什么以及如何 Num被特殊对待:
λ Prelude Data.Monoid > let x = () ::  Num a => ()
λ Prelude Data.Monoid > :t x
x :: ()
λ Prelude Data.Monoid > let x = () :: Monoid m => ()

<interactive>:12:9:
No instance for (Monoid m0)
...

关于haskell - Control.Arrow : Why "let (a,b) = (first,second)" fails?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37796783/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com