Haskell中`->`,`=`和``的优先顺序是什么? [英] What is the precedence of `->`, `=` and ` ` in Haskell?

查看:94
本文介绍了Haskell中`->`,`=`和``的优先顺序是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出Haskell中的一些默认运算符优先级,但是我找不到关于->=的一些好的文档(如f x).因此,我尝试在GHCI中使用:i (->):i (=)来获取一些信息,但这给了我一个语法错误.

I'm trying to figure out some default operator precedences in Haskell, but I was unable to find some good documentation on ->, = and (as in f x). So I tried :i (->) and :i (=) in GHCI to get some info, but it gives me a syntax error.

显然,这些令牌"只是语法的内置部分,因此,难怪:i不起作用.

Apparently these "tokens" are just a built-in part of the syntax, so no wonder, that :i doesn't work.

我是Haskell的新手,所以我没有意识到以下事实,即=不会返回任何值,我只是错误地认为,它在命令式语言中的行为等同于它,这是错误的当然.

I'm new to Haskell, so I wasn't aware of the fact, that = doesn't return any value, I just mistakingly assumed, that it behaves as its equivalents in imperative languages, which is wrong of course.

->充当运算符.它们分别返回类型/值和右/左关联.与实际的运算符一起使用时,它们具有某种性能.

-> and , on the other hand, behave as operators. They return a type/value and are right/left associative respectively. And they have some sort of perecedence when used along with actual operators.

推荐答案

  • ->是类型级别的运算符((->) :: * -> * -> *),如注释中所述,:i (->)显示它是infixr 0 *.
  • 可以将函数应用程序视为具有无限高"的左优先级,也就是说,如果%是任意运算符,则无论%具有什么优先级,f x % y都将始终读为(f x) % y,并且f x y z始终读为((f x) y) z.**这没有被记录为具有优先级,因为它不是运算符,并且在Haskell中不能声明无限"的优先级.
  • =不能被认为具有优越性,因为它始终是声明而不是表达式,因此在其周围加上括号是荒谬的.它不是运算符,因此不能具有优先级.
    • -> is a type-level operator ((->) :: * -> * -> *), and as mentioned in comments, :i (->) reveals that it is infixr 0*.
    • Function application could be seen as having 'infinitely high' left precedence, that is to say if % is any operator, then f x % y will always be read as (f x) % y no matter what precedence % has, and f x y z is always read as ((f x) y) z.** This isn't documented as having a precedence, because it isn't an operator, and 'infinite' precedence can't be declared in Haskell.
    • = cannot be seen as having precendence, as it is always declaration rather than an expression, so putting parentheses around it is absurd. It is not an operator, hence cannot have precedence.
    • *正如下面的注释中指出的那样,它的行为实际上好像具有优先级infixr -1一样,但这在普通运算符中是不允许的-这是语法而不是语义.

      * As pointed out in a below comment, this actually behaves as if it has precedence infixr -1, but this is not permitted in ordinary operators — this is syntactic rather than semantic.

      **请注意,这是->的对立",可以看作具有无限低"的右优先级.你能明白为什么这很自然吗?

      ** Note that this is the 'opposite' of ->, which could be seen as having 'infinitely low', right precedence. Can you see why this is natural?

      这篇关于Haskell中`->`,`=`和``的优先顺序是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