1

I have implemented a simple hash process in a bloom filter. How can I implement the same process by using clash?

Here is the Haskell code I have already implemented:

module testbloom where 
data HashTable = HashTable { location :: Int,  value :: [Int] } 
data HashFunction = HashFunction { hashfunction :: HashTable -> Int -> Int } 
data BloomFilter = BloomFilter { hashF :: [HashFunction], table :: HashTable } 

hashtable :: Int -> HashTable 
hashtable loc = HashTable { location = loc, value = replicate loc 0 } 

setbloom :: BloomFilter 
setbloom = BloomFilter { hashF = [defaultfunction], table = hashtable 32 } 

defaultfunction :: HashFunction 
defaultfunction = HashFunction { hashfunction = function } 

function :: HashTable -> Int -> Int 
function va t = t `mod` ( location va ) 

hash :: HashFunction -> HashTable -> Int -> Int 
hash func hash t = ( hashfunction func ) hash t 

replacebit :: [Int] -> Int -> Int -> [Int] 
replacebit hashtable posit v = a ++ (v:tail ax) 
        where (a,ax) = splitAt posit hashtable  

setToOne :: Int -> HashTable -> HashTable 
setToOne t hashtable = hashtable { value = values } 
         where values = replacebit (value hashtable) t 1 

fill :: BloomFilter -> [Int] -> BloomFilter 
fill bf t = bf {table = tables} 
  where tables = foldr setToOne initialbloom $ concat [ [ hash f initialbloom v  | v <- t] | f <- fns ] 
         initialbloom = table setbloom 
         fns = hashF setbloom 

judge:: BloomFilter -> Int -> Bool 
judge setbloom v  
  | vs == [1]   = True 
  | otherwise   = False 
  where vs = [(value bs) !! pos | pos <- positions] 
        positions = [ hash hf bs v | hf <- hashF setbloom] 
        bs = table setbloom  

bloom :: [Int] -> Int -> Bool 
bloom vs v = judge (fill setbloom vs) v 

test :: [Int] -> [Int] 
test vs = value $ table $ fill setbloom vs

How to convert it to clash-lang, thanks for help.

Cactus
  • 27,075
  • 9
  • 69
  • 149
Antony
  • 11
  • 3
  • please reformat with code formatting – cccnrc Jul 29 '19 at 14:08
  • Start by making sure everything is fixed-size. For example, instead of `[Int]`, use a `BitVector 32` or a `Vec 32 Bit` in `HashTable`'s `value` field. – Cactus Aug 01 '19 at 03:34

0 Answers0