[Haskell-cafe] Folded long string literals and CPP?

Viktor Dukhovni ietf-dane at dukhovni.org
Thu Nov 23 06:41:49 UTC 2023


On Tue, Nov 21, 2023 at 05:36:13PM -0800, Jeff Clites via Haskell-Cafe wrote:

> I like it. I would be interesting (though perhaps overkill) to have a special operator which is like ++ but which can only be applied to list literals (not only strings), and which is guaranteed to be syntactic sugar for the concatenated list. One step fancier would be to allow it not just for literals but for any known-at-compile-time values. (So it’s syntactically an operator but in actuality more of a compiler directive, to evaluate the concatenation at compile time, or fail if that’s not possible.)

Well, I guess we already have that in the form of Template Haskell
splices, but that's rather a heavy hammer to swat this particular
fly...

    {-# LANGUAGE TemplateHaskell #-}
    module Data.CompileTime(compileTimeString) where
    import Language.Haskell.TH.Syntax as TH

    compileTimeString :: TH.Quote m => String -> TH.Code m String
    compileTimeString str = let !lit = str in [|| lit ||]

Which when imported into:

    {-# LANGUAGE CPP, TemplateHaskell #-}
    module Main(main) where
    import Data.CompileTime

    hello :: String
    hello = $$( compileTimeString $ "Hello" ++ " World!" )

    main :: IO ()
    main = print hello

Produces the "Core" below:

    ...
    main :: IO ()
    main
      = print ($fShowList $fShowChar) (unpackCString# "Hello World!"#)

-- 
    Viktor.


More information about the Haskell-Cafe mailing list