Gmail Calendar Documents Web Reader more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Function overloading in Haskell
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  10 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Arijit  
View profile  
 More options Jan 27, 5:08 pm
Newsgroups: comp.lang.haskell
From: Arijit <pal_...@yahoo.co.in>
Date: Wed, 27 Jan 2010 14:08:34 -0800 (PST)
Local: Wed, Jan 27 2010 5:08 pm
Subject: Function overloading in Haskell
Hi,

Is if possible to overload functions in Haskell similar to that of C++
or C# ? For example, if I want to define a function add which has
multiple arity:

add x y = x + y
add x y z = x + y + z

This doesn't work, the functions need to have different names. After
some googling, I located the following paper by Simon Peyton Jones
where he discusses similar overloading - Object-Oriented Style
Overloading for Haskell ( http://research.microsoft.com/en-us/um/people/simonpj/papers/oo-haske...
). Is such overloading available in Haskell or as an extension to
GHC / Hugs ?

SPJ's paper is the only reference I found - all other searches for
Haskell and overloading leads to typeclass based overloading - which I
don't want.

Thanks and Regards,

Arijit


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Paul Rubin  
View profile  
 More options Jan 27, 5:19 pm
Newsgroups: comp.lang.haskell
From: Paul Rubin <no.em...@nospam.invalid>
Date: Wed, 27 Jan 2010 14:19:39 -0800
Local: Wed, Jan 27 2010 5:19 pm
Subject: Re: Function overloading in Haskell

Arijit <pal_...@yahoo.co.in> writes:
> Is if possible to overload functions in Haskell similar to that of C++
> or C# ? For example, if I want to define a function add which has
> multiple arity:

> add x y = x + y
> add x y z = x + y + z

You would have to do some fairly crazy typeclass hackery for that.
As an example, see Text.Printf .

    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Federico Zenith  
View profile  
 More options Jan 27, 8:19 pm
Newsgroups: comp.lang.haskell
Followup-To: comp.lang.haskell
From: Federico Zenith <non.mi...@mma.re>
Date: Thu, 28 Jan 2010 02:19:37 +0100
Local: Wed, Jan 27 2010 8:19 pm
Subject: Re: Function overloading in Haskell

Arijit wrote:
> Is if possible to overload functions in Haskell similar to that of C++
> or C# ? For example, if I want to define a function add which has
> multiple arity:

> add x y = x + y
> add x y z = x + y + z

Nope, and for very fundamental reasons. Take the second function, "add x y
z". Since Haskell allows partial application, "add x y" would be a function
of one variable (z). That, however, would overlap with your first definition
of "add x y".
How is the compiler supposed to figure out which is which?

Instead, you could define your function on a custom data type with several
constructors:
data Arities a = Two a a | Three a a a -- | more as necessary

and define then:
add (Two a b)     = a + b
add (Three a b c) = a + b + c

(well, obviously for this example you would use "sum" instead, but you catch
my drift.)

Cheers,
-Federico


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ertugrul Söylemez  
View profile  
 More options Jan 28, 1:20 am
Newsgroups: comp.lang.haskell
From: Ertugrul Söylemez <e...@ertes.de>
Date: Thu, 28 Jan 2010 07:20:11 +0100
Local: Thurs, Jan 28 2010 1:20 am
Subject: Re: Function overloading in Haskell

Paul Rubin <no.em...@nospam.invalid> wrote:
> Arijit <pal_...@yahoo.co.in> writes:
> > Is if possible to overload functions in Haskell similar to that of
> > C++ or C# ? For example, if I want to define a function add which
> > has multiple arity:

> > add x y = x + y
> > add x y z = x + y + z

> You would have to do some fairly crazy typeclass hackery for that.  As
> an example, see Text.Printf .

Furthermore you just don't want to do it anyway.  Using multi-arity
functions has always been a hack.  In Haskell you don't need them.

As Frederico suggested, use types.  If you want to emulate multi-arity
with different behaviours for each one, use a data structure (and
consider reevaluating your concept).  If you want to consume arbitrarily
many items for the same computation, use lists.

Greets
Ertugrul

--
nightmare = unsafePerformIO (getWrongWife >>= sex)
http://blog.ertes.de/


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Hans Aberg  
View profile  
 More options Jan 28, 6:36 am
Newsgroups: comp.lang.haskell
From: Hans Aberg <haberg_20080...@math.su.se>
Date: Thu, 28 Jan 2010 12:36:09 +0100
Local: Thurs, Jan 28 2010 6:36 am
Subject: Re: Function overloading in Haskell

