segment rule

This is a discussion on segment rule within the PROLOG forums in Programming Languages category; 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] ...

Go Back   Application Development Forum > Programming Languages > PROLOG

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 08-12-2008, 12:26 PM
Antonio Maschio
Guest
 
Default segment rule

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
Reply With Quote
  #2  
Old 08-12-2008, 03:18 PM
bart demoen
Guest
 
Default Re: segment rule

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
Reply With Quote
  #3  
Old 08-13-2008, 01:30 PM
Antonio Maschio
Guest
 
Default Re: segment rule

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
Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 02:57 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.