swi-prolog

How to use JPL (bidirectional Java/Prolog interface) on windows?

I'm interested in embedding a Prolog interpreter in Java. One option is using JPL, but the download links on the JPL site are broken, and the installation page mentions a jpl.zip that I can't find. I downloaded SWI-Prolog which seems to include JPL (it lists it as a component when installing), but I'm still not sure how I'd use it along ...

Calling the listing function in jpl

Hello, I was wondering whether anyone had managed to use the 'listing.' command in JPL to examine the contents of the Prolog knowledgebase? JPL requires you construct queries and will return solutions based on the variables which you set in the query. For example (Java): Query q = new Query("holdsAt((X,Y) = true, 3)"); while ( q.hasM...

Counting Sublist Elements in Prolog

How can I count nested list elements in prolog? I have the following predicates defined, which will count a nested list as one element: length([ ], 0). length([H|T],N) :- length(T,M), N is M+1. Usage: ?- length([a,b,c],Out). Out = 3 This works, but I would like to count nested elements as well i.e. length([a,b,[c,d,e],f],Output...

Prolog Operator =:=

Hi, There are some special operators in Prolog, one of them is "is", however, recently I came across the =:= operators, and I have no idea how it works. Can someone explain what the operator does, and also where can I find a predefined list of such special operators and what they do? Thanks. ...

Is there an alphabetical character check in prolog?

Greetings, Is there was test or a predicate I can use in prolog to verify that a certain given character is alphabetical? Right now, what I'm doing is: List of unallawed characters: \n -> 10, space -> 32, !->33, .->46, ,->44, :->58, ;->59% % 63->? , 45 -> -, 34->", 39-> % \+memb...

How to expand a resulting list in SWI-Prolog?

?- length(L,25). L = [_G245, _G248, _G251, _G254, _G257, _G260, _G263, _G266, _G 269|...]. If I use write(L) following the length predicate then the interpreter prints the list twice, one expanded and the other not. ...

Simple graph search in Prolog

Hi, I'm trying to code a simple graph search in SWI-Prolog. I came up with the following program: adjacent(1,4). adjacent(4,2). adjacent(3,6). adjacent(6,4). adjacent(7,10). adjacent(4,9). adjacent(5,10). adjacent(7,10). adjacent(7,12). adjacent(6, 11). adjacent(13,11). adjacent(9,14). adjacent(14,12). adjacent(10,15). adjacent(9,12). ...

prolog cut off in method

I have a question I would like to ask you something about a code snippet: insert_pq(State, [], [State]) :- !. insert_pq(State, [H|Tail], [State, H|Tail]) :- precedes(State, H). insert_pq(State, [H|T], [H|Tnew]) :- insert_pq(State, T, Tnew). precedes(X, Y) :- X < Y. % < needs to be defined depending on problem the function qui...

Prolog Question

hill(+IntList) succeeds if IntList consists of monotonically increasing integers followed by monotonically decreasing integers. For example, [1,2,5,8,11,6,3,-1] is a hill, but [1,2,5,8,11,6,9,3,-1] and [1,2,3,4,5,6] are not hills. You may assume that IntList contains only integers. This is what i have done so far. hill(List) :- incr...

SWI-Prolog conditional statements

I'm trying to write a function that will test to see if the word hello is contained in a list. If it is contained, i don't want it to say "true", i want it to say : "yes, the word hello is contained here", any ideas? Here's my code : contains_hello([hello|_]). contains_hello([Head|Tail]):- Head \= hello, contains_hello(Tail). ...

Given [1,2,3] in prolog get back [6,5,3] by reverse accumalation

Q. Given [1,2,3] in prolog get back [6,5,3] by reverse accumalation I have the start code: accumalate([H],[H]). accumalate([H1 | H2], [Hnew, H2]), Hnew is H1 + H2. .... I am looking for basic prolog solution. thanks guys ...

Working with lists in Prolog

First off let me state that this is part of a class exercise given as homework. But, the entire assignment is much more involved than the subject of this question. So.. I am searching through two lists given to a predication. My goal is to compare the corresponding elements in this list and determine if the first one is larger. If i...

Prolog : Learning by example

I am trying to learn a little bit about swi-prolog (beyond the basic, useless programs). Can anyone explain (perhaps in pseudocode) what this sudoku solver and the related functions are doing? If you need more reference it is found in the CLP(FD) package of swi-prolog. Thanks! | sudoku(Rows) :- ...

substitute in a nested list (prolog)

/* substitute(X,Y,Xs,Ys) is true if the list Ys is the result of substituting Y for all occurrences of X in the list Xs. This is what I have so far: subs(_,_,[],[]). subs(X,Y,[X|L1],[Y|L2]):- subs(X,Y,L1,L2). subs(X,Y,[H|L1],[H|L2]):- X\=H, not(H=[_|_]), subs(X,Y,L1,L2). subs(X,Y,[H|_],[L2]):- X\=H, H=[_|_], subs(X,Y,H,L2). My code w...

Compute a list of distinct odd numbers (if one exists), such that their sum is equal to a given number.

:- use_module(library(clpfd)). % load constraint library % [constraint] Compute a list of distinct odd numbers (if one exists), such that their sum is equal to a given number. odd(Num) :- Num mod 2 #= 1. sumOfList([],N,N) :- !. sumOfList([H|T],Counter,N) :- NewN #= H + Counter, sumOfList(T,NewN,N). buildOddList(N,InputList,L) :- ...

find the second largest element in the list

I know how to find the largest element of the list no problem, but how should I go about finding the second larget element? Say the predicate is secondlargest(+List,?Val) and succeeds if Val is the second largest element in the List. If there is a tie for largest, then second largest is the same as largest... I'm quite new to Prolog, ca...

checking if a tree is a heap [in prolog]

I need some help writing a predicate heap(Tree) in prolog that succeeds if Tree is a heap that satisfies both the heap property and the shape property: The shape property: the tree is an almost complete binary tree; that is, all levels of the tree, except possibly the last one (deepest) are fully filled, and, if the last level of the t...

Prolog Beginner

I'm trying to make predicates such that for lists X and Y, rmlast is true if X and Y are the same list except for the fact that Y has 1 element more. So [1] [1,2] or [3,2] [3,2,5]. rmlast([], [_]). :- true. rmlast([X|xtail], [Y|ytail]) :- rmlast(xtail,ytail), X is Y. This however, produces a false result for anything other than the ba...

Reading a string (from a file) in Prolog

I have written a lexer and a parser in Prolog. It unifies a string with its AST. This is part for a compiler/interpreter project I am working on. Naturally, I now want to read the string from a file to parse it. However, the predicates I have found for this is read, and it only reads Prolog atoms and predicates, like files with hello. ...

mysql connection fails after 3000+ attempts

Hi, I was just testing a small script in Prolog to sanity check the MySQL connection. The connection fails randomly, after making around 3000+ connections. Is there any limitation in MySQL Server for number of connections :-dynamic db_connection/1. sanity_check_open_db:- odbc_connect('myDSN', _, [ user(bob), ...