I am new to Python and prolog. From my understanding, Python is a strongly typed language. Is Prolog a strongly typed language also?
-
1I disagree with Python being strongly typed but there are so many ways to interpret it as shown here: http://wiki.python.org/moin/Why%20is%20Python%20a%20dynamic%20language%20and%20also%20a%20strongly%20typed%20language – May 07 '10 at 00:40
-
1@thyrgle There is a grey area, certainly, however, as dynamic as typing may appear, once a type has been determined by python interpreter, that type is set, and there is no mixing types. – johnc May 07 '10 at 00:43
-
@johnc: Yeah true, but its nothing like C, C++ or Java when it comes to being strongly typed. – May 07 '10 at 22:35
-
For one part: [is-python-strongly-typed](http://stackoverflow.com/questions/11328920/is-python-strongly-typed) – nawfal Jul 24 '14 at 01:08
4 Answers
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
X is Y + Z
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.

- 198,648
- 61
- 360
- 533
-
Well in some part there is strong typing for sure: ?- `atom(1)` is `false`, as well as `number(a)` is false too. There are strong types, but their number is just a few. – Sebastian Mar 06 '13 at 07:54
Python is strongly typed.
ie:
"1" + 1
Raises TypeError
I believe Prolog is not strongly typed though.

- 6,145
- 4
- 31
- 40