Skip to content

Instantly share code, notes, and snippets.

@chpatrick
Last active August 10, 2021 18:11
Show Gist options
  • Save chpatrick/863a02927dc79b0b316d to your computer and use it in GitHub Desktop.
Save chpatrick/863a02927dc79b0b316d to your computer and use it in GitHub Desktop.
Baked-in Storable vectors
{-# LANGUAGE QuasiQuotes, TemplateHaskell, ScopedTypeVariables #-}
module BakedVector where
import qualified Data.ByteString.Lazy as BSL
import Data.ByteString.Lazy (unpack)
import Data.ByteString.Builder (toLazyByteString)
import Data.ByteString.Builder.Prim
import Foreign.Storable
import qualified Data.Vector.Storable as VS
import Foreign.ForeignPtr
import GHC.Ptr
import Language.Haskell.TH.Quote
import Language.Haskell.TH.Syntax
import Text.Read
import System.IO.Unsafe
-- Compiled-in Storable Vectors.
-- Example: [intVector| 1, 2, 4, 6, 7 |]
-- [intString|\xEF\xBE\xAD\xDE\NUL\NUL\NUL\NUL]
-- $(bakeVector [ 1, 2, 3 ])
class (Read a, Storable a) => Bakable a where
bakeType :: proxy a -> Q Type
bakePrim :: proxy a -> FixedPrim a
bakeString :: forall proxy a. Bakable a => proxy a -> BSL.ByteString -> Q Exp
bakeString p s =
case fromIntegral (BSL.length s) `quotRem` sizeOf (undefined :: a) of
( len, 0 ) ->
[| let ptr = unsafeDupablePerformIO $ newForeignPtr_ $ Ptr $(litE)
in VS.unsafeFromForeignPtr ptr 0 len :: VS.Vector $(bakeType p)
|] where litE = return $ LitE $ StringPrimL $ unpack s
_ -> fail "Invalid string length."
bakeVector :: Bakable a => [ a ] -> Q Exp
bakeVector xs
= bakeString xs $ toLazyByteString $ primMapListFixed (bakePrim xs) xs
quoter :: (String -> Q Exp) -> QuasiQuoter
quoter qExp = QuasiQuoter qExp unsupported unsupported unsupported
where unsupported = fail "Baked vectors must be used as expressions."
bakedString :: Bakable a => proxy a -> QuasiQuoter
bakedString p = quoter $ \s ->
case readMaybe ("\"" ++ s ++ "\"") of
Nothing -> fail "Failed to parse baked vector string."
Just s' -> bakeString p s'
bakedVector :: forall proxy a. Bakable a => proxy a -> QuasiQuoter
bakedVector _ = quoter $ \s ->
case readMaybe ("[" ++ s ++ "]") of
Nothing -> fail "Failed to parse baked vector literal."
Just xs -> bakeVector (xs :: [ a ])
instance Bakable Int where
bakeType _ = [t|Int|]
bakePrim _ = intHost
intVector :: QuasiQuoter
intVector = bakedVector intHost
intString :: QuasiQuoter
intString = bakedString intHost
instance Bakable Float where
bakeType _ = [t|Float|]
bakePrim _ = floatHost
floatVector :: QuasiQuoter
floatVector = bakedVector floatHost
floatString :: QuasiQuoter
floatString = bakedString floatHost
instance Bakable Double where
bakeType _ = [t|Double|]
bakePrim _ = doubleHost
doubleVector :: QuasiQuoter
doubleVector = bakedVector doubleHost
doubleString :: QuasiQuoter
doubleString = bakedString doubleHost
@chpatrick
Copy link
Author

The MIT License (MIT)

Copyright (c) 2015 Patrick Chilton

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment