This is how it should work (Ciao Prolog example).

Example used:

:-module(ciao_1, _, []).
:- use_module(library(write),[write/1]).

:- multifile portray_attribute/2.

portray_attribute(formula(_Var,Formula), Var) :-
write(Formula), write(' --- '), write(Var).

p(X) :- attach_attribute(X, formula(X, [X/0, X/fA(_Y)])).
p(s(X)) :- p(X).

And execution:

?- use_module('ciao_1.pl').

yes
?- p(X).

X attributed formula(X,[X/0,X/fA(_)]) ? ;

X = s(_A),
_A attributed formula(_A,[_A/0,_A/fA(_)]) ? ;

X = s(s(_A)),
_A attributed formula(_A,[_A/0,_A/fA(_)]) ? ;

X = s(s(s(_A))),
_A attributed formula(_A,[_A/0,_A/fA(_)]) ?

yes
?-

Thanks for your time.
Regards,

Victor