PROLOG
P R I N C I P L E S O F P R O G R A M M I N G L A N G U A G E S
PRESENTED BY:
H A S S A A N A H M A D ( 0 7 )
A Z E E M A S H R A F ( 1 4 )
M U H A M M A D U S A M A I Q B A L ( 1 5 )
S Y E D B A Q A R A B B A S ( 1 9 )
WHAT IS PROLOG?
• Prolog stands for programming in logic (PROgrammation en LOGique).
• Prolog is the most widely used language to have been inspired by logic
programming research.
• Prolog is the only successful example of the family of logic
programming languages.
CONT..
• Prolog is a declarative programming language.
• In the case of prolog, it will tell you whether a prolog sentence is true or not.
• if it contains variables, what the values of the variables need to be.
• Prolog is declarative. A Prolog programmer concentrates on what the program
needs to do, not on how to do it.
A LITTLE HISTORY
• Prolog was invented by Alain Colmerauer, a professor of computer science at
the university of Aix-Marseille in France, in 1972.
• The first application of Prolog was in natural language processing.
• Its theoretical underpinning are due to Donald Loveland of Duke university
through Robert Kowalski (formerly) of the university of Edinburgh
ANATOMY OF PROLOG PROGRAM
• Prolog programs are made up of facts and rules.
• A fact asserts some property of an object, or relation between two or more
objects.
e.g. parent(jane,alan).
Can be read as “Jane is the parent of Alan.”
• Rules allow us to infer that a property or relationship holds based on
preconditions.
e.g. parent(X,Y) :- mother(X,Y).
= “Person X is the parent of person Y if X is Y‟s mother.”
DATA TYPES
• Prolog single data type is the term. Terms are either atoms, numbers or
variables.
• An Atom is a general-purpose name with no inherent meaning.
Examples of atoms include
– X
– Red
– Taco
• Numbers can be floats or integers.
• Variables are denoted by a string consisting of letters, numbers and
underscore characters, and beginning with an upper-case letter or
underscore.
WHY LEARN PROLOG?
• Learning prolog will certainly not always be as fruitful as learning C++,
Java or Python.
• Prolog is most useful in the areas related to Artificial Intelligence
research, such as problem solving, (path) planning or natural language
interpretation.
• As prolog uses certain programming techniques that are considered
difficult or advanced
• prolog programmer will certainly have an in-depth understanding of
such concepts as recursion, searching problem trees etc.
CONT..
• Very complex programs have been written in prolog.
• Most of all prolog is a complex, powerful, and elegant language that
can inspire great satisfaction in its users.
• learning prolog is not easy because Prolog doesn't work as naturally as
procedural languages and therefore requires a specific way of thinking.
INTRODUCTION TO LOGIC
• Since Prolog is based heavily on formal logic, it's useful to have some
experience with it.
• There are two types of logical languages:
– propositional logic
– first-order logic.
PROPOSITIONAL LOGIC
• Propositional logic has two basic elements:
– terms
– Connectives
• Terms are represented by letters (usually upper-case), and represent
the values true and false.
• Connectives, like the word suggests, connect two terms.
• Connectives are usually represented by symbols rather than words.
FIRST ORDER LOGIC
• First Order Logic (also known as predicate logic) expands on
propositional logic, by using predicates, variables and objects.
• In first order logic the atomic sentences are predicates.
• Predicates have a name (starting with a capital) which is followed
by several variables (starting with a lowercase letter).
CONT..
• The following are examples of predicates:
– Predicate(variable1, variable2)
– BrotherOf(sibling, brother)
– MotherOf(child, mother)
– HasWheels(thing)
• these variables can be instantiated with objects.
CONT..
• Objects are elements represented by words that start with a capital
letter.
• Such a collection of sentences that are true is called a knowledge base.
• Such a knowledge base could, for instance contain the following
sentences:
– HasWheels(Car)
– MotherOf(Charles, Elizabeth)
• The sentences tell us that the HasWheels predicate is true
PREDICATE DEFINITIONS
• Both facts and rules are predicate definitions.
• ‘Predicate’ is the name given to the word occurring before the bracket in
a fact
or rule:
– parent (jane,alan).
• By defining a predicate you are specifying which information needs to
be
known for the property denoted by the predicate to be true.
CLAUSES
• Predicate definitions consist of clauses.
• An individual definition (whether it be a fact or rule).
e.g. mother(jane, alan). = Fact
parent(P1,P2):- mother(P1,P2). = Rule
• A clause consists of a head And sometimes a body.
• Facts don't have a body because they are always true.
• Clauses with empty bodies are called facts.
• Clauses with bodies are called rules.
ARGUMENTS
• A predicate head consists of a predicate name and sometimes some
arguments
contained within brackets and separated by commas.
mother(jane, alan).
Predicate name Arguments
• A body can be made up of any number of subgoals (calls to other
predicates) and terms.
CONT..
• Arguments also consist of terms, which can be:
– Constants e.g. jane,
– Variables e.g. Person1, or
– Compound terms
TERMS
CONSTANTS
Constants can either be:
• Numbers:
– integers are the usual form (e.g. 1, 0, -1, etc), but
– floating-point numbers can also be used (e.g. 3.9)
• Symbolic constants:
– always start with a lower case alphabetic character and contain any
mixture of
letters, digits, and underscores (but no spaces, punctuation, or an initial
capital).
– e.g. abc, big_long_constant, x4_3t.
• String constants:
VARIABLES
• Variables always start with an upper case alphabetic character or an
underscore.
• Other than the first character they can be made up of any mixture of letters,
digits, and underscores.
e.g. X, ABC, _89two5, _very_long_variable
• There are no “types” for variables (or constants) – a variable can take any
value.
• All Prolog variables have a “local” scope:
They only keep the same value within a clause; the same variable used
outside
of a clause refers as “global” scope.
NAMING TIPS
• Use real English when naming predicates, constants, and variables.
– e.g. “John wants to help Somebody.”
– Could be: wants(john, to_help , Somebody).
– Not: x87g(j,_789).
• Use a Verb-Subject-Object structure:
– wants(john, to_help).
• BUT do not assume Prolog Understands the meaning of your chosen
names
RUNNING
PROLOG
O N W I N D O W S .
After SWI-Prolog has been installed on a Windows system, the following
important new things are available to the user:
• A folder (in program files) called swipl containing the executable, libraries, etc.,
of the system.
• No files are installed outside this directory.
• Program swipl-win.exe, providing a window for interaction with Prolog.
• The program swipl.exe is a version of SWI-Prolog that runs in a console
window.
• The file extension .pl is associated with the program swipl-win.exe.
EXECUTING A QUERY
After loading a program, one can ask Prolog queries about the program.
?- likes (sam, x) .
X = biryani ;
X = tandoori ;
……
X = chips ;
?-
PROLOG EXECUTION
• Most Prolog clauses have both a declarative reading and a procedural
reading.
• Whenever possible, the declarative reading is to be preferred.
– mother (X, Y) :- parent (X, Y), female (X) .
• Declarative reading: x is the mother of y
– if x is parent of y and x is female
CONT..
Procedural reading :
• To show that x is the mother of y, first show that x is a parent of y, then
show that x is female.
Clauses:
oparent (john, bill) .
oparent (jane, bill) .
ofemale(jane) .
Query:
o| ?- mother (M, bill) .
CONT..
• The clause of mother will be checked, and the unification X=M, Y=bill will occur.
• Then parent (M, bill) will be attempted, resulting in the unification M=john.
• Next, female (john) will be attempted, but will fail.
• Prolog will backtrack to parent (M, bill) and look for another solution for this; it will
succeed and unify M=jane.
• Finally, it will attempt female (jane), and succeed; so the inquiry will succeed,
having performed the unification M=jane.
ADVANTAGES
• Ease of representing knowledge.
• Natural support of pattern-matching.
• Natural support of meta-programming.
• Meaning of programs is independent of how they are executed.
• Simple connection between programs and computed answers and
specifications.
• No need to distinguish programs from databases.
LIMITATIONS
• Although Prolog is widely used in research and education, Prolog and other logic
programming languages have not had a significant impact on the computer
industry in general.
• Most applications are small by industrial standards, with few exceeding 100,000
lines of code.
• Programming in the large is considered to be complicated because not all Prolog
compilers support modules
• because there are compatibility problems between the module systems of the
major Prolog compilers.
• Prolog is not purely declarative because of constructs like the cut operator,
• a procedural reading of a Prolog program is needed to understand it.
EXTENSIONS
• Various implementations have been developed from Prolog to extend logic
programming capabilities
• these include:
– types,
– modes,
– constraint logic programming (CLP),
– object-oriented logic programming (OOLP),
– linear logic (LLP),
– functional and higher-order logic programming capabilities,
– interoperability with knowledge bases
TYPES
• Prolog is an untyped language.
• Attempts to introduce types date back to the 1980s and as of 2008
there are still attempts to extend Prolog with types.
MODES
• The syntax of Prolog does not specify which arguments of a predicate
are inputs and which are outputs.
• Modes provide valuable information when reasoning about Prolog
programs and can also be used to accelerate execution.
EXAMPLE # 1:
EXAMPLE # 2:
GRAPHICS
• Prolog systems that provide a graphics library are:
– SWI-Prolog
– Visual Prolog WIN-PROLOG
– B-Prolog
• SWI also support server-side web programming with support for web
protocols, HTML and XML.
THANK
YOU

