103 points · by 0xmohit · 10y ago · 71 comments
The "monomorphism" in "monomorphism restriction" isn't the term from category theory: it's just the opposite of "polymorphism". You can read more about the details of the restriction and why it exists here: https://www.haskell.org/onlinereport/haskell2010/haskellch4....
My biggest gripe is record handling. There are a few libraries that help dealing with the pain of records, but if you're using 'vanilla' Haskell, they really can be frustrating.
For instance, any field name in a record pollutes the global namespace for that module.
data MyRecord = MyRecord1 { name :: String } | MyRecord2 { name :: NameType } <-- This is an error because the "name" field can't be both a String and a NameType (a made up type, just for the example). You need to use something like rName or r2Name to differentiate the two constructors...
...Which leads me to the other problem of different record constructors of the same data type having different fields, which can lead to some very ugly runtime exceptions (so much for a strong type system!): => data MyData = MyData1 { name :: String } | MyData2 { value :: Int }
=> let x = MyData1 { name = "hello" }
=> x { value = 0 } -- <-- this causes an exception because 'value' field does not exist for the constructor MyData1, but does for MyData2 so it matches the type system and there is no compile-time warning to catch it.
Another big issue I've recently had with my code was dealing with multiple constructors into pattern matches/pattern guard conditionals. For example, I have a record type like this: data MyData = A { ... } | B { ... } | C { ... }
and I want a case to match either A or B, otherwise do C. Let's say I have a function f1 that I want to run on A and B, and a function f2 that I want to run on C. I'm forced to do something like case myData of
A{} -> f1
B{} -> f1
C{} -> f2
Whereas I'd love to be able to do case myData of
A{} or B{} -> f1
C{} -> f2
This becomes a big problem when I have a lot of cases to take care of, and repetition makes it harder to spot bugs and easier to mess up.Record update in constructors is statically checked:
type myData = MyData1 of { name : string } | MyData2 of { value : int }
let x = MyData1 { name = "hello" }
(*
* rejected:
* let y = MyData2 { x with value = 0 }
*)
let y = match x with
| MyData1 z -> failwith "todo"
| MyData2 z -> MyData2 { z with value = 0 }
And one can write: match x with
| A | B -> f1
| C -> f2One way to deal with this is to not use fields at all together with algebraic data types (i.e. when you have more than one constructor), just pattern match. When you have some common field across all constructors, define a regular function to pull it out:
someData :: MyData -> a
and use that instead. As to the second use case, if you need to access something common to A and B but not C, then: someOtherData :: MyData -> Maybe a
and pattern match on that. Or first pattern match on C and then have a catch all _ for A and B.lens remedies a lot of Haskell's pain points when it comes to records, but I have to say that PureScripts record system is so much nicer.
Do you mean Template Haskell? If so, I'm pretty sure it's not required for use, just to have the lenses defined for you. You can still define them manually, at the cost of a little extra boilerplate.
If you actually mean ghci, then I have no idea what you're talking about...
For what it's worth, lenses can avoid some of these issues. For instance, they can allow you to have the same field name in multiple records:
data RecordA { _a_name :: String }
data RecordB { _b_name :: String }
makeLensesWith underscoreFields ''RecordA
makeLensesWith underscoreFields ''RecordB
You can now use something like `view name x` or `x^.name` for values of type `RecordA` or `RecordB`. Within the record definitions themselves, the field names still have to be disambiguated. The naming convention I chose here is pretty arbitrary. The first argument to `makeLensesWith` tells it how to parse the field names to get the "true" name.https://ghc.haskell.org/trac/ghc/wiki/Records/OverloadedReco... https://ghc.haskell.org/trac/ghc/wiki/Records/OverloadedReco...
I agree that this is poor design.
Field accessors for a field present in one constructor and absent in others should give a Maybe.
I'm not certain what should be done for the case of different types. Packaging it up in an Either seems possible, but the obvious approach would make the shape of the return type depend on the order in which I've listed my constructors, which is ugly... Refusing to generate the accessor function seems okay. Disallowing the collision - effectively the current state - isn't so bad.
In practice, the rule others have mentioned works fine: pretend Haskell doesn't support named fields in types with more than one constructor. The RecordWildCards extension also makes traditional Haskell records more pleasant to work with.
A{} or B{} -> f1
a big part of the problem is that A and B are going to be putting different things into scope in the context of f1.If you're not binding anything, you could do it with guards:
...
| isA myData || isB myData -> f1
...I rarely use wildcards that aren't embedded in the middle of some larger structure. They stop the compiler from telling me points I need to consider, as I change my code.
https://downloads.haskell.org/~ghc/master/users-guide/8.0.1-...
3.5 Naming Conventions
Some names are inconsistent. For example Functor is not called Mappable while Traversable is not named after abstract nonsense. It is best to focus on what things are instead of what they are called.
I wanted to rename "Monoid" to "Joinable" in my code ("Concatenatable" is too long). But it turns out you can't alias a type class while maintaining compatibility with libraries that use it.
Do tell me more about how "concatenatable" is a "completely wrong" impression of what Monoids are for. I know you can use them to, like, multiply using abstract nonsense, but that's not why you care what a Monoid is. Monoids let you generalize `mappend` over many kinds of sequences.
newtype Sum a = Sum { getSum :: a }
instance (Num a) => Monoid (Sum a) where
mempty = Sum 0
mappend (Sum a) (Sum b) = Sum (a + b)
Finally, and perhaps most importantly, I seriously doubt anybody reading your codebase is going to do so in complete isolation of the rest of the Haskell ecosystem. They are going to encounter the word "monoid", so you're not really doing them any favors by hiding it from them. You're just forcing them to jump through the mental hurdles of reconciling your wording with what's used everywhere else. type Natural = [()]
zero, one, two, three, four :: Natural
zero = []
one = [()]
two = [(), ()]
three = [(), (), ()]
four = [(), (), (), ()]
...
concatenation `(++) :: Natural -> Natural -> Natural`: >>> four
[(), (), (), ()]
>>> three ++ one
[(), (), (), ()]
Your point still holds.Our (non-jargon) vocabulary simply isn't equipped to deal with this level of abstraction so we need jargon if we intend to be fully descriptive — existing words don't cut it and we are just at the bottom rung of the abstraction ladder.
It's up to us as a community how much we care about faithfully describing things, Simon Peyton Jones himself claims tongue-in-cheek that their biggest mistake was not calling monads “warm fuzzy things”[0] :)
[0] http://research.microsoft.com/en-us/um/people/simonpj/papers...
{-# LANGUAGE ConstraintKinds #-}
import Data.Monoid
type Joinable = Monoid
something :: Joinable m => m -> m -> m
something = (<>)
But like MustardTiger says, don't.this would get downvoted quickly if it was a comment
A much better example would be "Show"(Showable) vs "Traversable".
If you really want to mess things up, use names that mean something different every day.
But I would know what a “Mappable” should be, regardless of PL.
No, it has the same meaning. It does not behave identically because the languages do not have identical designs.
>But I would know what a “Mappable” should be, regardless of PL.
Lots of people wouldn't, and would be wondering what cartography has to do with anything. And even for people familiar with the functional "map", now they would just have an incorrect idea of what Functor is. Lots of Functors are things most people would not think of as being "Mappable".
E.g., "classOf fmap" as a synonym for Functor.
class (HasMap f) <= Functor f
class (Functor f, HasApply f) <= Apply f
class (Apply f, HasPure f) <= Applicative f
class (Apply m, HasChain m) <= Bind m
class (Applicative m, Bind m) <= Monad m
[1]: https://github.com/tfausak/purescript-neon/blob/v0.5.4/src/N...
[2]: https://pursuit.purescript.org/packages/purescript-prelude/1...Actually, I think there are three hard problems in CS. The third being pedants.
Some of these complaints are goofy like the one about the naming of Functor.
Others like the one about records being bad are perfectly legitimate. It would be awesome to see real improvement on this. Unnecessary runtime errors are evil.