sections for record settors

Hi, When you create a record in haskell, it automatically generates gettor functions for accessing the record fields. It also provides you with a record update syntax: rec {field=val} but there is no settor function. It would be nice if there was some sort of section-like syntax to access the settor function, like: {field=} -- val rec {field=val} -- rec or even if there was a name for that function (such as field_settor). Without such a syntax, one often has to write lambdas like: \r v -> r {field=v} \r -> r {field=3} Consider for example an cmd line parser that collects flags in a record. Instead of writing: opt "l" (NoArg {c_loop=False}) "dont loop", opt "o" (optStr {c_out=} "dir") "output dir", opt "t" (optInt {c_maxtime=} "mins") "maximum time" we would have to write it out as: options = [ opt "l" (NoArg (\c -> c {c_loop=False})) "dont loop", opt "o" (optStr (\v c -> c {c_out=v}) "dir") "output dir", opt "t" (optInt (\v c -> c {c_maxtime=v}) "mins") "maximum time" ] where opt cs d s = Option cs [] d s optStr f name = ReqArg f name optInt f name = ReqArg (f . read) name getOptions :: String -> [String] -> IO Config getOptions prog argv = let header = "Usage: " ++ prog ++ " [OPTIONS...] files..." in let usage s = ioError (userError (s ++ "\n" ++ usageInfo header options)) in let (updates, files, errs) = getOpt2 options argv in let config = updates defConfig {c_files=files} in case () of _ | not (null errs) -> usage (concat errs) | null (c_files config) -> usage "no files specified" | otherwise -> return config Tim Newsham http://www.thenewsh.com/~newsham/

Hi, I agree that a function syntax for a record setter is a really good idea.
Consider for example an cmd line parser that collects flags in a record. Instead of writing:
opt "l" (NoArg {c_loop=False}) "dont loop", opt "o" (optStr {c_out=} "dir") "output dir", opt "t" (optInt {c_maxtime=} "mins") "maximum time"
Just to show that this is a perfect example, see http://www.cs.york.ac.uk/fp/darcs/yhc-devel/src/compiler98/Flags.hs which has exactly the kind of problem you mention. Because there is no setter each line is given as: Opt "" "lbound" (bf $ \f x -> f{sLBound=x}) "show symbol table after lambda lifting" The \f x -> f bit is only there because setters are not first class thingies. It's not quite as much additional syntax as you have in your example, but its still not useful. Thanks Neil

It would be nice if there was some sort of section-like syntax to access the settor function
Indeed - I'd like it as well. Also these threads seem to deal with similar questions: http://www.haskell.org/pipermail/haskell/2005-February/015354.html http://www.haskell.org/pipermail/haskell-cafe/2005-January/008875.html http://www.haskell.org/pipermail/template-haskell/2005-February/000409.html Cheers, Misha

On 20.09 01:05, Misha Aizatulin wrote:
It would be nice if there was some sort of section-like syntax to access the settor function
Indeed - I'd like it as well. Also these threads seem to deal with similar questions: http://www.haskell.org/pipermail/haskell/2005-February/015354.html http://www.haskell.org/pipermail/haskell-cafe/2005-January/008875.html http://www.haskell.org/pipermail/template-haskell/2005-February/000409.html
Having a pure Haskell solution would be nice - but I think getting better record types has more priority. One can already simulate this with using DrIFT to derive setter/update functions for the record members. - Einar Karttunen

Hello Tim, Wednesday, September 20, 2006, 1:28:47 AM, you wrote:
rec {field=val}
but there is no settor function. It would be nice if there was some sort of section-like syntax to access the settor function, like:
you can use DriFT which generates setter, isA and many other functions (or you can use Template Haskell to implement it yourself and share your implementation with us :) http://repetae.net/john/computer/haskell/DrIFT/drop/DrIFT-2.1.1.tar.gz http://repetae.net/john/computer/haskell/DrIFT/drift.ps -- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com
participants (5)
-
Bulat Ziganshin
-
Einar Karttunen
-
Misha Aizatulin
-
Neil Mitchell
-
Tim Newsham