如何自动将任意haskell表达式括起来? [英] How to automatically parenthesize arbitrary haskell expressions?

查看:86
本文介绍了如何自动将任意haskell表达式括起来?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



当然,我期望这样,只要有足够的余量实践中,精神解析haskell将成为第二性质,但与此同时,为了理解我遇到的情况,我想找到一些自动的方法来翻译任意的标准haskell 1 < sup>表达式转化为其中通过引入必要的括号来消除所有不明确的 2 子表达式的表达式。 例如,它会翻译表达式

  fghi 

...

 ((fg)h)i 

...,或

  a  - > b  - > c  - > d 

...进入

  a  - > (b  - >(c  - > d))

...等等。 p>

最好,这是我可以通过手机访问的一个工具,因为我在阅读haskell时已经把大部分时间从一台合适的计算机上读取。






1 当然,没有这样的工具可能适用于未知固定性和相关性的自定义运算符。 标准哈斯克尔的意思是前奏和标准哈斯克尔库中定义的东西。



2 我在这里用含糊不清作为没有优先规则时含糊不清的简写。例如。 2 + 3 * 5 是不明确的,除非有一些优先规则解决了两个操作中的哪一个会首先执行的问题。

解决方案

如果你真的想去做它的工作,你可以写一个 TemplateHaskell 函数为你做这个 - 你基本上只是走AST并随意添加parens。我开始这样做,但意识到它会很快很乏味。我觉得现在可能会更方便,因为你只有在实际进场时才考虑咖喱(带有未完全应用的功能)。




但是,您可以使用一个技巧来处理问题的子代:围绕着您不熟悉的固定性和关联性的操作符。在GHCi中,使用正确的标志启动后,只需在 $([|< expression> |])中包装感兴趣的表达式。然后,在评估结果之前,您可以看到表达式的圆括号。

  $ ghci -ddump-splices  - XTemplateHaskell 
前奏> $([| 1 + 2 ^ 3 * 4 |])
< interactive>:1:3-21:拼接表达式
[| 1 + 2 ^ 3 * 4 |] ======> (1 +((2 ^ 3)* 4))
33
前奏> $([| 1 | $ pure 4 |> = \ x - > const mempty =< [(+),(*)] * [1,2] [False] |])
< interactive>:2:3-77:拼接表达式
[| 1< $ pure 4
>> =
(\ x_a6PT→const mempty = ((1 <$(纯4))
>> =
(< * [False] |]
======>
((const mempty)=<(([(+),(*)] * [1,2])* [False]) )))
[]
Prelude>

然而, t以您想要的方式修复类型签名或函数应用程序。


I'm learning haskell, and I have a lot of difficulty with mentally parsing many haskell expressions I come across.

Of course, I expect that, with enough practice, mentally parsing haskell will become second-nature, but in the meantime, in order to make sense of what I come across, I'd like to find some automatic way to translate an arbitrary "standard haskell"1 expression into one in which all "ambiguous"2 sub-expressions have been eliminated by introducing the necessary parentheses.

For example, it would translate the expression

f g h i

...into

((f g) h) i

..., or

a -> b -> c -> d

...into

a -> (b -> (c -> d))

..., etc.

Preferably, this would be a tool I can access with my phone, since I do much of my reading on haskell away from a proper computer.


1Of course, no such tool could possibly work with custom operators of unknown fixity and associativity. By "standard haskell" I mean the stuff defined in the prelude and in the standard haskell library.

2I'm using "ambiguous" here as shorthand for "ambiguous in the absence of precedence rules". E.g. 2 + 3 * 5 is ambiguous unless there's some precedence rule that settles the question of which of the two operations will be performed first.

解决方案

If you really want to go to the work of it, you could write a TemplateHaskell function to do this for you - you essentially just walk the AST and add parens at will. I started doing that, but realized it will get pretty long and tedious pretty fast. I think it may be more convenient for the moment for you to only think about currying when it actually comes into play (with functions that are not fully applied).


However, there is a trick you can use for a subcase of your problem: parens around operators whose fixity and associativity you aren't familiar with. In GHCi, after booting up with the right flags, just wrap the expression you are interested in inspecting in $([| <expression> |]). Then, you get to see a parenthesized version of your expression before the result of evaluating it.

$ ghci -ddump-splices -XTemplateHaskell
Prelude> $([| 1 + 2 ^ 3 * 4 |])
<interactive>:1:3-21: Splicing expression
    [| 1 + 2 ^ 3 * 4 |] ======> (1 + ((2 ^ 3) * 4))
33
Prelude> $([| 1 <$ pure 4 >>= \x -> const mempty =<< [(+),(*)] <*> [1,2] <* [False] |])
<interactive>:2:3-77: Splicing expression
    [| 1 <$ pure 4
       >>=
         (\ x_a6PT -> const mempty =<< [(+), (*)] <*> [1, 2] <* [False] |]
  ======>
    ((1 <$ (pure 4))
     >>=
       (\ x_a6PT
          -> ((const mempty) =<< (([(+),(*)] <*> [1,2]) <* [False]))))
[]
Prelude>

However, this definitely won't fix the type signatures or function applications the way you want.

这篇关于如何自动将任意haskell表达式括起来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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