[Haskell-cafe] Object oriented haskell.

Arjun Comar nrujac at gmail.com
Thu May 15 18:16:44 UTC 2014


How would setting/modification work in your scheme?

This problem has been solved a couple of times within the Haskell community
and you're most likely looking for a lens library. They make data accessors
first class and a subset of them compose in a way that reads just like
object oriented notation. For example, with `lens` you can do the following:

    data MyState = MyState { _person :: Person }
    data Person = Person { _pos :: (Int, Int) }
    makeLenses [''MyState, ''Person]

   (person.pos._1 += 1) :: State MyState ()

Notice that the lenses compose with (.), compose in the order you expect
from OO programming (and opposite normal function composition -- though
it's actually the same), and allow you to set (as well as view). The `lens`
package also provides these lenses for most of base along with many other
useful tools for this kind of programming (notice _1 that acts as an
accessor into a tuple).


On Thu, May 15, 2014 at 1:44 PM, Kyle Marek-Spartz <
kyle.marek.spartz at gmail.com> wrote:

> Recently, on LtU: http://lambda-the-ultimate.org/node/4951
>
> Most relevantly:
> https://ghc.haskell.org/trac/ghc/wiki/Records/DeclaredOverloadedRecordFields/DotPostfix
>
>> Kyle Marek-Spartz
>
>
>
> On May 15, 2014, 12:38:29 PM, silvio <silvio.frischi at gmail.com> wrote:
> ------------------------------
>
> 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
> _______________________________________________
> Haskell-Cafe mailing list
> mailto:Haskell-Cafe at haskell.org <Haskell-Cafe at haskell.org>
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20140515/b22b78af/attachment.html>


More information about the Haskell-Cafe mailing list