FAQ Database Discussion Community


Calling Python file in Prolog Program

python,text,prolog,text-to-speech,swi-prolog
I want to use a python file in my prolog code. I want to pass the text generated in prolog into text_to_speech_converter of python.How can I do it? I prefer to keep the interface of prolog only.

Prolog returning true/false instead of variable

prolog
I'm working on the very easy reverse list example in Prolog. append(A, [], [A]). append(A, B, [A|B]). reverse([], ReversedList). reverse([A,B], ReversedList) :- reverse(B, TemporaryList), append(A, TemporaryList, ReversedList). append works correctly. However, when I call reverse the interpreter doesn't respond with a variable like append but instead it just write true...

Prolog beginner help get current time

prolog,artificial-intelligence,swi-prolog
I am modifying the Eliza program http://cs.nyu.edu/courses/fall11/CSCI-GA.2110-003/documents/eliza.pl trying to get it to print the system time when user asks - What Time is it? After hours of reading through manual I got my get_date_time_value() function to work. As in get_date_time_value(Key, Value) :- get_time(Stamp), stamp_date_time(Stamp, DateTime, local), date_time_value(Key, DateTime, Value). However...

Prolog: append is failing inside loop

prolog
I have the following Prolog code: my_function(A):- my_function(A,[],1). my_function(A,List,Num):- Num_new is Num+1, generateL(O,S,L), write(L),nl, %append(L,List,List_New), ( Num_new < 20 -> my_function(A,List_New,Num_new) ). Which is working. It loops through 19 times, calls generateL which generates a new term L which I print to the screen. I now want on each iteration...

Avoid recursion in predicate

prolog
I have the following question for the following predicate, how can i drop the recursive call f(T,S1) from both predicates. Flow model: (i,o) f([],0). f([H|T],S):- f(T,S1), S1 > 2,!, S is S1 + H. f([_|T],S):- f(T,S1), S is S1 + 1. This is a trick question, and I am not...

Unification with STO detection

algorithm,prolog,unification,iso-prolog
In ISO Prolog unification is defined only for those cases that are NSTO (not subject to occurs-check). The idea behind is to cover those cases of unifications that are mostly used in programs and that are actually supported by all Prolog systems. More specifically, ISO/IEC 13211-1:1995 reads: 7.3.3 Subject to...

How to write number classifiers in prolog?

prolog
Playing with Prolog for the first time and while I thought I knew what it basically is good for, I find it hard to get anything done in it. So, I tried to find the easiest possible task and even fail to accomplish that. I think it is due to...

All partitions of a list in prolog

prolog
I am coding in Prolog. I want to find all distinct partitions of a list. I look at a list as a set here. Each partition is a set of sets in which none is empty, the union of all of them is the main set, and the pairwise intersection...

Prolog assigning values to elements of a list

prolog
I am new to Prolog and the problem I am dealing with is the following: Given a list of variables, I want to assign a value for each element of that list, and then check if a restriction containing some of those variables is true. This is an example of...

Execution order in Prolog: ; operator

prolog,operators
I am currently delving into Prolog but coming from a JavaScript background a lot seems strange. For instance, I have a definition such as this: np # Subject, pp # IObject ; np # IObject, However, in executing the result is not as expected. But when using parantheses it is....

Unwanted logical variable in Prolog output

prolog
I have to do a Prolog homework for College and I'm mostly done, but i keep getting a _G variable in the output. We need to compare a List - L - to a regular term - K -, and, if the item in the list is bigger than the...

How to check whether a Variable=AnotherVariable+1 in prolog

prolog
I want to have a conditional expression as below in Prolog and the test it. checkNum(X,Y,helloWorld):- X=Y+1. But when I test checkNum(7,6,Z) it says No! I think this condition is true but I can't understand why it doesn't work....

Creating a List in prolog

prolog
I have a predicate that has the following format: pilot( ID1, ID2 ). I'm searching for multiple values for pilot and Im able to get this predicate from a findall, but how can I go from there and use that new predicate to create a list like this: [ pilot(...

swi-prolog: remove '|:' before user input in compiled program

prolog,swi-prolog
Whenever I compile anything with swi-prolog, it adds |: before user input to show that you are supposed to write something, which would be fine, but I need to pipe the output of this program to another program, preferably without the |:. My compilation options are: swipl -nodebug -g true...

What does `\+` signify as an operator in prolog?

syntax,prolog
I have encountered the symbol \+ in prolog and am not sure as to what it signifies. I have gone through the prolog documentation and am either not being too resourceful or have not really noticed it. Thank you for your answer! ...

Checking that a list contains only zeros

prolog
I have been given a question in my assignment that asks to write a Prolog program that takes as input a list of numbers and succeeds if the list contains only 0s. I am having trouble on how to make the program search for zeroes. For example, a query like:...

Solving constraints with string concatenations in Prolog

prolog,swi-prolog
Here, I tried to solve some simple string constraints in Prolog. While this problem seems to be straightforward, the main predicate still does not print the value of FinalString, which should be "hello world". Is it possible for Prolog to solve constraints that contain string concatenations, like this one? :-...

Prolog-iterating through list

list,prolog,iteration,head
Let's say i have list Xs = [a,b,c]. Now i want to iterate through all elements and call another function for this elements. My question is: how to do this using head and tail? I would be grateful for help.

Use reified constraints to make 3 numbers consecutive

prolog,constraints,clpfd
Here's an outline of my SWI-Prolog program: :- use_module(library(clpfd)). consec1(L) :- L=[L1,L2,L3,L4,L5,L6,L7,L8,L9], L ins 1..9, ..., abs(L5-L4)#=1, all_different(L), labeling([],L) abs(L5-L4)#=1 makes L5 and L4 next to each other. If I wanted to make three numbers next to each other e.g. L3, L4 and L5, how could I use reified constraints...

Is there a way or an algorithm to convert DCG into normal definite clauses in Prolog?

algorithm,parsing,prolog,grammar,dcg
I am a newbie in Prolog, and I am trying to understand how a grammar can be translated into a normal definite clause from a DCG. I understood that DCG notation is just syntactic sugar for normal definite clauses in Prolog. I started to depict some similarities between the normal...

Why I can't get an answer for the Ship Puzzle with Prolog?

prolog,zebra-puzzle
I need to solve the Ship Puzzle problem with using Prolog. Here are the facts. There are 5 ships. 1. The Greek ship leaves at six and carries coffee. 2. The Ship in the middle has a black chimney. 3. The English ship leaves at nine. 4. The French ship...

How can I call a function random inside other function in prolog?

prolog,prolog-assert
I'm trying to call the random function inside another function. For example I want to do this assert(fact(random()). But it does not work. How can I insert a random number this way? Thanks.

Prolog isomorphic graphs

graph,prolog,isomorphism
Good night. Trying to solve the isomorphic graphs problem here. Assignment info: Determine whether 2 undirected graphs are isomorphic. No isolated vertices. Number of vertices is less than 30 Edges of graphs are given as predicates, i.e. e(1, 2). f(1, 2). I'm trying to use the following approach: For every...

prolog - out of local stack error [duplicate]

prolog
This question already has an answer here: Never-ending fill method, infinite loop that returns answer but doesn't exit 1 answer I am trying to create a program that has following specifications: N=int X=int R=list containing N copies of X • R=[] if N=0 • N will always be given...

Prolog - simplify derivative

prolog,simplify
so I just got started with Prolog this semester, and got the homework to implement a pretty basic d(function, variable, derivative) which I did like this: d(X,X,1) :- !. d(C,X,0) :- atomic(C). %, (C \= X). d(X**E,X,E*X**(E-1)). d(U+V,X,A+B) :- d(U,X,A), d(V,X,B). d(U-V,X,A-B) :- d(U,X,A), d(V,X,B). d(U*V,X,DU*V+U*DV) :- d(U,X,DU), d(V,X,DV). d(U/V,X,(DU*V-U*DV)/(V*V))...

How to multiply all elements of two lists with each other in Prolog

