Prolog.1 Prolog. Prolog.2 Textbook and Software  Title PROLOG programming for artificial intelligence  Author Ivan Bratko  Get the software – windows.

Slides:



Advertisements
Similar presentations
Chapter 4 Loops Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Advertisements

Artificial Intelligence: Natural Language and Prolog
By John E. Hopcroft, Rajeev Motwani and Jeffrey D. Ullman
Business Transaction Management Software for Application Coordination 1 Business Processes and Coordination.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
DIVIDING INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
Addition Facts
Year 6 mental test 5 second questions
Relational data objects 1 Lecture 6. Relational data objects 2 Answer to last lectures activity.
Overview of Lecture Partitioning Evaluating the Null Hypothesis ANOVA
Intelligent Architectures for Electronic Commerce A (Brief) Prolog Tutorial.
Formal Models of Computation Part II The Logic Model
CS4026 Formal Models of Computation Part II The Logic Model Lecture 6 – Arithmetic, fail and the cut.
Programming Language Concepts
Lecture 15 Linked Lists part 2
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 5.
0 PROGRAMMING IN HASKELL Chapter 5 - List Comprehensions.
Modern Programming Languages, 2nd ed.
ABC Technology Project
Hash Tables.
Yong Choi School of Business CSU, Bakersfield
Logic Programming Two possible work modes: 1.At the lab: Use SICstus Prolog. To load a prolog file (*.pl or *.pro extension) to the interpreter, use: ?-
1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight.
A Third Look At ML 1. Outline More pattern matching Function values and anonymous functions Higher-order functions and currying Predefined higher-order.
© 2012 National Heart Foundation of Australia. Slide 2.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
GG Consulting, LLC I-SUITE. Source: TEA SHARS Frequently asked questions 2.
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.
25 seconds left…...
Chapter Three Arithmetic Expressions and Assignment Statements
Week 1.
Chapter 9 Interactive Multimedia Authoring with Flash Introduction to Programming 1.
Types of selection structures
We will resume in: 25 Minutes.
©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
PSSA Preparation.
1 Decidability continued…. 2 Theorem: For a recursively enumerable language it is undecidable to determine whether is finite Proof: We will reduce the.
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
L6:CSC © Dr. Basheer M. Nasef Lecture #6 By Dr. Basheer M. Nasef.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 3 Loops.
Prolog.
Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
1 Logic Programming. 2 A little bit of Prolog Objects and relations between objects Facts and rules. Upper case are variables. parent(pam, bob).parent(tom,bob).
2. Syntax and Meaning. Contents Data Objects Matching Declarative meaning of Prolog Procedural meaning Example: monkey and banana Order of clauses and.
4. PROLOG Data Objects And PROLOG Arithmetic
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
(9.1) COEN Logic Programming  Logic programming and predicate calculus  Prolog statements  Facts and rules  Matching  Subgoals and backtracking.
1 COMP 205 Introduction to Prolog Dr. Chunbo Chu Week 13 Slides Courtesy to: Peter LO.
Formal Models of Computation Part II The Logic Model
For Wednesday Read “lectures” 7-10 of Learn Prolog Now Chapter 9, exs 4 and 6. –6 must be in Horn clause form Prolog Handout 2.
PROLOG SYNTAX AND MEANING Ivan Bratko University of Ljubljana Faculty of Computer and Info. Sc. Ljubljana, Slovenia.
Introduction to Prolog. Outline What is Prolog? Prolog basics Prolog Demo Syntax: –Atoms and Variables –Complex Terms –Facts & Queries –Rules Examples.
Knowledge Based Information System
Chapter 2 Syntax and meaning of prolog programs Part 1.
Logic Programming Logic programming Candidates should be familiar with the concept of logic programming for declaring logical relationships.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Section 16.5, 16.6 plus other references
PROLOG.
Prolog programming Introduction to Prolog (part2)
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter Two: Syntax and Meaning of Prolog Programs
Chapter 2 Syntax and meaning of prolog programs
Chapter 2: Prolog (Introduction and Basic Concepts)
Presentation transcript:

Prolog.1 Prolog

Prolog.2 Textbook and Software  Title PROLOG programming for artificial intelligence  Author Ivan Bratko  Get the software – windows Home users: download from the course site Install software to preferred directory (e.g. c:\pl) Run by c:\pl\bin\plcon.exe (or plwin.exe ) Welcome to SWI-Prolog version x.y | ?- Computer farm: Software is installed in rooms 014 & 018  Get the software – csl1 Just run pl

Prolog.3 Introduction  What is Prolog? PROgramming LOGic A programming language for symbolic non-numeric computation  What is programming in Prolog like? Defining relations and querying about relations A program consists of clauses: facts, rules, and questions  What is SWI-PROLOG? An interpreter ?- main prompt | secondary prompt

Prolog.4  Facts are means of stating relation between things  Facts can have arbitrary number of arguments (arity): summer. sad(john). plus(2,3,5).  Collections of facts are useful: mammal(rat). mammal(bear). fish(salmon). eats(bear,honey). eats(bear,salmon). eats(rat, salmon). eats(salmon, worm). Facts

Prolog.5  A means of retrieving information from a logic program. We ask whether a certain relation holds between things ?- mammal(bear). true. ?- fish(rat). false.  Queries may contain variables that stands for an unspecified thing ?- fish(X). X = salmon. ?- mammal(X). X = rat. if we now type a “;” we get the response: X = bear. Queries

Prolog.6  Logical AND: Which X eats honey and the same X eats salmon? ?- eats(X,salmon), eats(X,honey). X = bear.  Logical OR: Which X eats salmon or which one eats honey? ?- eats(X,salmon) ; eats(X,honey). X = bear ; X = rat ; X = bear Queries

Prolog.7 Rules  A clause with one or more conditions.  The conditions in the rule body are called goals.  Example: For all X and Y, X’s survival depends on Y’s existence if X eats Y survival_dependency(X,Y) :- eats(X,Y). The relation survival_dependency is defined as follows: if eats(a,b) then survival_dependency (a,b)

Prolog.8 Recursive Definitions  For all X and Y, X’s survival depends on Y’s existence if : X eats Y OR X eats some Z and Z’s survival depends on Y’s existence. survival_dependency(X,Y) :- eats(X,Y). survival_dependency(X,Y) :- eats(X,Z), survival_dependency(Z,Y). Afterwards we can ask: ?-survival_dependency (rat,X). X = salmon ; X = worm. Or: ?- survival_dependency(X,worm). X = salmon ; X = bear ; X = rat.

Prolog.9 Declerative and Procedural Meaning of Programs  The meaning of the definitions is understood Declaratively The inference algorithm – the “control flow” - should be regarded as an implementation detail Unfortunately, this is not always easy to achieve  The meaning of the definitions can be understood Procedurally But thinking procedurally makes it harder to come up with an elegant solution And it beats the purpose of the paradigm

Prolog.10 Writing Prolog Programs  You can use files to declare rules and facts Create a file named “c:\1\prog.pl” (for example …) Enter the prolog interpreter Type: consult(‘c:\\1\\prog.pl’). As a result all the facts and rules in the program are loaded.  Comments: /* This is a comment */ % This is also a comment  At the interpreter prompt you can only type “queries” (or “goals”, or “questions”) Question: good_grade(X).  To change the database use the goal assert (which always succeeds) or retract (which can fail) assert( (good _ grade(Pupil) :- study(Pupil)) ). retract( (good _ grade(Pupil) :- study(Pupil)) ).

Prolog.11 Terms  Both Atoms & Numbers are defined over the following characters: upper-case letters A,B,..,Z lower-case letters a,b,...,z digits 0,1,...,9 special characters such as + - * / = :. & _ ~  Atoms can be constructed in 3 ways: 1. Strings of letters, digits & the underscore, starting with a lower-case letter. anna x_25 nil 2. String of special characters ::==.:. 3. Strings of characters enclosed in single quotes: ‘Tom’ ‘x_>:’  Reals: e3  Integers:

Prolog.12  Variables: strings of letters, digits & “_”. Start with an UPPER-CASE letter or an “_”. X_25 _result  A single “_” is an anonymous variable getsEaten(X) :- eats(_,X).  Structures can have several components, each component is a term and thus can be a structure. The name of the structure is called functor. course(pl, fall, 90). ?- course(pl, fall, Grade). Grade = 90. Course NameSemsterGrade Query Terms Fact Declaration