Federico Zenith wrote:
> Arijit wrote:
>> Is if possible to overload functions in Haskell similar to that of C++
>> or C# ? For example, if I want to define a function add which has
>> multiple arity:

>> add x y = x + y
>> add x y z = x + y + z

> Nope, and for very fundamental reasons. Take the second function, "add x y
> z". Since Haskell allows partial application, "add x y" would be a function
> of one variable (z). That, however, would overlap with your first definition
> of "add x y".
> How is the compiler supposed to figure out which is which?

This is an interesting comment, but by turning on extensions in 'hugs
-98', one can do some arity overloading, sort of:

class Add a where
   add :: a -> (a -> a)

instance Add Integer where
   add x = (+x)

instance Add (Integer -> Integer) where
   add f g = \x -> f x + g x

instance Show (Integer -> Integer) where
   show _ = "(Integer -> Integer)"

instance Eq (Integer -> Integer) where
   (==) = error "Eq.(Integer -> Integer): not implemented."

instance Num (Integer -> Integer) where
   fromInt n = \x -> fromIntegral n
   fromInteger n = \x -> fromIntegral n

Then in Hugs
   add (1 :: Integer) 3
   4
and
   add ((\x -> x + 1) :: Integer -> Integer) 3 7
   11

Tough perhaps not exactly what asked for. :-)

   Hans


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Mark T. B. Carroll  
View profile  
 More options Jan 28, 9:15 am
Newsgroups: comp.lang.haskell
From: "Mark T. B. Carroll" <Mark.Carr...@Aetion.com>
Date: Thu, 28 Jan 2010 09:15:02 -0500
Local: Thurs, Jan 28 2010 9:15 am
Subject: Re: Function overloading in Haskell

Ertugrul Söylemez <e...@ertes.de> writes:
> Furthermore you just don't want to do it anyway.  Using multi-arity
> functions has always been a hack.  In Haskell you don't need them.

I have to say, while I think that the implementation of
Text.Printf.printf is pretty cool, I do agree that I've never been
pained by such awkwardness myself because I never find myself wanting
multi-arity functions.

(snip)

> If you want to consume arbitrarily many items for the same
> computation, use lists.

Exactly. With various type system extensions (existential quantification
at the least) helping when the list items can't all be of the same type.
Though, normally, wrapping different expected types in an algebraic data
type suffices.

Mark


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Paul Rubin  
View profile  
 More options Jan 28, 2:44 pm
Newsgroups: comp.lang.haskell
From: Paul Rubin <no.em...@nospam.invalid>
Date: Thu, 28 Jan 2010 11:44:21 -0800
Local: Thurs, Jan 28 2010 2:44 pm
Subject: Re: Function overloading in Haskell
"Mark T. B. Carroll" <Mark.Carr...@Aetion.com> writes:

> I have to say, while I think that the implementation of
> Text.Printf.printf is pretty cool,

