LLVM-22 removes all Type and Value creation functions that rely on a global Context. This forces me to make Contexts explicit in the llvm-tf package. Now I had the crazy idea to turn the Contexts into type parameters like so: {-# LANGUAGE DataKinds #-} {-# LANGUAGE TypeAbstractions #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE KindSignatures #-} module LLVM.Core.CodeGenTest where import qualified LLVM.FFI.Core as FFI import Foreign.ForeignPtr (ForeignPtr, withForeignPtr) import Foreign.C.String (withCString) import Data.Int (Int64) newtype Value (context :: ForeignPtr FFI.Context) a = Value FFI.ValueRef This would allow me to define functions like "add" that ensure that both operands and the result are allocated in the same Context: newtype CodeGenFunction (context :: ForeignPtr FFI.Context) r a = CodeGenFunction (IO a) add :: Value ctx a -> Value ctx a -> CodeGenFunction ctx r (Value ctx a) However, that crazy idea stops working when I actually try to construct a Value: int64Value :: forall ctx. Int64 -> IO (Value ctx Int64) int64Value @context n = withForeignPtr context $ \ctx -> withCString (show n) $ \cstr -> do typ <- FFI.intTypeInContext ctx 64 fmap Value $ FFI.constIntOfString typ cstr 10 GHC-10.0 rejects with: • Illegal term-level use of the type variable ‘context’ Is it expected that this would work with further extensions from the Dependent Haskell roadmap? With what extensions? Or is it already possible with todays extensions?
On Fri, Dec 04, 2026 at 11:54:48PM +0100, Henning Thielemann wrote:
However, that crazy idea stops working when I actually try to construct a Value:
int64Value :: forall ctx. Int64 -> IO (Value ctx Int64) int64Value @context n = withForeignPtr context $ \ctx -> withCString (show n) $ \cstr -> do typ <- FFI.intTypeInContext ctx 64 fmap Value $ FFI.constIntOfString typ cstr 10
GHC-10.0 rejects with:
• Illegal term-level use of the type variable ‘context’
Is it expected that this would work with further extensions from the Dependent Haskell roadmap? With what extensions? Or is it already possible with todays extensions?
In principle it works with the singletons library: int64Value :: forall ctx. SingKind (ForeignPtr FFIContext) => SingI cxt => Int64 -> IO (Value ctx Int64) int64Value @context n = withForeignPtr (demote context) $ \ctx -> withCString (show n) $ \cstr -> do typ <- FFI.intTypeInContext ctx 64 fmap Value $ FFI.constIntOfString typ cstr 10 However, in practice you'd have to work out a way of writing SingI instance for types of kind `ForeignPtr t`. I suppose it's possible that you could do what `KnownNat` does (`KnownNat` is basically `SingI Nat`) and define newtype SForeignPtr a (p :: ForeignPtr a) = UnsafeForeignPtr_ (ForeignPtr a) Maybe that gives you somewhere to start looking? Tom
participants (2)
-
Henning Thielemann -
Tom Ellis