Python 和 Prolog 哪个是强类型语言?
我是 Python 和 prolog 的新手。据我了解,Python是一种强类型语言。 Prolog 也是强类型语言吗?
I am new to Python and prolog. From my understanding, Python is a strongly typed language. Is Prolog a strongly typed language also?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
与 Python 一样,如果您尝试添加非整数的内容,Prolog 会给您一个类型错误。但这只是 Prolog 能为您做的事情的极限。说 Prolog 是或不是“强类型”并不是很有用——我已经写了很多关于“强类型”问题的答案,并重写了其他人对“强类型”问题的错误答案,我从来不想再次听到这些话。然而互联网上的某个地方是错误的。
以下是一些有用的信息:
Prolog 和 Python 都是动态类型的,也就是说,直到运行时才检查程序是否有“类型错误”。在这种情况下,典型的“类型错误”是应用于“错误”类型值的函数/方法 (Python) 或关系 (Prolog)。 Python 会检测到您将某些内容应用于错误数量参数的情况。
在Python中,有相当多的术语(表达式)是错误类型的,即在运行时会因为类型错误而被拒绝。
在 Prolog 中,几乎每个术语的定义都是类型正确的。例如,用户定义的仿函数可以应用于任何任何长度的项列表,Prolog 会很乐意将其解释为格式良好的关系。如果你得到一个关系的参数数量“错误”,Prolog 不会将其视为类型错误;而是将其视为类型错误。它只是假设您有两个具有相同名称的不同参数的不同关系。 (这种行为是否有用还有待商榷,但这就是 Prolog 的行为方式。)Prolog 对于 IS 等内置关系有点严格,如
真正且有用的知识是在 Prolog 中,动态类型系统拒绝很少的情况》 中所提到的那样。术语——比 Python 的动态类型系统少得多。如果基于此您选择将 Prolog 称为“较弱”,将 Python 称为“较强”,您可以这样做,因为术语“强”和“弱”没有任何普遍认可的技术含义。但您最好想想并说,Prolog 的动态类型系统接受几乎所有类型良好的关系和术语——与 Python 不同。这样您就可以传达实际发生的情况。
Like Python, Prolog will give you a type error if you try to add things that aren't integers. But that's just about the limit of what Prolog will do for you. It's not terribly useful to say that Prolog is or is not "strongly typed"—I have already written so many answers to questions about "strongly types", and rewritten other people's wrong answers to questions about "strongly typed", that I never want to hear the words again. And yet somewhere, someone on the Internet is wrong.
Here's what's useful to know:
Both Prolog and Python are dynamically typed, which is to say that programs are not checked for "type errors" until run time. A typical "type error" in this case is a function/method (Python) or a relation (Prolog) applied to the "wrong" kinds of values. And Python will detect cases where you apply something to the wrong number of arguments.
In Python, there are quite a few terms (expressions) that are ill-typed, i.e., that will be rejected at run time because of type errors.
In Prolog, almost every term is type-correct by definition. For example, a user-defined functor may be applied to any list of terms of any length, and Prolog will cheerfully try to interpret it as a well-formed relation. If you get the "wrong" number of arguments to a relation, Prolog doesn't treat this as a type error; it just assumes you have two different relations with different arities of the same name. (Whether this behavior is useful is subject to debate, but that's the way Prolog behaves.) Prolog is a bit stricter with builtin relations like IS, as in
What's true and useful to know is that in Prolog, the dynamic type system rejects very few terms—many fewer than Python's dynamic type system. If on this account you choose to call Prolog "weaker" and Python "stronger", you can do that, because the terms "strong" and "weak" don't have any universally agreed-upon technical meaning. But you'd be better off thinking, and saying, that Prolog's dynamic type system accepts almost all relations and terms as well typed—unlike Python's. That way you will communicate what is actually going on.
Python 是强类型的。
ie:
Raises
TypeError
我相信 Prolog 不是强类型的。
Python is strongly typed.
ie:
Raises
TypeError
I believe Prolog is not strongly typed though.
Prolog 不是一种强类型语言。
参考:http://scom.hud.ac.uk/scomtlm/book/节点187.html
Prolog is not a strongly typed language.
ref: http://scom.hud.ac.uk/scomtlm/book/node187.html
否
No