Prolog

Official website

Prolog is not managed by a single organization, so there isn’t one “official” website for the language as a whole. Instead, there are several well‐established implementations and communities that support Prolog. Here are a few notable ones:

• SWI-Prolog: A widely used and actively developed implementation. Their official website is http://www.swi-prolog.org.

• GNU Prolog: An open-source Prolog compiler with constraint solving over finite domains. More information can be found at https://www.gnu.org/software/prolog/.

• SICStus Prolog: A high-performance commercial Prolog system used primarily in academia and industry. Details are available at https://sicstus.sics.se/.

If you are looking for standards or historical context, you might also explore academic sites, but for practical usage, the above implementations are the go-to resources.

Static typing

Prolog is generally considered an untyped or dynamically typed language. Here’s why:

  1. No explicit type declarations: In Prolog, you don’t declare variables or predicates with specific types as you might in statically typed languages. You simply work with terms such as atoms, numbers, lists, etc.

  2. Terms with implicit “types”: While Prolog doesn’t enforce types at compile time, it does have different kinds of terms (e.g., atoms, numbers, variables, structures). The “type” of a term is determined at runtime through pattern matching and unification.

  3. Unification: Prolog’s core mechanism is unification, which is performed at runtime and doesn’t require type declarations. This means that the language resolves the types (or, more precisely, the forms) of terms dynamically during execution.

  4. Some implementations: Some modern Prolog implementations introduce features that allow for type-checking or type declarations as an extension or for debugging purposes. However, these features are not part of the standard language, which remains untyped or dynamically typed.

In summary, Prolog does not have a static type system in the way that languages like Java or Haskell do, but it does have a notion of built-in term types that are managed dynamically at runtime.

Example code

Below is an example of a Prolog algorithm that searches for a value in a binary search tree (BST). In this example, the BST is represented with the functor tree(Left, Root, Right) where an empty tree is represented by the atom nil.

% search_bst(Tree, Value)
% Succeeds if Value is found in the BST Tree.

% Base case: an empty tree does not contain any value.
search_bst(nil, _) :-
    fail.

% When the Value is equal to the root's value, we've found it.
search_bst(tree(_, Root, _), Value) :-
    Value =:= Root.

% When the Value is less than the Root, search in the left subtree.
search_bst(tree(Left, Root, _), Value) :-
    Value < Root,
    search_bst(Left, Value).

% When the Value is greater than the Root, search in the right subtree.
search_bst(tree(_, Root, Right), Value) :-
    Value > Root,
    search_bst(Right, Value).

Explanation

  1. Representation of the BST:
    • A non-empty node is represented as tree(Left, Root, Right).
    • An empty tree is represented as nil.
  2. Predicate search_bst/2:
    • The first clause handles an empty tree. If the tree is nil, the predicate fails (fail).
    • The second clause checks if the value at the current node (Root) is equal to Value using the arithmetic equality operator =:=.
    • The third clause checks whether the searched Value is less than Root. If so, it recursively searches the left subtree.
    • The fourth clause checks whether the searched Value is greater than Root. If so, it recursively searches the right subtree.

Example Usage

Suppose we have the following BST:

      8
     / \
    3   10
   / \    \
  1   6    14
     / \   /
    4   7 13 

We can represent this tree in Prolog as:

Tree = tree(
           tree(
             tree(nil, 1, nil),
             3,
             tree(
               tree(nil, 4, nil),
               6,
               tree(nil, 7, nil)
             )
           ),
           8,
           tree(
             nil,
             10,
             tree(
               tree(nil, 13, nil),
               14,
               nil
             )
           )
       ).

To search for the value 7 in this tree, you would run:

?- Tree = tree(tree(tree(nil,1,nil),3,tree(tree(nil,4,nil),6,tree(nil,7,nil))),8,tree(nil,10,tree(tree(nil,13,nil),14,nil))),
   search_bst(Tree, 7).

If the value 7 is found, the query succeeds; otherwise, it fails.