export constructors only for matching (Was: qualified exports)
Henning Thielemann
lemming at henning-thielemann.de
Fri Feb 22 07:36:03 UTC 2019
On Fri, 22 Feb 2019, Bryan Richter wrote:
> > Another pet peeve: Export constructors but only for pattern matching purposes, i.e., elimination; not for
> construction--we'd export smart constructors for the latter which would ensure invariants
>
> I believe you are in luck. Since 7.8.1, GHC supports "unidirectional pattern synonyms", as described somewhere in
> this section:
> https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/glasgow_exts.html#extension-PatternSynonyms
In some cases you can even solve it in Haskell 98.
If you have
data T = Cons Char
and you want to let users match Cons without exporting it, you may export
a "destructor" like
uncons :: T -> Char
uncons (Cons a) = a
or you export a continuation passing function:
withT :: (Char -> a) -> T -> a
withT f (Cons a) = f a
and the user would no longer write
g :: Int -> T -> Ordering -> Bool
g x (Cons y) z = foo x y z
but
g :: Int -> T -> Ordering -> Bool
g x = withT $ \y z -> foo x y z
More information about the Libraries
mailing list