functional-programming - 什么是 "strongly moded"编程语言?

标签 functional-programming language-lawyer mode mercury

我正在浏览 Mercury 编程语言的 about page当我发现它说的一部分时:

Mercury is a strongly moded language


这是什么意思!?我在互联网上搜索了所有内容,都没有找到答案!

最佳答案

我不知道有任何其他语言 modes如在 mercury 中使用。以下来自 Mercurial 手册

The mode of a predicate (or function) is a mapping from the initial
state of instantiation of the arguments of the predicate (or the
arguments and result of a function) to their final state of
instantiation.
如果您熟悉 prolog,您可能知道这意味着什么。
考虑 C 中具有以下 typedecl 的函数
void sort(int * i, int * o);
假设这个函数对数组 i 进行排序进入另一个数组 o .仅凭此声明,我们无法保证 i读取自和 o被写入。如果我们可以另外写 mode sort(in, out)这向编译器建议函数 sort 从第一个参数读取并写入第二个参数。然后编译器检查函数体以向我们保证没有写入 i并阅读 o发生。
对于像 C 这样的语言这可能不适合,但对于 prolog家庭语言这是一个非常受欢迎的功能。考虑 append/3当前两个列表连接时成功的谓词是第三个列表。
append([1, 2], [a, b], X).
X = [1, 2, a, b]
所以如果我们提供两个输入列表,我们就会得到一个输出列表。但是当我们提供输出列表并询问导致它的所有解决方案时,我们有
append(X, Y, [1, 2, a, b]).
X = [],
Y = [1, 2, a, b] ;
X = [1],
Y = [2, a, b] ;
X = [1, 2],
Y = [a, b] ;
X = [1, 2, a],
Y = [b] ;
X = [1, 2, a, b],
Y = [] ;
false.
append([1], [2], [3]).失败,如 append([1], [2], [1, 2]).成功。
因此,根据我们如何使用谓词,我们可以有一个确定性的答案、多个答案或根本没有答案。谓词的所有这些属性最初都可以通过模式声明来声明。以下是 append 的模式声明:
:- pred append(list(T), list(T), list(T)).
:- mode append(in, in, out) is det.
:- mode append(out, out, in) is multi.
:- mode append(in, in, in) is semidet.
如果您提供前两个,则输出是确定性确定的。如果您提供最后一个参数,那么您对前两个参数有多种解决方案。如果您提供所有三个列表,那么它只会检查当附加前两个列表时我们是否得到第三个列表。
模式不限于输入、输出。你会看到 di破坏性输入和 uo处理 IO 时的唯一输出。它们只是告诉我们谓词如何改变我们提供的参数的实例化。输出从自由变量变为接地项,而输入保持接地项。因此,作为用户,您可以定义 :- mode input == ground >> ground.:- mode output == free >> ground.并使用它们,这正是 inout模式被定义。
考虑一个计算列表长度的谓词。我们不需要实例化整个列表,因为我们知道即使 X, Y 是自由变量,length([X, Y], 2) 也是真的。所以模式声明:- mode length(in, out) is det.是不够的,因为不需要实例化整个第一个参数。所以我们也可以定义参数的实例化:- inst listskel == bound([] ; [free | listskel]).它指出一个参数是 listskel如果它是一个空列表或另一个前面的自由变量,则实例化 listskel列表。
由于 Haskell 的惰性,这种部分评估也发生在 Haskell 中,例如,不需要评估整个列表以了解其长度。
引用:
modes
determinism
编辑:来自 Mercurial 网站 Currently only a subset of the intended mode system is implemented. This subset effectively requires arguments to be either fully input (ground at the time of call and at the time of success) or fully output (free at the time of call and ground at the time of success).

关于functional-programming - 什么是 "strongly moded"编程语言?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64560357/

相关文章:

scala - 用Scala中的占位符替换字符串中的值

c++ - 假设相同的 lambda 表达式具有不同的类型是否安全?

c++ - 代码因未初始化的变量而崩溃,即使我实际上并没有使用它

OpenCV:计算一组 cv::Mat 的统计模式

function - 函数的参数太多

scala - 具有匿名函数和占位符的 val 和 var 的不同行为

python - 重新采样 pandas 数据框并应用模式

wpf - KeyboardNavigationMode Contained 和 Cycle 之间的区别?

performance - 为什么在函数式编程中计算阶乘更快/更有效?

c++ - int a[] = {1,2,};为什么允许在初始化列表中使用尾随逗号?