{-| Instead of checking time-stamps we compute a hash of the module source and
    store it in the interface file. This module contains the functions to do
    that. -}
module Agda.Utils.Hash where

import Data.ByteString as B
import Data.Word
import qualified Data.Hash as H
import qualified Data.List as L
import Data.Digest.Murmur64
import qualified Data.Text.Encoding as T
import Data.Text.Lazy (Text)
import qualified Data.Text.Lazy as T

import Agda.Utils.FileName
import Agda.Utils.IO.UTF8 (readTextFile)

type Hash = Word64

hashByteString :: ByteString -> Hash
hashByteString :: ByteString -> Hash
hashByteString = Hash -> Hash
H.asWord64 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> Word8 -> a) -> a -> ByteString -> a
B.foldl' (\Hash
h Word8
b -> Hash -> Hash -> Hash
H.combine Hash
h (Word8 -> Hash
H.hashWord8 Word8
b)) (Word8 -> Hash
H.hashWord8 Word8
0)

hashTextFile :: AbsolutePath -> IO Hash
hashTextFile :: AbsolutePath -> IO Hash
hashTextFile AbsolutePath
file = Text -> Hash
hashText forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> String -> IO Text
readTextFile (AbsolutePath -> String
filePath AbsolutePath
file)

-- | Hashes a piece of 'Text'.

hashText :: Text -> Hash
hashText :: Text -> Hash
hashText = ByteString -> Hash
hashByteString forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ByteString
T.encodeUtf8 forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text
T.toStrict

combineHashes :: [Hash] -> Hash
combineHashes :: [Hash] -> Hash
combineHashes [Hash]
hs = Hash -> Hash
H.asWord64 forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
L.foldl' Hash -> Hash -> Hash
H.combine (Word8 -> Hash
H.hashWord8 Word8
0) forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
L.map forall a. Hashable a => a -> Hash
H.hash [Hash]
hs

-- | Hashing a module name for unique identifiers.
hashString :: String -> Word64
hashString :: String -> Hash
hashString = Hash64 -> Hash
asWord64 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Hashable64 a => a -> Hash64
hash64