
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. 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?) 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...

You can use state monad to mimic mutation.
Also, take a look at the recursive decent monadic parsec library. It may
have done what you are trying to do.
Regards,
Ben.
John Goerzen

On 2004-09-29, Ben.Yu@combined.com
You can use state monad to mimic mutation.
Is that really what I want? In other words, is there a completely different, more Haskellish, way to look at this?
Also, take a look at the recursive decent monadic parsec library. It may have done what you are trying to do.
Thanks for the pointer. I'll take a look. -- John

At 01:44 30/09/04 +0000, John Goerzen wrote:
On 2004-09-29, Ben.Yu@combined.com
wrote: You can use state monad to mimic mutation.
Is that really what I want? In other words, is there a completely different, more Haskellish, way to look at this?
Also, take a look at the recursive decent monadic parsec library. It may have done what you are trying to do.
Thanks for the pointer. I'll take a look.
Sometimes what you want really *is* a mutable value of some kind, but far less than you might expect. I recently implemented an RDF/XML parser using Parsec to parse from an "event stream" of XMLish things. Parsec is Monadic, and provides for user state in the parser, which I duly used. But the amount of user state I used was tiny: [[ data RDFParserState = RDFParserState { nodegen :: Int , listgen :: Int } ]] I.e., just two counters that were used for generating identifiers within the parsing process. Everything else was quite comfortably treated as immutable values. The code can be seen at: http://www.ninebynine.org/Software/HaskellRDF/RDF/Harp/RDFXMLParser.hs The function that invokes Parsec being 'parseEventsToRDF'. (This may not be the best introductory example, because it touches a lot of other logic. There is a test suite in the same directory if you want to dig deeper.) #g ------------ Graham Klyne For email: http://www.ninebynine.org/#Contact

John Goerzen
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.
I would expect these signatures: | module Configuration where | | type Key = String | type Value = String | type Configuration = FiniteMap Key Value | | read :: FileName -> IO Configuration | write :: FileName -> Configuration -> IO () | update :: Configuration -> Key -> Value -> IO () | lookup :: Configuration -> Key -> Value
The next thing that occured to me is that, unlike OCaml and Python classes, Haskell has no mutable variables. 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?)
The purest approach is to have every configurable function accept a Configuration. Functions that in Python would alter the dictionary would simply return a new one.

--- John Goerzen mumbled on 2004-09-29 20.29.47 +0000 ---
The next thing that occured to me is that, unlike OCaml and Python classes, Haskell has no mutable variables. 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?)
http://www.ccs.neu.edu/home/matthias/Presentations/ecoop2004.pdf Mutation is not very OO-like. -- Mike Burns netgeek@speakeasy.net http://mike-burns.com

On Wed, Sep 29, 2004 at 08:29:47PM +0000, John Goerzen wrote:
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. 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?)
I might define just two IO functions: parseConfig :: FilePath -> IO Config modifyConfig :: FilePath -> (Config -> Config) -> IO Config This way, you could do all the modification in pure functional code, which as Alastair said, would create a "new" Config rather than modifying the existing one. Of course, you could also define a writeConfig :: FilePath -> Config -> IO () but then a user of your class could accidentally overwrite a change, if you had two parts of the code which read the same config, each made separate changes, and then each wrote their separate changes. -- David Roundy http://www.abridgegame.org

John (and Haskell community), I just subscribed to the Haskell mailing list the other day and this posting grabbed my attention. I've been workin with Python for a few years now and have recently decided to try to expand my horizons to Haskell (and OCaml). I love Python, but I feel like I could learn a lot that could be applied to Python from understanding FP languages like Haskell (and OCaml). That being said, can you point me to any documentation online or books that are more suited to a Python person trying to understand Haskell (and OCaml)? Any help is appreciated. Jeremy Jones John Goerzen wrote:
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. 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?)
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...
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On Thursday 30 September 2004 07:54 am, Jeremy Jones wrote:
John (and Haskell community),
I just subscribed to the Haskell mailing list the other day and this posting grabbed my attention. I've been workin with Python for a few years now and have recently decided to try to expand my horizons to Haskell (and OCaml). I love Python, but I feel like I could learn a lot that could be applied to Python from understanding FP languages like Haskell (and OCaml). That being said, can you point me to any
One of the interesting things about Python is that it has slowly been adding functional programming mechanisms to the language. Some of the constructs we all love in FP are available on Python. For instance: * anonymous functions ("lambda" keyword) * nested scopes and the ability to return functions * iterable objects On the other hand, Python's typing system is nowhere near as powerful as that of Haskell or OCaml. OCaml, and to an even larger extent, Haskell, have a wonderful typing system: it's strong, yet it inobtrusive. I am a fan of that, and it helps these languages scale to large projects better than Python -- while at the same time keeping them suitable for small ones (unlike, say, Java) OCaml has an object system. It's not as powerful as Python's in most respects, and can be loosely described as a functional hybrid of the Python and Java object systems <grin> I find myself writing most of my new code in OCaml these days. If I can manage to get a Haskell compiler built on AIX, I may move to Haskell :-) (Building anything on that platform is difficult, sigh)
documentation online or books that are more suited to a Python person trying to understand Haskell (and OCaml)? Any help is appreciated.
This is a great resource: Learning OCaml for C, C++, Perl, and Java programmers: http://www.merjis.com/developers/ocaml_tutorial/ I wish something like that existed for Haskell. I may write one someday. :-) Once you know OCaml, though, Haskell will come a lot easier. The typing systems are quite similar, as are many of the concepts. I/O is very different though. Haskell I/O is different from anything else I've ever seen, so I can't really come up with a good analogy for you there :-) This is a good Haskell tutorial: http://www.isi.edu/~hdaume/htut/

"John Goerzen"
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...
"John Goerzen"
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...
participants (8)
-
Ben.Yu@combined.com
-
Daniel Yokomizo
-
David Roundy
-
Graham Klyne
-
Jeremy Jones
-
John Goerzen
-
Mikael Brockman
-
Mike Burns