printf seems like a horrible hack, but it's very useful and I use it all
the time even though it breaks very easily (requires type annotations
where you don't expect to need them, etc).  Generating messages like

   print $ "Hello " ++ your_name ++ ", it's sure a nice day in " ++
     your_city  ....

is a big pain in the neck.  If printf didn't exist I'd be looking for
crazy schemes with existential wrappers, Template Haskell concoctions,
etc.


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ertugrul Söylemez  
View profile  
 More options Jan 30, 12:17 am
Newsgroups: comp.lang.haskell
From: Ertugrul Söylemez <e...@ertes.de>
Date: Sat, 30 Jan 2010 06:17:34 +0100
Local: Sat, Jan 30 2010 12:17 am
Subject: Re: Function overloading in Haskell

If it's just string concatenation, I would use the most straightforward
way to do it:  concat.

  concat [ "Hello ", yourName,
           ", it's sure a nice day in ", yourCity, ...]

If your format is complicated, printf won't help anyway.  In such cases
a Writer monad comes in handy:

  import MonadLib

  formatIRC :: WriterM m String => Maybe String -> String -> [String] -> m ()
  formatIRC prefix command args = do
    case prefix of
      Just pfx -> put ":" >> put pfx >> put " "
      Nothing  -> return ()
    put command
    forM_ args $ \arg -> do
      put " "
      when (' ' `elem` arg) $ put ":"
      put arg

Printf is really only useful where you want to format numbers and it's
probably best to limit its use to that.

Greets
Ertugrul

--
nightmare = unsafePerformIO (getWrongWife >>= sex)
http://blog.ertes.de/


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Arijit  
View profile  
 More options Feb 2, 5:47 pm
Newsgroups: comp.lang.haskell
From: Arijit <pal_...@yahoo.co.in>
Date: Tue, 2 Feb 2010 14:47:02 -0800 (PST)
Local: Tues, Feb 2 2010 5:47 pm
Subject: Re: Function overloading in Haskell
On Jan 28, 6:19 am, Federico Zenith <non.mi...@mma.re> wrote:

> Arijit wrote:
> > Is if possible to overload functions in Haskell similar to that of C++
> > or C# ? For example, if I want to define a function add which has
> > multiple arity:

> > add x y = x + y
> > add x y z = x + y + z

> Nope, and for very fundamental reasons. Take the second function, "add x y
> z". Since Haskell allows partial application, "add x y" would be a function
> of one variable (z). That, however, would overlap with your first definition
> of "add x y".
> How is the compiler supposed to figure out which is which?

[snip]

Thanks for your reply. I understand the difficulty associated with
multiple arity functions in Haskell 98. However the paper by SPJ I
linked to specifically talks about extending the language to allow for
such overloading. And that paper is a few years old. I was wondering
if the extension discussed in the paper been incorporated into the
language / available as a GHC extension. My belief was such an
extension would be available, given 1) the plethora of extensions that
exist for haskell, 2) the usefulness of the feature and 3) the feature
was conceived by SPJ few years back - enough time for an
implementation.

I have seen implementations of a multiple arity zip function using
template haskell, but I believe you need to specify the arity
explicitly - no automatic inference. But documentation on template
haskell is very scarce - the only good reference I was able to locate
is SPJ's original paper.

Thanks and Regards,

Arijit


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dirk Thierbach  
View profile  
 More options Feb 3, 3:24 am
Newsgroups: comp.lang.haskell
From: Dirk Thierbach <dthierb...@usenet.arcornews.de>
Date: Wed, 3 Feb 2010 09:24:39 +0100
Local: Wed, Feb 3 2010 3:24 am
Subject: Re: Function overloading in Haskell

Arijit <pal_...@yahoo.co.in> wrote:
> On Jan 28, 6:19 am, Federico Zenith <non.mi...@mma.re> wrote:
>> Arijit wrote:
>>> Is if possible to overload functions in Haskell similar to that of C++
>>> or C# ? For example, if I want to define a function add which has
>>> multiple arity:

>> > add x y = x + y
>> > add x y z = x + y + z
> Thanks for your reply. I understand the difficulty associated with
> multiple arity functions in Haskell 98. However the paper by SPJ I
> linked to specifically talks about extending the language to allow for
> such overloading.

I have only skimmed the paper, but unless I'm mistaken, it looks like
the paper is dealing with OO-style overloading of methods (i.e.,
a fixed number of signatures, possibly with different arity), not with
declaring functions of arbitrary (indefinite) arity.

Maybe an actual demonstration will make things clearer. For simplicity,
we'll restrict to Integers:

  {-# LANGUAGE FlexibleInstances #-}

  class AddType r where
    add' :: [Integer] -> r

  instance AddType Integer where
    add' args = sum args

  instance AddType r => AddType (Integer -> r) where
    add' args = \a -> add' (a:args)

  add :: (AddType r) => r
  add = add' []

Then one can do:

*Main> add (2 :: Integer) (3 :: Integer) :: Integer
5
*Main> add (2 :: Integer) (3 :: Integer) (4 :: Integer) :: Integer
9

and so on. But as others have said, this is ugly as hell, it's annoying
because you have to be very careful with the types and might need to
add annotations, and in this particular case it's much better to use
just "sum [2,3]" and "sum [2,3,4]" instead.

This technique has its uses for border cases like "printf", but even then
the error messages can be cryptic if you make a mistake.

So, if you really just want to define a functions "add" with arbitrary
arity: Don't. If you would like to so something else: Maybe, depending
on what it actually is.

> And that paper is a few years old. I was wondering if the extension
> discussed in the paper been incorporated into the language /
> available as a GHC extension.

Overlapping instances have been incorporated. Closed classes/method
constraints have not, AFAIK, and my gut feeling is that at some point
they may become problematic (which is maybe the reason they haven't
been added).

- Dirk


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2010 Google