# Is haskell style pattern matching allowed in conventional mathematics and if not, how do you work out the numerator of an arbitrary rational number?

Is it possible to define a function "Numerator" in the following way? $$N(a/b) = a$$

Or likewise is it possible to define a function "Denominator" like this? $$D(a/b) = b$$

These functions take a rational number, and match it against the pattern $a/b$, thus assigning the numerator to $a$ and the denominator to $b$.

This is common practice in functional programming languages like Haskell, and I am just dying to do similar in my standard mathematical escapades. Having access to this style of function would make my life much easier in a particular problem I'm working on right now.

If this is not possible (I haven't seen it done before), could someone kindly provide me with a formula or algorithm for working out the numerator of a rational number? (And the denominator for bonus points)

So given the number $1/2$, I want to know how to get at the $1$, and more generally, given $a/b$ I want to know how to get the $a$

You might wonder why I can't just do it by inspection: I'm dealing with a function which returns a rational number, and I only want the numerator, but because I only can see the function I can't see the numerator. In this way, I only have my number in the form $f(x)$ so I am unable to work out the numerator by inspection, I need some algorithm or formula for extracting it.

• formula or algorithm for working out the numerator of a rational number In what form or format is that rational number? I guess it's not a fraction, otherwise you could cancel out the $\gcd$ and get $a,b$. But it could be a fixed point, or floating point, or continued fraction, or whatever other format, and the answer might not be the same in all cases. – dxiv Mar 15 '17 at 0:39
• Continued fractions are not rational right? (Or perhaps there is a distinction between finite and infinite continued fractions). In any case I'm looking for the numerator of a fraction that has been simplified as much as possible. So 2/4 should be simplified to 1/2 before extracting the denominator – TheIronKnuckle Mar 15 '17 at 0:43
• Continued fractions are irrational iff infinite. It's still not obvious what the input is, though. In your example, if you know that the fraction is $2/4$ then simply divide both numerator and denominator by $\gcd(2,4)=2$ to get it into irreducible form $1/2$. Note that the $\gcd$ can be calculated using Euclid's algorithm. – dxiv Mar 15 '17 at 0:47

## 1 Answer

That isn't a well-defined function, unless you specify some further conditions. Since $1/2 = 2/4$, we should have $f(2/4) = f(1/2) = 1$, not $2$.

But we can define a function $f$ such that $f(x) = a$, where $x = a/b$ in lowest terms. Because this uniquely specifies what $a$ is, this defines a function.

However, I don't think it's possible to express $f$ in terms of $+, -, \times, \div$.

Edit: you want $f$ expressed in "mathematical notation". The above is a valid mathematical definition of $f$, but I assume you're looking for a more algorithmic definition.

Such a thing will be hard to produce, unless you implement it inside the "rational" object. I would have said impossible earlier today, but I came across a result by Julia Robison, stating that you can tell if a rational number is an integer just using $+,\times,<$. That being said, it's wildly impractical to implement it using this.

If you're doing a proof, you can simply declare that the function exists, and move on.

Julia Robinson, Definability and Decision Problems in Arithmetic. The Journal of Symbolic Logic, Vol. 14, No. 2 (Jun., 1949) , pp. 98-114

• so, would it be permissible for me to use your f(x) in a proof, even though it is impossible to state the definition with conventional terms? – TheIronKnuckle Mar 14 '17 at 11:50
• Yeah, that's totally an acceptable function. But I'd be careful about using the phrase "with conventional terms". There's many functions out there that aren't definable in terms of arithmetic operators, and they are no less legitimate than any other. – Henry Swanson Mar 14 '17 at 12:13
• You've been very helpful! If you just edit your answer to include a function definition for f that purely uses mathematical notation i will accept the answer. If it's not possible to do that just say and I will accept anyway. – TheIronKnuckle Mar 14 '17 at 21:49
• (It's particularly the part where you say "x=a/b in lowest terms". I would like to know how to express "in lowest terms" using mathematical notation) – TheIronKnuckle Mar 15 '17 at 0:45
• $a$ and $b$ are in lowest terms (equivalently, relatively prime) if: $d$ divides $a$ and $b$ implies $d=\pm 1$. Equivalently, there exist integers $x$ and $y$ such that $ax+by=1$. – Henry Swanson Mar 15 '17 at 0:51