This is a SWI-Prolog implementation of the corresponding Quintus library, based on the generalised arg/3 predicate of SWI-Prolog.
?- freeze(S, integer(S)), occurrences_of_term(S, f(1,2,a), C). C = 2, freeze(S, integer(S)).
An example use-case is refactoring a large clause body by introducing intermediate predicates. This predicate can be used to find the arguments that must be passed to the new predicate.