Questions tagged [prolog]
Prolog is the most commonly used logic programming language. It supports non-deterministic programming through chronological backtracking and pattern matching through unification.
1
vote
1answer
18 views
Get index of given element in list
I am trying to solve some basic puzzles to learn prolog. I am trying to get the index of a given element in a list with recursion. I got stuck trying to solve the problem and I am not sure why. When I ...
0
votes
0answers
20 views
Creating a Prolog predicate via macro-like meta programming
I need a way for this to work:
?- create_pred(f, [A, B], (write(That), write(B), write(A), write(This))).
true.
?- f(this, a, b).
_L154bathis
true.
I have it working without the This so far:
?- ...
1
vote
1answer
42 views
How to find Nephew in Prolog Family Code?
I'm trying to get Prolog to output people in the family trees nephew, as in nephew(X,Y) will list
X = the nephew
Y = Aunt/ Uncle
I've tried writing some of the code already, I'm pretty confident ...
-2
votes
0answers
31 views
Defining a predicate that converts a number of minutes into hours and minutes recursively [duplicate]
I have to define a predicate that recursively subtracts 60 from a number of minutes and increments the number of hours. ie
mins_to_hours_and_mins(124, H, M).
H = 2
M = 4
How would i start this task?...
1
vote
2answers
55 views
Prolog path backtracking runs forever depending on grid size
I've written some code to do backtracking in Prolog that generates all the possible paths to reach the Gold cell from the initial one (Agent). The input of getAllPaths is the map size NxN. When I run ...
3
votes
0answers
65 views
Can this depth-first search be coded with failure-driven loop without recursion?
Could you please help me to convert the below of my code into the one using the failure-driven loop (without using recursion)?
I coded a simple failure-driven loop that prints the elements in a list ...
0
votes
1answer
30 views
My Swi-prolog code return true for every query
I'm trying to write Swi-Prolog code for family relations. There are no error but it always returns true. P
man(_Pete).
man(_Mark).
man(_John).
man(_Frank).
man(_Tom).
man(_Matt).
man(_Henry).
man(...
0
votes
1answer
28 views
Recursive list creation
I'm stuck on this issue with lists in SWI-prolog.
In Prolog a variable can be written once, so i can't deal with this problem.
check(Parameter, [H | T], Result) :-
get_res(Parameter, H, Res),
...
2
votes
0answers
36 views
Problem with getting an item out of a list
Basically I have a list with elements with the following syntax:
name : value
(all are a symbol)
So now let's say that I have such an item in a list:
[price : '42']
Ideally using
nth0(0, [...
1
vote
2answers
72 views
Avoid findall overflow with n-fractions problem
I am trying to print all solutions of the n-fractions problem for n=4:
:- lib(ic).
fractions(Digits) :-
Digits = [A,B,C,D,E,F,G,H,I,J,K,L],
Digits #:: 1..9,
ic:alldifferent(Digits),
X ...
-1
votes
0answers
21 views
How to simplify algebra equations represented as list of list by manipulating lists only [duplicate]
With Prolog I want to simplify algebra expression represented as as list of list using list manipulation only:
algebra equation
f = 3x+2
list of list
[[3,1],[2,0]]
3 and 2 are coefficients
1 and 0 ...
0
votes
1answer
64 views
Length and Path in Graphs Prolog (avoid infinite recursion for cycles)
node(a).
node(b).
node(c).
node(d).
node(e).
node(f).
node(g).
node(h).
edge(a,b).
edge(b,c).
edge(c,a).
edge(c,e).
edge(c,d).
edge(d,e).
edge(d,h).
edge(e,g).
edge(g,e).
edge(e,f).
edge(f,g).
parent(...
1
vote
1answer
34 views
How Do I Compare a List to a Database Prolog
I have a database of facts that has entries like this snippet
symptom(shingles,headache).
symptom(shingles,fever).
symptom(shingles,malaise).
symptom(shingles,headache).
symptom(...
1
vote
2answers
36 views
Prolog - return list of unique numbers in nested lists
Hi everyone I'm trying to learn Prolog on my own and I am stuck trying to create a function that takes a nested list and returns a list of only the unique numbers. So unique(L,T) means that T is a ...
1
vote
2answers
55 views
Arithmetic operations in prolog
How is it possible to obtain 28 using 7 10 3 2 in this order and without using parentheses?I've been thinking for the past 1 1/2 hours and I couldn't get anywhere.
0
votes
0answers
33 views
Prolog list of compounds to classical first order logic
I have to write first order logic (FOL) formulas of a Prolog program and don't know how express lists and operations on lists.
Suppose I have the following toy program
:-use_module(library(clpr)).
...
2
votes
0answers
52 views
Why does Prolog rule sometimes return False with similar inputs?
I cannot understand why false is returned for jordan but not for china:
When I query:
countries_visited(jordan, X).
I get a return of amman and false.
But when I query
countries_visited(china, X)....
5
votes
2answers
71 views
Equivalence of disjunction operator and definition with several rules
I just stumbled over the definition of ;/2 in the SWI Prolog Manual which states:
The `or' predicate is defined as:
Goal1 ; _Goal2 :- Goal1.
_Goal1 ; Goal2 :- Goal2.
Wouldn't that mean that ;/2 ...
1
vote
1answer
34 views
How to bypass arguments not instantiated on SWI-Prolog online editor?
I'm using SWISH to write Prolog and I've got a program that converts minutes into hours.
My output for minutes is fine however my hours outputs as H = 0+1+1 rather than H = 2. Is there any way that ...
1
vote
1answer
44 views
How to backtrack over a NxN board in Prolog?
I currently have to make some sort of Wumpus World implementation in SWI Prolog and give all possible paths over a board of size NxN, I have done several prolog tutorials but I can't figure how to ...
1
vote
1answer
26 views
How to turn a java array into a prolog list and make a jpl Query with it?
I'm making a sudoku solver in java, using a small prolog kb at it's core. The prolog "sudoku" rule requires a prolog list of lists. In java I have an int[][] with the sudoku values.
I've made the ...
3
votes
3answers
129 views
Prolog convert mins to hours
This is the code I have created.
mins_to_hours(In, H, M):-
In < 60,
H = 0,
M is In.
mins_to_hours(In, H, M):-
In >= 60,
H is H1+1,
In1 is In-60,
mins_to_hours(In1, H1, M).
It ...
6
votes
0answers
58 views
Reified call_with_time_limit / call_with_inference_limit
I am trying to define a relation callto_status(Goal, Status) that always succeeds and unifies Status according to the result of calling Goal (in other words, I would like to implement a reified ...
3
votes
2answers
55 views
How to Write User Input to a List Prolog
I am trying to write the user input into a list but am running into two problems. Using this code
getInput([Symptom|List]):-
writeln('Enter Element:'),
read(Symptom),
dif(Symptom,stop),
...
1
vote
2answers
51 views
Can I use integers as parameters?
How can I know if a person X is descendant of a person Y given the descendancy degree?
I've tried this:
descendant(X, Y, 1) :- son(X, Y).
descendant(X, Y, Degree) :- son(X, Z) , descendant(Z, Y, ...
1
vote
1answer
40 views
Why does my else statement in prolog execute even if it doesn't satisfy the condition?
I'm new to Prolog and I am confused because the else clause is being executed even if it doesn't meet the condition.
printNum(A, B) :-
A =< B,
writeln(A),
A1 is A + 1,
...
1
vote
1answer
76 views
Backtracking issue SWI-Prolog
i have some issues with backtracking in SWI-Prolog
In my predicate i have 2 lists as an input and the result is a third one.
I take from L1 each element with nth0/3, then i use another predicate ...
0
votes
0answers
23 views
How do I pass a java array to a prolog query using jpl7?
I don't have code handy since I'm at work right now, but I'm trying to use this sudoku code in Java: http://www.swi-prolog.org/pldoc/man?section=clpfd-sudoku.
I have an int [][] for my Java side ...
0
votes
1answer
60 views
PROLOG program semantic and exercise
First of all I have a doubt about the semantic of a program , for example :
length([],0).
length([_|L],N):-
length(L,N0),
N is N0 + 1.
The first instruction means the base case , or it has ...
3
votes
1answer
63 views
Prolog unifies a variable with a term then forgets the unification
I have the following predicate which I have written for recognising when two lists are the same except the two elements at indices I1 and I2 are swapped:
swapped(I1, I2, List, NewList) :-
% The ...
3
votes
4answers
85 views
Prolog compress list with quantity — repeated answers
I have a list of elements which contains the number of friends a person has.
[friends(mike, 4), friends(joe, 3), friends(mike, 1), friends(mike, 2)]
I want to compress this list and obtain the ...
0
votes
0answers
42 views
How to do a query in Prolog
family(person( john, cohen, date(17,may,1990), unemployed),
person( lily, cohen, date(9,may,1990), unemployed),[ ] ).
family(person( john, armstrong, date(7,may,1988), unemployed),
person( lily, ...
1
vote
0answers
34 views
Java- Cannot import JIprolog packages
In Java I use these imports:
import com.ugos.jiprolog.engine.JIPEngine;
import com.ugos.jiprolog.engine.JIPQuery;
import com.ugos.jiprolog.engine.JIPSyntaxErrorException;
import com.ugos.jiprolog....
0
votes
0answers
80 views
can you please explain how this code works?
There is an exercise in Learn Prolog Now! Chapter 3, Practical Session. number 3.
I don't understand this part which takes another arguments named G. what is G doing exactly?
What does the recursive ...
2
votes
1answer
53 views
Putting SWI-Prolog's JPL interface to work
I want to use JPL to create a graphic interface to my project, so I went and searched a code example to start working with, I downloaded the code but when I run it, it shows the following message:
...
1
vote
3answers
68 views
Prolog - How to separate atoms from expression involving predicates?
If I’ve an expression:
x + y * (-z)
How to I separate them to form a list of [x,y,z]?
My idea:
split2(X, [X]) :-
X \= +(_,_),
*(_,_),
-(_).
split2(X + Y, [H|T]) :-
split2(X,[H]), ...
3
votes
1answer
45 views
How to permanently save changes of dynamic predicates to .pl file? (Tau-Prolog)
Question - Asserted dynamic predicates don't save permanently, after creating new session they're missing. How to save them permanently?
I'm using
Tau Prolog: A Prolog interpreter in JavaScript.
I'm ...
2
votes
1answer
30 views
Prolog - Change elements of a list
Complete beginner doing exercises from a book, I want take a list X and change its elements so that for some [H|X] the new list Y would have the HEAD element be (H-1,H+1).
Something like:
X=[1,20,...
2
votes
1answer
42 views
Prolog: read predicate omits some parentheses in the user input
I am new to Prolog. I am trying to solve a number link puzzle with varying board size and start end positions of the points to connect. Since the board configuration detail vary, I am getting all ...
0
votes
0answers
30 views
Operator expected when using ?X but not when using X?
I am trying to get this recursive function to work, but when I have ? I get Operator expected error. If I remove the ? the program works. Am I misunderstanding how to use ? or is there some other ...
0
votes
1answer
65 views
How can find all states of operator equals of one number in list of number
We have list of number and list of operator we must find state witch of equals to operate element by define
for example:
input :
Numbers: 1,2,3
Operators: +,-,/,*,(,)
output :
1-> +2+3 (...
2
votes
1answer
30 views
uncaught exception: error(existence_error(procedure,likes/0),friend/0) (Seven Languages)
Currently working through the Seven Languages in Seven Weeks book, and am stuck on getting the first prolog examples to run. This concerns the same code piece as this question; however I believe my ...
1
vote
1answer
27 views
How to Change Package Directory in SWI-Prolog
I am attempting to install a package (sldnfdraw) in SWI-Prolog, but I cannot find a way to specify an installation directory (ie where I want it to be installed).
The documentation seems to indicate ...
3
votes
1answer
54 views
Context sensitive generation in prolog
I am interested in generating elements of a context sensitive language as described by Chomsky, as described in Chomsky Classification of Grammars
under the section "Type - 1 Grammar".
(Basically, ...
0
votes
1answer
46 views
Prolog list of list n number with condition
I'm studying prolog language and i have an issue regarding this problem.
I've already created a program that, given a number N, returns a list with elements between 0 and N:
list2val(N,L):- list2val(...
2
votes
2answers
45 views
Constructing XOR/3 in Prolog
I have the following clauses for /3 propositional logic.
statement(false).
statement(true).
not(false, true).
not(true, false).
and(false, false, false).
and(false, true, false).
and(true, ...
1
vote
4answers
137 views
Prolog: How to generate simple math expressions?
I need to write (not calculate) all the states of list of number that means :
Input:
Numbers: 1,2,3
Operators: +,-,/,*
Output:
1+2+3
1-2-3
1/2/3
1*2*3
1+2-3
1+2/3
1+2*3
1-2+3
1-2/3
1-2*3
1/2+3
1/...
2
votes
3answers
94 views
For length/2 how to add human readable variable names
How can human readable variable names be displayed for system generated variable names?
As a simple example:
?- length(Ls,N).
Ls = [],
N = 0 ;
Ls = [_5112],
N = 1 ;
Ls = [_5112, _5118],
N = 2 ;
Ls = ...
1
vote
1answer
36 views
List with atomic elements Swi-Prolog
I'm stuck on this problem. I want to check if all the elements in the list are atomic, but I can't solve it.
For example:
[] is atomic
list [1, 2, 3] is made by atomic elements
list [[1], 2, 3] not ...
1
vote
2answers
52 views
An easy to implement prolog graphical interface [closed]
So, I have a college project now that uses Prolog, I'm not the first one to work on the code so it's already pretty huge, but I have to put it working with a Prolog graphical interface, so my question ...