Prolog.13 Matching  An operation on terms. Two terms match if: they are identical, or the variables in both terms can be instantiated to objects in such a way that after the substitution of variables by these objects the terms become identical. course(N,S,95) matches course(X,fall,G) course(N,S,95) doesn’t match course(Y,M,96) course(X) doesn’t match semester(X)  The operator ‘=‘ performs matching between two terms.  If matching succeeds it always results in the most general instantiation possible. course(N,M,85) = course(N1,fall,G). N = N1, M = fall, G = 85.

Prolog.14 1)If S and T are constants, then S and T match iff they are the same atom, or the same number. 2)If S is a variable and T is any type of term, then S and T match, and S is instantiated to T. Similarly, if T is a variable and S is any type of term, then S and T match, and T is instantiated to S. (So if they are both variables, they’re both instantiated to each other, and we say that they share values.) 3)If S and T are complex terms, then they match iff: 1)They have the same functor and arity. 2)All their corresponding arguments match 3)The variable instantiations are compatible. General rules for matching two terms S and T

Prolog.15 Geometric Example  Use structures to represent simple geometric shapes. point - two numbers representing X and Y coordinates. seg - a line defined by two points triangle - defined by three points. point(1,1) seg( point(1,1), point(2,3) ) triangle( point(4,2), point(6,4), point(7,1) )  In the same program we can also use three dimensional points: point(1,3,5) This will result in a different relation with the same name.  We want to match: triangle(point(1,1), A, point(2,3)) with triangle(X, point(4,Y), point(2,Z)).

Prolog.16 triangle = triangle point(1,1) = X A = point(4,Y) point(2,3) = point(2,Z) The resulting instantiation is: X = point(1,1), A = point(4,Y), Z = 3. triangle point X Y Z   triangle point A    Geometric Example triangle(point(1,1), A, point(2,3)) = triangle(X, point(4,Y), point(2,Z)).

Prolog.17  A program with two facts: vertical( seg( point(X,Y), point(X, Y1) ) ). horizontal( seg( point(X,Y), point(X1,Y) ) ).  Conversation: ?- vertical( seg( point(1,1), point(1,2) )). true. ?- vertical( seg( point(1,1), point(2,Y) )). false. ?- vertical( seg( point(2,3), P)). P = point(2,Y).  When prolog has to invent a variable name (like the Y above) it will be in the form _n where n is an arbitrary number. So the last line will actually be in Prolog: P = point(2,_G501) Matching as means of Computation

Prolog.18 Arithmetics  Predefined operators for basic arithmetic: +, -, *, /, div, mod  If not explicitly requested, the operators are just like any other relation  Example: ?- X = X = 1+2.  The predefined operator ‘is’ forces evaluation. ?- X is X = 3.  A is B (A and B here can be anything) means Evaluate B to a number and perform matching of the result with A  The comparison operators also force evaluation. 145 * 34 > 100. true.

Prolog.19 X > Y X is greater than Y. X < Y X is less than Y. X >= Y X is greater than or equal to Y. X =< Y X is less than or equal to Y. X =:= Y the values of X and Y are equal. X =\= Y the values of X and Y are not equal. Comparison Operators

Prolog.20 = and =:=  X = Y causes the matching of X and Y and possibly instantiation of variables.  X =:= Y causes an arithmetic evaluation of X and Y, and cannot cause any instantiation of variables. ? =:= true. ? = false. ?- 1 + A = B + 2. A = 2, B = 1. ?- 1 + A =:= B + 2. ERROR: =:=/2: Arguments are not sufficiently instantiated

Prolog.21 Example: The Greatest Common Devisor  given X and Y, the gcd D can be found by: (1) If X and Y are equal then D is equal to X. (2) If X < Y then D is equal to the gcd of X and (Y-X). (3) If Y < X then do the same as in (2) with X and Y interchanged. gcd(X,X,X). gcd(X,Y,D) :- X < Y, Y1 is Y - X, gcd(X,Y1,D). gcd(X,Y,D) :- Y < X, gcd(Y,X,D).

Prolog.22 Example: The Greatest Common Devisor gcd(X,X,X). gcd(X,Y,D) :- X < Y, Y1 is Y - X, gcd(X,Y1,D). gcd(X,Y,D) :- Y < X, gcd(Y,X,D). ?-gcd(12,20,D). gcd(12,20,D):- 12<20, Y1 is 8, gcd(12,8,D) Rule 2 Rule 1 12 != 20 Rule 1 12 != 8 gcd(12,8,D):- 12<8 Rule 2 gcd(12,8,D):- 8<12,gcd(8,12,D) Rule 3 gcd(8,12,D):-8<12, Y1 is 4,gcd(8,4,D) Rule 2 Rule 1 8 != 12 Rule 1 8 != 4 gcd(8,4,D):-8<4 Rule 2 gcd(8,4,D):-4<8, gcd(4,8,D) Rule 3 gcd(4,8,D):-4<8, Y1 is 4, gcd(4,4,D) Rule 2 Rule 1 4 != 8 Rule 1 gcd(4,4,4) D=4!

Prolog.23  A sequence of any number of items.  Structure of lists:.( Head, Tail ).(a,.(b,[ ])) :  Shorthand: [tom, jerry] is the same as.(tom,.(jerry, [])) [a | tail] is the same as.(a, tail) [a,b,c] = [a|[b,c]] = [a,b|[c]] = [a,b,c |[]]  Elements can be lists and structures: [a, [1, 2, 3], tom, 1995, date(1,may,1995) ] Lists a b []..

Prolog.24 Length  The length of a list is defined in the following way: if the list is empty then its length is 0. if the list is not empty then List = [Head | Tail]. In this case the length is equal to 1 plus the length of the tail Tail.  length is built in. If you want to try defining it, change the name. length([], 0). length([_|Tail], N) :- length(Tail, N1), N is 1 + N1. ?- length([a,b,[c,d],e], N). N = 4. ?- length(L,4). L = [_G1202, _G1205, _G1208, _G1211]. what happens if the order of these clauses is changed?

Prolog.25 Operations on Lists  Typing (predefined) is_list(Xs) ← Xs is a list. is_list([]). is_list([X|Xs]) :- is_list(Xs).  Membership (predefined) member(X, L) ← X is a member of the list L. member(X, [X|Xs]). member(X, [Y|Ys]) :- member(X, Ys).  Prefix prefix([], L). prefix([X|Xs], [X|Ys]) :- prefix(Xs, Ys).  Suffix suffix(Xs, Xs). suffix(Xs, [Y|Ys]) :- suffix(Xs, Ys).

Prolog.26  Deleting an item (different from built-in delete/3) del(X, [X|Xs], Xs). del(X, [Y|Ys], [Y|Zs]) :- del(X, Ys, Zs). If there are several occurrences of X in the list then del will be able to delete only one of them. To insert an item at any place in the list: ?- del(a, Xs, [1,2,3]). Xs = [a,1,2,3]; Xs = [1,a,2,3]; Xs = [1,2,a,3]; Xs = [1,2,3,a]; false.  We can define insert using del : insert(X, List, BiggerList) :- del(X, BiggerList, List). Deletion and Insertion

Prolog.27 Operations on Lists  concatenation (predefined) append(Xs, Ys, XsYs) ← XsYs is the result of concatenating the lists Xs and Ys. append([], Ys, Ys). append([X|Xs], Ys, [X|Zs]) :- append(Xs, Ys, Zs).  Redefining member using append: member(X, Xs) :- append(_, [X|_], Xs). [X|Xs] [X|Zs] XXsYs Zs

Prolog.28 ?- append( [a,b,c], [1,2,3], L). > L = [a,b,c,1,2,3] ?- append( L1, L2, [a,b,c] ). > L1 = [] L2 = [a,b,c]; > L1 = [a] L2 = [b,c]; > L1 = [a,b] L2 = [c]; > L1 = [a,b,c] L2 = []; > false. ?- append( Before, [4|After], [1,2,3,4,5,6,7]). > Before = [1,2,3], After = [5,6,7] ?- append(_, [Pred, 4, Succ |_], [1,2,3,4,5,6,7]). > Pred = 3, Succ = 5 Examples

Prolog.29  The sublist relation sublist(Xs, Ys) :- append(As, Bs, Ys), append(Xs, Cs, Bs). ?- sublist(Xs, [a,b,c]). Xs = []; Xs = [a];... Xs = [b,c];... AsCsXs Ys Bs Lists (contd.)

