3

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?

nosefouratyou
  • 331
  • 1
  • 8
  • When you "search" a tree you can stop searching as soon as you find what you're looking for. So that means there is a difference between a "depth-first-search" and a "breadth-first-search". But Prolog isn't doing a search - it's doing a computation. There is no difference to a "depth-first-computation" and a "breadth-first-computation" as you have to do a full computation to get a result. – Enigmativity Jun 05 '17 at 04:06
  • Are you really talking about depth-first in *unification*? Is this what you mean? – false Jun 05 '17 at 07:34
  • Could you give an example of what you are describing by "*unification* depth-first-search"? Or do you just mean depth first search in general, as in searching a tree structure? – lurker Jun 05 '17 at 11:58
  • In typical BFS task you can process current node without processing all its child nodes. In Prolog to process node you should process all its child, like, as you cannot find result of `x + 5` without finding `x`. – Stanislav Ivanov Jun 09 '17 at 10:51

1 Answers1

3

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.

Daniel Lyons
  • 22,421
  • 2
  • 50
  • 77