Apply and compose functions in Prolog