为什么管道定义内部函数
why pipes defines inner functions
我在看pipes library source code and for instance in the Core module我不明白为什么作者到处都是使用这样定义函数的模式:
runEffect = go
where
go p = ...
或者:
pull = go
where
go a' = ...
或者:
reflect = go
where
go p = ...
这是启用某些优化的一些技巧吗?我觉得它很难看,如果它是一些优化技巧,我真的希望编译器可以在没有类似东西的情况下做到这一点。但也许还有其他原因?
GHC 将仅内联非递归函数,并且仅当它们从句法的角度来看 "fully applied" 时(即在调用站点,它们将应用于出现在左侧的参数数量定义中的一侧)。
在您发布的示例中没有参数,但是定义可能是递归的并且不会被内联。进行此转换可能允许在调用站点对定义进行内联和专门化(对于 m
等的具体类型)。
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.
是的,超级蹩脚。
我在看pipes library source code and for instance in the Core module我不明白为什么作者到处都是使用这样定义函数的模式:
runEffect = go
where
go p = ...
或者:
pull = go
where
go a' = ...
或者:
reflect = go
where
go p = ...
这是启用某些优化的一些技巧吗?我觉得它很难看,如果它是一些优化技巧,我真的希望编译器可以在没有类似东西的情况下做到这一点。但也许还有其他原因?
GHC 将仅内联非递归函数,并且仅当它们从句法的角度来看 "fully applied" 时(即在调用站点,它们将应用于出现在左侧的参数数量定义中的一侧)。
在您发布的示例中没有参数,但是定义可能是递归的并且不会被内联。进行此转换可能允许在调用站点对定义进行内联和专门化(对于 m
等的具体类型)。
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.
是的,超级蹩脚。