Presentation is loading. Please wait.

Presentation is loading. Please wait.

School of Computing and Mathematics, University of Huddersfield Computing Science: WEEK 18 LECTURE: Comparative Programming Languages + Prolog PRACTICALS:

Similar presentations


Presentation on theme: "School of Computing and Mathematics, University of Huddersfield Computing Science: WEEK 18 LECTURE: Comparative Programming Languages + Prolog PRACTICALS:"— Presentation transcript:

1 School of Computing and Mathematics, University of Huddersfield Computing Science: WEEK 18 LECTURE: Comparative Programming Languages + Prolog PRACTICALS: Again, read through the notes on Blackboard OR on web – http://scom.hud.ac.uk/scomtlm/prolog/ and do the exercises as directed

2 School of Computing and Mathematics, University of Huddersfield Prolog - getting started Writing programs in Prolog is a cycle involving 1. Write/Edit the program in a text-editor 2. Save the program in the text editor 3. Tell Prolog to load the program 4. If Prolog gives you errors, go back to step 1 and fix them 5. Test it - if it doesn't do what you expected, go back to step 1 (reference: http://www.cs.may.ie/~jpower/Courses/PROLOG/)

3 School of Computing and Mathematics, University of Huddersfield Prolog Structure parent(X,Y) :- father(X,Y). parent(X,Y) :- mother(X,Y). grandad(X,Y) :- father(X,Z),parent(Z,Y). father(jacob,freda). mother(freda,frank). -- a Prolog program is a sequence of CLAUSES, each ending in ‘.’ -- each clause may be either a FACT or a RULE. ::=. ::= ( ) ::= | | symbol( )

4 School of Computing and Mathematics, University of Huddersfield Prolog Structure parent(X,Y) :- father(X,Y). parent(X,Y) :- mother(X,Y). grandad(X,Y) :- father(X,Z),parent(Z,Y). father(jacob,freda). mother(freda,frank). A rule contains a HEAD and RULE BODY separated by the ':-' symbol. Each head must be a predicate and each rule body is a sequence of predicates separated by commas Rules can be read two ways: DECLARATIVELY or PROCEDURALLY. ** X is the grandad of Y is true if there exists a Z such that X is the father of Z and Z is the parent of Y. (declarative) ** To prove X is the grandad of Y, first prove X is the father of some Z, then prove this Z is the parent of Y. (procedural)

5 School of Computing and Mathematics, University of Huddersfield To execute Prolog programs parent(X,Y) :- father(X,Y). parent(X,Y) :- mother(X,Y). grandad(X,Y) :- father(X,Z),parent(Z,Y). father(jacob,freda). mother(freda,frank). To execute/activate/run a program a GOAL is typed to the Prolog interpreter. It responds with no (failure) or yes (success) together with the successful instantiations of variables that appeared in the goal. ?- grandad(jacob,frank). -yes ?- grandad(X,frank). X = jacob -yes

6 School of Computing and Mathematics, University of Huddersfield How Prolog works…. parent(X,Y) :- father(X,Y). parent(X,Y) :- mother(X,Y). grandad(X,Y) :- father(X,Z),parent(Z,Y). father(jacob,freda). mother(freda,frank). To solve a goal such as: ?-grandad(jacob,frank). Prolog tries to MATCH this goal with facts and rule heads in the program, starting from the top and working down. -- constants/predicate symbols must match with constants/predicate symbols -- variables can match with any term When a variable MATCHES with a term we say it is instantiated.

7 School of Computing and Mathematics, University of Huddersfield How Prolog works…. parent(X,Y) :- father(X,Y). parent(X,Y) :- mother(X,Y). grandad(X,Y) :- father(X,Z),parent(Z,Y). father(jacob,freda). mother(freda,frank). To solve a goal such as: ?-grandad(jacob,frank). When a rule head is matched, matching variables become instantiated throughout the rule, and the TAIL of the rule is executed as if it were a GOAL. So Prolog executes ‘father(jacob,Z),parent(Z,frank)’ next….

8 School of Computing and Mathematics, University of Huddersfield How Prolog works….example from notes.. online(kings_cross,northern). online(kings_cross,circle). online(kings_cross,victoria). online(kings_cross,piccadilly). online(barbican,circle). online(holburn,circle). online(moorgate,northern). online(moorgate,circle). online(barbican,circle). online(bank,northern). online(angel,northern). online(finsbury_park,piccadilly). sameline(Station1,Station2,Line1) :- online(Station1,Line1), online(Station2,Line1). interchange(Line1,Line2,Station1) :- online(Station1,Line1), online(Station1,Line2). route(Station1,Station2) :- sameline(Station1,Station2,Line1), nl,write('use '),write(Line1),write(' direct'),nl. route(Station1,Station2) :- online(Station1,Line1), interchange(Line1,Line2,StationX), sameline(Station2,StationX,Line2), nl,write('use '),write(Line1), nl,write('change at '),write(StationX), nl,write('then use '),write(Line2).

9 School of Computing and Mathematics, University of Huddersfield Summary - A Prolog program is a list of facts and rules, executed by typing in a goal. n Facts and rules are made from predicates – which is a predicate symbol followed by 0,1, or more terms n A rule may be given a declarative (logical) or procedural interpretation. n Two predicates match if their predicate symbols and ‘arities’ are identical, and corresponding arguments are identical or variables can be instantiated to terms. n Prolog executes a goal by trying to match the goal with the head of a rule (or a fact) in top down order. If the goal matches with a rule head, Prolog then tries to execute the rule’s tail (which is a list of goals).


Download ppt "School of Computing and Mathematics, University of Huddersfield Computing Science: WEEK 18 LECTURE: Comparative Programming Languages + Prolog PRACTICALS:"

Similar presentations


Ads by Google