packages feed

feldspar-language-0.5.0.1: Examples/Simple/SizeInference.hs

module Examples.Simple.SizeInference where



import qualified Prelude as P

import Feldspar
import Feldspar.Compiler hiding (setLength)



-- This example shows that size inference works through `forLoop` if the step
-- function does not increase the size of the state.
reverse :: Type a => Data [a] -> Data [a]
reverse a = forLoop l a $ \i a' -> setIx a' i (a ! (l-i-1))
  where
    l = getLength a

test_reverse = icompile $ reverse
    -:: tArr1 tI32 >-> id
    -:: notAbove 100 |> id >-> id



-- This example shows that size inference fails if the step function increases
-- the size of the state.
reverseBad :: Type a => Data [a] -> Data [a]
reverseBad a = forLoop l a $ \i a' -> setLength (l-1) $ setIx a' i (a ! (l-i-1))
  where
    l = getLength a

test_reverseBad = drawDecor $ reverseBad
    -:: tArr1 tI32 >-> id
    -:: notAbove 100 |> id >-> id