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

2013-06-07 Thread David Banas
Hi All,

Referring to the following, which is taken from the *Control.Newtype
*documentation
page:

op :: 
Newtype
n
o => (o -> n) -> n ->
oSource

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'?

Thanks,
-db
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


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

2013-06-07 Thread Roman Cheplyaka
* David Banas  [2013-06-07 07:08:19-0700]
> Hi All,
> 
> Referring to the following, which is taken from the *Control.Newtype
> *documentation
> page:
> 
> op :: 
> Newtype
> n
> o => (o -> n) -> n ->
> oSource
> 
> 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

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


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

2013-06-07 Thread Tom Ellis
On Fri, Jun 07, 2013 at 07:08:19AM -0700, David Banas wrote:
> op :: 
> Newtype
> n
> o => (o -> n) -> n ->
> oSource
> 
> 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'?

I am puzzled by this too.

What does "op" stand for?  I hypothesis "opposite" in the sense of inverse,
since as Roman points out "op Constructor :: n -> o" is the inverse of
"Constructor :: o -> n".

But I admit I do not see how this provides value over "unpack" itself.

Tom

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


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

2013-06-07 Thread Joe Q
The phantom parameter solves the same problem as scoped type variables.
Granted, if you find yourself in that kind of polymorphic soup you have
deeper problems...
On Jun 7, 2013 2:53 PM, "Tom Ellis" <
tom-lists-haskell-cafe-2...@jaguarpaw.co.uk> wrote:

> On Fri, Jun 07, 2013 at 07:08:19AM -0700, David Banas wrote:
> > 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'?
>
> I am puzzled by this too.
>
> What does "op" stand for?  I hypothesis "opposite" in the sense of inverse,
> since as Roman points out "op Constructor :: n -> o" is the inverse of
> "Constructor :: o -> n".
>
> But I admit I do not see how this provides value over "unpack" itself.
>
> Tom
>
> ___
> Haskell-Cafe mailing list
> Haskell-Cafe@haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


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

2013-06-07 Thread Tom Ellis
On Fri, Jun 07, 2013 at 04:05:09PM -0400, Joe Q wrote:
> The phantom parameter solves the same problem as scoped type variables.
> Granted, if you find yourself in that kind of polymorphic soup you have
> deeper problems...

I don't understand this.  Scoped type variables are used when you want to
use a type variable from the top level within the body of a function.  If
you use "op" and specify a particular constructor then you don't have a
variable but a concrete instance of a type.  But maybe I'm missing some more
powerful way this can be used ...

Tom




> On Jun 7, 2013 2:53 PM, "Tom Ellis" 
>  wrote:
> > On Fri, Jun 07, 2013 at 07:08:19AM -0700, David Banas wrote:
> > > 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'?
> >
> > I am puzzled by this too.
> >
> > What does "op" stand for?  I hypothesis "opposite" in the sense of inverse,
> > since as Roman points out "op Constructor :: n -> o" is the inverse of
> > "Constructor :: o -> n".
> >
> > But I admit I do not see how this provides value over "unpack" itself.
> >
> > Tom
> >
> > ___
> > Haskell-Cafe mailing list
> > Haskell-Cafe@haskell.org
> > http://www.haskell.org/mailman/listinfo/haskell-cafe
> >

> ___
> Haskell-Cafe mailing list
> Haskell-Cafe@haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


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

2013-06-07 Thread Joe Quinn

On 6/7/2013 4:51 PM, Tom Ellis wrote:

On Fri, Jun 07, 2013 at 04:05:09PM -0400, Joe Q wrote:

The phantom parameter solves the same problem as scoped type variables.
Granted, if you find yourself in that kind of polymorphic soup you have
deeper problems...

I don't understand this.  Scoped type variables are used when you want to
use a type variable from the top level within the body of a function.  If
you use "op" and specify a particular constructor then you don't have a
variable but a concrete instance of a type.  But maybe I'm missing some more
powerful way this can be used ...

Tom


You can use scoped type variables to correct an ambiguous type error.

You can think of op as a variation on asTypeOf, as documented here on 
http://www.haskell.org/haskellwiki/Scoped_type_variables#Avoiding_Scoped_Type_Variables.


If I tried to come up with an example that's specific to op, it would 
only be horribly contrived.


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


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

2013-06-12 Thread David Banas
Ah, so is the idea, then, to use *op()* when `n` wasn't actually
constructed formally, but rather "assembled" by the user, so as to match
the type of the accessor function normally supplied as the argument to the
constructor?


On 6/7/2013 4:51 PM, Tom Ellis wrote:
> On Fri, Jun 07, 2013 at 04:05:09PM -0400, Joe Q wrote:
>> The phantom parameter solves the same problem as scoped type variables.
>> Granted, if you find yourself in that kind of polymorphic soup you have
>> deeper problems...
> I don't understand this.  Scoped type variables are used when you want to
> use a type variable from the top level within the body of a function.  If
> you use "op" and specify a particular constructor then you don't have a
> variable but a concrete instance of a type.  But maybe I'm missing some
more
> powerful way this can be used ...
>
> Tom

You can use scoped type variables to correct an ambiguous type error.

You can think of op as a variation on asTypeOf, as documented here on
http://www.haskell.org/haskellwiki/Scoped_type_variables#Avoiding_Scoped_Type_Variables
.

If I tried to come up with an example that's specific to op, it would
only be horribly contrived.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe