Note that if List contains only ground terms, it cannot contain proper instances or variants, but only duplicates. Therefore, it is faster to use a sorting predicate to prune it.
Success: prune_instances([5,2,3,5,4,2],L). (gives L=[5,2,3,4]). prune_instances([f(1,2),f(1,M),1],L). % instance (gives M=_g74,L=[f(1,_g74),1]). prune_instances([f(1,2,3),f(1,M,3),f(1,2,N)],L). (gives M=_g80,N=_g70, L=[f(1,_g80,3),f(1,2,_g70)]). prune_instances([f(1,N),f(1,M),1],L). % variants (gives N=_g72,M=_g76, L=[f(1,_g72),1]). prune_instances([f(1,X),f(1,2),g(1)],L). (gives X=_g80; L=[f(1,_g80),g(1)]). Fail: prune_instances([1,2,3,1,4,2],[2,3,4]).