RE: [Haskell-cafe] File path programme

On 26 January 2005 14:30, Malcolm Wallace wrote:
Keean Schupke
writes: So let's define a new algebraic datatype something like:
I think you can guess what comming - why not use a class for extensibility: class Path p where ... The path class would provide all the operations for using paths...
Yup, good idea, and I think this would also allow David Roundy's suggestion of a FastString implementation of filepaths - it becomes just another instance.
This has the advantage that people can add instances for platforms without having to alter the code in the library...
Exactly. Yes.
Hmm, I deliberately didn't suggest using a type class. Do you really want to expose Win32 paths and POSIX paths, and whatever else, as different types? Maybe you do for extensibility, but you really want a single Path type too - otherwise portable code will have to use conditional compilation. The Path type therefore has to be an existential wrapper: data Path = (CPath p) => Path p Maybe we do want to do this, but it causes portability issues with the Path library itself: nhc98 can't compile this code for example - you can rewrite it using an explicit record of operations, as long as none of the operations are polymorphic... Cheers, Simon

"Simon Marlow"
Hmm, I deliberately didn't suggest using a type class. Do you really want to expose Win32 paths and POSIX paths, and whatever else, as different types? Maybe you do for extensibility, but you really want a single Path type too - otherwise portable code will have to use conditional compilation.
Good point. Ideally, you want to be able to write all FilePath code polymorphically at type (Path p => p), and have a decent defaulting mechanism available to choose a particular instance at compile time (perhaps conditionally, based on OS platform). Sadly, in the absence of a better 'default' declaration semantics, we are left having to resolve the ambiguity ourselves, or resort to some other type-system extension, like as you say existentials.
The Path type therefore has to be an existential wrapper:
data Path = (CPath p) => Path p
Maybe we do want to do this, but it causes portability issues with the Path library itself: nhc98 can't compile this code for example
Actually, nhc98 /can/ cope with existentials when written as: data Path = forall p . (CPath p) => Path p but it doesn't do local universal quantification like: data Path = Path (forall p . p) Regards, Malcolm

Malcolm Wallace wrote:
Good point. Ideally, you want to be able to write all FilePath code
polymorphically at type (Path p => p), and have a decent defaulting mechanism available to choose a particular instance at compile time (perhaps conditionally, based on OS platform).
Sadly, in the absence of a better 'default' declaration semantics, we are left having to resolve the ambiguity ourselves, or resort to some other type-system extension, like as you say existentials.
You could define a type in the library using conditional compilation: #ifdef Unix type SystemPath = UnixPath #endif #ifdef Windows type SystemPath = WindowsPath #endif Then you do: let myPath = (emptyPath :: SystemPath) in ... Keean.

Simon Marlow wrote:
Hmm, I deliberately didn't suggest using a type class. Do you really want to expose Win32 paths and POSIX paths, and whatever else, as different types? Maybe you do for extensibility, but you really want a single Path type too - otherwise portable code will have to use conditional compilation. The Path type therefore has to be an existential wrapper:
data Path = (CPath p) => Path p
Maybe we do want to do this, but it causes portability issues with the Path library itself: nhc98 can't compile this code for example - you can rewrite it using an explicit record of operations, as long as none of the operations are polymorphic...
Not at all. You just include some nice operation in the class: emptyPath :: Path p => p appendPath :: Path p => p -> String -> p etc... So it is an abstract datatype and class. The user never needs to touch the concrete type, even though they use it... The types remain polymophic. Keean.
participants (3)
-
Keean Schupke
-
Malcolm Wallace
-
Simon Marlow