Introduction to Prolog

About Prolog

Prolog is short for PROgramming in LOGic, it is a logcial and declarative language. It is considered to be a very high level language, and was made particularly popular afte the Japanese government invested in its usage. You can get a good, free interpreter here.

Uses of Prolog

  • Natural Languag Systems
  • AI
  • Expert Systems
  • Theorem Proving

    Creating a Program

    Programs are normally written in text editors then interpreted by a prolog implementation. An example of a statment is
    likes(mary,food).
    Note the full stop, they are important like the ; in other language.

    Key commands

  • You can see prologs actions as it works through a goal by tyiping trace.
  • You can see what information prolog has recorded by typing listing.

    Asking a question

    You can tell prolog that bob is pats parent with parent (bob, pat).
    You can can ask prolog who's pats parent with ?- parent (X, pat). and it should return X=bob.
    You can attatch two requirements together with a comma. For example parent (X, Y), parent (Y, pat). will find the grandparent of pat.

    There are three kinds of clauses in Prolog:
  • Facts declare things that are always true
  • Rules declare things are true dependant upon conditions
  • Questions return what is true, and what makes a clause true. Unary relations are used to declare simple facts, eg; male(pat).

    Goal finding

    If a goal fails, prolog backtracks and will try another path.

    Objects

    Atoms start with a lower-case letter pat is an atom in male(pat).
    Variables start with an upper case letter. Structures have multiple components eg; date(13,April,1976)
    Lists can be made of any objects, even lists eg; [1, 2, 3, [pat, tom, mary ] ]

    Backracking

    Automatic backtracking is useful as it saves you having to program it explicitly, but it can be inefficient. The cut command is used to prevent backtracking when it would be futile or if you dont want multiple answers.

    If a variable is used only once an underscore character can be used in its place.