[Haskell-cafe] Split the IsList class for OverloadedLists
olf at aatal-apotheke.de
Sat Oct 31 19:43:40 UTC 2020
> Hello Café,
> Right now the IsList class used by the OverloadedLists extension
> requires both fromList, to construct something using list syntax,
> toList, to pattern-match on something using list syntax.
> So types implementing that class are expected to be isomorphic to
> This is a very strong restriction. In particular, this gets in the
> of implementing IsList for aeson's Value type, since there's no
> sensible total implementation of toList.
> : That is a recently proposed idea
For constructing values from list syntax, I can understand that only
fromList is used and Aeson's Value indeed contains lists. But if both
fromList and toList are required, I would expect that
fromList.toList=id and I can not see how that can ever be achieved with
Maybe a lightweight quasi-quoter can be syntactically almost as
convenient for construction?
MonoFoldable and MonoTraversable instances for Value might also be
helpful: MonoFoldable gives you a toList function.
> Proposal: split toList and fromList in two separate classes.
> (And modify the OverloadedLists extension accordingly.)
> Since they rely on an associated type family Item, it would be made
> standalone family.
> type family Item (l :: Type) :: Type
> class ToList l where
> toList :: l -> [Item l]
> class FromList l where
> fromList :: [Item l] -> l
> fromListN :: Int -> [Item l] -> l
> (Note: we can't just replace ToList with Foldable, because these
> have different kinds.)
> - Any objections? An obvious concern is backwards compatibility. Is
> a deal breaker? Are there other issues with this idea?
You would lose the pattern matching functionality, it seems.
It reminds me very much of OverloadedStrings and that is not always as
convenient as you might think. The issue is that a previously
monomorphic piece of syntax suddenly is polymorphic, so in some places
with OveroadedStrings one must provide type annotations. And that
destroys the brevity one was after.
> - Should that be a GHC proposal?
> - Has this been discussed before?
> : https://github.com/ghc-proposals/ghc-proposals
> One alternative is to use RebindableSyntax, which already allows
> to redefine toList and fromList however they want. The issue is it
> also mess with all other kinds of syntactic sugar just enough that
> unpleasantness is not worth the trouble.
> For example, if you wanted to use multiple list-like types in one
> module, you would want an overloaded version of fromList/fromListN.
> either roll your own or find a suitable dependency. Either way it's
> overhead you might not be willing to pay for, as opposed to
> that's already in base and Just Works.
> So even with some existing workarounds in mind, the above proposal
> a net improvement over the status quo.
> Maybe some day we'll also get to take fromInteger out of Num.
More information about the Haskell-Cafe