|
![](/i/fill.gif) |
On 31/05/2012 11:00 AM, Invisible wrote:
> Right now I want to talk about what I've produced.
>
> It's a small program called Logic Box. It lets you do logic programming,
> after a fashion.
Attached to this message is version 2, release #1 of Logic Box.
It's a single JAR file. If you double-click it, the program should fire
up. You'll probably need Java 6 or newer.
(I developed it with NetBeans and JDK 6. It works fine on my Windows
box, but I can't guarantee it will work elsewhere. You know how it goes;
Write Once, Test Everywhere(tm)...)
As I explained at length in my previous message, Logic Box is a
predicate solver. If you type a predicate into the top pane and jab
"solve", Logic Box will attempt to solve it.
If the predicate is unconditionally true (e.g., 2 = 2), you will get one
empty solution. If the predicate is unconditionally false (e.g., 7 = 5),
you will get zero solutions. But predicates can have any number of
solutions, and Logic Box will try to find them all.
By default, Logic Box tries to find 10 solutions, and then stops. Jab
"more" to ask for the next 10. As a safety feature, Logic Box stops
searching after 1,000 processing steps. (This is mainly so that if you
write a dud predicate, you don't hang the whole program.) Again, jab
more to let it run for another 1,000 steps; occasionally a valid
predicate takes that long.
(Note: The Results pane always shows you the predicate that the
solutions were generated from, in case you edited the predicate text and
can't remember if you pressed Solve yet. The More button always
generates more solutions for the predicate shown in the Results pane,
not the one in the Input pane.)
The main feature of Logic Box, however, is that it shows you what the
solver is actually /doing/ when it tries to solve a predicate. I'm not
completely happy with the output, but it works after a fashion.
Pressing "solve" just prints out the solution(s), if any exist. To do an
interactive session, click "interactive". You should see the Results
pane go blank, and the State pane fill up with a description of the
top-level predicate. (The Results pane will say "0 solutions"; that's
just how many solutions have been found /so far/.) You can now jab Step
to walk through the solution process. I won't attempt to describe it in
detail; hopefully everything is moderately self-explanatory.
For example, the default predicate [which appears when you start the
program] is "x = 1 & y = 2 | x = y & y = 5". Remember AND before OR, so
that parses as ((x = 1) & (y = 2)) | ((x = y) & (y = 5)). The top-most
predicate, then, is OR. So you get something like
OR:
Predicate #1: (x = 1) & (y = 2)
Predicate #2: (x = y) & (y = 5)
When you jab Step, predicate #1 starts running. The top-most predicate
here is AND, so we get something like
AND:
Predicate #1: x = 1
Predicate #2: y = 2
These are both "equal" predicates, so we begin with
Equal:
Raw LHS: x
Raw RHS: 1
Cooked LHS: x
Cooked RHS: 1
By settings x := 1, the cooked LHS changes from x to 1, which then
matches the cooked RHS, solving the predicate. y = 2 is dealt with
similarly, yielding solution #1. (You can watch it bubble up through the
chain of predicates.)
The second branch is more interesting. Because OR is at the top, we
start again with an empty set of bindings (rather than keeping the ones
from the previous solution). The first predicate is x = y, which yields
x := y. Then the second predicate is y := 5. This gives us
x := y; y := 5;
You can then see a simplification step where the x := y is updated to x
:= 5 (because y := 5). This gives us solution #2.
The process is a bit slower than it could be, because "equal" always
generates exactly one solution, but AND and OR still have to test for
the possibility of multiple solutions.
If you press the "library manager" button, you can see a list of all the
named predicates currently defined. (LJoin{}, SMember{}, etc.) You can
add new predicates by typing one into the lower pane and clicking Add.
You can also click on an existing predicate to see its source code, or
to remove it with the Delete button. You can even rename an existing
predicate: Select it, edit the name in the text pane, Add it, and then
Delete the old one.
There are a few predicates which are hard-wired. You can delete them,
but you can never get them back again. (They're not definable from the GUI.)
You can hit Reset to reload the default library. There's also Load and
Save buttons so you can keep your own favourite predicates on disk. It's
just a standard text file, so you can write it using your favourite text
editor rather than the crappy Java Swing text field if you wish. The
parser is a little fragile though. Also, there is no syntax for
inserting comments! (Note that loading and then saving a file /will/
mangle any brackets or white-space you might have added for clarity.)
OK, well, I think that's just about it... As I say, it's taken me about
a month to write this. (In the good old "iterative prototype" model of a
software life-cycle.) I'd like to say "I hope you like it", but I doubt
anybody will actually play with it for more than 15 seconds...
Post a reply to this message
Attachments:
Download 'logicbox-02 release1.jar.zip' (196 KB)
|
![](/i/fill.gif) |