Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explanation for why append/3 produces infinite number of solutions in these cases

I'm having trouble understanding this question about Prolog. The question is as follows:

Select all of the following goals that have an infinite number of solutions.

And here are the possible answers:

append([a,b,c,d], Y, Z)
append(X, Y, X)
append(X, [a,b,c,d], Z)
append(X, Y, [a,b,c,d])

Apparently the correct answers are 2 and 3, but I don't get why 1 isn't also correct - wouldn't there be infinite possibilities for Z and hence also Y? Also, why would 2 be correct, since the first argument and the third argument ("the result") are identical? Sounds like Y could just be [].

Thanks a lot!

like image 409
Veins Avatar asked Sep 10 '20 08:09

Veins


People also ask

What is the difference between having no solution and infinite solutions?

Having no solution means that an equation has no answer whereas infinite solutions of an equation means that any value for the variable would make the equation true. What Are Infinite Solutions? The total number of variables in an equation determines the number of solutions it will produce.

Does a linear system in three variables have an infinite solution?

A linear system in three variables with an infinite solution will give a true statement. Learn more about the linear system and how to solve both conditions using the elimination method. Updated: 10/08/2021 You are probably familiar with linear equations, such as y = 3 x + 4 or y - 3 x = 4.

How do you prove an equation has an infinite solution?

If you simplify the equation using an infinite solutions formula or method, you’ll get both sides equal, hence, it is an infinite solution. Infinite represents limitless or unboundedness. It is usually represented by the symbol ” ∞ “. An equation will produce an infinite solution if it satisfies some conditions for infinite solutions.

How many equations do you need to solve a system with 3 unknowns?

If there are 3 unknowns, then you would need 3 equations. However, if one of the equations would turn out to be a linear combination of the others, then basically it might be just “useless” that is because it is redundant and will offer you with no information about how to resolve the system.


1 Answers

append(X, Y, X) can only be true of Y == [] (that's a "theorem for free" à la Wadler) but under that constraint, X can be any of infinite set of possibilities:

SWI-Prolog finds out about Y == [] and is noncommittal about the content of the X list, giving it only as a list of unbound variables:

?- append(X,Y,X).
X = Y, Y = [] ;   % alternative writing of X = [], Y = []
X = [_6696],
Y = [] ;
X = [_6696, _7824],
Y = [] ;
X = [_6696, _7824, _8952],
Y = [] ;
X = [_6696, _7824, _8952, _10080],
Y = [] ;
X = [_6696, _7824, _8952, _10080, _11208],
Y = [] 
...

And you are correct that append([a,b,c,d], Y, Z) should be listed as admitting an infinite number of solutions.

Interestingly, SWI-Prolog does not enumerate templates / lists of placeholder variables / candidates in this case but spits out what is essentially a rewrite of the constraint append([a,b,c,d], Y, Z), i.e. it behaves more theorem-proverish than in case 1:

?- append([a,b,c,d],Y,Z).
Z = [a, b, c, d|Y].

(That's a Prolog ambiguity: When does it enumerate and when not? If there were a Prolog notation for "list of N values of unspecified content, N an integer between 0 and +oo" such a notation could be used as output of append(X,Y,X) instead.)

The alternative, not chosen here, would be:

?- append([a,b,c,d],Y,Z).
Y = [], 
Z = [a,b,c,d] ;
Y = [_1], 
Z = [a,b,c,d,_1] ;
Y = [_1,_2], 
Z = [a,b,c,d,_1,_2] ;
...

Are there any Prologs that do the above? There might be.

like image 58
David Tonhofer Avatar answered Sep 29 '22 05:09

David Tonhofer