/* Filename: car2.pro * * Programmer: Br. David Carlson * * Date: March 10, 1991 * * Revised: December 11, 1999 to run under yap. * * Description: This program is intended to advise as to the proper * action(s) for a car to take in various circumstances, which are * entered as Prolog facts. Change the initial facts as desired and * then run the program. This program is based on an example by * Neil C. Rowe in his book ARTIFICIAL INTELLIGENCE THROUGH PROLOG. * This version of the program prints the list of possible recommended * actions to take. Enter the goal: report */ /* Initial facts section -- change these as needed */ safe_stop(possible). light(yellow, steady). light(green_arrow, left). /* Legal facts to use are of the form: * * light(Type, Direction) Indicates current traffic light(s) * Type can be: red, yellow, green, red_arrow, yellow_arrow, * or green_arrow * Direction can be: left or right (for arrow lights), or * steady or flashing (for other lights) * light(none, none) can also be used to indicate no light in sight * * safe_stop(Qualifier) Indicates if car could now stop safely * Qualifier can be: possible or impossible */ /* advice for arrow lights */ advise(action(stop)) :- light(yellow_arrow, _), safe_stop(possible). advise(action(yield_then_turn, Direction)) :- light(yellow_arrow, Direction), safe_stop(impossible). advise(action(yield_then_turn, Direction)) :- light(green_arrow, Direction). advise(action(stop)) :- light(red_arrow, _). /* advice for regular lights */ advise(action(stop)) :- light(red, steady). advise(action(stop_then_proceed)) :- light(red, flashing). advise(action(stop)) :- light(yellow, steady), safe_stop(possible). advise(action(yield_then_proceed)) :- light(yellow, steady), safe_stop(impossible). advise(action(yield_then_proceed)) :- light(green, steady). advise(action(slow_down)) :- light(yellow, flashing). /* default advice */ advise(action(continue)) :- not(caution_needed). caution_needed :- advise(action(stop)). caution_needed :- advise(action(stop_then_proceed)). caution_needed :- advise(action(yield_then_proceed)). caution_needed :- advise(action(yield_then_turn, _)). report :- advise(X), write('Recommended action:'), nl, write(X), nl, fail. report.