info-gnuprologjava
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Info-gnuprologjava] problems running a simple quicksort example


From: Benjamin Brunzel
Subject: Re: [Info-gnuprologjava] problems running a simple quicksort example
Date: Fri, 19 Nov 2010 13:02:53 +0100
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.15) Gecko/20101030 Icedove/3.0.10

Hi,

Thanks again for your efforts. We found our mistake in our java file.
But it seems like there is another thing preventing us from running our
prolog quicksort.
We made some tests and it looks like there is a problem with our list
representation
in the java program.
We tried to run a simple single list rule in the prolog that looks like
this:

echo "quicksort([H|T],T)." >> quicksort.pl

Running this with our java file we get a return value of -1 which
implies that there
was no unification taking place.

Is it possible to use the [Head|Tail] notation in  gnu prolog for java?
Is our list representation in the java file correct?
Do you know what might be the problem?

Best regards,
Ben


The current Java File:

import gnu.prolog.term.*;
import gnu.prolog.vm.Environment;
import gnu.prolog.vm.Interpreter;
import gnu.prolog.vm.PrologException;
 
 
public class Test {
    public static void main(String[] args) throws PrologException {
        //initialisation
        Environment e = new Environment();
        e.ensureLoaded(AtomTerm.get("quicksort.pl"));
        Interpreter i = e.createInterpreter();
        e.runInitialization(i);
       
        //definition of the Terms
        VariableTerm result = new VariableTerm("R");
         
        IntegerTerm[] elements = new IntegerTerm[5];
        elements[0] = new IntegerTerm(5);
        elements[1] = new IntegerTerm(8);
        elements[2] = new IntegerTerm(1);
        elements[3] = new IntegerTerm(7);
        elements[4] = new IntegerTerm(3);

        // defining the list
        CompoundTerm list = new CompoundTerm(".", elements);
 
        Term[] arguments = {list, result};       
        //composing the request
        CompoundTerm request = new CompoundTerm("quicksort", arguments);
         
        System.out.println(result);
               
        //execute the request
        Interpreter.Goal goal = i.prepareGoal(request);
        int rc = i.execute(goal);
        //printing results
        System.out.println(rc);
        System.out.println(result);         
    }
}


