Is combining StaticPointers and TemplateHaskell safe?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Is combining StaticPointers and TemplateHaskell safe?

Matthew Pickering
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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs
Reply | Threaded
Open this post in threaded view
|

Re: Is combining StaticPointers and TemplateHaskell safe?

Facundo Domínguez-3
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
<[hidden email]> 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
> [hidden email]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs
_______________________________________________
ghc-devs mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs