Is combining StaticPointers and TemplateHaskell safe?

Facundo Domínguez facundo.dominguez at tweag.io
Tue Sep 4 15:48:50 UTC 2018


Hello,

> The question, is this safe in general?

It looks like it would work as long as the meta expression is spliced
in the library where the static form is defined, or in a library
depending on it.

> would it be acceptable to add an extra field to `StaticPtr a` which contained a `TExp a` value?

That could work. It would produce a copy of the value though, rather
than giving the value in the SPT.

Best,
Facundo

On Tue, Sep 4, 2018 at 11:18 AM Matthew Pickering
<matthewtpickering at gmail.com> wrote:
>
> I can implement a function which acts like `lift` using static
> pointers in the following way:
>
> ```
> liftS :: StaticPtr a ->  Q (TExp a)
> liftS sp =
>   let sk = staticKey sp
>   in [|| deRefStaticPtr (fromJust (unsafePerformIO
> (unsafeLookupStaticPtr sk)))  ||]
> ```
>
> The question, is this safe in general? It seems to me that this
> referencing should be safe because
> I could have achieved the same without quoting using `deRefStaticPtr`
> directly. The only reason I have to use the `unsafeLookup` function is
> because `StaticPtr` is not an instance of `Lift`.
>
> However, it's a bit annoying that this lookup has to be deferred to
> runtime, would it be acceptable to add an extra field to `StaticPtr a`
> which contained a `TExp a` value? Then the implementation of `liftS`
> just extracts this value from the `StaticPtr`.
>
> Using `static` is preferable to using `lift` in many situations as
> `Lift` is implemented using a type class it can't lift things such as
> 1. functions and 2. top-level identifiers. Further to this, it's
> annoying to have to incur a `Lift a` constraint when trying to lift
> `Nothing`. `static` has none of these problems.
>
> Cheers,
>
> Matt
> _______________________________________________
> ghc-devs mailing list
> ghc-devs at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs


More information about the ghc-devs mailing list