On 11/13/2010 04:49 PM, Daniel Thomas wrote:
> Hello,
>
> That error implies that at some point your prolog code is trying to
> compare a variable which fails as a variable does not evaluate hence the
> instantiation error.
>
> I put:
> :- 1 < 2.
> :- X = 1, Y = 2, X < Y, Y > X, write(success).
> at the end of quicksort.pl to check that < and > are working correctly
> and it indicates that they are.
>
> Daniel
>
> On Fri, 2010-11-12 at 13:45 +0100, Benjamin Brunzel wrote:
>   
>> Hi,
>>
>> Thanks for your answer. As you mentioned there was a bug in the prolog
>> rules.
>> In line 1 of the prolog file quicksort(A,A). just returns the given
>> list. which is obviously wrong.
>> I corrected this and now it throws the exception described by me in the
>> last mail.
>>
>> /usr/lib/jvm/java-6-sun/bin/java -cp
>> '/tmp/test/gnuprologjava-0.2.5.jar:.' Test
>> ergebnis
>> Exception in thread "main" gnu.prolog.vm.PrologException:
>> error(instantiation_error,error)
>>         at gnu.prolog.vm.PrologException.getError(PrologException.java:129)
>>         at gnu.prolog.vm.PrologException.getError(PrologException.java:118)
>>         at
>> gnu.prolog.vm.PrologException.instantiationError(PrologException.java:145)
>>         at gnu.prolog.vm.Evaluate.evaluate(Evaluate.java:153)
>>         at
>> gnu.prolog.vm.buildins.arithmetics.Predicate_less_than.execute(Predicate_less_than.java:38)
>>         at
>> gnu.prolog.vm.interpreter.InterpretedByteCode.execute(InterpretedByteCode.java:522)
>>         at
>> gnu.prolog.vm.interpreter.InterpretedByteCode.execute(InterpretedByteCode.java:522)
>>         at
>> gnu.prolog.vm.interpreter.InterpretedByteCode.execute(InterpretedByteCode.java:522)
>>         at
>> gnu.prolog.vm.interpreter.Predicate_call.staticExecute(Predicate_call.java:144)
>>         at gnu.prolog.vm.Interpreter.execute(Interpreter.java:507)
>>         at Test.main(Test.java:37)
>>
>> the fixed prolog file is:
>>
>> quicksort([],[]).
>> quicksort([Kopf|Rest], SortierteListe):- teilen(Kopf, Rest, Kleiner,
>> Groesser),quicksort(Kleiner, Kleiner_Sortiert),quicksort(Groesser,
>> Groesser_Sortiert),append(Kleiner_Sortiert, [Kopf|Groesser_Sortiert],
>> SortierteListe).
>> teilen(_, [], [], []).
>> teilen(Element, [Kopf|Rest], [Kopf|Kleiner], Groesser):-Kopf < Element,
>> !,teilen(Element, Rest, Kleiner, Groesser).
>> teilen(Element, [Kopf|Rest], Kleiner, [Kopf|Groesser]):-teilen(Element,
>> Rest, Kleiner, Groesser).
>>
>> I think the error is related to the less than.
>> Prolog programs not using relations like greater than, less than, equal,
>> ... seem to work.
>> Do you know that error? Do you know a solution to that issue?
>>
>> Thanks,
>> Ben
>>
>>
>>
>> On 11/11/2010 07:54 PM, Daniel Thomas wrote:
>>     
>>> Hello,
>>> I have tried running your code on my machine and the results were as
>>> follows:
>>>
>>> address@hidden:~$ vim Test.java #I removed the line numbers and edited the 
>>> path to quicksort.pl
>>> address@hidden:~$ vim quicksort.pl
>>> address@hidden:~$ javac -cp 
>>> '/home/daniel/dev/gnuprolog/releases/0.2.5/gnuprologjava-0.2.5/gnuprologjava-0.2.5.jar'
>>>  Test.java
>>> address@hidden:~$ java -cp 
>>> '/home/daniel/dev/gnuprolog/releases/0.2.5/gnuprologjava-0.2.5/gnuprologjava-0.2.5.jar:.'
>>>  Test
>>> ergebnis
>>> 0
>>> .(5,8,1,7,3)
>>>
>>>
>>> What exception do you get? I think that ".(5,8,1,7,3)" is probably the
>>> wrong answer as it doesn't appear to be particularly sorted.
>>>
>>> There is one bug I have found in your prolog:
>>> teilen(Element, [Kopf|Rset], Kleiner, [Kopf|Groesser]):-teilen(Element,
>>>         Rest, Kleiner, Groesser).
>>> "[Kopf|Rset]" should probably be "[Kopf|Rest]"
>>>
>>> That doesn't fix the missorting though.
>>>
>>> Does this help you? There is a demo program in the demo directory of the
>>> release zip file.
>>>
>>> In 0.3.0 the return codes of Interpreter.execute(Goal) etc. will be
>>> changed to be an enum which will make System.out.println(rc); print
>>> something slightly more useful than 0;
>>>
>>>
>>> Daniel
>>>
>>> On Wed, 2010-11-10 at 14:51 +0100, Benjamin Martin Brunzel wrote:
>>>   
>>>       
>>>> Hi Folks,
>>>>
>>>> I am currently trying to run a simple quicksort example using gnu prolog
>>>> for java. I get an exception every time I  use a prolog file with more
>>>> then one rule in it.
>>>> I think the problem might be the representation of lists in a CompoundTerm
>>>> object.
>>>> Does anyone know what is wrong with it?
>>>> Is there a simple example program available?
>>>>
>>>> The Java Class:
>>>>
>>>>   1 import gnu.prolog.term.*;
>>>>   2 import gnu.prolog.vm.Environment;
>>>>   3 import gnu.prolog.vm.Interpreter;
>>>>   4 import gnu.prolog.vm.PrologException;
>>>>   5
>>>>   6
>>>>   7 public class Test {
>>>>   8
>>>>   9         /**
>>>>  10          * @param args
>>>>  11          * @throws PrologException
>>>>  12          */
>>>>  13         public static void main(String[] args) throws PrologException {
>>>>  14
>>>>  15                 Environment e = new Environment();
>>>>  16                
>>>> e.ensureLoaded(AtomTerm.get("C:\\Path\\to\\prolog\\files\\quicksort.pl"));
>>>>  17                 Interpreter i = e.createInterpreter();
>>>>  18                 e.runInitialization(i);
>>>>  19
>>>>  20                 VariableTerm ergebnis = new VariableTerm("ergebnis");
>>>>  21                 IntegerTerm[] elemente = new IntegerTerm[5];
>>>>  22                 elemente[0] = new IntegerTerm(5);
>>>>  23                 elemente[1] = new IntegerTerm(8);
>>>>  24                 elemente[2] = new IntegerTerm(1);
>>>>  25                 elemente[3] = new IntegerTerm(7);
>>>>  26                 elemente[4] = new IntegerTerm(3);
>>>>  27
>>>>  28                 CompoundTerm liste = new CompoundTerm(".", elemente);
>>>>  29
>>>>  30                 Term[] argumente = {ergebnis, liste};
>>>>  31
>>>>  32                 CompoundTerm anfrage = new CompoundTerm("quicksort",
>>>> argumente);
>>>>  33
>>>>  34                 System.out.println(ergebnis);
>>>>  35
>>>>  36                 Interpreter.Goal goal = i.prepareGoal(anfrage);
>>>>  37                 int rc = i.execute(goal);
>>>>  38                 System.out.println(rc);
>>>>  39
>>>>  40                 System.out.println(ergebnis);
>>>>  41
>>>>  42         }
>>>>  43
>>>>  44 }
>>>>
>>>> The Prolog File:
>>>>
>>>> quicksort(A, A).
>>>> quicksort([Kopf|Rest], SortierteListe):- teilen(Kopf, Rest, Kleiner,
>>>> Groesser),quicksort(Kleiner, Kleiner_Sortiert),quicksort(Groesser,
>>>> Groesser_Sortiert),append(Kleiner_Sortiert    , [Kopf|Groesser_Sortiert],
>>>> SortierteListe).
>>>> teilen(_, [], [], []).
>>>> teilen(Element, [Kopf|Rest], [Kopf|Kleiner], Groesser):-Kopf < Element,
>>>> !,teilen(Element, Rest, Kleiner, Groesser).
>>>> teilen(Element, [Kopf|Rset], Kleiner, [Kopf|Groesser]):-teilen(Element,
>>>> Rest, Kleiner, Groesser).
>>>>
>>>>
>>>> I used the version 0.2.5
>>>>
>>>> Please note that I am not on the mailing list so please cc me on your
>>>> answers.
>>>>
>>>> Thanks for your support.
>>>>
>>>>
>>>>
>>>>     
>>>>         
>>>   
>>>       
>>     
>   




reply via email to

[Prev in Thread] Current Thread [Next in Thread]