RE: [Haskell] overuse of maybe and [] in prelude and libs
Alex You've become a very sophisticated Haskell programmer! We did at one stage do something like this, by making list comprehensions into monad comprehensions. So [ x*x | x <- xs, pred x] meant the same as do { x <- xs; if pred x then mzero else return (); return (x*x)} But in the next iteration of the language we undid the change, a controversial decision that some still regret. Because naïve users were getting too many perplexing error messages about monads and functors when they thought they were just manipulating lists. Haskell is pretty good about letting you install a different Prelude, so you could try it yourself. Simon | -----Original Message----- | From: haskell-bounces@haskell.org [mailto:haskell-bounces@haskell.org] On Behalf Of S. Alexander | Jacobson | Sent: 08 April 2004 04:42 | To: Haskell Mailing List | Subject: [Haskell] overuse of maybe and [] in prelude and libs | | It feels like the Prelude insists on using [] and | Maybe too much. I keep writing things like this: | | foo = foldl union emptySet $ maybe mzero return $ lookup pairs key | goo = maybe emptySet toSomething $ lookup pairs key | | which really should look like this: | | foo = concat $ lookup pairs key | goo = fmap toSomething $ lookup pairs key | | But, even if we don't have a Monadic/Functor Set, | foo should at least be: | | foo = foldl union emptySet $ lookup key | | In other words, shouldn't Prelude define concat | and lookup as: | | concat = foldr mplus mzero -- (Also, see PS) | | lookup key [] = mzero | lookup key ((x,y):xyz) | | key == x = return y | | otherwise = lookup key xyz | | And if it is a fundamental problem adding | constraints to instances, why not add all | automatically derivable classes as constraints to | all the Prelude classes (esp. Monad and Functor!) | and automatically derive instances of all | derivable classes unless the programmer defines | his/own methods. | | -Alex- | | PS Shouldn't concat be defined with foldl and not | foldr? Doesn't foldr imply that you can't concat | infinite lists? (I know this is a FAQ, but | where?) | | _________________________________________________________________ | S. Alexander Jacobson mailto:me@alexjacobson.com | tel:917-770-6565 http://alexjacobson.com | _______________________________________________ | Haskell mailing list | Haskell@haskell.org | http://www.haskell.org/mailman/listinfo/haskell
On Thu, 8 Apr 2004, Simon Peyton-Jones wrote:
You've become a very sophisticated Haskell programmer!
Thank you, but I think it may only seem that way. I'll post a complete program up somewhere and they we'll see where I really stand. Among other things, I don't yet have a feel for idiom/style (e.g. when do you define classes rather than modules, when do use in default methods, in what order do you define types classes functions etc for maximum readability).
We did at one stage do something like this, by making list comprehensions into monad comprehensions. So [ x*x | x <- xs, pred x] meant the same as do { x <- xs; if pred x then mzero else return (); return (x*x)}
But in the next iteration of the language we undid the change, a controversial decision that some still regret. Because na�ve users were getting too many perplexing error messages about monads and functors when they thought they were just manipulating lists.
I guess I'm in the regret group. Nothing stops beginners from importing BeginnerUtils and using list typed functions with names like concatList (or implementors from writing better error message copy). But, ok...
Haskell is pretty good about letting you install a different Prelude, so you could try it yourself.
Hmm. That's interesting! How does this work? * Can this change propogate through the libs so that e.g. lookupFM also returns a Monad rather than Maybe without manual modification of all the libs? (Note: I am actually using lookupFM in my code, I used lookup in my example to simplify) * If I modify Monad in my prelude to have Ord, will do-notation work in my new monad class? Will the IO monad work? Can I make () an instance of Ord or EQ? * Can different modules use different Preludes? It seems like making a module dependent on a different Prelude means potential incompatibilities with third party modules. e.g. if I want to use HaXML and it hypothetically changes to define one Prelude and HaskellDB which hypothetically changes to define another Prelude, is there an easy way to import ONLY the functions defined in those modules and not all the functions defined in their respective Preludes. * Is there a formal definition of what in the Prelude is actually core to Haskell and what is really just common utilities located their? Alternatively, does an "advanced prelude" already exist that does what I probably want but don't know yet? -Alex- _________________________________________________________________ S. Alexander Jacobson mailto:me@alexjacobson.com tel:917-770-6565 http://alexjacobson.com
Simon
| -----Original Message----- | From: haskell-bounces@haskell.org [mailto:haskell-bounces@haskell.org] On Behalf Of S. Alexander | Jacobson | Sent: 08 April 2004 04:42 | To: Haskell Mailing List | Subject: [Haskell] overuse of maybe and [] in prelude and libs | | It feels like the Prelude insists on using [] and | Maybe too much. I keep writing things like this: | | foo = foldl union emptySet $ maybe mzero return $ lookup pairs key | goo = maybe emptySet toSomething $ lookup pairs key | | which really should look like this: | | foo = concat $ lookup pairs key | goo = fmap toSomething $ lookup pairs key | | But, even if we don't have a Monadic/Functor Set, | foo should at least be: | | foo = foldl union emptySet $ lookup key | | In other words, shouldn't Prelude define concat | and lookup as: | | concat = foldr mplus mzero -- (Also, see PS) | | lookup key [] = mzero | lookup key ((x,y):xyz) | | key == x = return y | | otherwise = lookup key xyz | | And if it is a fundamental problem adding | constraints to instances, why not add all | automatically derivable classes as constraints to | all the Prelude classes (esp. Monad and Functor!) | and automatically derive instances of all | derivable classes unless the programmer defines | his/own methods. | | -Alex- | | PS Shouldn't concat be defined with foldl and not | foldr? Doesn't foldr imply that you can't concat | infinite lists? (I know this is a FAQ, but | where?) | | _________________________________________________________________ | S. Alexander Jacobson mailto:me@alexjacobson.com | tel:917-770-6565 http://alexjacobson.com | _______________________________________________ | Haskell mailing list | Haskell@haskell.org | http://www.haskell.org/mailman/listinfo/haskell
On Thu, 8 Apr 2004, S. Alexander Jacobson wrote:
Haskell is pretty good about letting you install a different Prelude, so you could try it yourself.
Hmm. That's interesting! How does this work?
For your purpose it may suffice to hide some identifiers from the Prelude and provide your own implementations. The abstract Collections do it like this at the moment, you use them thus: import Prelude () import Collections So a user doesn't need to care which indentifiers exactly are replaced and which not: only the wrapper module needs to care. Example: http://www.stud.tu-ilmenau.de/~robertw/dessy/fun/Collections.hs In fact, this style for Collections already allows programmers to work with a much more expressive kind of Haskell, e.g. one 'fold' or 'filter' or... for all kinds of Collections (including traditional lists). (The grand unified Collections Framework is already there, you just need to use it!) Robert
Also, out of curiosity (since you are at MSFT) are you using any formal Cognitive Dimension stuff in the design of Haskell or its libraries? http://www.gotdotnet.com/team/brada/describingandevaluatingapiusabilityatmic... Your Excel paper is mentioned in the end-notes. http://research.microsoft.com/Users/simonpj/papers/excel/excel.pdf If so, is there a publicly available CD analysis of Haskell and its libs available? -Alex- _________________________________________________________________ S. Alexander Jacobson mailto:me@alexjacobson.com tel:917-770-6565 http://alexjacobson.com On Thu, 8 Apr 2004, Simon Peyton-Jones wrote:
Alex
You've become a very sophisticated Haskell programmer!
We did at one stage do something like this, by making list comprehensions into monad comprehensions. So [ x*x | x <- xs, pred x] meant the same as do { x <- xs; if pred x then mzero else return (); return (x*x)}
But in the next iteration of the language we undid the change, a controversial decision that some still regret. Because naïve users were getting too many perplexing error messages about monads and functors when they thought they were just manipulating lists.
Haskell is pretty good about letting you install a different Prelude, so you could try it yourself.
Simon
| -----Original Message----- | From: haskell-bounces@haskell.org [mailto:haskell-bounces@haskell.org] On Behalf Of S. Alexander | Jacobson | Sent: 08 April 2004 04:42 | To: Haskell Mailing List | Subject: [Haskell] overuse of maybe and [] in prelude and libs | | It feels like the Prelude insists on using [] and | Maybe too much. I keep writing things like this: | | foo = foldl union emptySet $ maybe mzero return $ lookup pairs key | goo = maybe emptySet toSomething $ lookup pairs key | | which really should look like this: | | foo = concat $ lookup pairs key | goo = fmap toSomething $ lookup pairs key | | But, even if we don't have a Monadic/Functor Set, | foo should at least be: | | foo = foldl union emptySet $ lookup key | | In other words, shouldn't Prelude define concat | and lookup as: | | concat = foldr mplus mzero -- (Also, see PS) | | lookup key [] = mzero | lookup key ((x,y):xyz) | | key == x = return y | | otherwise = lookup key xyz | | And if it is a fundamental problem adding | constraints to instances, why not add all | automatically derivable classes as constraints to | all the Prelude classes (esp. Monad and Functor!) | and automatically derive instances of all | derivable classes unless the programmer defines | his/own methods. | | -Alex- | | PS Shouldn't concat be defined with foldl and not | foldr? Doesn't foldr imply that you can't concat | infinite lists? (I know this is a FAQ, but | where?) | | _________________________________________________________________ | S. Alexander Jacobson mailto:me@alexjacobson.com | tel:917-770-6565 http://alexjacobson.com | _______________________________________________ | Haskell mailing list | Haskell@haskell.org | http://www.haskell.org/mailman/listinfo/haskell
participants (3)
-
Robert Will -
S. Alexander Jacobson -
Simon Peyton-Jones