Nonholonomic mechanics and optimal control