Skip to content

Commit

Permalink
WIP, refactor random walk 1
Browse files Browse the repository at this point in the history
  • Loading branch information
turion committed Dec 5, 2023
1 parent 14af945 commit 6656c54
Show file tree
Hide file tree
Showing 8 changed files with 242 additions and 120 deletions.
3 changes: 0 additions & 3 deletions cabal.project
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
packages: */*.cabal

program-options
ghc-options: -freverse-errors -fmax-errors=1
1 change: 1 addition & 0 deletions rhine-bayes/app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import Control.Monad.Trans.MSF.Except

-- rhine
import FRP.Rhine hiding (Rhine, flow, sn)
import FRP.Rhine.Rhine.Free
import FRP.Rhine.SN.Free

-- rhine-gloss
Expand Down
25 changes: 15 additions & 10 deletions rhine-examples/src/ADSR.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ when the user stops pressing the key.
module Main where

-- rhine
import FRP.Rhine
import FRP.Rhine hiding (Rhine, flow, (-->), (>--), (>>>^), (@@), (^>>>))
import FRP.Rhine.Rhine.Free
import FRP.Rhine.SN.Free

-- * The definition of an ADSR

Expand Down Expand Up @@ -133,8 +135,10 @@ linearly timeSpan initialAmplitude finalAmplitude overdue = proc _ -> do
let
remainingTime = timeSpan - time
currentLevel =
( initialAmplitude * remainingTime
+ finalAmplitude * time
( initialAmplitude
* remainingTime
+ finalAmplitude
* time
)
/ timeSpan
_ <- throwOn' -< (remainingTime < 0, remainingTime)
Expand Down Expand Up @@ -203,14 +207,15 @@ release r s = linearly r s 0 0
-- * The main program

-- | A signal that alternates between 'False' and 'True' on every console newline.
key :: Rhine IO StdinClock () Bool
key = (count @Integer >>^ odd) @@ StdinClock
key :: Rhine IO UTCTime '[StdinClock] () (At StdinClock Bool)
key = Present ^>>> (count @Integer >>^ odd) @@ StdinClock

{- | Output the current amplitude of the ADSR hull on the console,
every 0.03 seconds.
-}
consoleADSR :: Rhine IO (Millisecond 30) Bool ()
consoleADSR = runADSR myADSR >-> arrMCl print @@ waitClock
-- | Output is produced every 0.03 seconds
type OutputClock = Millisecond 30

-- | Output the current amplitude of the ADSR hull on the console.
consoleADSR :: Rhine IO UTCTime '[OutputClock] (At OutputClock Bool) ()
consoleADSR = (runADSR myADSR >-> arrMCl print @@ waitClock) >>>^ const ()

{- | Runs the main program, where you have the choice between console output
and pulse output.
Expand Down
19 changes: 13 additions & 6 deletions rhine-examples/src/Demonstration.hs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
{-# LANGUAGE Arrows #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}

import FRP.Rhine
import FRP.Rhine hiding (Rhine, flow, sn, (-->), (>--), (@@), (^>>>))
import FRP.Rhine.Rhine.Free
import FRP.Rhine.SN.Free

{- | Create a simple message containing the time stamp since initialisation,
for each tick of the clock.
Expand Down Expand Up @@ -48,11 +52,14 @@ printEverySecond = arrMCl print
-}
main :: IO ()
main =
flow $
ms500 @@ waitClock |@|
ms1200 @@ waitClock
>-- collect
--> printEverySecond @@ waitClock
flow
$ Rhine
{ clocks = waitClock @500 .:. waitClock @1200 .:. waitClock @1000 .:. cnil
, sn = proc _ -> do
msg500 <- resampling collect <<< synchronous ms500 -< Present ()
msg1200 <- resampling collect <<< synchronous ms1200 -< Present ()
synchronous printEverySecond -< (++) <$> msg500 <*> msg1200
}

{- | Rhine prevents the consumption of a signal at a different clock than it is created,
if no explicit resampling strategy is given.
Expand Down
6 changes: 4 additions & 2 deletions rhine-examples/src/HelloWorld.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{-# LANGUAGE DataKinds #-}

import FRP.Rhine
import FRP.Rhine hiding ((^>>>), (@@), flow)
import FRP.Rhine.Rhine.Free
import FRP.Rhine.SN.Free

main :: IO ()
main = flow $ constMCl (putStrLn "Hello World!") @@ (waitClock :: Millisecond 100)
main = flow $ Present ^>>> constMCl (putStrLn "Hello World!") @@ (waitClock :: Millisecond 100)
37 changes: 21 additions & 16 deletions rhine-examples/src/RandomWalk.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ The internal state is a point in 2D space.
Every millisecond, a unit step is taken in a random direction along either the X or Y axis.
The current position and the distance to the origin is shown, as well as the position and distance to a saved point.
(A point can be saved by pressing enter.)
This mainly exists to test the 'feedbackRhine' construct.
-}
module Main where

Expand All @@ -20,28 +18,29 @@ import System.Random
import Data.Vector2

-- rhine
import FRP.Rhine
import FRP.Rhine hiding (flow, sn, Rhine)
import FRP.Rhine.SN.Free
import FRP.Rhine.Rhine.Free

type Point = Vector2 Float

type SimulationClock = Millisecond 1
type DisplayClock = Millisecond 1000
type AppClock = SequentialClock StdinClock (SequentialClock SimulationClock DisplayClock)
type AppClock = '[StdinClock, SimulationClock, DisplayClock]

{- | On every newline, show the current point and the local time.
Also, forward the current point so it can be saved.
-}
keyboard :: ClSF IO StdinClock ((), Point) Point
keyboard = proc ((), currentPoint) -> do
keyboard :: ClSF IO StdinClock Point Point
keyboard = proc currentPoint -> do
arrMCl putStrLn -< "Saving: " ++ show currentPoint
debugLocalTime -< ()
returnA -< currentPoint

{- | Every millisecond, go one step up, down, right or left.
Also, forward the current point when it was marked by the last newline.
-}
simulation :: ClSF IO SimulationClock Point (Point, Point)
simulation = feedback zeroVector $ proc (savedPoint, lastPoint) -> do
simulation :: ClSF IO SimulationClock () Point
simulation = feedback zeroVector $ proc ((), lastPoint) -> do
direction <- constMCl $ randomRIO (0, 3 :: Int) -< ()
let
shift = case direction of
Expand All @@ -51,12 +50,12 @@ simulation = feedback zeroVector $ proc (savedPoint, lastPoint) -> do
3 -> vector2 0 1
_ -> error "simulation: Internal error"
nextPoint = lastPoint ^+^ shift
returnA -< ((savedPoint, nextPoint), nextPoint)
returnA -< (nextPoint, nextPoint)

{- | Every second, display the current simulated point and the point saved by the keyboard,
together with the distances from current point to origin and saved point, respectively.
-}
display :: ClSF IO DisplayClock (Point, Point) ((), Point)
display :: ClSF IO DisplayClock (Point, Point) ()
display = proc (savedPoint, currentPoint) -> do
let
distanceOrigin = norm currentPoint
Expand All @@ -69,7 +68,6 @@ display = proc (savedPoint, currentPoint) -> do
, "Distance to origin: " ++ show distanceOrigin
, "Distance to saved: " ++ show distanceSaved
]
returnA -< ((), currentPoint)

-- | A helper to observe the difference between time since clock initialisation and local time
debugLocalTime :: BehaviourF IO UTCTime a a
Expand All @@ -80,10 +78,17 @@ debugLocalTime = proc a -> do
returnA -< a

-- | Wire together all components
mainRhine :: Rhine IO AppClock () ()
mainRhine =
feedbackRhine (debugLocalTime ^->> keepLast zeroVector) $
keyboard @@ StdinClock >-- keepLast zeroVector --> simulation @@ waitClock >-- keepLast (zeroVector, zeroVector) --> display @@ waitClock
mainRhine :: Rhine IO UTCTime AppClock () ()
mainRhine = Rhine
{ clocks = StdinClock .:. waitClock .:. waitClock .:. cnil
, sn = feedbackSN (debugLocalTime ^->> keepLast zeroVector) $ proc (lastPoint, ()) -> do
savedPoint <- resampling (keepLast zeroVector) <<< synchronous keyboard -< lastPoint
currentPoint <- resampling (keepLast zeroVector) <<< synchronous simulation -< pure ()
synchronous display -< (,) <$> savedPoint <*> currentPoint
returnA -< (currentPoint, ())
}
-- feedbackRhine (debugLocalTime ^->> keepLast zeroVector) $
-- keyboard @@ StdinClock >-- keepLast zeroVector --> simulation @@ waitClock >-- keepLast (zeroVector, zeroVector) --> display @@ waitClock

-- | Execute the main Rhine
main :: IO ()
Expand Down
4 changes: 3 additions & 1 deletion rhine/rhine.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ library
FRP.Rhine.ResamplingBuffer.MSF
FRP.Rhine.ResamplingBuffer.Timeless
FRP.Rhine.ResamplingBuffer.Util
FRP.Rhine.Rhine.Free
FRP.Rhine.Schedule
FRP.Rhine.SN
FRP.Rhine.SN.Combinators
Expand Down Expand Up @@ -142,7 +143,8 @@ library
, time-domain ^>= 0.1.0.2
, monad-schedule ^>= 0.1.2
, free-category ^>= 0.0.4.5
, generics-sop ^>= 0.5.1.3
, sop-core ^>= 0.5.0.2
, profunctors ^>= 5.6.2

-- Directories containing source files.
hs-source-dirs: src
Expand Down
Loading

0 comments on commit 6656c54

Please sign in to comment.