Re: SWI-prolog, XPCE help

From: Henrik ?stman (trycoon2_at_hotmail.com)
Date: 10/21/04


Date: 20 Oct 2004 18:23:06 -0700

Jan Wielemaker <jan@ct.xs4all.nl> wrote in message news:<slrncncp3u.ov2.jan@ct.xs4all.nl>...
> In article <909d9f43.0410200402.79d80bd5@posting.google.com>,
> Henrik ?stman wrote:
> > My current program looks like this(almost the same as you left me
> > with):
> >
> >:- use_module(library(pce)).
> >
> > jv_kontroll :-
> > send(new(jv_kontroll), open, point(0,0)).
> >
> > :- pce_begin_class(jv_kontroll, dialog).
> > initialise(P) :->
> > send_super(P, initialise, 'JV-kontroll'),
> > send(P, display, new(Box1, box(75, 64)), point(110,
> > 48)),
> > send(P, display, new(Box2, box(75, 64)), point(110,
> > 120)),
> > send(Box1, colour(lightgrey)),
> > send(Box1, fill_pattern, colour(grey)),
> > %send(Box1, radius, 10),
> > send(Box2, colour(red)),
> > send(Box2, fill_pattern, colour(red)),
> > %send(Box2, radius, 10),
> > send(Box1, name, box1),
> > send(Box2, name, box2),
> > send(P, display, button(switch), point(110,220)).
> >
> > switch(P) :->
> > "Kallas när man trycker på knappen"::
> > get(P, member, box1, Box1),
> > get(P, member, box2, Box2),
> >
> > % pseudo-code, for how i would like it to work..
> > %if(get(P, Box1, colour(white)))
> > % {
> > % send(Box1, fill_pattern, @nil),
> > % send(Box2, fill_pattern, colour(red).
> > % }
> > %else
> > % {
> > % send(Box1, fill_pattern, colour(white)),
> > % send(Box2, fill_pattern, @nil).
> > % }
> > % if the colour "@nil" does'nt work, use colour(grey)
> >
> > if(get(Box1, colour, 'white')), % If box if
> > "white".
> > send(Box1, fill_pattern, @nil), %Then, make
> > that box invissible.
> > send(Box1, fill_pattern,
> > colour(white)).%Else, make that box "white".
> > :- pce_end_class.
> >
> > What's wrong with the IF-statement? And how do i process more than
> > only one line of code in the "Then" and "Else"-section? Do i have to
> > call yet two other methods just to execute two lines of code?(does'nt
> > seem right).
>
> Various things are wrong with it. First of all the use of if(...). I
> do not know from where you get it, but I suspect you've looked at the
> XPCE manual decribing the class 'if'. The body of a method however is
> executed in _Prolog_ and therefore you must use Prolog code to express
> it. Prolog if-then-else is expressed as (If -> Then ; Else) and you
> find the details in the Prolog manual or (better) a Prolog textbook.
>
> So, you get (for example)
>
> ( get(Box, colour, colour(green))
> -> send(Box, colour, colour(red))
> ; send(Box, colour, colour(green))
> )
>
> Now you must be a bit careful with this as colour(green) actually creates
> a colour _object_ and there are some potential problems with identity
> and equality around the corner. For this particular example however,
> it works. In general however you'd have to deal with this. Here is
> an alternative:
>
> ( get(Box, colour, Old),
> get(Old, name, green)
> -> send(Box, colour, red)
> ; send(Box, colour, green)
> ).
>
> Actually the two action (send(..)) lines are the same as above and
> either form can be used in both examples. If 'green' is sent to a
> method that only accepts objects of type colour the system asks
> class colour to convert 'green' into an object described by it.
>
> Of course you can also query the RBG value, etc. In many applications
> it is better to keep the state represented seperately and in some more
> meaningful way than colours.
>
> Finally, IF gets back to XPCE's notion of executable code that can
> be passed and stored in objects. So, you can also write the code
> below, which doesn't touch Prolog at all if you click the button.
>
> send(P, display, new(B, box(100,100))),
> send(B, colour, green),
> ...,
> send(P, display,
> button(switch,
> if(B?colour?name == green,
> message(B, colour, red),
> message(B, colour, green)))),
> ...
>
> --- Jan

Thanks again Jan!

Yes, you are right, i found the IF-class in the XPCE-manual and
thought i could use it for the program. I think i have to study some
more Prolog because i seam to lack some fundamental knowlage.. =)
Have you ever considered writing a book about Prolog or XPCE?? I have
learned more by having this diskution with you than reading all the
e-books that i have!
I used your second if-example since it looked more prologish and
"right" than the last one. Thanks!

// Henrik


Quantcast