Is combining StaticPointers and TemplateHaskell safe?

Matthew Pickering matthewtpickering at gmail.com
Tue Sep 4 14:18:47 UTC 2018


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


More information about the ghc-devs mailing list