Search code examples
haskellliteralstemplate-haskell

What does `BytesPrimL` represent?


I can figure out what most literals represent with ppr, e.g. WordPrimL 7 = 7##, CharPrimL 'x' = 'x'#, etc. The only constructor I can't figure out is BytesPrimL:

λ> bytes <- mallocForeignPtrArray @Word8 5
λ> let bytesLit = LitE (BytesPrimL (mkBytes bytes 0 5)
λ> ppr bytesLit
"<binary data>"

So, what code would generate an Exp with a BytesPrimL constructor? What is it supposed to represent?


Solution

  • ghci> bytes <- mallocForeignPtrArray @Word8 5
    ghci> let bytesLit = LitE (BytesPrimL (mkBytes bytes 0 5))
    ghci> :t $(pure bytesLit)
    $(pure bytesLit) :: GHC.Prim.Addr#
    

    Incorrect, see edit below:

    I'm pretty sure there is no Haskell code that corresponds to that. Instead, this is piggy-backing on the way GHC already compiles string literals to allow you to embed arbitrary binary data in the executable efficiently.

    Edit:

    ghci> :t "foo"#
    "foo"# :: GHC.Prim.Addr#
    

    It turns out there is a literal for that, when -XMagicHash is enabled.