{-# LANGUAGE NoImplicitPrelude #-}
module Copilot.Library.Stacks
( stack, stack' ) where
import Copilot.Language
stack :: (Integral a, Typed b) =>
a
-> b
-> Stream Bool
-> Stream Bool
-> Stream b
-> Stream b
stack :: a -> b -> Stream Bool -> Stream Bool -> Stream b -> Stream b
stack a
depth b
startValue
Stream Bool
popSignal Stream Bool
pushSignal Stream b
pushValue =
let depth' :: Int
depth' = a -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral a
depth
startValue' :: Stream b
startValue' = b -> Stream b
forall a. Typed a => a -> Stream a
constant b
startValue
stackValue :: Stream b -> Stream b -> Stream b
stackValue Stream b
pushValue' Stream b
popValue' =
let stackValue' :: Stream b
stackValue' = [ b
startValue ]
[b] -> Stream b -> Stream b
forall a. Typed a => [a] -> Stream a -> Stream a
++ Stream Bool -> Stream b -> Stream b -> Stream b
forall a.
Typed a =>
Stream Bool -> Stream a -> Stream a -> Stream a
mux Stream Bool
popSignal
Stream b
popValue'
( Stream Bool -> Stream b -> Stream b -> Stream b
forall a.
Typed a =>
Stream Bool -> Stream a -> Stream a -> Stream a
mux Stream Bool
pushSignal
Stream b
pushValue'
Stream b
stackValue' )
in Stream b
stackValue'
toStack :: [Stream b -> Stream b -> Stream b] -> Stream b
toStack [Stream b -> Stream b -> Stream b]
l =
let toStack' :: Stream b -> [Stream b -> Stream b -> Stream b] -> Stream b
toStack' Stream b
_ [] = Stream b
startValue'
toStack' Stream b
prev ( Stream b -> Stream b -> Stream b
sv : [Stream b -> Stream b -> Stream b]
svs ) =
let current :: Stream b
current = Stream b -> Stream b -> Stream b
sv Stream b
prev ( Stream b -> [Stream b -> Stream b -> Stream b] -> Stream b
toStack' Stream b
current [Stream b -> Stream b -> Stream b]
svs )
in Stream b
current
in Stream b -> [Stream b -> Stream b -> Stream b] -> Stream b
toStack' Stream b
pushValue [Stream b -> Stream b -> Stream b]
l
in [Stream b -> Stream b -> Stream b] -> Stream b
toStack ([Stream b -> Stream b -> Stream b] -> Stream b)
-> [Stream b -> Stream b -> Stream b] -> Stream b
forall a b. (a -> b) -> a -> b
$ Int
-> (Stream b -> Stream b -> Stream b)
-> [Stream b -> Stream b -> Stream b]
forall a. Int -> a -> [a]
replicate Int
depth' Stream b -> Stream b -> Stream b
stackValue
stack' :: (Integral a, Typed b) =>
a
-> b
-> Stream Bool
-> Stream Bool
-> Stream b
-> Stream b
stack' :: a -> b -> Stream Bool -> Stream Bool -> Stream b -> Stream b
stack' a
depth b
startValue
Stream Bool
popSignal Stream Bool
pushSignal Stream b
pushValue =
let depth' :: Int
depth' = a -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral a
depth
startValue' :: Stream b
startValue' = b -> Stream b
forall a. Typed a => a -> Stream a
constant b
startValue
stackValue :: Stream b -> Stream b -> Stream b
stackValue Stream b
pushValue' Stream b
popValue' =
let stackValue' :: Stream b
stackValue' = [ b
startValue ]
[b] -> Stream b -> Stream b
forall a. Typed a => [a] -> Stream a -> Stream a
++ Stream Bool -> Stream b -> Stream b -> Stream b
forall a.
Typed a =>
Stream Bool -> Stream a -> Stream a -> Stream a
mux Stream Bool
pushSignal
Stream b
pushValue'
( Stream Bool -> Stream b -> Stream b -> Stream b
forall a.
Typed a =>
Stream Bool -> Stream a -> Stream a -> Stream a
mux Stream Bool
popSignal
Stream b
popValue'
Stream b
stackValue' )
in Stream b
stackValue'
toStack :: [Stream b -> Stream b -> Stream b] -> Stream b
toStack [Stream b -> Stream b -> Stream b]
l =
let toStack' :: Stream b -> [Stream b -> Stream b -> Stream b] -> Stream b
toStack' Stream b
_ [] = Stream b
startValue'
toStack' Stream b
prev ( Stream b -> Stream b -> Stream b
sv : [Stream b -> Stream b -> Stream b]
svs ) =
let current :: Stream b
current = Stream b -> Stream b -> Stream b
sv Stream b
prev ( Stream b -> [Stream b -> Stream b -> Stream b] -> Stream b
toStack' Stream b
current [Stream b -> Stream b -> Stream b]
svs )
in Stream b
current
in Stream b -> [Stream b -> Stream b -> Stream b] -> Stream b
toStack' Stream b
pushValue [Stream b -> Stream b -> Stream b]
l
in [Stream b -> Stream b -> Stream b] -> Stream b
toStack ([Stream b -> Stream b -> Stream b] -> Stream b)
-> [Stream b -> Stream b -> Stream b] -> Stream b
forall a b. (a -> b) -> a -> b
$ Int
-> (Stream b -> Stream b -> Stream b)
-> [Stream b -> Stream b -> Stream b]
forall a. Int -> a -> [a]
replicate Int
depth' Stream b -> Stream b -> Stream b
stackValue