coded in Prolog, one of the class project for VT