| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
| |||
| |||
| Hi, I was trying to compose the following rule in gprolog: % segment(L1,L2) is true if L1 is a segment list of the list L2 % e.g. segment([2,3],[1,2,3,4]) is true segment(L1,L2) :- append(_P,L1,Prefix), append(Prefix,_S,L2). This rule works well to test the truth, and using append/3 is useful even to get L1 or L2 (L2 is meaningless, though). The problem is that, in gprolog, the query: | ?- segment(X,[1,2,3,4]). returns the following: X = [] ? a X = [1] X = [1,2] X = [1,2,3] X = [1,2,3,4] X = [] X = [2] X = [2,3] X = [2,3,4] X = [] X = [3] X = [3,4] X = [] X = [4] Bus error Why the end with a bus error? I tried to add an ending rule like segment([],_). or segment([],[]). but of course they didn't work, since all the job is done by append. Any idea? Maybe the problem lies in the repeated answer X = []? -- Antonio |
|
#2
| |||
| |||
| On Tue, 12 Aug 2008 18:26:17 +0200, Antonio Maschio wrote: > Hi, > > I was trying to compose the following rule in gprolog: > > % segment(L1,L2) is true if L1 is a segment list of the list L2 % e.g. > segment([2,3],[1,2,3,4]) is true segment(L1,L2) :- append(_P,L1,Prefix), > append(Prefix,_S,L2). > > This rule works well to test the truth, and using append/3 is useful > even to get L1 or L2 (L2 is meaningless, though). > > The problem is that, in gprolog, the query: > > | ?- segment(X,[1,2,3,4]). > [...] > Bus error > > Why the end with a bus error? I tried to add an ending rule like > segment([],_). > > or > > segment([],[]). > > but of course they didn't work, since all the job is done by append. > > Any idea? Maybe the problem lies in the repeated answer X = []? > > -- Antonio The first goal in the body of segment/2 calls append/3 with three free arguments - most often a bad idea. ALL Prolog systems will give up at some point - most in expanding or garbage collecting the heap. gprolog does not have a heap gc or expander (AFAIK) so it should give up telling you that it has not enough heap - the Bus error is clearly a bug but indicates the same problem with your program. Try switching the two goals in the body of segment and see what happens ! Cheers Bart Demoen |
|
#3
| |||
| |||
| bart demoen ha scritto: > On Tue, 12 Aug 2008 18:26:17 +0200, Antonio Maschio wrote: > >> Hi, >> >> I was trying to compose the following rule in gprolog: >> >> % segment(L1,L2) is true if L1 is a segment list of the list L2 % e.g. >> segment([2,3],[1,2,3,4]) is true segment(L1,L2) :- append(_P,L1,Prefix), >> append(Prefix,_S,L2). >> >> This rule works well to test the truth, and using append/3 is useful >> even to get L1 or L2 (L2 is meaningless, though). >> >> The problem is that, in gprolog, the query: >> >> | ?- segment(X,[1,2,3,4]). >> > [...] >> Bus error >> >> Why the end with a bus error? I tried to add an ending rule like >> segment([],_). >> >> or >> >> segment([],[]). >> >> but of course they didn't work, since all the job is done by append. >> >> Any idea? Maybe the problem lies in the repeated answer X = []? >> >> -- Antonio > > > The first goal in the body of segment/2 calls append/3 with three free > arguments - most often a bad idea. ALL Prolog systems will give up at > some point - most in expanding or garbage collecting the heap. > gprolog does not have a heap gc or expander (AFAIK) so it should give up > telling you that it has not enough heap - the Bus error is clearly a bug > but indicates the same problem with your program. > > Try switching the two goals in the body of segment and see what happens ! > > Cheers > > Bart Demoen Right, inverting the two append goals the BUS error won't come again. Thanks, Bart. Now the rule is segment(L1,L2) :- append(Prefix,_S,L2), append(_P,L1,Prefix). and it works well with the query ?- segment(X,[1,2,3,4]). under gprolog. Thanks again. -- Antonio |
![]() |
| Thread Tools | |
| Display Modes | |
In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.