Evolution of alternation of haploid and diploid phases in life cycles