Prolog final

  • 1.
    PROLOG P R IN C I P L E S O F P R O G R A M M I N G L A N G U A G E S
  • 2.
    PRESENTED BY: H AS S A A N A H M A D ( 0 7 ) A Z E E M A S H R A F ( 1 4 ) M U H A M M A D U S A M A I Q B A L ( 1 5 ) S Y E D B A Q A R A B B A S ( 1 9 )
  • 3.
    WHAT IS PROLOG? •Prolog stands for programming in logic (PROgrammation en LOGique). • Prolog is the most widely used language to have been inspired by logic programming research. • Prolog is the only successful example of the family of logic programming languages.
  • 4.
    CONT.. • Prolog isa declarative programming language. • In the case of prolog, it will tell you whether a prolog sentence is true or not. • if it contains variables, what the values of the variables need to be. • Prolog is declarative. A Prolog programmer concentrates on what the program needs to do, not on how to do it.
  • 5.
    A LITTLE HISTORY •Prolog was invented by Alain Colmerauer, a professor of computer science at the university of Aix-Marseille in France, in 1972. • The first application of Prolog was in natural language processing. • Its theoretical underpinning are due to Donald Loveland of Duke university through Robert Kowalski (formerly) of the university of Edinburgh
  • 6.
    ANATOMY OF PROLOGPROGRAM • Prolog programs are made up of facts and rules. • A fact asserts some property of an object, or relation between two or more objects. e.g. parent(jane,alan). Can be read as “Jane is the parent of Alan.” • Rules allow us to infer that a property or relationship holds based on preconditions. e.g. parent(X,Y) :- mother(X,Y). = “Person X is the parent of person Y if X is Y‟s mother.”
  • 7.
    DATA TYPES • Prologsingle data type is the term. Terms are either atoms, numbers or variables. • An Atom is a general-purpose name with no inherent meaning. Examples of atoms include – X – Red – Taco • Numbers can be floats or integers. • Variables are denoted by a string consisting of letters, numbers and underscore characters, and beginning with an upper-case letter or underscore.
  • 8.
    WHY LEARN PROLOG? •Learning prolog will certainly not always be as fruitful as learning C++, Java or Python. • Prolog is most useful in the areas related to Artificial Intelligence research, such as problem solving, (path) planning or natural language interpretation. • As prolog uses certain programming techniques that are considered difficult or advanced • prolog programmer will certainly have an in-depth understanding of such concepts as recursion, searching problem trees etc.
  • 9.
    CONT.. • Very complexprograms have been written in prolog. • Most of all prolog is a complex, powerful, and elegant language that can inspire great satisfaction in its users. • learning prolog is not easy because Prolog doesn't work as naturally as procedural languages and therefore requires a specific way of thinking.
  • 10.
    INTRODUCTION TO LOGIC •Since Prolog is based heavily on formal logic, it's useful to have some experience with it. • There are two types of logical languages: – propositional logic – first-order logic.
  • 11.
    PROPOSITIONAL LOGIC • Propositionallogic has two basic elements: – terms – Connectives • Terms are represented by letters (usually upper-case), and represent the values true and false. • Connectives, like the word suggests, connect two terms. • Connectives are usually represented by symbols rather than words.
  • 13.
    FIRST ORDER LOGIC •First Order Logic (also known as predicate logic) expands on propositional logic, by using predicates, variables and objects. • In first order logic the atomic sentences are predicates. • Predicates have a name (starting with a capital) which is followed by several variables (starting with a lowercase letter).
  • 14.
    CONT.. • The followingare examples of predicates: – Predicate(variable1, variable2) – BrotherOf(sibling, brother) – MotherOf(child, mother) – HasWheels(thing) • these variables can be instantiated with objects.
  • 15.
    CONT.. • Objects areelements represented by words that start with a capital letter. • Such a collection of sentences that are true is called a knowledge base. • Such a knowledge base could, for instance contain the following sentences: – HasWheels(Car) – MotherOf(Charles, Elizabeth) • The sentences tell us that the HasWheels predicate is true
  • 16.
    PREDICATE DEFINITIONS • Bothfacts and rules are predicate definitions. • ‘Predicate’ is the name given to the word occurring before the bracket in a fact or rule: – parent (jane,alan). • By defining a predicate you are specifying which information needs to be known for the property denoted by the predicate to be true.
  • 17.
    CLAUSES • Predicate definitionsconsist of clauses. • An individual definition (whether it be a fact or rule). e.g. mother(jane, alan). = Fact parent(P1,P2):- mother(P1,P2). = Rule • A clause consists of a head And sometimes a body. • Facts don't have a body because they are always true. • Clauses with empty bodies are called facts. • Clauses with bodies are called rules.
  • 18.
    ARGUMENTS • A predicatehead consists of a predicate name and sometimes some arguments contained within brackets and separated by commas. mother(jane, alan). Predicate name Arguments • A body can be made up of any number of subgoals (calls to other predicates) and terms.
  • 19.
    CONT.. • Arguments alsoconsist of terms, which can be: – Constants e.g. jane, – Variables e.g. Person1, or – Compound terms
  • 20.
  • 21.
    CONSTANTS Constants can eitherbe: • Numbers: – integers are the usual form (e.g. 1, 0, -1, etc), but – floating-point numbers can also be used (e.g. 3.9) • Symbolic constants: – always start with a lower case alphabetic character and contain any mixture of letters, digits, and underscores (but no spaces, punctuation, or an initial capital). – e.g. abc, big_long_constant, x4_3t. • String constants:
  • 22.
    VARIABLES • Variables alwaysstart with an upper case alphabetic character or an underscore. • Other than the first character they can be made up of any mixture of letters, digits, and underscores. e.g. X, ABC, _89two5, _very_long_variable • There are no “types” for variables (or constants) – a variable can take any value. • All Prolog variables have a “local” scope: They only keep the same value within a clause; the same variable used outside of a clause refers as “global” scope.
  • 23.
    NAMING TIPS • Usereal English when naming predicates, constants, and variables. – e.g. “John wants to help Somebody.” – Could be: wants(john, to_help , Somebody). – Not: x87g(j,_789). • Use a Verb-Subject-Object structure: – wants(john, to_help). • BUT do not assume Prolog Understands the meaning of your chosen names
  • 24.
    RUNNING PROLOG O N WI N D O W S .
  • 25.
    After SWI-Prolog hasbeen installed on a Windows system, the following important new things are available to the user: • A folder (in program files) called swipl containing the executable, libraries, etc., of the system. • No files are installed outside this directory. • Program swipl-win.exe, providing a window for interaction with Prolog. • The program swipl.exe is a version of SWI-Prolog that runs in a console window. • The file extension .pl is associated with the program swipl-win.exe.
  • 26.
    EXECUTING A QUERY Afterloading a program, one can ask Prolog queries about the program. ?- likes (sam, x) . X = biryani ; X = tandoori ; …… X = chips ; ?-
  • 27.
    PROLOG EXECUTION • MostProlog clauses have both a declarative reading and a procedural reading. • Whenever possible, the declarative reading is to be preferred. – mother (X, Y) :- parent (X, Y), female (X) . • Declarative reading: x is the mother of y – if x is parent of y and x is female
  • 28.
    CONT.. Procedural reading : •To show that x is the mother of y, first show that x is a parent of y, then show that x is female. Clauses: oparent (john, bill) . oparent (jane, bill) . ofemale(jane) . Query: o| ?- mother (M, bill) .
  • 29.
    CONT.. • The clauseof mother will be checked, and the unification X=M, Y=bill will occur. • Then parent (M, bill) will be attempted, resulting in the unification M=john. • Next, female (john) will be attempted, but will fail. • Prolog will backtrack to parent (M, bill) and look for another solution for this; it will succeed and unify M=jane. • Finally, it will attempt female (jane), and succeed; so the inquiry will succeed, having performed the unification M=jane.
  • 30.
    ADVANTAGES • Ease ofrepresenting knowledge. • Natural support of pattern-matching. • Natural support of meta-programming. • Meaning of programs is independent of how they are executed. • Simple connection between programs and computed answers and specifications. • No need to distinguish programs from databases.
  • 31.
    LIMITATIONS • Although Prologis widely used in research and education, Prolog and other logic programming languages have not had a significant impact on the computer industry in general. • Most applications are small by industrial standards, with few exceeding 100,000 lines of code. • Programming in the large is considered to be complicated because not all Prolog compilers support modules • because there are compatibility problems between the module systems of the major Prolog compilers. • Prolog is not purely declarative because of constructs like the cut operator, • a procedural reading of a Prolog program is needed to understand it.
  • 32.
    EXTENSIONS • Various implementationshave been developed from Prolog to extend logic programming capabilities • these include: – types, – modes, – constraint logic programming (CLP), – object-oriented logic programming (OOLP), – linear logic (LLP), – functional and higher-order logic programming capabilities, – interoperability with knowledge bases
  • 33.
    TYPES • Prolog isan untyped language. • Attempts to introduce types date back to the 1980s and as of 2008 there are still attempts to extend Prolog with types.
  • 34.
    MODES • The syntaxof Prolog does not specify which arguments of a predicate are inputs and which are outputs. • Modes provide valuable information when reasoning about Prolog programs and can also be used to accelerate execution.
  • 35.
  • 36.
  • 37.
    GRAPHICS • Prolog systemsthat provide a graphics library are: – SWI-Prolog – Visual Prolog WIN-PROLOG – B-Prolog • SWI also support server-side web programming with support for web protocols, HTML and XML.
  • 38.

Editor's Notes

  • #24 You create meaning by specifying the body (i.e. preconditions) of a clause.
  • #26 Sociaal-Wetenschappelijke Informatica ("Social Science Informatics")