Duality and distance formulas in Banach function spaces