Questions tagged [failure-slice]

A failure-slice is a fragment of a Prolog program obtained by inserting one or more `false` goals somewhere in it. Failure-slices help to localize reasons for universal non-termination and instantiation errors of a pure monotonic Prolog program. They also help to give a lower bound for the number of inferences needed. It is a concrete program-slicing technique.

Filter by
Sorted by
Tagged with
1 vote
1 answer
244 views

How to get the list of all friends of a given person in prolog?

I'm a beginner in Prolog, and I just want to get the list of all friends of a given person. if i have predicates, like: friend(hend, khadija). friend(huda, mariam). friend(huda, aisha). friend(huda, ...
Mahmoud Sayed's user avatar
3 votes
6 answers
159 views

Even and odd cases for the pivot/2 predicate aren't running properly when placed together

My goal is to implement the pivot(Before,After) predicate that returns true when Before's first half is equal to After's second half and Before's second half is equal to After's first half. With an ...
Silver-Bullet03's user avatar
1 vote
3 answers
124 views

Non-termination of common reverse/2 implementation, and better solutions?

The following is a standard textbook definition of reverse(X,Y) which is true if the list Y is the reverse of the list X. The code is often used to introduce or illustrate the use of an accumulator. % ...
Prolog ByExample's user avatar
2 votes
3 answers
227 views

why prolog is entering infinite loop?

I made this code so basically, it detects if the array in the first argument is the reverse array in the second argument, it works well but when I want to have the reversed array for a giving array, ...
iiMouad's user avatar
  • 33
4 votes
1 answer
61 views

Prolog not halting when implementing reverse list

This is my implementation: popped_last_el([], [_]). popped_last_el([F | R], [F | X]) :- popped_last_el(R, X). last_el(X, [X]). last_el(X, [_ | L]) :- last_el(X, L). reverse_list([], []). ...
BonkClout's user avatar
1 vote
1 answer
107 views

Argument is not instantiated, need it to start at zero but also be able to change it

Whenever I run my code, I get an error that the arguments are not instantiated. ads(X,Z):- mod(X,2) =:= 0, Z is Z+X. ads(X,Z) :- mod(N,2) =\= 0,Z is Z. sum_of_nums(0,0,0). sum_of_nums(X,Y,Z) :- X=&...
Ferret-2742's user avatar
2 votes
1 answer
130 views

Predicate order change causes infinite loop

So I've got a predicate that works for uninstantiated variables nat_cset(N,Cs) that relates a natural number with the counting set Sn = {1,2,...,N}. nat_cset_(0,Acc,Acc). nat_cset_(N,Acc,Cs) :- N #>...
Luiz's user avatar
  • 99
5 votes
1 answer
300 views

How to implement list item deletion for all argument modes?

The following Prolog program defines a predicate deleted/3 for deleting all the occurrences of the item passed in first argument from the list passed in second argument and results in the list passed ...
Géry Ogam's user avatar
  • 6,602
2 votes
3 answers
368 views

How to implement the Fibonacci sequence in successor arithmetics for all argument modes?

