Help understanding this type

Hello Haskell-Cafe, I have been recently studying the XMonad code and some related types available in the X11 bindings library. I came across this type: newtype XEvent = XEvent XEventPtr type XEventPtr = Ptr http://hackage.haskell.org/package/base-4.10.1.0/docs/Foreign-Ptr.html#t:Ptr XEvent http://hackage.haskell.org/package/X11-1.9/docs/Graphics-X11-Xlib-Event.html... Available here: http://hackage.haskell.org/package/X11-1.9/docs/Graphics-X11-Xlib-Event.html... It seems that this type is circular here. how does one use this type? Is it possible to create a value out of this type? What is the use of this type? Please could someone help me wrap my head around this? Thanks

The type on "Ptr" doesn't actually affect what's inside; it's a convenience
for programmers. The functions that do things with XEvents just read bytes
from the Ptr's location.
On Tue, Nov 6, 2018, 10:36 Guru Devanla Hello Haskell-Cafe, I have been recently studying the XMonad code and some related types
available in the X11 bindings library. I came across this type: newtype XEvent = XEvent XEventPtr type XEventPtr = Ptr
http://hackage.haskell.org/package/base-4.10.1.0/docs/Foreign-Ptr.html#t:Ptr
XEvent
http://hackage.haskell.org/package/X11-1.9/docs/Graphics-X11-Xlib-Event.html... Available here: http://hackage.haskell.org/package/X11-1.9/docs/Graphics-X11-Xlib-Event.html... It seems that this type is circular here. how does one use this type? Is
it possible to create a value out of this type? What is the use of this
type? Please could someone help me wrap my head around this? Thanks _______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.

I use that all the time. It is for typechecking things passed to C functions. Greets, Branimir. On 11/6/18 5:35 PM, Guru Devanla wrote:
Hello Haskell-Cafe,
I have been recently studying the XMonad code and some related types available in the X11 bindings library.
I came across this type:
newtype XEvent = XEvent XEventPtr
type XEventPtr = Ptr http://hackage.haskell.org/package/base-4.10.1.0/docs/Foreign-Ptr.html#t:Ptr XEvent http://hackage.haskell.org/package/X11-1.9/docs/Graphics-X11-Xlib-Event.html...
Available here: http://hackage.haskell.org/package/X11-1.9/docs/Graphics-X11-Xlib-Event.html...
It seems that this type is circular here. how does one use this type? Is it possible to create a value out of this type? What is the use of this type?
Please could someone help me wrap my head around this?
Thanks
_______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.

The meaning of XEvent is different on the left and right sides of the
newtype declaration. On the left, it's a type; on the right, it's a data
constructor. The constructor wraps a value which refers to the type, but
not recursively back to the constructor itself.
On Tue, Nov 6, 2018, 8:36 AM Guru Devanla Hello Haskell-Cafe, I have been recently studying the XMonad code and some related types
available in the X11 bindings library. I came across this type: newtype XEvent = XEvent XEventPtr type XEventPtr = Ptr
http://hackage.haskell.org/package/base-4.10.1.0/docs/Foreign-Ptr.html#t:Ptr
XEvent
http://hackage.haskell.org/package/X11-1.9/docs/Graphics-X11-Xlib-Event.html... Available here: http://hackage.haskell.org/package/X11-1.9/docs/Graphics-X11-Xlib-Event.html... It seems that this type is circular here. how does one use this type? Is
it possible to create a value out of this type? What is the use of this
type? Please could someone help me wrap my head around this? Thanks _______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.

Thank you all for the responses, After reading through the responses, it
became clear that this is a commonly pattern used in Haskell FFI
programming.
I went back and reviewed the chapter in RWH. There is a discussion on this
pattern, under 'Typed Pointers'.
http://book.realworldhaskell.org/read/interfacing-with-c-the-ffi.html
Which means similar to the type listed in the chapter
newtype PCRE = PCRE (Ptr PCRE)
The example I provided reduces to:
newtype XEvent = XEvent (Ptr XEvent). -- a recursive newtype that
cannot be dereferenced
In summary:
1. This pattern can be use to specify the types just for type-checking
within Haskell. They do not contain any data. Therefore, not data can
be cerated for XEvent.
2. Data can be created for type `Ptr XEvent` using the `alloca`
interface, and only way to de-reference these values would be through
`peek`. But, while derefencing the resulting values will have other
types.
Next, I plan to spend some time working on these examples to get a better sense.
Thank you all!
On Wed, Nov 7, 2018 at 2:29 AM Theodore Lief Gannon
The meaning of XEvent is different on the left and right sides of the newtype declaration. On the left, it's a type; on the right, it's a data constructor. The constructor wraps a value which refers to the type, but not recursively back to the constructor itself.
On Tue, Nov 6, 2018, 8:36 AM Guru Devanla
Hello Haskell-Cafe,
I have been recently studying the XMonad code and some related types available in the X11 bindings library.
I came across this type:
newtype XEvent = XEvent XEventPtr
type XEventPtr = Ptr http://hackage.haskell.org/package/base-4.10.1.0/docs/Foreign-Ptr.html#t:Ptr XEvent http://hackage.haskell.org/package/X11-1.9/docs/Graphics-X11-Xlib-Event.html...
Available here:
http://hackage.haskell.org/package/X11-1.9/docs/Graphics-X11-Xlib-Event.html...
It seems that this type is circular here. how does one use this type? Is it possible to create a value out of this type? What is the use of this type?
Please could someone help me wrap my head around this?
Thanks
_______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.

