2

我试过这个:

type TestT = Either Int Float

testM :: (a -> a) -> TestT -> TestT
testM f (Left x) = Left (f x)
testM f (Right x) = Right (f x)

但它不起作用,有没有办法做到这一点?我环顾四周,所有类似的东西都非常复杂和有限。

错误消息,根据要求:

Main.hs:101:28: error:
    • Couldn't match expected type ‘a’ with actual type ‘Int’
      ‘a’ is a rigid type variable bound by
        the type signature for:
          testM :: forall a. (a -> a) -> TestT -> TestT
        at Main.hs:100:1-35
    • In the first argument of ‘f’, namely ‘x’
      In the first argument of ‘Left’, namely ‘(f x)’
      In the expression: Left (f x)
    • Relevant bindings include
        f :: a -> a (bound at Main.hs:101:7)
        testM :: (a -> a) -> TestT -> TestT (bound at Main.hs:101:1)
4

2 回答 2

3

一种方法是使用 Bifunctor:

Prelude> :m +Data.Bifunctor
Prelude Data.Bifunctor> bimap show show (Left 3)
Left "3"
Prelude Data.Bifunctor> bimap show show (Right 'x')
Right "'x'"
Prelude Data.Bifunctor>
于 2020-12-09T03:26:17.177 回答
3

我认为你不能用基本语言做到这一点。如评论中所述,您可能需要启用几个扩展,例如 RankNTypes。

由于所有涉及的类型都是数字类型,因此很容易使用增量函数,例如 (+1) 作为多态函数。

让我们试试ghci

$ ghci
GHCi, version 8.6.5: http://www.haskell.org/ghc/  :? for help
 λ> 
 λ> type TestT = Either Int Float 
 λ> 
 λ> :set +m
 λ> 
 λ> :set -XRankNTypes
 λ> :set -XScopedTypeVariables
 λ> 
 λ> {-
|λ> let { testM :: (forall a. Num a => a -> a) -> TestT -> TestT ;
|λ>       testM fn (Left x) = Left (fn x) ;
|λ>       testM fn (Right x) = Right (fn x) }
|λ> -}
 λ> 
 λ> :type testM
testM :: (forall a. Num a => a -> a) -> TestT -> TestT
 λ> 
 λ> testM (+3) (Left 42)
Left 45
 λ> 
 λ> testM (+3) (Right 3.14159)
Right 6.14159
 λ> 

注意 1:如果您省略语言扩展,它会中断,并显示一条提示 RankNTypes 的消息。

注意 2:如果您使用forall a. Num a => (a -> a)而不是(forall a. Num a => a -> a),它也会中断。

注 3:此处的一些现有技术:SO-q38298119以及来自 Alexis King 的有用评论。

于 2020-12-05T20:30:19.977 回答