I just started learning about prolog and I was wondering why it's dfs instead of bfs and why there isn't an easy way to change it.
Does ISO prolog mandate it?
I just started learning about prolog and I was wondering why it's dfs instead of bfs and why there isn't an easy way to change it.
Does ISO prolog mandate it?
First of all, it is fairly easy to change. Most Prolog texts explain how both how to write a predicate that performs a BFS and how to create a meta-interpreter that does it with arbitrary terms. The truth is that students who get a taste of Prolog at the university get through (basically) the first week or two of using Prolog. To do this isn't exactly a basic Prolog task, but it isn't an advanced Prolog technique either. If you spent two months on Prolog it would not be an intimidating thing to do. That sounds like a lot of Prolog, but compared to (say) Java it really isn't much. For some reason we expect to get to the finish line with Prolog much faster than we do for systems that are actually much less interesting.
I believe the search strategy mandated by ISO is called SLD Resolution, and depth-first search arises from this resolution mechanism. I have not read the ISO standard, so perhaps someone better informed than me will comment. I think it would be difficult to manage Prolog standardization if the resolution method (and thus, depth-first or breadth-first) were not mandatory, since computations that succeed one way may enter an infinite loop the other way. A language standard that does not specify the behavior of normal-ish programs would be a rather poor standard. Although, there's no reason there couldn't be a built-in for specifying an alternate search strategy.
I don't know the reason for mandating DFS in particular. Having used Prolog for a while, the idea of not-DFS seems obviously inefficient to me. For instance, if I add some code to handle an edge case, I'm going to pay for it every time with BFS, but only in cases where it is necessary with DFS. I feel like DFS is going to be more memory efficient; I'm not going to have to keep track of a bunch of possibly-useless code paths, for instance. I feel like DFS is probably easier to control, because I can easily prune the search tree. But these are just feelings; maybe my sense of what is natural is completely a result of what I've used. The lack of existence of a Prolog competitor that is BFS-based is a kind of suggestion that it may not be a great idea though. On the other hand, what was inefficient in 1980 still informs Prolog implementations today, even though things are very different now.