Prolog的附件有什么问题? [英] What's wrong with Prolog's append?

查看:92
本文介绍了Prolog的附件有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我大学的逻辑课程,对于以下查询,我们可以期望得到与Prolog定义不同的结果:

According to my university's course in logic we could expect a different outcome than defined by Prolog for the following query:

append([], a, X)

(统一为 X = a )。

但是我不明白他们的目标是什么?鉴于append应该将X统一为 [] a

However I don't get what they're aiming at? What should be expected as a valid response, given that append should unify X for (in this example) the concatenation of [] and a?

我认为他们可能期望返回 false [a] ;但是我想这应该是连接 a [] 而不是 []的结果 a (因为 [] [ a] )。

I assume they may be expecting a return of false or [a]; however I suppose that should be the result of concatenating a and [], not [] and a (since [] is the tail of [a]).

推荐答案


但是我什么都没得到他们的目标是?

However I don't get what they're aiming at?

不问他们,确切地知道他们的目标当然是不可能的。

Knowing exactly what they are aiming at is of course impossible without asking them.

不过,我认为他们的目的是表明序言(或多或少) 未键入 append / 3 记录为:

Nevertheless I think they aim to show that Prolog is (more or less) untyped. append/3 is documented as:


append(?List1,?List2,?List1AndList2)

append(?List1, ?List2, ?List1AndList2)

   List1AndList2 List1 List2

   List1AndList2 is the concatenation of List1 and List2.

因此很明显,人们希望这三个参数是列表,而 a 不是列表。 a 不是 [] a 的串联人们会认为这两个不是 可连接的。

So clearly one expects that the three arguments are lists and a is not a list. a is not the concatenation of [] and a since one would consider the two not "concatenatable".

现在这仍然成功了,因为 append / 3 通常实现为:

Now this still succeeds, because append/3 is usually implemented as:

append([],T,T).
append([H|T],T2,[H|R]) :-
    append(T,T2,R).

所以,如果您给它 append([],a,X)。 ,它将简单地与第一个子句统一并统一 X = a

So if you give it append([],a,X)., it will simply unify with the first clause and unify X = a.

append([14],a,X)也会发生同样的怪异行为。在这里 X = [14 | a] 也不是列表。这是因为Prolog解释器不知道它正在使用列表。对于Prolog, [A | B] 与其他函子相同。

The same "weird" behavior happens with append([14],a,X). Here X = [14|a] which is not a list as well. This is because the Prolog interpreter does not "know" it is working with lists. For Prolog [A|B] is the same like any other functor.

A 更多类型安全 的处理方式可能是:

A more "type safe" way to handle this could be:

append([],[],[]).
append([H|T],T2,[H|R]) :-
    append(T,T2,R).
append([],[H|T],[H|R]) :-
    append([],T,R).

或更优雅:

list([]).
list([_|T]) :-
    list(T).

append([],T,T) :-
    list(T).
append([H|T],T2,[H|R]) :-
    append(T,T2,R).

因为这里我们检查第二个参数是否为列表。但是不利的是,现在我们将在 O(m + n)中以 m 的长度 append / 3 第一个列表,第二个列表的长度 n ,而在原始代码中,仅需 O(m)时间。此外请注意,Prolog在解析时将发出警告/错误。在查询这些内容时,只会在 [] 后面加上 a

since here we check whether the second argument is a list. The downside however is that now we will append/3 in O(m+n) with m the length of the first list and n the length of the second list whereas in the original code it would take only O(m) time. Furthermore note that Prolog will not raise a warning/error at parse time. It will only fail to append [] with a at the moment you query these.

不检查类型会导致以下事实:如果在将程序提供给解释器时程序编译/不引发错误,则您的保证就更少。这可能是一件好事,但问题可能是,您以某些他们不期望的方式调用某些谓词,这最终可能会在以后引发错误。这就是为什么有时使用静态类型的语言的原因:它们保证(至少在某种程度上)如果您提出问题,则不会发生此类错误。当然,这并不意味着程序无法在其他事情上出错(或根本没有意义)。例如, haskell 是静态类型的,并且具有类似:

Not checking types results in the fact that you have less guarantees if the program compiles/does not raises errors when you feed it to an interpreter. This can be a good thing, but a problem might be that you call some predicates in a way they don't expect which may raise errors eventually later. That is why statically typed languages are sometimes used: they "guarantee" (at least to some extent) that if you call the problem, no such errors will occur. Of course that does not mean that the program cannot error on other things (or simply make no sense). haskell for instance is statically typed and has an append like:

(++) [] t2 = t2
(++) (h:t) t2 = h:((++) t t2)

定义是或多或少相同,但是Haskell将推导(++)的类型为(++):: [a]-> [a]-> [a] 。因为它知道每个函数的输入和输出的类型,所以可以对它执行演算,因此在 compile 时,如果给出(+ +)与列表不同。

The definition is "more or less" the same, but Haskell will derive that the type of (++) is (++) :: [a] -> [a] -> [a]. Because it know the type of the input and output of every function, it can perform calculus on it, and therefore at compile time, it will raise errors if you would give (++) something different than a list.

这是否是一件好事,当然是一个不同的问题:动态类型化的编程语言经过精心设计,因为它可以提供更大的灵活性。

Whether that is a good thing is of course a different question: dynamically typed programming languages are designed that way deliberately since it allows more flexibility.

这篇关于Prolog的附件有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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