问题
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