[Haskell-cafe] Object oriented haskell.

silvio silvio.frischi at gmail.com
Thu May 15 17:38:29 UTC 2014


Hi Haskell,

I've been wondering if (.) is so cool in other languages why can't we
make it even cooler in haskell. And went on to implement such a (.)
based on multiparameter type classes and type families.

type family Output object action
class Action object action where
	(.) :: object -> action -> Output object action

I'm not sure if this has been done before like this but i didn't find
anything.
I used Map as an example, and here is what I ended up with:

> :m -Prelude
> import Prelude hiding ((.))
> import Object
> import Object.Example
> import Data.Map hiding (size)
> let m = empty . [ 'f' := Just 1, 'o' := Just 2, 'o' := Nothing ]
> m
fromList [('f',Just 1),('o',Nothing)]
> m . 'f'
Just 1
> m . size
2

I also have a pretty cool (almost) solution to the name collision problem.

Visit the project homepage for a more thorough explanation.

https://github.com/yokto/object

And to those who gonna hate on me because they like the (.) as function
composition I have only this to say.

type instance Output (b -> c) (a -> b') = (a -> c)
instance (b ~ b') => Action (b -> c) (a -> b') where
	f . g = f Prelude.. g


Have fun,

Silvio


More information about the Haskell-Cafe mailing list