(=<<<<&#;)组合子的鸟名?

时间:2018-03-21 17:16:27

标签: haskell functional-programming combinators

Haskell鸟舍组合子将(=<<)列为:

(a -> r -> b) -> (r -> a) -> r -> b

这是否有官方鸟类名称?或者它可以通过预先存在的派生出来吗?

3 个答案:

答案 0 :(得分:4)

  

这是否有正式的鸟类名称?

我无法在Data.Aviary.Birds找到它,所以我认为没有。如果有,它可能会在您链接的列表中被引用。

  

或者它可以通过预先存在的派生出来吗?

当然。最简单的可能是从签名相似的starling开始,然后用flip组成,即

(=<<) = bluebird starling cardinal

答案 1 :(得分:3)

也许会像:blackbird warbler bluebird 这就像

(...) = (.) . (.) -- blackbird
(.) -- bluebird
join -- warbler
-- and your function will be 
f = join ... (.)

答案 2 :(得分:1)

引用评论:

  

顺便问一下,您对如何组合组合以获得特定签名有任何建议吗?我觉得我错过了一些技巧(我目前盯着名单并做精神体操的技巧不能很好地扩展!)

让类型指导你。您正在寻找:

http://your/server/url/calls

虽然你不会在the list中找到它,但有一些非常相似的东西:

-- This name is totally made-up.
mino :: (b -> a -> c) -> (a -> b) -> a -> c

如果我们有办法以某种方式将starling :: (a -> b -> c) -> (a -> b) -> a -> c 扭曲成我们想要的......

starling

这个神秘的mino :: (b -> a -> c) -> (a -> b) -> a -> c mino = f starling -- f :: ((a -> b -> c) -> (a -> b) -> a -> c) -> (b -> a -> c) -> (a -> b) -> a -> c 有一个相当笨拙的类型,所以让我们暂时缩写:fx ~ b -> a -> cy ~ a -> b -> c,我们有

z -> (a -> b) -> a -> c

另一个列表显示这符合f :: (y -> z) -> x -> z 的结果类型:

queer

进展!

queer :: (a -> b) -> (b -> c) -> a -> c

对于mino :: (b -> a -> c) -> (a -> b) -> a -> c mino = queer g starling -- g :: x -> y -- g :: (b -> a -> c) -> a -> b -> c ,在列表顶部附近有一个很棒的候选人:

g

它就是:

cardinal :: (a -> b -> c) -> b -> a -> c

mino :: (b -> a -> c) -> (a -> b) -> a -> c mino = queer cardinal starling 当然是queer(即反向函数组合),它将我们带回Bergi's bluebird starling cardinal

GHC实际上可以帮助您进行这种推导:

cardinal bluebird
import Data.Aviary.Birds

mino :: (b -> a -> c) -> (a -> b) -> a -> c
mino = _f starling

如果你想要一个干净的输出,你必须轻轻地问:

GHCi> :l Mino.hs
[1 of 1] Compiling Main             ( Mino.hs, interpreted )

Mino.hs:4:8: error:
    * Found hole:
        _f
          :: ((a0 -> b0 -> c0) -> (a0 -> b0) -> a0 -> c0)
             -> (b -> a -> c) -> (a -> b) -> a -> c
      Where: `b0' is an ambiguous type variable
             `a0' is an ambiguous type variable
             `c0' is an ambiguous type variable
             `b' is a rigid type variable bound by
               the type signature for:
                 mino :: forall b a c. (b -> a -> c) -> (a -> b) -> a -> c
               at Mino.hs:3:1-43
             `a' is a rigid type variable bound by
               the type signature for:
                 mino :: forall b a c. (b -> a -> c) -> (a -> b) -> a -> c
               at Mino.hs:3:1-43
             `c' is a rigid type variable bound by
               the type signature for:
                 mino :: forall b a c. (b -> a -> c) -> (a -> b) -> a -> c
               at Mino.hs:3:1-43
      Or perhaps `_f' is mis-spelled, or not in scope
    * In the expression: _f
      In the expression: _f starling
      In an equation for `mino': mino = _f starling
    * Relevant bindings include
        mino :: (b -> a -> c) -> (a -> b) -> a -> c (bound at Mino.hs:4:1)
  |
4 | mino = _f starling
  |        ^^
Failed, no modules loaded.

{-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE PartialTypeSignatures #-} import Data.Aviary.Birds mino :: forall b a c. (b -> a -> c) -> (a -> b) -> a -> c mino = let s :: (a -> b -> c) -> _ s = starling in _f s 的类型注释会使定义starling变得不必要;但是,使用更复杂的表达式,这种风格会很快变得难看。)

s

上面描述的过程仍然涉及相当多的盯着名单,因为我们正在使用除了他们的无畏威严的鸟类之外的任何东西。但是,如果没有这些限制,我们可能会以不同的方式进行:

GHCi> :l Mino.hs
[1 of 1] Compiling Main             ( Mino.hs, interpreted )

Mino.hs:10:8: error:
    * Found hole:
        _f
          :: ((a -> b -> c) -> (a -> b) -> a -> c)
             -> (b -> a -> c) -> (a -> b) -> a -> c
      Where: `b' is a rigid type variable bound by
               the type signature for:
                 mino :: forall b a c. (b -> a -> c) -> (a -> b) -> a -> c
               at Mino.hs:6:1-57
             `a' is a rigid type variable bound by
               the type signature for:
                 mino :: forall b a c. (b -> a -> c) -> (a -> b) -> a -> c
               at Mino.hs:6:1-57
             `c' is a rigid type variable bound by
               the type signature for:
                 mino :: forall b a c. (b -> a -> c) -> (a -> b) -> a -> c
               at Mino.hs:6:1-57
      Or perhaps `_f' is mis-spelled, or not in scope
    * In the expression: _f
      In the expression: _f s
      In the expression:
        let
          s :: (a -> b -> c) -> _
          s = starling
        in _f s
    * Relevant bindings include
        s :: (a -> b -> c) -> (a -> b) -> a -> c (bound at Mino.hs:9:9)
        mino :: (b -> a -> c) -> (a -> b) -> a -> c (bound at Mino.hs:7:1)
   |
10 |     in _f s
   |        ^^
Failed, no modules loaded.

该洞的类型为mino :: (b -> a -> c) -> (a -> b) -> a -> c mino g f = _ ,因此我们知道这是一个需要a -> c的函数:

a

此处mino :: (b -> a -> c) -> (a -> b) -> a -> c mino g f = \x -> _ -- x :: a 唯一需要的是a

g

洞的类型现在是mino :: (b -> a -> c) -> (a -> b) -> a -> c mino g f = \x -> g _ x ,唯一能提供b的是[{1}}:

b

这当然是读者f的通常定义。如果我们翻转mino :: (b -> a -> c) -> (a -> b) -> a -> c mino g f = \x -> g (f x) x ,但是......

(=<<)

...读者g(即S组合子)变得可识别:

mino :: (b -> a -> c) -> (a -> b) -> a -> c
mino g f = \x -> flip g x (f x)

然后我们可以写点免费...

(<*>)

...并翻译成birdpeak:

mino :: (b -> a -> c) -> (a -> b) -> a -> c
mino g f = \x -> (<*>) (flip g) f x