简体   繁体   English

Haskell:理解部分应用?

[英]Haskell: understanding partial application?

I'm reading the LYAH chapter on applicative functors, and I don't seem to understand the following example: 我正在阅读有关应用仿函数的LYAH章节 ,我似乎不理解以下示例:

ghci> :t fmap (++) (Just "hey")  
fmap (++) (Just "hey") :: Maybe ([Char] -> [Char])

But when I look at this: 但是当我看到这个:

ghci> :t (++)
(++) :: [a] -> [a] -> [a]
ghci> :t fmap
fmap :: Functor f => (a -> b) -> f a -> f b

I do understand how something like (*3) or (++"this") fits into the (a -> b) type, but I just can't see how [a] -> [a] -> [a] fits into (a -> b) ? 我确实理解(* 3)(++“this”)类似于 a - > b)类型,但我只是看不出[a] - > [a] - > [a]适合(a - > b)

The key is that -> associates to the right, so a type like a -> b -> c is really a -> (b -> c) . 关键是->右边的关联,所以像a -> b -> c这样的类型实际上a -> (b -> c) So [a] -> [a] -> [a] fits into c -> d by setting c ~ [a] and d ~ [a] -> [a] . 所以[a] -> [a] -> [a]通过设置c ~ [a]d ~ [a] -> [a]拟合c -> d You can view a function [a] -> [a] -> [a] either as a function of 2 arguments that returns a result of type [a] , or a function of 1 argument that returns a result of type [a] -> [a] . 您可以查看函数[a] -> [a] -> [a]作为返回类型[a]的结果的2个参数的函数,或者返回类型[a] -> [a]的结果的1个参数的函数[a] -> [a]

The thing to realise is that the b in a -> b doesn't have to be a scalar - it can be a function. 要认识到的一点是,在ba -> b不必是标-它可以是一个函数。

[a] -> [a] -> [a] can be thought of as [a] -> ([a] -> [a]) , so b is [a] -> [a] [a] -> [a] -> [a]可以被认为是[a] -> ([a] -> [a]) ,所以b[a] -> [a]

Putting the stuff side-by-side as usual, 像往常一样把东西放在一边,

fmap :: Functor f => ( a    ->      b      )   ->      f a        ->   f b
fmap                       (++)                    (Just "hey")   ::   f b
(++) ::               [c]   -> ([c] -> [c])

So, 所以,

a ~ [c]  ,    b ~ ([c] -> [c])  ,    f ~ Maybe  ,    a ~ [Char]  ,   c ~ Char

f b ~ Maybe b ~ Maybe ([c] -> [c]) ~  Maybe ([Char] -> [Char])

No thinking is involved here. 这里不涉及任何想法。 Unification of types is a mechanical process. 类型的统一是一个机械过程。


And to answer your specific question (paraphrased), " how [c] -> [c] -> [c] can be matched with a -> b " , here goes: 并回答你的具体问题(释义), “如何[c] -> [c] -> [c]可以与a -> b ”匹配 ,这里是:

  • Omitting parentheses in type signatures is evil (when teaching Haskell to newbies) 在签名类型中省略括号是邪恶的 (将Haskell教给新手时)
  • In Haskell, there are no binary functions . 在Haskell中, 没有二进制函数 Every function is unary. 每个功能都是一元的。
  • Hence (as others mentioned already), arrows in type signatures associate to the right . 因此(正如其他已经提到的那样), 类型签名中的箭头与右侧相关联

It is simple really :-). 这很简单:-)。 let me add a simple parenthesis: 让我添加一个简单的括号:

[a]->[a]->[a] is like [a]->([a]->[a]) [a]->[a]->[a]就像[a]->([a]->[a])

So it fits in a->b by replacing a by [a] and b by [a]->[a] . 因此它通过将[a]和b替换为[a]->[a]来拟合a->b You give a string to ++ and you get a function of type string->string in return 你给一个字符串给++,你得到一个string->string类型的函数作为回报

fmap (++) (Just "hey") is a maybe monad holding a function which prefix the string "hey" to another string. fmap (++) (Just "hey")是一个可能的monad,它持有一个函数,该函数将字符串“hey”加到另一个字符串fmap (++) (Just "hey") It is indeed of type Maybe ([Char] -> [Char]) 它确实是类型Maybe ([Char] -> [Char])

Consider the definition of fmap for the Maybe type. 考虑Maybe类型的fmap定义。

fmap f (Just x) = Just (f x)

which for your example looks like 对于你的例子看起来像

fmap (++) (Just "Hey") = Just ("Hey" ++) :: Maybe ([Char] -> [Char])

As fmap should, you have simply lifted the (++) function inside the Maybe container and applied it to the contents. 如fmap所示,您只需在Maybe容器中解除(++)函数并将其应用于内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM