A martingale approach to a dynamic program with general state and action spaces is taken. Several necessary and sufficient conditions are given for a policy to be optimal. The results comprehend and modify different criteria of optimality given for dynamic programming problems. Finally, two applications are stated.