受限函数上的函数相等

发布于 2024-10-15 03:51:25 字数 722 浏览 2 评论 0原文

我已经发布了一个关于函数相等的问题。它很快得出结论,一般函数相等是一个极其困难的问题,并且可能在数学上无法证明。

我想存根一个函数

function equal(f, g, domain) {

}

f & g 是带有一个参数的停止函数。他们的论证是一个自然数。这些函数将返回一个布尔值。

如果没有传递域,那么您可以假设域默认为所有自然数。

domain 的结构对于 equal 函数来说是最方便的。

另一个重要的事实是 f & g 是确定性的。并将始终为 f(n) 返回相同的布尔值 m

您可以假设 fg 始终返回,并且只要它们的输入位于 domain

这个问题与语言无关,并且要求实现 equal 函数。我不确定 SO 是否是合适的地方。

f & g没有副作用。并且域不必是有限的。

I already posted a question about function equality. It quickly concluded that general function equality is an incredibly hard problem and might be mathematically disprovable.

I would like to stub up a function

function equal(f, g, domain) {

}

f & g are halting functions that take one argument. Their argument is an natural number. These functions will return a boolean.

If no domain is passed then you may assume the domain defaults to all natural numbers.

The structure of domain is whatever is most convenient for the equal function.

Another important fact is that f & g are deterministic. and will consistantly return the same boolean m for f(n).

You may assume that f and g always return and don't throw any exceptions or crash due to errors as long as their input is within the domain

The question is language-agnostic and Asking for an implementation of the equal function. i'm not certain whether SO is the right place for this anymore.

f & g have no side-effects. and the domain does not have to be finite.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

黒涩兲箜 2024-10-22 03:51:25

这仍然是不可能的。

您可以针对有限数量的输入测试这两个函数,并检查它们在这些输入上是否相等。如果它们对于任何输入都不相等,则这两个函数不相同。如果它们在您测试的每种情况下都相等,那么它们很可能是相同的函数,但您不能完全确定。

一般来说,除非域很小,否则测试每个可能的输入是不可行的。如果域是 32 位整数并且您的函数计算速度相当快,那么检查每个可能的输入可能是可行的。

It's still not possible.

You could test both functions for some finite number of inputs and check them for equality on those inputs. If they are unequal for any input then the two functions are not identical. If they are equal in every case you tested then there is a reasonable chance that they are the same function, but you can't be completely certain.

In general it is infeasible to test every possible input unless the domain is small. If the domain is a 32 bit integer and your function is quite fast to evaluate then it might be feasible to check every possible input.

×眷恋的温暖 2024-10-22 03:51:25

我相信以下是无需对源代码进行静态分析即可完成的最佳操作:

function equal(f, g, domain) {
    var n;
    for (n in domain) {
        if (f(domain[n]) != g(domain[n])) return false;
    }
    return true;
}

请注意,这假设域是有限的。

如果域不是有限的,莱斯定理会阻止这样的算法存在:

如果我们让 f 和 g 为实现,F 和 G 是这些实现计算其值的数学函数,那么莱斯定理表明不可能确定 f 计算 G 还是 g 计算 F,因为这些是实现的非平凡属性。

有关更多详细信息,请参阅我对上一个问题的回答

I believe the following to be the best you can do without doing static analysis on the source code:

function equal(f, g, domain) {
    var n;
    for (n in domain) {
        if (f(domain[n]) != g(domain[n])) return false;
    }
    return true;
}

Note that this assumes the domain to be finite.

If the domain is not finite, Rice's theorem prevents such an algorithm from existing:

If we let f and g be the implementations and F and G be the mathematical functions these implementations calculate the values of, then it's Rice's theorem says that it's impossible to determine if f calculates G or g calculates F, as these are non-trivial properties of the implementations.

For further detail, see my answer to the previous question.

烟─花易冷 2024-10-22 03:51:25

根据您的用例,您也许能够对 f & 做一些假设。 g .也许在你的情况下,它们在特定条件下应用可能使它可以解决。

在其他情况下,我唯一推荐的是在抽象语法树或其他表示上进行模糊测试。

Depending on your use-case, you might be able to do some assumptions about f & g . Maybe in your case, they apply under specific conditions what might make it solvable.

In other cases, the only thing what I might recommend is fuzzy testing , on Abstract Syntax Tree or other representation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文