I think you want this:
If a nonland card is played or would come into play, if a card with the same name is in any graveyard or in play, remove that card from the game.
(Spells removed in this manner don't resolve.)That version is cleaner, requires vastly less work, and can be abused with cards that have an effect without being played - such as the Forecast cards from Dissension.
Alternately you could do this:
Enchantment
Whenever a player plays a spell, if a card of the same name is in any graveyard, counter that spell.
When ~this~ comes into play, you may search your library for a card named Leyline of Singularity and put it into play.
Which basically does what you want it to.
