Encapsulation in Haskell

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Encapsulation in Haskell

Creighton Hogg-2
Hi guys,
So one of the big things in object oriented programming is
encapsulation, and I'm wondering how to do it properly in
Haskell.  How do you define new data types but minimize the
dependence of external packages on the exact nature of the
data definition?
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Encapsulation in Haskell

Philippa Cowderoy
On Thu, 22 Dec 2005, Creighton Hogg wrote:

> Hi guys,
> So one of the big things in object oriented programming is
> encapsulation, and I'm wondering how to do it properly in
> Haskell.  How do you define new data types but minimize the
> dependence of external packages on the exact nature of the
> data definition?
>

Use modules. Don't expose the constructors.

--
[hidden email]

The task of the academic is not to scale great
intellectual mountains, but to flatten them.
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Encapsulation in Haskell

Jared Updike
> > So one of the big things in object oriented programming is
> > encapsulation, and I'm wondering how to do it properly in
> > Haskell.  How do you define new data types but minimize the
> > dependence of external packages on the exact nature of the
> > data definition?
> Use modules. Don't expose the constructors.

Instead, just expose functions that implement the functionality users
of your library will need:

http://www.haskell.org/hawiki/AbstractDataType
http://www.haskell.org/hawiki/AbstractDataTypes

  Jared.

--
[hidden email]
http://www.updike.org/~jared/
reverse ")-:"
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Encapsulation in Haskell

Gregory Woodhouse
In reply to this post by Philippa Cowderoy
--- Philippa Cowderoy <[hidden email]> wrote:

> On Thu, 22 Dec 2005, Creighton Hogg wrote:
>
> > Hi guys,
> > So one of the big things in object oriented programming is
> > encapsulation, and I'm wondering how to do it properly in
> > Haskell.  How do you define new data types but minimize the
> > dependence of external packages on the exact nature of the
> > data definition?
> >
>
> Use modules. Don't expose the constructors.
>

The trick, though, is that objects are stateful things. If you want to
program a binary search tree, then insertion ought to return an
entirely new tree, just as if objects were all immutable, and
operations on those objects simply creaqte new objects exactly like the
existing ones (except for the one change). I've thought about trying to
model this using monads and actions, but I'm not sure that's the best approach.

===
Gregory Woodhouse  <[hidden email]>
"Interaction is the mind-body problem of computing."
--Philip L. Wadler
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Encapsulation in Haskell

Tomasz Zielonka
On Thu, Dec 22, 2005 at 01:53:34PM -0800, Greg Woodhouse wrote:
> The trick, though, is that objects are stateful things. If you want to
> program a binary search tree, then insertion ought to return an
> entirely new tree, just as if objects were all immutable, and
> operations on those objects simply creaqte new objects exactly like the
> existing ones (except for the one change). I've thought about trying to
> model this using monads and actions, but I'm not sure that's the best approach.

Use lexical closures. Initialize the state, variables, threads, etc, and
then return (= expose) only the "methods", ie. monadic actions. Use the
fact that both functions and IO actions are abstract (opaque?).

Example:

    initObject = do
        var <- newVar
        let
          inc = do
            x <- readVar var
            writeVar var (x + 1)
          get = do
            readVar var
        return (inc, get)

Best regards
Tomasz

--
I am searching for a programmer who is good at least in some of
[Haskell, ML, C++, Linux, FreeBSD, math] for work in Warsaw, Poland
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe