Simplest way to disable statement `must_be/2`

The fact that must_be/2

  • Succeeds with no further constraining of Term if Term is sufficiently instantiated to decide that it is a Type and is indeed a Type in must_be(+Type, @Term)
  • Otherwise throws.

practically commands that it be used only for computational state checks (in this case, whether the caller passed the correct goods) immediately at predicate entry; or maybe a bit further to the right to verify that things are still “on track”.

This also means that one should be able to disable must_be/2 in programs that are “sufficiently tested” without any change in semantics (unless the developer did something that he should not) – improving performance. Similar to disabling assertions in Java.

How can that be done simplest? Maybe with a specific expand_term/2 statement? It would not work for precompiled libraries though (are there any, even? Like .class files in Java?)

One could always filter the source code directly with a script of course. Perl scripts I can do.

SWI-Prolog does have Conditional compilation but I would not use that for the noted purpose as it would jut make the code that much harder to read.

If the must_be/2 is used during development to keep me from making dumb mistakes like passing an empty variable then I leave them in until I know the predicate is working for the production code then strip them out by hand.

If the must_be/2 is used for code that is checking for resources like a file or database connection that must exist for the code to work then I leave them in, even for the production code.

However if during development they are acting as a guard and I want the code to halt as soon as the problem is found but then work as guard with a silent fail once it is in production, I use must_be/2 during development and then change them to is_of_type/2 once the code is production quality.

There are more scenarios but you should start to get the idea that the goal is to get code that works correctly in production, what happens in development is not set in stone.

EDIT

The reverse process also has uses.

If production code has a problem or I did not write the code and want to understand it, I bring the code back in for development, change is_of_type/2 to must_be/2, add additional must_be/2 and other such changes.

1 Like

This should do the job (before loading the application):

user:goal_expansion(must_be(_,_), true).
3 Likes

Eric, can I borrow that text for the SWI-Prolog site comment box?

You caught me off guard with that one. I had to do mental a syntactic and semantic analysis on the sentence to understand it.

Yes, I consider everything I write in public forums to be

Attribution-ShareAlike
CC BY-SA

unless noted otherwise.