why pipes defines inner functions

天大地大妈咪最大 提交于 2020-01-02 04:36:04

问题


I'm looking at the pipes library source code and for instance in the Core module I don't understand why the author is all over the place using the pattern of defining functions like that:

runEffect = go
  where
    go p = ...

Or:

pull = go
  where
    go a' = ...

Or:

reflect = go
  where
    go p = ...

Is this some trick to enable some optimizations? I find it ugly, if it's some optimization trick I really wish the compiler could do it without things like that. But maybe there's another reason?


回答1:


GHC will only inline non-recursive functions, and only when they are "fully applied" from a syntactic point of view (i.e. at the call site they are applied to the number of arguments that appear in the left hand side in the definition).

In the examples you posted there are no arguments, however the definitions are likely recursive and wouldn't be inlined. Doing this transformation probably allows the definitions to be inlined and specialized (for the concrete types of m etc.) at the call site.

Is this some trick to enable some optimizations? I find it ugly, if it's some optimization trick I really wish the compiler could do it without things like that.

Yeah it's super lame.



来源:https://stackoverflow.com/questions/31168743/why-pipes-defines-inner-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!