The concept you are looking for is "phantom type": a type that is used
solely as a "tag" of sorts, with no associated data. The Ptr type
constructor uses a phantom type so you can differentiate between machine
addresses pointing at different kinds of data and gain some small measure
of safety.
On Wed, Nov 7, 2018 at 8:20 AM Guru Devanla
Thank you all for the responses, After reading through the responses, it became clear that this is a commonly pattern used in Haskell FFI programming.
I went back and reviewed the chapter in RWH. There is a discussion on this pattern, under 'Typed Pointers'. http://book.realworldhaskell.org/read/interfacing-with-c-the-ffi.html
Which means similar to the type listed in the chapter
newtype PCRE = PCRE (Ptr PCRE)
The example I provided reduces to: newtype XEvent = XEvent (Ptr XEvent). -- a recursive newtype that cannot be dereferenced
In summary:
1. This pattern can be use to specify the types just for type-checking within Haskell. They do not contain any data. Therefore, not data can be cerated for XEvent. 2. Data can be created for type `Ptr XEvent` using the `alloca` interface, and only way to de-reference these values would be through `peek`. But, while derefencing the resulting values will have other types.
Next, I plan to spend some time working on these examples to get a better sense.
Thank you all!
On Wed, Nov 7, 2018 at 2:29 AM Theodore Lief Gannon
wrote: The meaning of XEvent is different on the left and right sides of the newtype declaration. On the left, it's a type; on the right, it's a data constructor. The constructor wraps a value which refers to the type, but not recursively back to the constructor itself.
On Tue, Nov 6, 2018, 8:36 AM Guru Devanla
Hello Haskell-Cafe,
I have been recently studying the XMonad code and some related types available in the X11 bindings library.
I came across this type:
newtype XEvent = XEvent XEventPtr
type XEventPtr = Ptr http://hackage.haskell.org/package/base-4.10.1.0/docs/Foreign-Ptr.html#t:Ptr XEvent http://hackage.haskell.org/package/X11-1.9/docs/Graphics-X11-Xlib-Event.html...
Available here:
http://hackage.haskell.org/package/X11-1.9/docs/Graphics-X11-Xlib-Event.html...
It seems that this type is circular here. how does one use this type? Is it possible to create a value out of this type? What is the use of this type?
Please could someone help me wrap my head around this?
Thanks
_______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.
_______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.
-- brandon s allbery kf8nh allbery.b@gmail.com

Normally phantom types do add type safety.
But Ptr provides castPtr :: Ptr
http://hackage.haskell.org/package/base-4.10.1.0/docs/Foreign-Ptr.html#t:Ptr
a
-> Pt
http://hackage.haskell.org/package/base-4.10.1.0/docs/Foreign-Ptr.html#t:Ptrr
b, which throws type safety out the window.
Newtype is applied to ameliorate the situation, but I don't see how to make
type safety airtight, do you?
On Wednesday, November 7, 2018, Brandon Allbery
The concept you are looking for is "phantom type": a type that is used solely as a "tag" of sorts, with no associated data. The Ptr type constructor uses a phantom type so you can differentiate between machine addresses pointing at different kinds of data and gain some small measure of safety.
On Wed, Nov 7, 2018 at 8:20 AM Guru Devanla
wrote: Thank you all for the responses, After reading through the responses, it became clear that this is a commonly pattern used in Haskell FFI programming.
I went back and reviewed the chapter in RWH. There is a discussion on this pattern, under 'Typed Pointers'. http://book.realworldhaskell.org/read/interfacing-with-c-the-ffi.html
Which means similar to the type listed in the chapter
newtype PCRE = PCRE (Ptr PCRE)
The example I provided reduces to: newtype XEvent = XEvent (Ptr XEvent). -- a recursive newtype that cannot be dereferenced
In summary:
1. This pattern can be use to specify the types just for type-checking within Haskell. They do not contain any data. Therefore, not data can be cerated for XEvent. 2. Data can be created for type `Ptr XEvent` using the `alloca` interface, and only way to de-reference these values would be through `peek`. But, while derefencing the resulting values will have other types.
Next, I plan to spend some time working on these examples to get a better sense.
Thank you all!
On Wed, Nov 7, 2018 at 2:29 AM Theodore Lief Gannon
wrote: The meaning of XEvent is different on the left and right sides of the newtype declaration. On the left, it's a type; on the right, it's a data constructor. The constructor wraps a value which refers to the type, but not recursively back to the constructor itself.
On Tue, Nov 6, 2018, 8:36 AM Guru Devanla
Hello Haskell-Cafe,
I have been recently studying the XMonad code and some related types available in the X11 bindings library.
I came across this type:
newtype XEvent = XEvent XEventPtr
type XEventPtr = Ptr http://hackage.haskell.org/package/base-4.10.1.0/docs/Foreign-Ptr.html#t:Ptr XEvent http://hackage.haskell.org/package/X11-1.9/docs/Graphics-X11-Xlib-Event.html...
Available here: http://hackage.haskell.org/package/X11-1.9/docs/Graphics- X11-Xlib-Event.html#t:XEvent
It seems that this type is circular here. how does one use this type? Is it possible to create a value out of this type? What is the use of this type?
Please could someone help me wrap my head around this?
Thanks
_______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.
_______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.
-- brandon s allbery kf8nh allbery.b@gmail.com
-- -- Kim-Ee

