How do I best represent this operation in prolog

This is a discussion on How do I best represent this operation in prolog within the PROLOG forums in Programming Languages category; 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....

Go Back   Application Development Forum > Programming Languages > PROLOG

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 06-27-2008, 02:40 PM
ashley.fernandes@gmail.com
Guest
 
Default How do I best represent this operation in prolog

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.
Reply With Quote
  #2  
Old 06-27-2008, 04:06 PM
ashley.fernandes@gmail.com
Guest
 
Default Re: How do I best represent this operation in prolog

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?
Reply With Quote
  #3  
Old 07-04-2008, 09:47 AM
Nick Wedd
Guest
 
Default Re: How do I best represent this operation in prolog

In message
<ebefb380-eea5-4386-8343-4e0bf765b479@k37g2000hsf.googlegroups.com>,
ashley.fernandes@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
>
>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.


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 nick@maproom.co.uk
Reply With Quote
  #4  
Old 07-06-2008, 03:43 AM
Chip Eastham
Guest
 
Default Re: How do I best represent this operation in prolog

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

>
> >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.

>
> 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 * *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-Pro...exception.html

regards, chip
Reply With Quote
  #5  
Old 07-07-2008, 02:27 PM
ashley.fernandes@gmail.com
Guest
 
Default Re: How do I best represent this operation in prolog

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

>
> > >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.

>
> > 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 * *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


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.
Reply With Quote
  #6  
Old 07-07-2008, 03:51 PM
Joachim Schimpf
Guest
 
Default Re: How do I best represent this operation in prolog

ashley.fernandes@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
Reply With Quote
  #7  
Old 07-07-2008, 04:19 PM
ashley.fernandes@gmail.com
Guest
 
Default Re: How do I best represent this operation in prolog

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

>
> > >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.

>
> > 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 * *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



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.
Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 04:00 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.