Skip to content

Instantly share code, notes, and snippets.

@gdeest
Created October 18, 2016 09:39
Show Gist options
  • Select an option

  • Save gdeest/7f8376b2c775543450da48a32c7d4b94 to your computer and use it in GitHub Desktop.

Select an option

Save gdeest/7f8376b2c775543450da48a32c7d4b94 to your computer and use it in GitHub Desktop.
{-# LANGUAGE ForeignFunctionInterface #-}
{-# LANGUAGE TemplateHaskell #-}
module Lib
(islVersion)
where
import Lib.Internal
import Foreign.C.Types
import Foreign.C.String
islVersion :: IO String
islVersion = peekCString =<< cisl_version
{-# LANGUAGE TemplateHaskell #-}
module Lib.Internal where
import Foreign.C.String (CString)
import Lib.Internal.TH
-- Illegal type variable name: ‘CString’
-- When splicing a TH declaration:
-- foreign import ccall unsafe "isl_version" cisl_version :: GHC.Types.IO Foreign.C.String.CString
$(generateDecl "cisl_version")
{-# LANGUAGE ForeignFunctionInterface #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskellQuotes #-}
module Lib.Internal.TH where
import Language.Haskell.TH (Q, Dec(..), Callconv(..), Safety(..), Type(..), lookupTypeName, mkName)
import Language.Haskell.TH.Syntax (Foreign(..))
generateDecl :: String -> Q [Dec]
generateDecl fname = do
let name = mkName fname
Just io <- lookupTypeName "IO"
Just cstring <- lookupTypeName "CString"
let retType = AppT (VarT io) (VarT cstring)
let dec = ForeignD $ ImportF CCall Unsafe "isl_version" name retType
return [dec]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment