Skip to content

Instantly share code, notes, and snippets.

@johannes-riecken
Last active November 18, 2020 07:55
Show Gist options
  • Save johannes-riecken/8882107a4d8f409fc59b8e2bfdaa4751 to your computer and use it in GitHub Desktop.
Save johannes-riecken/8882107a4d8f409fc59b8e2bfdaa4751 to your computer and use it in GitHub Desktop.
Integral using causal commutative arrows
import Control.Arrow
import Control.Arrow.Operations
import Control.Arrow.Transformer.All
import Data.Stream
frequency :: Int
frequency = 1024
exp :: StreamArrow (->) () Double
exp = fixA (integral >>> arr (+1))
fixA :: ArrowLoop a => a b b -> a () b
fixA f = loop (second f >>> arr (\((), y) -> (y, y)))
integral :: ArrowCircuit a => a Double Double
integral = loop (arr (\(v, i) -> i + dt * v) >>> delay 0 >>> arr diag)
diag :: a -> (a, a)
diag x = (x, x)
units = Cons () units
dt :: Double
dt = recip $ fromIntegral frequency
main :: IO ()
main = print $ case (fixA (integral >>> arr (+1))) of StreamArrow x -> x units Data.Stream.!! frequency
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment