280 likes | 393 Views
Programming Paradigms. CPSC 449 Week 10 Prepared by : Mona Hosseinkhani , Winter 2014. Department of Computer Science, University of Calgary. SWI-Prolog. http://www.swi-prolog.org / http:// www.gprolog.org /. MacOSX. Download the installation file and run it Search for swipl
E N D
Programming Paradigms CPSC 449 Week 10 Prepared by : Mona Hosseinkhani, Winter 2014 Department of Computer Science, University of Calgary
SWI-Prolog • http://www.swi-prolog.org/ • http://www.gprolog.org/
MacOSX • Download the installation file and run it • Search for swipl • In Mac OS, you can find it in • /opt/local/bin/ • Run swipl
Windows • Download the installation file and run it • Run swipl.exe in the directory where swipl is installed • Running a.plfile when starting swipl, change directory to the directory in which the file to open resides, and load the file.
How to write and run a program • Write all facts and rules in a .pl file • Run swipl • Load your database using [pl file] command • To quit swipl, use“halt.” command or use “<ctrl>+<D>” • For help, use “help.” command • For comments, use • /* */
How to write and run a program • Prolog provides an interactive shell that you can use it directly by asserting facts and asking queries as you go along. • ?- Y is 7, X is Y+3. Y = 7, X = 10. • The listing command can be used to list the predicates that you have specified in your program, for example: • listing(woman) (in kb1.pl example)
Syntax • The name of all objects and relationship must begin with a lowercase letter. • The name of all variable must begin with an uppercase letter • At the end of each clause (fact or rule), there must be a dot, called period or full stop. • The order of objects in a relationship is arbitrary but it must be kept consistent. • The name of a relationship is called a predicate . • A collection of facts and rules is called a database. • ‘,’ stands for conjunction.
Example1 • ?-male(albert) • True • ?-male(X) • albert • You can just press ; to see other instantiations of X male(albert). male(edward). female(alice). female(victoria). parents(edward,victoria,albert). parents(alice,victoria,albert). sister_of(X,Y):-female(X),parents(X,F,M),parents(Y,F,M).
Example2 • Database • ?-sister_of(alice, edward). male(albert). male(edward). female(alice). female(victoria). parents(edward,victoria,albert). parents(alice,victoria,albert). sister_of(X,Y):-female(X),parents(X,F,M),parents(Y,F,M).
Example2 • sister_of(alice, edward) unifies with sister_of(X,Y) • X is instantiated to alice and Y is instantiated to edward. • ?-female(alice) • True male(albert). male(edward). female(alice). female(victoria). parents(edward,victoria,albert). parents(alice,victoria,albert). sister_of(X,Y):-female(X),parents(X,F,M),parents(Y,F,M).
Example2 • ?- parents(alice, F, M) • F is instantiated to victoria and M is instantiated to albert. • ?- parents(edward, victoria, albert) • True male(albert). male(edward). female(alice). female(victoria). parents(edward,victoria,albert). parents(alice,victoria,albert). sister_of(X,Y):-female(X),parents(X,F,M),parents(Y,F,M).
Example2 • ?-sister_of(alice, edward). • True. male(albert). male(edward). female(alice). female(victoria). parents(edward,victoria,albert). parents(alice,victoria,albert). sister_of(X,Y):-female(X),parents(X,F,M),parents(Y,F,M).
Example3 • ?-sister_of(X, Y). • ?- female (X) • X is instantiated to alice. • ?- parents(alice, M, F) male(albert). male(edward). female(alice). female(victoria). parents(edward,victoria,albert). parents(alice,victoria,albert). sister_of(X,Y):-female(X),parents(X,M,F),parents(Y,M,F).
Example3 • ?- parents(alice, M, F) • M is instantiated to victoria, and F is instantiated to albert. • ?- parents(Y, victoria, albert) male(albert). male(edward). female(alice). female(victoria). parents(edward,victoria,albert). parents(alice,victoria,albert). sister_of(X,Y):-female(X),parents(X,M,F),parents(Y,M,F).
Example3 • ?- parents(Y, victoria, albert) • Y is instantiated to edward male(albert). male(edward). female(alice). female(victoria). parents(edward,victoria,albert). parents(alice,victoria,albert). sister_of(X,Y):-female(X),parents(X,M,F),parents(Y,M,F).
Example • ?- sister_of(X, Y) • X= alice, Y= edward • ; male(albert). male(edward). female(alice). female(victoria). parents(edward,victoria,albert). parents(alice,victoria,albert). sister_of(X,Y):-female(X),parents(X,M,F),parents(Y,M,F).
Example3 • ?- sister_of(X, Y) • From the previous search • X is instantiated to alice, M is instantiated to victoria, and F is instantiated to Albert • ?-parents(Y, victoria, Albert) male(albert). male(edward). female(alice). female(victoria). parents(edward,victoria,albert). parents(alice,victoria,albert). sister_of(X,Y):-female(X),parents(X,M,F),parents(Y,M,F).
Example3 • ?-parents(Y, victoria, Alberta) • Y is instantiated to alice male(albert). male(edward). female(alice). female(victoria). parents(edward,victoria,albert). parents(alice,victoria,albert). sister_of(X,Y):-female(X),parents(X,M,F),parents(Y,M,F).
Example3 • ?-sister_of(X, Y). • X=Y, Y=alice • i.e. alice is a sister of alice male(albert). male(edward). female(alice). female(victoria). parents(edward,victoria,albert). parents(alice,victoria,albert). sister_of(X,Y):-female(X),parents(X,M,F),parents(Y,M,F).
Example3 • It does not make sense that a person be her own sister. • Change the rules to avoid such a situation male(albert). male(edward). female(alice). female(victoria). parents(edward,victoria,albert). parents(alice,victoria,albert). sister_of(X,Y):-female(X),parents(X,M,F),parents(Y,M,F), X\=Y.
Example4 (backtracking) • ?-eligible(X, Y) • ?-student (X) • X is instantiated to albert • ?-scholarship(Y, Z) student(albert). student(edward). scholarship(vanier,3). scholarship(queenElizabeth, 4). year(albert, 2). year(edward, 4). eligible(X,Y):-student(X),scholarship(Y, Z),year(X, Z).
Example4 (backtracking) • ?-scholarship(Y, Z) • Y is instantiated to vanier, and Z is instantiated to 3 • ?-year(albert,3) • false student(albert). student(edward). scholarship(vanier,3). scholarship(queenElizabeth, 4). year(albert, 2). year(edward, 4). eligible(X,Y):-student(X),scholarship(Y, Z),year(X, Z).
Example4 (backtracking) • ?-scholarship(Y, Z) • Y is instantiated to queenElizabeth, and Z is instantiated to 4 • ?-year(albert,4) • false student(albert). student(edward). scholarship(vanier,3). scholarship(queenElizabeth, 4). year(albert, 2). year(edward, 4). eligible(X,Y):-student(X),scholarship(Y, Z),year(X, Z).
Example4 (backtracking) • ?-student(X) • X is instantiated to edward. • ?-scholarship(Y, Z) student(albert). student(edward). scholarship(vanier,3). scholarship(queenElizabeth, 4). year(albert, 2). year(edward, 4). eligible(X,Y):-student(X),scholarship(Y, Z),year(X, Z).
Example4 (backtracking) • ?-scholarship(Y, Z) • Y is instantiated to vanier, and Z is instantiated to 3 • ?-year(edward,3) • false student(albert). student(edward). scholarship(vanier,3). scholarship(queenElizabeth, 4). year(albert, 2). year(edward, 4). eligible(X,Y):-student(X),scholarship(Y, Z),year(X, Z).
Example4 (backtracking) • ?-scholarship(Y, Z) • Y is instantiated to queenElizabeth, and Z is instantiated to 4 • ?-year(edward,4) • True student(albert). student(edward). scholarship(vanier,3). scholarship(queenElizabeth, 4). year(albert, 2). year(edward, 4). eligible(X,Y):-student(X),scholarship(Y, Z),year(X, Z).
Example4 (backtracking) • ?-eligible(X, Y) • X=edward, Y=queenElizabeth student(albert). student(edward). scholarship(vanier,3). scholarship(queenElizabeth, 4). year(albert, 2). year(edward, 4). eligible(X,Y):-student(X),scholarship(Y, Z),year(X, Z).
Q&A hossem<@>ucalgary<.>ca