我可以在哪里阅读haskell“ - >”运营商? [英] Where can I read up on the haskell "->" operator?
问题描述
我想详细了解haskell的 - >
运算符。我不是很清楚它是如何模糊特殊语法和某种类型类之间的界限,我想要做点什么。特殊的,我已经看到了这样的事情:
pre $实例Monad(( - >)r)其中...
这激怒了我的兴趣。
当我尝试搜索haskell箭头或haskell函数或haskell类函数时,遇到了明显的问题:获取
什么是 - >
被调用,我可以在哪里读到更多关于它的信息?
( - >)
通常被称为功能箭头函数类型构造函数,虽然它确实有一些特殊的语法,但 并没有特别关注它。
它本质上是一个中缀类型运算符。给它两种类型,它给你这些类型之间的函数类型。就像 2 + 3
是来自< - >将
>的语法糖添加到。你可以把它看作( - >)从
>到从
的函数,如果符号很混乱。
pre $ $ $ $ $ $ $ $实例Monad(函数来自)其中...
它清楚地表明我们正在讨论带有某些任意(但是固定)类型参数的函数。实际上,这个monad实例可以在 Control.Monad.Instances ,它基本上与 Reader monad 。
观看来源,它确实很简单:
实例Monad(( - >)r)其中
return = const
f>>> = k = \ r - > k(fr)r
由返回的小数值
忽略参数,并且(>> =)
运算符将参数 r
分配给双方。
值得注意的是,在相应的 Applicative
函数实例中, pure
和(*)
对应于 SKI combinator微积分。
最后,注意符号 - >
也出现在语法的其他或多或少不相关的部分,包括lambda抽象 \ x - > ...
,case表达式 case ... of x - > ...
等。相反的符号< -
也出现在几个不相关的上下文中。不要将它们与函数箭头混淆。
I'd like to read more about haskell's ->
operator. I'm not really clear on how much it blurs the line between special syntax and some sort of type class, and I'd like to do some poking around. Specificalyl, I've seen things like this:
instance Monad ((->) r) where ...
That have piqued my interest.
However, when I try to search for "haskell arrow" or "haskell function" or "haskell class function", I run into the obvious problems of getting results for Control.Arrow
or simple type class tutorials.
What is ->
called and where can I read more about it?
(->)
is often called the "function arrow" or "function type constructor", and while it does have some special syntax, there's not that much special about it.
It's essentially an infix type operator. Give it two types, and it gives you the type of functions between those types. Just like 2 + 3
is syntactic sugar for (+) 2 3
, so is from -> to
syntactic sugar for (->) from to
. You can think of it like Function from to
if the symbols are confusing.
In other words, the instance you mentioned can be read as
instance Monad (Function from) where ...
which makes it clear that we're talking about functions which take arguments of some arbitrary (but fixed) type. In fact, this monad instance is found in Control.Monad.Instances and it is essentially the same as the Reader monad.
Looking at the source, it's really quite simple:
instance Monad ((->) r) where
return = const
f >>= k = \ r -> k (f r) r
The trivial values given by return
ignore the argument, and the (>>=)
operator distributes the argument r
to both sides.
It's also interesting to note that in the corresponding Applicative
instance for functions, pure
and (<*>)
correspond to the K and S combinators of the SKI combinator calculus.
(->)
is also generalized by the Arrow type class. An introduction to arrows can be found here.
Finally, note that the symbol ->
also appears in other more or less unrelated parts of the syntax, including lambda abstractions \x -> ...
, case expressions case ... of x -> ...
, etc. The reverse symbol <-
also occurs in several unrelated contexts. Don't confuse those with the function arrow.
这篇关于我可以在哪里阅读haskell“ - >”运营商?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!