[Haskell-cafe] Help understanding this type

Brandon Allbery allbery.b at gmail.com
Wed Nov 7 15:49:59 UTC 2018


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 <gurudev.devanla at gmail.com>
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 <tanuki at gmail.com>
> 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 <gurudev.devanla at gmail.com
>> 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#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
>>>
>>>
>>>
>>>
>>>
>>>
>>> _______________________________________________
>>> 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 at gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20181107/27c1ad86/attachment.html>


More information about the Haskell-Cafe mailing list