0

Hi if i have the next knowladge base

natural(0).
natural(suc(X)):-natural(X).
sum(0,N,N).
sum(suc(N),suc(N),R).
mult(N,1,N).
mult(number,times,result):-mult(number,times-1,partial), sum(partial,number,result).

and i ask the query ? mult(5,3,15).

how does prolog runs this??? first i instanciate number to 5, times to 3, and 5 to result. And after it unificates with the head of the query, how does works with the body of the clause, specially what value would partial take

user3105533
  • 321
  • 3
  • 11
  • 3
    `?- trace, mult(5,3,15).` – Paulo Moura Sep 24 '14 at 21:48
  • 2
    You should decide if you are using `0, suc(0), suc(suc(0)), ...`, or `0, 1, 2, 3, ...`. Mixing them is not going to solve your problems. You should also figure out why using `times-1` as an argument does not work, either. Go ahead and google "prolog peano". Just do it, really. –  Sep 25 '14 at 05:37
  • @Boris is right - the whole point of defining natural numbers as a successors is (apart from logic) the pattern matching, so you should definitely use that as much as possible. For example use `suc(0)` rather than `1` as well as using `suc(times)` in `mult` head in order to use only `times` in "recursive call" instead of "times-1". – petrbel Sep 29 '14 at 18:34
  • 1
    maybe you find useful my own implementation (lines 22 and 31) - I hope you deal with foreign language properly - https://github.com/petrbel/NPRG005-non-procedural-programming/blob/master/task2/aritm.prolog – petrbel Sep 29 '14 at 18:36
  • @petrbel Yes, and my point here is that even the shallowest research will tell OP everything one needs to get this right. The only reason I did not vote to close this question and gave pointers instead is that there seems to be some honest attempt at own solution, however uninformed and misguided. –  Sep 30 '14 at 05:30
  • Possible duplicate of [Prolog order of unification](http://stackoverflow.com/questions/27334691/prolog-order-of-unification) –  Nov 11 '16 at 22:13

0 Answers0