<< Previous exercise (4.57) | Index | Next exercise (4.59) >>


 (assert! (rule (bigshot ?person ?division) 
                (and (job ?person (?division . ?rest)) 
                     (or (not (supervisor ?person ?boss)) 
                         (and (supervisor ?person ?boss) 
                              (not (job ?boss (?division . ?r)))))))) 
 ;;; Query output: 
 (bigshot (Warbucks Oliver) administration) 
 (bigshot (Scrooge Eben) accounting) 
 (bigshot (Bitdiddle Ben) computer) 

The bigshot rule should include a recursive call back to itself, as even if a person's direct supervisor isn't in their department, it's possible for their supervisor's supervisor (or so forth) to be in their department, thus disqualifying the original individual from being a bigshot.


 (rule (bigshot ?person ?division) 
       (and (job ?person (?division . ?rest)) 
            (or (not (supervisor ?person ?boss)) 
                (and (supervisor ?person ?boss) 
                     (not (job ?boss (?division . ?r))) 
                     (not (bigshot ?boss ?division)))))) 

Great catch! I certainly didn't think to account for this situation. Methinks this could happen with temporary assignments to different departments for a project!

The rule still appears to fail though. Consider that we match on ?boss that isn't in the ?division. The bigshot rule will always fail in that case for ?boss because ?boss isn't in the department (despite ?boss having ?bosses-boss in ?department. We never even checked because it 'short-circuits'). It would appear that it is necessary to have a separate rule that will check if 'any supervisor is in the division' that won't fail if the ?person isn't in the division.

 (rule (any-supervisor-in-division ?person ?division) 
       ;; recursively find out if a supervisor is in the division 
       ;; this is necessary because the big-shot rule 'short circuits' if 
       ;; ?person is not in ?division, but we are still asking the question 
       ;; even if the current person is not in ?division 
       (and (supervisor ?person ?supervisor) 
            (or (job ?supervisor (?division . ?type)) 
                (any-supervisor-in-division ?supervisor ?division)))) 
 (rule (big-shot ?person ?division) 
       (and (job ?person (?division . ?type)) 
            (not (any-supervisor-in-division ?person ?division)))) 


(rule (bigshot ?bshot ?division)
      (and (job ?bshot (?division . ?bshot-rest))    
           (not (and (supervisor ?bshot ?boss)          
                     (job ?boss (?division . ?boss-rest))))))

;;; Query results:
(bigshot (Scrooge Eben) accounting)
(bigshot (Warbucks Oliver) administration)
(bigshot (Bitdiddle Ben) computer)