[Haskell-cafe] Re: Rethinking OO idioms
Daniel Yokomizo
daniel_yokomiso at yahoo.com.br
Fri Oct 1 14:12:17 EDT 2004
"John Goerzen" <jgoerzen at complete.org> escreveu na mensagem
news:slrnclm6pr.5mf.jgoerzen at christoph.complete.org...
> I've worked with languages with object-oriented features for awhile now.
> Python and OCaml, the two with which I work the most, both have OO.
>
> One of my first projects in Haskell would be to write a Haskell version
> of Python's ConfigParser[1] class. I wrote[2],[3] a version of this for
> OCaml that works very well.
>
> In a nutshell, ConfigParser is a utility for working with sectioned
> configuration files in a style similar to the familiar .ini files from
> Windows. It has methods to read a configuration file, get/set the items
> that are being configured, and write a new file back out. This, then,
> is a fairly typical metaphor for OO programs: an instance of a class has
> some state that can be accessed or modified, and possibly stored and
> retrieved.
>
> So I am thinking about a ConfigParser for Haskell. The first thing that
> occured to me is that Haskell has no OO features, so I'm not sure what
> is the best way to handle the "class" and its various methods.
>
> The next thing that occured to me is that, unlike OCaml and Python
> classes, Haskell has no mutable variables.
There are mutable variables, but they are called IORef and STRef (see the
hierarchical libraries, under Data). Of course we need to respect
referencial transparency, so we need to sequence these computations as
usual.
> A call like config.setOption("main", "initpath", "/usr") in Python
> -- which alters the state of the config object and returns nothing --
> would be impossible in Haskell (unless perhaps the FiniteMaps are
> mutable somehow?)
It can be done. The signature would be something like:
setOption :: Config -> String -> String -> String -> IO () -- or ST s ()
Such functions don't follow the Haskell way of programming, but they're
possible.
> I guess I'm having trouble translating this common OO language paradigm
> into the Haskell world.
>
> Thanks for any insight.
>
> -- John
>
> BTW, if I get a working ConfigParser for Haskell, I will publish it
> under the GPL like all the rest of my code.
>
> [1] http://www.python.org/doc/current/lib/RawConfigParser-objects.html
> [2] http://gopher.quux.org:70/devel/missinglib/html/ConfigParser.html
> [3]
http://gopher.quux.org:70/devel/missinglib/html/ConfigParser.rawConfigParser.html
"John Goerzen" <jgoerzen at complete.org> escreveu na mensagem
news:slrnclm6pr.5mf.jgoerzen at christoph.complete.org...
> I've worked with languages with object-oriented features for awhile now.
> Python and OCaml, the two with which I work the most, both have OO.
>
> One of my first projects in Haskell would be to write a Haskell version
> of Python's ConfigParser[1] class. I wrote[2],[3] a version of this for
> OCaml that works very well.
>
> In a nutshell, ConfigParser is a utility for working with sectioned
> configuration files in a style similar to the familiar .ini files from
> Windows. It has methods to read a configuration file, get/set the items
> that are being configured, and write a new file back out. This, then,
> is a fairly typical metaphor for OO programs: an instance of a class has
> some state that can be accessed or modified, and possibly stored and
> retrieved.
>
> So I am thinking about a ConfigParser for Haskell. The first thing that
> occured to me is that Haskell has no OO features, so I'm not sure what
> is the best way to handle the "class" and its various methods.
>
> The next thing that occured to me is that, unlike OCaml and Python
> classes, Haskell has no mutable variables.
There are mutable variables, but they are called IORef and STRef (see the
hierarchical libraries, under Data). Of course we need to respect
referencial transparency, so we need to sequence these computations as
usual.
> A call like config.setOption("main", "initpath", "/usr") in Python
> -- which alters the state of the config object and returns nothing --
> would be impossible in Haskell (unless perhaps the FiniteMaps are
> mutable somehow?)
It can be done. The signature would be something like:
setOption :: Config -> String -> String -> String -> IO () -- or ST s ()
Such functions don't follow the Haskell way of programming, but they're
possible.
> I guess I'm having trouble translating this common OO language paradigm
> into the Haskell world.
>
> Thanks for any insight.
>
> -- John
>
> BTW, if I get a working ConfigParser for Haskell, I will publish it
> under the GPL like all the rest of my code.
>
> [1] http://www.python.org/doc/current/lib/RawConfigParser-objects.html
> [2] http://gopher.quux.org:70/devel/missinglib/html/ConfigParser.html
> [3]
http://gopher.quux.org:70/devel/missinglib/html/ConfigParser.rawConfigParser.html
More information about the Haskell-Cafe
mailing list