Cognitive Modelling Prolog
This booklet is intended to introduce you to the basics of Prolog and enable you to solve problems and
represent or model certain scenarios in Prolog.
Modelling using production rules
Facts and relations between facts
Anderson’s (1983; 1993b) ACT models contain two forms of long-term memory: a declarative memory (factual
knowledge) and a procedural memory (‘how-to’ knowledge). Declarative memory would contain facts such as ‘I was
tired last night’, the way to this room, what ‘bread’ means, dandelions are yellow, a labrador is a type of
dog, and so on. Facts such as these usually contain some kind of attribute of things or some kind of
relationship between things. For example, if you believe something like ‘Hippies are really cool’ then you
have an adjective ‘cool’ that refers to ‘hippies’. Another way of saying this is to state that an attribute of
hippies is their real coolness. A way of representing this attribute of hippies is to strip out some of the
words in a sentence and concentrate on the main underlying ideas, which are: ‘hippies’; the way of describing
them – ‘cool’; and the fact that ‘cool’ is an ‘attribute’.
Figure 1 shows a visual representation of the category of hippies and the attached attributes of that
category.
Figure 1 Representation of an object (hippies) and some attributes of that object (e.g., ‘cool’).
This can be written in a kind of shorthand thus:
a) cool(hippies)
b) hippies(cool)
Or alternatively:
c) attribute(hippies, cool).
Notice here the very important point that there can be more than one way of representing knowledge. The way
you choose to represent it as either a) or c) has implications for how such a representation can be
manipulated, how efficient or ‘economical’ it is and hence how useful such a representation might be.
Snow, as you know, is white; so that can be written:
attribute(snow, white).
Snow is also cold so you could add:
attribute(snow, cold).
to your database of facts about snow. This way of representing a sentence is known as a proposition. Indeed,
knowledge in general can be represented by a network of such interrelated propositions.
Activity 1
You know that coffee is usually warm and wet. How would you write that in the form of propositions?
What you have just done in ACTIVITY 1 is to write your first bit of Prolog. Easy, wasn’t it?
You can also represent a relationship between concepts. For example, Paris, Vienna and Madrid are the capital
cities of France, Austria and Spain respectively. Figure 2 shows a diagrammatic representation of the relation
between one city and the country.
Figure 2 Diagrammatic representation of the relation (capital_of) between a city (Paris) and a country
(France).
The capital city relation can be represented in Prolog as propositions:
capital_of(paris, france).
(The reason for the lower case letters instead of upper case will be explained later.)
Likewise you can have a list of such relations:
capital_of(madrid, spain).
capital_of(vienna, austria).
capital_of(rome, italy).
Outside the brackets is the relation and inside the brackets there are two arguments. A set of facts such as
those constitutes a database. In this form of representation the relation (or attribute or whatever) outside
the brackets is known as predicate.
A bit about Prolog
Prolog is a programming language and stands for Programming in Logic. In that sense Prolog is like a language
such as English. English is not a paragraph, it’s not a set of instructions; it is the language in which a
paragraph or set of instructions might be written. A similar paragraph of set of instructions could be written
in French or Greek. Similarly a program or model can be written in Prolog. They can also be written in Lisp or
in C++. We happen to be using Prolog.
In a Prolog program there is usually some kind of database and a set of rules. We will come onto rules a bit
later but for the moment we will look only at a database of facts such as the ‘capital_of’ set of facts above.
You can then interrogate the database to find things out. So you could ask it what the capital of France is or
check whether Vienna is indeed the capital of Austria.
Essentially all Prolog does is try to find a pattern that matches information in its database. If it finds
such a pattern it will respond ‘YES’ and if it doesn’t it will say ‘NO’. A database just sits there doing
nothing until you interrogate it in some way. To find things out about a database you have to ask a question
in Prolog. This is known as a query (and is often in a separate window where there is a ‘?’ prompt for the
query). So, if you were to query the database of capital cities in Prolog you might enter:
?capital_of(paris, france).
which would be similar to asking ‘Is Paris the capital of France?’ SinceProlog has a statement in the database
that matches the query exactly it would reply:
Yes
Activity 2
How would Prolog respond to:
?capital_of(paris, spain).
?capital_of(italy, rome).
Why?
Values and variables
Inside the brackets of the ‘capital_of’ database there are specific names of cities and countries. However,
sometimes it is useful to make a generalisation rather than refer to specific instances. For example, you
might know that in a particular classroom there are 4 people: Sheelagh, Sachdev, Katerina and Dave. You also
know that Sheelagh speaks French, Sachdev speaks French, Katerina speaks French and Dave speaks French. A more
economical way of representing this information is to state that everyone in the classroom speaks French. If
you are told that everyone there speaks French and then someone asks you if Mukesh speaks French, you can say
yes. In this little scenario ‘everyone’ is a word that can stand for any specific individual in the classroom.
In Prolog, too, you can have terms that can stand for a range of other specific instances. Such a word is
known as a variable. A variable is a bit like a box that you can put different things in at different times.
The objects that you put in a box are known as values. If you are asked “Does Mukesh speak French?” and you
know that everyone in the classroom speaks French and that Mukesh is in the classroom then ‘everyone’ is a
variable and ‘Mukesh’ is the value it takes on when you answer the question. There are several variables in
the last paragraph including, for example the word ‘you’. This variable changes its value depending on whoever
happens to be reading this right now.
In Prolog anything beginning with a capital letter (except the relation name outside the brackets) is a
variable. So ‘dave’ is a specific value but if you were to write ‘Dave’ it would turn into a variable, so you
need to be careful what names you use for variables so you don’t get yourself confused.
Suppose you had a database that says:
speaks_french(dave).
speaks_french(sheelagh).
you could pose the query:
?speaks_french(Who).
Since ‘Who’ begins with a capital, it is treated as a variable. You could write any-thing that begins with a
capital. speaks_french(X). speaks_french(NRYU??53j)would also work.What Prolog does is to check through its
database for any relation called speaks_french and then tries to find the first value that will fill the
variable box: ‘Who’. The first one it comes across is speaks_french(dave), so Prolog will respond:
Who = dave
Yes.
As was mentioned earlier Prolog just tries to prove a statement true or false based on the information in its
database, so the response above can be interpreted by human beings to mean:
Yes I can find a pattern that matches the query, oh and by the way, ‘Who’ now contains the value ‘dave’.
Activity 3
How would you write a query that asks what is the capital of Spain (Hint: Look at the database labelled B
above)?
How would Prolog respond?
Facts and rules
So far we have concentrated on the kinds of facts one might have stored in long-term memory. However we also
have knowledge in the form of rules. Rules are extremely useful since they provide a way of storing general
knowledge derived from experience economically. For example, from experience you may have formed the rule that
if you lie in the sun too long you will get sunburned; or, if someone comes from Germany they will speak
German; or, if someone is blond then they are dumb. Notice that your rules are not right or wrong but, rather,
more or less useful. Our prejudices and assumptions can be represented as sets of inference rules.
For example, experience may tell you that Jenny has long hair and Jenny is a girl; Samantha has long hair and
Samantha is a girl, Mumtaj has long hair Mumtaj is a girl, etc. Here is a database of people with long hair:
hair_length(jenny, long).
hair_length(mumtaj, long).
hair_length(samantha, long).
As a result of your experience (represented in the database above) you might have generated a general rule
that says: ‘If someone has long hair then that someone is a girl’. Here is one way of representing that rule
in Prolog:
sex(Person, female) The sex of a person is female (‘Person’ starts with a capital letter and so it is a
variable and refers to any person)
:- IF (the symbol :- means ‘if’ in Prolog roughly speaking)
hair_length(Person, long). that same person has long hair.
The whole rule in Prolog would be written thus:
sex(Person, female):-
hair_length(Person, long).
Activity 4
Here is a database of information about the whereabouts of various people:
location(dave, room).
location(sheelagh, room).
location(mukesh, room).
location(katerina, room).
location(sam, kitchen).
location(claudette, tescos).
You know that the people in the room all speak French. How would you write a rule in Prolog that states that
any particular person speaks French if they happen to be in the room.
(Hint: Use examples C, D & E above to help you.)
NOTE:
Notice the full stops at the end of every Prolog statement. The full stop marks the end of a simple fact or of
a very complex rule. Commas are used in Prolog to mean ‘and’.
Summary
• Prolog can represent facts in the form of attributes and relations (amongst other things).
• A relation between objects or attribute of objects can be represented as a predicate outside a set of
brackets that contains arguments e.g., offered(colin, eleni, flowers).
• Items inside brackets written in lowercase letters are specific values.
• Items that begin with an uppercase letter are variables. Variables are like slots that can take on
(can be ‘filled with’) specific values.
• A query in Prolog begins with a ?. Prolog tries to find a match in its database for whatever is stated
in the query. The match has to be exact.
• If Prolog finds a match it responds with a YES. If not it responds NO. Anything else it does is a side
effect. Usually it is the side effects we are interested in.
• A statement in Prolog represents a declarative fact (usually).
• A set of facts constitutes a database. A database can also contain rules.
• Rules in Prolog are ‘IF…THEN’ rules. Prolog is therefore a Production System language.
• The head of the rule is what has to be proved.
• The symbol ‘:-‘ means ‘IF’, roughly speaking.
• The end of a Prolog statement is marked by a full stop.
• A comma in Prolog means ‘and’.
• The body of the rule contains all the things that have to be satisfied before the head of the rule can
be proved.
How Prolog works
There are three important problems involved in designing and building a model.
1. How to represent the cognitive processes in the first place;
2. How to turn that representation into Prolog code;
3. How to debug it when it goes wrong.
We are going to start with the last of these for the time being. But before you can debug the program you need
to have an idea how Prolog works in the first place.
Activity 5
Using the database above write a rule called full_siblings that says that two people are full siblings if they
share the same mother and father.
Representing the problem
a. You are asked to call the rule ‘full_siblings’ so we can begin with that. What goes inside the
brackets? (Hint: the full siblings must be two children)
full_siblings( )
b. They can only be siblings if the two children share the same father. This means the father of one
child is the same person as the father of the second child.
(Hint: look at the first half of the database above and remember that the first name inside the brackets is
the name of the father; the second name is the child of that father. Hint 2: there are two children.)
c. They can only be siblings if the two children share the same mother as well. This means the mother of
one child is the same person as the mother of the other.
For fun (?!):
Recursion
Recursion occurs in many places including language, mathematics and computing. It refers to a rule that calls
itself within itself. In language we can embed clauses inside other clauses and embed another clause inside
it, and so on. Here is one verse from the Mother Goose poem “The House that Jack Built”.
This is the priest all shaven and shorn,
That married the man all tattered and torn,
That kissed the maiden all forlorn,
That milked the cow with the crumpled horn,
That tossed the dog,
That worried the cat,
That killed the rat,
That ate the malt
That lay in the house that Jack built.
There are numerous humorous definitions of recursion such as:
Recursionn. See Recursion.
Which takes you round in an infinite loop – a big danger with recursion. If you write a computer program
involving recursion with an infinite loop it will try to go on forever, run out of memory very quickly and
crash. There needs to be a “halting condition” so that the program gets a chance to stop somewhere. If you
extended the father_of and mother_of database by a considerable amount you could use it to write a rule about
who your ancestors are. The mother of your father is your ancestor; the mother of the mother of your father is
your ancestor; the mother of the mother of the mother of your father is your ancestor, and so on. Fortunately,
it stops when the rule reaches your parents since ancestors don’t go any further back than that.
Here is an example of a recursive rule in Prolog. The first part, H, is a database of facts about who has been
kissing whom. The second part, I, is one fact and a rule both with the same name “has_flu”. The first
represents the fact that Peter has flu. The second is a rule saying that anyone who kisses someone who has flu
also has flu. has_flu(peter) is the halting condition. It allows the program to work out whether anyone in the
list has flu.
kisses(dave, jane).
kisses(jane, bill).
kisses(bill, simone).
kisses(simone, mary).
kisses(mary, peter).
has_flu(peter). Peter has flu (halting condition).
has_flu(Person):- Someone has flu if
kisses(Person, Someone), they kiss someone
has_flu(Someone). who has flu.
Activity 6
How would you write a rule that works out whether someone is a grandparent?
Representing the problem
To show that someone is a grandparent you need to determine if that someone is the parent of someone who has a
child (i.e., the parent of someone who is, in turn, the parent of someone else).
a. Write the head of the rule. (Hint: you are trying to show that a particular person is a grandparent):
grandparent( )
c. Write the body of the rule.
(Hint: Start with the father_of database. Someone is a grandfather if they are the father of a child and that
child is the father of another child):
Now check your answer on page 16
The cognitive modelling project
Children’s Subtraction Errors
Prolog is a Production System. That is, it uses ‘if-then’ or ‘condition-action’ rules. Some researchers (e.g.,
Anderson, 1993b) have argued that cognitive skills can be modelled as a Production System. Production Systems
were discussed in the Introductory lecture (and in Cognitive Psychology PSY12-2) and are discussed in many
Cognitive Psychology textbooks. This project involves a Production System model of children’s subtraction
errors. Here you will be modelling a theory or theories that argue that children’s subtraction errors are not
random but most are in fact rule-governed. When children make mistakes they are following rules very well, but
the rules themselves are ‘buggy’ — they have built-in errors or are incomplete. You will find a list of
children’s errors in Brown and Burton’s (1978) paper and in Young and O’Shea (1981) (in Short Loan
collection). For example, a common error is always to take the small number from the large one in a
subtraction sum irrespective of whether the large number is on the top or on the bottom. Thus the child might
generate the following buggy solution:
45
-27
22
In this section I will talk you through the process of building a Prolog model of the normal (correct)
subtraction rule and the errors described in those papers.
Before you start working out how to represent the problem of modelling the errors, you can make some
simplifying assumptions. To make it easier, the first thing to assume is that children already know some
simple subtraction facts such as 4 minus 2 equals 2. This can be written in Prolog as:
minus(4, 2, 2).
Activity 6
How would you represent the following:
‘8 minus 7 equals 1’
‘5 minus 3 equals 2’
Whichever way you decide to represent the simple subtraction ‘facts’ make sure you use the order of figures in
the brackets consistently. You can also assume that children know some simple addition facts such as 1 plus 3
is 4, or 10 plus 5 is 15. You could represent them as ‘add’ facts such as:
add(1, 3, 4).
add(10, 5, 15).
Based on these assumptions the first task (and one of the hardest) is to work out the detailed rules in
English for solving two figure subtraction problems. Based on the English rules you now need to work out how
to translate that into Prolog.
1. First, generate a detailed rule in English of how to do two figure subtraction and then figure out how
this might be written in Prolog. Assume that you want to do the following subtraction sum:
45
– 23
•
In your group write down, in detail, how you would solve this sum.
Hints: Begin with the overall goal:
‘In order to subtract…’
Then sat how you would achieve that goal.
‘First subtract…
‘And write down the answer…
1. Assuming that the student knows that 5-3=2 and that 4-2=2, how would you convert your English rule for
the subtraction sum into a Prologrule. Give your subtraction rule a useful name like ‘subtraction’.
Hint: Your Prolog rule will be of the form:
subtraction( … ):-
minus( … ),
minus( … ).
2. Now try the subtraction rule out in AmziProlog. First write the two minus facts you need, then type in
the subtraction rule. In the query window type ‘subtraction(4, 5, 2, 3).’ Prolog should now reply:
YES
As far as you are concerned this means ‘Yes I can do this subtraction sum’; as far as Prolog is concerned this
means ‘Yes I can find a pattern that matches that query.’
3. What you now have is a specific rule for a specific sum (45 minus 23). This isn’t much use as a rule
since it doesn’t cover any other sum. To make it generalisable you need to replace the specific values with
variables. That way anytwo column subtraction problem will work using the general rule. Rewrite the rule
replacing fixed values with variables.
4. Once you have worked out how to do a simple subtraction where the top right figure is larger than the
bottom right figure, you can rewrite the rule to deal with cases where the top right figure is smaller than
the bottom right figure, as in 43 – 25 = 18. To do this you have to subtract 1 from the top left (using the
‘decomposition’ method of subtraction) and add 10 to the top right figure. There will therefore have to be
some addition facts in the database.
Again go through the sum together in English before translating it into Prolog, and begin with that specific
sum only.
5. Now rewrite the rule using variables to replace the specific values.
6. So far all Prolog will respond to any query is YES. This isn’t much of a model of the cognitive
processes of a child learning subtraction. What you want to do is to get Prolog to write down the answer that
a child would write down when doing this sum. At the top of the program you will see a rule called ‘writeit’.
This has the effect of writing out a sum the way a child would write it. If it has to write out the sum it
needs to know the figures (TopLeft, TopRight, BottomLeft, BottomRight, AnswerLeft, AnswerRight).
If you include a call to the writeit rule at the end of each rule, Prolog will print out the sum for you (as a
side effect of answering the query). At the end of the two subtraction rules, replace the full stop with a
comma, go to the next line and write:
writeit(TopLeft, TopRight, BottomLeft, BottomRight, AnswerLeft, AnswerRight).
Now try running the two subtraction rules in Prolog filling in the correct sum in the query for each.
7. The next thing to do is to take one of the simple errors, such as always taking the smaller number
from the larger number, and model it using a variation of the subtraction rule in part 2 above. At this stage
it is best to give each ‘buggy’ subtraction rule a different name.
8. Now try modelling more of the subtraction errors including ‘zero pattern’ errors. You might find it
useful to use the ‘write’ and ‘nl’ predicates to make comments on what the error is and why it occurs.
The Report
The project report should be around 2000-25000 words and should take much the same form as an experimental
report. It should begin with an informative title and an Abstract of about 120 words. Each section of the
report is discussed in more detail below. The sections are:
1. anIntroduction that deals with the theory that is being modelled (children’s subtraction);
2. anMethod section where you discuss the model itself and how you built it;
3. aResults section which gives some examples of what happened when the program ran;
4. aDiscussion section where you should examine how well the model does what it sets out to do;
5. aReference section where all in-text citations are referenced (not included in the word count);
6. anAppendix containing a printout of the program and some illustrative queries (not included in the
word count).
The Abstract
The Abstract has the same format and logic as an Abstract for an experimental report. You are saying what you
did, why you did it, what you expected to find, what you found, what it means.
The Introduction
In the Introduction the focus is on the theory or ‘domain’ that your program is meant to model. There are two
main areas that you are bringing together here. The first is children’s subtraction errors, and the second is
that much human problem solving appears to be rule-governed. If this is the case then it should be possible to
model children’s errors by specifying the rules they are using. Explaining why you are building this model is
the equivalent of a rationale. The Introduction should conclude with an account of what you expect to find if
the model is correct (this is the equivalent of a hypothesisbut it is not a hypothesis).
The Method
Here you describe how you modelled different aspects of the theory with a few examples. For example, you might
describe what method of subtraction you are modelling, the rule for correct subtraction and one or two
examples of ‘buggy’ subtraction, and how you deal with ‘carries’ and ‘borrows’. You would also state briefly
here how you justified certain decisions to put some things into your rules or miss things out. This last
aspect might be important in the Discussion section.
The Results
This should be a relatively short section. Give examples of how well your program worked, and what, if
anything, it failed to model adequately. Again, provide brief examples (4 or 5) of the running of one or two
queries and refer to the Appendix where the program is written out. Please make it easy for the poor reader to
find what you are referring to.
The Discussion
Here you begin by discussing how well the program modelled what it set out to model (i.e., how well it did
what you said it would do at the end of the Introduction). Does it model children’s subtraction errors
adequately? Are there any fudges? Does it provide evidence that human thinking is rule-governed? You would
discuss its shortcomings — briefly, as it would take a PhD to do this kind of thing properly — and its
successes. You might then go on to discuss the implications of the model (the role of Production Systems
perhaps) and where you might go from here. You might end with a brief look at the role of cognitive modelling
in general and its role in helping us understand human cognitive processes.
Grading
The bulk of the marks will be for the Introduction and Discussion.
For an A-grade:
you will need to build up to a clear rationale for the model relating it to past research and current theory
in the Introduction (e.g., rule-governed behaviour, Anderson’s 1993a, b ACT-R theory) and should also include
evidence of wide reading on the topic area. The Implementation and Results section will show a clear
understanding of how Prolog works and how it models the errors. The Discussion section should show a good
understanding and critical evaluation of:
• how well the program models cognitive processes
• how this can be interpreted
• the issues involved in modelling using Production systems
• the role of cognitive modelling as evidence
There should also be a clear structure of the argument in the Introduction and Discussion.
For a B grade:
you will need to build up to a clear rationale for the model relating it to past research and current theory
in the Introduction (e.g., rule-governed behaviour, Anderson’s 1993a, b ACT-R theory). The Implementation and
Results section will show a reasonably clear understanding of how Prolog works and how it models the errors.
The Discussion section should show an understanding of
• how the program models cognitive processes
• the issues involved in modelling using Production systems
• the role of cognitive modelling as evidence
There should also be evidence of some reading beyond the specific texts available on the topic area.
For a C grade:
There should be a reasonably clear structure to the Report, or the structure may be weak but show some
understanding of the issues. Overall the description and discussion of the model and issues should be correct
or contain few misconceptions or errors but may not go into those issues in much depth.
A D grade
• will show evidence of some misunderstandings and/or lack clarity
• have relatively sparse or inadequate coverage of the material
• miss key points of the arguments.
ORDER THIS ESSAY HERE NOW AND GET A DISCOUNT !!!
You can place an order similar to this with us. You are assured of an authentic custom paper delivered within the given deadline besides our 24/7 customer support all through.
Latest completed orders:
# | topic title | discipline | academic level | pages | delivered |
---|---|---|---|---|---|
6
|
Writer's choice
|
Business
|
University
|
2
|
1 hour 32 min
|
7
|
Wise Approach to
|
Philosophy
|
College
|
2
|
2 hours 19 min
|
8
|
1980's and 1990
|
History
|
College
|
3
|
2 hours 20 min
|
9
|
pick the best topic
|
Finance
|
School
|
2
|
2 hours 27 min
|
10
|
finance for leisure
|
Finance
|
University
|
12
|
2 hours 36 min
|