prolog

Complexity in Prolog programs?

In Prolog, problems are solved using backtracking. It's a declarative paradigm rather than an imperative one (like in C, PHP or Python). In this kind of languages is it worth to think in terms of complexity? The natural way you think problems seems to be O(N^2) as someone pointed in this question. ...

Prolog list question

I have a database consisting of the following rules; speaks(fred [german, english, dutch]). speaks(mary [spanish, arabic, dutch]). speaks(jim [norwegian, italian, english]). speaks(sam [polish, swedish, danish]). etc As part of a much larger program, how would I find out 3 people who speak the same language? Jen ...

SWI Prolog - conditional NOT?

Hi, I'm trying to make a prolog function. The funtion reads in a sentance, and then tries to extract a key word. If a key word is found, it prints a message. I want it to also print a message if no keywords are found. Here is my example : contains([word1|_]) :- write('word1 contained'). contains([Head|Tail]) :- Head \= word1, contains...

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) :- ...

members predicate in Prolog

I'd like to define a members predicate. members(A, B) means that all members of the list A are members of list B. top(N) defines how long A can be. This is my try: top(5). members([X], L):- member(X, L). members([X| Xs], L):- member(X, L), members(Xs, L), length(Xs, M), top(N), M < N. I'd like to use it as follow: members(L, [1,2,...

Parsing with DCGs in Scheme (without Prolog)?

Lots of Prolog-in-Scheme implementations are out there. E.g. Kanren, Schelog. Apparently in "Paradigms of AI Programming" Norvig implements Prolog-to-Lisp compiler in Lisp in order to use Definite Clause Grammars. But is there a simpler cleaner way? Maybe some clever use of amb to avoid implementing a full "Prolog"? What is the easiest...

Two clause definition to find the maximum number on a list.

How would I write a two clause recursive definition to find the maximum value in a list. So far I have written this: max(L,M):- max([H|T],M):- max(T,H,M). max([],M,M). max([H|T],Y,M):- H =< Y, max(T,Y,M). max([H|T],Y,M):- H > Y, max(T,H,M). This doesn't work, it says there is a syntax error which I can't quite see,...

Embedded Prolog Interpreter/Compiler for Java

I'm working on an application in Java, that needs to do some complex logic rule deductions as part of its functionality. I'd like to code my logic deductions in Prolog or some other logic/constraint programming language, instead of Java, as I believe the resulting code will be significantly simpler and more maintainable. I Googled for e...

Using And clause in HEAD of a prolog statement.

Every member of this club is either educated or rich or both. I wanted to write a statement very similar to above in my PROLOG code. I wrote everything else. edu(X);rich(X) :- member(X). This is what I had written. But then PROLOG does allow any operators in the head clause. I have spent 5 hours till now trying to do various things...

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...

Problem with integer division opertor in Prolog

This code is a part of the program that I am writing to solve a cryptarithmatic puzzle in prolog. I am solving this using CLPFD (SICStus Prolog). I am getting an error while using the inbuilt integer division operator "//" (See Below for details). I was not able to solve this So I wrote my own integer division function. What I want to k...

Java and Prolog - Interprolog help

Hello I am currently trying to set up interprolog(see http://www.declarativa.com/interprolog/) and i've followed the steps on the site, but now I'm wondering how i can use interprologs classes from my eclipse ide? Do i need to import the classes into my project so i can use interprolog?? How can i go about doing so? Thanks ...

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...

How to define a predicate in prolog

I am new to Prolog and I have so far learned how to define a predicate in a file and the run the interpreter to use it. But I would like to know if there is a way to define the predicate at the ?- prompt so that I don't have to switch back and forth. the way I am doing it now is like this file defs.pl: adjacent(1,2). adjacent(1,3). ...

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), ...

Whats wrong with this version of functor(Prolog)?

I tried to write functor inbuilt in Prolog. This is my version: lenlist(L,N) :- lenlist(L,0,N). lenlist([],N,N). lenlist([_|T],P,N) :- P1 is P+1 , lenlist(T,P1,N). functor1(Term,F,N) :- Term =.. [F|Args] , lenlist(Args,N). Here is a sample execution of prolog inbuilt functor ?- functor(Term,f,6). Term = f(_G247, _G248, _G249, _G25...

N-Queens Problem..How far can we go?

The N-Queens Problem: This problem states that Given a chess board of size N by N. Find the different permutations in which N queens can be placed on the Board without any one killing each other. My question is: What is the maximum value of N for which a program can calculate the answer in reasonable amount of time? Or what is the lar...

Prolog difference routine help!

I need some help with a routine that I am trying to create. I need to make a routine that will look something like this: difference([(a,b),(a,c),(b,c),(d,e)],[(a,_)],X). X = [(b,c),(d,e)]. I really need help on this one.. I have written a method so far that can remove the first occurance that it finds.. how ever I need it to remove...