程序的导数
让我们假设您可以将程序表示为数学函数,这是可能的。该函数的一阶导数的程序表示是什么样的?有没有办法将程序转换为其“衍生”形式,这是否有意义?
Let us assume you can represent a program as mathematical function, that's possible. How does the program representation of the first derivative of that function look like? Is there a way to transform a program to its "derivative" form, and does this make sense at all?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果程序被表示为分布(Schwartz),那么您就会有一些导数的概念,假设测试函数对您的后置条件进行建模(您仍然可以采用极限来获得特征函数)。例如,赋值
x:=x+1
与狄拉克分布\delta_{x_0+1}
相关联,其中x_0
是初始分布变量x
的值。但是,我不知道\delta_{x_0+1}'
的计算意义是什么。If the program is denoted as a distribution (Schwartz) then you have some notion of derivative assuming that tests functions models your postcondition (you can still take the limit to get a characteristic function). For instance, the assignment
x:=x+1
is associated to the Dirac distribution\delta_{x_0+1}
wherex_0
is the initial value of the variablex
. However, I have no idea what is the computational meaning of\delta_{x_0+1}'
.我想知道,如果您尝试“推导”的程序使用某种形式的启发式怎么办?那么如何推导出来呢?
半开玩笑地说,我们都知道所有真实的程序都至少使用 rand()。
I am wondering, what if the program your're trying to "derive" uses some form of heursitics ? How can it be derived then ?
Half-jokingly, we all know that all real programs use at least a rand().
是的,它确实有道理,它被称为自动微分。有一个或两个实验性编译器可以做到这一点,例如 NAGware 的启用差异化的 Fortran 编译器技术。并且有很多关于该主题的研究论文。我建议你去谷歌搜索。
Yes it does make sense, it's known as Automatic Differentiation. There are one or two experimental compilers which can do this, for example NAGware's Differentiation Enabled Fortran Compiler Technology. And there are a lot of research papers on the topic. I suggest you get Googling.
首先,只有尝试获得纯函数的导数(不影响外部状态并为每个输入返回完全相同的输出)才有意义。其次,许多编程语言的类型系统涉及许多阶跃函数(例如整数),这意味着您必须让程序按照连续函数工作才能获得有效的一阶导数。第三,获得任何函数的导数都需要将其分解并进行符号操作。因此,如果不知道函数是如何进行运算的,就无法获得函数的导数。这可以通过反思来实现。
如果您的编程语言支持闭包(即嵌套函数以及将函数放入变量并返回它们的能力),您可以创建导数近似函数。以下是取自 http://en.wikipedia.org/wiki/Closure_% 的 JavaScript 示例28computer_science%29 :
因此,你可以说:
这里,
f_prime
将近似于function(x) {return 2*x;}
如果编程语言实现得更高-阶函数和足够的代数,可以在其中实现真正的导函数。那真是太酷了。
First, it only makes sense to try to get the derivative of a pure function (one that does not affect external state and returns the exact same output for every input). Second, the type system of many programming languages involves a lot of step functions (e.g. integers), meaning you'd have to get your program to work in terms of continuous functions in order to get a valid first derivative. Third, getting the derivative of any function involves breaking it down and manipulating it symbolically. Thus, you can't get the derivative of a function without knowing how what operations it is made of. This could be achieved with reflection.
You could create a derivative approximation function if your programming language supports closures (that is, nested functions and the ability to put functions into variables and return them). Here is a JavaScript example taken from http://en.wikipedia.org/wiki/Closure_%28computer_science%29 :
Thus, you could say:
Here,
f_prime
will approximatefunction(x) {return 2*x;}
If a programming language implemented higher-order functions and enough algebra, one could implement a real derivative function in it. That would be really cool.
请参阅 Lambda the Ultimate 关于数据类型的派生和剖析和正则表达式的派生
See Lambda the Ultimate discussions on Derivatives and dissections of data types and Derivatives of Regular Expressions
如何定义程序的数学函数?
导数表示函数的变化率。如果您的函数不连续,则其导数在大部分域上将是未定义的。
How do you define the mathematical function of a program?
A derivative represent the rate of change of a function. If your function isn't continuous its derivative will be undefined over most of the domain.
我只是想说这没有多大意义,因为程序比数学函数更加抽象和“无规则”。由于导数是随着输入变化而衡量输出变化的,因此肯定有一些程序可以应用它。 但是,您需要能够以数字形式量化您的输入/输出。
由于输入/输出都是数字,因此可以合理地假设您的程序表示或操作类似于数学函数,或一系列函数。因此,您可以轻松地表示导数,但这与将函数的数学导数转换为计算机程序没有什么不同。
I'm just gonna say that this doesn't make a lot of sense, as a program is much more abstract and "ruleless" than a mathematical function. As a derivative is a measure of the change in output as the input changes, there are certainly some programs where this could apply. However, you'd need to be able to quantify your input/output both in numerical terms.
Since input/output would both numerical, it's reasonable to assume that your program represents or operates similarly to a mathematical function, or series of functions. Hence, you can easily represent a derivative, but it would be no different than converting the mathematical derivative of a function to a computer program.