علمتني الثلوج أن أكون كبياض الثلج ال يحمل قلبي غير الحب والعطف والرحمة ال أسيئ الظن باألخرين وال أحكم عليهم من مظهرهم فإذا لم تعاشرهم فأنت تجهلهم ! الذكاء االصطناعي Artificial Intelligence ( AI) comp323 Lab (2) Knowledge Base 3 happy(vincent). listensToMusic(butch). playsAirGuitar(vincent):listensToMusic(vincent), happy(vincent). playsAirGuitar(butch):happy(butch). playsAirGuitar(butch):listensToMusic(butch). how many clauses in the KB3 ? What does this rule mean? if we posed the query ?- playsAirGuitar(vincent). ?- playsAirGuitar(butch). playsAirGuitar(butch):happy(butch). playsAirGuitar(butch):listensToMusic(butch). • We could replace the pair of rules given above by the single rule playsAirGuitar(butch):happy(butch); listensToMusic(butch). Knowledge Base 4 woman(mia). woman(jody). woman(yolanda). loves(vincent,mia). loves(marcellus,mia). loves(pumpkin,honey_bunny). loves(honey_bunny,pumpkin). • for the first time we’re going to make use of variables ?- woman(X). • Let’s try a more complicated query, namely ?- loves(marcellus,X),woman(X). Knowledge Base 5 loves(vincent,mia). loves(marcellus,mia). loves(pumpkin,honey_bunny). loves(honey_bunny,pumpkin). jealous(X,Y) :- loves(X,Z),loves(Y,Z). Suppose we pose the query: ?- jealous(marcellus,W). Prolog Syntax • what are facts, rules, and queries built out of? • The answer is terms, and there are four kinds of terms in Prolog: atoms, numbers, variables, and complex terms (or structures). • Atoms and numbers are lumped together under the heading constants, • and constants and variables together make up the simple terms of Prolog. Atoms 1. A string of characters made up of upper-case letters, lower-case letters, digits, and the underscore character, that begins with a lowercase letter. For example: butch, big_kahuna_burger, and m_monroe2. Atoms 2. An arbitrary sequence of character enclosed in single quotes. For example ’Vincent’, ’The Gimp’, ’Five_Dollar_Shake’, ’&^%&#@$ &*’, and ’ ’. The character between the single quotes is called the atom name. Atoms 3. A string of special characters. For example: @= and ====> and ; and :- are all atoms. As we have seen, some of these atoms, such as ; and :have a pre-defined meaning Numbers • integers (that is: ... -2, -1, 0, 1, 2, 3, ...) are useful for such tasks as counting the elements of a list Variables • A variable is a string of upper-case letters, lower-case letters, digits and underscore characters that starts either with an uppercase letter or with underscore. • For example, X, Y, Variable, _tag, X_526, and List, List24, _head, Tail, _input and Output are all Prolog variables. Complex terms • Complex terms are build out of a functor followed by a sequence of arguments. • The arguments are put in ordinary brackets, separated by commas, and placed after the functor. • The functor must be an atom. That is, variables cannot be used as functors. • On the other hand, arguments can be any kind of term. • For example, playsAirGuitar(jody) • its functor is playsAirGuitar • and its argument is jody • The number of arguments that a complex term has is called its arity arity • Arity is important to Prolog. Prolog would be quite happy for us to define two predicates with the same functor but with a different number of arguments • it is usual to use a suffix / followed by a number to indicate the predicate’s arity • To return to KB2 • listensToMusic/1 • happy/1 • playsAirGuitar/1 Practical Session 1 ?- listing. command is to display the contents of the current knowledge base ?- listing(playsAirGuitar). References • Prolog note H.W ( group ) • Exercise 1.1 – 1,2,3,6,9 • Exercise 1.2 – 1,2,3 • Exercise 1.3 • Exercise 1.4 – 2,3 • Exercise 1.5 – 2,5,7