Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Prolog COMP 112 #
© Peter Andreae COMP :2 Outline Prolog – a very different programming language Admin: No lecture next Monday (Queen’s Birthday) Final lecture: summary, exam, Tues 3 rd June No lecture 5 th June Lab on Friday, but not next week.
© Peter Andreae COMP :3 Programming Paradigms Imperative programming Program tells the computer what to do. Object Oriented programming imperative plus organise data and code around classes of objects Functional programming Specify computation of values No side effects, Lazy evaluation Logic programming specify relationships that are true ask queries. interpreter works outs how to use the specified relationships to answer the queries.
© Peter Andreae COMP :4 Prolog program Variables start with uppercase: X, List, Explanation, (or an underscore: _rest) literals start with lowercase: a, b name, bsc lists are in [ … ] contains( [X], X). contains([X | _rest], X). contains([_any | Rest], X) :- contains(Rest, X). which is input and which is output?
© Peter Andreae COMP :5 Why use prolog If the core of the problem is expressing complex relationships between data, prolog is much easier to use than Java. eg rule-based “expert-systems”, (now called “business-intelligence”) codifying business rules (eg credit limit applications) eg landcare’s possum control decision support system. Parsing natural language prolog is excellent for expressing structure in language. It allows you to redefine the syntax to match the problem.
© Peter Andreae COMP :6 Checking degree rules. Student completed bsc with M major because E1 and E2 and E3 and E4 and E5 and EM:- Student has 360 points from any numbered because E1, Student has 180 points from any numbered because E2, Student has 72 points from bsc numbered because E3, Student has 270 points from bsc numbered because E4, Student has 120 points from bsc numbered because E5, Student completed M major because EM. Student completed comp major because E1 and E2 and E3:- Student has 60 points in comp numbered because E1, Student has comp201 and comp202 and (comp203 or comp204) because E2, Student has math114 and (stat193 or stat131 or quan102) and (math214 or math207 or opre251 or stat231) because E3.
© Peter Andreae COMP :7 Degree rules…. Student has Required points from Schedule numbered Range because (TotPts >= Required points from Schedule numbered Range) :- allCourses(Student, Courses), validPoints(Courses, Schedule, any, Range, TotPts), TotPoints >= Required. Student has Course1 and Course2 because Expl1 and Expl2 :- Student has Course1 because Expl1, Student has Course2 because Expl2. Student has Course because Course:- passed(Student, Course).
© Peter Andreae COMP :8 Degree rules…. code to enable the “natural language” syntax: :- op(815, xfx, has).:- op(807, fx, in). :- op(815, xfx, completed).:- op(805, xfy, --). :- op(813, xfx, because).:- op(803, xfy, or). :- op(811, xfx, points).:- op(801, xfy, and). :- op(811, xfx, of).:- op(801, xfy, with). :- op(809, xfx, numbered).:- op(800, xf, major). :- op(807, fx, from).:- op(800, xf, submajor).
© Peter Andreae COMP :9 Language rules in Prolog: Prolog program using DCG rules: sentence(sent(NP,VP)) --> noun_phrase(NP), verb_phrase(VP). noun_phrase(np(D,N)) --> det(D), noun(N). verb_phrase(vp(V,NP)) --> verb(V), noun_phrase(NP). det(the) --> [the]. det(a) --> [a]. noun(cat) --> [cat]. noun(bat) --> [bat]. noun(flies) --> [flies]. verb(eats) --> [eats]. verb(flies) --> [flies]. verb(gives) --> [gives]. parse(Tree, L) :- sentence(Tree, L, []).