Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overloading of operators

I want to define an operator (call it +-+) that works in this:

if a,b are Char    => a +-+ b = [a][b]
if a,b are Integer => a +-+ b = a+b

I tried with:

class Summable a where
    (+-+)       :: a -> a -> b

instance Summable Integer where
    a +-+ b     = a + b

instance Summable Char where
    a +-+ b     = [a] ++ [b]

but I get the errors:

Couldn't match type `b' with `Integer'....
Couldn't match type `b' with `[Char]' ....

Is it possible to do this? How?

like image 768
Aslan986 Avatar asked Aug 22 '12 15:08

Aslan986


People also ask

What is operator overloading with example?

In C++, we can change the way operators work for user-defined types like objects and structures. This is known as operator overloading. For example, Suppose we have created three objects c1 , c2 and result from a class named Complex that represents complex numbers.

How are operators overloaded?

An overloaded operator is called an operator function. You declare an operator function with the keyword operator preceding the operator. Overloaded operators are distinct from overloaded functions, but like overloaded functions, they are distinguished by the number and types of operands used with the operator.

Which operators are used for overloading?

In the above syntax Return_Type is value type to be returned to another object, operator op is the function where the operator is a keyword and op is the operator to be overloaded. Operator function must be either non-static (member function) or friend function. Overloading unary operator.

What is operator overloading explain their types?

In computer programming, operator overloading, sometimes termed operator ad hoc polymorphism, is a specific case of polymorphism, where different operators have different implementations depending on their arguments. Operator overloading is generally defined by a programming language, a programmer, or both.


1 Answers

The problem is the type variable b can't be determined from the instance, even though it is fixed. (Having a free variable like that requires the function returns something that has any type, i.e. undefined.)

Can you give +-+ the type a -> a -> a? If so, do that. (But it looks like this isn't possible.)

Otherwise, you could use functional dependencies, so that the instance specifies the result type, or type families, so that one of the attributes of the instantiation is the result type.

For functional dependencies, the code might look like:

{-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies #-}

class Summable a result | a -> result where
  (+-+) :: a -> a -> result

instance Summable Char String where
  a +-+ b = [a] ++ [b]

instance Summable Integer Integer where
  a +-+ b = a + b

For type families, it would be like:

{-# LANGUAGE TypeFamilies #-}

class Summable a where
  type Result a
  (+-+) :: a -> a -> Result a

instance Summable Char where
  type Result Char = String
  a +-+ b = [a] ++ [b]

instance Summable Integer where
  type Result Integer = Integer
  a +-+ b = a + b

(Thank you to Vitus and Vladimir Matveev for fixing the various errors I had made! :) )

like image 123
huon Avatar answered Oct 02 '22 16:10

huon