google / google/codeworld

Reflex as a backend for all CodeWorld programs

Open
#1,040 0 comments 0 reactions 0 assignees View on GitHub
cleanup
Dominant language
Haskell
Stars
1.3k
Forks
201
PR merge metrics
No merged PRs in 30d

Description

After #1038 is done, it will become feasible to implement the traditional CodeWorld entry points in terms of the new Reflex backend. The existing entry points will behave the same as they do today, but instead of more hand-coded event loops, they will translate their input to FRP.

Naively, something like this would do it:

```haskell
import qualified CodeWorld.Reflex as CW.R
import qualified Reflex as R

import CodeWorld hiding (drawingOf, animationOf, activityOf)

drawingOf :: Picture -> IO ()
drawingOf pic = CW.R.reflexOf $ const $ return $ R.constDyn pic

animationOf :: (Double -> Picture) -> IO ()
animationOf frame = CW.R.reflexOf $ \input ->
return (frame <$> CW.R.currentTime input)

activityOf :: a -> (Event -> a -> a) -> (a -> Picture) -> IO ()
activityOf initial change picture = CW.R.reflexOf $ \input -> do
let events = R.mergeWith (.) [
change <$> KeyPress <$> CW.R.keyPress input,
change <$> KeyRelease <$> CW.R.keyRelease input,
change <$> TextEntry <$> CW.R.textEntry input,
change <$> PointerPress <$> CW.R.pointerPress input,
change <$> PointerRelease <$> CW.R.pointerRelease input,
change <$> PointerMovement <$> R.updated (CW.R.pointerPosition input),
change <$> TimePassing <$> CW.R.timePassing input
]
state <- R.foldDyn ($) initial events
return (picture <$> state)
```

The real world is not so clean, though.

* There are debug controls that cannot be implemented for arbitrary reflex programs, such as the time-traveling debugger. These will need to be implemented at the translation layer instead.
* Time-step elision is an optimization performed by observing sharing in the `change` function to learn about whether events need to be delivered, or the screen redrawn. It needs to be implemented in the transition, as well.
* `groupActivityOf` is still an open question. It has its own implementation anyway (this is why it has never even supported inspection), but it would be awesome if it could also be implemented in the same framework.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.