Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How do I best represent this operation in prolog

1 view
Skip to first unread message

ashley.f...@gmail.com

unread,
Jun 27, 2008, 2:40:48 PM6/27/08
to
What's the best way to represent the following logic in Prolog?

failed = false
try doing a
if a succeeded
try doing b
if b failed
failed = true
clean up after b
else
failed = true
clean up after a
return failed

I'm having trouble w/ having a's clean up run and propagating the
failure to the caller at the same time.

Thanks in advance.

ashley.f...@gmail.com

unread,
Jun 27, 2008, 4:06:11 PM6/27/08
to
Forgot to mention -- my attempt (in SWI Prolog) was:

#! /home/asfernan/pl/bin/pl -s

a(1).
b(2).

mainGoal:-
tryA(S),
format('success ~w\n', S).

tryA(S):-
( a(1) -> format('a succeeded\n'), tryB(S); S = 0, format('a failed
\n'), true ),
format('a cleanup\n').


tryB(S):-
( b(2) -> S = 1, format('b succeeded\n'); S = 0, format('b failed
\n') ),
format('b cleanup\n').

:- mainGoal, halt.

However, the use of S to propagate success/failure up seems a little
awkward. Is there a better way to do this?

Nick Wedd

unread,
Jul 4, 2008, 9:47:59 AM7/4/08
to
In message
<ebefb380-eea5-4386...@k37g2000hsf.googlegroups.com>,
ashley.f...@gmail.com writes

I find your question hard to understand. In particular, I don't know
what you mean by "clean up". Maybe it would be better if you explained
what you are trying to do, rather than presenting pseudocode.

This _may_ be what you want:

try( A, B, false ) :-
call( A ),
call( B ),
!.
try( _, _, true ).

Or if you can accept Prolog's success and failure, rather than the
clumsy (and confusing) device of return values of 'false' for success
and 'true' for failure, maybe this is all you need:

a, b.

Nick
--
Nick Wedd ni...@maproom.co.uk

Chip Eastham

unread,
Jul 6, 2008, 3:43:38 AM7/6/08
to
On Jul 4, 9:47 am, Nick Wedd <n...@maproom.co.uk> wrote:
> In message
> <ebefb380-eea5-4386-8343-4e0bf765b...@k37g2000hsf.googlegroups.com>,
> ashley.fernan...@gmail.com writes
> Nick Wedd    n...@maproom.co.uk

I also found the OP's description by pseudocode
confusing. Perhaps the mentions of "cleanup"
imply an exception handling mechanism:

[SWI-Prolog Exception Handling]
http://gollem.science.uva.nl/SWI-Prolog/Manual/exception.html

regards, chip

ashley.f...@gmail.com

unread,
Jul 7, 2008, 2:27:05 PM7/7/08
to

Sorry about the confusion. By cleanup, I meant remove resources
created by running a & b.

The operations were fetching a part of a code repository (operation a)
& then looking for some lines in a file in there (operation b).
Fetching the repository first creates a directory & looking for the
lines in the file involves writing out those lines to another file.
After executing operation a, whether it succeeds or not, I want to
remove the directory if it was created. Similarly, after executing
operation b, I want to delete the temporary file. What I want is to
imitate the finally block of Java's exception handling mechanism.
Finally, I want to pass success/failure to the caller. & yes, I want
to use Prolog's success/failure rather than a return value.

Also, I represented the operations as a(1) & b(2) just to try this
out.

Thanks once again. Hope this clears my question.

Joachim Schimpf

unread,
Jul 7, 2008, 3:51:04 PM7/7/08
to
ashley.f...@gmail.com wrote:
> On Jul 6, 3:43 am, Chip Eastham <hardm...@gmail.com> wrote:
>> On Jul 4, 9:47 am, Nick Wedd <n...@maproom.co.uk> wrote:
>>
>>
>>
>>> In message
>>> <ebefb380-eea5-4386-8343-4e0bf765b...@k37g2000hsf.googlegroups.com>,
>>> ashley.fernan...@gmail.com writes
>>>> What's the best way to represent the following logic in Prolog?
>>>> failed = false
>>>> try doing a
>>>> if a succeeded
>>>> try doing b
>>>> if b failed
>>>> failed = true
>>>> clean up after b
>>>> else
>>>> failed = true
>>>> clean up after a
>>>> return failed

>

> Sorry about the confusion. By cleanup, I meant remove resources
> created by running a & b.

You could do

(a ->
(b -> Res=true ; Res=fail),
cleanup_b
;
Res=fail
),
cleanup_a,
Res.


-- Joachim

ashley.f...@gmail.com

unread,
Jul 7, 2008, 4:19:13 PM7/7/08
to
On Jul 6, 3:43 am, Chip Eastham <hardm...@gmail.com> wrote:


Sorry I wasn't clear. & I thought I sent out this message, but it's
not showing on groups.google.com, so sorry if this is repeated.

Operation a: Fetch part of a code repository -- creates a directory
whether it succeeds or fails as a side effect
Operation b: Run a script to search for some lines in some files in
the part of the repository we fetched -- creates a temporary file
whether it succeeds or fails as a side effect

The cleanup consists of deleting the directory after trying operation
a & deleting the temporary file if we have tried operation b whether
they succeed or fail. Basically I'm trying to emulate Java's finally
block. I also want to pass success/failure to the caller, & yes, I
would prefer to avoid the true/false variable & use Prolog's success/
failure mechanism instead.

Also, I just used a(1) & b(2) to try out the control flow I thought
of.

Thanks again. I hope that makes my question clearer.

0 new messages