prolog interview questions
Top prolog frequently asked interview questions
I've took a course in which I learned some prolog. I couldn't figure out how / when to use cuts. Even though I get the general idea of cuts, I can't seem to use them properly.
Can anyone explain it briefly or give a good tutorial (that's not learnprolognow.org) on "cuts" that they can recommend?
Source: (StackOverflow)
What kind of problems is better solved in Prolog than in Haskell? What are the main differences between these two languages?
ADDED: Is there a Haskell library (kind of a logical solver) that can mimic Prolog functionality?
Source: (StackOverflow)
When I was skimming some prolog related questions recently, I stumbled upon this answer by @mat to question How to represent directed cyclic graph in Prolog with direct access to neighbour verticies .
So far, my personal experience with attributed variables in Prolog has been very limited. But the use-case given by @mat sparked my interest. So I tried using it for answering another question, ordering lists with constraint logic programming.
First, the good news: My first use of attributed variables worked out like I wanted it to.
Then, the not so good news: When I had posted by answer, I realized there were several API's and implementations for attributed variables in Prolog.
I feel I'm over my head here... In particular I want to know the following:
- What API's are in wide-spread use? Up to now, I found two: SICStus and SWI.
- Which features do the different attributed variable implementations offer? The same ones? Or does one subsume the other?
- Are there differences in semantics?
- What about the actual implementation? Are some more efficient than others?
- Can be (or is) using attributed variables a portability issue?
Lots of question marks, here... Please share your experience / stance?
Thank you in advance!
Edit 2015-04-22
Here's a code snippet of the answer mentioned above:
init_att_var(X,Z) :-
put_attr(Z,value,X).
get_att_value(Var,Value) :-
get_attr(Var,value,Value).
So far I "only" use put_attr/3
and get_attr/3
, but---according to the SICStus Prolog documentation on attributed variables---SICStus offers put_attr/2
and get_attr/2
.
So even this very shallow use-case requires some emulation layer (one way or the other).
Source: (StackOverflow)
I have been reading many articles trying to understand the difference between functional and logic programming, but the only deduction I have been able to make so far is that logic programming defines programs through mathematical expressions. But such a thing is not associated with logic programming.
I would really appreciate some light being shed on the difference between functional and logic programming.
Source: (StackOverflow)
I've started learning Prolog and wondering about the theoretical difference from the SQL language.
For example:
- both are declarative languages
- both support a fact-driven knowledge database
- both support question-styled data-retrieving
- both support functional dependencies
Any more common points? Any notable differences?
Source: (StackOverflow)
I am new to Prolog and noticed that ' and " give different behavior, but am curious as to why. Specifically, when loading a file, ?- ['test1.pl'].
works, while ?- ["test1.pl"].
doesn't.
Source: (StackOverflow)
When I want to read up on logic programming I always stumble over two "main" ways to do it nowadays:
- miniKanren, a minilanguage introduced in The Reasoned Schemer and popular at the moment due to core.logic.
- Prolog, the first "big" logic programming language.
What I'm interested in now: What are the principal technical differences between the two? Are they very similar in approach and implementation, or do they take completely different approaches to logic programming? Which branches of mathematics do they come from, and what are the theoretical foundations?
Source: (StackOverflow)
Many study Prolog in college, but I have personally not come in contact with it professionally. The traditional examples given are AI and expert system applications, but what have you used it for and what made Prolog a suitable language for the task?
Source: (StackOverflow)
What is one good for that the other's not in practice? I understand the theory of what they do, but what are their limitations and capabilities in practical use? I'm considering Drools vs a java prolog for a new AI project, but open to other suggestions. What are some popular approaches for inferencing on a complicated relational data set or alternatives?
Source: (StackOverflow)
Now since i've taken a class 3 years ago in A.I. im clearly proficient enough to ask this question......just kidding just kidding ;)
but seriously, what is it about these languages that make them so popular for A.I. research. Even though A.I. research is "old"...it's came probably the longest way in the past 5-10 years it seems like....
Is it because the languages were somewhat "designed" around the concept of A.I. , or just that we have nothing really better to use right now?
I ask this because I've always found it quite interesting, and Im just kinda curious. If im entirely wrong and they use different languages I would love to know what all they use. I mean i can understand prolog, especially with Sentient/Propositional Logic and Fuzzy logic. but I dont understand "Why" we would use Lisp...and even what else A.I. researchers would use to do machine learning etc.
Any articles/books on the subject matter is helpful too :)
Source: (StackOverflow)
The matter of deterministic success of some Prolog goal has turned up time and again in—at least—the following questions:
Different methods were used (e.g., provoking certain resource errors, or looking closely at the exact answers given by the Prolog toplevel), but they all appear somewhat ad-hack to me.
I'm looking for a generic, portable, and ISO-conformant way to find out if the execution of some Prolog goal (which succeeded) left some choice-point(s) behind. Some meta predicate, maybe?
Could you please hint me in the right direction? Thank you in advance!
Source: (StackOverflow)
I have come across an unfamiliar bit of Prolog syntax in Lee Naish's paper Higher-order logic programming in Prolog. Here is the first code sample from the paper:
% insertion sort (simple version)
isort([], []).
isort(A.As, Bs) :-
isort(As, Bs1),
isort(A, Bs1, Bs).
% insert number into sorted list
insert(N, [], [N]).
insert(N, H.L, N.H.L) :-
N =< H.
insert(N, H.LO, H.L) :-
N > H,
insert(N, LO, L).
My confusion is with A.As
in isort(A.As, Bs) :-
. From the context, it appears to be an alternate cons syntax for lists, the equivalent of isort([A|As], Bs) :-
.
As well N.H.L
appears to be a more convenient way to say [N|[H|L]]
.
But SWI Prolog won't accept this unusual syntax (unless I'm doing something wrong).
Does anyone recognize it? is my hypothesis correct? Which Prolog interpreter accepts that as valid syntax?
Source: (StackOverflow)
How to write in a standard conforming manner avs_term_rearranged(AVs, T, AVsR)
with given AVs
and T
such that AVsR
is a permutation of AVs
with the elements arranged in same order as their variables occur in left-to-right order in T
.
AVs
is a list of elements of the form A = V
where A
is an atom designating a variable name like 'X'
and V
is a corresponding variable. Such lists are produced by read_term/2,3
with the read-option variable_names/1
(7.10.3). Additionally, the precise order of elements is not defined.
| ?- read_term(T,[variable_names(AVs)]).
A+B+A+_+C.
AVs = ['A'=A,'B'=B,'C'=C]
T = A+B+A+_+C
T
is a term that contains all variables of AVs
plus some more.
Note that in a standard conforming program one cannot rely on the term order for variables (7.2.1):
7.2.1 Variable
If X
and Y
are variables which are not identical then X
term_precedes Y
shall be implementation dependent except that during the creation of a sorted list (7.1.6.5,
8.10.3.1 j) the ordering shall remain constant.
NOTE — If X
and Y
are both anonymous variables then they are not identical terms
(see 6.1.2 a).
Consider as an example from 8.4.3.4:
sort([f(U),U,U,f(V),f(U),V],L).
Succeeds, unifying L with [U,V,f(U),f(V)] or
[V,U,f(V),f(U)].
[The solution is implementation dependent.]
So there are two possible ways how sort/2
will work, and one cannot even rely on the success of:
sort([f(U),U,U,f(V),f(U),V],L), sort(L, K), L == K.
As an example:
?- avs_term_rearranged(['A'=A,'B'=B,'C'=C], A+C+F+B, AVsR).
AVsR = ['A'=A,'C'=C,'B'=B].
Source: (StackOverflow)
Does anyone know of a nice (and preferably free) way to integrate Prolog and C#?
Im looking to create a Prolog dll or similar to call from my managed code, and retrieve an answer once all the processing has been complete. Im looking for it to be predominantly one sided (c# calls Prolog).
I have seen this question which talks about Prologs real world usage but I was wondering if anyone had either any experience with c# & Prolog? or a nice tutorial/article?
Source: (StackOverflow)
Many systems provide a pure and efficient implementation of member/2
. In particular, no choice point is left open for:
?- member(b,[a,b]).
true.
whereas, a naive implementation of member/2
produces rather:
?- member(b,[a,b]).
true ;
false.
which is certainly correct from a declarative viewpoint, but less efficient.
On the other hand, there are some technical problems with member/2
. It permits redundant solutions, like in:
?- member(a,[a,a]).
true ;
true.
memberd/2
solves this problem using if_/3
and (=)/3
.
memberd(E, [X|Xs]) :-
if_(E = X, true, memberd(E, Xs)).
?- memberd(a,[a,a]).
true.
Unfortunately, this definition leaves choice points open again - producing ; false
("leftover choicepoints") in situations where member does not:
?- memberd(X,[a,b]).
X = a ;
X = b ;
false. % BAD - to be avoided!
?- member(X,[a,b]).
X = a ;
X = b.
So my question: Is there a definition of memberd/2
that avoids the choice point as this one above?
Source: (StackOverflow)