failure-slice

prolog dcg restriction

不想你离开。 提交于 2021-02-07 13:23:36
问题 I would like to use DCGs as a generator. As of now, the syntax is s-->a,b. a-->[]. a-->a,c. c-->[t1]. c-->[t2]. b-->[t3]. b-->[t4]. I would like to generate all s where the length of a is < someNumber . Using ?- phrase(a,X),length(X,Y),Y<4. i can get all a with less than 4 items. However, when all combinations are exhausted, the system (SWI-Prolog 6.2.5) seems to stall. Sometimes ago, a similar question was asked here. However, being new to Prolog i am not able to make it work with the

Out Of Global Stack Error in Prolog

时光怂恿深爱的人放手 提交于 2020-01-25 04:22:07
问题 I am trying to run the following program in Prolog. mama_mia1(A,M,LI,HI,LO,HO,AA) :- p1(A,M,LI,HI,LO,HO,PROGS), reverse(PROGS,PROG), atom_chars(AA,PROG), !. p1(_,_,LO,LO,LO,_,[]). p1(_,_,HO,HO,_,HO,[]). p1(_,_,LO,HO,LO,HO,[]). p1(_,_,X,LO,LO,HO,[]) :- X>LO,X<HO. p1(_,_,X,HO,LO,HO,[]) :- X>LO,X<HO. p1(_,_,LO,Y,LO,HO,[]) :- Y>LO,Y<HO. p1(_,_,HO,Y,LO,HO,[]) :- Y>LO,Y<HO. p1(_,_,X,Y,LO,HO,[]) :- X>LO,X<HO,Y>LO,Y<HO. p1(A,M,X,Y,LO,HO,PROG) :- ( (X1 is X+A, H1 is HO+1, X1<H1, Y1 is Y+A, Y1<H1 ) ->

Out Of Global Stack Error in Prolog

徘徊边缘 提交于 2020-01-25 04:21:05
问题 I am trying to run the following program in Prolog. mama_mia1(A,M,LI,HI,LO,HO,AA) :- p1(A,M,LI,HI,LO,HO,PROGS), reverse(PROGS,PROG), atom_chars(AA,PROG), !. p1(_,_,LO,LO,LO,_,[]). p1(_,_,HO,HO,_,HO,[]). p1(_,_,LO,HO,LO,HO,[]). p1(_,_,X,LO,LO,HO,[]) :- X>LO,X<HO. p1(_,_,X,HO,LO,HO,[]) :- X>LO,X<HO. p1(_,_,LO,Y,LO,HO,[]) :- Y>LO,Y<HO. p1(_,_,HO,Y,LO,HO,[]) :- Y>LO,Y<HO. p1(_,_,X,Y,LO,HO,[]) :- X>LO,X<HO,Y>LO,Y<HO. p1(A,M,X,Y,LO,HO,PROG) :- ( (X1 is X+A, H1 is HO+1, X1<H1, Y1 is Y+A, Y1<H1 ) ->

Reversible tree length relation

点点圈 提交于 2020-01-11 05:25:14
问题 I'm trying to write reversible relations in "pure" Prolog (no is , cut, or similar stuff. Yes it's homework), and I must admit I don't have a clue how. I don't see any process to create such a thing. We are given "unpure" but reversible arithmetic relations (add,mult,equal,less,...) which we must use to create those relations. Right now I'm trying to understand how to create reversible functions by creating the relation tree(List,Tree) which is true if List is the list of the leaves of the

Why does Prolog crash in this simple example?

旧城冷巷雨未停 提交于 2020-01-03 15:54:54
问题 likes(tom,jerry). likes(mary,john). likes(mary,mary). likes(tom,mouse). likes(jerry,jerry). likes(jerry,cheese). likes(mary,fruit). likes(john,book). likes(mary,book). likes(tom,john). likes(john,X):-likes(X,john), X\=john. Hi there, above is a very simple prolog file, with some facts and only one rule: John likes anyone who likes him. But after loading this file and ask Prolog the following query: likes(john,X). The program crashes. The reason is somehow prolog gets stuck at likes(john,john)

How to prevent duplicates in generated sequences by using dif/2?

血红的双手。 提交于 2019-12-24 00:15:55
问题 This question came up while answering another question on StackOverflow on (generalizing a bit) generating all sequences formed out of a finite set of elements with no duplicate occurrences. As Boris rightly indicated in the comments, there are many existing solutions to this problem. However, I am interested in a solution that does not use an accumulator (i.e., a list of already picked elements against which a newly selected element is to be compared) but that uses dif/2 statements instead.

Find adjacent members

断了今生、忘了曾经 提交于 2019-12-23 20:32:58
问题 I have to find if two members of a list are adjacent. The restriction is to use the append/3 predicate. So far I've done the below, it works if it's true, otherwise I get no answer, it's like it runs eternally. adjacent(X,Y,L):- append(L1,[X,Y],T1),append(T1,T2,L). 回答1: To see that your program will loop, it suffices to consider the following failure-slice: adjacent(X,Y,L):- append(L1,[X,Y],T1), false , append(T1,T2,L) . If this program will loop, then the original program will loop too. It