I'm working on creating a bunch of instances for a Fraction data type in Haskell, and I'm wondering if there's a place I would be able to implement the ^
operator.
What I mean is, I've got several instances of various Num
types, and within those instances, I define common operations such as +
, -
, etc.
With that, the data type behaves as a normal number, as I want it to (meaning I can call things like (Frac 1 2) + (Frac 1 4)
and get back Frac 3 4
)
What I'm trying to do is implement ^
directly. Right now, I've got it defined like this:
(|^|) :: Fraction -> Int -> Fraction
(|^|) f = foldr (*) mempty . flip replicate f
When I try to change the name of the function to ^
, I get an error because it conflicts with Prelude's definition of ^
. Is there a Num
type I can give my Fraction
type an instance of to allow me to use the ^
operator on it?
Thanks!
Prelude.^
is not part of any type class, so the only way you can define your own ^
function would be to hide the one from Prelude
.
Note that since the signature of Prelude.^
is (Num a, Integral b) => a -> b -> a
, you'll be able to use it on values of your Frac
type just fine as long as it's an instance of Num
. You just wouldn't be providing your own implementation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With