Convert app(_12282,_12284,_12286) to app(_,_,_) for printing

What converts app(_12282,_12284,_12286) to app(_,_,_)?

This is only needed for printing the predicate head.


Having seen this done many times it would seem that it should be easy to find but sadly I can not seem to find the predicate needed.

Also if someone knows the correct terminology for such that would help in locating the information again in the future.

After reading reply by Jan W. below.

For a predication use

?- functor(Predication,app,3),numbervars(Predication,0,_,[singletons(true)]).
Predication = app(_, _, _).

For a head use

?- functor(Head,app,3),numbervars(Head).
Head = app(A, B, C).

See: What is the difference between head and predication.


Earlier I found write_canonical/1 but using numbervars/1,3,4 is preferred.

?- write_canonical(app(_29106, _29108, _29110)).
app(_,_,_)
true.

Bonus

Generating examples.

?- functor(Term,app,3),write_canonical(Term).
app(_,_,_)
Term = app(_29106, _29108, _29110).

Using with format/3

?- functor(Term,app,3),format('~k~n',[Term]).
app(_,_,_)
Term = app(_770, _772, _774).

When using with predicate that contains both the module and head.

If the module is not separated from the head the result may not be what is desired, e.g.

?- Pred = module:app(A,B,C),format('~k~n',[Pred]).
:(module,app(_,_,_))
Pred = module:app(A, B, C).

Simply deconstruct the predicate term into a module and head and format each separately, e.g.

?- Pred = module:app(A,B,C),Pred=Module:Head,format('~k:~k~n',[Module,Head]).
module:app(_,_,_)
Pred = module:app(A, B, C),
Module = module,
Head = app(A, B, C).

Among others. The key functionality is provided by numbervars/4 using the option singletons(true). That assigns normal variables '$VAR'(N), where N is a positive integer and singletons '$VAR'('_'). From there on the numbervars(true) option of write_term/2 prints the variables as A, B or _.

Although write_canonical/1 using the same algorithm, it doesn’t actually use numbervars/4 as it is required to print '$VAR'(N) terms as-is.

1 Like