[Haskell-cafe] Question about Newtype "op()" function arguments.

Roman Cheplyaka roma at ro-che.info
Fri Jun 7 19:07:42 CEST 2013


* David Banas <capn.freako at gmail.com> [2013-06-07 07:08:19-0700]
> Hi All,
> 
> Referring to the following, which is taken from the *Control.Newtype
> *documentation
> page:
> 
> op :: Newtype<http://hackage.haskell.org/packages/archive/newtype/0.2/doc/html/Control-Newtype.html#t:Newtype>
> n
> o => (o -> n) -> n ->
> oSource<http://hackage.haskell.org/packages/archive/newtype/0.2/doc/html/src/Control-Newtype.html#op>
> 
> This function serves two purposes:
> 
>    1. Giving you the unpack of a newtype without you needing to remember
>    the name.
>    2. Showing that the first parameter is *completely ignored* on the value
>    level, meaning the only reason you pass in the constructor is to provide
>    type information. Typeclasses sure are neat.
> 
> As point #2, above, emphasizes, the only purpose for the first argument to
> the function (i.e. - the constructor "(o -> n)") is to specify the type of
> 'n'. However, being a *newtype*, 'n' can have only one constructor. So, why
> is it necessary to pass in the constructor to this function, when we're
> already passing in 'n'?

Hi David,

Note that that argument is in fact not necessary — 'op' without its
first argument is exactly 'unpack'.

I imagine that 'op' may be useful if you need to disambiguate the
instance in an otherwise ambiguous context. One way to do this is, of
course, to provide an explicit type signature, but passing a
constructor is another, and rather elegant, way to achieve that.

I am not a heavy user of the newtype package, so I can't say how often
this situation occurs in practice.

Roman



More information about the Haskell-Cafe mailing list