Re: [Haskell-cafe] sizeOf on a type

2010-01-14 Thread Henning Thielemann


On Fri, 25 Dec 2009, Lennart Augustsson wrote:


sizeOfPtr :: Ptr a - Int
sizeOfPtr = sizeOf . (undefined :: Ptr a - a)

No need for scoped type variables.  But it does assume sizeOf does not
use its argument.


That's even better. I'll add that to the Wiki.

  
http://www.haskell.org/haskellwiki/Scoped_type_variables#Avoiding_Scoped_Type_Variables___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] sizeOf on a type

2009-12-22 Thread Henning Thielemann


On Fri, 18 Dec 2009, Gregory Crosswhite wrote:


Yay, advancePtr is exactly what I needed!  I totally missed that one in the 
docs.

Also thanks to those of you who pointed me to the scoped type variables 
feature, since I had figured that a feature liked that had to exist but 
I just didn't know where to look for it.


You do not need the Scoped Type Variables feature here, although it may 
simplify writing. I remember we had a similar thread here, recently. A 
solution can be:


sizeOfPtr :: Ptr a - a - Int
sizeOfPtr _ x = sizeOf x

to be called by (sizeOfPtr ptr undefined).

I have added this example to:
 
http://www.haskell.org/haskellwiki/Scoped_type_variables#Avoiding_Scoped_Type_Variables

But in order to be found in Category:FAQ we might need a different title.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] sizeOf on a type

2009-12-19 Thread wren ng thornton

Sean Leather wrote:

I can see what's going on:  it can't tell that the a I am writing there
is the same a that's in the type specification, but is there any way that
I can make it identify a with the a in the specification for nextPtr?



Lexically scoped type variables:
http://www.haskell.org/ghc/docs/latest/html/users_guide/other-type-extensions.html#scoped-type-variables


And if you require H98+FFI only, you can also use the following:

ptrSizeOf = sizeOf . asTypeOf undefined . unsafePerformIO . peek

The asTypeOf function is the H98 way of handling many scoped type 
variable issues. It never evaluates its second argument so the 
unsafePerformIO is perfectly safe since the peek will never be evaluated 
either (and, heck, the whole argument to sizeOf won't be evaluated either).


--
Live well,
~wren
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] sizeOf on a type

2009-12-18 Thread Gregory Crosswhite
Hey everyone,

I would like to write a routine like

nextPtr :: Storable a = Ptr a - Ptr a
nextPtr = (`plusPtr` sizeOf (undefined :: a))

which increments a pointer by the correct amount, but GHC complains that the 
type variable a is ambiguous.  I can see what's going on:  it can't tell that 
the a I am writing there is the same a that's in the type specification, 
but is there any way that I can make it identify a with the a in the 
specification for nextPtr?

Cheers,
Greg

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


Re: [Haskell-cafe] sizeOf on a type

2009-12-18 Thread Thomas DuBuisson
 I would like to write a routine like

nextPtr :: Storable a = Ptr a - Ptr a
nextPtr = (`plusPtr` sizeOf (undefined :: a))

How about:
getA :: Ptr a - a
getA _ = undefined

nextPtr ptr = (`plusPtr` sizeOf (getA ptr)) ptr

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


Re: [Haskell-cafe] sizeOf on a type

2009-12-18 Thread Sean Leather
 I can see what's going on:  it can't tell that the a I am writing there
 is the same a that's in the type specification, but is there any way that
 I can make it identify a with the a in the specification for nextPtr?


Lexically scoped type variables:
http://www.haskell.org/ghc/docs/latest/html/users_guide/other-type-extensions.html#scoped-type-variables

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


Re: [Haskell-cafe] sizeOf on a type

2009-12-18 Thread Patai Gergely
Hi,

 I would like to write a routine like
 
   nextPtr :: Storable a = Ptr a - Ptr a
   nextPtr = (`plusPtr` sizeOf (undefined :: a))
 
 which increments a pointer by the correct amount, but
 GHC complains that the type variable a is ambiguous.

Maybe Foreign.Marshal.Array.advancePtr is what you really need in this
case.

Gergely

-- 
http://www.fastmail.fm - The professional email service

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


Re: [Haskell-cafe] sizeOf on a type

2009-12-18 Thread Gregory Crosswhite
Yay, advancePtr is exactly what I needed!  I totally missed that one in the 
docs.

Also thanks to those of you who pointed me to the scoped type variables 
feature, since I had figured that a feature liked that had to exist but I just 
didn't know where to look for it.

You guys rock!

Cheers,
Greg


On Dec 18, 2009, at 12:09 PM, Patai Gergely wrote:

 Hi,
 
 I would like to write a routine like
 
  nextPtr :: Storable a = Ptr a - Ptr a
  nextPtr = (`plusPtr` sizeOf (undefined :: a))
 
 which increments a pointer by the correct amount, but
 GHC complains that the type variable a is ambiguous.
 
 Maybe Foreign.Marshal.Array.advancePtr is what you really need in this
 case.
 
 Gergely
 
 -- 
 http://www.fastmail.fm - The professional email service
 
 ___
 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