I have an init predicate which asserts some facts. I would like to clean these up after execution.

I'm running queries of the form init, do_stuff, cleanup.. I would rather not have a separate cleanup call if i can avoid it (because I forget to call it).

I found this as a solution:

    assert(foo); not(retract(foo)).

Are there cases I would not want to do this or reasons why I shouldn't?

  • 1
    \$\begingroup\$ Just warning you prolog is not very active on this site. Only four questions on it were asked since January... \$\endgroup\$ – fartgeek Nov 6 '20 at 21:02
  • \$\begingroup\$ Wonder whether we can add it to the iftt bot that posts to Twitter. I will accept comments years from now as well :) \$\endgroup\$ – 2bigpigs Nov 7 '20 at 16:51
  • 2
    \$\begingroup\$ Why not use setup_call_cleanup and create a meta predicate? run_query(Init_fact, Goal) :- setup_call_cleanup(assert(Init_fact), Goal, retract(Init_fact)). \$\endgroup\$ – rajashekar Nov 8 '20 at 2:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.