Prolog.30  Define prefix and suffix: prefix(Xs, Ys) :- append(Xs, _, Ys). suffix(Xs, Ys) :- append(_, Bs, Ys).  Sublist - suffix of a prefix: sublist(Xs, Ys) :- prefix(Ps, Ys), suffix(Xs, Ps).  Sublist - prefix of a suffix: sublist(Xs, Ys) :- prefix(Xs, Ss), suffix(Ss, Ys). Prefix and Suffix

Prolog.31  Permutation (predefined) permutation([], []). permutation([X|L], P) :- permutation(L, L1), insert(X, L1, P). ?- permutation([a, b, c], P). P = [a,b,c]; P = [a,c,b]; P = [b,a,c];... permutation2([], []). permutation2(L, [X|P]) :- del(X, L, L1), permutation2(L1, P). Permutations

Prolog.32 Controlling Backtracking  Automatic backtracking can cause inefficiency: o1. if X < 3 then Y = 0 2. if 3 <= X and X < 6 then Y = 2 3. if 6 <= X then Y = 4 4 Y 2 36 X

Prolog.33  The relation f(X,Y) in prolog would be: f(X,0) :- X<3. f(X,2) :- 3=<X, X<6. f(X,4) :- 6=<X.  This procedure assumes that before f(X,Y) is executed X is already instantiated to a number.  The goal: “ f(1,Y), 2<Y. ” fails, but before prolog replies ‘no’, it tries all 3 rules.  The three rules are mutually exclusive so that one of them at most will succeed. If the goal matches the first rule and then fails, there is no point in trying the others.  The CUT mechanism will help us prevent this. Controlling Backtracking

Prolog.34  A cut prevents backtracking from some point on.  Written as a ‘!’ sub-goal that always succeeds, but prevents backtracking through it.  Correcting the example: f(X,0) :- X<3, !. f(X,2) :- 3=<X, X<6, !. f(X,4) :- 6=<X.  Whenever the goal f(X,Y) is encountered, only the first rule that matches will be tried.  If we now ask again “ f(1,Y), 2<Y. ” we will get the same answer, ‘no’, but only the first rule of ‘f’ will be tried,  note: the declarative meaning of the procedure did not change. Controlling Backtracking: Cut

Prolog.35 Another problem:  If we ask: f(7,Y). > Y=4.  What happened: 7 fail 3= fail 6= success.  Another improvement: The logical rule if X<3 then Y=0, otherwise if X<6 then Y=2, otherwise Y=4. Is translated into: f(X,0) :- X<3, !. f(X,2) :- X<6, !. f(X,4). Controlling Backtracking: Cut

Prolog.36  The last change improved efficiency. BUT, removing the cuts now will change the result !!! f(1,Y). > Y = 0; > Y = 2; > Y = 4.  In this version the cuts do not only effect the procedural meaning of the program, but also change the declarative meaning. Controlling Backtracking: Cut

Prolog.37 Red and Green cuts:  When a cut has no effect on the declarative meaning of the program it is called a ‘green cut’. When reading a program, green cuts can simply be ignored.  Cuts that do effect the declarative meaning are called ‘red cuts’. This type of cuts make programs hard to understand, and they should be used with special care. Controlling Backtracking: Cut

Prolog.38 The Meaning of Cut  When the cut is encountered as a goal it succeeds immediately, but it commits the system to all choices made between the time the parent goal was invoked and the time the cut was encountered.  H :- B1, B2,..., Bm, !,... Bn. when the ! is encountered: The solution to B1..Bm is frozen, and all other possible solutions are discarded. The parent goal cannot be matched to any other rule.

Prolog.39  Consider the program C :- P, Q, R, !, S, T, U. C :- V. A :- B, C, D. And the goal: A Backtracking is possible within P,Q,R. When the cut is reached, the current solution of P,Q,R is chosen, and all other solutions are dumped. The alternative clause “C :- V” is also dumped. Backtracking IS possible in S,T,U. The parent goal is “C” so the goal A is not effected. The automatic backtracking in B,C,D is active. The Meaning of Cut:

