Page 1 of 1

What does this code do?

Posted: 1 May 2020 22:02
by Klim0
Hey guys,
Im looking for help here about this code:

circuit(b,a).
circuit(a,b).
circuit(c,b).
circuit(a,c).

path([]).
path([_]).
path[X,Y|Zs]):- circuit(X,Y), path([Y|Zs]).
path(X,Y).

What does this program do and how does it work? Please, help :/

Re: What does this code do?

Posted: 3 May 2020 2:33
by Martin Meyer
Hi Klim (and Thomas),

the short answer is: Your code throws a syntax error in Visual Prolog (build 904) and does not work at all.

More in detail, I have tried to fit the code into VIP's syntax (which differs from 'standard' Prolog) and let it do a computation which makes sense in somehow. To test my/your code create a new console application project and replace the contents of main.pro by:

Code: Select all

implement main   domains     smbl = a; b; c.   class predicates     smbl_nd : (smbl X) nondeterm anyflow. clauses     smbl_nd(a).       smbl_nd(b).       smbl_nd(c).   class predicates     circuit : (smbl X, smbl Y) nondeterm anyflow. clauses     circuit(b, a).       circuit(a, b).       circuit(c, b).       circuit(a, c).   class predicates     path : (smbl* Path) multi (o) nondeterm ([i|o]). clauses     path([]).       path([X]) :-         smbl_nd(X).       path([X, Y | Zs]) :-         circuit(X, Y),         path([Y | Zs]).       path([X, Y]) :-         smbl_nd(X),         smbl_nd(Y).   clauses     run() :-         foreach path(Path) do             stdIO::write("Path ", Path, " is a solution"),             _ = stdIO::readLine()         end foreach.   end implement main   goal     console::runUtf8(main::run).
Execute it (in 32bit mode) by typing Alt+F5. Each time when you hit Return, it computes a new solution. I.e. it computes another path which matches the constraints you have stated in the clauses of predicates path/1 and circuit/2. Abort the loop with Ctrl+C.

In 64bit mode however the code runs on an exception. Please, Thomas, have a look at that.

Re: What does this code do?

Posted: 4 May 2020 7:45
by Thomas Linder Puls
Thank you Martin, we will look at it.

Re: What does this code do?

Posted: 4 May 2020 8:21
by Thomas Linder Puls
Hi Klim.

As Martin explains the program is a "traditional Prolog program, not a Visual Prolog program.

Besides that the program validates path's in a little circuit(/graph). The last clause is however must have to do with something different.

The circuit predicate defines the circuit, in terms of the direct connections (edges) between some nodes (vertices). The first clause says that there is a connection from a to b, the second clause says there is a connection from b to a (so connections must be directed/one way), and so forth.

path takes (or gives) a list of nodes and determines (ensures) that those nodes constitute a path you can follow in the circuit. [a, b, a] is such a path because starting at a you can go b and from b you can go back to a.

The first clause in path says that the empty path is a path in the circuit.

The second clause says that if you are at a node and don't move then it is a path in the circuit.

The third clauses says that if you have a path starting in some node X and with Y as next node and some additional steps Zs. Then this path is a valid path, if there is a direct connection from X to Y, and furthermore that the path starting in Y and continuing with the steps Zs is also a valid path.

The in last clause path have two arguments, so even though it is called path it is actually another predicate. My guess is that it should just be deleted. And in any case it doesn't have any influence on the rest of the program.

For further understanding and learning Visual Prolog (and thereby to to some extend "traditional" Prolog) I suggest that you download Visual Prolog read the tutorials.