为什么确定一个功能是纯粹的困难吗? [英] Why is determining if a function is pure difficult?

查看:131
本文介绍了为什么确定一个功能是纯粹的困难吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我昨天是在计算器开发天公约和扬声器的人谈论的Python。他表现出了memoize的功能,我问是否有什么办法可以阻止它在非纯函数被使用。他说没有,那基本上是不可能的,如果有人能想出办法来做到这一点,将成为一位伟大的博士论文。

I was at the StackOverflow Dev Days convention yesterday, and one of the speakers was talking about Python. He showed a Memoize function, and I asked if there was any way to keep it from being used on a non-pure function. He said no, that's basically impossible, and if someone could figure out a way to do it it would make a great PhD thesis.

之类的我感到困惑,因为它似乎并不那么困难的编译器/间preTER解决递归。在伪code:

That sort of confused me, because it doesn't seem all that difficult for a compiler/interpreter to solve recursively. In pseudocode:

function isPure(functionMetadata): boolean;
begin
   result = true;
   for each variable in functionMetadata.variablesModified
      result = result and variable.isLocalToThisFunction;
   for each dependency in functionMetadata.functionsCalled
      result = result and isPure(dependency);
end;

这是基本的思路。显然,你需要某种形式的检查,就互相依赖的函数prevent无限递归的,但是这不是太难以成立。

That's the basic idea. Obviously you'd need some sort of check to prevent infinite recursion on mutually-dependent functions, but that's not too difficult to set up.

高阶函数,把函数指针可能是有问题的,因为他们不能静态地验证,但我原来的问题presupposes编译器具有某种语言的约束来指定,只有纯函数指针可被传递到一个特定参数。如果存在,可以用来满足条件

Higher-order functions that take function pointers might be problematic, since they can't be verified statically, but my original question presupposes that the compiler has some sort of language constraint to designate that only a pure function pointer can be passed to a certain parameter. If one existed, that could be used to satisfy the condition.

显然,这将是编译型语言更容易比PTED之一的跨$ P $,因为这一切的数字处理程序执行前会完成,所以不会减慢什么,但我真的没有看到任何基本的问题,就不可能以评价

Obviously this would be easier in a compiled language than an interpreted one, since all this number-crunching would be done before the program is executed and so not slow anything down, but I don't really see any fundamental problems that would make it impossible to evaluate.

有没有人在这方面更多的知识知道我缺少的是什么?

Does anyone with a bit more knowledge in this area know what I'm missing?

推荐答案

这是特别难在Python。由于 anObject.aFunc 可以在运行时任意改变,你不能确定在编译时该功能将 anObject.aFunc()打电话或者即使这将是一个功能都没有。

It is particularly hard in Python. Since anObject.aFunc can be changed arbitrarily at runtime, you cannot determine at compile time which function will anObject.aFunc() call or even if it will be a function at all.

这篇关于为什么确定一个功能是纯粹的困难吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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