A generalization of the Farkas lemma and applications to convex programming