Page 73 - Discrete Mathematics and Its Applications
P. 73

52  1 / The Foundations: Logic and Proofs


                                                enrolled(s, c) to represent that professor p is the instructor of course c and that student s
                                                is enrolled in course c, respectively. For example, the Prolog facts in such a program might
                                                include:

                                                    instructor(chan,math273)
                                                    instructor(patel,ee222)
                                                    instructor(grossman,cs301)
                                                    enrolled(kevin,math273)
                                                    enrolled(juana,ee222)
                                                    enrolled(juana,cs301)
                                                    enrolled(kiko,math273)
                                                    enrolled(kiko,cs301)
                                                (Lowercase letters have been used for entries because Prolog considers names beginning with
                                                an uppercase letter to be variables.)
                                                    A new predicate teaches(p, s), representing that professor p teaches student s, can be
                                                defined using the Prolog rule

                                                    teaches(P,S) :- instructor(P,C), enrolled(S,C)

                                                which means that teaches(p, s) is true if there exists a class c such that professor p is the
                                                instructor of class c and student s is enrolled in class c. (Note that a comma is used to represent
                                                a conjunction of predicates in Prolog. Similarly, a semicolon is used to represent a disjunction
                                                of predicates.)
                                                    Prolog answers queries using the facts and rules it is given. For example, using the facts
                                                and rules listed, the query


                                                    ?enrolled(kevin,math273)

                                                produces the response

                                                    yes

                                                because the fact enrolled(kevin, math273) was provided as input. The query

                                                    ?enrolled(X,math273)

                                                produces the response

                                                    kevin
                                                    kiko
                                                To produce this response, Prolog determines all possible values of X for which
                                                enrolled(X, math273) has been included as a Prolog fact. Similarly, to find all the professors
                                                who are instructors in classes being taken by Juana, we use the query


                                                    ?teaches(X,juana)

                                                This query returns

                                                    patel                                                                      ▲
                                                    grossman
   68   69   70   71   72   73   74   75   76   77   78