[Haskell-cafe] Crazy idea: overloading function application notation

Stefan Holdermans stefan at cs.uu.nl
Thu May 31 02:11:09 EDT 2007


Jon,

> This is a crazy idea I've been working on: overload the syntax "x  
> y" so it can
> mean function application "f x = f(x)" or multiplication "x y =  
> x*y". The
> reason is simply that I appreciate the brevity of MLs function  
> application
> but I also appreciate the brevity of Mathematica's multiplication.
>
> Is it possible to implement this in Haskell using type classes? Is  
> there any
> way this could actually be practicable?

Well, of course, it is certainly not possible to implement it  
directly. Juxtaposition denotes by no means an ordinary binary  
operator. If you do pick an ordinary binary operator for application,  
say (#), then you can get quite far with some language extensions,  
although I think it'll render your code less readable. Anyway, I  
remember doing this dark deed once when working with finite maps.

I use associated type synonyms here, because we should get used to  
them anyway ;-). However, I'll attach a snippet employing functional  
dependencies, so you can actually play with it:

   infixl 9 #

   class Fun a where
     type Dom a :: *
     type Cod a :: *
     (#)        :: a -> Dom a -> Cod a

Now, let's have some fun ;-).

   instance Fun (a -> b) where
     type Arg (a -> b) = a
     type Res (a -> b) = b
     (#)               = ($)

   instance Fun Int where
     type Arg Int = Int
     type Res Int = Int
     (#)          = (*)

   instance Fun Bool where
     type Arg Bool = Bool
     type Res Bool = Bool
     (#)           = (&&)

   instance (Eq a) => Fun [(a, b)] where
     type Arg [(a, b)] = a
     type Res [(a, b)] = Maybe b
     (#)               = flip lookup

And here we go:

   head                           # [2, 3, 5]    ==>    2
   2                              # 3            ==>    6
   False                          # True         ==>    False
   [('E', 2), ('H', 3), ('C', 5)] # 'E'          ==>    Just 2

Now, how practicable is it? As said, I think constructions like this  
have a negative impact on the readability of your programs. Moreover,  
overloading function applications is bound to introduce a lot of  
ambiguities in your programs and, hence, the need for type annotations.

Cheers,

   Stefan


-------------- next part --------------
A non-text attachment was scrubbed...
Name: Fun.hs
Type: application/octet-stream
Size: 646 bytes
Desc: not available
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20070531/8c5cfc7c/Fun.obj


More information about the Haskell-Cafe mailing list