POV-Ray : Newsgroups : povray.off-topic : A silly little toy (~200 kb) : A silly little toy (~200 kb) Server Time
6 Sep 2024 21:18:42 EDT (-0400)
  A silly little toy (~200 kb)  
From: Invisible
Date: 15 Oct 2008 11:25:23
Message: <48f60b63@news.povray.org>
Hey people, check it out: Attached to this post is a small program I wrote.

(The attached binary works for M$ Windoze only. I'm not sure precisely 
which versions, but it DOES work for XP and DOES NOT work for NT 4.0. In 
principle it could be compiled for Linux or Mac OS X. The program is 
about 20 KB of Haskell source code.)

The program isn't terribly exciting; it's a predicate solver. That is, 
it attempts to solve extremely simple equations.

To run the program, double-click it. You should get a window of 
multicoloured terminal output, including a "Predicate>" prompt.

If you enter, say, "x = 1", then the program will respond that to solve 
this predicate, you have to set x equal to 1. (DUH!)

Alternatively, if you set "4 = 4", the program will find "1 solution", 
and that solution will be empty. This basically means that the predicate 
*always* holds. If you do "1 = 2", the program will report "no solutions 
found", indicating that the predicate can never be satisfied. So far so 
good.

If you say "x = 1 & y = 2" then the program will report a self-evident 
solution. If you say "x = 1 | y = 2" it will report two seperate 
solutions. And if you say "x = 1 & x = 2" it will fail to find any 
solutions (pretty obviously).

You can of course put predicates in brackets; by default, AND has a 
higher priority than OR. You can also prefix anything with "~" to 
indicate NOT (note that the engine doesn't support this quite so well).

The program understands (signed) integers and equality. It also 
understands lists. Try "{1, 2, 3} = {1, 2, 3}" and you will get an empty 
solution (i.e., the predicate always holds). Try "{1, 2, 3} = {3, 2, 1}" 
and you will get no solutions (i.e., the predicate never holds). You can 
also do "{x, y, z} = {1, 2, 3}", which yields a fairly obvious result.

Notice that, mathematically speaking, "x = 1" and "1 = x" are the same 
equation, and the program treats them identically.

The fun part begins when you start using the built-in predicate 
functions. There are currently just 4 of these; I plan to make it 
possible to add your own, but I haven't implemented this yet.

First, the rather dull "Empty" function tests whether a list is empty:

- "@Empty[{}]" will yeild an empty result set.
- "@Empty[{1, 2, 3}]" will give no results.

The "Element" function tests whether a given element is in a list:

- "@Element[5, {1, 2, 3}]" gives no results.
- "@Element[5, {4, 5, 6}]" gives an empty result set.

The weird part is that this works backwards as well as forwards:

- "@Element[x, {1, 2, 3}]" tells you all possible values for x that will 
satisfy the predicate!

Adventurers beware: Attempting to find all possible lists that 5 could 
be an element of will result in an infinite loop from which only Ctrl+C 
will escape.

Also of interest is the "Join" function:

- "@Join[{1, 2, 3}, {4, 5, 6}, {1, 2, 3, 4, 5, 6}]" gives an empty 
result set, indicating that the 3rd list is indeed the first two joined 
together.
- "@Join[{1, 2, 3}, {4, 5, 6}, z]" will set z to the join of the first 
two lists.

This also works backwards:

- "@Join[{1, 2, 3}, y, {1, 2, 3, 4, 5, 6}]" will find the correct value 
for y by stripping the first list off of the 3rd list.
- "@Join[x, {4, 5, 6}, {1, 2, 3, 4, 5, 6}]" works similarly.
- "@Join[x, y, {1, 2, 3}]" will find every possible way of splitting the 
final list into two sublists. How neat is that?!

Again, beware that other combinations may yield an infinite number of 
valid solutions [which will take an infinite amount of time to compute, 
and nothing is displayed until *all* solutions are fully computed].

Finally, there is the "Subset" function, which tells you if one set is a 
subset of another. For example, "@Subset[{1, 2}, {1, 2, 3}]". It 
_should_ also work backwards, but currently doesn't. I'm not sure why.

Anyway, it's an amusing little diversion...


Post a reply to this message


Attachments:
Download 'logic.exe.dat' (182 KB)

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.