1 / 28

Programming Paradigms

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

duncan
Download Presentation

Programming Paradigms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming Paradigms CPSC 449 Week 10 Prepared by : Mona Hosseinkhani, Winter 2014 Department of Computer Science, University of Calgary

  2. SWI-Prolog • http://www.swi-prolog.org/ • http://www.gprolog.org/

  3. MacOSX • Download the installation file and run it • Search for swipl • In Mac OS, you can find it in • /opt/local/bin/ • Run swipl

  4. 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.

  5. 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 • /* */

  6. 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)

  7. 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.

  8. 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).

  9. 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).

  10. 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).

  11. 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).

  12. 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).

  13. 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).

  14. 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).

  15. 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).

  16. 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).

  17. 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).

  18. 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).

  19. 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).

  20. 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.

  21. 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).

  22. 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).

  23. 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).

  24. 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).

  25. 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).

  26. 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).

  27. 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).

  28. Q&A hossem<@>ucalgary<.>ca

More Related