Using the pseudopotential plane-wave method within the density functional theory as implemented in the Vienna ab-initio simulation package, we investigate the crystal parameters, electronic structures and optical properties of rare earth Ce and Pr doped GaN. The local spin density approximation with Hubbard-U corrections method is used to treat the correlation effect of strongly localized rare-earth 4f electron states. The results show that the crystal parameters increase after doping Ce and Pr in GaN. The Ce impurity introduces defect level in the gap while for Pr the level lies near the valence band maximum, and the defect levels are contributed by Ce and Pr 4f electron states. In addition, the dopings of Ce and Pr give rise to spin polarization and magnetic-order. For GaN:Ce, there appear two new peaks, one is in the low energy region of imaginary dielectric function and the other is in the low energy region of absorption coefficient. These new peaks are probably related to the defect level in the gap. For GaN:Pr, red shifts of the dielectric peak and absorption edge duo to bandgap narrowing are observed.