The impossible happended, GHCi told me to report, so I'm doing it
Summary
Write a brief description of the issue.
Steps to reproduce
This is the GHCi session:
λ> :l uffa.hs
[1 of 1] Compiling Main ( uffa.hs, interpreted )
Ok, one module loaded.
λ> encode $ int2bits 10
[[0,1,14,15],[2,3,12,13],[4,5,10,11],[6,7,8,9]]
λ> encode $ int2bits 100
[[0,1,2,3,4,5,6,7,8,126,127],[9,10,11,12,13,14,15,16,17,117,118,119,120,121,122,123,124,125],[18,19,20,21,22,23,24,25,26,108,109,110,111,112,113,114,115,116],[27,28,29,30,31,32,33,34,35,99,100,101,102,103,104,105,106,107],[36,37,38,39,40,41,42,43,44,90,91,92,93,94,95,96,97,98],[45,46,47,48,49,50,51,52,53,81,82,83,84,85,86,87,88,89],[54,55,56,57,58,59,60,61,62,72,73,74,75,76,77,78,79,80]]
λ> x = encode $ int2bits 10000000
λ> x
*** Exception: heap overflow
λ> x = encode $ int2bits 10000000
λ> x = encode $ int2bits 10000000^?^?^[^Cghc: panic! (the 'impossible' happened)
(GHC version 8.10.4:
heap overflow
Please report this as a GHC bug: https://www.haskell.org/ghc/reportabug
and this is the uffa.hs
file:
import Data.Bits
import Data.List.Split (chunksOf)
type Bit = Bool
encode :: [Bit] -> [[Int]]
encode bs = take (length table `div` 2) $ zipWith (++) table (reverse table)
where
n = length bs
c = 2^n
s = c `div` n
table = chunksOf (s `div` 2) [0..(c - 1)]
int2bits :: Int -> [Bit]
int2bits = reverse . int2bits_helper
where
int2bits_helper x
| x == 0 = []
| otherwise = if even x
then False:int2bits_helper (x `div` 2)
else True:int2bits_helper ((x - 1) `div` 2)
Expected behavior
See summary.
Environment
- GHC version used: 8.10.4
Optional:
- Operating System: Archlinux
- System Architecture: