Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is a strongly typed language: Python or Prolog?

Tags:

typing

I am new to Python and prolog. From my understanding, Python is a strongly typed language. Is Prolog a strongly typed language also?

like image 579
User1 Avatar asked May 07 '10 00:05

User1


People also ask

Is Python a strongly typed?

Python is both a strongly typed and a dynamically typed language. Strong typing means that variables do have a type and that the type matters when performing operations on a variable. Dynamic typing means that the type of the variable is determined only during runtime.

Which one is a strongly typed language?

Why is Java a strongly typed language? Java is considered strongly typed because it demands the declaration of every variable with a data type. Users cannot create a variable without the range of values it can hold.

Is Prolog typed language?

Prolog is traditionally not statically typed. Since the benefits of static typing are huge, it was decided to grow a portable type system inside two widely used open source Prolog systems: SWI-Prolog and Yap. This requires close cooperation and agreement between the two systems.

What does it mean for Python to be strongly typed?

Python is strongly, dynamically typed. Strong typing means that the type of a value doesn't change in unexpected ways. A string containing only digits doesn't magically become a number, as may happen in Perl. Every change of type requires an explicit conversion.


1 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.

like image 157
Norman Ramsey Avatar answered Nov 15 '22 09:11

Norman Ramsey