Hume believes that the only meaningful thoughts are those about relations of ideas (known a priori, examples including mathematics and logic) and matters of fact (known a posterior, examples including 'the sun rises' and 'carrots are orange'). But Hume's psychology promotes the notion that thoughts have meanings only insofar as they are reducible to impressions, since according to Hume our ideas reduce to impressions. As such, why does Hume make an exception for mathematics and logic, holding them to be meaningful thoughts, even though they are not themselves reducible to impressions?
According to Hume, ideas are not merely reducible to impressions. Ideas are impressions, weakened stored impressions. Or at most ideas are impressions that were cut and pasted, like an idea of a winged horse, which is pasted from an impression of a horse and an impression of wings.
There seems to be, however, a curious exception when it comes to ideas of relations. Relations, in general, are not perceived. There are, in general, no impressions of relations, according to Hume. But we do seem to have ideas of relations. These ideas of relations do not stem from impressions of relations. Instead, they stem from mental acts of comparison between (non-relational) impressions, or ideas. For example, we have ideas of different shades of colors. These come from impressions. We also have the idea that one shade is darker than another. This 'darker' idea does not come from a 'darker' impression. It comes from a mental act of comparison between colorful ideas.
When any two objects possess the same QUALITY in common, the DEGREES, in which they possess it, form a fifth species of relation . . . Two colours, that are of the same kind, may yet be of different shades, and in that respect admit of comparison. (Treatise of Human Nature "Of Relations")
It is by this, I think, that Hume "sneaks in" apriori knowledge, as in arithmetic. No apriori knowledge could come directly from impressions. But apprently we are able to "discover" apriori knowledge when we compare impressions to extract ideas of relations.
These relations may be divided into two classes; into such as depend entirely on the ideas, which we compare together, and such as may be changed without any change in the ideas. It is from the idea of a triangle, that we discover the relation of equality, which its three angles bear to two right ones; and this relation is invariable, as long as our idea remains the same. On the contrary, the relations of contiguity and distance betwixt two objects may be changed merely by an alteration of their place, without any change on the objects themselves or on their ideas. (Treatise "Of Knowledge")