我是Mercury语言的初学者,虽然我之前学过Prolog。水星的一个新方面是dererminism。 main
函数必须是确定性的。为了做到这一点,我必须检查一个变量是统一/绑定到一个值,但我找不到如何做到这一点。特别是看代码:
main(!IO) :-
mother(X,"john"),
( if bound(X) <-----this is my failed try; how to check if X is unified?
then
io.format("%s\n", [s(X)], !IO)
else
io.write_string("Not available\n",!IO)
).
这样的main
不会失败,即它(我猜)会满足确定性约束。所以问题是如何检查变量是否绑定。
答案 0 :(得分:1)
我已经从这一侧的Prolog example翻译了家谱,以进行比较。
我已经指定了所有事实(人)和他们彼此之间的关系以及一些辅助谓词。
请注意,此类型版本确实找到了您在最常见答案中看到的错误:female(jane).
主谓词不必须是确定性的,它也可以是cc_multi
,这意味着Mercury将提交(不尝试其他)选择;
您可以将mother
替换为parent
来验证这一点。
你也不必检查你的变量的界限,而只是在if子句中使用任何不确定的术语,并且在接下来的部分将保证绑定变量,或者在else部分中不绑定。
如果您希望此示例更具动态性,则必须使用lexer
或term
模块将输入解析为person
原子。
如果您需要所有解决方案,请查看solution
模块。
%-------------------------------%
% vim: ft=mercury ff=unix ts=4 sw=4 et
%-------------------------------%
% File: relationship.m
%-------------------------------%
% Classical example of family relationship representation,
% based on: https://stackoverflow.com/questions/679728/prolog-family-tree
%-------------------------------%
:- module relationship.
:- interface.
:- import_module io.
%-------------------------------%
:- pred main(io::di, io::uo) is cc_multi.
%-------------------------------%
%-------------------------------%
:- implementation.
:- type person
---> john
; bob
; bill
; ron
; jeff
; mary
; sue
; nancy
; jane
.
:- pred person(person::out) is multi.
person(Person) :- male(Person).
person(Person) :- female(Person).
:- pred male(person).
:- mode male(in) is semidet.
:- mode male(out) is multi.
male(john).
male(bob).
male(bill).
male(ron).
male(jeff).
:- pred female(person).
:- mode female(in) is semidet.
:- mode female(out) is multi.
female(mary).
female(sue).
female(nancy).
female(jane).
:- pred parent(person, person).
:- mode parent(in, in) is semidet.
:- mode parent(in, out) is nondet.
:- mode parent(out, in) is nondet.
:- mode parent(out, out) is multi.
parent(mary, sue).
parent(mary, bill).
parent(sue, nancy).
parent(sue, jeff).
parent(jane, ron).
parent(john, bob).
parent(john, bill).
parent(bob, nancy).
parent(bob, jeff).
parent(bill, ron).
:- pred mother(person, person).
:- mode mother(in, in) is semidet.
:- mode mother(in, out) is nondet.
:- mode mother(out, in) is nondet.
:- mode mother(out, out) is nondet.
mother(Mother, Child) :-
female(Mother),
parent(Mother, Child).
:- pred father(person, person).
:- mode father(in, in) is semidet.
:- mode father(in, out) is nondet.
:- mode father(out, in) is nondet.
:- mode father(out, out) is nondet.
father(Father, Child) :-
male(Father),
parent(Father, Child).
%-------------------------------%
main(!IO) :-
Child = john, % try sue or whatever for the first answer
( if mother(Mother, Child) then
io.write(Mother, !IO),
io.print(" is ", !IO),
io.write(Child, !IO),
io.print_line("'s mother", !IO)
else
io.write(Child, !IO),
io.print_line("'s mother is unknown", !IO)
).
%-------------------------------%
:- end_module relationship.
%-------------------------------%
答案 1 :(得分:1)
您无需检查变量的实例化状态。我从未在近10年的时间里每天使用水星。对于每个谓词和谓词模式,Mercury在程序的每个点知道静态每个变量的实例化。所以使用你的例子:
% I'm making some assumptions here.
:- pred mother(string::out, string::in) is det.
main(!IO) :-
mother(X,"john"),
io.format("%s\n", [s(X)], !IO).
母亲的声明说它的第一个参数是输出参数,这意味着在调用母亲之后它的值将被接地。所以它可以打印。如果您确实使用了var谓词(并且标准库中有一个),它将始终失败。
为此,Mercury对程序员提出了其他要求。例如。
(
X = a,
Y = b
;
X = c
)
io.write(Y, !IO)
以上代码是非法的。因为Y是在第一种情况下产生的,而不是在第二种情况下产生的,所以它的基础性没有很好地定义。编译器还知道析取是一个开关(当X已经被接地时)因为只有一个析取可能是真的。所以它只产生一个答案。
当你有一个多模式谓词时,这种静态基础会变得棘手。 Mercury可能需要重新排序你的连词以使程序模式正确:例如在生成之后使用变量。然而,变量的使用和实例化状态将始终是静态已知的。
我知道这与Prolog完全不同,可能需要相当多的学习。
希望这有所帮助,一切顺利。
答案 2 :(得分:0)
X
这段代码在Mercury中没有多大意义。如果mother
是来自X
的标准输出变量,则mother
未绑定将永远不会成功。
如果det
在此模式下具有确定性(X
),则始终会为mother(X, "john")
提供单个值。在那种情况下,没有必要检查任何东西; mother
给了你约翰的母亲,故事的结尾,因此不需要&#34;不可用的情况&#34;。
由于您在mother
为您提供了某些内容并且当它没有时,您尝试编写案例来处理这两种情况,我假设semidet
不具有确定性。如果是X
则有两种可能性;它会在X
绑定某个内容时成功,或失败。如果main
未绑定,则无法成功。
如果mother
失败,您的代码无法说明main
(始终成功所需)的成功方式。请记住,逗号是逻辑连接(和)。如果mother(X, "john")
成功并且if .. then ... else ...) succeeds
&#34;您的代码说&#34; mother
成功。但如果if ... then ... else ...
失败,那么?由于这个原因,编译器会拒绝您的代码,即使代码的其余部分有效。
但mother(X, "john")
构造的设计完全允许您检查可能成功或失败的目标,并指定目标成功的时间和失败时的情况,以便整个if /然后/ else 总是成功。所以你需要做的就是将Openssl::with_cert_and_key
放在if / then / else的条件中。
在Mercury中,您不能打开绑定或未绑定的变量。相反,只需检查可能已绑定变量的目标是成功还是失败。