Prolog.40 Examples using CUT  Adding elements to a list without duplication: add(X,L,L1) If X is a member of L then L1=L. Otherwise L1 is equal to L with X inserted: add(X, L, L) :- member(X, L), !. add(X, L, [X|L]).  Assume we have the relations ‘big(X)’, ‘medium(X)’ and ‘small(X)’, for example: big(elephant). medium(cat). small(mouse). We want a relation ‘boe(X,Y)’ for X is bigger or equal to Y boe(X,Y) :- small(X),!,small(Y). boe(X,Y) :- big(Y),!, big(X). boe(X,Y).

Prolog.41 Negation  The special goal fail always fails. ( like 1=0. )  The special goal true always succeeds. ( like 1=1. )  “Mary likes all animals but snakes” likes( mary, X) :- snake(X), !, fail. likes( mary, X) :- animal(X).  Define the relation “different” by the matching meaning - two terms are different iff they do not match. different(X, X) :- !, fail. different(X, Y).

Prolog.42  Defining “not”: if Goal succeeds then not(Goal) fails. Otherwise not(Goal) succeeds. not(P) :- P, !, fail. not(P).  Previous examples that use the combination “!, fail” can now be rewritten: different(X, Y) :- not(X = Y). Negation

Prolog.43  When possible, it is better to use ‘not’ than to use the ‘cut and fail’ combination.  Note that if the goal “not(A)” succeeds it does not mean that “A is not true” but that “given the current database, A cannot be proved”.  Can you explain the following results: assert( r(a) ). assert( q(b) ). assert( p(X) :- not(r(X)) ). q(X), p(X). > X = b. p(X), q(X). > false. The ‘Not’ Operator:

Prolog.44 Problems with cut and negation  We can use cut to specify mutually exclusive rules If condition then conclusion Q, otherwise conclusion R  If there is no cut in the program we can change the order of the clause and goals without changing the declarative meaning of the program With CUT ( as we saw) the previous declaration is not true  Is this a red or a green cut? min(X,Y,X):- X =< Y, !. min(X,Y,Y).

Prolog.45 Database Query  Represent a database about families as a set of facts. Each family will be a clause.  The structure of a family: each family has a husband, a wife and children. children are represented as a list. each person has a name, surname, date of birth and job.  Example: family( person(tom, fox, date(7,may,1950), works(bbc,15200)), person(ann, fox, date(9,jan,1949), works(ibm,20000)), [ person(pat, fox, date(1,feb,1973), unemployed), person(jim, fox, date(4,may,1976), unemployed)]).

Prolog.46 family person ann fox date works 9 jan 1949ibm []

Prolog.47 Structure Queries  Are there armstrong families: family( person(_,armstrong,_,_),_,_)  Are there families with 3 children? family(_,_,[_,_,_])  Names of families with 3 children. family(person(_,Name,_,_),_,[_,_,_])  All married women that have at least two children: family(_,person(Name,Surname,_,_),[_,_|_]).

Prolog.48 Defining useful relations: husband(X) :- family(X,_,_). wife(X) :- family(_,X,_). child(X) :- family(_,_,Children), member(X, Children). % the member we % already defined exists( Person ) :- husband(Person); wife(Person); child(Person). dateofbirth( person(_,_,Date,_),Date). salary(person(_,_,_,works(_,S)), S). salary(person(_,_,_,unemployed, 0). Structure Queries Or operator

Prolog.49  Names of all people in the database: exists( person(Name,Surname,_,_)).  All employed wives: wife(person(Name,Surname,_,works(_,_))).  Unemployed people born before 1963: exists(person(Name,Surname,date(_,_,Year), unemployed)), Year <  People born before 1950 whose salary is less than 8000: exists(Person), dateofbirth(Person,date(_,_,Year)), Year < 1950, salary(Person, Salary), Salary < 8000 Structure Queries

Prolog.50  Calculating the total income of a family: total([], 0). total([Person|List], Sum) :- salary( Person, S), total(List, Rest), Sum is S + Rest. tot_income(family(Husband,Wife,Children),I) :- total([Husband, Wife | Children], I).  All families that have an income per family member of less than 2000: tot_income(family(Husband,Wife,Children), I), I/N < Structure Queries total(People_lis t, Total_salaries)

Prolog.51 From tests 2

Prolog.52 From tests 2

Prolog.53 From tests 3

Prolog.54

Prolog.55 From tests 4

Prolog.56

Prolog.57 From tests 5

Prolog.58