Discussion:
[Haskell-cafe] Help understanding this type
Guru Devanla
2018-11-06 16:35:56 UTC
Permalink
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#t:XEvent>

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
Zemyla
2018-11-06 16:40:18 UTC
Permalink
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.
Post by Guru Devanla
Hello Haskell-Cafe,
I have been recently studying the XMonad code and some related types
available in the X11 bindings library.
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#t:XEvent>
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
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Branimir Maksimovic
2018-11-06 16:46:39 UTC
Permalink
I use that all the time. It is for typechecking things passed to C
functions.

Greets, Branimir.
Post by Guru Devanla
Hello Haskell-Cafe,
I have been recently studying the XMonad code and some related types
available in the X11 bindings library.
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#t:XEvent>
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
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Theodore Lief Gannon
2018-11-07 10:28:53 UTC
Permalink
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.
Post by Guru Devanla
Hello Haskell-Cafe,
I have been recently studying the XMonad code and some related types
available in the X11 bindings library.
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#t:XEvent>
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
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Guru Devanla
2018-11-07 13:20:16 UTC
Permalink
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!
Post by 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.
Post by Guru Devanla
Hello Haskell-Cafe,
I have been recently studying the XMonad code and some related types
available in the X11 bindings library.
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#t:XEvent>
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
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Brandon Allbery
2018-11-07 15:49:59 UTC
Permalink
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.
Post by 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)
newtype XEvent = XEvent (Ptr XEvent). -- a recursive newtype that cannot be dereferenced
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!
Post by 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.
Post by Guru Devanla
Hello Haskell-Cafe,
I have been recently studying the XMonad code and some related types
available in the X11 bindings library.
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#t:XEvent>
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
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
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
***@gmail.com
Kim-Ee Yeoh
2018-11-08 03:42:48 UTC
Permalink
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:Ptr>r
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?
Post by 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.
Post by 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)
newtype XEvent = XEvent (Ptr XEvent). -- a recursive newtype that cannot be dereferenced
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!
Post by 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.
Post by Guru Devanla
Hello Haskell-Cafe,
I have been recently studying the XMonad code and some related types
available in the X11 bindings library.
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#t:XEvent>
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
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
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
--
-- Kim-Ee
Brandon Allbery
2018-11-08 03:46:54 UTC
Permalink
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.
Post by 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:Ptr>r
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?
Post by 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.
Post by 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)
newtype XEvent = XEvent (Ptr XEvent). -- a recursive newtype that cannot be dereferenced
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!
Post by 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.
Post by Guru Devanla
Hello Haskell-Cafe,
I have been recently studying the XMonad code and some related types
available in the X11 bindings library.
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#t:XEvent>
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
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
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
--
-- Kim-Ee
--
brandon s allbery kf8nh
***@gmail.com
Loading...