Article 27.1 of the TRIPS Agreement says: "patents shall be available and patent rights enjoyable without discrimination as to the ... the field of technology." The law is pretty clear, and doesn't leave much wiggle room.
But why is that the rule? Why shouldn't countries discriminate in relation to patents based on the field of technology? Aren't some patents more valuable to society than others? Aren't patents for pharmaceuticals more valuable than patents for, say, pumpkin carving tools? Why not give longer patent terms for life-saving drugs than for less important things?