Can a compiler automatically detect pure functions without the type information about purity?

后端 未结 4 778
暖寄归人
暖寄归人 2021-01-01 09:14

So I\'m arguing with my friend who claims that a compiler like GCC can detect a pure function automatically without any type information. I doubt that.

Languages lik

4条回答
  •  情话喂你
    2021-01-01 09:38

    Determining if a function is pure (even in the limited sense used by GCC) is equivalent to the halting problem, so the answer is "not for arbitrary functions." It is possible to automatically detect that some functions are pure, others are not pure, and flag the rest as "unknown", which allows for automatic parallelization in some cases.

    In my experience, even programmers aren't very good at figuring out such things, so I want the type system to help keep track of it for me, not just for the optimizer.

提交回复
热议问题