Coelho's The Alchemist relies on quite a number of literary devices to impart the story's themes and messages to the reader. For instance, there are many Biblical allusions that reflect similar situations that Santiago has to confront.
Coelho also uses irony and presents the robber as something of a foil. A foil is defined as...
…[a] character that serves by contrast to highlight or emphasize opposing traits in another character.
Although the robber appears only for a brief moment of time, his attitudes and actions serve to provide Santiago with a faint reflection of himself—a young man entrusted with dreams. The robber also shows the reader how very different these two young men are in terms of how they interact with the world.
The alchemist hints at this when he says:
No matter what he does, every person on earth plays a central role in the history of the world. And normally he doesn't know it.
This is certainly the case when Santiago is attacked near the Pyramids. He believes he is supposed to dig at a particular spot to find his treasure, and he must dig but not for his treasure: it is here that one member of a group of robbers will tell Santiago something he needs to hear (as the robber plays his central role). This part of Santiago's journey is necessary in realizing his Personal Legend. After the men have beaten the boy, Santiago finally tells them about his dream, and admits he is looking for treasure.
The leader decides that Santiago has nothing more of any value (and that he's is stupid), and decides to leave. But before they go, the leader's words show us first that he is not following his Personal Legend—for he has ignored his dreams. Second, his words tell Santiago that the boy will be rewarded because he has followed his dreams:
Two years ago, right here on this spot, I had a recurrent dream, too. I dreamed that I should travel to the fields of Spain and look for a ruined church where shepherds and their sheep slept.
The leader of the thieves continues to describe what he saw in his dreams—including the tree: the very same place Santiago rested with his sheep back home. The thief's dreams told him that at the roots of the tree, the thief would find treasure.
But I'm not so stupid as to cross an entire desert just because of a recurrent dream.
Here, then, Coelho also uses irony. Irony is the difference between what one believes will happen and what really happens. This is an example of situational irony, when...
...accidental events occur that seem oddly appropriate...
For this is certainly not what the reader or Santiago expected. And Santiago is not the stupid one—obviously the leader of the thieves is.
In situational irony, both the character in the story and the reader realize the importance of what has happened. Santiago knows immediately that he must return to his homeland, to the grove with the church ruins. At the base of the sycamore tree, if he digs, he will find treasure. And because Santiago has listened to the Language of the World and has never stopped pursuing his Personal Legend, he comes home to find what the universe contrived he would discover...what the universe hoped he would find before he ever left Spain.
"If he had not believed in the significance of recurring dreams," he would never have started on his journey, would never have the people who taught and directed him; he would not have found Fatima; and, he would not have found the treasure...or seen the Pyramids.