<commit_before>{-|
Module: Flaw.Build
Description: Basic definitions for build.
License: MIT
-}

{-# LANGUAGE TemplateHaskell #-}

module Flaw.Build
	( loadFile
	, Embed(..)
	, EmbedIO(..)
	, embedStringExp
	, fileExp
	, packList
	, packVector
	, genEmbed
	) where

import Control.Monad
import qualified Data.ByteString as B
import qualified Data.ByteString.Unsafe as B
import qualified Data.ByteString.Lazy as BL
import qualified Data.Text as T
import qualified Data.Vector.Generic as VG
import qualified Data.Vector.Storable as VS
import Foreign.Marshal.Array
import Foreign.Ptr
import Foreign.Storable
import Language.Haskell.TH
import Language.Haskell.TH.Syntax(qAddDependentFile)
import System.IO.Unsafe

-- | Load file and add it as a dependency.
loadFile :: FilePath -> Q BL.ByteString
loadFile filePath = do
	fileData <- runIO $ BL.readFile filePath
	qAddDependentFile filePath
	return fileData

-- | Class of pure data may be embedded.
-- An embedded expression is of type a.
class Embed a where
	embedExp :: a -> Q Exp

instance Embed Int where
	embedExp n = litE $ integerL $ fromIntegral n

instance Embed Float where
	embedExp n = litE $ rationalL $ toRational n

instance Embed Double where
	embedExp n = litE $ rationalL $ toRational n

instance Embed Bool where
	embedExp b = if b then (conE 'True) else (conE 'False)

instance Embed T.Text where
	embedExp t = [| T.pack $(litE $ stringL $ T.unpack t) |]

instance Embed Name where
	embedExp n = [| mkName $(litE $ stringL $ (maybe "" (++ ".") $ nameModule n) ++ nameBase n) |]

instance Embed B.ByteString where
	embedExp bytes = [| unsafePerformIO $(embedIOExp bytes) |]

instance Embed BL.ByteString where
	embedExp bytes = [| unsafePerformIO $(embedIOExp bytes) |]

instance Embed a => Embed [a] where
	embedExp a = listE $ map embedExp a

instance (Embed a, Embed b) => Embed (a, b) where
	embedExp (a, b) = [| ($(embedExp a), $(embedExp b)) |]

instance (Embed a, Embed b, Embed c) => Embed (a, b, c) where
	embedExp (a, b, c) = [| ($(embedExp a), $(embedExp b), $(embedExp c)) |]

instance (Embed a, Embed b, Embed c, Embed d) => Embed (a, b, c, d) where
	embedExp (a, b, c, d) = [| ($(embedExp a), $(embedExp b), $(embedExp c), $(embedExp d)) |]

-- | Class of data may be embedded.
-- An embedded expression is of type IO a.
class EmbedIO a where
	-- | Construct an expression for the data (of type 'IO a').
	embedIOExp :: a -> Q Exp

instance EmbedIO Int where
	embedIOExp n = [| return $(embedExp n) |]

instance EmbedIO Bool where
	embedIOExp b = [| return $(embedExp b) |]

instance EmbedIO B.ByteString where
	embedIOExp bytes = do
		[| B.unsafePackAddressLen $(litE $ integerL $ fromIntegral $ B.length bytes) $(litE $ stringPrimL $ B.unpack bytes) |]

instance EmbedIO BL.ByteString where
	embedIOExp bytes = do
		[| B.unsafePackAddressLen $(litE $ integerL $ fromIntegral $ BL.length bytes) $(litE $ stringPrimL $ BL.unpack bytes) |]

instance EmbedIO a => EmbedIO [a] where
	embedIOExp a = [| sequence $(listE $ map embedIOExp a) |]

instance (EmbedIO a, EmbedIO b) => EmbedIO (a, b) where
	embedIOExp (a, b) = [| do
		av <- $(embedIOExp a)
		bv <- $(embedIOExp b)
		return (av, bv)
		|]

instance (EmbedIO a, EmbedIO b, EmbedIO c) => EmbedIO (a, b, c) where
	embedIOExp (a, b, c) = [| do
		av <- $(embedIOExp a)
		bv <- $(embedIOExp b)
		cv <- $(embedIOExp c)
		return (av, bv, cv)
		|]
<commit_msg>fix wrong type of embedded lazy bytestring<commit_after>{-|
Module: Flaw.Build
Description: Basic definitions for build.
License: MIT
-}

{-# LANGUAGE TemplateHaskell #-}

module Flaw.Build
	( loadFile
	, Embed(..)
	, EmbedIO(..)
	, embedStringExp
	, fileExp
	, packList
	, packVector
	, genEmbed
	) where

import Control.Monad
import qualified Data.ByteString as B
import qualified Data.ByteString.Unsafe as B
import qualified Data.ByteString.Lazy as BL
import qualified Data.Text as T
import qualified Data.Vector.Generic as VG
import qualified Data.Vector.Storable as VS
import Foreign.Marshal.Array
import Foreign.Ptr
import Foreign.Storable
import Language.Haskell.TH
import Language.Haskell.TH.Syntax(qAddDependentFile)
import System.IO.Unsafe

-- | Load file and add it as a dependency.
loadFile :: FilePath -> Q BL.ByteString
loadFile filePath = do
	fileData <- runIO $ BL.readFile filePath
	qAddDependentFile filePath
	return fileData

-- | Class of pure data may be embedded.
-- An embedded expression is of type a.
class Embed a where
	embedExp :: a -> Q Exp

instance Embed Int where
	embedExp n = litE $ integerL $ fromIntegral n

instance Embed Float where
	embedExp n = litE $ rationalL $ toRational n

instance Embed Double where
	embedExp n = litE $ rationalL $ toRational n

instance Embed Bool where
	embedExp b = if b then (conE 'True) else (conE 'False)

instance Embed T.Text where
	embedExp t = [| T.pack $(litE $ stringL $ T.unpack t) |]

instance Embed Name where
	embedExp n = [| mkName $(litE $ stringL $ (maybe "" (++ ".") $ nameModule n) ++ nameBase n) |]

instance Embed B.ByteString where
	embedExp bytes = [| unsafePerformIO $(embedIOExp bytes) |]

instance Embed BL.ByteString where
	embedExp bytes = [| unsafePerformIO $(embedIOExp bytes) |]

instance Embed a => Embed [a] where
	embedExp a = listE $ map embedExp a

instance (Embed a, Embed b) => Embed (a, b) where
	embedExp (a, b) = [| ($(embedExp a), $(embedExp b)) |]

instance (Embed a, Embed b, Embed c) => Embed (a, b, c) where
	embedExp (a, b, c) = [| ($(embedExp a), $(embedExp b), $(embedExp c)) |]

instance (Embed a, Embed b, Embed c, Embed d) => Embed (a, b, c, d) where
	embedExp (a, b, c, d) = [| ($(embedExp a), $(embedExp b), $(embedExp c), $(embedExp d)) |]

-- | Class of data may be embedded.
-- An embedded expression is of type IO a.
class EmbedIO a where
	-- | Construct an expression for the data (of type 'IO a').
	embedIOExp :: a -> Q Exp

instance EmbedIO Int where
	embedIOExp n = [| return $(embedExp n) |]

instance EmbedIO Bool where
	embedIOExp b = [| return $(embedExp b) |]

instance EmbedIO B.ByteString where
	embedIOExp bytes = do
		[| B.unsafePackAddressLen $(litE $ integerL $ fromIntegral $ B.length bytes) $(litE $ stringPrimL $ B.unpack bytes) |]

instance EmbedIO BL.ByteString where
	embedIOExp bytes = do
		[| liftM BL.fromStrict $ B.unsafePackAddressLen $(litE $ integerL $ fromIntegral $ BL.length bytes) $(litE $ stringPrimL $ BL.unpack bytes) |]

instance EmbedIO a => EmbedIO [a] where
	embedIOExp a = [| sequence $(listE $ map embedIOExp a) |]

instance (EmbedIO a, EmbedIO b) => EmbedIO (a, b) where
	embedIOExp (a, b) = [| do
		av <- $(embedIOExp a)
		bv <- $(embedIOExp b)
		return (av, bv)
		|]

instance (EmbedIO a, EmbedIO b, EmbedIO c) => EmbedIO (a, b, c) where
	embedIOExp (a, b, c) = [| do
		av <- $(embedIOExp a)
		bv <- $(embedIOExp b)
		cv <- $(embedIOExp c)
		return (av, bv, cv)
		|]

{'fraction_non_alphanumeric': 0.121718727086083, 'fraction_numerical': 0.0, 'mean_word_length': 3.8334514528703045, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7024215', 'n_tokens_mistral': 2455, 'n_tokens_neox': 2382, 'n_words': 860}