1

I want to use plunit as implemented in SWI Prolog to run a few unit tests in classy style!

So I want to enter them directly on the terminal:

[user].

and then copy paste:

:- begin_tests(exercise).

test(foo) :- bar.

:- end_tests(exercise).

This works perfectly.

... but if I load the test via [user]. again -- there is a Westworld-like experience:

?- [user].
|: :- begin_tests(exercise).

ERROR: user://2:17:
ERROR:    module/2: No permission to redefine module `plunit_exercise' (Already loaded from user://1)
Warning: user://2:17:
Warning:    Goal (directive) failed: user:begin_tests(exercise)

It seems that the implementation creates a Module plunit_X for test X. That makes sense.

But is there a way to unload the test module exercise?

There must be...

Note that you can unload a file with unload_file/1 but not a Module?

Update:

Instead of entering the code using [user]. and using a [file] makes it work. Hmm.

false
  • 10,264
  • 13
  • 101
  • 209
David Tonhofer
  • 14,559
  • 5
  • 55
  • 51
  • I suppose I will raise this question on Discourse a bit later. It should be possible to unload a Module in any case, especially if a program uses it as fact store that it wants to dump wholesale later (ok, it _could_ use `retractall/1` but why bother?). This raises a lot of questions on transactionality, but you _can_ unload a file, so these seem to have been solved? – David Tonhofer Mar 11 '20 at 23:58

1 Answers1

1

Since I don't know how to reproduce your problem you will have to test this for yourself.

Use destroy_module/1

Since it is not exported from the module modules my guess is that modules:destroy_module(X) should work.

Since it is not an exported predicate and not documented Caveat emptor


EDIT

Instead of entering the code using [user]. and using a [file] makes it work. Hmm.

user is a predefined module, file is not AFAIK.

?- current_module(X).
X = prolog ;
X = sysetm ;
X = user ;
X = predicate_options ;
X = base32 ;
X = read_util ;
X = win_menu ;
X = shlib ;
X = qsave ;
X = prolog_clause ;
X = prolog_history ;
X = pce_swi_hooks ;
X = prolog_stack ;
X = system ;
X = ansi_term ;
X = link_xpce ;
false.
Guy Coder
  • 24,501
  • 8
  • 71
  • 136
  • Caveat [programmator](http://www.obta.uw.edu.pl/~draco/docs/voccomp.html)! Hic draci habitant. – David Tonhofer Mar 10 '20 at 22:19
  • @DavidTonhofer [Compilers: Principles, Techniques, and Tools](https://en.wikipedia.org/wiki/Compilers:_Principles,_Techniques,_and_Tools) - There be dragons too! – Guy Coder Mar 10 '20 at 22:55
  • Would you believe I have edition of 1986 (reprinted with corrections March 1988) ... copyright Bell Telephone Labs. Never managed to read through it :-( – David Tonhofer Mar 11 '20 at 13:03
  • 1
    @DavidTonhofer `Would you believe ` Of course I would. I actually took a parsing class before the first of the Dragon books were published and believe me, those books made a dramatic change to how much accessible parsing became for programming. Granted many still think Regular Expressions are equivalent to parsing, but access to proper parsing knowledge is better than it was 40 years ago. – Guy Coder Mar 11 '20 at 13:07