Java is a silly language
Aug. 3rd, 2007 04:44 pm![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
People are always telling me that my usual languages, like Prolog, are silly or, at the very least, impractical, but Java has just got me flummoxed.
I have a list (of logical formulae as it happens) and I want to know if my agent believes everything in the list. To complicate matters the the list can contain variables so the agent might believe them in different ways. So my list might be - do I believe there is something, x say, such that 1) x is blue and 2) x has four legs and the answer might be "blue cow is blue and blue cow has four legs"* but I might also believe that the sky is blue and any number of other things are blue. The object is to find something that I believe is blue and which I also believe has four legs.
This is incredibly easy in Prolog. I just go through the list of formulae one at a time creating a candidate answer, if the answer fails at any point then the programming language automatically back tracks up the list looking for alternatives.
Java doesn't do back tracking. I'm going to have to do something complicated involving keeping track of where I am in the list and which of the alternative candidates I have explored and then zipping back and forth in a sensible fashion. I've appealed to B. who is supposed to be a bit of a dab hand with imperative languages and he looked a bit blank and then started talking about custom iterators.
* with apologies to those of you don't watch CBeebies.
I have a list (of logical formulae as it happens) and I want to know if my agent believes everything in the list. To complicate matters the the list can contain variables so the agent might believe them in different ways. So my list might be - do I believe there is something, x say, such that 1) x is blue and 2) x has four legs and the answer might be "blue cow is blue and blue cow has four legs"* but I might also believe that the sky is blue and any number of other things are blue. The object is to find something that I believe is blue and which I also believe has four legs.
This is incredibly easy in Prolog. I just go through the list of formulae one at a time creating a candidate answer, if the answer fails at any point then the programming language automatically back tracks up the list looking for alternatives.
Java doesn't do back tracking. I'm going to have to do something complicated involving keeping track of where I am in the list and which of the alternative candidates I have explored and then zipping back and forth in a sensible fashion. I've appealed to B. who is supposed to be a bit of a dab hand with imperative languages and he looked a bit blank and then started talking about custom iterators.
* with apologies to those of you don't watch CBeebies.
(no subject)
Date: 2007-08-03 04:05 pm (UTC)Personally Prolog is one of my favourite languages, and one day i'm going to try and write something in LISP. Now there is a language that Java coders just don't get.
(no subject)
Date: 2007-08-03 04:09 pm (UTC)"You learned functional programming in LISP didn't you"
... and indeed my first ML program was a complete bracket fest. I've calmed down a bit now but there are still rather more brakets in my ML programs than strictly necessary, I can't quite get it out of my head that I need to be really clear exactly what goes with what.
On the whole I'd recommend ML over LISP. It has types and types are always worth getting your hands on. One of the reasons I rather liked lambda-Prolog was that it had types.
(no subject)
Date: 2007-08-03 04:31 pm (UTC)Thankfully I don't have to use Java.
But I do use Perl and Javascript and those are really fun kettles of whale guts.
(no subject)
Date: 2007-08-04 09:03 am (UTC)for x in list_of_things:
if all(b.test_belief(x) for b in list_of_formulae):
do_something_with(x)
break # if you only are looking for the first matching x
Hmmm, I don't know Java very well but doesn't it have the 'break' and 'continue' statements from C?
If you have an outer loop over things and an inner one over formulae, then 'break' out of the inner one
if a test returns false. After the inner loop, check if the last test was false or true, if false, then 'continue'
to the next x.
Ah, reading your description, it looks like you have the outer loop over the tests and the inner loop over the things? Is that neccessary? If so you need to keep a list of things that have passed all the tests so far, which, for each test, you go through and remove those that fail the test. At the end you are left with a list of all the things that have passed all the tests. Java must have lists, doesn't it?
(no subject)
Date: 2007-08-04 09:04 am (UTC)(no subject)
Date: 2007-08-06 04:31 pm (UTC)i.e.
ALL x in list. EXISTS sigma. believe(x.sigma)
so basically if I'm say I believe "sky is blue", "blue cow is blue", "blue cow has four legs" and I'm looking to see if I believe
EXISTS x. "x is blue" AND "x has four legs"
then I loop
believe?: x is blue
candidate: x = sky
believe?: x has four legs (when x = sky)
FAIL.
back track
believe? :x is blue
next candidate: x = blue cow
believe?: x has four legs (when x = blue cow)
SUCCESS
end loop
I've realised I need to implemented depth-first search in Java, its now a question of the most efficient way to do that with the iterators I already have for checking "believes" and returning candidates.