![Page 1: Declarative Programming Autumn 2014 Basic syntax and sample programs](https://reader030.vdocuments.us/reader030/viewer/2022032703/56649d0b5503460f949defc0/html5/thumbnails/1.jpg)
Declarative Programming
Autumn 2014
Basic syntax and sample
programs
![Page 2: Declarative Programming Autumn 2014 Basic syntax and sample programs](https://reader030.vdocuments.us/reader030/viewer/2022032703/56649d0b5503460f949defc0/html5/thumbnails/2.jpg)
Syntax of terms
Term
Constant VariableStructure
Atom Numberalpha17gross_payjohn_smithdyspepsia+=/=’12Q&A’
01571.6182.04e-27-13.6
likes(john, mary)book(dickens, Z, cricket)f(x)[1, 3, g(a), 7, 9]-(+(15, 17), t)15 + 17 - t
XGross_payDiagnosis_257_
Names an individual Stands for an individualunable to be named when program is written
Names an individualthat has parts
![Page 3: Declarative Programming Autumn 2014 Basic syntax and sample programs](https://reader030.vdocuments.us/reader030/viewer/2022032703/56649d0b5503460f949defc0/html5/thumbnails/3.jpg)
Symbols used
• Uppercase letters A,B,C,...,Z
• Lowercase letters a,b,c,...,z
• Digits 0,1,2,...,9
• Special symbols +,–,*,/,\,<,>,=,:,.,,,&,_,~,[,],(,),... (there are some that may not be allowed - e.g. %)
![Page 4: Declarative Programming Autumn 2014 Basic syntax and sample programs](https://reader030.vdocuments.us/reader030/viewer/2022032703/56649d0b5503460f949defc0/html5/thumbnails/4.jpg)
Atoms
• string of letters, digits and _, starting with a lowercase letter
• string of special symbols
• string of any symbols, delimited by ‘
![Page 5: Declarative Programming Autumn 2014 Basic syntax and sample programs](https://reader030.vdocuments.us/reader030/viewer/2022032703/56649d0b5503460f949defc0/html5/thumbnails/5.jpg)
Numbers
Depend from implementation...Some examples:
01571.6182.04e-27-13.6
![Page 6: Declarative Programming Autumn 2014 Basic syntax and sample programs](https://reader030.vdocuments.us/reader030/viewer/2022032703/56649d0b5503460f949defc0/html5/thumbnails/6.jpg)
Variables
• string of letters, digits and _, starting with an uppercase letter or _
• variable _ is called anonymous variable and has a special semantic meaning
![Page 7: Declarative Programming Autumn 2014 Basic syntax and sample programs](https://reader030.vdocuments.us/reader030/viewer/2022032703/56649d0b5503460f949defc0/html5/thumbnails/7.jpg)
Structures
parents(spot, fido, rover)
The parents of Spot are Fido and Rover.
Functor (an atom) of arity 3. components (any terms)
It is possible to depict the term as a tree:
parents
roverfidospot
![Page 8: Declarative Programming Autumn 2014 Basic syntax and sample programs](https://reader030.vdocuments.us/reader030/viewer/2022032703/56649d0b5503460f949defc0/html5/thumbnails/8.jpg)
Structures
=/=(15+X, (0*a)+(2<<5))
Some atoms have built-in operator declarations so they may be written in a syntactically convenient form. The meaning is not affected. This example looks like an arithmetic expression, but might not be. It is just a term.
<<
2
+
a
*
0
X
+
15
=/=
5
![Page 9: Declarative Programming Autumn 2014 Basic syntax and sample programs](https://reader030.vdocuments.us/reader030/viewer/2022032703/56649d0b5503460f949defc0/html5/thumbnails/9.jpg)
More about operators
Any atom may be designated an operator. The only purpose is for convenience; the only effect is how the term containing the atom is parsed. Operators are ‘syntactic sugar’.
Operators have three properties: position, precedence and associativity.
![Page 10: Declarative Programming Autumn 2014 Basic syntax and sample programs](https://reader030.vdocuments.us/reader030/viewer/2022032703/56649d0b5503460f949defc0/html5/thumbnails/10.jpg)
Examples of operator properties
Position Operator Syntax Normal SyntaxPrefix: -2 -(2)Infix: 5+17 +(17,5)Postfix: N! !(N)
Associativity: left, right, none. X+Y+Z is parsed as (X+Y)+Zbecause addition is left-associative.
Precedence: an integer.X+Y*Z is parsed as X+(Y*Z)because multiplication has higher precedence.
![Page 11: Declarative Programming Autumn 2014 Basic syntax and sample programs](https://reader030.vdocuments.us/reader030/viewer/2022032703/56649d0b5503460f949defc0/html5/thumbnails/11.jpg)
The last point about structures…
Constants are structures of arity 0.
badger
means the same as
badger()
![Page 12: Declarative Programming Autumn 2014 Basic syntax and sample programs](https://reader030.vdocuments.us/reader030/viewer/2022032703/56649d0b5503460f949defc0/html5/thumbnails/12.jpg)
Prolog programs
Programs consist of procedures.
Procedures consist of clauses.
Each clause is a fact or a rule.
Programs are executed by posing queries.
An example…
![Page 13: Declarative Programming Autumn 2014 Basic syntax and sample programs](https://reader030.vdocuments.us/reader030/viewer/2022032703/56649d0b5503460f949defc0/html5/thumbnails/13.jpg)
Example
elephant(george).elephant(mary).elephant(X) :- grey(X), mammal(X), hasTrunk(X).
Procedure for elephant
Predicate
Clauses
Rule
Facts
![Page 14: Declarative Programming Autumn 2014 Basic syntax and sample programs](https://reader030.vdocuments.us/reader030/viewer/2022032703/56649d0b5503460f949defc0/html5/thumbnails/14.jpg)
Example
?- elephant(george).
yes
?- elephant(jane).
no
Queries
Replies
![Page 15: Declarative Programming Autumn 2014 Basic syntax and sample programs](https://reader030.vdocuments.us/reader030/viewer/2022032703/56649d0b5503460f949defc0/html5/thumbnails/15.jpg)
Clauses: Facts and Rules
Head :- Body. This is a rule.
Head. This is a fact.
‘if’‘provided that’
Full stop at the end.
![Page 16: Declarative Programming Autumn 2014 Basic syntax and sample programs](https://reader030.vdocuments.us/reader030/viewer/2022032703/56649d0b5503460f949defc0/html5/thumbnails/16.jpg)
Body of a (rule) clause contains goals
likes(mary, X) :- human(X), honest(X).
Head Body
Goals
![Page 17: Declarative Programming Autumn 2014 Basic syntax and sample programs](https://reader030.vdocuments.us/reader030/viewer/2022032703/56649d0b5503460f949defc0/html5/thumbnails/17.jpg)
Clauses can be given a declarative reading or a procedural reading.
H :- G1, G2, …, Gn.
“That H is provable follows from goals G1, G2, …, Gn being provable.”
Declarative reading:
Procedural reading:
Form of clause:
Interpretation of clauses
“To execute procedure H, the procedures called by goals G1, G2, …, Gn are executed first.”
![Page 18: Declarative Programming Autumn 2014 Basic syntax and sample programs](https://reader030.vdocuments.us/reader030/viewer/2022032703/56649d0b5503460f949defc0/html5/thumbnails/18.jpg)
Sample program 1
likes(john,mary).likes(john,julie).likes(julie,john).likes(peter,jane).likes(jane,icecream).likes(jane,cat).
![Page 19: Declarative Programming Autumn 2014 Basic syntax and sample programs](https://reader030.vdocuments.us/reader030/viewer/2022032703/56649d0b5503460f949defc0/html5/thumbnails/19.jpg)
Sample program 1
likes(john,mary).likes(john,julie).likes(julie,john).likes(peter,jane).likes(jane,icecream).likes(jane,cat).
?-likes(john,mary).
![Page 20: Declarative Programming Autumn 2014 Basic syntax and sample programs](https://reader030.vdocuments.us/reader030/viewer/2022032703/56649d0b5503460f949defc0/html5/thumbnails/20.jpg)
Sample program 1
likes(john,mary).likes(john,julie).likes(julie,john).likes(peter,jane).likes(jane,icecream).likes(jane,cat).
?-likes(john,mary).yes
![Page 21: Declarative Programming Autumn 2014 Basic syntax and sample programs](https://reader030.vdocuments.us/reader030/viewer/2022032703/56649d0b5503460f949defc0/html5/thumbnails/21.jpg)
Sample program 1
likes(john,mary).likes(john,julie).likes(julie,john).likes(peter,jane).likes(jane,icecream).likes(jane,cat).
?-likes(jane,cat).yes
![Page 22: Declarative Programming Autumn 2014 Basic syntax and sample programs](https://reader030.vdocuments.us/reader030/viewer/2022032703/56649d0b5503460f949defc0/html5/thumbnails/22.jpg)
Sample program 1
likes(john,mary).likes(john,julie).likes(julie,john).likes(peter,jane).likes(jane,icecream).likes(jane,cat).
?-likes(julie,cat).no
![Page 23: Declarative Programming Autumn 2014 Basic syntax and sample programs](https://reader030.vdocuments.us/reader030/viewer/2022032703/56649d0b5503460f949defc0/html5/thumbnails/23.jpg)
Sample program 1
likes(john,mary).likes(john,julie).likes(julie,john).likes(peter,jane).likes(jane,icecream).likes(jane,cat).
?-likes(jane,X).X = icecream ?yes
![Page 24: Declarative Programming Autumn 2014 Basic syntax and sample programs](https://reader030.vdocuments.us/reader030/viewer/2022032703/56649d0b5503460f949defc0/html5/thumbnails/24.jpg)
Sample program 1
likes(john,mary).likes(john,julie).likes(julie,john).likes(peter,jane).likes(jane,icecream).likes(jane,cat).
?-likes(jane,X).X = icecream ? ;X = cat ? ;no
![Page 25: Declarative Programming Autumn 2014 Basic syntax and sample programs](https://reader030.vdocuments.us/reader030/viewer/2022032703/56649d0b5503460f949defc0/html5/thumbnails/25.jpg)
Sample program 1
likes(john,mary).likes(john,julie).likes(julie,john).likes(peter,jane).likes(jane,icecream).likes(jane,cat).
?-likes(X,Y),likes(Y,X).X = john,Y = julie ? yes
![Page 26: Declarative Programming Autumn 2014 Basic syntax and sample programs](https://reader030.vdocuments.us/reader030/viewer/2022032703/56649d0b5503460f949defc0/html5/thumbnails/26.jpg)
Sample program 1
likes(john,mary).likes(john,julie).likes(julie,john).likes(peter,jane).likes(jane,icecream).likes(jane,cat).
friends(X,Y) :- likes(X,Y),likes(Y,X).
?-friends(X,Y).X = john,Y = julie ? yes
![Page 27: Declarative Programming Autumn 2014 Basic syntax and sample programs](https://reader030.vdocuments.us/reader030/viewer/2022032703/56649d0b5503460f949defc0/html5/thumbnails/27.jpg)
Sample program 2
parent(john,peter).
parent(christine,peter).
parent(jane,peter).
parent(john,mary).
parent(christine,mary).
parent(jane,mary).
parent(mary,frances).
parent(mary,george).
parent(frances,edward).
parent(edward,isabel).
![Page 28: Declarative Programming Autumn 2014 Basic syntax and sample programs](https://reader030.vdocuments.us/reader030/viewer/2022032703/56649d0b5503460f949defc0/html5/thumbnails/28.jpg)
Sample program 2
parent(john,peter).
parent(christine,peter).
parent(jane,peter).
parent(john,mary).
parent(christine,mary).
parent(jane,mary).
parent(mary,frances).
parent(mary,george).
parent(frances,edward).
parent(edward,isabel).
?-parent(jane,peter).yes
![Page 29: Declarative Programming Autumn 2014 Basic syntax and sample programs](https://reader030.vdocuments.us/reader030/viewer/2022032703/56649d0b5503460f949defc0/html5/thumbnails/29.jpg)
Sample program 2
parent(john,peter).parent(christine,peter).parent(jane,peter).parent(john,mary).parent(christine,mary).parent(jane,mary).parent(mary,frances).parent(mary,george).parent(frances,edward).parent(edward,isabel).
siblings(X,Y) :- parent(X,Z),parent(Y,Z).
?-siblings(john,X).X = christine ?yes
![Page 30: Declarative Programming Autumn 2014 Basic syntax and sample programs](https://reader030.vdocuments.us/reader030/viewer/2022032703/56649d0b5503460f949defc0/html5/thumbnails/30.jpg)
Sample program 2
parent(john,peter).parent(christine,peter).parent(jane,peter).parent(john,mary).parent(christine,mary).parent(jane,mary).parent(mary,frances).parent(mary,george).parent(frances,edward).parent(edward,isabel).
grandparent(X,Y) :- parent(X,Z),parent(Z,Y).
?-grandparent(christine,X).X = frances ? ;X = george ? ;no
![Page 31: Declarative Programming Autumn 2014 Basic syntax and sample programs](https://reader030.vdocuments.us/reader030/viewer/2022032703/56649d0b5503460f949defc0/html5/thumbnails/31.jpg)
Sample program 2
parent(john,peter).parent(christine,peter).parent(jane,peter).parent(john,mary).parent(christine,mary).parent(jane,mary).parent(mary,frances).parent(mary,george).parent(frances,edward).parent(edward,isabel).
ancestor(X,Y) :- parent(X,Y).ancestor(X,Y) :- parent(X,Z),ancestor(Z,Y).
?-ancestor(christine,isabel).yes
![Page 32: Declarative Programming Autumn 2014 Basic syntax and sample programs](https://reader030.vdocuments.us/reader030/viewer/2022032703/56649d0b5503460f949defc0/html5/thumbnails/32.jpg)
Sample program 2
parent(john,peter).parent(christine,peter).parent(jane,peter).parent(john,mary).parent(christine,mary).parent(jane,mary).parent(mary,frances).parent(mary,george).parent(frances,edward).parent(edward,isabel).
ancestor(X,Y) :- parent(X,Y).ancestor(X,Y) :- ancestor(X,Z),parent(Z,Y).
?-ancestor(christine,isabel).yes
![Page 33: Declarative Programming Autumn 2014 Basic syntax and sample programs](https://reader030.vdocuments.us/reader030/viewer/2022032703/56649d0b5503460f949defc0/html5/thumbnails/33.jpg)
Sample program 2
parent(john,peter).parent(christine,peter).parent(jane,peter).parent(john,mary).parent(christine,mary).parent(jane,mary).parent(mary,frances).parent(mary,george).parent(frances,edward).parent(edward,isabel).
ancestor(X,Y) :- ancestor(X,Z),parent(Z,Y).ancestor(X,Y) :- parent(X,Y).
?-ancestor(christine,isabel).
![Page 34: Declarative Programming Autumn 2014 Basic syntax and sample programs](https://reader030.vdocuments.us/reader030/viewer/2022032703/56649d0b5503460f949defc0/html5/thumbnails/34.jpg)
How PROLOG answers queriesHow to answer a query ?-Q.?
• find the first fact P. or rule P:-R1,...,Rn. with lhs P unifiable with Q
• if P corresponds to fact, return yes together with the instantiation values of variables from Q
• if P corresponds to rule, answer the queries R1,..,Rn (in this order, keeping the instantiated variables from the previous ones)
• if all queries R1,..,Rn are successful, return yes
• otherwise find next fact or rule unifiable with Q
• if there are no more such facts or rules, return no
![Page 35: Declarative Programming Autumn 2014 Basic syntax and sample programs](https://reader030.vdocuments.us/reader030/viewer/2022032703/56649d0b5503460f949defc0/html5/thumbnails/35.jpg)
How PROLOG answers queries
Goals and sub-goals:
A logical sentence to be proved: succeed (satisfy) or fail.
A goal could be a query or the conditions of a rule: the relationship between queries and rules
Rule: “goal:-goal”, the relationship is “true”, but the truth values of condition and conclusion are unknown
Prolog breaks a complex goal into a sequence of sub-goals to evaluate the truth value of a (complex) goal
![Page 36: Declarative Programming Autumn 2014 Basic syntax and sample programs](https://reader030.vdocuments.us/reader030/viewer/2022032703/56649d0b5503460f949defc0/html5/thumbnails/36.jpg)
How PROLOG answers queries
Searching, Matching (unifying) and backtracking
S-M-B is the way in which goals are established
A goal is always broken into ground sub-goals consisting of individual predicates, which are then searched from left to right with due regard for the logical meaning. ?-a,b;c,d.
Searching: systematically (depth-first) searches the knowledge to find a match for a ground sub-goal.