| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
| |||
| |||
| 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. |
|
#2
| |||
| |||
| 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? |
|
#3
| |||
| |||
| 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 |
|
#4
| |||
| |||
| 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 |
|
#5
| |||
| |||
| 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. |
|
#6
| |||
| |||
| 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 |
|
#7
| |||
| |||
| 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. |
![]() |
| Thread Tools | |
| Display Modes | |
In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.