prolog
I am thinking how to multiply all elements of two list with each other. Then I want to put all results in List3. For example, List1 = [1,3,5]. List2 = [2,6,7]. List3should contain [1x2, 1x6, 1x7, 3x2, 3x6, 3x7, 5x2, 5x6, 5x7]. In the end; List3 = [2, 6, 7,...

What is the difference between _ and _variable in prolog?

syntax,prolog
I have encountered a lot of problems with a literal that starts with a _ such as _Peter. So what exactly would be the difference between: good(_,_). and good(_,_Peter). Thank you for answering my question!...

How would I be able to print out the values of a Fact in Prolog?

prolog
I have the following Prolog rule that allows the user to input three values and assert a Fact from those values: input(X,Y,Z) :- Fact = ves(X,Y,Z), assertz(Fact). How would I now be able to print out the values of the 'ves' Fact? I tried this, however I receive a singleton...

What's the module name of built-in predicate 'term_string/3' in SWI-Prolog 7.1.3?

module,prolog,predicate,swi-prolog,built-in
I am using a framework that has globally overridden the built-in predicate term_string/3 with different behaviour. Now i would like to use the built-in one, but what is its module name? I would like to call <ItsModuleName>:term_string(..) I know that list operations have a separate module :- use_module(library(lists)) that can...

Best editor for Prolog [closed]

prolog,swi-prolog
which is the best editor one can use for writing Prolog codes? Thanks

add some member to a list with prolog

list,prolog
i have some facts.I want to make a list of members of this facts and show these members by function.but i don't know how to do it. i try this code : hasDiabet(mina). hasvitaminD(milk). hasvitaminD(eggs). addto(X,L,[X|L]). hasvitamin(L):-hasvitaminD(x),addto(X,L,L),hasvitamin(L). eat(Y,X):-hasvitaminD(X). here i want to make a list of thing that hasvitaminD ->...

Prolog rules and query

prolog
I need some help to find the rules and/or query for knowledgebase in Prolog with information about Costumers in a supermarket. For example I have: Customer(Name,Age,Sex,Wage). customer(John,30,M,2000). customer(Mary,35,F,2500). customer(Mark,40,M,2500). invoice(Number, CostumerName, Product, Price). invoice(001, John, Potatoes, 20). invoice(002, John, Tomatoes, 10). invoice(003, Mary, Soap, 50). invoice(004, Mark, Potatoes, 20). invoice(005,...

freeze for more than one variable

prolog,prolog-coroutining
I tried to write a predicate that takes a list and converts it to a balanced tree. My code looks as follows: /* make_tree(list, tree) * * list: list with the elements of the tree in prefix order * tree: balanced tree of the elements in the list * */...

How can I compare two lists in prolog, returning true if the second list is made of every other element of list one?

list,recursion,prolog
I would solve it by comparing the first index of the first list and adding 2 to the index. But I do not know how to check for indexes in prolog. Also, I would create a counter that ignores what is in the list when the counter is an odd...

gprolog: Getting a stacktrace after an exception

prolog,gnu-prolog
While using gprolog I often have exceptions without any kind of line numbers or context like this one: uncaught exception: error(instantiation_error,(is)/2) Without any kind of context. I know I can do a trace but it would take very long to debug it with trace since I need to execute a...

Output in Sentence Form Prolog

prolog,swi-prolog
I want this output to be in string format. How can it be done? Output I am getting: [[[[a]|fat]|man],[[[[[was]|walking]|quickly],to],[[[[the]]|end],[of,[[[the]|long]|corridor]]]]] Expected Output: a fat man was walking quickly to the end of the long corridor ...

Prolog: missing feature?

prolog
Any programmer with some experience in Prolog knows the advantages of use unary notation for numbers. By example, if we represent a number as list of 1" ("4" is list "[1,1,1,1]" and so on), we can define: unary_succ(X,[1|X]). the following queries does what is expected: ?- X=[1,1],unary_succ(X,Y). X = [1,...

Find permutations of list with unique Heads

list,prolog,permutation
I have a list of unique elements, and I want to generate permutations of that list but I only want the elements to be the head of a list once, like this: ?- special_permutation([a,b,c], X). X = [a,b,c] ; X = [b,a,c] ; X = [c,a,b] ; false. ?- special_permutation([a,b,c,d,e],...

Prolog not giving correct answer

prolog,prolog-toplevel
I am learning Prolog. I wrote some simple facts. But it seems that Prolog is not giving me right answers. Please help me understand what I am doing wrong here. facts - weather.pl weather(pheonix,summer,hot). weather(pheonix,winter,warm). weather(la,summer,warm). When I run this in swi-prolog as follows ?- weather(City,_,warm). City = pheonix ....

How to get Prolog to explain your result beyond the true statement

prolog
I have the following facts and rules: flight(sea,msp). flight(msp,jfk). route(A,B) :- flight(A,B). route(B,A) :- flight(A,B). route(A,C) :- flight(A,B) , flight(B,C). when query for route(sea,jfk) I get a result true but what I wish to get is the explination: sea-->msp-->jfk this way I can tell not only that it's true but...

accessing program listing in prolog

prolog
I'm having some strange (or not so strange) problems defining variables in SWI-Prolog. Example: I'd like to do something like below: :- initialization(main). main :- X = listing(main), write(X). but it's simply printing "listing(main)" ...

Prolog: Summing elements of two lists representing an integer(restrictions inside not regular sum!!)

list,prolog
I was solving a problem: A list is representing an integer say 12345 by L=[12,34,5] each element should be from 0 to 99.The exercise is to write a function (sum) that sums two lists and gives the equivalent list of their sum which is representing the sum of two integers....

How to find the positive numbers in a list in Prolog?

prolog
I want to write a code in prolog that gets a list and find its positive numbers and adds them into a new list as below : ?- findPositives([-1,2,3,-5,-7,9],Result) Result : [2,3,9] How can I write this code? ...

Sorting a nested List Desceding order

prolog
Good day, I have this nested list: Xs = [ [Joe, Pilot, 100], [Stan, Co-Pilot, 300], [Steve, Pilot, 150], ]. How can I sort this nested list in order to do the following: (Using the third element in descending order) Xs = [ [Stan, Co-Pilot, 300], [Steve, Pilot, 150], [Joe,...

Prolog combinatorics

prolog,logic-programming
Is there any way to generate all possibilities like 9 letters to be divided in 3 teams, like this: - 1st team: 2 letters - 2nd team: 3 letters - 3rd team: 4 letters ? Example: find([a, b, c, d, e, f, g, h, i], T1, T2, T3). T1 =...

prolog misunderstanding. Split list into two list with even and odd positions. where is my mistake?

list,split,prolog
I'm trying to split a list into two other list. List Even with the elemnts in even possition and a List Odd with the others. I've been looking over iinternet a solution, but all seem to me like chinese, i can not understand the process. So this is what i've...

searching in list getting true infinitely in prolog

list,search,prolog
I tried to code a Prolog program that takes 2 value and calculates if the pair is valid or not. If pairs are in different lists, then pairs will be valid and they can make match. If two team in same list(group) then they can't make match which means false....

Prolog code gives two different results

list,prolog
So I'm making this code where there's a function that receives 2 arguments and tells if one of them is not a list. The code is the following: /*** List Check ***/ islist(L) :- L == [], !. islist(L) :- nonvar(L), aux_list(L). aux_list([_|_]). /*** Double List Check ***/ double_check(L, L1)...

prolog traverse nonstandard tree left to right

tree,prolog,tree-traversal
I need to implement a predicate trav(Tree,List) that performs a left to right tree traversal; Where: Tree is defined by the structure node(left,right), where left and right can be either another node or any Prolog data item. List is the list of leaf node values in the tree. For example:...

PROLOG: Keep list in recursion

list,recursion,prolog,key-pair
So, I've spent a lot of my time trying to figure this out without almost no progress. Hope you could help me. The goal is, to take a list like this(lets call it baselist): [[[feesc,11],[podshare,11]],[[feesc,11]],[]]. And make it become this: [[feesc,22],[podshare,11]]. I have a predicate responsible to add or sum...

Syntax Error, Operator Expected

sql-server,prolog
I am trying to query the MS SQL Server database using Prolog. Prolog Code lemmas:- odbc_query('my_db', 'SELECT * ,case when ActualCost<EstimatedCost then 'true' else 'false' end as Value from Work_Order ' ). It gives an error that operator is expected after 'true' . Note: The Query works in SQL-Server but...

Four Professions prolog puzzle

prolog,zebra-puzzle
I am trying to solve a puzzle in prolog and i made only a part of it, i can't figure out how to finish it. This is the problem: Consider four men with last names of Baker, Carpenter, Miller, and Farmer. Assume that the four professions represented by this group...

Backwards Chaining With Variables

prolog,logic,datalog
I have been reading up about inference in Prolog/Datalog and while forward chaining seems fairly simple to grasp, I have some issues with backward chaining with any sort of complex example which isn't simply propositional or used to determine a true or false value. I was reading an article the...

How to add a print command to DCG syntax

prolog,dcg
I want to add a print command to my DCG syntax, here is what I have: program( (R0 --> R) ) --> [begin],instructs(( R0 --> R )),[end]. instructs( ( R0 --> R ) ) --> instr(( R0 --> R )). instructs( ( R0 --> R ) ) --> instr(( R0...

Wait the end of a predicate Prolog

prolog,wait,predicate
I have a predicate list_moves(Move), that gives all the possible moves for a game. Then I have the following predicates : find_best_move:- nb_setval(best,0), list_moves(NewMove), nb_getval(best,OldMove), better(OldMove,NewMove). better(OldMove, NewMove):- NewMove > OldMove, nb_setval(best,NewMove), !. better(_,_). get_best_move(Move):- find_best_move, nb_getval(max,Move). I want to get only one move when I call get_best_move(Move) : the...

Why was the ISO standard for prolog a failure? [closed]

module,prolog,standards,iso,iso-prolog
In the introduction here, it states that the ISO standard for Prolog was a failure. Can anyone elaborate on this?

Multiple values of a variable inbetween 0 and a number prolog

algorithm,variables,recursion,prolog,logic
So I've been trying to teach myself prolog and I think I'm coming along nicely. However, I'm sort of stuck at this one method I'm trying to make. toN(N,A) A is equal to the integer values between 0 and N-1, generated in ascending order. so toN(5,A) would be A =...

procedure doesn't work for evey pattern in prolog

prolog,counter
I try to find how many children a person have. Created my sample space and in find procedure i should count the numbers of children and print the value. sample input/output: ?- find(joe,Result). false. i tried to change find(X,R) like that: find(X,R) :- R is 0 . R is R+1...

Constraint not propagated upon instantiation of list members

parsing,prolog,clpfd
I am building a parser and generator for dates and times. In an ordinary programming language these would be written separately. In Prolog+CLP(FD) I can write 1 predicate that does both :-) In my use case it often makes sense to parse a number of digits and convert the to...

Processing object topology in prolog: finding bidirectional edges from list of triangles

prolog
Here's what I want to do. Given the declaration: vertex(v0, coord(-1.0, 1.0, 0.0)). vertex(v1, coord(1.0, 1.0, 0.0)). vertex(v2, coord(-1.0, -1.0, 0.0)). vertex(v3, coord(1.0, -1.0, 0.0)). face(f0, v0, v1, v2). face(f1, v0, v2, v3). bidirEdge(A, B) :- edge(A, B), edge(B, A). Run query bidirEdge(X, Y). and receive X = v0, Y...

Google api oauth prolog

oauth,google-api,prolog,swi-prolog
I am trying to connect to google oauth. I have used javascript to make a connection to google and get a code, I then understand I need to exchange this code for a token as detailed here: https://developers.google.com/identity/protocols/OAuth2WebServer The code I have is : :- use_module(library(http/http_dispatch)). :- use_module(library(http/http_error)). :- use_module(library(http/html_write))....

How does the predicate 'repeat' create infinite choice points in prolog?

prolog
I do not understand how the predicate repeat evaluates to be true at all times and create infinite choice points. For example, when I call: :- repeat, reset, do(task,job). Thank you for your help! ...

Predicate that pick elements which are on list twice not less not more

prolog
I'm trying to write a predicate twice(El,L) which will return true. when El is on list exactly twice. Here is what I have: twice(El,L) :- select(El,L,L1), member(El,L1), \+ twice(El,L1). It works nice for twice(2,[1,2,2,3,4]) but for twice(X,[1,1,2,2,3,3]) it doubles every number X = 1 ; X = 1 ; X...

How to calculate factorial using prolog

prolog,factorial
I have a code for calculating factorial as below : fact(1,1). fact(X,R):- X1 is X-1, fact(X1,R1), R is R1*X. In my mind this code shouldn't work right but it does! What is my reason? I think when we call fact(3,R), first it calculate "X1 is X1 -1". Then it goes...

Prolog: arithmetic problems while writing rpg stat printing program

prolog
I've been trying to write (as an exercise - to learn a bit about prolog) a small code fragment that prints all possible stat combinations for specific race from Pen and Paper RPG. Relevant code fragment: %abilities ability(X) :- member(X, [str, dex, con, int, wis, cha]). race(tiefling, planetouched, [[dex, 2],...

Split list in prolog, produce two separate lists

list,prolog
The Question: Define a predicate split/4 which, when provided with a list L and integer N returns two lists, A and B, where A contains the items in L that are greater than or equal to N and B contains the items that are lower than N. Sample query with...

Can someone please help explain this copy function works?

list,recursion,prolog,copy
copyList([], []). % base case copyList([H|T], [H|R]) :- copyList(T, R). I "sort of" understand how recursion works, but when I analysed this function, I got really confused. Can someone please explain, step-by-step what happens in this function and how it reaches the end using the example below: ?- copyList([1,2,3],L). ...

Solving ship puzzle using prolog

prolog,zebra-puzzle
I need to solve the following problem using prolog. These are the facts provided. 1. The Greek ship leaves at six and carries coffee. 2. The Ship in the middle has a black chimney. 3. The English ship leaves at nine. 4. The French ship with blue chimney is to...

Representation of negative integers

prolog,iso-prolog
Does ISO-Prolog have any prescriptions / recommendations regarding the representation of negative integers and operations on them? 2's complement, maybe? Asking as a programmer/user: Are there any assumptions I can safely make when performing bit-level operations on negative integers?...

Prolog- Returning elements from facts

prolog,return,element,fact
i encounter a problem with facts. Let's say i got 3 facts and check(X) question. fact(a,b). fact(b,c). fact(a,d). check(X):- //some calculation with fact() How to make above question to return list of elements for given X from all facts? For instance: check(a) would give result b and d. So i...

How to keep punctuation as a separate element in split_string in Prolog

string,split,prolog,swi-prolog,punctuation
I'm doing an assignment in Prolog (I'm a newbie) and I have a small problem. I know you can split a string to a list of elements in Prolog like this: split_string("Hello, here I am!"," "," .!?-_'",Temp). But this takes out the basic punctuation. It returns: Temp = ['Hello','here','I','am']. How...

In SWI-Prolog, how to transform, convert, mutate, evolute, change a set list [ 1, ( 2, 3 ) ] to [ 1, 2, 3 ]?

prolog
I have a program that performs a user input with read/1 clause. Then creates a list containing the input information as follow: write('Quais os Colegas de Profissao? (Insira seus nomes entre \n' ), write('aspas simples e separados por virgula) '), write('Seguido por um ponto.'), read(ColegasDeProfissao), consult('Trabalho1/bancoDeDados.pl'), Predicado =.. [informacoesProfissionais, [...

Prolog: Obtain 1st element in “list of lists”

prolog
I have a "list of lists" e.g. [[A,B,C],[A,B],[D,F]]. I now wish to print to the screen the first element of the list, (i.e. [A,B,C] in this example). I had thought I could do this as follows: testfunction(List):- List=[[Top]|Rest], write(Top),nl,nl. However, when I run this with testfunction([[A,B,C],[A,B],[D,F]]). it seems to fail...

Prolog- singleton variable in branch warning

variables,prolog,singleton
Hello here is my code in Prolog: arc(a,h). arc(b,c). related_to(X, Ys) :- setof(Y, arc(X, Y), Ys). cut([H|T],Y) :- check(H,Y), T = [] -> cut(T,Y). check(X,Y) :- related_to(X,Xs), member(Y,Xs) -> write('There is a road'); cut(Xs,Y). When I am trying to run check(a,b) it doesn't run. I get the message Singleton variable...

PROLOG: Determining if elements in list are equal if order does not matter

prolog
I'm trying to figure out a way to check if two lists are equal regardless of their order of elements. My first attempt was: areq([],[]). areq([],[_|_]). areq([H1|T1], L):- member(H1, L), areq(T1, L). However, this only checks if all elements of the list on the left exist in the list on...

How to find the Nth element of a list in Prolog

list,prolog
I am trying to write a Prolog code finding the n-th element of a list. I wrote the below code but it doesn't return the element right. match([Elem|Tail],Num,Num,Elem). match([Elem|Tail],Num,C,MatchedNumber):- match(Tail,Num,N,Elem), C is N+1. In the first line I say, if the requested element number is equal to counter, then give...

Prefix in Prolog

list,prolog,prefix
I'm new in Prolog. I have a problem about predicate prefix but a little bit different. I want to get a prefix of a list but until an element The list can have repeat elements. An example: prefix(Element, List, Prefix) prefix(c, [a,b,c,d,e,f], [a, b]) The element is not included. What...

How to find if somebody is somebodies second cousin once removed in prolog?

prolog
I am writing a program to return true if a person is another persons second cousin once removed. The only information that is known is who is the parent of who else. I am using the family tree from here http://en.wikipedia.org/wiki/Cousin#Second_cousins_once_removed. Overall I got everything working but i cant make...

SWI Prolog pass a goal with non-zero arity through the command line arguments

bash,prolog,swi-prolog,logic-programming
SWI Prolog allows the execution of a goal before top level with non zero arity as follows swipl -s consultingfile.pl -g start however when a predicate with non zero arity is specified as follows swipl -s consultingfile.pl -g start(1) It gives an error syntax error near unexpected token '(' What...

Counting elements of a list in a predicate

prolog
I have something like this: plane(1, 2, aaa, b([1,2,3,4]), 3). I'm able to access the elements of plane and show it just like above, but the problem is b([1,2,3,4]). How can I access it to count the number of elements that is inside of that list?...

How do I append 3 lists efficiently in Prolog?

prolog
I know how to do it for 2 lists: append([],L,L). append([H|T],L,[H|R]):-append(T,L,R). but how to do it for 3? Without using the append for 2 lists twice....

Get operator operands

list,prolog,operators
I have list: List = [a=3, b=2, c=0]. For example, first element a=3, how can I get value Left = a, Right = 3. Left side is atom and right side is number....

How does pruning choice points in the code below make it more efficient (Prolog)?

prolog,prolog-cut
In the code given below, there is the ! (cut) that prunes the choice point for efficiency. I am pretty certain that the reverse predicate and the agent_do_moves predicate are essential. solve_task(Task,Cost):- agent_current_position(oscar,P), solve_task_a(Task,[b(0,0,P)],[],R,Cost,_NewPos),!, % prune choice point for efficiency reverse(R,[_Init|Path]), agent_do_moves(oscar,Path). ...

Freezing goal in prolog

prolog,primes,sieve-of-eratosthenes,prolog-coroutining
I want to freeze my goal until some variable, for example list, is unbounded, right now I have sieve(N,L) :- freeze(Aux,sieve(N,L,[],Aux)), numlist(2,N,Aux). sieve(N,L,R,[H|T]) :- freeze(X, X mod H =\= 0 ; X == H), findall(X,select(X,T,_),P), sieve(N,L,[H|R],P). sieve(_,L,L,[]). But it stop after some operations and waits forever. Could someone tell me...

make all elements in list equal with minimal cost

list,optimization,prolog
I am trying to create a prolog program that allows to convert a list into a list with the same length consisting of only 1 element from the original list. This element must be chosen in such a way that a minimal number of elements from the original list needs...

`nth0/3` behaviour when N is unbound

prolog,swi-prolog,iso-prolog
If I type in SWI Prolog a "nth0" query, the result is: ?- nth0(N,X,a). N = 0, X = [a|_G282] ; N = 1, X = [_G281, a|_G285] ; N = 2, X = [_G281, _G284, a|_G288] ; ... etc however, the SWI manual says: Errors type_error(integer, Index) if Index...

Prolog binding arguments

prolog,meta-predicate
In sicstus prolog, there's a predicate: maplist(:Pred, +List) Pred is supposed to take just one argument - List element. How can I pass a 2-argument predicate, with first argument defined? In other languages it would be written as: maplist(pred.bind(SomeValue), List) ...

How can I make Prolog speak?

prolog,text-to-speech
I have a list which holds some string values. A = [John, goes, to, party]. How can I pass this text to speech? Can any one tell me how to do text to speech conversion in prolog?...

Is one serie of constraints a subset of the other?

prolog,constraints,subset
I have two series of constraints S and S', they describe possibly infinitely large sets. Say for example S: x <= 10 and y <= x and S': x <= 20 and y <= 20. Now I want to know if S is a subset of S'? I thought I...

List Read and Pass it to Right Clause Functor In Prolog

prolog
This is my code: functor1(Action,[Action|_]) :- functor2(container(Action)). functor1(Action,[_|OtherActions]) :- functor1(Action,OtherActions). functor2(container([Abc,Def])):- append([Abc],[Def],Z),write(Z). Now, when I try to query, functor1(X,[[first, sign],[second, sign]]). What I expect with this query is that firstly X should read X = [first,sign] and then pass to functor2's container so that my functor2 can come into play....

Unifying a list of goals in prolog

prolog
I have a list of goals that I would like to be unified at runtime such that I get a list of Variable, value pairs. How might I do this? Example db.pl: alpha(a). bravo(b). bravo(c). gs([alpha(X), bravo(Y)]). Interpreter: ?- [db]. ?- gs(L), solve(L, Out). L = [alpha(X), bravo(Y)]. Out =...

Logic Programming

prolog,clpfd
Time 5, 3, 8, 2, 7, 3, 9, 3, 3, 5, 7 You have to schedule players (which uses Time) to three different showers. Get the best solution. So far, my solution is: use_module(library(clpfd)). shower(S, E, D, Done) :- D = [5, 3, 8, 2, 7, 3, 9, 3, 3,...

Translating a list to another list in prolog

prolog
I Tried to write a simple code in Prolog which translate a list to another list. for instance, if we call listtrans([a,b,c],L), L will become [1,2,3]. (a,b,c is replaced with 1,2,3). But i faced with a syntax error in last line. what is the problem? here is my code: trans(a,1)....

prolog rules as arguments

prolog,artificial-intelligence,expert-system
I'm building an expert system shell based on Luger & Stubblefield's ExShell. In their system, they define rules in the following way: rule((Goal :- (Premise)), CF_Rule). Ignore the CF_Rule. An example of this syntax is: rule((fix(Advice) :- (bad_component(X),fix(X, Advice))), 100). I want to add an OR in certain rules, but...

Different representation of line

prolog
I am learning the Prolog. I tried to write Anew is + (A is H1 * H2), instead of is(Anew,+(A,*(H1,H2))), but it produces an error. Where is my mistake? Can you help me? Thank you....

PROLOG: Summing up the series procedure

prolog
I have been trying to work on this problem that I found in a book, but am not able to make sense of it in my head. The problem asks me to use series(N, Total) for this procedure; 1 + 1/2 + 1/3 + 1/4 + ... + 1/(N-1). Any...

What is the meaning of (+,?,?) or (-,-,+) in Prolog? [duplicate]

prolog
This question already has an answer here: Meaning of instantiation mode indicators in arguments of Prolog predicates 2 answers Sometimes when I read some samples in Prolog I can see it has said that "this code is in the form of (+,?,?)". So, what is the meaning of these...

prolog need to compute the tree size

prolog,size,nodes
I need to get the size of a tree using: size(Tree,Size) What I have so far is wrong, please advise! size(empty, Size). size(tree(L, _, R), Size) :- size(L, Left_Size), size(R, Right_Size), Size is Left_Size + Right_Size + 1. Output should produce: ?- size(node(1,2),X). X = 2. ?- size(node(1,[2,3,4]),X). X =...

ANSI escape characters in gprolog

prolog,escaping,iso-prolog
Trying to print bold and underlined text in prolog but can't write them write('\033[1mbold\033[0m') Makes this (expected) error: syntax error: \ expected in \constant\ sequence What's the correct way to do it with gprolog ? Maybe with format ?...

Never-ending fill method, infinite loop that returns answer but doesn't exit

list,prolog
So I'm working on some prolog,and have been running into an issue that I don't understand why is appearing. The issue actually happens in a few of my methods, but hopefully I can figure it out with just some guidance in this method. fill(3,a,L) -> L = [a,a,a] here's my...