The following Prolog program defines a predicate fib/2 for computing the Fibonacci number of an integer in successor arithmetics: fib(0, 0). fib(s(0), s(0)). fib(s(s(N)), F) :- fib(N, F1), fib(s(N)...
Géry Ogam's user avatar
  • 6,602
3 votes
2 answers
339 views

How to implement the factorial sequence in successor arithmetics for all argument modes?

The following Prolog program defines a predicate fact/2 for computing the factorial of an integer in successor arithmetics: fact(0, s(0)). fact(s(X), Y) :- fact(X, Z), prod(s(X), Z, Y). prod(0, _,...
Géry Ogam's user avatar
  • 6,602
2 votes
2 answers
429 views

How to fix this permutation sort?

The following Prolog program defines a predicate sorted/2 for sorting by permutation (permutation sort) in ascending order a list passed in first argument, which results in the list passed in second ...
Géry Ogam's user avatar
  • 6,602
2 votes
1 answer
130 views

How to fix this list reversal?

The following Prolog program defines a predicate rev/2 for reversing a list passed in first argument which results in the list passed in second argument: rev([], []). rev([XH|XT], Y) :- rev(XT, Z), ...
Géry Ogam's user avatar
  • 6,602
0 votes
1 answer
156 views

How to fix this recursive multiplication?

I am new to logic programming and Prolog. The following Prolog program defines a predicate mul/3 for multiplying the first argument to the second argument, which results in the third argument, based ...
Géry Ogam's user avatar
  • 6,602
4 votes
2 answers
338 views

Prolog - Infinite loop with very basic rule definition

I was trying to practice Prolog, as suggested by my TA, I am trying to create the rule append3(A,B,C,D) which means D is the result of the append of A,B and C. The definition of append(A,B,C) is given ...
Yan Zhuang's user avatar
3 votes
2 answers
190 views

Understanding prolog better

I'm trying to understand Prolog and how the resolution algorithm it uses. I have this example that I found: hates(1, 2). hates(2, 3). hates(3, 4). jealous(A, B) :- jealous(A, C), jealous(C,B). jealous(...
Gringo's user avatar
  • 91
3 votes
2 answers
89 views

Why does my list reversal only work correctly in one direction?

I have produced the following code. list_reverse([],[]). list_reverse([X],[X]). list_reverse(Ls,[R|Rs]) :- last_elem(Ls,R), without_last_elem(Ls,Next), list_reverse(Next,Rs). last_elem([...
Nicholas Hubbard's user avatar
1 vote
1 answer
282 views

Why does my prolog rule get stuck in infinite recursion

My code works for its intended purpose but always gets stuck in a loop at the end giving me an error saying "Stack limit exceeded." My code is below: byCar(auckland,hamilton). byCar(hamilton,...
Sam Pepper's user avatar
0 votes
2 answers
79 views

Prolog - why does the following code generate the solution X=root forever?

black(root). black(v1). black(v3). black(v4). edge(root,root). edge(v1,root). edge(v2,v1). edge(v3,v1). edge(v4,v3). edge(v5,v2). edge(v5,v4). edge(v6,v5). foo(root). foo(X) :- edge(X,Y),...
איתן לוי's user avatar
1 vote
3 answers
138 views

How to get full stop in this Prolog predicate?

I have this following code: factor_in_inches(Unit, Scale) :- recursiveScale(Unit, Scale, inch). %Part3 scale_factor(Unit1, Unit2, Factor) :- recursiveScale(Unit2, Factor, Unit1). %Part 1 - wrote ...
Brian Pattison's user avatar
2 votes
1 answer
176 views

Prolog binary addition

I need to implement binary addition in prolog, where binary numbers are represented as follows: 0: bot 1 : o(bot) 2 -> 10: z(o(bot)) 3 -> 11: o(o(bot)) 10 -> 1010: z(o(z(o(bot)))) I've ...
CodeHoarder's user avatar
0 votes
1 answer
94 views

Prolog stucks after giving the result

So I have this knowledge base and I am trying to achieve the goals in the code given as comments. %format of foo: foo(Tag,NumList) foo(a, [2, 4]). foo(b,[2, 8, 8, 6,2]). foo(c,[4, 8, 8, 8, 7]). ...
Mark R. Chandar's user avatar
2 votes
1 answer
121 views

Finding whether a number is a multiple of another

Looking at the code below: multiple(X,0). multiple(X,Y) :- lt(0,X), lt(0,Y), diff(Y,X,D), multiple(X,D). There happens to be something wrong. For your reference: lt/2 is whether the first argument ...
Huzo's user avatar
  • 1,652
2 votes
2 answers
749 views

Prolog Infinite loop on circular facts

Here is circular part of my facts (define relation between people): connection(harry, ron). connection(ron, harry). connection(hermione, harry). I want to find out if there is a connection, either ...
MaxSha's user avatar
  • 129
0 votes
1 answer
673 views

Why do i get a stack limit exceeded error when defining a predicate that convert the relation of two atoms?

I want to know why does the program goes in an infinite recursion in those cases: ?- love(kay, amanda). and ?- love(rob, amanda). And here is the code: love(amanda, kay). love(kay, geo). love(...
youssef elhefnawy's user avatar
1 vote
2 answers
129 views

I am trying to write a prolog program for fibbonaci series.This code is going in infinite loop .Can someone tell what's the error?

fib(0,0). fib(1,1). fib(A,Result):- fib(A-1,R), fib(A-2,P), Result is R+P.
mohit saraswat's user avatar
1 vote
2 answers
178 views

Prolog doesn't terminate after goal reordering

I'm currently working through the Learn Prolog Now examples and for the one exercise I have a KB that runs out of local stack if I just have a tiny change in one rule. this is the KB: byCar(auckland,...
Aurel Gruber's user avatar
2 votes
1 answer
123 views

How can I avoid nontermination with an integer base case?

I want to write a predicate that recurses until 0, but it consistently fails to terminate. I used failure-slicing to narrow it down to this: f(a, 0). f(b, 0). f(X, Y) :- false. When I load the file ...
Andrew's user avatar
  • 1,849
3 votes
2 answers
375 views

Prolog Implementation of Mergersort won't halt

I am a new Prolog developer and am trying to get merge sort working.The query mergesort([2,1],T). Will Produce T=[1,2]; T=[1,2]; T=[1,2]; ... So Although it seems to "correctly" sort the sequence ...
Drawkcab Esrever's user avatar
4 votes
3 answers
3k views

prolog list insert at any position

New to Prolog, trying to write a predicate to give all the options that an element could be inserted in to a list at any position. Ex: ins(a, [b,c], R). should give: R = [a,b,c] R = [b,a,c] R = [b,c,a]...
ABane's user avatar
  • 43
2 votes
1 answer
243 views

Beginner Prolog Stack Overflow

I am working on my very first Prolog assignment, and on recursive problems, I cannot seem to stop overflowing the stack. It's like an addiction; I don't know how to stop. Let me give you an example. ...
SupposedlySleeping's user avatar
0 votes
1 answer
88 views

Some good answers by the rule, but cannot get them all

These are my rules, where my problem lies: get_row([H|_],1,H):-!. get_row([_|T],I,X) :- I1 is I-1, get_row(T,I1,X). get_column([],_,[]). get_column([H|T], I, [R|X]):- get_row(H, I, R), ...
Falcon's user avatar
  • 29
4 votes
3 answers
1k views

All possible knight positions in n moves - infinite loop in prolog

I have a problem with backtracking in Prolog when calculation solution for all possible knight positions in n moves with knowing the exact path. My solution print some of the first results and then ...
Szymon Modelewski's user avatar
4 votes
2 answers
190 views

Prolog DCG: Match different symbols on a chain

I'm trying to match some sentences (e.g. 001 [0,0,1], (1+(1/0)) ['(',1,+,'(',1,/,0,')',')'], and so on. I've made myself following small DCG. g3 --> s3. s3 --> e3. e3 --> eAdd. e3 --> ...
user452306's user avatar
7 votes
3 answers
2k views

Prolog - infinite loop

I want to check if element is in the middle of list. I search middle element and next I check if is a member of list, but I get infinite loop. My predicates: remove_first([_,H1|T], [H1|T]). ...
user's user avatar
  • 4,500
2 votes
2 answers
495 views

Enumerate inorder in Prolog

I'm trying to "reverse" an inorder traversal by turning the inorder list back into BSTs. BSTs has the predicate form node(Value,Left,Right) or leaf, so the empty tree is just leaf, a tree with one ...
bli00's user avatar
  • 2,311
3 votes
1 answer
167 views

Understanding recursivity in Prolog

I have this example: descend(X,Y) :- child(X,Y). descend(X,Y) :- child(X,Z), descend(Z,Y). child(anne,bridget). child(bridget,caroline). child(caroline,donna). It works great and I ...
Gonzalo Solera's user avatar
4 votes
3 answers
806 views

Prolog Out of stack error

I'm working on Problem 26 from 99 Prolog Problems: P26 (**) Generate the combinations of K distinct objects chosen from the N elements of a list Example: ?- combination(3,[a,b,c,d,e,f],L). ...
user avatar
1 vote
1 answer
984 views

Prolog: check transitivity for simple facts

My intention was to implement a simple example (just for myself) of transitivity in Prolog. These are my facts: trust_direct(p1, p2). trust_direct(p1, p3). trust_direct(p2, p4). trust_direct(p2, p5)....
daniel451's user avatar
  • 10.7k
4 votes
1 answer
856 views

Prolog factorial predicate

I have a factorial predicatefact(N,F), where either N or F or both are bounded to a number. For example I can have fact(3,F) or fact(N,6). Here is my predicate, which works but I don't really ...
zaig's user avatar
  • 391
1 vote
1 answer
162 views

STRIPS Planner loops indefinitely

I defined in Prolog a STRIPS Planner to solve logic problems. After a few tryouts with other simpler problems I set out to see if it could solve a more complex one. I gave him a STRIPS definition of ...
Daniel Casadinho's user avatar
0 votes
2 answers
174 views

Prolog raises out of local stack for no good reason

I'm trying to implement Levenshtein distance in Prolog. The implementation is pretty straightforward: levenshtein(W1, W2, D) :- atom_length(W1, L1), atom_length(W2, L2), lev(W1, W2, L1, ...
s1ddok's user avatar
  • 4,615
1 vote
1 answer
2k views

Prolog: Out of local stack error

Consider the following code (taken from Introduction to Prolog by RP Suri): /* A set of father-child pairs declared */ father("Motilal", "Jawaharlal"). father("Motilal", "Vijayalakshmi"). father("...
user avatar
1 vote
1 answer
3k views

Removing duplicates in prolog

I'm fairly new to prolog and I'm currently reading through a book which is giving me practice examples to code. It has tasked me with removing duplicates. Note: I have read other stackoverflows, and ...
user3667111's user avatar
0 votes
1 answer
2k views

Magic Square NxN

I'm new to Prolog and I'm trying to write fully working magic square program, but to say the truth I don't really know how to do, I have started but I feel that I'm doing it wrong. I'm sharing my code ...
EasyCode's user avatar
3 votes
1 answer
309 views

Prolog: Trying to solve a puzzle! returned false

The code at the end on my post is supposed to answer the following puzzle: Brown, Clark, Jones and Smith are 4 substantial citizens who serve their community as achitect, banker, doctor and ...
AIM's user avatar
  • 137
1 vote
1 answer
93 views

Sorting list with only two elements in prolog

firstly look at following example: ?- fp(X,[b,a,b]). false Ok, because second argument must be two-elements sorted list. (In my program I assume that a<b) ?-fp(X,[a,b,b]). X = [a, b, b] ; X ...
user avatar
5 votes
2 answers
290 views

List of integers and infinite loop in Prolog CLPFD

Suppose I want to represent integers like so: integer:Sign:[FirstDigit,SecondDigit,...]. For instance, 42 would be represented as integer:positive:[4,2]. I need a predicate that generates the value ...
Fatalize's user avatar
  • 3,523
2 votes
1 answer
337 views

Debugging a command-line program

If I have a program that I use as a command-line tool, what are my options for debugging? For the sake of the example, let's say that the program looks like this. Listing of do_stuff.pl: main :- ...
user avatar
1 vote
1 answer
288 views

How to avoid Out of global stack ERROR when looking for multiple answers?

After loading the following program using SWI-Prolog and entering queries such as cells([o,x,o,x,o], A). or cells(A, [o,x,o,x,o]). the first result seems to always be correct, but after submitting ...
luckysori's user avatar
2 votes
2 answers
176 views

Prolog clause terminates individually, but not together

So ?- canCall(mary, Person). works and terminates and ?- canFind(mary, Person). also works and terminates. But somehow ?- canCall(mary, Person), canFind(mary, Person). does not terminate. What ...
absolutelydevastated's user avatar