0

I'm trying to write a definite clause grammar that outputs the partitions of a number. For example ?- w(3,L,[]) should output :

[1,1,1]
[2,1]
[1,2]
[3]

My code looks as follows:

w(PosNumber) --> {partition(PosNumber,L)},L.

partition(0,[]).
partition(PosNumber,[X|List]):-
   between(1,PosNumber,X),
   Y is (PosNumber-X),
   partition(Y,List).

My partition function seems to work fine but I'm not sure how to correctly instantiate 'w'. Sorry if this is really basic, I'm very new to prolog.

azrdev
  • 225
  • 1
  • 13
J.M
  • 15
  • 3
  • It seems to work fine for me. Please try again to explain what is the problem. – Tomas By Dec 19 '17 at 16:56
  • If i put in the query `?- w(3,L,[])` I get the error `Arguments are not sufficiently instantiated Reachable from: swish_trace:swish_call(w(3,A,[])) '$swish wrapper'(w(3,A,[]),B)` – J.M Dec 19 '17 at 17:00
  • It works fine in SWI Prolog. Are you using a web site or something? – Tomas By Dec 19 '17 at 17:05
  • Maybe you could move the error message to the question. – Tomas By Dec 19 '17 at 17:07
  • SWISH is SWI based, but is a subset of SWI Prolog capability. – lurker Dec 19 '17 at 17:08
  • ah yes im using swish.swi-prolog.org! just tried it in terminal and it worked fine! Didn't realise that could be a problem. Thanks for the help. – J.M Dec 19 '17 at 17:09
  • 2
    Why do you require a DCG? Seems perhaps more suited for CLP(FD). What you have isn't what I'd call a DCG implementation since you are immediately breaking into standard clauses right from the initial call. You wouldn't *instantiate* `w`. You'd call it: `phrase(w(N), Sequence)`. – lurker Dec 19 '17 at 17:10

0 Answers0