It's a pointer to raw memory, presumably obtained from C code; full type
safety is impossible, and no type system can help you with things like C
not distinguishing between pointers and arrays in function parameters and
not having arrays that know their sizes.
On Wed, Nov 7, 2018 at 10:42 PM Kim-Ee Yeoh
Normally phantom types do add type safety.
But Ptr provides castPtr :: Ptr http://hackage.haskell.org/package/base-4.10.1.0/docs/Foreign-Ptr.html#t:Ptr a -> Pt http://hackage.haskell.org/package/base-4.10.1.0/docs/Foreign-Ptr.html#t:Ptrr b, which throws type safety out the window.
Newtype is applied to ameliorate the situation, but I don't see how to make type safety airtight, do you?
On Wednesday, November 7, 2018, Brandon Allbery
wrote: The concept you are looking for is "phantom type": a type that is used solely as a "tag" of sorts, with no associated data. The Ptr type constructor uses a phantom type so you can differentiate between machine addresses pointing at different kinds of data and gain some small measure of safety.
On Wed, Nov 7, 2018 at 8:20 AM Guru Devanla
wrote: Thank you all for the responses, After reading through the responses, it became clear that this is a commonly pattern used in Haskell FFI programming.
I went back and reviewed the chapter in RWH. There is a discussion on this pattern, under 'Typed Pointers'. http://book.realworldhaskell.org/read/interfacing-with-c-the-ffi.html
Which means similar to the type listed in the chapter
newtype PCRE = PCRE (Ptr PCRE)
The example I provided reduces to: newtype XEvent = XEvent (Ptr XEvent). -- a recursive newtype that cannot be dereferenced
In summary:
1. This pattern can be use to specify the types just for type-checking within Haskell. They do not contain any data. Therefore, not data can be cerated for XEvent. 2. Data can be created for type `Ptr XEvent` using the `alloca` interface, and only way to de-reference these values would be through `peek`. But, while derefencing the resulting values will have other types.
Next, I plan to spend some time working on these examples to get a better sense.
Thank you all!
On Wed, Nov 7, 2018 at 2:29 AM Theodore Lief Gannon
wrote: The meaning of XEvent is different on the left and right sides of the newtype declaration. On the left, it's a type; on the right, it's a data constructor. The constructor wraps a value which refers to the type, but not recursively back to the constructor itself.
On Tue, Nov 6, 2018, 8:36 AM Guru Devanla
Hello Haskell-Cafe,
I have been recently studying the XMonad code and some related types available in the X11 bindings library.
I came across this type:
newtype XEvent = XEvent XEventPtr
type XEventPtr = Ptr http://hackage.haskell.org/package/base-4.10.1.0/docs/Foreign-Ptr.html#t:Ptr XEvent http://hackage.haskell.org/package/X11-1.9/docs/Graphics-X11-Xlib-Event.html...
Available here:
http://hackage.haskell.org/package/X11-1.9/docs/Graphics-X11-Xlib-Event.html...
It seems that this type is circular here. how does one use this type? Is it possible to create a value out of this type? What is the use of this type?
Please could someone help me wrap my head around this?
Thanks
_______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.
_______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.
-- brandon s allbery kf8nh allbery.b@gmail.com
-- -- Kim-Ee
-- brandon s allbery kf8nh allbery.b@gmail.com
participants (6)
-
Brandon Allbery
-
Branimir Maksimovic
-
Guru Devanla
-
Kim-Ee Yeoh
-
Theodore Lief Gannon
-
Zemyla