on d20ce36 help sign in
.github/workflows/haskell.ymldeleted−47
since splitsnapshots
unifiedsplit
@@ -1,47 +0,0 @@
1−name: github-action
2−
3−on: [push, pull_request]
4−
5−jobs:
6− build:
7− strategy:
8− matrix:
9− ghc: ['8.0.2', '8.2.2', '8.4.4', '8.6.5', '8.8.4', '8.10.2']
10− os: ['ubuntu-latest', 'macos-latest']
11− exclude:
12− # There are some linker warnings in 802 on darwin that
13− # cause compilation to fail
14− # See https://github.com/NixOS/nixpkgs/issues/25139
15− - ghc: '8.0.2'
16− os: 'macos-latest'
17− runs-on: ${{ matrix.os }}
18−
19− name: GHC ${{ matrix.ghc }} on ${{ matrix.os }}
20− steps:
21− - uses: actions/checkout@v2
22− - uses: actions/setup-haskell@v1
23− with:
24− ghc-version: ${{ matrix.ghc }}
25− - name: Cache
26− uses: actions/cache@v1
27− env:
28− cache-name: cache-cabal
29− with:
30− path: ~/.cabal
31− key: ${{ runner.os }}-${{ matrix.ghc }}-build-${{ env.cache-name }}-${{ hashFiles('**/*.cabal') }}-${{ hashFiles('**/cabal.project') }}
32− restore-keys: |
33− ${{ runner.os }}-${{ matrix.ghc }}-build-${{ env.cache-name }}-
34− ${{ runner.os }}-${{ matrix.ghc }}-build-
35− ${{ runner.os }}-${{ matrix.ghc }}-
36− ${{ runner.os }}
37−
38− - name: Install dependencies
39− run: |
40− cabal update
41− cabal build --only-dependencies --enable-tests --enable-benchmarks
42− - name: Build
43− run: cabal build --enable-tests --enable-benchmarks all
44− - name: Run tests
45− run: cabal test --enable-tests all
46− - name: Build Docs
47− run: cabal haddock
.gitignore−7
@@ -1,7 +1,4 @@
1dist
1dist
2−cabal.sandbox.config
3−.cabal-sandbox/
4−dist-*
5cabal-dev
2cabal-dev
6*.o
3*.o
7*.hi
4*.hi
@@ -32,7 +29,3 @@ tags
32hsenv.log
29hsenv.log
33\#*#
30\#*#
34.#*
31.#*
35−/shell.nix
36−/ghci-tmp
37−*.dump-*
38−*.verbose-core2core
.stylish-haskell.yamldeleted−153
@@ -1,153 +0,0 @@
1−# stylish-haskell configuration file
2−# ==================================
3−
4−# The stylish-haskell tool is mainly configured by specifying steps. These steps
5−# are a list, so they have an order, and one specific step may appear more than
6−# once (if needed). Each file is processed by these steps in the given order.
7−steps:
8− # Convert some ASCII sequences to their Unicode equivalents. This is disabled
9− # by default.
10− # - unicode_syntax:
11− # # In order to make this work, we also need to insert the UnicodeSyntax
12− # # language pragma. If this flag is set to true, we insert it when it's
13− # # not already present. You may want to disable it if you configure
14− # # language extensions using some other method than pragmas. Default:
15− # # true.
16− # add_language_pragma: true
17−
18− # Import cleanup
19− - imports:
20− # There are different ways we can align names and lists.
21− #
22− # - global: Align the import names and import list throughout the entire
23− # file.
24− #
25− # - file: Like global, but don't add padding when there are no qualified
26− # imports in the file.
27− #
28− # - group: Only align the imports per group (a group is formed by adjacent
29− # import lines).
30− #
31− # - none: Do not perform any alignment.
32− #
33− # Default: global.
34− align: none
35−
36− # Folowing options affect only import list alignment.
37− #
38− # List align has following options:
39− #
40− # - after_alias: Import list is aligned with end of import including
41− # 'as' and 'hiding' keywords.
42− #
43− # > import qualified Data.List as List (concat, foldl, foldr, head,
44− # > init, last, length)
45− #
46− # - with_alias: Import list is aligned with start of alias or hiding.
47− #
48− # > import qualified Data.List as List (concat, foldl, foldr, head,
49− # > init, last, length)
50− #
51− # - new_line: Import list starts always on new line.
52− #
53− # > import qualified Data.List as List
54− # > (concat, foldl, foldr, head, init, last, length)
55− #
56− # Default: after alias
57− list_align: after_alias
58−
59− # Long list align style takes effect when import is too long. This is
60− # determined by 'columns' setting.
61− #
62− # - inline: This option will put as much specs on same line as possible.
63− #
64− # - new_line: Import list will start on new line.
65− #
66− # - new_line_multiline: Import list will start on new line when it's
67− # short enough to fit to single line. Otherwise it'll be multiline.
68− #
69− # - multiline: One line per import list entry.
70− # Type with contructor list acts like single import.
71− #
72− # > import qualified Data.Map as M
73− # > ( empty
74− # > , singleton
75− # > , ...
76− # > , delete
77− # > )
78− #
79− # Default: inline
80− long_list_align: inline
81−
82− # List padding determines indentation of import list on lines after import.
83− # This option affects 'list_align' and 'long_list_align'.
84− list_padding: 2
85−
86− # Separate lists option affects formating of import list for type
87− # or class. The only difference is single space between type and list
88− # of constructors, selectors and class functions.
89− #
90− # - true: There is single space between Foldable type and list of it's
91− # functions.
92− #
93− # > import Data.Foldable (Foldable (fold, foldl, foldMap))
94− #
95− # - false: There is no space between Foldable type and list of it's
96− # functions.
97− #
98− # > import Data.Foldable (Foldable(fold, foldl, foldMap))
99− #
100− # Default: true
101− separate_lists: true
102−
103− # Language pragmas
104− - language_pragmas:
105− # We can generate different styles of language pragma lists.
106− #
107− # - vertical: Vertical-spaced language pragmas, one per line.
108− #
109− # - compact: A more compact style.
110− #
111− # - compact_line: Similar to compact, but wrap each line with
112− # `{-#LANGUAGE #-}'.
113− #
114− # Default: vertical.
115− style: vertical
116−
117− # Align affects alignment of closing pragma brackets.
118− #
119− # - true: Brackets are aligned in same collumn.
120− #
121− # - false: Brackets are not aligned together. There is only one space
122− # between actual import and closing bracket.
123− #
124− # Default: true
125− align: false
126−
127− # stylish-haskell can detect redundancy of some language pragmas. If this
128− # is set to true, it will remove those redundant pragmas. Default: true.
129− remove_redundant: true
130−
131− # Align the types in record declarations
132− # - records: {}
133−
134− # Replace tabs by spaces. This is disabled by default.
135− # - tabs:
136− # # Number of spaces to use for each tab. Default: 8, as specified by the
137− # # Haskell report.
138− # spaces: 8
139−
140− # Remove trailing whitespace
141− - trailing_whitespace: {}
142−
143−# A common setting is the number of columns (parts of) code will be wrapped
144−# to. Different steps take this into account. Default: 80.
145−columns: 110
146−
147−# Sometimes, language extensions are specified in a cabal file or from the
148−# command line instead of using language pragmas in the file. stylish-haskell
149−# needs to be aware of these, so it can parse the file correctly.
150−#
151−# No language extensions are enabled by default.
152−language_extensions:
153− - TemplateHaskell
.travis.ymlnew file+24
@@ -0,0 +1,24 @@
1+sudo: false
2+before_install:
3+ - travis_retry cabal-$CABALVER update
4+ - export PATH=/opt/ghc/$GHCVER/bin:/opt/happy/1.19.5/bin:/opt/alex/3.1.4/bin:$PATH
5+install:
6+ - travis_retry cabal-$CABALVER install --only-dependencies -j --enable-tests
7+script:
8+ - cabal-$CABALVER configure -v2 --enable-tests
9+ - cabal-$CABALVER build
10+ - cabal-$CABALVER test
11+ - cabal-$CABALVER sdist # tests that a source-distribution can be generated
12+# Check that the resulting source distribution can be built & installed.
13+# If there are no other `.tar.gz` files in `dist`, this can be even simpler:
14+# `cabal install --force-reinstalls dist/*-*.tar.gz`
15+ - SRC_TGZ=$(cabal-$CABALVER info . | awk '{print $2;exit}').tar.gz &&
16+ (cd dist && cabal-$CABALVER install --force-reinstalls "$SRC_TGZ")
17+
18+matrix:
19+ include:
20+ - env: CABALVER=1.18 GHCVER=7.8.4
21+ addons: {apt: {packages: [cabal-install-1.18, ghc-7.8.4, alex-3.1.4, happy-1.19.5], sources: [hvr-ghc]}}
22+ - env: CABALVER=1.22 GHCVER=7.10.1
23+ addons: {apt: {packages: [cabal-install-1.22, ghc-7.10.1, alex-3.1.4, happy-1.19.5],sources: [hvr-ghc]}}
24+ fast_finish: true
CONTRIBUTING.mddeleted−66
@@ -1,66 +0,0 @@
1−# Contribution Guide
2−
3−Contributions and issue reports are encouraged and appreciated!
4−
5−- [Opening Issues](#opening-issues)
6−- [Submitting Changes](#submitting-changes)
7− - [Guidelines for Commit Messages](#guidelines-for-commit-messages)
8− - [Guidelines for Pull Requests](#guidelines-for-pull-requests)
9− - [Code Quality](#code-quality)
10− - [Documentation](#documentation)
11−
12−## Opening Issues
13−
14−Before opening an issue, please check whether your issue has already been reported. Assuming it has not:
15−
16−* Describe the issue you're encountering or the suggestion you're making
17−* Include any relevant steps to reproduce or code samples you can. It's always easier for us to debug if we have something that demonstrates the error.
18−* Let us know what version of patch you were using. If you're using a github checkout, provide the git hash.
19−* Describe how you're building patch (i.e., via reflex-platform, cabal install, stack, obelisk, etc.). If you're using reflex-platform or obelisk, provide the git hash of your checkout.
20−
21−## Submitting Changes
22−
23−### Guidelines for Commit Messages
24−
25−#### Summary Line
26−The summary line of your commit message should summarize the changes being made. Commit messages should be written in the imperative mood and should describe what happens when the commit is applied.
27−
28−One way to think about it is that your commit message should be able to complete the sentence:
29−"When applied, this commit will..."
30−
31−#### Body
32−For breaking changes, new features, refactors, or other major changes, the body of the commit message should describe the motivation behind the change in greater detail and may include references to the issue tracker. The body shouldn't repeat code/comments from the diff.
33−
34−### Guidelines for Pull Requests
35−
36−Wherever possible, pull requests should add a single feature or fix a single bug. Pull requests should not bundle several unrelated changes.
37−
38−### Code Quality
39−
40−#### Warnings
41−
42−Your pull request should add no new warnings to the project. It should also generally not disable any warnings.
43−
44−#### Build and Test
45−
46−Make sure the project builds and that the tests pass! This will generally also be checked by CI before merge, but trying it yourself first means you'll catch problems earlier and your contribution can be merged that much sooner!
47−
48−#### Dependencies
49−
50−Include version bounds whenever adding a dependency to the library stanza of the cabal file. Note that libraries added to patch.cabal also need to be added to default.nix.
51−
52−### Documentation
53−
54−#### In the code
55−We're always striving to improve documentation. Please include [haddock](https://haskell-haddock.readthedocs.io/en/latest/index.html) documentation for any added code, and update the documentation for any code you modify.
56−
57−#### In the [Changelog](ChangeLog.md)
58−Add an entry to the changelog when your PR:
59−* Adds a feature
60−* Deprecates something
61−* Includes a breaking change
62−* Makes any other change that will impact users
63−
64−#### In the [Readme](README.md)
65−The readme is the first place a lot of people look for information about the repository. Update any parts of the readme that are affected by your PR.
66−
ChangeLog.mddeleted−61
@@ -1,61 +0,0 @@
1−# Revision history for patch
2−
3−## Unreleased
4−
5−* New dep of `data-orphans` for old GHC to get instances honestly instead of
6− via `monoidal-containers`.
7−
8−## 0.0.5.0 - 2021-12-17
9−
10−* `Additive` now lives in `Data.Semigroup.Additive`, but is still reexported
11− from `Data.Patch` for compatability.
12−
13−* Rewrite `PatchMapWithMove` in terms of `PatchMapWithPatchingMove`.
14− Care is taken to make this not a breaking change.
15− In particular, `PatchMapWithMove` is a newtype of `PatchMapWithPatchingMove`, as is the `NodeInfo` and `From` of `PatchMapWithPatchingMove`'s versions of those.
16− There are complete constructor and field patterns too, and everything is
17− exported under the newtype as real constructors and fields would be.
18−
19−## 0.0.4.0 - 2021-04-20
20−
21−* Enable PolyKinds
22−
23−## 0.0.3.2 - 2020-11-06
24−
25−* Update version bounds
26−
27−## 0.0.3.1 - 2020-02-05
28−
29−* Replace `fromJust` with something easier to debug.
30−
31−## 0.0.3.0 - 2020-02-05
32−
33−* Create `PatchMapWithPatchingMove` variant which supports moves with a patch.
34−
35−* Create `DecidablyEmpty` subclass of `Monoid`.
36−
37−## 0.0.2.0 - 2020-01-17
38−
39−* Consistently provide:
40−
41− - `Wrapped` instances
42−
43− - `*WithIndex` instances
44−
45− - `un*` newtype unwrappers
46−
47− for `PatchMap`, `PatchIntMap`, and `PatchMapWithMove`.
48−
49−## 0.0.1.0 - 2020-01-09
50−
51−* Support older GHCs with `split-these` flag.
52−
53−* Additional instances for the `Group` class for basic types.
54−
55−## 0.0.0.1 - 2020-01-08
56−
57−* Remove unneeded dependencies
58−
59−## 0.0.0.0 - 2020-01-08
60−
61−* Extract patching functionality from Reflex.
Quickref.mdnew file+178
@@ -0,0 +1,178 @@
1+# Reflex Quick(ish) Reference
2+
3+## Typeclasses
4+
5+Many Reflex functions operate in monadic context `m a`, where the monad 'm' supports various additional typeclasses such as MonadWidget, MonadHold, or MonadSample in addition to Monad itself. The actual 'm' in use will be determined by the top-level entry point of the FRP host (such as Reflex-Dom -- see the bottom of the Reflex-Dom quick reference for details).
6+
7+The function signatures here have been simplified by removing many typeclass constraints and adding simple annotations to each function. Also the ubiquitous 't' type parameter has been removed.
8+
9+Some of these functions are *pure*: They operate on Events, Behaviors, or Dynamics uniformly without regard to the "current time". Other functions must operate in some monadic context, because they produce a result "as of now" (e.g., any function that takes an "initial" value, or returns a "current" value). Annotations are used to distinguish these cases:
10+
11+```haskell
12+[ ] -- Pure function
13+[S] -- Function runs in any monad supporting MonadSample
14+[H] -- Function runs in any monad supporting MonadHold
15+```
16+
17+Since MonadHold depends on MonadSample, any [S] function also runs in [H] context.
18+
19+## Functions producing Event
20+
21+```haskell
22+-- Trivial Event
23+[ ] never :: Event a
24+
25+-- Extract Event from Dynamic
26+[ ] updated :: Dynamic a -> Event a
27+
28+-- Transform Event to Event using function
29+[ ] fmap :: (a -> b) -> Event a -> Event b
30+[ ] fmapMaybe :: (a -> Maybe b) -> Event a -> Event b
31+[ ] ffilter :: (a -> Bool) -> Event a -> Event a
32+[ ] ffor :: Event a -> (a -> b) -> Event b
33+[ ] fforMaybe :: Event a -> (a -> Maybe b) -> Event b
34+
35+-- Event to identical Event with debug trace. (Only prints if Event is ultimately used.)
36+[ ] traceEvent :: Show a => String -> Event a -> Event a
37+[ ] traceEventWith :: (a -> String) -> Event a -> Event a
38+
39+-- Transform Event to Event by sampling Behavior or Dynamic
40+[ ] gate :: Behavior Bool -> Event a -> Event a
41+[ ] tag :: Behavior a -> Event b -> Event a
42+[ ] tagDyn :: Dynamic a -> Event b -> Event a
43+[ ] attach :: Behavior a -> Event b -> Event (a, b)
44+[ ] attachDyn :: Dynamic a -> Event b -> Event (a, b)
45+[ ] attachWith :: (a -> b -> c) -> Behavior a -> Event b -> Event c
46+[ ] attachDynWith :: (a -> b -> c) -> Dynamic a -> Event b -> Event c
47+[ ] attachWithMaybe :: (a -> b -> Maybe c) -> Behavior a -> Event b -> Event c
48+[ ] attachDynWithMaybe :: (a -> b -> Maybe c) -> Dynamic a -> Event b -> Event c
49+
50+-- Combine multiple Events
51+[ ] <> :: Monoid a => Event a -> Event a -> Event a
52+[ ] mergeWith :: (a -> a -> a) -> [Event a] -> Event a
53+[ ] leftmost :: [Event a] -> Event a
54+[ ] mergeList :: [Event a] -> Event (NonEmpty a)
55+[ ] merge :: GCompare k => DMap (WrapArg Event k) -> Event (DMap k)
56+[ ] mergeMap :: Ord k => Map k (Event a) -> Event (Map k a)
57+
58+-- Efficient one-to-many fanout
59+[ ] fanMap :: Ord k => Event (Map k a) -> EventSelector (Const2 k a)
60+[ ] fan :: GCompare k => Event (DMap k) -> EventSelector k
61+[ ] select :: EventSelector k -> k a -> Event a
62+
63+-- Event to Event via function that can sample current values
64+[ ] push :: (a -> m (Maybe b)) -> Event a -> Event b
65+[ ] pushAlways :: (a -> m b ) -> Event a -> Event b
66+ -- Note supplied function operates in [H] context
67+
68+-- Split Event into next occurrence and all other future occurrences, as of now.
69+[H] headE :: Event a -> m (Event a)
70+[H] tailE :: Event a -> m (Event a)
71+[H] headTailE :: Event a -> m (Event a, Event a)
72+```
73+
74+## Functions producing Behavior
75+
76+```haskell
77+-- Trivial Behavior
78+[ ] constant :: a -> Behavior a
79+
80+-- Extract Behavior from Dynamic
81+[ ] current :: Dynamic a -> Behavior a
82+
83+-- Create Behavior with given initial value, updated when the Event fires.
84+[H] hold :: a -> Event a -> m (Behavior a)
85+
86+-- Transform Behavior to Behavior using function
87+[ ] fmap :: (a -> b) -> Behavior a -> Behavior b
88+[ ] ffor :: Behavior a -> (a -> b) -> Behavior b
89+[ ] <*> :: Behavior (a -> b) -> Behavior a -> Behavior b
90+[ ] >>= :: Behavior a -> (a -> Behavior b) -> Behavior b
91+[ ] <> :: Monoid a => Behavior a -> Behavior a -> Behavior a
92+-- ... plus many more due to typeclass membership
93+
94+-- Behavior to Behavior by sampling current values
95+[S] sample :: Behavior a -> m a
96+[ ] pull :: m a -> Behavior a
97+ -- Note supplied value is in [S] context
98+```
99+
100+## Functions producing Dynamic
101+
102+```haskell
103+-- Trivial Dynamic
104+[ ] constDyn :: a -> Dynamic a
105+
106+-- Create Dynamic with given initial value, updated (various ways) when the Event fires.
107+[H] holdDyn :: a -> Event a -> m (Dynamic a)
108+[H] foldDyn :: (a -> b -> b ) -> b -> Event a -> m (Dynamic b)
109+[H] foldDynMaybe :: (a -> b -> Maybe b ) -> b -> Event a -> m (Dynamic b)
110+[H] foldDynM :: (a -> b -> m' b ) -> b -> Event a -> m (Dynamic b)
111+[H] foldDynMaybeM :: (a -> b -> m' (Maybe b)) -> b -> Event a -> m (Dynamic b)
112+ -- Note m' supplies [H] context
113+
114+-- Initial value is 0; counts Event firings from now.
115+[H] count :: Num b => Event a -> m (Dynamic b)
116+-- Initial Bool value is supplied; toggles at each Event firing.
117+[H] toggle :: Bool -> Event a -> m (Dynamic Bool)
118+
119+-- Transform Dynamic to Dynamic using function
120+[H] forDyn :: Dynamic a -> (a -> b) -> m (Dynamic b)
121+[H] mapDyn :: (a -> b) -> Dynamic a -> m (Dynamic b)
122+[H] mapDynM :: (a -> m' b) -> Dynamic a -> m (Dynamic b)
123+ -- Note m' supplies [S] context
124+[H] splitDyn :: Dynamic (a, b) -> m (Dynamic a, Dynamic b)
125+
126+-- Combine multiple Dynamics
127+[H] mconcatDyn :: Monoid a => [Dynamic a] -> m (Dynamic a)
128+[H] distributeDMapOverDyn :: GCompare k => DMap (WrapArg Dynamic k) -> m (Dynamic (DMap k))
129+[H] combineDyn :: (a -> b -> c) -> Dynamic a -> Dynamic b -> m (Dynamic c)
130+
131+-- Efficient one-to-many fanout
132+[ ] demux :: Ord k => Dynamic k -> Demux k
133+[H] getDemuxed :: Eq k => Demux k -> k -> m (Dynamic Bool)
134+
135+-- Dynamic to Dynamic, removing updates w/o value change
136+[ ] nubDyn :: Eq a => Dynamic a -> Dynamic a
137+
138+-- Dynamic to identical Dynamic with debug trace. (Only prints if Dynamic is ultimately used.)
139+[ ] traceDyn :: Show a => String -> Dynamic a -> Dynamic a
140+[ ] traceDynWith :: (a -> String) -> Dynamic a -> Dynamic a
141+```
142+
143+## Flattening functions
144+
145+These functions flatten nested types such as Event-of-Event, Behavior-of-Event, Event-of-Behavior etc, removing the outer wrapper.
146+
147+For Events, the returned Event fires whenever the latest Event supplied by the wrapper fires. There are differences in how the functions handle *coincidences* -- situations where the old or new Event fires at the same instant that we switch from old to new. In some cases, the output Event tracks the old Event at the instant of switchover, while in other cases it tracks the new Event.
148+
149+```haskell
150+-- Flatten Behavior-of-Event to Event. Old Event is used during switchover.
151+[ ] switch :: Behavior (Event a) -> Event a
152+
153+-- Flatten Dyanmic-of-Event to Event. New Event is used immediately.
154+[ ] switchPromptlyDyn :: Dynamic (Event a) -> Event a
155+
156+-- Flatten Event-of-Event to Event that fires when both wrapper AND new Event fire.
157+[ ] coincidence :: Event (Event a) -> Event a
158+
159+-- Flatten Behavior-of-Behavior to Behavior. Behavior is an instance of Monad,
160+-- so the 'join' function from 'Control.Monad' suffices. The output of 'join' is
161+-- a Behavior whose value is equal to the current value of the current value of
162+-- the input.
163+[ ] join :: Behavior (Behavior a) -> Behavior a
164+
165+
166+-- Flatten Dynamic-of-Dynamic to Dynamic. New Dynamic is used immediately.
167+-- Output updated whenever inner OR outer Dynamic updates.
168+[ ] joinDyn :: Dynamic (Dynamic a) -> Dynamic a
169+[ ] joinDynThroughMap :: Ord k => Dynamic (Map k (Dynamic a)) -> Dynamic (Map k a)
170+
171+-- Analogous to 'hold': Create a Behavior that is initially identical to the
172+-- supplied Behavior. Updated to track a new Behavior whenever the Event fires.
173+[H] switcher :: Behavior a -> Event (Behavior a) -> m (Behavior a)
174+
175+-- Similar to above, for Events. Created Event initially tracks the first argument.
176+-- At switchover, the output Event immediately tracks the new Event.
177+[H] switchPromptly :: Event a -> Event (Event a) -> m (Event a)
178+```
README.md+11−44
@@ -1,52 +1,19 @@
1−# patch
1+## Reflex
2+### Practical Functional Reactive Programming
2
3
3−[![Haskell](https://img.shields.io/badge/language-Haskell-orange.svg)](https://haskell.org) [![Hackage](https://img.shields.io/hackage/v/patch.svg)](https://hackage.haskell.org/package/patch) [![Hackage CI](https://matrix.hackage.haskell.org/api/v2/packages/patch/badge)](https://matrix.hackage.haskell.org/#/package/patch) [![Travis CI](https://api.travis-ci.org/reflex-frp/patch.svg?branch=develop)](https://travis-ci.org/reflex-frp/patch) [![BSD3 License](https://img.shields.io/badge/license-BSD3-blue.svg)](https://github.com/reflex-frp/patch/LICENSE)
4+Reflex is an fully-deterministic, higher-order Functional Reactive Programming (FRP) interface and an engine that efficiently implements that interface.
4
5
5−Data structures for describing changes to other data structures.
6+[Reflex-DOM](https://github.com/reflex-frp/reflex-dom) is a framework built on Reflex that facilitates the development of web pages, including highly-interactive single-page apps.
6
7
7−A `Patch` type represents a kind of change made to a data structure.
8+Comprehensive documentation is still a work in progress, but a tutorial for Reflex and Reflex-DOM is available at https://github.com/reflex-frp/reflex-platform and an introductory talk given at the New York Haskell Meetup is available here: [Part 1](https://www.youtube.com/watch?v=mYvkcskJbc4) / [Part 2](https://www.youtube.com/watch?v=3qfc9XFVo2c).
8
9
9−```haskell
10+A summary of Reflex functions is available in the [quick reference](Quickref.md).
10−class Patch p where
11− type PatchTarget p :: *
12− -- | Apply the patch p a to the value a. If no change is needed, return
13− -- 'Nothing'.
14− apply :: p -> PatchTarget p -> Maybe (PatchTarget p)
15−```
16
11
17−## Patching Maps
12+### Additional resources
18−For example, `Data.Patch.Map` defines the `PatchMap` type which can be used to patch `Map`s. A `PatchMap` represents updates to a `Map` that can insert, remove, or replace items in the `Map`. In this example, the `Map` is the `PatchTarget` and the `PatchMap` is the `Patch`. Keep in mind that there are many other possible `Patch`es that can be applied to a `Map` (i.e., `Map` can be the `PatchTarget` for many different `Patch` instances).
13+[Get started with Reflex](https://github.com/reflex-frp/reflex-platform)
19
14
20−`PatchMap` is defined as:
15+[/r/reflexfrp](https://www.reddit.com/r/reflexfrp)
21
16
22−```haskell
17+[hackage](https://hackage.haskell.org/package/reflex)
23−newtype PatchMap k v = PatchMap { unPatchMap :: Map k (Maybe v) }
24−```
25
18
26−The `Maybe` around the value is used to represent insertion/updates or deletion of the element at a given key.
19+[irc.freenode.net #reflex-frp](http://webchat.freenode.net?channels=%23reflex-frp&uio=d4)
27−
28−Its `Patch` instance begins with:
29−
30−```haskell
31−instance Ord k => Patch (PatchMap k v) where
32− type PatchTarget (PatchMap k v) = Map k v
33− ...
34−```
35−
36−When a `PatchMap` is applied to its `PatchTarget`, the following changes can occur:
37−
38−- If the key is present in the `Patch` and the `PatchTarget`...
39−
40− - And the `Patch` value at that key is `Nothing`: delete that key from the `PatchTarget`.
41−
42− - And the `Patch` value at that key is `Just x`: update the value at that key in the `PatchTarget` to `x`.
43−
44−- If the key is present in the `Patch` and not present in the `PatchTarget`...
45−
46− - And the `Patch` value at that key is `Nothing`: do nothing because we're trying to delete a key that doesn't exist in the target in the first place.
47−
48− - And the `Patch` value at that key is `Just x`: insert the key and the value `x` into the `PatchTarget`
49−
50−- If the key is *not* present in the `Patch` but present in the `PatchTarget`: do nothing.
51−
52−There are, of course, more complicated ways of patching maps involving, for example, moving values from one key to another. You can find the code for that in `Data.Patch.PatchMapWithMove`. Note that the `PatchTarget` type associated with the `PatchMapWithMove` patch instance is still `Map k v`!
bench-cbits/checkCapability.c+12−11
@@ -42,7 +42,7 @@ typedef struct Task_ {
42 // or just continue immediately. It's a workaround for the fact
42 // or just continue immediately. It's a workaround for the fact
43 // that signalling a condition variable doesn't do anything if the
43 // that signalling a condition variable doesn't do anything if the
44 // thread is already running, but we want it to be sticky.
44 // thread is already running, but we want it to be sticky.
45− HsBool wakeup;
45+ rtsBool wakeup;
46#endif
46#endif
47
47
48 // This points to the Capability that the Task "belongs" to. If
48 // This points to the Capability that the Task "belongs" to. If
@@ -62,14 +62,14 @@ typedef struct Task_ {
62 // The current top-of-stack InCall
62 // The current top-of-stack InCall
63 struct InCall_ *incall;
63 struct InCall_ *incall;
64
64
65− uint32_t n_spare_incalls;
65+ nat n_spare_incalls;
66 struct InCall_ *spare_incalls;
66 struct InCall_ *spare_incalls;
67
67
68− HsBool worker; // == rtsTrue if this is a worker Task
68+ rtsBool worker; // == rtsTrue if this is a worker Task
69− HsBool stopped; // this task has stopped or exited Haskell
69+ rtsBool stopped; // this task has stopped or exited Haskell
70
70
71 // So that we can detect when a finalizer illegally calls back into Haskell
71 // So that we can detect when a finalizer illegally calls back into Haskell
72− HsBool running_finalizers;
72+ rtsBool running_finalizers;
73
73
74 // Links tasks on the returning_tasks queue of a Capability, and
74 // Links tasks on the returning_tasks queue of a Capability, and
75 // on spare_workers.
75 // on spare_workers.
@@ -88,7 +88,7 @@ struct Capability_ {
88 StgFunTable f;
88 StgFunTable f;
89 StgRegTable r;
89 StgRegTable r;
90
90
91− uint32_t no; // capability number.
91+ nat no; // capability number.
92
92
93 // The Task currently holding this Capability. This task has
93 // The Task currently holding this Capability. This task has
94 // exclusive access to the contents of this Capability (apart from
94 // exclusive access to the contents of this Capability (apart from
@@ -98,12 +98,12 @@ struct Capability_ {
98
98
99 // true if this Capability is running Haskell code, used for
99 // true if this Capability is running Haskell code, used for
100 // catching unsafe call-ins.
100 // catching unsafe call-ins.
101− HsBool in_haskell;
101+ rtsBool in_haskell;
102
102
103 // Has there been any activity on this Capability since the last GC?
103 // Has there been any activity on this Capability since the last GC?
104− uint32_t idle;
104+ nat idle;
105
105
106− HsBool disabled;
106+ rtsBool disabled;
107
107
108 // The run queue. The Task owning this Capability has exclusive
108 // The run queue. The Task owning this Capability has exclusive
109 // access to its run queue, so can wake up threads without
109 // access to its run queue, so can wake up threads without
@@ -159,7 +159,7 @@ struct Capability_ {
159#if defined(THREADED_RTS)
159#if defined(THREADED_RTS)
160 // Worker Tasks waiting in the wings. Singly-linked.
160 // Worker Tasks waiting in the wings. Singly-linked.
161 Task *spare_workers;
161 Task *spare_workers;
162− uint32_t n_spare_workers; // count of above
162+ nat n_spare_workers; // count of above
163
163
164 // This lock protects:
164 // This lock protects:
165 // running_task
165 // running_task
@@ -192,9 +192,10 @@ struct Capability_ {
192
192
193 // Per-capability STM-related data
193 // Per-capability STM-related data
194 StgTVarWatchQueue *free_tvar_watch_queues;
194 StgTVarWatchQueue *free_tvar_watch_queues;
195+ StgInvariantCheckQueue *free_invariant_check_queues;
195 StgTRecChunk *free_trec_chunks;
196 StgTRecChunk *free_trec_chunks;
196 StgTRecHeader *free_trec_headers;
197 StgTRecHeader *free_trec_headers;
197− uint32_t transaction_tokens;
198+ nat transaction_tokens;
198} // typedef Capability is defined in RtsAPI.h
199} // typedef Capability is defined in RtsAPI.h
199 // We never want a Capability to overlap a cache line with anything
200 // We never want a Capability to overlap a cache line with anything
200 // else, so round it up to a cache line size:
201 // else, so round it up to a cache line size:
bench/Main.hsnew file+109
@@ -0,0 +1,109 @@
1+{-# LANGUAGE CPP #-}
2+{-# LANGUAGE BangPatterns #-}
3+{-# LANGUAGE TupleSections #-}
4+{-# LANGUAGE ScopedTypeVariables #-}
5+{-# LANGUAGE TypeFamilies #-}
6+
7+-- The instance for NFData (TVar a) is an orphan, but necessary here
8+{-# OPTIONS_GHC -fno-warn-orphans #-}
9+
10+module Main where
11+
12+import Data.Functor.Misc
13+import Control.Monad.IO.Class
14+import Control.Monad.Identity
15+import Data.Dependent.Sum
16+import Control.Concurrent.STM
17+import Data.IORef
18+import Control.DeepSeq
19+import Control.Exception (evaluate)
20+import Reflex
21+import Reflex.Host.Class
22+import Criterion.Main
23+import Data.Dependent.Map (DMap)
24+import qualified Data.Dependent.Map as DMap
25+
26+main :: IO ()
27+main = defaultMain
28+ [ bgroup "micro" micros ]
29+
30+#if !(MIN_VERSION_deepseq(1,4,2))
31+instance NFData (IORef a) where
32+ rnf x = seq x ()
33+#endif
34+
35+instance NFData (TVar a) where
36+ rnf x = seq x ()
37+
38+newtype WHNF a = WHNF a
39+instance NFData (WHNF a) where
40+ rnf (WHNF a) = seq a ()
41+
42+withSetup :: NFData b => String -> SpiderHost Global a -> (a -> SpiderHost Global b) -> Benchmark
43+withSetup name setup action = env (WHNF <$> runSpiderHost setup) $ \ ~(WHNF a) ->
44+ bench name . nfIO $ runSpiderHost (action a)
45+
46+withSetupWHNF :: String -> SpiderHost Global a -> (a -> SpiderHost Global b) -> Benchmark
47+withSetupWHNF name setup action = env (WHNF <$> runSpiderHost setup) $ \ ~(WHNF a) ->
48+ bench name . whnfIO $ runSpiderHost (action a)
49+
50+micros :: [Benchmark]
51+micros =
52+ [ bench "newIORef" $ whnfIO $ void $ newIORef ()
53+ , env (newIORef (42 :: Int)) (bench "readIORef" . whnfIO . readIORef)
54+ , bench "newTVar" $ whnfIO $ void $ newTVarIO ()
55+ , env (newTVarIO (42 :: Int)) (bench "readTVar" . whnfIO . readTVarIO)
56+ , bench "newEventWithTrigger" $ whnfIO . void $ runSpiderHost $ newEventWithTrigger $
57+ \trigger -> return () <$ evaluate trigger
58+ , bench "newEventWithTriggerRef" $ whnfIO . void $ runSpiderHost newEventWithTriggerRef
59+ , withSetupWHNF "subscribeEvent" newEventWithTriggerRef $ subscribeEvent . fst
60+ , withSetupWHNF "subscribeSwitch"
61+ (join $ hold <$> fmap fst newEventWithTriggerRef <*> fmap fst newEventWithTriggerRef)
62+ (subscribeEvent . switch)
63+ , withSetupWHNF "subscribeMerge(1)" (setupMerge 1) $ \(ev,_) -> subscribeEvent ev
64+ , withSetupWHNF "subscribeMerge(100)" (setupMerge 100) (subscribeEvent . fst)
65+ , withSetupWHNF "subscribeMerge(10000)" (setupMerge 10000) (subscribeEvent . fst)
66+ , bench "runHostFrame" $ whnfIO $ runSpiderHost $ runHostFrame $ return ()
67+ , withSetupWHNF "fireEventsAndRead(single/single)"
68+ (newEventWithTriggerRef >>= subscribePair)
69+ (\(subd, trigger) -> fireAndRead trigger (42 :: Int) subd)
70+ , withSetupWHNF "fireEventsOnly"
71+ (newEventWithTriggerRef >>= subscribePair)
72+ (\(_, trigger) -> do
73+ Just key <- liftIO $ readIORef trigger
74+ fireEvents [key :=> Identity (42 :: Int)])
75+ , withSetupWHNF "fireEventsAndRead(head/merge1)"
76+ (setupMerge 1 >>= subscribePair)
77+ (\(subd, t:_) -> fireAndRead t (42 :: Int) subd)
78+ , withSetupWHNF "fireEventsAndRead(head/merge100)"
79+ (setupMerge 100 >>= subscribePair)
80+ (\(subd, t:_) -> fireAndRead t (42 :: Int) subd)
81+ , withSetupWHNF "fireEventsAndRead(head/merge10000)"
82+ (setupMerge 10000 >>= subscribePair)
83+ (\(subd, t:_) -> fireAndRead t (42 :: Int) subd)
84+ , withSetupWHNF "fireEventsOnly(head/merge100)"
85+ (setupMerge 100 >>= subscribePair)
86+ (\(_, t:_) -> do
87+ Just key <- liftIO $ readIORef t
88+ fireEvents [key :=> Identity (42 :: Int)])
89+ , withSetupWHNF "hold" newEventWithTriggerRef $ \(ev, _) -> hold (42 :: Int) ev
90+ , withSetupWHNF "sample" (newEventWithTriggerRef >>= hold (42 :: Int) . fst) sample
91+ ]
92+
93+setupMerge :: Int
94+ -> SpiderHost Global ( Event (SpiderEnv Global) (DMap (Const2 Int a) Identity)
95+ , [IORef (Maybe (EventTrigger Spider a))]
96+ )
97+setupMerge num = do
98+ (evs, triggers) <- unzip <$> replicateM num newEventWithTriggerRef
99+ let !m = DMap.fromList [(Const2 i) :=> v | (i,v) <- zip [0..] evs]
100+ pure (merge m, triggers)
101+
102+subscribePair :: (Event (SpiderEnv Global) a, b) -> SpiderHost Global (EventHandle (SpiderEnv Global) a, b)
103+subscribePair (ev, b) = (,b) <$> subscribeEvent ev
104+
105+fireAndRead :: IORef (Maybe (EventTrigger (SpiderEnv Global) a)) -> a -> EventHandle (SpiderEnv Global) b
106+ -> SpiderHost Global (Maybe b)
107+fireAndRead trigger val subd = do
108+ Just key <- liftIO $ readIORef trigger
109+ fireEventsAndRead [key :=> Identity val] $ readEvent subd >>= sequence
bench/RunAll.hsnew file+143
@@ -0,0 +1,143 @@
1+{-# LANGUAGE ConstraintKinds, TypeSynonymInstances, BangPatterns, ScopedTypeVariables, TupleSections, GADTs, RankNTypes, FlexibleInstances, FlexibleContexts, MultiParamTypeClasses, GeneralizedNewtypeDeriving, ForeignFunctionInterface, ViewPatterns, TemplateHaskell, PatternSynonyms #-}
2+{-# OPTIONS_GHC -fno-warn-orphans #-}
3+
4+module Main where
5+
6+import Criterion.Main
7+import Criterion.Types
8+
9+import Reflex
10+import Reflex.Host.Class
11+
12+import Reflex.TestPlan
13+import Reflex.Plan.Reflex
14+
15+import Reflex.Spider.Internal (SpiderEventHandle)
16+import qualified Reflex.Bench.Focused as Focused
17+
18+import Control.Applicative
19+import Control.DeepSeq (NFData (..))
20+
21+import System.IO
22+import System.Mem
23+import Prelude
24+
25+import Data.IORef
26+import System.Mem.Weak
27+import Data.Function
28+import Control.Concurrent
29+import Data.Time.Clock
30+import GHC.Stats
31+import Data.Monoid
32+import Control.Arrow
33+import Control.Monad
34+import Data.Bool
35+import Control.Concurrent.STM
36+import Data.Int
37+import Control.Exception
38+import Text.Read
39+import System.Environment
40+import Debug.Trace.LocationTH
41+import Control.Monad.Trans
42+import System.Process
43+
44+import Unsafe.Coerce
45+
46+import Data.Map (Map)
47+import qualified Data.Map as Map
48+
49+type MonadReflexHost' t m = (MonadReflexHost t m, MonadIORef m, MonadIORef (HostFrame t))
50+
51+
52+setupFiring :: (MonadReflexHost t m, MonadIORef m) => Plan t (Event t a) -> m (EventHandle t a, Schedule t)
53+setupFiring p = do
54+ (e, s) <- runPlan p
55+ h <- subscribeEvent e
56+ return (h, s)
57+
58+-- Hack to avoid the NFData constraint for EventHandle which is a synonym
59+newtype Ignore a = Ignore a
60+instance NFData (Ignore a) where
61+ rnf !_ = ()
62+
63+instance NFData (SpiderEventHandle a) where
64+ rnf !_ = ()
65+
66+instance NFData (Behavior t a) where
67+ rnf !_ = ()
68+
69+instance NFData (Firing t) where
70+ rnf !(Firing _ _) = ()
71+
72+-- Measure the running time
73+benchFiring :: forall t m. (MonadReflexHost' t m, MonadSample t m) => (forall a. m a -> IO a) -> TestCase -> Int -> IO ()
74+benchFiring runHost tc n = runHost $ do
75+ let runIterations :: m a -> m ()
76+ runIterations test = replicateM_ (10*n) $ do
77+ result <- test
78+ liftIO $ evaluate result
79+ case tc of
80+ TestE p -> do
81+ (h, s) <- setupFiring p
82+ runIterations $ readSchedule_ s $ readEvent' h
83+ TestB p -> do
84+ (b, s) <- runPlan p
85+ runIterations $ readSchedule_ (makeDense s) $ sample b
86+
87+waitForFinalizers :: IO ()
88+waitForFinalizers = do
89+ performGC
90+ x <- getCurrentTime
91+ isFinalized <- newIORef False
92+ mkWeakPtr x $ Just $ writeIORef isFinalized True
93+ performGC
94+ fix $ \loop -> do
95+ f <- readIORef isFinalized
96+ if f then return () else do
97+ threadDelay 1
98+ loop
99+
100+benchmarks :: [(String, Int -> IO ())]
101+benchmarks = implGroup "spider" runSpiderHost cases
102+ where
103+ implGroup :: (MonadReflexHost' t m, MonadSample t m) => String -> (forall a. m a -> IO a) -> [(String, TestCase)] -> [(String, Int -> IO ())]
104+ implGroup name runHost = group name . fmap (second (benchFiring runHost))
105+ group name = fmap $ first ((name <> "/") <>)
106+ sub n frames = group ("subscribing " ++ show (n, frames)) $ Focused.subscribing n frames
107+ firing n = group ("firing " <> show n) $ Focused.firing n
108+ merging n = group ("merging " <> show n) $ Focused.merging n
109+ dynamics n = group ("dynamics " <> show n) $ Focused.dynamics n
110+ cases = concat
111+ [ sub 100 40
112+ , dynamics 100
113+ , dynamics 1000
114+ , firing 1000
115+ , firing 10000
116+ , merging 10
117+ , merging 50
118+ , merging 100
119+ , merging 200
120+ ]
121+
122+pattern RunTestCaseFlag = "--run-test-case"
123+
124+spawnBenchmark :: String -> Benchmark
125+spawnBenchmark name = Benchmark name $ Benchmarkable $ \n -> do
126+ self <- getExecutablePath
127+ callProcess self [RunTestCaseFlag, name, show n, "+RTS", "-N1"]
128+
129+foreign import ccall unsafe "myCapabilityHasOtherRunnableThreads" myCapabilityHasOtherRunnableThreads :: IO Bool
130+
131+main :: IO ()
132+main = do
133+ args <- getArgs
134+ case args of
135+ RunTestCaseFlag : t -> case t of
136+ [name, readMaybe -> Just count] -> do
137+ case lookup name benchmarks of
138+ Just testCase -> testCase count
139+ performGC
140+ fix $ \loop -> bool (return ()) (yield >> loop) =<< myCapabilityHasOtherRunnableThreads
141+ return ()
142+ _ -> $failure "--run-test-case: expected test name and iteration count to follow"
143+ _ -> defaultMainWith (defaultConfig { timeLimit = 20, csvFile = Just "dmap-original.csv", reportFile = Just "report.html" }) $ fmap (spawnBenchmark . fst) benchmarks
cabal.projectdeleted−1
@@ -1 +0,0 @@
1−packages: .
default.nixnew file+24
@@ -0,0 +1,24 @@
1+{ mkDerivation, base, containers, dependent-map, dependent-sum
2+, exception-transformers, haskell-src-exts, haskell-src-meta
3+, MemoTrie, mtl, primitive, ref-tf, semigroups, stdenv, syb
4+, template-haskell, these, transformers, transformers-compat
5+, criterion, deepseq, split, stm, loch-th
6+}:
7+mkDerivation {
8+ pname = "reflex";
9+ version = "0.4.0";
10+ src = builtins.filterSource (path: type: baseNameOf path != ".git") ./.;
11+ libraryHaskellDepends = [
12+ base containers dependent-map dependent-sum exception-transformers
13+ haskell-src-exts haskell-src-meta mtl primitive ref-tf semigroups
14+ syb template-haskell these transformers transformers-compat
15+ ];
16+ testHaskellDepends = [
17+ base containers dependent-map MemoTrie mtl ref-tf
18+ # Benchmark dependencies
19+ criterion deepseq split stm loch-th
20+ ];
21+ homepage = "https://github.com/reflex-frp/reflex";
22+ description = "Higher-order Functional Reactive Programming";
23+ license = stdenv.lib.licenses.bsd3;
24+}
dep/reflex-platform/default.nixdeleted−8
@@ -1,8 +0,0 @@
1−# DO NOT HAND-EDIT THIS FILE
2−let fetch = { private ? false, fetchSubmodules ? false, owner, repo, rev, sha256, ... }:
3− if !fetchSubmodules && !private then builtins.fetchTarball {
4− url = "https://github.com/${owner}/${repo}/archive/${rev}.tar.gz"; inherit sha256;
5− } else (import <nixpkgs> {}).fetchFromGitHub {
6− inherit owner repo rev sha256 fetchSubmodules private;
7− };
8−in import (fetch (builtins.fromJSON (builtins.readFile ./github.json)))
dep/reflex-platform/github.jsondeleted−8
@@ -1,8 +0,0 @@
1−{
2− "owner": "reflex-frp",
3− "repo": "reflex-platform",
4− "branch": "master",
5− "private": false,
6− "rev": "c9d11db1b98855fe8ab24a3ff6a5dbe0ad902ad9",
7− "sha256": "0sfzkqdvyah5mwvmli0wq1nl0b8cvk2cmfgfy4rz57wv42x3099y"
8−}
patch.cabaldeleted−100
@@ -1,100 +0,0 @@
1−Name: patch
2−Version: 0.0.5.0
3−Synopsis: Data structures for describing changes to other data structures.
4−Description:
5− Data structures for describing changes to other data structures.
6− .
7− In this library, a patch is something which can be applied, analogous to a
8− function, and which distinguishes returning the argument it was provided from
9− returning something else.
10−License: BSD3
11−License-file: LICENSE
12−Author: Ryan Trinkle
13−Maintainer: maintainer@obsidian.systems
14−Stability: Experimental
15−Category: FRP
16−Build-type: Simple
17−Cabal-version: >=1.10
18−homepage: https://obsidian.systems
19−bug-reports: https://github.com/reflex-frp/patch/issues
20−extra-source-files:
21− README.md
22− ChangeLog.md
23−
24−tested-with:
25− GHC ==8.0.2 || ==8.2.2 || ==8.4.4 || ==8.6.5 || ==8.8.1
26− GHCJS ==8.4
27−
28−flag split-these
29− description: Use split these/semialign packages
30− manual: False
31− default: True
32−
33−library
34− hs-source-dirs: src
35− default-language: Haskell2010
36− build-depends: base >= 4.9 && < 4.15
37− , constraints-extras >= 0.3 && < 0.4
38− , containers >= 0.6 && < 0.7
39− , dependent-map >= 0.3 && < 0.5
40− , dependent-sum >= 0.6 && < 0.8
41− , lens >= 4.7 && < 5
42− , semigroupoids >= 4.0 && < 6
43− , transformers >= 0.5.6.0 && < 0.6
44− , witherable >= 0.3 && < 0.5
45−
46− if impl(ghc < 8.6) -- really, if base < 8.12
47− build-depends: base-orphans >= 0.8 && < 0.9
48−
49− exposed-modules: Data.Functor.Misc
50− , Data.Monoid.DecidablyEmpty
51− , Data.Patch
52− , Data.Patch.Class
53− , Data.Patch.DMap
54− , Data.Patch.DMapWithMove
55− , Data.Patch.IntMap
56− , Data.Patch.Map
57− , Data.Patch.MapWithMove
58− , Data.Patch.MapWithPatchingMove
59− , Data.Semigroup.Additive
60−
61− ghc-options: -Wall -fwarn-redundant-constraints -fwarn-tabs
62− default-extensions: PolyKinds
63−
64− if flag(split-these)
65− build-depends: these >= 1 && <1.2
66− , semialign >=1 && <1.2
67− , monoidal-containers >= 0.6 && < 0.7
68− else
69− build-depends: these >= 0.4 && <0.9
70− , monoidal-containers == 0.4.0.0
71−
72−test-suite tests
73− default-language: Haskell2010
74− type: exitcode-stdio-1.0
75− main-is: tests.hs
76− hs-source-dirs: test
77− build-depends: base
78− , patch
79− , containers
80− , hedgehog
81− , HUnit
82− if impl(ghcjs)
83− buildable: False
84−
85−test-suite hlint
86− default-language: Haskell2010
87− type: exitcode-stdio-1.0
88− main-is: hlint.hs
89− hs-source-dirs: test
90− build-depends: base
91− , directory
92− , filepath
93− , filemanip
94− , hlint (< 2.1 || >= 2.2.2) && < 3.3
95− if impl(ghcjs)
96− buildable: False
97−
98−source-repository head
99− type: git
100− location: https://github.com/reflex-frp/patch
reflex.cabalnew file+118
@@ -0,0 +1,118 @@
1+Name: reflex
2+Version: 0.4.0
3+Synopsis: Higher-order Functional Reactive Programming
4+Description: Reflex is a high-performance, deterministic, higher-order Functional Reactive Programming system
5+License: BSD3
6+License-file: LICENSE
7+Author: Ryan Trinkle
8+Maintainer: ryan.trinkle@gmail.com
9+Stability: Experimental
10+Category: FRP
11+Build-type: Simple
12+Cabal-version: >=1.9.2
13+homepage: https://github.com/reflex-frp/reflex
14+bug-reports: https://github.com/reflex-frp/reflex/issues
15+
16+library
17+ hs-source-dirs: src
18+ build-depends:
19+ base >= 4.7 && < 4.10,
20+ dependent-sum == 0.3.*,
21+ dependent-map >= 0.2.2 && < 0.3,
22+ semigroups >= 0.16 && < 0.19,
23+ mtl >= 2.1 && < 2.3,
24+ containers == 0.5.*,
25+ these >= 0.4 && < 0.7,
26+ primitive >= 0.5 && < 0.7,
27+ template-haskell >= 2.9 && < 2.12,
28+ ref-tf == 0.4.*,
29+ exception-transformers == 0.4.*,
30+ transformers >= 0.2,
31+ transformers-compat >= 0.3,
32+ haskell-src-exts >= 1.16 && < 1.18,
33+ haskell-src-meta == 0.6.*,
34+ syb >= 0.5 && < 0.7,
35+ stm == 2.4.*
36+
37+ exposed-modules:
38+ Reflex,
39+ Reflex.Spider,
40+ Reflex.Spider.Internal,
41+ Reflex.Class,
42+ Reflex.Dynamic,
43+ Reflex.Dynamic.TH,
44+ Reflex.Host.Class,
45+ Data.Functor.Misc,
46+ Data.WeakBag
47+
48+ other-extensions: TemplateHaskell
49+ ghc-options: -Wall -fwarn-tabs -funbox-strict-fields -O2
50+ ghc-prof-options: -auto-all
51+
52+
53+test-suite all
54+ type: exitcode-stdio-1.0
55+ main-is: Reflex/Test/RunAll.hs
56+ ghc-options: -O2 -Wall -rtsopts
57+ hs-source-dirs: test
58+ build-depends:
59+ base,
60+ reflex,
61+ ref-tf,
62+ mtl,
63+ containers,
64+ dependent-map,
65+ dependent-sum,
66+ transformers >= 0.3,
67+ MemoTrie == 0.6.*,
68+ deepseq >= 1.3 && < 1.5,
69+ bifunctors,
70+ split
71+
72+
73+benchmark spider-bench
74+ type: exitcode-stdio-1.0
75+ hs-source-dirs: bench
76+ main-is: Main.hs
77+ ghc-options: -Wall -O2 -rtsopts
78+ build-depends:
79+ base,
80+ dependent-sum,
81+ dependent-map,
82+ transformers >= 0.3,
83+ stm == 2.4.*,
84+ deepseq >= 1.3 && < 1.5,
85+ mtl,
86+ primitive,
87+ criterion == 1.1.*,
88+ reflex
89+
90+
91+benchmark saulzar-bench
92+ type: exitcode-stdio-1.0
93+ hs-source-dirs: bench test
94+ c-sources: bench-cbits/checkCapability.c
95+ main-is: RunAll.hs
96+ ghc-options: -Wall -O2 -rtsopts -threaded
97+ ghc-prof-options: -auto-all
98+ build-depends:
99+ base,
100+ containers == 0.5.*,
101+ ref-tf == 0.4,
102+ dependent-sum,
103+ dependent-map,
104+ transformers >= 0.3,
105+ stm == 2.4.*,
106+ deepseq >= 1.3 && < 1.5,
107+ mtl,
108+ primitive,
109+ criterion == 1.1.*,
110+ split,
111+ reflex,
112+ time,
113+ loch-th,
114+ process
115+
116+source-repository head
117+ type: git
118+ location: https://github.com/reflex-frp/reflex
release.nixdeleted−49
@@ -1,49 +0,0 @@
1−{ reflex-platform-fun ? import ./dep/reflex-platform
2−}:
3−
4−let
5− native-reflex-platform = reflex-platform-fun {};
6− inherit (native-reflex-platform.nixpkgs) lib;
7− systems = ["x86_64-linux" "x86_64-darwin"];
8−
9− perPlatform = lib.genAttrs systems (system: let
10− reflex-platform = reflex-platform-fun { inherit system; };
11− compilers = [
12− "ghc"
13− "ghcjs"
14− ] ++ lib.optionals (reflex-platform.androidSupport) [
15− "ghcAndroidAarch64"
16− "ghcAndroidAarch32"
17− ] ++ lib.optionals (reflex-platform.iosSupport) [
18− "ghcIosAarch64"
19− ];
20− compilerPkgs = lib.genAttrs compilers (ghc: let
21− reflex-platform = reflex-platform-fun {
22− inherit system;
23− haskellOverlays = [
24− # Use this package's source for reflex
25− (self: super: {
26− _dep = super._dep // {
27− patch = builtins.filterSource (path: type: !(builtins.elem (baseNameOf path) [
28− "release.nix"
29− ".git"
30− "dist"
31− "dist-newstyle"
32− "cabal.haskell-ci"
33− "cabal.project"
34− ".travis.yml"
35− ])) ./.;
36− };
37− })
38− ];
39− };
40− in reflex-platform.${ghc}.patch);
41− in compilerPkgs // {
42− cache = reflex-platform.pinBuildInputs "patch-${system}"
43− (builtins.attrValues compilerPkgs);
44− });
45−
46− metaCache = native-reflex-platform.pinBuildInputs "patch-everywhere"
47− (map (a: a.cache) (builtins.attrValues perPlatform));
48−
49−in perPlatform // { inherit metaCache; }
src/Data/Functor/Misc.hs+52−194
@@ -1,79 +1,41 @@
1−{-# LANGUAGE CPP #-}
1+{-# LANGUAGE KindSignatures, GADTs, DeriveDataTypeable, RankNTypes, ScopedTypeVariables, PolyKinds, FlexibleInstances, MultiParamTypeClasses, FlexibleContexts, StandaloneDeriving #-}
2−{-# LANGUAGE DeriveDataTypeable #-}
3−{-# LANGUAGE DeriveFunctor #-}
4−{-# LANGUAGE FlexibleContexts #-}
5−{-# LANGUAGE FlexibleInstances #-}
6−{-# LANGUAGE GADTs #-}
7−{-# LANGUAGE LambdaCase #-}
8−{-# LANGUAGE MultiParamTypeClasses #-}
9−{-# LANGUAGE PolyKinds #-}
10−{-# LANGUAGE RankNTypes #-}
11−{-# LANGUAGE ScopedTypeVariables #-}
12−{-# LANGUAGE StandaloneDeriving #-}
13−-- | This module provides types and functions with no particular theme, but
14−-- which are relevant to the use of 'Functor'-based datastructures like
15−-- 'Data.Dependent.Map.DMap'.
16−module Data.Functor.Misc
2+module Data.Functor.Misc where
17− ( -- * Const2
3+
18− Const2 (..)
19− , unConst2
20− , dmapToMap
21− , dmapToIntMap
22− , dmapToMapWith
23− , mapToDMap
24− , weakenDMapWith
25− -- * WrapArg
26− , WrapArg (..)
27− -- * Convenience functions for DMap
28− , mapWithFunctorToDMap
29− , intMapWithFunctorToDMap
30− , mapKeyValuePairsMonotonic
31− , combineDMapsWithKey
32− , EitherTag (..)
33− , dmapToThese
34− , eitherToDSum
35− , dsumToEither
36− , ComposeMaybe (..)
37− ) where
38−
39−import Control.Applicative ((<$>))
40−import Data.Dependent.Map (DMap)
41−import qualified Data.Dependent.Map as DMap
42−import Data.Dependent.Sum
43−import Data.Functor.Identity
44import Data.GADT.Compare
4import Data.GADT.Compare
45−import Data.GADT.Show
46−import Data.IntMap (IntMap)
47−import qualified Data.IntMap as IntMap
48import Data.Map (Map)
5import Data.Map (Map)
49import qualified Data.Map as Map
6import qualified Data.Map as Map
50−import Data.Some (Some, mkSome)
7+import Data.GADT.Show
51−import Data.These
8+import Data.Dependent.Sum
52−import Data.Type.Equality ((:~:)(Refl))
9+import Data.Dependent.Map (DMap)
10+import qualified Data.Dependent.Map as DMap
53import Data.Typeable hiding (Refl)
11import Data.Typeable hiding (Refl)
12+import Data.These
13+import Control.Monad.Identity
14+
15+data WrapArg :: (k -> *) -> (k -> *) -> * -> * where
16+ WrapArg :: f a -> WrapArg g f (g a)
17+
18+instance GEq f => GEq (WrapArg g f) where
19+ geq (WrapArg a) (WrapArg b) = fmap (\Refl -> Refl) $ geq a b
54
20
55−--------------------------------------------------------------------------------
21+instance GCompare f => GCompare (WrapArg g f) where
56−-- Const2
22+ gcompare (WrapArg a) (WrapArg b) = case gcompare a b of
57−--------------------------------------------------------------------------------
23+ GLT -> GLT
24+ GEQ -> GEQ
25+ GGT -> GGT
58
26
59−-- | 'Const2' stores a value of a given type 'k' and ensures that a particular
60−-- type 'v' is always given for the last type parameter
61−data Const2 :: * -> x -> x -> * where
27+data Const2 :: * -> * -> * -> * where
62 Const2 :: k -> Const2 k v v
28 Const2 :: k -> Const2 k v v
63 deriving (Typeable)
29 deriving (Typeable)
64
30
65−-- | Extract the value from a Const2
66−unConst2 :: Const2 k v v' -> k
67−unConst2 (Const2 k) = k
68−
69−deriving instance Eq k => Eq (Const2 k v v')
31+deriving instance Show k => Show (Const2 k v v)
70−deriving instance Ord k => Ord (Const2 k v v')
71−deriving instance Show k => Show (Const2 k v v')
72−deriving instance Read k => Read (Const2 k v v)
73
32
74instance Show k => GShow (Const2 k v) where
33instance Show k => GShow (Const2 k v) where
75 gshowsPrec n x@(Const2 _) = showsPrec n x
34 gshowsPrec n x@(Const2 _) = showsPrec n x
76
35
36+instance (Show k, Show (f v)) => ShowTag (Const2 k v) f where
37+ showTaggedPrec (Const2 _) = showsPrec
38+
77instance Eq k => GEq (Const2 k v) where
39instance Eq k => GEq (Const2 k v) where
78 geq (Const2 a) (Const2 b) =
40 geq (Const2 a) (Const2 b) =
79 if a == b
41 if a == b
@@ -86,81 +48,13 @@ instance Ord k => GCompare (Const2 k v) where
86 EQ -> GEQ
48 EQ -> GEQ
87 GT -> GGT
49 GT -> GGT
88
50
89−-- | Convert a 'DMap' to a regular 'Map'
51+{-# INLINE sequenceDmap #-}
90−dmapToMap :: DMap (Const2 k v) Identity -> Map k v
52+sequenceDmap :: (Monad m, GCompare f) => DMap f m -> m (DMap f Identity)
91−dmapToMap = Map.fromDistinctAscList . map (\(Const2 k :=> Identity v) -> (k, v)) . DMap.toAscList
53+sequenceDmap = DMap.foldrWithKey (\k mv mx -> mx >>= \x -> mv >>= \v -> return $ DMap.insert k (Identity v) x)
92−
54+ (return DMap.empty)
93−-- | Convert a 'DMap' to an 'IntMap'
94−dmapToIntMap :: DMap (Const2 IntMap.Key v) Identity -> IntMap v
95−dmapToIntMap = IntMap.fromDistinctAscList . map (\(Const2 k :=> Identity v) -> (k, v)) . DMap.toAscList
96−
97−-- | Convert a 'DMap' to a regular 'Map', applying the given function to remove
98−-- the wrapping 'Functor'
99−dmapToMapWith :: (f v -> v') -> DMap (Const2 k v) f -> Map k v'
100−dmapToMapWith f = Map.fromDistinctAscList . map (\(Const2 k :=> v) -> (k, f v)) . DMap.toAscList
101−
102−-- | Convert a regular 'Map' to a 'DMap'
103−mapToDMap :: Map k v -> DMap (Const2 k v) Identity
104−mapToDMap = DMap.fromDistinctAscList . map (\(k, v) -> Const2 k :=> Identity v) . Map.toAscList
105−
106−-- | Convert a regular 'Map', where the values are already wrapped in a functor,
107−-- to a 'DMap'
108−mapWithFunctorToDMap :: Map k (f v) -> DMap (Const2 k v) f
109−mapWithFunctorToDMap = DMap.fromDistinctAscList . map (\(k, v) -> Const2 k :=> v) . Map.toAscList
110−
111−-- | Convert a regular 'IntMap', where the values are already wrapped in a
112−-- functor, to a 'DMap'
113−intMapWithFunctorToDMap :: IntMap (f v) -> DMap (Const2 IntMap.Key v) f
114−intMapWithFunctorToDMap = DMap.fromDistinctAscList . map (\(k, v) -> Const2 k :=> v) . IntMap.toAscList
115−
116−-- | Convert a 'DMap' to a regular 'Map' by forgetting the types associated with
117−-- the keys, using a function to remove the wrapping 'Functor'
118−weakenDMapWith :: (forall a. v a -> v') -> DMap k v -> Map (Some k) v'
119−weakenDMapWith f = Map.fromDistinctAscList . map (\(k :=> v) -> (mkSome k, f v)) . DMap.toAscList
120−
121−--------------------------------------------------------------------------------
122−-- WrapArg
123−--------------------------------------------------------------------------------
124−
125−-- | 'WrapArg' can be used to tag a value in one functor with a type
126−-- representing another functor. This was primarily used with dependent-map <
127−-- 0.2, in which the value type was not wrapped in a separate functor.
128−data WrapArg :: (k -> *) -> (k -> *) -> * -> * where
129− WrapArg :: f a -> WrapArg g f (g a)
130−
131−deriving instance Eq (f a) => Eq (WrapArg g f (g' a))
132−deriving instance Ord (f a) => Ord (WrapArg g f (g' a))
133−deriving instance Show (f a) => Show (WrapArg g f (g' a))
134−deriving instance Read (f a) => Read (WrapArg g f (g a))
135−
136−instance GEq f => GEq (WrapArg g f) where
137− geq (WrapArg a) (WrapArg b) = (\Refl -> Refl) <$> geq a b
138−
139−instance GCompare f => GCompare (WrapArg g f) where
140− gcompare (WrapArg a) (WrapArg b) = case gcompare a b of
141− GLT -> GLT
142− GEQ -> GEQ
143− GGT -> GGT
144−
145−--------------------------------------------------------------------------------
146−-- Convenience functions for DMap
147−--------------------------------------------------------------------------------
148−
149−-- | Map over all key/value pairs in a 'DMap', potentially altering the key as
150−-- well as the value. The provided function MUST preserve the ordering of the
151−-- keys, or the resulting 'DMap' will be malformed.
152−mapKeyValuePairsMonotonic :: (DSum k v -> DSum k' v') -> DMap k v -> DMap k' v'
153−mapKeyValuePairsMonotonic f = DMap.fromDistinctAscList . map f . DMap.toAscList
154
55
155{-# INLINE combineDMapsWithKey #-}
56{-# INLINE combineDMapsWithKey #-}
156−-- | Union two 'DMap's of different types, yielding another type. Each key that
157−-- is present in either input map will be present in the output.
158−combineDMapsWithKey :: forall f g h i.
57+combineDMapsWithKey :: forall f g h i. GCompare f => (forall (a :: *). f a -> These (g a) (h a) -> i a) -> DMap f g -> DMap f h -> DMap f i
159− GCompare f
160− => (forall a. f a -> These (g a) (h a) -> i a)
161− -> DMap f g
162− -> DMap f h
163− -> DMap f i
164combineDMapsWithKey f mg mh = DMap.fromList $ go (DMap.toList mg) (DMap.toList mh)
58combineDMapsWithKey f mg mh = DMap.fromList $ go (DMap.toList mg) (DMap.toList mh)
165 where go :: [DSum f g] -> [DSum f h] -> [DSum f i]
59 where go :: [DSum f g] -> [DSum f h] -> [DSum f i]
166 go [] hs = map (\(hk :=> hv) -> hk :=> f hk (That hv)) hs
60 go [] hs = map (\(hk :=> hv) -> hk :=> f hk (That hv)) hs
@@ -170,62 +64,26 @@ combineDMapsWithKey f mg mh = DMap.fromList $ go (DMap.toList mg) (DMap.toList m
170 GEQ -> (gk :=> f gk (These gv hv)) : go gs' hs'
64 GEQ -> (gk :=> f gk (These gv hv)) : go gs' hs'
171 GGT -> (hk :=> f hk (That hv)) : go gs hs'
65 GGT -> (hk :=> f hk (That hv)) : go gs hs'
172
66
173−-- | Extract the values of a 'DMap' of 'EitherTag's.
67+wrapDMap :: (forall a. a -> f a) -> DMap k Identity -> DMap k f
174−dmapToThese :: DMap (EitherTag a b) Identity -> Maybe (These a b)
68+wrapDMap f = DMap.mapWithKey $ \_ -> f . runIdentity
175−dmapToThese m = case (DMap.lookup LeftTag m, DMap.lookup RightTag m) of
69+
176− (Nothing, Nothing) -> Nothing
70+rewrapDMap :: (forall (a :: *). f a -> g a) -> DMap k f -> DMap k g
177− (Just (Identity a), Nothing) -> Just $ This a
71+rewrapDMap f = DMap.mapWithKey $ \_ -> f
178− (Nothing, Just (Identity b)) -> Just $ That b
179− (Just (Identity a), Just (Identity b)) -> Just $ These a b
180−
181−-- | Tag type for 'Either' to use it as a 'DSum'.
182−data EitherTag l r a where
183− LeftTag :: EitherTag l r l
184− RightTag :: EitherTag l r r
185− deriving (Typeable)
186
72
187−deriving instance Show (EitherTag l r a)
73+unwrapDMap :: (forall a. f a -> a) -> DMap k f -> DMap k Identity
188−deriving instance Eq (EitherTag l r a)
74+unwrapDMap f = DMap.mapWithKey $ \_ -> Identity . f
189−deriving instance Ord (EitherTag l r a)
75+
190−
76+unwrapDMapMaybe :: (forall a. f a -> Maybe a) -> DMap k f -> DMap k Identity
191−instance GEq (EitherTag l r) where
77+unwrapDMapMaybe f m = DMap.fromDistinctAscList [k :=> Identity w | (k :=> v) <- DMap.toAscList m, Just w <- [f v]]
192− geq a b = case (a, b) of
78+
193− (LeftTag, LeftTag) -> Just Refl
79+mapToDMap :: Map k v -> DMap (Const2 k v) Identity
194− (RightTag, RightTag) -> Just Refl
80+mapToDMap = DMap.fromDistinctAscList . map (\(k, v) -> Const2 k :=> Identity v) . Map.toAscList
195− _ -> Nothing
81+
196−
82+mapWithFunctorToDMap :: Map k (f v) -> DMap (Const2 k v) f
197−instance GCompare (EitherTag l r) where
83+mapWithFunctorToDMap = DMap.fromDistinctAscList . map (\(k, v) -> Const2 k :=> v) . Map.toAscList
198− gcompare a b = case (a, b) of
84+
199− (LeftTag, LeftTag) -> GEQ
85+extractFunctorDMap :: DMap (Const2 k (f v)) Identity -> DMap (Const2 k v) f
200− (LeftTag, RightTag) -> GLT
86+extractFunctorDMap = DMap.fromDistinctAscList . map (\(Const2 k :=> Identity v) -> Const2 k :=> v) . DMap.toAscList
201− (RightTag, LeftTag) -> GGT
87+
202− (RightTag, RightTag) -> GEQ
88+dmapToMap :: DMap (Const2 k v) Identity -> Map k v
203−
89+dmapToMap = Map.fromDistinctAscList . map (\(Const2 k :=> Identity v) -> (k, v)) . DMap.toAscList
204−instance GShow (EitherTag l r) where
205− gshowsPrec _ a = case a of
206− LeftTag -> showString "LeftTag"
207− RightTag -> showString "RightTag"
208−
209−-- | Convert 'Either' to a 'DSum'. Inverse of 'dsumToEither'.
210−eitherToDSum :: Either a b -> DSum (EitherTag a b) Identity
211−eitherToDSum = \case
212− Left a -> (LeftTag :=> Identity a)
213− Right b -> (RightTag :=> Identity b)
214−
215−-- | Convert 'DSum' to 'Either'. Inverse of 'eitherToDSum'.
216−dsumToEither :: DSum (EitherTag a b) Identity -> Either a b
217−dsumToEither = \case
218− (LeftTag :=> Identity a) -> Left a
219− (RightTag :=> Identity b) -> Right b
220−
221−--------------------------------------------------------------------------------
222−-- ComposeMaybe
223−--------------------------------------------------------------------------------
224−
225−-- | We can't use @Compose Maybe@ instead of 'ComposeMaybe', because that would
226−-- make the 'f' parameter have a nominal type role. We need f to be
227−-- representational so that we can use safe 'coerce'.
228−newtype ComposeMaybe f a =
229− ComposeMaybe { getComposeMaybe :: Maybe (f a) } deriving (Show, Eq, Ord)
230−
231−deriving instance Functor f => Functor (ComposeMaybe f)
src/Data/Monoid/DecidablyEmpty.hsdeleted−115
@@ -1,115 +0,0 @@
1−{-# LANGUAGE CPP #-}
2−{-# LANGUAGE DefaultSignatures #-}
3−{-# LANGUAGE GeneralizedNewtypeDeriving #-}
4−{-# LANGUAGE StandaloneDeriving #-}
5−{-# LANGUAGE TypeOperators #-}
6−
7−-- TODO upstream somwhere else?
8−module Data.Monoid.DecidablyEmpty where
9−
10−import Data.Functor.Identity
11−import Data.Functor.Const
12−import Data.Monoid
13−import Data.Maybe (isNothing)
14−#if MIN_VERSION_base(4,11,0)
15−import Data.Ord
16−#endif
17−import Data.Proxy
18−import Data.Semigroup hiding (First, Last)
19−#if MIN_VERSION_base(4,12,0)
20−import GHC.Generics
21−#endif
22−
23−import qualified Data.IntSet as IntSet
24−import qualified Data.IntMap as IntMap
25−import qualified Data.Map as Map
26−import qualified Data.Sequence as Seq
27−import qualified Data.Set as Set
28−
29−import Data.GADT.Compare
30−import qualified Data.Dependent.Map as DMap
31−
32−-- | A 'DecidablyEmpty' is one where it can be computed whether or not an
33−-- arbitrary value is 'mempty'.
34−--
35−-- By using this class rather than 'Eq', we avoid unnecessary constraining the
36−-- contents of 'Functor's. This makes it possible to efficiently combine and/or
37−-- nest patch maps with 'Eq'-lacking values (e.g. functions) at the leaves.
38−class Monoid a => DecidablyEmpty a where
39− isEmpty :: a -> Bool
40− default isEmpty :: Eq a => a -> Bool
41− isEmpty = (==) mempty
42−
43−-- base
44−
45−instance DecidablyEmpty Ordering
46−instance DecidablyEmpty ()
47−instance DecidablyEmpty Any
48−instance DecidablyEmpty All
49−-- instance DecidablyEmpty Lifetime
50−-- instance DecidablyEmpty Event
51−instance DecidablyEmpty [a] where
52− isEmpty = null
53−instance
54−#if MIN_VERSION_base(4,11,0)
55− Semigroup a
56−#else
57− Monoid a
58−#endif
59− => DecidablyEmpty (Maybe a) where
60− isEmpty = isNothing
61−deriving instance (Num a, DecidablyEmpty a) => DecidablyEmpty (Product a)
62−deriving instance (DecidablyEmpty a, Num a) => DecidablyEmpty (Sum a)
63−deriving instance DecidablyEmpty a => DecidablyEmpty (Dual a)
64−instance DecidablyEmpty (First a) where
65− isEmpty (First a) = isNothing a
66−instance DecidablyEmpty (Last a) where
67− isEmpty (Last a) = isNothing a
68−deriving instance DecidablyEmpty a => DecidablyEmpty (Identity a)
69−instance Semigroup a => DecidablyEmpty (Option a) where
70− isEmpty (Option a) = isNothing a
71−deriving instance DecidablyEmpty m => DecidablyEmpty (WrappedMonoid m)
72−instance (Ord a, Bounded a) => DecidablyEmpty (Max a)
73−instance (Ord a, Bounded a) => DecidablyEmpty (Min a)
74−instance DecidablyEmpty (Proxy s)
75−deriving instance DecidablyEmpty a => DecidablyEmpty (Const a b)
76−#if MIN_VERSION_base(4,11,0)
77−deriving instance DecidablyEmpty a => DecidablyEmpty (Down a)
78−#endif
79−#if MIN_VERSION_base(4,12,0)
80−deriving instance DecidablyEmpty p => DecidablyEmpty (Par1 p)
81−instance DecidablyEmpty (U1 p)
82−deriving instance DecidablyEmpty (f p) => DecidablyEmpty (Rec1 f p)
83−deriving instance DecidablyEmpty (f p) => DecidablyEmpty (M1 i c f p)
84−deriving instance DecidablyEmpty c => DecidablyEmpty (K1 i c p)
85−instance (DecidablyEmpty (f p), DecidablyEmpty (g p)) => DecidablyEmpty ((f :*: g) p) where
86− isEmpty (x :*: y) = isEmpty x && isEmpty y
87−deriving instance DecidablyEmpty (f (g p)) => DecidablyEmpty ((f :.: g) p)
88−#endif
89−
90−instance (DecidablyEmpty a, DecidablyEmpty b) => DecidablyEmpty (a, b) where
91− isEmpty (a, b) = isEmpty a && isEmpty b
92−instance (DecidablyEmpty a, DecidablyEmpty b, DecidablyEmpty c) => DecidablyEmpty (a, b, c) where
93− isEmpty (a, b, c) = isEmpty a && isEmpty b && isEmpty c
94−instance (DecidablyEmpty a, DecidablyEmpty b, DecidablyEmpty c, DecidablyEmpty d) => DecidablyEmpty (a, b, c, d) where
95− isEmpty (a, b, c, d) = isEmpty a && isEmpty b && isEmpty c && isEmpty d
96−instance (DecidablyEmpty a, DecidablyEmpty b, DecidablyEmpty c, DecidablyEmpty d, DecidablyEmpty e) => DecidablyEmpty (a, b, c, d, e) where
97− isEmpty (a, b, c, d, e) = isEmpty a && isEmpty b && isEmpty c && isEmpty d && isEmpty e
98−
99−-- containers
100−
101−instance DecidablyEmpty IntSet.IntSet where
102− isEmpty = IntSet.null
103−instance DecidablyEmpty (IntMap.IntMap v) where
104− isEmpty = IntMap.null
105−instance Ord k => DecidablyEmpty (Map.Map k v) where
106− isEmpty = Map.null
107−instance DecidablyEmpty (Seq.Seq v) where
108− isEmpty = Seq.null
109−instance Ord k => DecidablyEmpty (Set.Set k) where
110− isEmpty = Set.null
111−
112−-- dependent-map
113−
114−instance GCompare k => DecidablyEmpty (DMap.DMap k v) where
115− isEmpty = DMap.null
src/Data/Patch.hsdeleted−95
@@ -1,95 +0,0 @@
1−{-# LANGUAGE CPP #-}
2−{-# LANGUAGE GeneralizedNewtypeDeriving #-}
3−{-# LANGUAGE StandaloneDeriving #-}
4−{-# LANGUAGE TypeFamilies #-}
5−{-# LANGUAGE TypeOperators #-}
6−-- |
7−-- Module:
8−-- Data.Patch
9−-- Description:
10−-- This module defines the 'Patch' class.
11−module Data.Patch
12− ( module Data.Patch
13− , module X
14− ) where
15−
16−import Control.Applicative
17−import Data.Functor.Const (Const (..))
18−import Data.Functor.Identity
19−import Data.Map.Monoidal (MonoidalMap)
20−import Data.Proxy
21−#if !MIN_VERSION_base(4,11,0)
22−import Data.Semigroup (Semigroup (..))
23−#endif
24−import GHC.Generics
25−
26−import Data.Semigroup.Additive as X
27−import Data.Patch.Class as X
28−import Data.Patch.DMap as X hiding (getDeletions)
29−import Data.Patch.DMapWithMove as X
30− ( PatchDMapWithMove, const2PatchDMapWithMoveWith, mapPatchDMapWithMove
31− , patchDMapWithMoveToPatchMapWithMoveWith
32− , traversePatchDMapWithMoveWithKey, unPatchDMapWithMove
33− , unsafePatchDMapWithMove, weakenPatchDMapWithMoveWith
34− )
35−import Data.Patch.IntMap as X hiding (getDeletions)
36−import Data.Patch.Map as X
37−import Data.Patch.MapWithMove as X
38− ( PatchMapWithMove, patchMapWithMoveNewElements
39− , patchMapWithMoveNewElementsMap, unPatchMapWithMove
40− , unsafePatchMapWithMove
41− )
42−
43−-- | A 'Group' is a 'Monoid' where every element has an inverse.
44−class (Semigroup q, Monoid q) => Group q where
45− negateG :: q -> q
46− (~~) :: q -> q -> q
47− r ~~ s = r <> negateG s
48−
49−-- | The elements of an 'Additive' 'Semigroup' can be considered as patches of their own type.
50−newtype AdditivePatch p = AdditivePatch { unAdditivePatch :: p }
51−
52−instance Additive p => Patch (AdditivePatch p) where
53− type PatchTarget (AdditivePatch p) = p
54− apply (AdditivePatch p) q = Just $ p <> q
55−
56−instance (Ord k, Group q) => Group (MonoidalMap k q) where
57− negateG = fmap negateG
58−
59−-- | Trivial group.
60−instance Group () where
61− negateG _ = ()
62− _ ~~ _ = ()
63−
64−-- | Product group. A Pair of groups gives rise to a group
65−instance (Group a, Group b) => Group (a, b) where
66− negateG (a, b) = (negateG a, negateG b)
67− (a, b) ~~ (c, d) = (a ~~ c, b ~~ d)
68−
69−-- See https://gitlab.haskell.org/ghc/ghc/issues/11135#note_111802 for the reason Compose is not also provided.
70−-- Base does not define Monoid (Compose f g a) so this is the best we can
71−-- really do for functor composition.
72−instance Group (f (g a)) => Group ((f :.: g) a) where
73− negateG (Comp1 xs) = Comp1 (negateG xs)
74− Comp1 xs ~~ Comp1 ys = Comp1 (xs ~~ ys)
75−
76−-- | Product of groups, Functor style.
77−instance (Group (f a), Group (g a)) => Group ((f :*: g) a) where
78− negateG (a :*: b) = negateG a :*: negateG b
79− (a :*: b) ~~ (c :*: d) = (a ~~ c) :*: (b ~~ d)
80−
81−-- | Trivial group, Functor style
82−instance Group (Proxy x) where
83− negateG _ = Proxy
84− _ ~~ _ = Proxy
85−
86−-- | Const lifts groups into a functor.
87−deriving instance Group a => Group (Const a x)
88−
89−-- | Identity lifts groups pointwise (at only one point)
90−deriving instance Group a => Group (Identity a)
91−
92−-- | Functions lift groups pointwise.
93−instance Group b => Group (a -> b) where
94− negateG f = negateG . f
95− (~~) = liftA2 (~~)
src/Data/Patch/Class.hsdeleted−45
@@ -1,45 +0,0 @@
1−{-# LANGUAGE CPP #-}
2−{-# LANGUAGE ScopedTypeVariables #-}
3−{-# LANGUAGE TypeFamilies #-}
4−-- | The interface for types which represent changes made to other types
5−module Data.Patch.Class where
6−
7−import Data.Functor.Identity
8−import Data.Kind (Type)
9−import Data.Maybe
10−#if !MIN_VERSION_base(4,11,0)
11−import Data.Semigroup (Semigroup(..))
12−#endif
13−import Data.Proxy
14−
15−-- | A 'Patch' type represents a kind of change made to a datastructure.
16−--
17−-- If an instance of 'Patch' is also an instance of 'Semigroup', it should obey
18−-- the law that @applyAlways (f <> g) == applyAlways f . applyAlways g@.
19−class Patch p where
20− type PatchTarget p :: Type
21− -- | Apply the patch @p a@ to the value @a@. If no change is needed, return
22− -- 'Nothing'.
23− apply :: p -> PatchTarget p -> Maybe (PatchTarget p)
24−
25−-- | Apply a 'Patch'; if it does nothing, return the original value
26−applyAlways :: Patch p => p -> PatchTarget p -> PatchTarget p
27−applyAlways p t = fromMaybe t $ apply p t
28−
29−-- | 'Identity' can be used as a 'Patch' that always fully replaces the value
30−instance Patch (Identity a) where
31− type PatchTarget (Identity a) = a
32− apply (Identity a) _ = Just a
33−
34−-- | 'Proxy' can be used as a 'Patch' that does nothing.
35−instance forall (a :: Type). Patch (Proxy a) where
36− type PatchTarget (Proxy a) = a
37− apply ~Proxy _ = Nothing
38−
39−-- | Like '(.)', but composes functions that return patches rather than
40−-- functions that return new values. The Semigroup instance for patches must
41−-- apply patches right-to-left, like '(.)'.
42−composePatchFunctions :: (Patch p, Semigroup p) => (PatchTarget p -> p) -> (PatchTarget p -> p) -> PatchTarget p -> p
43−composePatchFunctions g f a =
44− let fp = f a
45− in g (applyAlways fp a) <> fp
src/Data/Patch/DMap.hsdeleted−93
@@ -1,93 +0,0 @@
1−{-# LANGUAGE CPP #-}
2−{-# LANGUAGE GeneralizedNewtypeDeriving #-}
3−{-# LANGUAGE LambdaCase #-}
4−{-# LANGUAGE PolyKinds #-}
5−{-# LANGUAGE Rank2Types #-}
6−{-# LANGUAGE ScopedTypeVariables #-}
7−{-# LANGUAGE StandaloneDeriving #-}
8−{-# LANGUAGE TypeFamilies #-}
9−
10−-- | 'Patch'es on 'DMap' that consist only of insertions (or overwrites) and deletions.
11−module Data.Patch.DMap where
12−
13−import Data.Patch.Class
14−import Data.Patch.IntMap
15−import Data.Patch.Map
16−
17−import Data.Dependent.Map (DMap)
18−import Data.Dependent.Sum (DSum (..))
19−import Data.GADT.Compare (GCompare (..))
20−import qualified Data.Dependent.Map as DMap
21−import Data.Functor.Constant
22−import Data.Functor.Misc
23−import qualified Data.IntMap as IntMap
24−import qualified Data.Map as Map
25−import Data.Monoid.DecidablyEmpty
26−#if !MIN_VERSION_base(4,11,0)
27−import Data.Semigroup (Semigroup (..))
28−#endif
29−import Data.Some (Some)
30−
31−-- | A set of changes to a 'DMap'. Any element may be inserted/updated or deleted.
32−-- Insertions are represented as @'ComposeMaybe' (Just value)@,
33−-- while deletions are represented as @'ComposeMaybe' Nothing@.
34−newtype PatchDMap k v = PatchDMap { unPatchDMap :: DMap k (ComposeMaybe v) }
35−
36−deriving instance GCompare k => Semigroup (PatchDMap k v)
37−
38−deriving instance GCompare k => Monoid (PatchDMap k v)
39−
40−-- It won't let me derive for some reason
41−instance GCompare k => DecidablyEmpty (PatchDMap k v) where
42− isEmpty (PatchDMap m) = DMap.null m
43−
44−-- | Apply the insertions or deletions to a given 'DMap'.
45−instance GCompare k => Patch (PatchDMap k v) where
46− type PatchTarget (PatchDMap k v) = DMap k v
47− apply (PatchDMap diff) old = Just $! insertions `DMap.union` (old `DMap.difference` deletions) --TODO: return Nothing sometimes --Note: the strict application here is critical to ensuring that incremental merges don't hold onto all their prerequisite events forever; can we make this more robust?
48− where insertions = DMap.mapMaybeWithKey (const $ getComposeMaybe) diff
49− deletions = DMap.mapMaybeWithKey (const $ nothingToJust . getComposeMaybe) diff
50− nothingToJust = \case
51− Nothing -> Just $ Constant ()
52− Just _ -> Nothing
53−
54−-- | Map a function @v a -> v' a@ over any inserts/updates in the given
55−-- @'PatchDMap' k v@ to produce a @'PatchDMap' k v'@.
56−mapPatchDMap :: (forall a. v a -> v' a) -> PatchDMap k v -> PatchDMap k v'
57−mapPatchDMap f (PatchDMap p) = PatchDMap $ DMap.map (ComposeMaybe . fmap f . getComposeMaybe) p
58−
59−-- | Map an effectful function @v a -> f (v' a)@ over any inserts/updates in the given
60−-- @'PatchDMap' k v@ to produce a @'PatchDMap' k v'@.
61−traversePatchDMap :: Applicative f => (forall a. v a -> f (v' a)) -> PatchDMap k v -> f (PatchDMap k v')
62−traversePatchDMap f = traversePatchDMapWithKey $ const f
63−
64−-- | Map an effectful function @k a -> v a -> f (v' a)@ over any inserts/updates
65−-- in the given @'PatchDMap' k v@ to produce a @'PatchDMap' k v'@.
66−traversePatchDMapWithKey :: Applicative m => (forall a. k a -> v a -> m (v' a)) -> PatchDMap k v -> m (PatchDMap k v')
67−traversePatchDMapWithKey f (PatchDMap p) = PatchDMap <$> DMap.traverseWithKey (\k (ComposeMaybe v) -> ComposeMaybe <$> traverse (f k) v) p
68−
69−-- | Weaken a @'PatchDMap' k v@ to a @'PatchMap' (Some k) v'@ using a function
70−-- @v a -> v'@ to weaken each value contained in the patch.
71−weakenPatchDMapWith :: (forall a. v a -> v') -> PatchDMap k v -> PatchMap (Some k) v'
72−weakenPatchDMapWith f (PatchDMap p) = PatchMap $ weakenDMapWith (fmap f . getComposeMaybe) p
73−
74−-- | Convert a weak @'PatchDMap' ('Const2' k a) v@ where the @a@ is known by way of
75−-- the @Const2@ into a @'PatchMap' k v'@ using a rank 1 function @v a -> v'@.
76−patchDMapToPatchMapWith :: (v a -> v') -> PatchDMap (Const2 k a) v -> PatchMap k v'
77−patchDMapToPatchMapWith f (PatchDMap p) = PatchMap $ dmapToMapWith (fmap f . getComposeMaybe) p
78−
79−-- | Convert a @'PatchMap' k v@ into a @'PatchDMap' ('Const2' k a) v'@ using a function @v -> v' a@.
80−const2PatchDMapWith :: forall k v v' a. (v -> v' a) -> PatchMap k v -> PatchDMap (Const2 k a) v'
81−const2PatchDMapWith f (PatchMap p) = PatchDMap $ DMap.fromDistinctAscList $ g <$> Map.toAscList p
82− where g :: (k, Maybe v) -> DSum (Const2 k a) (ComposeMaybe v')
83− g (k, e) = Const2 k :=> ComposeMaybe (f <$> e)
84−
85−-- | Convert a @'PatchIntMap' v@ into a @'PatchDMap' ('Const2' Int a) v'@ using a function @v -> v' a@.
86−const2IntPatchDMapWith :: forall v f a. (v -> f a) -> PatchIntMap v -> PatchDMap (Const2 IntMap.Key a) f
87−const2IntPatchDMapWith f (PatchIntMap p) = PatchDMap $ DMap.fromDistinctAscList $ g <$> IntMap.toAscList p
88− where g :: (IntMap.Key, Maybe v) -> DSum (Const2 IntMap.Key a) (ComposeMaybe f)
89− g (k, e) = Const2 k :=> ComposeMaybe (f <$> e)
90−
91−-- | Get the values that will be replaced or deleted if the given patch is applied to the given 'DMap'.
92−getDeletions :: GCompare k => PatchDMap k v -> DMap k v' -> DMap k v'
93−getDeletions (PatchDMap p) m = DMap.intersectionWithKey (\_ v _ -> v) m p
src/Data/Patch/DMapWithMove.hsdeleted−371
@@ -1,371 +0,0 @@
1−{-# LANGUAGE CPP #-}
2−{-# LANGUAGE FlexibleContexts #-}
3−{-# LANGUAGE FlexibleInstances #-}
4−{-# LANGUAGE GADTs #-}
5−{-# LANGUAGE LambdaCase #-}
6−{-# LANGUAGE MultiParamTypeClasses #-}
7−{-# LANGUAGE PatternGuards #-}
8−{-# LANGUAGE PolyKinds #-}
9−{-# LANGUAGE Rank2Types #-}
10−{-# LANGUAGE ScopedTypeVariables #-}
11−{-# LANGUAGE TypeFamilies #-}
12−{-# LANGUAGE UndecidableInstances #-}
13−
14−-- |Module containing @'PatchDMapWithMove' k v@ and associated functions, which represents a 'Patch' to a @'DMap' k v@ which can insert, update, delete, and
15−-- move values between keys.
16−module Data.Patch.DMapWithMove where
17−
18−import Data.Patch.Class
19−import Data.Patch.MapWithMove (PatchMapWithMove (..))
20−import qualified Data.Patch.MapWithMove as MapWithMove
21−
22−import Data.Constraint.Extras
23−import Data.Dependent.Map (DMap)
24−import Data.Dependent.Sum (DSum (..))
25−import qualified Data.Dependent.Map as DMap
26−import Data.Functor.Constant
27−import Data.Functor.Misc
28−import Data.Functor.Product
29−import Data.GADT.Compare (GEq (..), GCompare (..))
30−import Data.GADT.Show (GShow, gshow)
31−import qualified Data.Map as Map
32−import Data.Maybe
33−import Data.Monoid.DecidablyEmpty
34−#if !MIN_VERSION_base(4,11,0)
35−import Data.Semigroup (Semigroup (..))
36−#endif
37−import Data.Some (Some, mkSome)
38−import Data.These
39−
40−-- | Like 'PatchMapWithMove', but for 'DMap'. Each key carries a 'NodeInfo' which describes how it will be changed by the patch and connects move sources and
41−-- destinations.
42−--
43−-- Invariants:
44−--
45−-- * A key should not move to itself.
46−-- * A move should always be represented with both the destination key (as a 'From_Move') and the source key (as a @'ComposeMaybe' ('Just' destination)@)
47−newtype PatchDMapWithMove k v = PatchDMapWithMove (DMap k (NodeInfo k v))
48−
49−-- It won't let me derive for some reason
50−instance GCompare k => DecidablyEmpty (PatchDMapWithMove k v) where
51− isEmpty (PatchDMapWithMove m) = DMap.null m
52−
53−-- |Structure which represents what changes apply to a particular key. @_nodeInfo_from@ specifies what happens to this key, and in particular what other key
54−-- the current key is moving from, while @_nodeInfo_to@ specifies what key the current key is moving to if involved in a move.
55−data NodeInfo k v a = NodeInfo
56− { _nodeInfo_from :: !(From k v a)
57− -- ^Change applying to the current key, be it an insert, move, or delete.
58− , _nodeInfo_to :: !(To k a)
59− -- ^Where this key is moving to, if involved in a move. Should only be @ComposeMaybe (Just k)@ when there is a corresponding 'From_Move'.
60− }
61− deriving (Show)
62−
63−-- |Structure describing a particular change to a key, be it inserting a new key (@From_Insert@), updating an existing key (@From_Insert@ again), deleting a
64−-- key (@From_Delete@), or moving a key (@From_Move@).
65−data From (k :: a -> *) (v :: a -> *) :: a -> * where
66− -- |Insert a new or update an existing key with the given value @v a@
67− From_Insert :: v a -> From k v a
68− -- |Delete the existing key
69− From_Delete :: From k v a
70− -- |Move the value from the given key @k a@ to this key. The source key should also have an entry in the patch giving the current key as @_nodeInfo_to@,
71− -- usually but not necessarily with @From_Delete@.
72− From_Move :: !(k a) -> From k v a
73− deriving (Show, Read, Eq, Ord)
74−
75−-- |Type alias for the "to" part of a 'NodeInfo'. @'ComposeMaybe' ('Just' k)@ means the key is moving to another key, @ComposeMaybe Nothing@ for any other
76−-- operation.
77−type To = ComposeMaybe
78−
79−-- |Test whether a 'PatchDMapWithMove' satisfies its invariants.
80−validPatchDMapWithMove :: forall k v. (GCompare k, GShow k) => DMap k (NodeInfo k v) -> Bool
81−validPatchDMapWithMove = not . null . validationErrorsForPatchDMapWithMove
82−
83−-- |Enumerate what reasons a 'PatchDMapWithMove' doesn't satisfy its invariants, returning @[]@ if it's valid.
84−validationErrorsForPatchDMapWithMove :: forall k v. (GCompare k, GShow k) => DMap k (NodeInfo k v) -> [String]
85−validationErrorsForPatchDMapWithMove m =
86− noSelfMoves <> movesBalanced
87− where
88− noSelfMoves = mapMaybe selfMove . DMap.toAscList $ m
89− selfMove (dst :=> NodeInfo (From_Move src) _) | Just _ <- dst `geq` src = Just $ "self move of key " <> gshow src <> " at destination side"
90− selfMove (src :=> NodeInfo _ (ComposeMaybe (Just dst))) | Just _ <- src `geq` dst = Just $ "self move of key " <> gshow dst <> " at source side"
91− selfMove _ = Nothing
92− movesBalanced = mapMaybe unbalancedMove . DMap.toAscList $ m
93− unbalancedMove (dst :=> NodeInfo (From_Move src) _) =
94− case DMap.lookup src m of
95− Nothing -> Just $ "unbalanced move at destination key " <> gshow dst <> " supposedly from " <> gshow src <> " but source key is not in the patch"
96− Just (NodeInfo _ (ComposeMaybe (Just dst'))) ->
97− if isNothing (dst' `geq` dst)
98− then Just $ "unbalanced move at destination key " <> gshow dst <> " from " <> gshow src <> " is going to " <> gshow dst' <> " instead"
99− else Nothing
100− _ ->
101− Just $ "unbalanced move at destination key " <> gshow dst <> " supposedly from " <> gshow src <> " but source key has no move to key"
102− unbalancedMove (src :=> NodeInfo _ (ComposeMaybe (Just dst))) =
103− case DMap.lookup dst m of
104− Nothing -> Just $ " unbalanced move at source key " <> gshow src <> " supposedly going to " <> gshow dst <> " but destination key is not in the patch"
105− Just (NodeInfo (From_Move src') _) ->
106− if isNothing (src' `geq` src)
107− then Just $ "unbalanced move at source key " <> gshow src <> " to " <> gshow dst <> " is coming from " <> gshow src' <> " instead"
108− else Nothing
109−
110− _ ->
111− Just $ "unbalanced move at source key " <> gshow src <> " supposedly going to " <> gshow dst <> " but destination key is not moving"
112− unbalancedMove _ = Nothing
113−
114−-- |Test whether two @'PatchDMapWithMove' k v@ contain the same patch operations.
115−instance (GEq k, Has' Eq k (NodeInfo k v)) => Eq (PatchDMapWithMove k v) where
116− PatchDMapWithMove a == PatchDMapWithMove b = a == b
117−
118−-- |Higher kinded 2-tuple, identical to @Data.Functor.Product@ from base ≥ 4.9
119−data Pair1 f g a = Pair1 (f a) (g a)
120−
121−-- |Helper data structure used for composing patches using the monoid instance.
122−data Fixup k v a
123− = Fixup_Delete
124− | Fixup_Update (These (From k v a) (To k a))
125−
126−-- |Compose patches having the same effect as applying the patches in turn: @'applyAlways' (p <> q) == 'applyAlways' p . 'applyAlways' q@
127−instance GCompare k => Semigroup (PatchDMapWithMove k v) where
128− PatchDMapWithMove ma <> PatchDMapWithMove mb = PatchDMapWithMove m
129− where
130− connections = DMap.toList $ DMap.intersectionWithKey (\_ a b -> Pair1 (_nodeInfo_to a) (_nodeInfo_from b)) ma mb
131− h :: DSum k (Pair1 (ComposeMaybe k) (From k v)) -> [DSum k (Fixup k v)]
132− h (_ :=> Pair1 (ComposeMaybe mToAfter) editBefore) = case (mToAfter, editBefore) of
133− (Just toAfter, From_Move fromBefore)
134− | isJust $ fromBefore `geq` toAfter
135− -> [toAfter :=> Fixup_Delete]
136− | otherwise
137− -> [ toAfter :=> Fixup_Update (This editBefore)
138− , fromBefore :=> Fixup_Update (That (ComposeMaybe mToAfter))
139− ]
140− (Nothing, From_Move fromBefore) -> [fromBefore :=> Fixup_Update (That (ComposeMaybe mToAfter))] -- The item is destroyed in the second patch, so indicate that it is destroyed in the source map
141− (Just toAfter, _) -> [toAfter :=> Fixup_Update (This editBefore)]
142− (Nothing, _) -> []
143− mergeFixups _ Fixup_Delete Fixup_Delete = Fixup_Delete
144− mergeFixups _ (Fixup_Update a) (Fixup_Update b)
145− | This x <- a, That y <- b
146− = Fixup_Update $ These x y
147− | That y <- a, This x <- b
148− = Fixup_Update $ These x y
149− mergeFixups _ _ _ = error "PatchDMapWithMove: incompatible fixups"
150− fixups = DMap.fromListWithKey mergeFixups $ concatMap h connections
151− combineNodeInfos _ nia nib = NodeInfo
152− { _nodeInfo_from = _nodeInfo_from nia
153− , _nodeInfo_to = _nodeInfo_to nib
154− }
155− applyFixup _ ni = \case
156− Fixup_Delete -> Nothing
157− Fixup_Update u -> Just $ NodeInfo
158− { _nodeInfo_from = fromMaybe (_nodeInfo_from ni) $ getHere u
159− , _nodeInfo_to = fromMaybe (_nodeInfo_to ni) $ getThere u
160− }
161− m = DMap.differenceWithKey applyFixup (DMap.unionWithKey combineNodeInfos ma mb) fixups
162− getHere :: These a b -> Maybe a
163− getHere = \case
164− This a -> Just a
165− These a _ -> Just a
166− That _ -> Nothing
167− getThere :: These a b -> Maybe b
168− getThere = \case
169− This _ -> Nothing
170− These _ b -> Just b
171− That b -> Just b
172−
173−-- |Compose patches having the same effect as applying the patches in turn: @'applyAlways' (p <> q) == 'applyAlways' p . 'applyAlways' q@
174−instance GCompare k => Monoid (PatchDMapWithMove k v) where
175− mempty = PatchDMapWithMove mempty
176− mappend = (<>)
177−
178−{-
179−mappendPatchDMapWithMoveSlow :: forall k v. (ShowTag k v, GCompare k) => PatchDMapWithMove k v -> PatchDMapWithMove k v -> PatchDMapWithMove k v
180−PatchDMapWithMove dstAfter srcAfter `mappendPatchDMapWithMoveSlow` PatchDMapWithMove dstBefore srcBefore = PatchDMapWithMove dst src
181− where
182− getDstAction k m = fromMaybe (From_Move k) $ DMap.lookup k m -- Any key that isn't present is treated as that key moving to itself
183− removeRedundantDst toKey (From_Move fromKey) | isJust (toKey `geq` fromKey) = Nothing
184− removeRedundantDst _ a = Just a
185− f :: forall a. k a -> From k v a -> Maybe (From k v a)
186− f toKey _ = removeRedundantDst toKey $ case getDstAction toKey dstAfter of
187− From_Move fromKey -> getDstAction fromKey dstBefore
188− nonMove -> nonMove
189− dst = DMap.mapMaybeWithKey f $ DMap.union dstAfter dstBefore
190− getSrcAction k m = fromMaybe (ComposeMaybe $ Just k) $ DMap.lookup k m
191− removeRedundantSrc fromKey (ComposeMaybe (Just toKey)) | isJust (fromKey `geq` toKey) = Nothing
192− removeRedundantSrc _ a = Just a
193− g :: forall a. k a -> ComposeMaybe k a -> Maybe (ComposeMaybe k a)
194− g fromKey _ = removeRedundantSrc fromKey $ case getSrcAction fromKey srcBefore of
195− ComposeMaybe Nothing -> ComposeMaybe Nothing
196− ComposeMaybe (Just toKeyBefore) -> getSrcAction toKeyBefore srcAfter
197− src = DMap.mapMaybeWithKey g $ DMap.union srcAfter srcBefore
198−-}
199−
200−-- |Make a @'PatchDMapWithMove' k v@ which has the effect of inserting or updating a value @v a@ to the given key @k a@, like 'DMap.insert'.
201−insertDMapKey :: k a -> v a -> PatchDMapWithMove k v
202−insertDMapKey k v =
203− PatchDMapWithMove . DMap.singleton k $ NodeInfo (From_Insert v) (ComposeMaybe Nothing)
204−
205−-- |Make a @'PatchDMapWithMove' k v@ which has the effect of moving the value from the first key @k a@ to the second key @k a@, equivalent to:
206−--
207−-- @
208−-- 'DMap.delete' src (maybe dmap ('DMap.insert' dst) (DMap.lookup src dmap))
209−-- @
210−moveDMapKey :: GCompare k => k a -> k a -> PatchDMapWithMove k v
211−moveDMapKey src dst = case src `geq` dst of
212− Nothing -> PatchDMapWithMove $ DMap.fromList
213− [ dst :=> NodeInfo (From_Move src) (ComposeMaybe Nothing)
214− , src :=> NodeInfo From_Delete (ComposeMaybe $ Just dst)
215− ]
216− Just _ -> mempty
217−
218−-- |Make a @'PatchDMapWithMove' k v@ which has the effect of swapping two keys in the mapping, equivalent to:
219−--
220−-- @
221−-- let aMay = DMap.lookup a dmap
222−-- bMay = DMap.lookup b dmap
223−-- in maybe id (DMap.insert a) (bMay <> aMay)
224−-- . maybe id (DMap.insert b) (aMay <> bMay)
225−-- . DMap.delete a . DMap.delete b $ dmap
226−-- @
227−swapDMapKey :: GCompare k => k a -> k a -> PatchDMapWithMove k v
228−swapDMapKey src dst = case src `geq` dst of
229− Nothing -> PatchDMapWithMove $ DMap.fromList
230− [ dst :=> NodeInfo (From_Move src) (ComposeMaybe $ Just src)
231− , src :=> NodeInfo (From_Move dst) (ComposeMaybe $ Just dst)
232− ]
233− Just _ -> mempty
234−
235−-- |Make a @'PatchDMapWithMove' k v@ which has the effect of deleting a key in the mapping, equivalent to 'DMap.delete'.
236−deleteDMapKey :: k a -> PatchDMapWithMove k v
237−deleteDMapKey k = PatchDMapWithMove $ DMap.singleton k $ NodeInfo From_Delete $ ComposeMaybe Nothing
238−
239−{-
240−k1, k2 :: Const2 Int () ()
241−k1 = Const2 1
242−k2 = Const2 2
243−p1, p2 :: PatchDMapWithMove (Const2 Int ()) Identity
244−p1 = moveDMapKey k1 k2
245−p2 = moveDMapKey k2 k1
246−p12 = p1 <> p2
247−p21 = p2 <> p1
248−p12Slow = p1 `mappendPatchDMapWithMoveSlow` p2
249−p21Slow = p2 `mappendPatchDMapWithMoveSlow` p1
250−
251−testPatchDMapWithMove = do
252− print p1
253− print p2
254− print $ p12 == deleteDMapKey k1
255− print $ p21 == deleteDMapKey k2
256− print $ p12Slow == deleteDMapKey k1
257− print $ p21Slow == deleteDMapKey k2
258−
259−dst (PatchDMapWithMove x _) = x
260−src (PatchDMapWithMove _ x) = x
261−-}
262−
263−-- |Extract the 'DMap' representing the patch changes from the 'PatchDMapWithMove'.
264−unPatchDMapWithMove :: PatchDMapWithMove k v -> DMap k (NodeInfo k v)
265−unPatchDMapWithMove (PatchDMapWithMove p) = p
266−
267−-- |Wrap a 'DMap' representing patch changes into a 'PatchDMapWithMove', without checking any invariants.
268−--
269−-- __Warning:__ when using this function, you must ensure that the invariants of 'PatchDMapWithMove' are preserved; they will not be checked.
270−unsafePatchDMapWithMove :: DMap k (NodeInfo k v) -> PatchDMapWithMove k v
271−unsafePatchDMapWithMove = PatchDMapWithMove
272−
273−-- |Wrap a 'DMap' representing patch changes into a 'PatchDMapWithMove' while checking invariants. If the invariants are satisfied, @Right p@ is returned
274−-- otherwise @Left errors@.
275−patchDMapWithMove :: (GCompare k, GShow k) => DMap k (NodeInfo k v) -> Either [String] (PatchDMapWithMove k v)
276−patchDMapWithMove dm =
277− case validationErrorsForPatchDMapWithMove dm of
278− [] -> Right $ unsafePatchDMapWithMove dm
279− errs -> Left errs
280−
281−-- |Map a natural transform @v -> v'@ over the given patch, transforming @'PatchDMapWithMove' k v@ into @'PatchDMapWithMove' k v'@.
282−mapPatchDMapWithMove :: forall k v v'. (forall a. v a -> v' a) -> PatchDMapWithMove k v -> PatchDMapWithMove k v'
283−mapPatchDMapWithMove f (PatchDMapWithMove p) = PatchDMapWithMove $
284− DMap.map (\ni -> ni { _nodeInfo_from = g $ _nodeInfo_from ni }) p
285− where g :: forall a. From k v a -> From k v' a
286− g = \case
287− From_Insert v -> From_Insert $ f v
288− From_Delete -> From_Delete
289− From_Move k -> From_Move k
290−
291−-- |Traverse an effectful function @forall a. v a -> m (v ' a)@ over the given patch, transforming @'PatchDMapWithMove' k v@ into @m ('PatchDMapWithMove' k v')@.
292−traversePatchDMapWithMove :: forall m k v v'. Applicative m => (forall a. v a -> m (v' a)) -> PatchDMapWithMove k v -> m (PatchDMapWithMove k v')
293−traversePatchDMapWithMove f = traversePatchDMapWithMoveWithKey $ const f
294−
295−-- |Map an effectful function @forall a. k a -> v a -> m (v ' a)@ over the given patch, transforming @'PatchDMapWithMove' k v@ into @m ('PatchDMapWithMove' k v')@.
296−traversePatchDMapWithMoveWithKey :: forall m k v v'. Applicative m => (forall a. k a -> v a -> m (v' a)) -> PatchDMapWithMove k v -> m (PatchDMapWithMove k v')
297−traversePatchDMapWithMoveWithKey f (PatchDMapWithMove p) = PatchDMapWithMove <$> DMap.traverseWithKey (nodeInfoMapFromM . g) p
298− where g :: forall a. k a -> From k v a -> m (From k v' a)
299− g k = \case
300− From_Insert v -> From_Insert <$> f k v
301− From_Delete -> pure From_Delete
302− From_Move fromKey -> pure $ From_Move fromKey
303−
304−-- |Map a function which transforms @'From' k v a@ into a @'From' k v' a@ over a @'NodeInfo' k v a@.
305−nodeInfoMapFrom :: (From k v a -> From k v' a) -> NodeInfo k v a -> NodeInfo k v' a
306−nodeInfoMapFrom f ni = ni { _nodeInfo_from = f $ _nodeInfo_from ni }
307−
308−-- |Map an effectful function which transforms @'From' k v a@ into a @f ('From' k v' a)@ over a @'NodeInfo' k v a@.
309−nodeInfoMapFromM :: Functor f => (From k v a -> f (From k v' a)) -> NodeInfo k v a -> f (NodeInfo k v' a)
310−nodeInfoMapFromM f ni = fmap (\result -> ni { _nodeInfo_from = result }) $ f $ _nodeInfo_from ni
311−
312−-- |Weaken a 'PatchDMapWithMove' to a 'PatchMapWithMove' by weakening the keys from @k a@ to @'Some' k@ and applying a given weakening function @v a -> v'@ to
313−-- values.
314−weakenPatchDMapWithMoveWith :: forall k v v'. (forall a. v a -> v') -> PatchDMapWithMove k v -> PatchMapWithMove (Some k) v'
315−weakenPatchDMapWithMoveWith f (PatchDMapWithMove p) = PatchMapWithMove $ weakenDMapWith g p
316− where g :: forall a. NodeInfo k v a -> MapWithMove.NodeInfo (Some k) v'
317− g ni = MapWithMove.NodeInfo
318− { MapWithMove._nodeInfo_from = case _nodeInfo_from ni of
319− From_Insert v -> MapWithMove.From_Insert $ f v
320− From_Delete -> MapWithMove.From_Delete
321− From_Move k -> MapWithMove.From_Move $ mkSome k
322− , MapWithMove._nodeInfo_to = mkSome <$> getComposeMaybe (_nodeInfo_to ni)
323− }
324−
325−-- |"Weaken" a @'PatchDMapWithMove' (Const2 k a) v@ to a @'PatchMapWithMove' k v'@. Weaken is in scare quotes because the 'Const2' has already disabled any
326−-- dependency in the typing and all points are already @a@, hence the function to map each value to @v'@ is not higher rank.
327−patchDMapWithMoveToPatchMapWithMoveWith :: forall k v v' a. (v a -> v') -> PatchDMapWithMove (Const2 k a) v -> PatchMapWithMove k v'
328−patchDMapWithMoveToPatchMapWithMoveWith f (PatchDMapWithMove p) = PatchMapWithMove $ dmapToMapWith g p
329− where g :: NodeInfo (Const2 k a) v a -> MapWithMove.NodeInfo k v'
330− g ni = MapWithMove.NodeInfo
331− { MapWithMove._nodeInfo_from = case _nodeInfo_from ni of
332− From_Insert v -> MapWithMove.From_Insert $ f v
333− From_Delete -> MapWithMove.From_Delete
334− From_Move (Const2 k) -> MapWithMove.From_Move k
335− , MapWithMove._nodeInfo_to = unConst2 <$> getComposeMaybe (_nodeInfo_to ni)
336− }
337−
338−-- |"Strengthen" a @'PatchMapWithMove' k v@ into a @'PatchDMapWithMove ('Const2' k a)@; that is, turn a non-dependently-typed patch into a dependently typed
339−-- one but which always has a constant key type represented by 'Const2'. Apply the given function to each @v@ to produce a @v' a@.
340−-- Completemented by 'patchDMapWithMoveToPatchMapWithMoveWith'
341−const2PatchDMapWithMoveWith :: forall k v v' a. (v -> v' a) -> PatchMapWithMove k v -> PatchDMapWithMove (Const2 k a) v'
342−const2PatchDMapWithMoveWith f (PatchMapWithMove p) = PatchDMapWithMove $ DMap.fromDistinctAscList $ g <$> Map.toAscList p
343− where g :: (k, MapWithMove.NodeInfo k v) -> DSum (Const2 k a) (NodeInfo (Const2 k a) v')
344− g (k, ni) = Const2 k :=> NodeInfo
345− { _nodeInfo_from = case MapWithMove._nodeInfo_from ni of
346− MapWithMove.From_Insert v -> From_Insert $ f v
347− MapWithMove.From_Delete -> From_Delete
348− MapWithMove.From_Move fromKey -> From_Move $ Const2 fromKey
349− , _nodeInfo_to = ComposeMaybe $ Const2 <$> MapWithMove._nodeInfo_to ni
350− }
351−
352−-- | Apply the insertions, deletions, and moves to a given 'DMap'.
353−instance GCompare k => Patch (PatchDMapWithMove k v) where
354− type PatchTarget (PatchDMapWithMove k v) = DMap k v
355− apply (PatchDMapWithMove p) old = Just $! insertions `DMap.union` (old `DMap.difference` deletions) --TODO: return Nothing sometimes --Note: the strict application here is critical to ensuring that incremental merges don't hold onto all their prerequisite events forever; can we make this more robust?
356− where insertions = DMap.mapMaybeWithKey insertFunc p
357− insertFunc :: forall a. k a -> NodeInfo k v a -> Maybe (v a)
358− insertFunc _ ni = case _nodeInfo_from ni of
359− From_Insert v -> Just v
360− From_Move k -> DMap.lookup k old
361− From_Delete -> Nothing
362− deletions = DMap.mapMaybeWithKey deleteFunc p
363− deleteFunc :: forall a. k a -> NodeInfo k v a -> Maybe (Constant () a)
364− deleteFunc _ ni = case _nodeInfo_from ni of
365− From_Delete -> Just $ Constant ()
366− _ -> Nothing
367−
368−-- | Get the values that will be replaced, deleted, or moved if the given patch is applied to the given 'DMap'.
369−getDeletionsAndMoves :: GCompare k => PatchDMapWithMove k v -> DMap k v' -> DMap k (Product v' (ComposeMaybe k))
370−getDeletionsAndMoves (PatchDMapWithMove p) m = DMap.intersectionWithKey f m p
371− where f _ v ni = Pair v $ _nodeInfo_to ni
src/Data/Patch/IntMap.hsdeleted−76
@@ -1,76 +0,0 @@
1−{-# LANGUAGE CPP #-}
2−{-# LANGUAGE DeriveTraversable #-}
3−{-# LANGUAGE FlexibleInstances #-}
4−{-# LANGUAGE GeneralizedNewtypeDeriving #-}
5−{-# LANGUAGE MultiParamTypeClasses #-}
6−{-# LANGUAGE StandaloneDeriving #-}
7−{-# LANGUAGE TemplateHaskell #-}
8−{-# LANGUAGE TypeFamilies #-}
9−
10−-- | Module containing 'PatchIntMap', a 'Patch' for 'IntMap' which allows for
11−-- insert/update or delete of associations.
12−module Data.Patch.IntMap where
13−
14−import Control.Lens
15−import Data.IntMap.Strict (IntMap)
16−import qualified Data.IntMap.Strict as IntMap
17−import Data.Maybe
18−import Data.Monoid.DecidablyEmpty
19−#if !MIN_VERSION_base(4,11,0)
20−import Data.Semigroup (Semigroup (..))
21−#endif
22−import Data.Patch.Class
23−
24−-- | 'Patch' for 'IntMap' which represents insertion or deletion of keys in the mapping.
25−-- Internally represented by 'IntMap (Maybe a)', where @Just@ means insert/update
26−-- and @Nothing@ means delete.
27−newtype PatchIntMap a = PatchIntMap { unPatchIntMap :: IntMap (Maybe a) }
28− deriving ( Show, Read, Eq, Ord
29− , Functor, Foldable, Traversable
30− , Monoid, DecidablyEmpty
31− )
32−
33−-- | @a <> b@ will apply the changes of @b@ and then apply the changes of @a@.
34−-- If the same key is modified by both patches, the one on the left will take
35−-- precedence.
36−deriving instance Semigroup (PatchIntMap v)
37−
38−makeWrapped ''PatchIntMap
39−
40−-- | Apply the insertions or deletions to a given 'IntMap'.
41−instance Patch (PatchIntMap a) where
42− type PatchTarget (PatchIntMap a) = IntMap a
43− apply (PatchIntMap p) v = if IntMap.null p then Nothing else Just $
44− let removes = IntMap.filter isNothing p
45− adds = IntMap.mapMaybe id p
46− in IntMap.union adds $ v `IntMap.difference` removes
47−
48−instance FunctorWithIndex Int PatchIntMap
49−instance FoldableWithIndex Int PatchIntMap
50−instance TraversableWithIndex Int PatchIntMap where
51− itraverse = itraversed . Indexed
52− itraversed = _Wrapped .> itraversed <. traversed
53−
54−-- | Map a function @Int -> a -> b@ over all @a@s in the given @'PatchIntMap' a@
55−-- (that is, all inserts/updates), producing a @PatchIntMap b@.
56−mapIntMapPatchWithKey :: (Int -> a -> b) -> PatchIntMap a -> PatchIntMap b
57−mapIntMapPatchWithKey f (PatchIntMap m) = PatchIntMap $ IntMap.mapWithKey (\ k mv -> f k <$> mv) m
58−
59−-- | Map an effectful function @Int -> a -> f b@ over all @a@s in the given @'PatchIntMap' a@
60−-- (that is, all inserts/updates), producing a @f (PatchIntMap b)@.
61−traverseIntMapPatchWithKey :: Applicative f => (Int -> a -> f b) -> PatchIntMap a -> f (PatchIntMap b)
62−traverseIntMapPatchWithKey f (PatchIntMap m) = PatchIntMap <$> IntMap.traverseWithKey (traverse . f) m
63−
64−-- | Extract all @a@s inserted/updated by the given @'PatchIntMap' a@.
65−patchIntMapNewElements :: PatchIntMap a -> [a]
66−patchIntMapNewElements (PatchIntMap m) = catMaybes $ IntMap.elems m
67−
68−-- | Convert the given @'PatchIntMap' a@ into an @'IntMap' a@ with all
69−-- the inserts/updates in the given patch.
70−patchIntMapNewElementsMap :: PatchIntMap a -> IntMap a
71−patchIntMapNewElementsMap (PatchIntMap m) = IntMap.mapMaybe id m
72−
73−-- | Subset the given @'IntMap' a@ to contain only the keys that would be
74−-- deleted by the given @'PatchIntMap' a@.
75−getDeletions :: PatchIntMap v -> IntMap v' -> IntMap v'
76−getDeletions (PatchIntMap m) v = IntMap.intersection v m
src/Data/Patch/Map.hsdeleted−72
@@ -1,72 +0,0 @@
1−{-# LANGUAGE CPP #-}
2−{-# LANGUAGE DeriveTraversable #-}
3−{-# LANGUAGE FlexibleInstances #-}
4−{-# LANGUAGE GeneralizedNewtypeDeriving #-}
5−{-# LANGUAGE LambdaCase #-}
6−{-# LANGUAGE MultiParamTypeClasses #-}
7−{-# LANGUAGE StandaloneDeriving #-}
8−{-# LANGUAGE TemplateHaskell #-}
9−{-# LANGUAGE TypeFamilies #-}
10−
11−-- | 'Patch'es on 'Map' that consist only of insertions (including overwrites)
12−-- and deletions
13−module Data.Patch.Map where
14−
15−import Data.Patch.Class
16−
17−import Control.Lens
18−import Data.Map (Map)
19−import qualified Data.Map as Map
20−import Data.Maybe
21−import Data.Monoid.DecidablyEmpty
22−import Data.Semigroup (Semigroup (..), stimesIdempotentMonoid)
23−
24−-- | A set of changes to a 'Map'. Any element may be inserted/updated or
25−-- deleted. Insertions are represented as values wrapped in 'Just', while
26−-- deletions are represented as 'Nothing's
27−newtype PatchMap k v = PatchMap { unPatchMap :: Map k (Maybe v) }
28− deriving ( Show, Read, Eq, Ord
29− , Foldable, Traversable
30− , DecidablyEmpty
31− )
32−
33−-- | 'fmap'ping a 'PatchMap' will alter all of the values it will insert.
34−-- Deletions are unaffected.
35−deriving instance Functor (PatchMap k)
36−-- | The empty 'PatchMap' contains no insertions or deletions
37−deriving instance Ord k => Monoid (PatchMap k v)
38−
39−-- | @a <> b@ will apply the changes of @b@ and then apply the changes of @a@.
40−-- If the same key is modified by both patches, the one on the left will take
41−-- precedence.
42−instance Ord k => Semigroup (PatchMap k v) where
43− PatchMap a <> PatchMap b = PatchMap $ a `mappend` b --TODO: Add a semigroup instance for Map
44− -- PatchMap is idempotent, so stimes n is id for every n
45− stimes = stimesIdempotentMonoid
46−
47−-- | Apply the insertions or deletions to a given 'Map'.
48−instance Ord k => Patch (PatchMap k v) where
49− type PatchTarget (PatchMap k v) = Map k v
50− {-# INLINABLE apply #-}
51− apply (PatchMap p) old = Just $! insertions `Map.union` (old `Map.difference` deletions) --TODO: return Nothing sometimes --Note: the strict application here is critical to ensuring that incremental merges don't hold onto all their prerequisite events forever; can we make this more robust?
52− where insertions = Map.mapMaybeWithKey (const id) p
53− deletions = Map.mapMaybeWithKey (const nothingToJust) p
54− nothingToJust = \case
55− Nothing -> Just ()
56− Just _ -> Nothing
57−
58−instance FunctorWithIndex k (PatchMap k)
59−instance FoldableWithIndex k (PatchMap k)
60−instance TraversableWithIndex k (PatchMap k) where
61− itraverse = itraversed . Indexed
62− itraversed = _Wrapped .> itraversed <. traversed
63−
64−-- | Returns all the new elements that will be added to the 'Map'
65−patchMapNewElements :: PatchMap k v -> [v]
66−patchMapNewElements (PatchMap p) = catMaybes $ Map.elems p
67−
68−-- | Returns all the new elements that will be added to the 'Map'
69−patchMapNewElementsMap :: PatchMap k v -> Map k v
70−patchMapNewElementsMap (PatchMap p) = Map.mapMaybe id p
71−
72−makeWrapped ''PatchMap
src/Data/Patch/MapWithMove.hsdeleted−312
@@ -1,312 +0,0 @@
1−{-# LANGUAGE CPP #-}
2−{-# LANGUAGE DeriveTraversable #-}
3−{-# LANGUAGE FlexibleContexts #-}
4−{-# LANGUAGE FlexibleInstances #-}
5−{-# LANGUAGE GeneralizedNewtypeDeriving #-}
6−{-# LANGUAGE LambdaCase #-}
7−{-# LANGUAGE MultiParamTypeClasses #-}
8−{-# LANGUAGE PatternGuards #-}
9−{-# LANGUAGE PatternSynonyms #-}
10−{-# LANGUAGE ScopedTypeVariables #-}
11−{-# LANGUAGE StandaloneDeriving #-}
12−{-# LANGUAGE TemplateHaskell #-}
13−{-# LANGUAGE TypeApplications #-}
14−{-# LANGUAGE TypeFamilies #-}
15−{-# LANGUAGE ViewPatterns #-}
16−
17−-- | 'Patch'es on 'Map' that can insert, delete, and move values from one key to
18−-- another
19−module Data.Patch.MapWithMove
20− ( PatchMapWithMove
21− ( PatchMapWithMove
22− , unPatchMapWithMove
23− , ..
24− )
25− , patchMapWithMove
26− , patchMapWithMoveInsertAll
27− , insertMapKey
28− , moveMapKey
29− , swapMapKey
30− , deleteMapKey
31− , unsafePatchMapWithMove
32− , patchMapWithMoveNewElements
33− , patchMapWithMoveNewElementsMap
34− , patchThatSortsMapWith
35− , patchThatChangesAndSortsMapWith
36− , patchThatChangesMap
37−
38− -- * Node Info
39− , NodeInfo
40− ( NodeInfo
41− , _nodeInfo_to
42− , _nodeInfo_from
43− , ..
44− )
45− , bitraverseNodeInfo
46− , nodeInfoMapFrom
47− , nodeInfoMapMFrom
48− , nodeInfoSetTo
49−
50− -- * From
51− , From
52− ( From_Insert
53− , From_Delete
54− , From_Move
55− , ..
56− )
57− , bitraverseFrom
58−
59− -- * To
60− , To
61− ) where
62−
63−import Data.Coerce
64−import Data.Kind (Type)
65−import Data.Patch.Class
66−import Data.Patch.MapWithPatchingMove (PatchMapWithPatchingMove(..), To)
67−import qualified Data.Patch.MapWithPatchingMove as PM -- already a transparent synonym
68−
69−import Control.Lens
70−import Data.List
71−import Data.Map (Map)
72−import qualified Data.Map as Map
73−import Data.Proxy
74−#if !MIN_VERSION_base(4,11,0)
75−import Data.Semigroup (Semigroup (..))
76−#endif
77−import Data.Traversable (foldMapDefault)
78−
79−-- | Patch a Map with additions, deletions, and moves. Invariant: If key @k1@
80−-- is coming from @From_Move k2@, then key @k2@ should be going to @Just k1@,
81−-- and vice versa. There should never be any unpaired From/To keys.
82−newtype PatchMapWithMove k (v :: Type) = PatchMapWithMove'
83− { -- | Extract the underlying 'PatchMapWithPatchingMove k (Proxy v)'
84− unPatchMapWithMove' :: PatchMapWithPatchingMove k (Proxy v)
85− }
86− deriving ( Show, Read, Eq, Ord
87−-- Haddock cannot handle documentation here before GHC 8.6
88− ,
89−#if __GLASGOW_HASKELL__ >= 806
90− -- | Compose patches having the same effect as applying the
91− -- patches in turn: @'applyAlways' (p <> q) == 'applyAlways' p .
92− -- 'applyAlways' q@
93−#endif
94− Semigroup
95− , Monoid
96− )
97−
98−pattern Coerce :: Coercible a b => a -> b
99−pattern Coerce x <- (coerce -> x)
100− where Coerce x = coerce x
101−
102−{-# COMPLETE PatchMapWithMove #-}
103−pattern PatchMapWithMove :: Map k (NodeInfo k v) -> PatchMapWithMove k v
104−-- | Extract the representation of the 'PatchMapWithMove' as a map of
105−-- 'NodeInfo'.
106−unPatchMapWithMove :: PatchMapWithMove k v -> Map k (NodeInfo k v)
107−pattern PatchMapWithMove { unPatchMapWithMove } = PatchMapWithMove' (PatchMapWithPatchingMove (Coerce unPatchMapWithMove))
108−
109−_PatchMapWithMove
110− :: Iso
111− (PatchMapWithMove k0 v0)
112− (PatchMapWithMove k1 v1)
113− (Map k0 (NodeInfo k0 v0))
114− (Map k1 (NodeInfo k1 v1))
115−_PatchMapWithMove = iso unPatchMapWithMove PatchMapWithMove
116−
117−instance Functor (PatchMapWithMove k) where
118− fmap f = runIdentity . traverse (Identity . f)
119−
120−instance Foldable (PatchMapWithMove k) where
121− foldMap = foldMapDefault
122−
123−instance Traversable (PatchMapWithMove k) where
124− traverse =
125− _PatchMapWithMove .
126− traverse .
127− traverse
128−
129−instance FunctorWithIndex k (PatchMapWithMove k)
130−instance FoldableWithIndex k (PatchMapWithMove k)
131−instance TraversableWithIndex k (PatchMapWithMove k) where
132− itraverse = itraversed . Indexed
133− itraversed =
134− _PatchMapWithMove .>
135− itraversed <.
136− traverse
137−
138−-- | Create a 'PatchMapWithMove', validating it
139−patchMapWithMove :: Ord k => Map k (NodeInfo k v) -> Maybe (PatchMapWithMove k v)
140−patchMapWithMove = fmap PatchMapWithMove' . PM.patchMapWithPatchingMove . coerce
141−
142−-- | Create a 'PatchMapWithMove' that inserts everything in the given 'Map'
143−patchMapWithMoveInsertAll :: Map k v -> PatchMapWithMove k v
144−patchMapWithMoveInsertAll = PatchMapWithMove' . PM.patchMapWithPatchingMoveInsertAll
145−
146−-- | Make a @'PatchMapWithMove' k v@ which has the effect of inserting or updating a value @v@ to the given key @k@, like 'Map.insert'.
147−insertMapKey :: k -> v -> PatchMapWithMove k v
148−insertMapKey k v = PatchMapWithMove' $ PM.insertMapKey k v
149−
150−-- |Make a @'PatchMapWithMove' k v@ which has the effect of moving the value from the first key @k@ to the second key @k@, equivalent to:
151−--
152−-- @
153−-- 'Map.delete' src (maybe map ('Map.insert' dst) (Map.lookup src map))
154−-- @
155−moveMapKey :: Ord k => k -> k -> PatchMapWithMove k v
156−moveMapKey src dst = PatchMapWithMove' $ PM.moveMapKey src dst
157−
158−-- |Make a @'PatchMapWithMove' k v@ which has the effect of swapping two keys in the mapping, equivalent to:
159−--
160−-- @
161−-- let aMay = Map.lookup a map
162−-- bMay = Map.lookup b map
163−-- in maybe id (Map.insert a) (bMay <> aMay)
164−-- . maybe id (Map.insert b) (aMay <> bMay)
165−-- . Map.delete a . Map.delete b $ map
166−-- @
167−swapMapKey :: Ord k => k -> k -> PatchMapWithMove k v
168−swapMapKey src dst = PatchMapWithMove' $ PM.swapMapKey src dst
169−
170−-- |Make a @'PatchMapWithMove' k v@ which has the effect of deleting a key in the mapping, equivalent to 'Map.delete'.
171−deleteMapKey :: k -> PatchMapWithMove k v
172−deleteMapKey = PatchMapWithMove' . PM.deleteMapKey
173−
174−-- | Wrap a @'Map' k (NodeInfo k v)@ representing patch changes into a @'PatchMapWithMove' k v@, without checking any invariants.
175−--
176−-- __Warning:__ when using this function, you must ensure that the invariants of 'PatchMapWithMove' are preserved; they will not be checked.
177−unsafePatchMapWithMove :: Map k (NodeInfo k v) -> PatchMapWithMove k v
178−unsafePatchMapWithMove = coerce PM.unsafePatchMapWithPatchingMove
179−
180−-- | Apply the insertions, deletions, and moves to a given 'Map'
181−instance Ord k => Patch (PatchMapWithMove k v) where
182− type PatchTarget (PatchMapWithMove k v) = Map k v
183− apply (PatchMapWithMove' p) = apply p
184−
185−-- | Returns all the new elements that will be added to the 'Map'.
186−patchMapWithMoveNewElements :: PatchMapWithMove k v -> [v]
187−patchMapWithMoveNewElements = PM.patchMapWithPatchingMoveNewElements . unPatchMapWithMove'
188−
189−-- | Return a @'Map' k v@ with all the inserts/updates from the given @'PatchMapWithMove' k v@.
190−patchMapWithMoveNewElementsMap :: PatchMapWithMove k v -> Map k v
191−patchMapWithMoveNewElementsMap = PM.patchMapWithPatchingMoveNewElementsMap . unPatchMapWithMove'
192−
193−-- | Create a 'PatchMapWithMove' that, if applied to the given 'Map', will sort
194−-- its values using the given ordering function. The set keys of the 'Map' is
195−-- not changed.
196−patchThatSortsMapWith :: Ord k => (v -> v -> Ordering) -> Map k v -> PatchMapWithMove k v
197−patchThatSortsMapWith cmp = PatchMapWithMove' . PM.patchThatSortsMapWith cmp
198−
199−-- | Create a 'PatchMapWithMove' that, if applied to the first 'Map' provided,
200−-- will produce a 'Map' with the same values as the second 'Map' but with the
201−-- values sorted with the given ordering function.
202−patchThatChangesAndSortsMapWith :: (Ord k, Ord v) => (v -> v -> Ordering) -> Map k v -> Map k v -> PatchMapWithMove k v
203−patchThatChangesAndSortsMapWith cmp oldByIndex newByIndexUnsorted = patchThatChangesMap oldByIndex newByIndex
204− where newList = Map.toList newByIndexUnsorted
205− newByIndex = Map.fromList $ zip (fst <$> newList) $ sortBy cmp $ snd <$> newList
206−
207−-- | Create a 'PatchMapWithMove' that, if applied to the first 'Map' provided,
208−-- will produce the second 'Map'.
209−patchThatChangesMap :: (Ord k, Ord v) => Map k v -> Map k v -> PatchMapWithMove k v
210−patchThatChangesMap oldByIndex newByIndex = PatchMapWithMove' $
211− PM.patchThatChangesMap oldByIndex newByIndex
212−
213−--
214−-- NodeInfo
215−--
216−
217−-- | Holds the information about each key: where its new value should come from,
218−-- and where its old value should go to
219−newtype NodeInfo k (v :: Type) = NodeInfo' { unNodeInfo' :: PM.NodeInfo k (Proxy v) }
220−
221−deriving instance (Show k, Show p) => Show (NodeInfo k p)
222−deriving instance (Read k, Read p) => Read (NodeInfo k p)
223−deriving instance (Eq k, Eq p) => Eq (NodeInfo k p)
224−deriving instance (Ord k, Ord p) => Ord (NodeInfo k p)
225−
226−{-# COMPLETE NodeInfo #-}
227−pattern NodeInfo :: To k -> From k v -> NodeInfo k v
228−_nodeInfo_to :: NodeInfo k v -> To k
229−_nodeInfo_from :: NodeInfo k v -> From k v
230−pattern NodeInfo { _nodeInfo_to, _nodeInfo_from } = NodeInfo'
231− PM.NodeInfo
232− { PM._nodeInfo_to = _nodeInfo_to
233− , PM._nodeInfo_from = Coerce _nodeInfo_from
234− }
235−
236−_NodeInfo
237− :: Iso
238− (NodeInfo k0 v0)
239− (NodeInfo k1 v1)
240− (PM.NodeInfo k0 (Proxy v0))
241− (PM.NodeInfo k1 (Proxy v1))
242−_NodeInfo = iso unNodeInfo' NodeInfo'
243−
244−instance Functor (NodeInfo k) where
245− fmap f = runIdentity . traverse (Identity . f)
246−
247−instance Foldable (NodeInfo k) where
248− foldMap = foldMapDefault
249−
250−instance Traversable (NodeInfo k) where
251− traverse = bitraverseNodeInfo pure
252−
253−bitraverseNodeInfo
254− :: Applicative f
255− => (k0 -> f k1)
256− -> (v0 -> f v1)
257− -> NodeInfo k0 v0 -> f (NodeInfo k1 v1)
258−bitraverseNodeInfo fk fv = fmap NodeInfo'
259− . PM.bitraverseNodeInfo fk (\ ~Proxy -> pure Proxy) fv
260− . coerce
261−
262−-- | Change the 'From' value of a 'NodeInfo'
263−nodeInfoMapFrom :: (From k v -> From k v) -> NodeInfo k v -> NodeInfo k v
264−nodeInfoMapFrom f = coerce $ PM.nodeInfoMapFrom (unFrom' . f . From')
265−
266−-- | Change the 'From' value of a 'NodeInfo', using a 'Functor' (or
267−-- 'Applicative', 'Monad', etc.) action to get the new value
268−nodeInfoMapMFrom
269− :: Functor f
270− => (From k v -> f (From k v))
271− -> NodeInfo k v -> f (NodeInfo k v)
272−nodeInfoMapMFrom f = fmap NodeInfo'
273− . PM.nodeInfoMapMFrom (fmap unFrom' . f . From')
274− . coerce
275−
276−-- | Set the 'To' field of a 'NodeInfo'
277−nodeInfoSetTo :: To k -> NodeInfo k v -> NodeInfo k v
278−nodeInfoSetTo = coerce . PM.nodeInfoSetTo
279−
280−--
281−-- From
282−--
283−
284−-- | Describe how a key's new value should be produced
285−newtype From k (v :: Type) = From' { unFrom' :: PM.From k (Proxy v) }
286−
287−{-# COMPLETE From_Insert, From_Delete, From_Move #-}
288−
289−-- | Insert the given value here
290−pattern From_Insert :: v -> From k v
291−pattern From_Insert v = From' (PM.From_Insert v)
292−
293−-- | Delete the existing value, if any, from here
294−pattern From_Delete :: From k v
295−pattern From_Delete = From' PM.From_Delete
296−
297−-- | Move the value here from the given key
298−pattern From_Move :: k -> From k v
299−pattern From_Move k = From' (PM.From_Move k Proxy)
300−
301−bitraverseFrom
302− :: Applicative f
303− => (k0 -> f k1)
304− -> (v0 -> f v1)
305− -> From k0 v0 -> f (From k1 v1)
306−bitraverseFrom fk fv = fmap From'
307− . PM.bitraverseFrom fk (\ ~Proxy -> pure Proxy) fv
308− . coerce
309−
310−makeWrapped ''PatchMapWithMove
311−makeWrapped ''NodeInfo
312−makeWrapped ''From
src/Data/Patch/MapWithPatchingMove.hsdeleted−432
@@ -1,432 +0,0 @@
1−{-# LANGUAGE CPP #-}
2−{-# LANGUAGE GeneralizedNewtypeDeriving #-}
3−{-# LANGUAGE FlexibleContexts #-}
4−{-# LANGUAGE FlexibleInstances #-}
5−{-# LANGUAGE LambdaCase #-}
6−{-# LANGUAGE MultiParamTypeClasses #-}
7−{-# LANGUAGE PatternGuards #-}
8−{-# LANGUAGE ScopedTypeVariables #-}
9−{-# LANGUAGE StandaloneDeriving #-}
10−{-# LANGUAGE TemplateHaskell #-}
11−{-# LANGUAGE TypeFamilies #-}
12−{-# LANGUAGE UndecidableInstances #-}
13−
14−-- | 'Patch'es on 'Map' that can insert, delete, and move values from one key to
15−-- another
16−module Data.Patch.MapWithPatchingMove
17− ( PatchMapWithPatchingMove (..)
18− , patchMapWithPatchingMove
19− , patchMapWithPatchingMoveInsertAll
20− , insertMapKey
21− , moveMapKey
22− , swapMapKey
23− , deleteMapKey
24− , unsafePatchMapWithPatchingMove
25− , patchMapWithPatchingMoveNewElements
26− , patchMapWithPatchingMoveNewElementsMap
27− , patchThatSortsMapWith
28− , patchThatChangesAndSortsMapWith
29− , patchThatChangesMap
30−
31− -- * Node Info
32− , NodeInfo (..)
33− , bitraverseNodeInfo
34− , nodeInfoMapFrom
35− , nodeInfoMapMFrom
36− , nodeInfoSetTo
37−
38− -- * From
39− , From(..)
40− , bitraverseFrom
41−
42− -- * To
43− , To
44−
45− -- TODO internals module
46− , Fixup (..)
47− ) where
48−
49−import Data.Patch.Class
50−
51−import Control.Lens hiding (from, to)
52−import Control.Lens.TH (makeWrapped)
53−import Data.Align (align)
54−import Data.Foldable (toList)
55−import Data.Function
56−import Data.List
57−import Data.Map (Map)
58−import qualified Data.Map as Map
59−import Data.Maybe
60−#if !MIN_VERSION_base(4,11,0)
61−import Data.Semigroup (Semigroup (..))
62−#endif
63−import Data.Monoid.DecidablyEmpty
64−import Data.Set (Set)
65−import qualified Data.Set as Set
66−import Data.These (These (..))
67−
68−-- | Patch a Map with additions, deletions, and moves. Invariant: If key @k1@
69−-- is coming from @From_Move k2@, then key @k2@ should be going to @Just k1@,
70−-- and vice versa. There should never be any unpaired From/To keys.
71−newtype PatchMapWithPatchingMove k p = PatchMapWithPatchingMove
72− { -- | Extract the internal representation of the 'PatchMapWithPatchingMove'
73− unPatchMapWithPatchingMove :: Map k (NodeInfo k p)
74− }
75−
76−deriving instance (Show k, Show p, Show (PatchTarget p)) => Show (PatchMapWithPatchingMove k p)
77−deriving instance (Ord k, Read k, Read p, Read (PatchTarget p)) => Read (PatchMapWithPatchingMove k p)
78−deriving instance (Eq k, Eq p, Eq (PatchTarget p)) => Eq (PatchMapWithPatchingMove k p)
79−deriving instance (Ord k, Ord p, Ord (PatchTarget p)) => Ord (PatchMapWithPatchingMove k p)
80−
81−deriving instance ( Ord k
82−#if !MIN_VERSION_base(4,11,0)
83− , Semigroup p
84−#endif
85− , DecidablyEmpty p
86− , Patch p
87− ) => DecidablyEmpty (PatchMapWithPatchingMove k p)
88−
89−-- | Create a 'PatchMapWithPatchingMove', validating it
90−patchMapWithPatchingMove
91− :: Ord k => Map k (NodeInfo k p) -> Maybe (PatchMapWithPatchingMove k p)
92−patchMapWithPatchingMove m = if valid then Just $ PatchMapWithPatchingMove m else Nothing
93− where valid = forwardLinks == backwardLinks
94− forwardLinks = Map.mapMaybe _nodeInfo_to m
95− backwardLinks = Map.fromList $ catMaybes $ flip fmap (Map.toList m) $ \(to, p) ->
96− case _nodeInfo_from p of
97− From_Move from _ -> Just (from, to)
98− _ -> Nothing
99−
100−-- | Create a 'PatchMapWithPatchingMove' that inserts everything in the given 'Map'
101−patchMapWithPatchingMoveInsertAll
102− :: Map k (PatchTarget p) -> PatchMapWithPatchingMove k p
103−patchMapWithPatchingMoveInsertAll m = PatchMapWithPatchingMove $ flip fmap m $ \v -> NodeInfo
104− { _nodeInfo_from = From_Insert v
105− , _nodeInfo_to = Nothing
106− }
107−
108−-- | Make a @'PatchMapWithPatchingMove' k p@ which has the effect of inserting or replacing a value @v@ at the given key @k@, like 'Map.insert'.
109−insertMapKey
110− :: k -> PatchTarget p -> PatchMapWithPatchingMove k p
111−insertMapKey k v = PatchMapWithPatchingMove . Map.singleton k $ NodeInfo (From_Insert v) Nothing
112−
113−-- |Make a @'PatchMapWithPatchingMove' k p@ which has the effect of moving the value from the first key @k@ to the second key @k@, equivalent to:
114−--
115−-- @
116−-- 'Map.delete' src (maybe map ('Map.insert' dst) (Map.lookup src map))
117−-- @
118−moveMapKey
119− :: ( DecidablyEmpty p
120−#if !MIN_VERSION_base(4,11,0)
121− , Semigroup p
122−#endif
123− , Patch p
124− )
125− => Ord k => k -> k -> PatchMapWithPatchingMove k p
126−moveMapKey src dst
127− | src == dst = mempty
128− | otherwise =
129− PatchMapWithPatchingMove $ Map.fromList
130− [ (dst, NodeInfo (From_Move src mempty) Nothing)
131− , (src, NodeInfo From_Delete (Just dst))
132− ]
133−
134−-- |Make a @'PatchMapWithPatchingMove' k p@ which has the effect of swapping two keys in the mapping, equivalent to:
135−--
136−-- @
137−-- let aMay = Map.lookup a map
138−-- bMay = Map.lookup b map
139−-- in maybe id (Map.insert a) (bMay <> aMay)
140−-- . maybe id (Map.insert b) (aMay <> bMay)
141−-- . Map.delete a . Map.delete b $ map
142−-- @
143−swapMapKey
144− :: ( DecidablyEmpty p
145−#if !MIN_VERSION_base(4,11,0)
146− , Semigroup p
147−#endif
148− , Patch p
149− )
150− => Ord k => k -> k -> PatchMapWithPatchingMove k p
151−swapMapKey src dst
152− | src == dst = mempty
153− | otherwise =
154− PatchMapWithPatchingMove $ Map.fromList
155− [ (dst, NodeInfo (From_Move src mempty) (Just src))
156− , (src, NodeInfo (From_Move dst mempty) (Just dst))
157− ]
158−
159−-- | Make a @'PatchMapWithPatchingMove' k v@ which has the effect of deleting a key in
160−-- the mapping, equivalent to 'Map.delete'.
161−deleteMapKey
162− :: k -> PatchMapWithPatchingMove k v
163−deleteMapKey k = PatchMapWithPatchingMove . Map.singleton k $ NodeInfo From_Delete Nothing
164−
165−-- | Wrap a @'Map' k (NodeInfo k v)@ representing patch changes into a @'PatchMapWithPatchingMove' k v@, without checking any invariants.
166−--
167−-- __Warning:__ when using this function, you must ensure that the invariants of 'PatchMapWithPatchingMove' are preserved; they will not be checked.
168−unsafePatchMapWithPatchingMove
169− :: Map k (NodeInfo k p) -> PatchMapWithPatchingMove k p
170−unsafePatchMapWithPatchingMove = PatchMapWithPatchingMove
171−
172−-- | Apply the insertions, deletions, and moves to a given 'Map'
173−instance (Ord k, Patch p) => Patch (PatchMapWithPatchingMove k p) where
174− type PatchTarget (PatchMapWithPatchingMove k p) = Map k (PatchTarget p)
175− -- TODO: return Nothing sometimes
176− -- Note: the strict application here is critical to ensuring that incremental
177− -- merges don't hold onto all their prerequisite events forever; can we make
178− -- this more robust?
179− apply (PatchMapWithPatchingMove m) old = Just $! insertions `Map.union` (old `Map.difference` deletions)
180− where insertions = flip Map.mapMaybeWithKey m $ \_ ni -> case _nodeInfo_from ni of
181− From_Insert v -> Just v
182− From_Move k p -> applyAlways p <$> Map.lookup k old
183− From_Delete -> Nothing
184− deletions = flip Map.mapMaybeWithKey m $ \_ ni -> case _nodeInfo_from ni of
185− From_Delete -> Just ()
186− _ -> Nothing
187−
188−-- | Returns all the new elements that will be added to the 'Map'
189−patchMapWithPatchingMoveNewElements
190− :: PatchMapWithPatchingMove k p -> [PatchTarget p]
191−patchMapWithPatchingMoveNewElements = Map.elems . patchMapWithPatchingMoveNewElementsMap
192−
193−-- | Return a @'Map' k v@ with all the inserts/updates from the given @'PatchMapWithPatchingMove' k v@.
194−patchMapWithPatchingMoveNewElementsMap
195− :: PatchMapWithPatchingMove k p -> Map k (PatchTarget p)
196−patchMapWithPatchingMoveNewElementsMap (PatchMapWithPatchingMove p) = Map.mapMaybe f p
197− where f ni = case _nodeInfo_from ni of
198− From_Insert v -> Just v
199− From_Move _ _ -> Nothing
200− From_Delete -> Nothing
201−
202−-- | Create a 'PatchMapWithPatchingMove' that, if applied to the given 'Map', will sort
203−-- its values using the given ordering function. The set keys of the 'Map' is
204−-- not changed.
205−patchThatSortsMapWith
206− :: (Ord k, Monoid p)
207− => (PatchTarget p -> PatchTarget p -> Ordering)
208− -> Map k (PatchTarget p) -> PatchMapWithPatchingMove k p
209−patchThatSortsMapWith cmp m = PatchMapWithPatchingMove $ Map.fromList $ catMaybes $ zipWith g unsorted sorted
210− where unsorted = Map.toList m
211− sorted = sortBy (cmp `on` snd) unsorted
212− f (to, _) (from, _) = if to == from then Nothing else
213− Just (from, to)
214− reverseMapping = Map.fromList $ catMaybes $ zipWith f unsorted sorted
215− g (to, _) (from, _) = if to == from then Nothing else
216− let Just movingTo = Map.lookup from reverseMapping
217− in Just (to, NodeInfo (From_Move from mempty) $ Just movingTo)
218−
219−-- | Create a 'PatchMapWithPatchingMove' that, if applied to the first 'Map' provided,
220−-- will produce a 'Map' with the same values as the second 'Map' but with the
221−-- values sorted with the given ordering function.
222−patchThatChangesAndSortsMapWith
223− :: forall k p. (Ord k, Ord (PatchTarget p), Monoid p)
224− => (PatchTarget p -> PatchTarget p -> Ordering)
225− -> Map k (PatchTarget p) -> Map k (PatchTarget p) -> PatchMapWithPatchingMove k p
226−patchThatChangesAndSortsMapWith cmp oldByIndex newByIndexUnsorted = patchThatChangesMap oldByIndex newByIndex
227− where newList = Map.toList newByIndexUnsorted
228− newByIndex = Map.fromList $ zip (fst <$> newList) $ sortBy cmp $ snd <$> newList
229−
230−-- | Create a 'PatchMapWithPatchingMove' that, if applied to the first 'Map' provided,
231−-- will produce the second 'Map'.
232−-- Note: this will never produce a patch on a value.
233−patchThatChangesMap
234− :: forall k p
235− . (Ord k, Ord (PatchTarget p), Monoid p)
236− => Map k (PatchTarget p) -> Map k (PatchTarget p) -> PatchMapWithPatchingMove k p
237−patchThatChangesMap oldByIndex newByIndex = patch
238− where invert :: Map k (PatchTarget p) -> Map (PatchTarget p) (Set k)
239− invert = Map.fromListWith (<>) . fmap (\(k, v) -> (v, Set.singleton k)) . Map.toList
240− -- In the places where we use unionDistinct, a non-distinct key indicates a bug in this function
241− unionDistinct :: forall k' v'. Ord k' => Map k' v' -> Map k' v' -> Map k' v'
242− unionDistinct = Map.unionWith (error "patchThatChangesMap: non-distinct keys")
243− unionPairDistinct :: (Map k (From k v), Map k (To k)) -> (Map k (From k v), Map k (To k)) -> (Map k (From k v), Map k (To k))
244− unionPairDistinct (oldFroms, oldTos) (newFroms, newTos) = (unionDistinct oldFroms newFroms, unionDistinct oldTos newTos)
245− -- Generate patch info for a single value
246− -- Keys that are found in both the old and new sets will not be patched
247− -- Keys that are found in only the old set will be moved to a new position if any are available; otherwise they will be deleted
248− -- Keys that are found in only the new set will be populated by moving an old key if any are available; otherwise they will be inserted
249− patchSingleValue :: PatchTarget p -> Set k -> Set k -> (Map k (From k p), Map k (To k))
250− patchSingleValue v oldKeys newKeys = foldl' unionPairDistinct mempty $ align (toList $ oldKeys `Set.difference` newKeys) (toList $ newKeys `Set.difference` oldKeys) <&> \case
251− This oldK -> (mempty, Map.singleton oldK Nothing) -- There's nowhere for this value to go, so we know we are deleting it
252− That newK -> (Map.singleton newK $ From_Insert v, mempty) -- There's nowhere fo this value to come from, so we know we are inserting it
253− These oldK newK -> (Map.singleton newK $ From_Move oldK mempty, Map.singleton oldK $ Just newK)
254− -- Run patchSingleValue on a These. Missing old or new sets are considered empty
255− patchSingleValueThese :: PatchTarget p -> These (Set k) (Set k) -> (Map k (From k p), Map k (To k))
256− patchSingleValueThese v = \case
257− This oldKeys -> patchSingleValue v oldKeys mempty
258− That newKeys -> patchSingleValue v mempty newKeys
259− These oldKeys newKeys -> patchSingleValue v oldKeys newKeys
260− -- Generate froms and tos for all values, then merge them together
261− (froms, tos) = foldl' unionPairDistinct mempty $ Map.mapWithKey patchSingleValueThese $ align (invert oldByIndex) (invert newByIndex)
262− patch = unsafePatchMapWithPatchingMove $ align froms tos <&> \case
263− This from -> NodeInfo from Nothing -- Since we don't have a 'to' record for this key, that must mean it isn't being moved anywhere, so it should be deleted.
264− That to -> NodeInfo From_Delete to -- Since we don't have a 'from' record for this key, it must be getting deleted
265− These from to -> NodeInfo from to
266−
267−--
268−-- NodeInfo
269−--
270−
271−-- | Holds the information about each key: where its new value should come from,
272−-- and where its old value should go to
273−data NodeInfo k p = NodeInfo
274− { _nodeInfo_from :: !(From k p)
275− -- ^ Where do we get the new value for this key?
276− , _nodeInfo_to :: !(To k)
277− -- ^ If the old value is being kept (i.e. moved rather than deleted or
278− -- replaced), where is it going?
279− }
280−deriving instance (Show k, Show p, Show (PatchTarget p)) => Show (NodeInfo k p)
281−deriving instance (Read k, Read p, Read (PatchTarget p)) => Read (NodeInfo k p)
282−deriving instance (Eq k, Eq p, Eq (PatchTarget p)) => Eq (NodeInfo k p)
283−deriving instance (Ord k, Ord p, Ord (PatchTarget p)) => Ord (NodeInfo k p)
284−
285−-- | Traverse the 'NodeInfo' over the key, patch, and patch target. Because of
286−-- the type families here, this doesn't it any bi- or tri-traversal class.
287−bitraverseNodeInfo
288− :: Applicative f
289− => (k0 -> f k1)
290− -> (p0 -> f p1)
291− -> (PatchTarget p0 -> f (PatchTarget p1))
292− -> NodeInfo k0 p0 -> f (NodeInfo k1 p1)
293−bitraverseNodeInfo fk fp fpt (NodeInfo from to) = NodeInfo
294− <$> bitraverseFrom fk fp fpt from
295− <*> traverse fk to
296−
297−-- | Change the 'From' value of a 'NodeInfo'
298−nodeInfoMapFrom
299− :: (From k v -> From k v) -> NodeInfo k v -> NodeInfo k v
300−nodeInfoMapFrom f ni = ni { _nodeInfo_from = f $ _nodeInfo_from ni }
301−
302−-- | Change the 'From' value of a 'NodeInfo', using a 'Functor' (or
303−-- 'Applicative', 'Monad', etc.) action to get the new value
304−nodeInfoMapMFrom
305− :: Functor f => (From k v -> f (From k v)) -> NodeInfo k v -> f (NodeInfo k v)
306−nodeInfoMapMFrom f ni = fmap (\result -> ni { _nodeInfo_from = result }) $ f $ _nodeInfo_from ni
307−
308−-- | Set the 'To' field of a 'NodeInfo'
309−nodeInfoSetTo
310− :: To k -> NodeInfo k v -> NodeInfo k v
311−nodeInfoSetTo to ni = ni { _nodeInfo_to = to }
312−
313−--
314−-- From
315−--
316−
317−-- | Describe how a key's new value should be produced
318−data From k p
319− = From_Insert (PatchTarget p) -- ^ Insert the given value here
320− | From_Delete -- ^ Delete the existing value, if any, from here
321− | From_Move !k !p -- ^ Move the value here from the given key, and apply the given patch
322−
323−deriving instance (Show k, Show p, Show (PatchTarget p)) => Show (From k p)
324−deriving instance (Read k, Read p, Read (PatchTarget p)) => Read (From k p)
325−deriving instance (Eq k, Eq p, Eq (PatchTarget p)) => Eq (From k p)
326−deriving instance (Ord k, Ord p, Ord (PatchTarget p)) => Ord (From k p)
327−
328−-- | Traverse the 'From' over the key, patch, and patch target. Because of
329−-- the type families here, this doesn't it any bi- or tri-traversal class.
330−bitraverseFrom
331− :: Applicative f
332− => (k0 -> f k1)
333− -> (p0 -> f p1)
334− -> (PatchTarget p0 -> f (PatchTarget p1))
335− -> From k0 p0 -> f (From k1 p1)
336−bitraverseFrom fk fp fpt = \case
337− From_Insert pt -> From_Insert <$> fpt pt
338− From_Delete -> pure From_Delete
339− From_Move k p -> From_Move <$> fk k <*> fp p
340−
341−--
342−-- To
343−--
344−
345−-- | Describe where a key's old value will go. If this is 'Just', that means
346−-- the key's old value will be moved to the given other key; if it is 'Nothing',
347−-- that means it will be deleted.
348−type To = Maybe
349−
350−--
351−-- Fixup
352−--
353−
354−-- | Helper data structure used for composing patches using the monoid instance.
355−data Fixup k v
356− = Fixup_Delete
357− | Fixup_Update (These (From k v) (To k))
358−
359−-- | Compose patches having the same effect as applying the patches in turn:
360−-- @'applyAlways' (p <> q) == 'applyAlways' p . 'applyAlways' q@
361−instance ( Ord k
362−#if !MIN_VERSION_base(4,11,0)
363− , Semigroup p
364−#endif
365− , DecidablyEmpty p
366− , Patch p
367− ) => Semigroup (PatchMapWithPatchingMove k p) where
368− PatchMapWithPatchingMove ma <> PatchMapWithPatchingMove mb = PatchMapWithPatchingMove m
369− where
370− connections = Map.toList $ Map.intersectionWithKey (\_ a b -> (_nodeInfo_to a, _nodeInfo_from b)) ma mb
371− h :: (k, (Maybe k, From k p)) -> [(k, Fixup k p)]
372− h (_, (mToAfter, editBefore)) = case (mToAfter, editBefore) of
373− (Just toAfter, From_Move fromBefore p)
374− | fromBefore == toAfter && isEmpty p
375− -> [(toAfter, Fixup_Delete)]
376− | otherwise
377− -> [ (toAfter, Fixup_Update (This editBefore))
378− , (fromBefore, Fixup_Update (That mToAfter))
379− ]
380− (Nothing, From_Move fromBefore _) -> [(fromBefore, Fixup_Update (That mToAfter))] -- The item is destroyed in the second patch, so indicate that it is destroyed in the source map
381− (Just toAfter, _) -> [(toAfter, Fixup_Update (This editBefore))]
382− (Nothing, _) -> []
383− mergeFixups _ Fixup_Delete Fixup_Delete = Fixup_Delete
384− mergeFixups _ (Fixup_Update a) (Fixup_Update b)
385− | This x <- a, That y <- b
386− = Fixup_Update $ These x y
387− | That y <- a, This x <- b
388− = Fixup_Update $ These x y
389− mergeFixups _ _ _ = error "PatchMapWithPatchingMove: incompatible fixups"
390− fixups = Map.fromListWithKey mergeFixups $ concatMap h connections
391− combineNodeInfos _ nia nib = NodeInfo
392− { _nodeInfo_from = _nodeInfo_from nia
393− , _nodeInfo_to = _nodeInfo_to nib
394− }
395− applyFixup _ ni = \case
396− Fixup_Delete -> Nothing
397− Fixup_Update u -> Just $ NodeInfo
398− { _nodeInfo_from = case _nodeInfo_from ni of
399− f@(From_Move _ p') -> case getHere u of -- The `from` fixup comes from the "old" patch
400− Nothing -> f -- If there's no `from` fixup, just use the "new" `from`
401− Just (From_Insert v) -> From_Insert $ applyAlways p' v
402− Just From_Delete -> From_Delete
403− Just (From_Move oldKey p) -> From_Move oldKey $ p' <> p
404− _ -> error "PatchMapWithPatchingMove: fixup for non-move From"
405− , _nodeInfo_to = fromMaybe (_nodeInfo_to ni) $ getThere u
406− }
407− m = Map.differenceWithKey applyFixup (Map.unionWithKey combineNodeInfos ma mb) fixups
408− getHere :: These a b -> Maybe a
409− getHere = \case
410− This a -> Just a
411− These a _ -> Just a
412− That _ -> Nothing
413− getThere :: These a b -> Maybe b
414− getThere = \case
415− This _ -> Nothing
416− These _ b -> Just b
417− That b -> Just b
418−
419−--TODO: Figure out how to implement this in terms of PatchDMapWithPatchingMove rather than duplicating it here
420−-- | Compose patches having the same effect as applying the patches in turn:
421−-- @'applyAlways' (p <> q) == 'applyAlways' p . 'applyAlways' q@
422−instance ( Ord k
423−#if !MIN_VERSION_base(4,11,0)
424− , Semigroup p
425−#endif
426− , DecidablyEmpty p
427− , Patch p
428− ) => Monoid (PatchMapWithPatchingMove k p) where
429− mempty = PatchMapWithPatchingMove mempty
430− mappend = (<>)
431−
432−makeWrapped ''PatchMapWithPatchingMove
src/Data/Semigroup/Additive.hsdeleted−54
@@ -1,54 +0,0 @@
1−{-# LANGUAGE CPP #-}
2−{-# LANGUAGE TypeOperators #-}
3−{-# LANGUAGE TypeFamilies #-}
4−-- |
5−-- Module:
6−-- Data.Semigroup.Additive
7−-- Description:
8−-- This module defines a class for commutative semigroups, until it is moved
9−-- to another library.
10−module Data.Semigroup.Additive
11− ( Additive
12− ) where
13−
14−import Data.Functor.Const (Const (..))
15−import Data.Functor.Identity
16−import Data.Proxy
17−#if !MIN_VERSION_base(4,12,0)
18−-- for :*: and :.: semigroup instances
19−import Data.Orphans ()
20−#endif
21−#if !MIN_VERSION_base(4,11,0)
22−import Data.Semigroup (Semigroup (..))
23−#endif
24−import GHC.Generics
25−
26−-- | An 'Additive' 'Semigroup' is one where (<>) is commutative
27−class Semigroup q => Additive q where
28−
29−-- | Trivial additive semigroup.
30−instance Additive ()
31−
32−-- | Product additive semigroup.
33−-- A Pair of additive semigroups gives rise to a additive semigroup
34−instance (Additive a, Additive b) => Additive (a, b)
35−
36−-- See https://gitlab.haskell.org/ghc/ghc/issues/11135#note_111802 for the reason Compose is not also provided.
37−-- Base does not define Monoid (Compose f g a) so this is the best we can
38−-- really do for functor composition.
39−instance Additive (f (g a)) => Additive ((f :.: g) a)
40−
41−-- | Product of additive semigroups, Functor style.
42−instance (Additive (f a), Additive (g a)) => Additive ((f :*: g) a)
43−
44−-- | Trivial additive semigroup, Functor style
45−instance Additive (Proxy x)
46−
47−-- | Const lifts additive semigroups into a functor.
48−instance Additive a => Additive (Const a x)
49−
50−-- | Identity lifts additive semigroups pointwise (at only one point)
51−instance Additive a => Additive (Identity a)
52−
53−-- | Functions lift additive semigroups pointwise.
54−instance Additive b => Additive (a -> b)
src/Data/WeakBag.hsnew file+81
@@ -0,0 +1,81 @@
1+{-# LANGUAGE BangPatterns #-}
2+module Data.WeakBag (WeakBag, WeakBagTicket, empty, singleton, insert, Data.WeakBag.traverse, remove) where
3+
4+import Control.Concurrent.STM
5+import Control.Exception
6+import Control.Monad
7+import Control.Monad.IO.Class
8+import Data.IORef
9+import System.Mem.Weak
10+import Data.IntMap.Strict (IntMap)
11+import qualified Data.IntMap.Strict as IntMap
12+
13+data WeakBag a = WeakBag
14+ { _weakBag_nextId :: {-# UNPACK #-} !(TVar Int) --TODO: what if this wraps around?
15+ , _weakBag_children :: {-# UNPACK #-} !(TVar (IntMap (Weak a)))
16+ }
17+
18+data WeakBagTicket a = WeakBagTicket
19+ { _weakBagTicket_weakItem :: {-# UNPACK #-} !(Weak a)
20+ , _weakBagTicket_item :: {-# NOUNPACK #-} !a
21+ }
22+
23+{-# INLINE insert #-}
24+insert :: a -> WeakBag a -> IORef (Weak b) -> (b -> IO ()) -> IO (WeakBagTicket a)
25+insert a (WeakBag nextId children) wbRef finalizer = do
26+ a' <- evaluate a
27+ wbRef' <- evaluate wbRef
28+ myId <- atomically $ do
29+ myId <- readTVar nextId
30+ writeTVar nextId $! succ myId
31+ return myId
32+ let cleanup = do
33+ wb <- readIORef wbRef'
34+ mb <- deRefWeak wb
35+ forM_ mb $ \b -> do
36+ isLastNode <- atomically $ do
37+ cs <- readTVar children
38+ let csWithoutMe = IntMap.delete myId cs
39+ writeTVar children $! csWithoutMe
40+ return $ IntMap.size csWithoutMe == 0
41+ when isLastNode $ finalizer b
42+ return ()
43+ return ()
44+ wa <- mkWeakPtr a' $ Just cleanup
45+ atomically $ modifyTVar' children $ IntMap.insert myId wa
46+ return $ WeakBagTicket
47+ { _weakBagTicket_weakItem = wa
48+ , _weakBagTicket_item = a'
49+ }
50+
51+{-# INLINE empty #-}
52+empty :: IO (WeakBag a)
53+empty = do
54+ nextId <- newTVarIO 1
55+ children <- newTVarIO IntMap.empty
56+ let bag = WeakBag
57+ { _weakBag_nextId = nextId
58+ , _weakBag_children = children
59+ }
60+ return bag
61+
62+{-# INLINE singleton #-}
63+singleton :: a -> IORef (Weak b) -> (b -> IO ()) -> IO (WeakBag a, WeakBagTicket a)
64+singleton a wbRef finalizer = do
65+ bag <- empty
66+ ticket <- insert a bag wbRef finalizer
67+ return (bag, ticket)
68+
69+{-# INLINE traverse #-}
70+-- | Visit every node in the given list. If new nodes are appended during the traversal, they will not be visited.
71+-- Every live node that was in the list when the traversal began will be visited exactly once; however, no guarantee is made about the order of the traversal.
72+traverse :: MonadIO m => WeakBag a -> (a -> m ()) -> m ()
73+traverse (WeakBag _ children) f = do
74+ cs <- liftIO $ readTVarIO children
75+ forM_ cs $ \c -> do
76+ ma <- liftIO $ deRefWeak c
77+ mapM_ f ma
78+
79+{-# INLINE remove #-}
80+remove :: WeakBagTicket a -> IO ()
81+remove = finalize . _weakBagTicket_weakItem
src/Reflex.hsnew file+10
@@ -0,0 +1,10 @@
1+module Reflex ( module Reflex.Class
2+ , module Reflex.Dynamic
3+ , module Reflex.Dynamic.TH
4+ , module Reflex.Spider
5+ ) where
6+
7+import Reflex.Class
8+import Reflex.Dynamic
9+import Reflex.Dynamic.TH
10+import Reflex.Spider
src/Reflex/Class.hsnew file+503
@@ -0,0 +1,503 @@
1+{-# LANGUAGE TypeFamilies, FlexibleContexts, FlexibleInstances, MultiParamTypeClasses, RankNTypes, GADTs, ScopedTypeVariables, FunctionalDependencies, RecursiveDo, UndecidableInstances, GeneralizedNewtypeDeriving, StandaloneDeriving, EmptyDataDecls, NoMonomorphismRestriction, TypeOperators, DeriveDataTypeable, PackageImports, TemplateHaskell, LambdaCase, CPP #-}
2+module Reflex.Class where
3+
4+import Control.Applicative
5+import Control.Monad.Identity hiding (mapM, mapM_, forM, forM_, sequence, sequence_)
6+import Control.Monad.State.Strict hiding (mapM, mapM_, forM, forM_, sequence, sequence_)
7+import Control.Monad.Reader hiding (mapM, mapM_, forM, forM_, sequence, sequence_)
8+import Control.Monad.Trans.Writer (WriterT())
9+import Control.Monad.Trans.Except (ExceptT())
10+import Control.Monad.Trans.Cont (ContT())
11+import Control.Monad.Trans.RWS (RWST())
12+import Data.List.NonEmpty (NonEmpty (..))
13+import Data.These
14+import Data.Align
15+import Data.GADT.Compare (GEq (..), (:~:) (..))
16+import Data.GADT.Show (GShow (..))
17+import Data.Dependent.Sum (ShowTag (..))
18+import Data.Map (Map)
19+import Data.Dependent.Map (DMap, DSum (..), GCompare (..), GOrdering (..))
20+import qualified Data.Dependent.Map as DMap
21+import Data.Functor.Misc
22+import Data.Semigroup (Semigroup, (<>), sconcat, stimes, stimesIdempotentMonoid)
23+import Data.Traversable
24+import Data.Functor.Compose
25+import Data.Functor.Constant
26+import Data.Foldable
27+import Data.Maybe
28+
29+-- Note: must come last to silence warnings due to AMP on GHC < 7.10
30+import Prelude hiding (mapM, mapM_, sequence, sequence_, foldl)
31+
32+import Debug.Trace (trace)
33+
34+class (MonadHold t (PushM t), MonadSample t (PullM t), MonadFix (PushM t), Functor (Event t), Functor (Behavior t), Functor (Dynamic t), Monad (Dynamic t)) => Reflex t where
35+ -- | A container for a value that can change over time. Behaviors can be sampled at will, but it is not possible to be notified when they change
36+ data Behavior t :: * -> *
37+ -- | A stream of occurrences. During any given frame, an Event is either occurring or not occurring; if it is occurring, it will contain a value of the given type (its "occurrence type")
38+ data Event t :: * -> *
39+ -- | A container for a value that can change over time and allows notifications on changes.
40+ -- Basically a combination of a 'Behavior' and an 'Event', with a rule that the Behavior will
41+ -- change if and only if the Event fires.
42+ data Dynamic t :: * -> *
43+ -- | A container for a value that can change over time and allows notifications on changes.
44+ -- Basically a combination of a 'Behavior' and an 'Event', with a rule that the Behavior will
45+ -- change if and only if the Event fires.
46+ data Incremental t :: (* -> *) -> * -> *
47+ -- | An Event with no occurrences
48+ never :: Event t a
49+ -- | Create a Behavior that always has the given value
50+ constant :: a -> Behavior t a --TODO: Refactor to use 'pure' from Applicative instead; however, we need to make sure that encouraging Applicative-style use of Behaviors doesn't have a negative performance impact
51+ -- | Create an Event from another Event; the provided function can sample Behaviors and hold Events, and use the results to produce a occurring (Just) or non-occurring (Nothing) result
52+ push :: (a -> PushM t (Maybe b)) -> Event t a -> Event t b
53+ -- | A monad for doing complex push-based calculations efficiently
54+ type PushM t :: * -> *
55+ -- | Create a Behavior by reading from other Behaviors; the result will be recomputed whenever any of the read Behaviors changes
56+ pull :: PullM t a -> Behavior t a
57+ -- | A monad for doing complex pull-based calculations efficiently
58+ type PullM t :: * -> *
59+ -- | Merge a collection of events; the resulting Event will only occur if at least one input event is occuring, and will contain all of the input keys that are occurring simultaneously
60+ merge :: GCompare k => DMap k (Event t) -> Event t (DMap k Identity) --TODO: Generalize to get rid of DMap use --TODO: Provide a type-level guarantee that the result is not empty
61+ -- | Efficiently fan-out an event to many destinations. This function should be partially applied, and then the result applied repeatedly to create child events
62+ fan :: GCompare k => Event t (DMap k Identity) -> EventSelector t k --TODO: Can we help enforce the partial application discipline here? The combinator is worthless without it
63+ -- | Create an Event that will occur whenever the currently-selected input Event occurs
64+ switch :: Behavior t (Event t a) -> Event t a
65+ -- | Create an Event that will occur whenever the input event is occurring and its occurrence value, another Event, is also occurring
66+ coincidence :: Event t (Event t a) -> Event t a
67+ -- | Extract the 'Behavior' of a 'Dynamic'.
68+ current :: Dynamic t a -> Behavior t a
69+ -- | Extract the 'Event' of the 'Dynamic'.
70+ updated :: Dynamic t a -> Event t a
71+ -- | Create a new 'Dynamic'. The given PullM must always return the most recent firing of the given Event, if any.
72+ unsafeBuildDynamic :: PullM t a -> Event t a -> Dynamic t a
73+ -- | Create a new 'Incremental'. The given PullM's value must always change in the same way that the accumulated application of patches would change that value.
74+ unsafeBuildIncremental :: Patch p => PullM t a -> Event t (p a) -> Incremental t p a
75+ -- | Create a merge whose parents can change over time
76+ mergeIncremental :: GCompare k => Incremental t PatchDMap (DMap k (Event t)) -> Event t (DMap k Identity)
77+ -- | Extract the 'Behavior' component of an 'Incremental'
78+ currentIncremental :: Patch p => Incremental t p a -> Behavior t a
79+ -- | Extract the 'Event' component of an 'Incremental'
80+ updatedIncremental :: Patch p => Incremental t p a -> Event t (p a)
81+ -- | Convert an 'Incremental' to a 'Dynamic'
82+ incrementalToDynamic :: Patch p => Incremental t p a -> Dynamic t a
83+
84+class Patch p where
85+ apply :: p a -> a -> Maybe a -- Return Nothing if no change is necessary
86+
87+instance Patch Identity where
88+ apply (Identity a) _ = Just a
89+
90+data PatchDMap a where
91+ PatchDMap :: GCompare k => DMap k (Compose Maybe v) -> PatchDMap (DMap k v)
92+
93+instance GCompare k => Semigroup (PatchDMap (DMap k v)) where
94+ PatchDMap a <> PatchDMap b = PatchDMap $ a `mappend` b --TODO: Add a semigroup instance for DMap
95+ -- PatchDMap is idempotent, so stimes n is id for every n
96+#if MIN_VERSION_semigroups(0,17,0)
97+ stimes = stimesIdempotentMonoid
98+#else
99+ times1p n x = case compare n 0 of
100+ LT -> error "stimesIdempotentMonoid: negative multiplier"
101+ EQ -> mempty
102+ GT -> x
103+#endif
104+
105+instance GCompare k => Monoid (PatchDMap (DMap k v)) where
106+ mempty = PatchDMap mempty
107+ mappend = (<>)
108+
109+instance Patch PatchDMap where
110+ apply (PatchDMap diff) old = Just $! insertions `DMap.union` (old `DMap.difference` deletions) --TODO: return Nothing sometimes --Note: the strict application here is critical to ensuring that incremental merges don't hold onto all their prerequisite events forever; can we make this more robust?
111+ where insertions = DMap.mapMaybeWithKey (const $ id . getCompose) diff
112+ deletions = DMap.mapMaybeWithKey (const $ nothingToJust . getCompose) diff
113+ nothingToJust = \case
114+ Nothing -> Just $ Constant ()
115+ Just _ -> Nothing
116+
117+unsafeDynamic :: Reflex t => Behavior t a -> Event t a -> Dynamic t a
118+unsafeDynamic = unsafeBuildDynamic . sample
119+
120+constDyn :: Reflex t => a -> Dynamic t a
121+constDyn = pure
122+
123+class (Applicative m, Monad m) => MonadSample t m | m -> t where
124+ -- | Get the current value in the Behavior
125+ sample :: Behavior t a -> m a
126+
127+class MonadSample t m => MonadHold t m where
128+ -- | Create a new Behavior whose value will initially be equal to the given value and will be updated whenever the given Event occurs. The update takes effect immediately after the Event occurs; if the occurrence that sets the Behavior (or one that is simultaneous with it) is used to sample the Behavior, it will see the *old* value of the Behavior, not the new one.
129+ hold :: a -> Event t a -> m (Behavior t a)
130+ -- | Create a 'Dynamic' value using the given initial value that changes every
131+ -- time the 'Event' occurs.
132+ holdDyn :: a -> Event t a -> m (Dynamic t a)
133+ -- | Create an 'Incremental' value using the given initial value that changes every
134+ -- time the 'Event' occurs.
135+ holdIncremental :: Patch p => a -> Event t (p a) -> m (Incremental t p a)
136+
137+newtype EventSelector t k = EventSelector { select :: forall a. k a -> Event t a }
138+
139+--------------------------------------------------------------------------------
140+-- Instances
141+--------------------------------------------------------------------------------
142+
143+instance MonadSample t m => MonadSample t (ReaderT r m) where
144+ sample = lift . sample
145+
146+instance MonadHold t m => MonadHold t (ReaderT r m) where
147+ hold a0 = lift . hold a0
148+ holdDyn a0 = lift . holdDyn a0
149+ holdIncremental a0 = lift . holdIncremental a0
150+
151+instance (MonadSample t m, Monoid r) => MonadSample t (WriterT r m) where
152+ sample = lift . sample
153+
154+instance (MonadHold t m, Monoid r) => MonadHold t (WriterT r m) where
155+ hold a0 = lift . hold a0
156+ holdDyn a0 = lift . holdDyn a0
157+ holdIncremental a0 = lift . holdIncremental a0
158+
159+instance MonadSample t m => MonadSample t (StateT s m) where
160+ sample = lift . sample
161+
162+instance MonadHold t m => MonadHold t (StateT s m) where
163+ hold a0 = lift . hold a0
164+ holdDyn a0 = lift . holdDyn a0
165+ holdIncremental a0 = lift . holdIncremental a0
166+
167+instance MonadSample t m => MonadSample t (ExceptT e m) where
168+ sample = lift . sample
169+
170+instance MonadHold t m => MonadHold t (ExceptT e m) where
171+ hold a0 = lift . hold a0
172+ holdDyn a0 = lift . holdDyn a0
173+ holdIncremental a0 = lift . holdIncremental a0
174+
175+instance (MonadSample t m, Monoid w) => MonadSample t (RWST r w s m) where
176+ sample = lift . sample
177+
178+instance (MonadHold t m, Monoid w) => MonadHold t (RWST r w s m) where
179+ hold a0 = lift . hold a0
180+ holdDyn a0 = lift . holdDyn a0
181+ holdIncremental a0 = lift . holdIncremental a0
182+
183+instance MonadSample t m => MonadSample t (ContT r m) where
184+ sample = lift . sample
185+
186+instance MonadHold t m => MonadHold t (ContT r m) where
187+ hold a0 = lift . hold a0
188+ holdDyn a0 = lift . holdDyn a0
189+ holdIncremental a0 = lift . holdIncremental a0
190+
191+--------------------------------------------------------------------------------
192+-- Convenience functions
193+--------------------------------------------------------------------------------
194+
195+-- | Create an Event from another Event.
196+-- The provided function can sample 'Behavior's and hold 'Event's.
197+pushAlways :: Reflex t => (a -> PushM t b) -> Event t a -> Event t b
198+pushAlways f = push (liftM Just . f)
199+
200+-- | Flipped version of 'fmap'.
201+ffor :: Functor f => f a -> (a -> b) -> f b
202+ffor = flip fmap
203+
204+instance Reflex t => Functor (Behavior t) where
205+ fmap f = pull . liftM f . sample
206+
207+instance Reflex t => Applicative (Behavior t) where
208+ pure = constant
209+ f <*> x = pull $ sample f `ap` sample x
210+ _ *> b = b
211+ a <* _ = a
212+
213+instance Reflex t => Monad (Behavior t) where
214+ a >>= f = pull $ sample a >>= sample . f
215+ -- Note: it is tempting to write (_ >> b = b); however, this would result in (fail x >> return y) succeeding (returning y), which violates the law that (a >> b = a >>= \_ -> b), since the implementation of (>>=) above actually will fail. Since we can't examine Behaviors other than by using sample, I don't think it's possible to write (>>) to be more efficient than the (>>=) above.
216+ return = constant
217+ fail = error "Monad (Behavior t) does not support fail"
218+
219+instance (Reflex t, Semigroup a) => Semigroup (Behavior t a) where
220+ a <> b = pull $ liftM2 (<>) (sample a) (sample b)
221+ sconcat = pull . liftM sconcat . mapM sample
222+#if MIN_VERSION_semigroups(0,17,0)
223+ stimes n = fmap $ stimes n
224+#else
225+ times1p n = fmap $ times1p n
226+#endif
227+
228+instance (Reflex t, Monoid a) => Monoid (Behavior t a) where
229+ mempty = constant mempty
230+ mappend a b = pull $ liftM2 mappend (sample a) (sample b)
231+ mconcat = pull . liftM mconcat . mapM sample
232+
233+--TODO: See if there's a better class in the standard libraries already
234+-- | A class for values that combines filtering and mapping using 'Maybe'.
235+class FunctorMaybe f where
236+ -- | Combined mapping and filtering function.
237+ fmapMaybe :: (a -> Maybe b) -> f a -> f b
238+
239+instance FunctorMaybe [] where
240+ fmapMaybe f = catMaybes . fmap f
241+
242+-- | Flipped version of 'fmapMaybe'.
243+fforMaybe :: FunctorMaybe f => f a -> (a -> Maybe b) -> f b
244+fforMaybe = flip fmapMaybe
245+
246+-- | Filter 'f a' using the provided predicate.
247+-- Relies on 'fforMaybe'.
248+ffilter :: FunctorMaybe f => (a -> Bool) -> f a -> f a
249+ffilter f = fmapMaybe $ \x -> if f x then Just x else Nothing
250+
251+instance Reflex t => FunctorMaybe (Event t) where
252+ fmapMaybe f = push $ return . f
253+
254+instance Reflex t => Functor (Event t) where
255+ fmap f = fmapMaybe $ Just . f
256+
257+-- | Create a new 'Event' by combining each occurence with the next value
258+-- of the list using the supplied function. If the list runs out of items,
259+-- all subsequent 'Event' occurrences will be ignored.
260+zipListWithEvent :: (Reflex t, MonadHold t m, MonadFix m) => (a -> b -> c) -> [a] -> Event t b -> m (Event t c)
261+zipListWithEvent f l e = do
262+ rec lb <- hold l eTail
263+ let eBoth = flip push e $ \o -> do
264+ l' <- sample lb
265+ return $ case l' of
266+ (h : t) -> Just (f h o, t)
267+ [] -> Nothing
268+ let eTail = fmap snd eBoth
269+ lb `seq` eBoth `seq` eTail `seq` return ()
270+ return $ fmap fst eBoth
271+
272+-- | Replace each occurrence value of the 'Event' with the value of the
273+-- 'Behavior' at the time of that occurrence.
274+tag :: Reflex t => Behavior t b -> Event t a -> Event t b
275+tag b = pushAlways $ \_ -> sample b
276+
277+-- | Create a new 'Event' that combines occurences of supplied 'Event'
278+-- with the current value of the 'Behavior'.
279+attach :: Reflex t => Behavior t a -> Event t b -> Event t (a, b)
280+attach = attachWith (,)
281+
282+-- | Create a new 'Event' that occurs when the supplied 'Event' occurs
283+-- by combining it with the current value of the 'Behavior'.
284+attachWith :: Reflex t => (a -> b -> c) -> Behavior t a -> Event t b -> Event t c
285+attachWith f = attachWithMaybe $ \a b -> Just $ f a b
286+
287+-- | Create a new 'Event' by combining each occurence with the current
288+-- value of the 'Behavior'. The occurrence is discarded if the combining function
289+-- returns Nothing
290+attachWithMaybe :: Reflex t => (a -> b -> Maybe c) -> Behavior t a -> Event t b -> Event t c
291+attachWithMaybe f b e = flip push e $ \o -> liftM (flip f o) $ sample b
292+
293+-- | Alias for 'headE'
294+onceE :: (Reflex t, MonadHold t m, MonadFix m) => Event t a -> m (Event t a)
295+onceE = headE
296+
297+-- | Create a new 'Event' that only occurs on the first occurence of
298+-- the supplied 'Event'.
299+headE :: (Reflex t, MonadHold t m, MonadFix m) => Event t a -> m (Event t a)
300+headE e = do
301+ rec be <- hold e $ fmap (const never) e'
302+ let e' = switch be
303+ e' `seq` return ()
304+ return e'
305+
306+-- | Create a new 'Event' that occurs on all but the first occurence
307+-- of the supplied 'Event'.
308+tailE :: (Reflex t, MonadHold t m, MonadFix m) => Event t a -> m (Event t a)
309+tailE e = liftM snd $ headTailE e
310+
311+-- | Create a tuple of two 'Event's with the first one occuring only
312+-- the first time the supplied 'Event' occurs and the second occuring
313+-- on all but the first occurence.
314+headTailE :: (Reflex t, MonadHold t m, MonadFix m) => Event t a -> m (Event t a, Event t a)
315+headTailE e = do
316+ eHead <- headE e
317+ be <- hold never $ fmap (const e) eHead
318+ return (eHead, switch be)
319+
320+-- | Split the supplied 'Event' into two individual 'Event's occuring
321+-- at the same time with the respective values from the tuple.
322+splitE :: Reflex t => Event t (a, b) -> (Event t a, Event t b)
323+splitE e = (fmap fst e, fmap snd e)
324+
325+-- | Print the supplied 'String' and the value of the 'Event' on each
326+-- occurence. This should /only/ be used for debugging.
327+--
328+-- Note: As with Debug.Trace.trace, the message will only be printed if
329+-- the 'Event' is actually used.
330+traceEvent :: (Reflex t, Show a) => String -> Event t a -> Event t a
331+traceEvent s = traceEventWith $ \x -> s <> ": " <> show x
332+
333+-- | Print the output of the supplied function on each occurence of
334+-- the 'Event'. This should /only/ be used for debugging.
335+--
336+-- Note: As with Debug.Trace.trace, the message will only be printed if
337+-- the 'Event' is actually used.
338+traceEventWith :: Reflex t => (a -> String) -> Event t a -> Event t a
339+traceEventWith f = push $ \x -> trace (f x) $ return $ Just x
340+
341+-- | Tag type for 'Either' to use it as a 'DSum'.
342+data EitherTag l r a where
343+ LeftTag :: EitherTag l r l
344+ RightTag :: EitherTag l r r
345+
346+instance GEq (EitherTag l r) where
347+ geq a b = case (a, b) of
348+ (LeftTag, LeftTag) -> Just Refl
349+ (RightTag, RightTag) -> Just Refl
350+ _ -> Nothing
351+
352+instance GCompare (EitherTag l r) where
353+ gcompare a b = case (a, b) of
354+ (LeftTag, LeftTag) -> GEQ
355+ (LeftTag, RightTag) -> GLT
356+ (RightTag, LeftTag) -> GGT
357+ (RightTag, RightTag) -> GEQ
358+
359+instance GShow (EitherTag l r) where
360+ gshowsPrec _ a = case a of
361+ LeftTag -> showString "LeftTag"
362+ RightTag -> showString "RightTag"
363+
364+instance (Show l, Show r) => ShowTag (EitherTag l r) Identity where
365+ showTaggedPrec t n (Identity a) = case t of
366+ LeftTag -> showsPrec n a
367+ RightTag -> showsPrec n a
368+
369+-- | Convert 'Either' to a 'DSum'. Inverse of 'dsumToEither'.
370+eitherToDSum :: Either a b -> DSum (EitherTag a b) Identity
371+eitherToDSum = \case
372+ Left a -> (LeftTag :=> Identity a)
373+ Right b -> (RightTag :=> Identity b)
374+
375+-- | Convert 'DSum' to 'Either'. Inverse of 'eitherToDSum'.
376+dsumToEither :: DSum (EitherTag a b) Identity -> Either a b
377+dsumToEither = \case
378+ (LeftTag :=> Identity a) -> Left a
379+ (RightTag :=> Identity b) -> Right b
380+
381+-- | Extract the values of a 'DMap' of 'EitherTag's.
382+dmapToThese :: DMap (EitherTag a b) Identity -> Maybe (These a b)
383+dmapToThese m = case (DMap.lookup LeftTag m, DMap.lookup RightTag m) of
384+ (Nothing, Nothing) -> Nothing
385+ (Just (Identity a), Nothing) -> Just $ This a
386+ (Nothing, Just (Identity b)) -> Just $ That b
387+ (Just (Identity a), Just (Identity b)) -> Just $ These a b
388+
389+-- | Create a new 'Event' that occurs if at least one of the supplied
390+-- 'Event's occurs. If both occur at the same time they are combined
391+-- using 'mappend'.
392+appendEvents :: (Reflex t, Monoid a) => Event t a -> Event t a -> Event t a
393+appendEvents e1 e2 = mergeThese mappend <$> align e1 e2
394+
395+{-# DEPRECATED sequenceThese "Use bisequenceA or bisequence from the bifunctors package instead" #-}
396+sequenceThese :: Monad m => These (m a) (m b) -> m (These a b)
397+sequenceThese t = case t of
398+ This ma -> liftM This ma
399+ These ma mb -> liftM2 These ma mb
400+ That mb -> liftM That mb
401+
402+instance (Semigroup a, Reflex t) => Semigroup (Event t a) where
403+ (<>) = alignWith (mergeThese (<>))
404+ sconcat = fmap sconcat . mergeList . toList
405+#if MIN_VERSION_semigroups(0,17,0)
406+ stimes n = fmap $ stimes n
407+#else
408+ times1p n = fmap $ times1p n
409+#endif
410+
411+instance (Semigroup a, Reflex t) => Monoid (Event t a) where
412+ mempty = never
413+ mappend = (<>)
414+ mconcat = fmap sconcat . mergeList
415+
416+-- | Create a new 'Event' that occurs if at least one of the 'Event's
417+-- in the list occurs. If multiple occur at the same time they are
418+-- folded from the left with the given function.
419+mergeWith :: Reflex t => (a -> a -> a) -> [Event t a] -> Event t a
420+mergeWith f es = fmap (Prelude.foldl1 f . map (\(Const2 _ :=> Identity v) -> v) . DMap.toList)
421+ . merge
422+ . DMap.fromDistinctAscList
423+ . map (\(k, v) -> (Const2 k) :=> v)
424+ $ zip [0 :: Int ..] es
425+
426+-- | Create a new 'Event' that occurs if at least one of the 'Event's
427+-- in the list occurs. If multiple occur at the same time the value is
428+-- the value of the leftmost event.
429+leftmost :: Reflex t => [Event t a] -> Event t a
430+leftmost = mergeWith const
431+
432+-- | Create a new 'Event' that occurs if at least one of the 'Event's
433+-- in the list occurs and has a list of the values of all 'Event's
434+-- occuring at that time.
435+mergeList :: Reflex t => [Event t a] -> Event t (NonEmpty a)
436+mergeList [] = never
437+mergeList es = mergeWith (<>) $ map (fmap (:|[])) es
438+
439+-- | Create a new 'Event' combining the map of 'Event's into an
440+-- 'Event' that occurs if at least one of them occurs and has a map of
441+-- values of all 'Event's occuring at that time.
442+mergeMap :: (Reflex t, Ord k) => Map k (Event t a) -> Event t (Map k a)
443+mergeMap = fmap dmapToMap . merge . mapWithFunctorToDMap
444+
445+-- | Split the event into an 'EventSelector' that allows efficient
446+-- selection of the individual 'Event's.
447+fanMap :: (Reflex t, Ord k) => Event t (Map k a) -> EventSelector t (Const2 k a)
448+fanMap = fan . fmap mapToDMap
449+
450+-- | Switches to the new event whenever it receives one; the new event is used immediately, on the same frame that it is switched to
451+switchPromptly :: forall t m a. (Reflex t, MonadHold t m) => Event t a -> Event t (Event t a) -> m (Event t a)
452+switchPromptly ea0 eea = do
453+ bea <- hold ea0 eea
454+ let eLag = switch bea
455+ eCoincidences = coincidence eea
456+ return $ leftmost [eCoincidences, eLag]
457+
458+instance Reflex t => Align (Event t) where
459+ nil = never
460+ align ea eb = fmapMaybe dmapToThese $ merge $ DMap.fromList [LeftTag :=> ea, RightTag :=> eb]
461+
462+-- | Create a new 'Event' that only occurs if the supplied 'Event'
463+-- occurs and the 'Behavior' is true at the time of occurence.
464+gate :: Reflex t => Behavior t Bool -> Event t a -> Event t a
465+gate = attachWithMaybe $ \allow a -> if allow then Just a else Nothing
466+
467+-- | Create a new behavior given a starting behavior and switch to a the
468+-- behvior carried by the event when it fires.
469+switcher :: (Reflex t, MonadHold t m)
470+ => Behavior t a -> Event t (Behavior t a) -> m (Behavior t a)
471+switcher b eb = pull . (sample <=< sample) <$> hold b eb
472+
473+zipDynWith :: Reflex t => (a -> b -> c) -> Dynamic t a -> Dynamic t b -> Dynamic t c
474+zipDynWith f da db =
475+ let eab = align (updated da) (updated db)
476+ ec = flip push eab $ \o -> do
477+ (a, b) <- case o of
478+ This a -> do
479+ b <- sample $ current db
480+ return (a, b)
481+ That b -> do
482+ a <- sample $ current da
483+ return (a, b)
484+ These a b -> return (a, b)
485+ return $ Just $ f a b
486+ in unsafeBuildDynamic (f <$> sample (current da) <*> sample (current db)) ec
487+
488+instance (Reflex t, Monoid a) => Monoid (Dynamic t a) where
489+ mconcat = fmap (mconcat . map (\(Const2 _ :=> Identity v) -> v) . DMap.toList) . distributeDMapOverDynPure . DMap.fromList . map (\(k, v) -> Const2 k :=> v) . zip [0 :: Int ..]
490+ mempty = constDyn mempty
491+ mappend = zipDynWith mappend
492+
493+distributeDMapOverDynPure :: forall t k. (Reflex t, GCompare k) => DMap k (Dynamic t) -> Dynamic t (DMap k Identity)
494+distributeDMapOverDynPure dm = case DMap.toList dm of
495+ [] -> constDyn DMap.empty
496+ [k :=> v] -> fmap (DMap.singleton k . Identity) v
497+ _ ->
498+ let getInitial = DMap.traverseWithKey (\_ -> fmap Identity . sample . current) dm
499+ edmPre = merge $ rewrapDMap updated dm
500+ result = unsafeBuildDynamic getInitial $ flip pushAlways edmPre $ \news -> do
501+ olds <- sample $ current result
502+ return $ DMap.unionWithKey (\_ _ new -> new) olds news
503+ in result
src/Reflex/Dynamic.hsnew file+457
@@ -0,0 +1,457 @@
1+{-# LANGUAGE CPP, TypeFamilies, FlexibleContexts, FlexibleInstances, MultiParamTypeClasses, RankNTypes, GADTs, ScopedTypeVariables, FunctionalDependencies, RecursiveDo, UndecidableInstances, GeneralizedNewtypeDeriving, StandaloneDeriving, EmptyDataDecls, NoMonomorphismRestriction, TypeOperators, DeriveDataTypeable, PackageImports, TemplateHaskell, LambdaCase, DataKinds, PolyKinds #-}
2+module Reflex.Dynamic ( Dynamic -- Abstract so we can preserve the law that the current value is always equal to the most recent update
3+ , current
4+ , updated
5+ , constDyn
6+ , holdDyn
7+ , nubDyn
8+ , count
9+ , toggle
10+ , switchPromptlyDyn
11+ , tagDyn
12+ , attachDyn
13+ , attachDynWith
14+ , attachDynWithMaybe
15+ , mapDyn
16+ , forDyn
17+ , foldDyn
18+ , foldDynM
19+ , foldDynMaybe
20+ , foldDynMaybeM
21+ , combineDyn
22+ , apDyn
23+ , collectDyn
24+ , mconcatDyn
25+ , distributeDMapOverDyn
26+ , joinDyn
27+ , joinDynThroughMap
28+ , traceDyn
29+ , traceDynWith
30+ , splitDyn
31+ , splitDynPure
32+ , Demux
33+ , demux
34+ , getDemuxed
35+ -- Things that probably aren't very useful:
36+ , HList (..)
37+ , FHList (..)
38+ , distributeFHListOverDyn
39+ -- Unsafe
40+ , unsafeDynamic
41+ ) where
42+
43+import Prelude hiding (mapM, mapM_)
44+
45+import Reflex.Class
46+import Data.Functor.Misc
47+
48+import Control.Monad hiding (mapM, mapM_, forM, forM_)
49+import Control.Monad.Fix
50+import Control.Monad.Identity hiding (mapM, mapM_, forM, forM_)
51+import Data.These
52+import Data.Align
53+import Data.Map (Map)
54+import Data.Dependent.Map (DMap)
55+import qualified Data.Dependent.Map as DMap
56+import Data.Dependent.Sum (DSum (..))
57+import Data.GADT.Compare (GCompare (..), GEq (..), (:~:) (..), GOrdering (..))
58+import Data.Monoid
59+
60+import Debug.Trace
61+
62+data HList (l::[*]) where
63+ HNil :: HList '[]
64+ HCons :: e -> HList l -> HList (e ': l)
65+
66+infixr 2 `HCons`
67+
68+type family HRevApp (l1 :: [k]) (l2 :: [k]) :: [k]
69+type instance HRevApp '[] l = l
70+type instance HRevApp (e ': l) l' = HRevApp l (e ': l')
71+
72+hRevApp :: HList l1 -> HList l2 -> HList (HRevApp l1 l2)
73+hRevApp HNil l = l
74+hRevApp (HCons x l) l' = hRevApp l (HCons x l')
75+
76+hReverse :: HList l -> HList (HRevApp l '[])
77+hReverse l = hRevApp l HNil
78+
79+hBuild :: (HBuild' '[] r) => r
80+hBuild = hBuild' HNil
81+
82+class HBuild' l r where
83+ hBuild' :: HList l -> r
84+
85+instance (l' ~ HRevApp l '[])
86+ => HBuild' l (HList l') where
87+ hBuild' l = hReverse l
88+
89+instance HBuild' (a ': l) r
90+ => HBuild' l (a->r) where
91+ hBuild' l x = hBuild' (HCons x l)
92+
93+-- | Create a new 'Dynamic' that only signals changes if the values
94+-- actually changed.
95+nubDyn :: (Reflex t, Eq a) => Dynamic t a -> Dynamic t a
96+nubDyn d =
97+ let e' = attachWithMaybe (\x x' -> if x' == x then Nothing else Just x') (current d) (updated d)
98+ in unsafeDynamic (current d) e' --TODO: Avoid invalidating the outgoing Behavior
99+
100+{-
101+instance Reflex t => Functor (Dynamic t) where
102+ fmap f d =
103+ let e' = fmap f $ updated d
104+ eb' = push (\b' -> liftM Just $ constant b') e'
105+ b0 = fmap f $ current d
106+
107+-}
108+
109+-- | Map a function over a 'Dynamic'.
110+mapDyn :: (Reflex t, Monad m) => (a -> b) -> Dynamic t a -> m (Dynamic t b)
111+mapDyn f = return . fmap f
112+
113+-- | Flipped version of 'mapDyn'.
114+forDyn :: (Reflex t, Monad m) => Dynamic t a -> (a -> b) -> m (Dynamic t b)
115+forDyn = flip mapDyn
116+
117+-- | Create a 'Dynamic' using the initial value and change it each
118+-- time the 'Event' occurs using a folding function on the previous
119+-- value and the value of the 'Event'.
120+foldDyn :: (Reflex t, MonadHold t m, MonadFix m) => (a -> b -> b) -> b -> Event t a -> m (Dynamic t b)
121+foldDyn f = foldDynMaybe $ \o v -> Just $ f o v
122+
123+-- | Create a 'Dynamic' using the initial value and change it each
124+-- time the 'Event' occurs using a monadic folding function on the
125+-- previous value and the value of the 'Event'.
126+foldDynM :: (Reflex t, MonadHold t m, MonadFix m) => (a -> b -> PushM t b) -> b -> Event t a -> m (Dynamic t b)
127+foldDynM f = foldDynMaybeM $ \o v -> liftM Just $ f o v
128+
129+foldDynMaybe :: (Reflex t, MonadHold t m, MonadFix m) => (a -> b -> Maybe b) -> b -> Event t a -> m (Dynamic t b)
130+foldDynMaybe f = foldDynMaybeM $ \o v -> return $ f o v
131+
132+foldDynMaybeM :: (Reflex t, MonadHold t m, MonadFix m) => (a -> b -> PushM t (Maybe b)) -> b -> Event t a -> m (Dynamic t b)
133+foldDynMaybeM f z e = do
134+ rec let e' = flip push e $ \o -> do
135+ v <- sample $ current d'
136+ f o v
137+ d' <- holdDyn z e'
138+ return d'
139+
140+-- | Create a new 'Dynamic' that counts the occurences of the 'Event'.
141+count :: (Reflex t, MonadHold t m, MonadFix m, Num b) => Event t a -> m (Dynamic t b)
142+count e = holdDyn 0 =<< zipListWithEvent const (iterate (+1) 1) e
143+
144+-- | Create a new 'Dynamic' using the initial value that flips its
145+-- value every time the 'Event' occurs.
146+toggle :: (Reflex t, MonadHold t m, MonadFix m) => Bool -> Event t a -> m (Dynamic t Bool)
147+toggle = foldDyn (const not)
148+
149+-- | Switches to the new 'Event' whenever it receives one. Switching
150+-- occurs *before* the inner 'Event' fires - so if the 'Dynamic' changes and both the old and new
151+-- inner Events fire simultaneously, the output will fire with the value of the *new* 'Event'.
152+switchPromptlyDyn :: forall t a. Reflex t => Dynamic t (Event t a) -> Event t a
153+switchPromptlyDyn de =
154+ let eLag = switch $ current de
155+ eCoincidences = coincidence $ updated de
156+ in leftmost [eCoincidences, eLag]
157+
158+{-
159+
160+mergeEventsWith :: Reflex t m => (a -> a -> a) -> Event t a -> Event t a -> m (Event t a)
161+mergeEventsWith f ea eb = mapE (mergeThese f) =<< alignEvents ea eb
162+
163+firstE :: (Reflex t m) => [Event t a] -> m (Event t a)
164+firstE [] = return never
165+firstE (h:t) = mergeEventsLeftBiased h =<< firstE t
166+
167+concatEventsWith :: (Reflex t m) => (a -> a -> a) -> [Event t a] -> m (Event t a)
168+concatEventsWith _ [] = return never
169+concatEventsWith _ [e] = return e
170+concatEventsWith f es = mapEM (liftM (foldl1 f . map (\(Const2 _ :=> v) -> v) . DMap.toList) . sequenceDmap) <=< mergeEventDMap $ DMap.fromList $ map (\(k, v) -> WrapArg (Const2 k) :=> v) $ zip [0 :: Int ..] es
171+--concatEventsWith f (h:t) = mergeEventsWith f h =<< concatEventsWith f t
172+
173+mconcatE :: (Reflex t m, Monoid a) => [Event t a] -> m (Event t a)
174+mconcatE = concatEventsWith mappend
175+-}
176+
177+-- | Split the 'Dynamic' into two 'Dynamic's, each taking the
178+-- respective value of the tuple.
179+splitDyn :: (Reflex t, Monad m) => Dynamic t (a, b) -> m (Dynamic t a, Dynamic t b)
180+splitDyn = return . splitDynPure
181+
182+splitDynPure :: Reflex t => Dynamic t (a, b) -> (Dynamic t a, Dynamic t b)
183+splitDynPure d = (fmap fst d, fmap snd d)
184+
185+-- | Merge the 'Dynamic' values using their 'Monoid' instance.
186+mconcatDyn :: forall t m a. (Reflex t, Monad m, Monoid a) => [Dynamic t a] -> m (Dynamic t a)
187+mconcatDyn = return . mconcat
188+
189+-- | Create a 'Dynamic' with a 'DMap' of values out of a 'DMap' of
190+-- Dynamic values.
191+distributeDMapOverDyn :: (Reflex t, Monad m, GCompare k) => DMap k (Dynamic t) -> m (Dynamic t (DMap k Identity))
192+distributeDMapOverDyn = return . distributeDMapOverDynPure
193+
194+distributeMapOverDynPure :: (Reflex t, Ord k) => Map k (Dynamic t v) -> Dynamic t (Map k v)
195+distributeMapOverDynPure = fmap dmapToMap . distributeDMapOverDynPure . mapWithFunctorToDMap
196+
197+-- | Merge two 'Dynamic's into a new one using the provided
198+-- function. The new 'Dynamic' changes its value each time one of the
199+-- original 'Dynamic's changes its value.
200+combineDyn :: forall t m a b c. (Reflex t, Monad m) => (a -> b -> c) -> Dynamic t a -> Dynamic t b -> m (Dynamic t c)
201+combineDyn f da db = return $ zipDynWith f da db
202+
203+-- | A psuedo applicative version of ap for 'Dynamic'. Example useage:
204+--
205+-- > do
206+-- > person <- Person `mapDyn` dynFirstName
207+-- > `apDyn` dynListName
208+-- > `apDyn` dynAge
209+-- > `apDyn` dynAddress
210+apDyn :: forall t m a b. (Reflex t, Monad m)
211+ => m (Dynamic t (a -> b))
212+ -> Dynamic t a
213+ -> m (Dynamic t b)
214+apDyn m a = do
215+ r <- m
216+ return $ r <*> a
217+
218+joinDyn :: Reflex t => Dynamic t (Dynamic t a) -> Dynamic t a
219+joinDyn = join
220+
221+--TODO: Generalize this to functors other than Maps
222+-- | Combine a 'Dynamic' of a 'Map' of 'Dynamic's into a 'Dynamic'
223+-- with the current values of the 'Dynamic's in a map.
224+joinDynThroughMap :: forall t k a. (Reflex t, Ord k) => Dynamic t (Map k (Dynamic t a)) -> Dynamic t (Map k a)
225+joinDynThroughMap = joinDyn . fmap distributeMapOverDynPure
226+
227+-- | Print the value of the 'Dynamic' on each change and prefix it
228+-- with the provided string. This should /only/ be used for debugging.
229+--
230+-- Note: Just like Debug.Trace.trace, the value will only be shown if something
231+-- else in the system is depending on it.
232+traceDyn :: (Reflex t, Show a) => String -> Dynamic t a -> Dynamic t a
233+traceDyn s = traceDynWith $ \x -> s <> ": " <> show x
234+
235+-- | Print the result of applying the provided function to the value
236+-- of the 'Dynamic' on each change. This should /only/ be used for
237+-- debugging.
238+--
239+-- Note: Just like Debug.Trace.trace, the value will only be shown if something
240+-- else in the system is depending on it.
241+traceDynWith :: Reflex t => (a -> String) -> Dynamic t a -> Dynamic t a
242+traceDynWith f d =
243+ let e' = traceEventWith f $ updated d
244+ getV0 = do
245+ x <- sample $ current d
246+ trace (f x) $ return x
247+ in unsafeBuildDynamic getV0 e'
248+
249+-- | Replace the value of the 'Event' with the current value of the 'Dynamic'
250+-- each time the 'Event' occurs.
251+--
252+-- Note: `tagDyn d e` differs from `tag (current d) e` in the case that `e` is firing
253+-- at the same time that `d` is changing. With `tagDyn d e`, the *new* value of `d`
254+-- will replace the value of `e`, whereas with `tag (current d) e`, the *old* value
255+-- will be used, since the 'Behavior' won't be updated until the end of the frame.
256+-- Additionally, this means that the output 'Event' may not be used to directly change
257+-- the input 'Dynamic', because that would mean its value depends on itself. When creating
258+-- cyclic data flows, generally `tag (current d) e` is preferred.
259+tagDyn :: Reflex t => Dynamic t a -> Event t b -> Event t a
260+tagDyn = attachDynWith const
261+
262+-- | Attach the current value of the 'Dynamic' to the value of the
263+-- 'Event' each time it occurs.
264+--
265+-- Note: `attachDyn d` is not the same as `attach (current d)`. See 'tagDyn' for details.
266+attachDyn :: Reflex t => Dynamic t a -> Event t b -> Event t (a, b)
267+attachDyn = attachDynWith (,)
268+
269+-- | Combine the current value of the 'Dynamic' with the value of the
270+-- 'Event' each time it occurs.
271+--
272+-- Note: `attachDynWith f d` is not the same as `attachWith f (current d)`. See 'tagDyn' for details.
273+attachDynWith :: Reflex t => (a -> b -> c) -> Dynamic t a -> Event t b -> Event t c
274+attachDynWith f = attachDynWithMaybe $ \a b -> Just $ f a b
275+
276+-- | Create a new 'Event' by combining the value at each occurence
277+-- with the current value of the 'Dynamic' value and possibly
278+-- filtering if the combining function returns 'Nothing'.
279+--
280+-- Note: `attachDynWithMaybe f d` is not the same as `attachWithMaybe f (current d)`. See 'tagDyn' for details.
281+attachDynWithMaybe :: Reflex t => (a -> b -> Maybe c) -> Dynamic t a -> Event t b -> Event t c
282+attachDynWithMaybe f d e =
283+ let e' = attach (current d) e
284+ in fforMaybe (align e' $ updated d) $ \case
285+ This (a, b) -> f a b -- Only the tagging event is firing, so use that
286+ These (_, b) a -> f a b -- Both events are firing, so use the newer value
287+ That _ -> Nothing -- The tagging event isn't firing, so don't fire
288+
289+--------------------------------------------------------------------------------
290+-- Demux
291+--------------------------------------------------------------------------------
292+
293+-- | Represents a time changing value together with an 'EventSelector'
294+-- that can efficiently detect when the underlying Dynamic has a particular value.
295+-- This is useful for representing data like the current selection of a long list.
296+--
297+-- Semantically,
298+-- > getDemuxed (demux d) k === mapDyn (== k) d
299+-- However, the when getDemuxed is used multiple times, the complexity is only /O(log(n))/,
300+-- rather than /O(n)/ for mapDyn.
301+data Demux t k = Demux { demuxValue :: Behavior t k
302+ , demuxSelector :: EventSelector t (Const2 k Bool)
303+ }
304+
305+-- | Demultiplex an input value to a 'Demux' with many outputs. At any given time, whichever output is indicated by the given 'Dynamic' will be 'True'.
306+demux :: (Reflex t, Ord k) => Dynamic t k -> Demux t k
307+demux k = Demux (current k)
308+ (fan $ attachWith (\k0 k1 -> if k0 == k1
309+ then DMap.empty
310+ else DMap.fromList [Const2 k0 :=> Identity False,
311+ Const2 k1 :=> Identity True])
312+ (current k) (updated k))
313+
314+--TODO: The pattern of using hold (sample b0) can be reused in various places as a safe way of building certain kinds of Dynamics; see if we can factor this out
315+-- | Select a particular output of the 'Demux'; this is equivalent to (but much faster than)
316+-- mapping over the original 'Dynamic' and checking whether it is equal to the given key.
317+getDemuxed :: (Reflex t, Monad m, Eq k) => Demux t k -> k -> m (Dynamic t Bool)
318+getDemuxed d k = return $ demuxed d k
319+
320+demuxed :: (Reflex t, Eq k) => Demux t k -> k -> Dynamic t Bool
321+demuxed d k =
322+ let e = select (demuxSelector d) (Const2 k)
323+ in unsafeBuildDynamic (liftM (==k) $ sample $ demuxValue d) e
324+
325+--------------------------------------------------------------------------------
326+-- collectDyn
327+--------------------------------------------------------------------------------
328+
329+--TODO: This whole section is badly in need of cleanup
330+
331+data FHList f l where
332+ FHNil :: FHList f '[]
333+ FHCons :: f e -> FHList f l -> FHList f (e ': l)
334+
335+instance GEq (HListPtr l) where
336+ HHeadPtr `geq` HHeadPtr = Just Refl
337+ HHeadPtr `geq` HTailPtr _ = Nothing
338+ HTailPtr _ `geq` HHeadPtr = Nothing
339+ HTailPtr a `geq` HTailPtr b = a `geq` b
340+
341+instance GCompare (HListPtr l) where -- Warning: This ordering can't change, dmapTo*HList will break
342+ HHeadPtr `gcompare` HHeadPtr = GEQ
343+ HHeadPtr `gcompare` HTailPtr _ = GLT
344+ HTailPtr _ `gcompare` HHeadPtr = GGT
345+ HTailPtr a `gcompare` HTailPtr b = a `gcompare` b
346+
347+data HListPtr l a where
348+ HHeadPtr :: HListPtr (h ': t) h
349+ HTailPtr :: HListPtr t a -> HListPtr (h ': t) a
350+
351+fhlistToDMap :: forall (f :: * -> *) l. FHList f l -> DMap (HListPtr l) f
352+fhlistToDMap = DMap.fromList . go
353+ where go :: forall l'. FHList f l' -> [DSum (HListPtr l') f]
354+ go = \case
355+ FHNil -> []
356+ FHCons h t -> (HHeadPtr :=> h) : map (\(p :=> v) -> (HTailPtr p) :=> v) (go t)
357+
358+class RebuildSortedHList l where
359+ rebuildSortedFHList :: [DSum (HListPtr l) f] -> FHList f l
360+ rebuildSortedHList :: [DSum (HListPtr l) Identity] -> HList l
361+
362+instance RebuildSortedHList '[] where
363+ rebuildSortedFHList l = case l of
364+ [] -> FHNil
365+ _ : _ -> error "rebuildSortedFHList{'[]}: empty list expected"
366+ rebuildSortedHList l = case l of
367+ [] -> HNil
368+ _ : _ -> error "rebuildSortedHList{'[]}: empty list expected"
369+
370+instance RebuildSortedHList t => RebuildSortedHList (h ': t) where
371+ rebuildSortedFHList l = case l of
372+ ((HHeadPtr :=> h) : t) -> FHCons h . rebuildSortedFHList . map (\(HTailPtr p :=> v) -> p :=> v) $ t
373+ _ -> error "rebuildSortedFHList{h':t}: non-empty list with HHeadPtr expected"
374+ rebuildSortedHList l = case l of
375+ ((HHeadPtr :=> Identity h) : t) -> HCons h . rebuildSortedHList . map (\(HTailPtr p :=> v) -> p :=> v) $ t
376+ _ -> error "rebuildSortedHList{h':t}: non-empty list with HHeadPtr expected"
377+
378+dmapToHList :: forall l. RebuildSortedHList l => DMap (HListPtr l) Identity -> HList l
379+dmapToHList = rebuildSortedHList . DMap.toList
380+
381+distributeFHListOverDyn :: forall t m l. (Reflex t, Monad m, RebuildSortedHList l) => FHList (Dynamic t) l -> m (Dynamic t (HList l))
382+distributeFHListOverDyn = return . distributeFHListOverDynPure
383+
384+distributeFHListOverDynPure :: (Reflex t, RebuildSortedHList l) => FHList (Dynamic t) l -> Dynamic t (HList l)
385+distributeFHListOverDynPure l = fmap dmapToHList $ distributeDMapOverDynPure $ fhlistToDMap l
386+
387+class AllAreFunctors (f :: a -> *) (l :: [a]) where
388+ type FunctorList f l :: [*]
389+ toFHList :: HList (FunctorList f l) -> FHList f l
390+ fromFHList :: FHList f l -> HList (FunctorList f l)
391+
392+instance AllAreFunctors f '[] where
393+ type FunctorList f '[] = '[]
394+ toFHList l = case l of
395+ HNil -> FHNil
396+#if !defined(__GLASGOW_HASKELL__) || __GLASGOW_HASKELL__ < 800
397+ _ -> error "toFHList: impossible" -- Otherwise, GHC complains of a non-exhaustive pattern match; see https://ghc.haskell.org/trac/ghc/ticket/4139
398+#endif
399+ fromFHList FHNil = HNil
400+
401+instance AllAreFunctors f t => AllAreFunctors f (h ': t) where
402+ type FunctorList f (h ': t) = f h ': FunctorList f t
403+ toFHList l = case l of
404+ a `HCons` b -> a `FHCons` toFHList b
405+#if !defined(__GLASGOW_HASKELL__) || __GLASGOW_HASKELL__ < 800
406+ _ -> error "toFHList: impossible" -- Otherwise, GHC complains of a non-exhaustive pattern match; see https://ghc.haskell.org/trac/ghc/ticket/4139
407+#endif
408+ fromFHList (a `FHCons` b) = a `HCons` fromFHList b
409+
410+collectDyn :: ( RebuildSortedHList (HListElems b)
411+ , IsHList a, IsHList b
412+ , AllAreFunctors (Dynamic t) (HListElems b)
413+ , Reflex t, Monad m
414+ , HListElems a ~ FunctorList (Dynamic t) (HListElems b)
415+ ) => a -> m (Dynamic t b)
416+collectDyn = return . collectDynPure
417+
418+collectDynPure :: ( RebuildSortedHList (HListElems b)
419+ , IsHList a, IsHList b
420+ , AllAreFunctors (Dynamic t) (HListElems b)
421+ , Reflex t
422+ , HListElems a ~ FunctorList (Dynamic t) (HListElems b)
423+ ) => a -> Dynamic t b
424+collectDynPure ds = fmap fromHList $ distributeFHListOverDynPure $ toFHList $ toHList ds
425+
426+-- Poor man's Generic
427+class IsHList a where
428+ type HListElems a :: [*]
429+ toHList :: a -> HList (HListElems a)
430+ fromHList :: HList (HListElems a) -> a
431+
432+instance IsHList (a, b) where
433+ type HListElems (a, b) = [a, b]
434+ toHList (a, b) = hBuild a b
435+ fromHList l = case l of
436+ a `HCons` b `HCons` HNil -> (a, b)
437+#if !defined(__GLASGOW_HASKELL__) || __GLASGOW_HASKELL__ < 800
438+ _ -> error "fromHList: impossible" -- Otherwise, GHC complains of a non-exhaustive pattern match; see https://ghc.haskell.org/trac/ghc/ticket/4139
439+#endif
440+
441+instance IsHList (a, b, c, d) where
442+ type HListElems (a, b, c, d) = [a, b, c, d]
443+ toHList (a, b, c, d) = hBuild a b c d
444+ fromHList l = case l of
445+ a `HCons` b `HCons` c `HCons` d `HCons` HNil -> (a, b, c, d)
446+#if !defined(__GLASGOW_HASKELL__) || __GLASGOW_HASKELL__ < 800
447+ _ -> error "fromHList: impossible" -- Otherwise, GHC complains of a non-exhaustive pattern match; see https://ghc.haskell.org/trac/ghc/ticket/4139
448+#endif
449+
450+instance IsHList (a, b, c, d, e, f) where
451+ type HListElems (a, b, c, d, e, f) = [a, b, c, d, e, f]
452+ toHList (a, b, c, d, e, f) = hBuild a b c d e f
453+ fromHList l = case l of
454+ a `HCons` b `HCons` c `HCons` d `HCons` e `HCons` f `HCons` HNil -> (a, b, c, d, e, f)
455+#if !defined(__GLASGOW_HASKELL__) || __GLASGOW_HASKELL__ < 800
456+ _ -> error "fromHList: impossible" -- Otherwise, GHC complains of a non-exhaustive pattern match; see https://ghc.haskell.org/trac/ghc/ticket/4139
457+#endif
src/Reflex/Dynamic/TH.hsnew file+67
@@ -0,0 +1,67 @@
1+{-# LANGUAGE TemplateHaskell, ScopedTypeVariables, TypeOperators, GADTs, EmptyDataDecls, PatternGuards #-}
2+module Reflex.Dynamic.TH (qDyn, unqDyn, mkDyn) where
3+
4+import Reflex.Dynamic
5+
6+import Language.Haskell.TH
7+import qualified Language.Haskell.TH.Syntax as TH
8+import Language.Haskell.TH.Quote
9+import Data.Data
10+import Control.Monad.State
11+import qualified Language.Haskell.Exts as Hs
12+import qualified Language.Haskell.Meta.Syntax.Translate as Hs
13+import Data.Monoid
14+import Data.Generics
15+
16+-- | Quote a Dynamic expression. Within the quoted expression, you can use '$(unqDyn [| x |])' to refer to any expression 'x' of type 'Dynamic t a'; the unquoted result will be of type 'a'
17+qDyn :: Q Exp -> Q Exp
18+qDyn qe = do
19+ e <- qe
20+ let f :: forall d. Data d => d -> StateT [(Name, Exp)] Q d
21+ f d = case eqT of
22+ Just (Refl :: d :~: Exp)
23+ | AppE (VarE m) eInner <- d
24+ , m == 'unqMarker
25+ -> do n <- lift $ newName "dynamicQuotedExpressionVariable"
26+ modify ((n, eInner):)
27+ return $ VarE n
28+ _ -> gmapM f d
29+ (e', exprsReversed) <- runStateT (gmapM f e) []
30+ let exprs = reverse exprsReversed
31+ arg = foldr (\a b -> ConE 'FHCons `AppE` a `AppE` b) (ConE 'FHNil) $ map snd exprs
32+ param = foldr (\a b -> ConP 'HCons [VarP a, b]) (ConP 'HNil []) $ map fst exprs
33+ [| mapDyn $(return $ LamE [param] e') =<< distributeFHListOverDyn $(return arg) |]
34+
35+unqDyn :: Q Exp -> Q Exp
36+unqDyn e = [| unqMarker $e |]
37+
38+-- | This type represents an occurrence of unqDyn before it has been processed by qDyn. If you see it in a type error, it probably means that unqDyn has been used outside of a qDyn context.
39+data UnqDyn
40+
41+-- unqMarker must not be exported; it is used only as a way of smuggling data from unqDyn to qDyn
42+--TODO: It would be much nicer if the TH AST was extensible to support this kind of thing without trickery
43+unqMarker :: a -> UnqDyn
44+unqMarker = error "An unqDyn expression was used outside of a qDyn expression"
45+
46+mkDyn :: QuasiQuoter
47+mkDyn = QuasiQuoter
48+ { quoteExp = mkDynExp
49+ , quotePat = error "mkDyn: pattern splices are not supported"
50+ , quoteType = error "mkDyn: type splices are not supported"
51+ , quoteDec = error "mkDyn: declaration splices are not supported"
52+ }
53+
54+mkDynExp :: String -> Q Exp
55+mkDynExp s = case Hs.parseExpWithMode (Hs.defaultParseMode { Hs.extensions = [ Hs.EnableExtension Hs.TemplateHaskell ] }) s of
56+ Hs.ParseFailed (Hs.SrcLoc _ l c) err -> fail $ "mkDyn:" <> show l <> ":" <> show c <> ": " <> err
57+ Hs.ParseOk e -> qDyn $ return $ everywhere (id `extT` reinstateUnqDyn) $ Hs.toExp $ everywhere (id `extT` antiE) e
58+ where TH.Name (TH.OccName occName) (TH.NameG _ _ (TH.ModName modName)) = 'unqMarker
59+ antiE x = case x of
60+ Hs.SpliceExp se ->
61+ Hs.App (Hs.Var $ Hs.Qual (Hs.ModuleName modName) (Hs.Ident occName)) $ case se of
62+ Hs.IdSplice v -> Hs.Var $ Hs.UnQual $ Hs.Ident v
63+ Hs.ParenSplice ps -> ps
64+ _ -> x
65+ reinstateUnqDyn (TH.Name (TH.OccName occName') (TH.NameQ (TH.ModName modName')))
66+ | modName == modName' && occName == occName' = 'unqMarker
67+ reinstateUnqDyn x = x
src/Reflex/Host/Class.hsnew file+227
@@ -0,0 +1,227 @@
1+{-# LANGUAGE ExistentialQuantification, GADTs, ScopedTypeVariables, TypeFamilies, FlexibleInstances, MultiParamTypeClasses, GeneralizedNewtypeDeriving, RankNTypes, BangPatterns, UndecidableInstances, EmptyDataDecls, RecursiveDo, RoleAnnotations, FunctionalDependencies, FlexibleContexts #-}
2+module Reflex.Host.Class where
3+
4+import Reflex.Class
5+
6+import Control.Applicative
7+import Control.Monad
8+import Control.Monad.Fix
9+import Control.Monad.Identity
10+import Control.Monad.Trans
11+import Control.Monad.Trans.Reader (ReaderT())
12+import Control.Monad.Trans.Writer (WriterT())
13+import Control.Monad.Trans.Cont (ContT())
14+import Control.Monad.Trans.Except (ExceptT())
15+import Control.Monad.Trans.RWS (RWST())
16+import Control.Monad.Trans.State (StateT())
17+import qualified Control.Monad.Trans.State.Strict as Strict
18+import Data.Dependent.Sum (DSum (..))
19+import Data.Monoid
20+import Data.GADT.Compare
21+import Control.Monad.Ref
22+
23+-- Note: this import must come last to silence warnings from AMP
24+import Prelude hiding (mapM, mapM_, sequence, sequence_, foldl)
25+
26+-- | Framework implementation support class for the reflex implementation represented by @t@.
27+class (Reflex t, MonadReflexCreateTrigger t (HostFrame t), MonadSample t (HostFrame t), MonadHold t (HostFrame t), MonadFix (HostFrame t), MonadSubscribeEvent t (HostFrame t)) => ReflexHost t where
28+ type EventTrigger t :: * -> *
29+ type EventHandle t :: * -> *
30+ type HostFrame t :: * -> *
31+
32+-- | Monad in which Events can be 'subscribed'. This forces all underlying event sources to be initialized, so that the event will fire whenever it ought to. Events must be subscribed before they are read using readEvent
33+class (Reflex t, Monad m) => MonadSubscribeEvent t m | m -> t where
34+ -- | Subscribe to an event and set it up if needed.
35+ --
36+ -- This function will create a new 'EventHandle' from an 'Event'. This handle may then be used via
37+ -- 'readEvent' in the read callback of 'fireEventsAndRead'.
38+ --
39+ -- If the event wasn't subscribed to before (either manually or through a dependent event or behavior)
40+ -- then this function will cause the event and all dependencies of this event to be set up.
41+ -- For example, if the event was created by 'newEventWithTrigger', then it's callback will be executed.
42+ --
43+ -- It's safe to call this function multiple times.
44+ subscribeEvent :: Event t a -> m (EventHandle t a)
45+
46+-- | Monad that allows to read events' values.
47+class (ReflexHost t, Applicative m, Monad m) => MonadReadEvent t m | m -> t where
48+ -- | Read the value of an 'Event' from an 'EventHandle' (created by calling 'subscribeEvent').
49+ --
50+ -- After event propagation is done, all events can be in two states: either they are firing with some value or they are not firing.
51+ -- In the former case, this function returns @Just act@, where @act@ in an action to read the current value of
52+ -- the event. In the latter case, the function returns @Nothing@.
53+ --
54+ -- This function is normally used in the calllback for 'fireEventsAndRead'.
55+ readEvent :: EventHandle t a -> m (Maybe (m a))
56+
57+-- | A monad where new events feed from external sources can be created.
58+class (Applicative m, Monad m) => MonadReflexCreateTrigger t m | m -> t where
59+ -- | Creates a root 'Event' (one that is not based on any other event).
60+ --
61+ -- When a subscriber first subscribes to an event (building another event
62+ -- that depends on the subscription) the given callback function is run and
63+ -- passed a trigger. The callback function can then set up the event source in IO.
64+ -- After this is done, the callback function must return an accompanying teardown action.
65+ --
66+ -- Any time between setup and teardown the trigger can be used to fire
67+ -- the event, by passing it to 'fireEventsAndRead'.
68+ --
69+ -- Note: An event may be set up multiple times. So after the teardown action is executed, the event may still be set up again in the future.
70+ newEventWithTrigger :: (EventTrigger t a -> IO (IO ())) -> m (Event t a)
71+ newFanEventWithTrigger :: GCompare k => (forall a. k a -> EventTrigger t a -> IO (IO ())) -> m (EventSelector t k)
72+
73+class (ReflexHost t, MonadReflexCreateTrigger t m, MonadSubscribeEvent t m, MonadReadEvent t (ReadPhase m), MonadSample t (ReadPhase m), MonadHold t (ReadPhase m)) => MonadReflexHost t m | m -> t where
74+ type ReadPhase m :: * -> *
75+ -- | Propagate some events firings and read the values of events afterwards.
76+ --
77+ -- This function will create a new frame to fire the given events. It will then update all
78+ -- dependent events and behaviors. After that is done, the given callback is executed which
79+ -- allows to read the final values of events and check whether they have fired in this frame or
80+ -- not.
81+ --
82+ -- All events that are given are fired at the same time.
83+ --
84+ -- This function is typically used in the main loop of a reflex framework implementation.
85+ -- The main loop waits for external events to happen (such as keyboard input or a mouse click)
86+ -- and then fires the corresponding events using this function. The read callback can be used
87+ -- to read output events and perform a corresponding response action to the external event.
88+ fireEventsAndRead :: [DSum (EventTrigger t) Identity] -> (ReadPhase m a) -> m a
89+
90+ -- | Run a frame without any events firing.
91+ --
92+ -- This function should be used when you want to use 'sample' and 'hold' when no events are currently firing.
93+ -- Using this function in that case can improve performance, since the implementation can assume that no events
94+ -- are firing when 'sample' or 'hold' are called.
95+ --
96+ -- This function is commonly used to set up the basic event network when the application starts up.
97+ runHostFrame :: HostFrame t a -> m a
98+
99+-- | Like 'fireEventsAndRead', but without reading any events.
100+fireEvents :: MonadReflexHost t m => [DSum (EventTrigger t) Identity] -> m ()
101+fireEvents dm = fireEventsAndRead dm $ return ()
102+{-# INLINE fireEvents #-}
103+
104+-- | Create a new event and store its trigger in an 'IORef' while it's active.
105+--
106+-- An event is only active between the set up (when it's first subscribed to) and the teardown phases (when noboby is subscribing the event anymore).
107+-- This function returns an Event and an 'IORef'. As long as the event is active, the 'IORef' will contain 'Just' the event trigger to
108+-- trigger this event. When the event is not active, the 'IORef' will contain 'Nothing'. This allows event sources to be more efficient,
109+-- since they don't need to produce events when nobody is listening.
110+newEventWithTriggerRef :: (MonadReflexCreateTrigger t m, MonadRef m, Ref m ~ Ref IO) => m (Event t a, Ref m (Maybe (EventTrigger t a)))
111+newEventWithTriggerRef = do
112+ rt <- newRef Nothing
113+ e <- newEventWithTrigger $ \t -> do
114+ writeRef rt $ Just t
115+ return $ writeRef rt Nothing
116+ return (e, rt)
117+{-# INLINE newEventWithTriggerRef #-}
118+
119+fireEventRef :: (MonadReflexHost t m, MonadRef m, Ref m ~ Ref IO) => Ref m (Maybe (EventTrigger t a)) -> a -> m ()
120+fireEventRef mtRef input = do
121+ mt <- readRef mtRef
122+ case mt of
123+ Nothing -> return ()
124+ Just trigger -> fireEvents [trigger :=> Identity input]
125+
126+fireEventRefAndRead :: (MonadReflexHost t m, MonadRef m, Ref m ~ Ref IO) => Ref m (Maybe (EventTrigger t a)) -> a -> EventHandle t b -> m (Maybe b)
127+fireEventRefAndRead mtRef input e = do
128+ mt <- readRef mtRef
129+ case mt of
130+ Nothing -> return Nothing -- Since we aren't firing the input, the output can't fire
131+ Just trigger -> fireEventsAndRead [trigger :=> Identity input] $ do
132+ mGetValue <- readEvent e
133+ case mGetValue of
134+ Nothing -> return Nothing
135+ Just getValue -> liftM Just getValue
136+
137+
138+--------------------------------------------------------------------------------
139+-- Instances
140+--------------------------------------------------------------------------------
141+
142+instance MonadReflexCreateTrigger t m => MonadReflexCreateTrigger t (ReaderT r m) where
143+ newEventWithTrigger = lift . newEventWithTrigger
144+ newFanEventWithTrigger initializer = lift $ newFanEventWithTrigger initializer
145+
146+instance MonadSubscribeEvent t m => MonadSubscribeEvent t (ReaderT r m) where
147+ subscribeEvent = lift . subscribeEvent
148+
149+instance MonadReflexHost t m => MonadReflexHost t (ReaderT r m) where
150+ type ReadPhase (ReaderT r m) = ReadPhase m
151+ fireEventsAndRead dm a = lift $ fireEventsAndRead dm a
152+ runHostFrame = lift . runHostFrame
153+
154+instance (MonadReflexCreateTrigger t m, Monoid w) => MonadReflexCreateTrigger t (WriterT w m) where
155+ newEventWithTrigger = lift . newEventWithTrigger
156+ newFanEventWithTrigger initializer = lift $ newFanEventWithTrigger initializer
157+
158+instance (MonadSubscribeEvent t m, Monoid w) => MonadSubscribeEvent t (WriterT w m) where
159+ subscribeEvent = lift . subscribeEvent
160+
161+instance (MonadReflexHost t m, Monoid w) => MonadReflexHost t (WriterT w m) where
162+ type ReadPhase (WriterT w m) = ReadPhase m
163+ fireEventsAndRead dm a = lift $ fireEventsAndRead dm a
164+ runHostFrame = lift . runHostFrame
165+
166+instance MonadReflexCreateTrigger t m => MonadReflexCreateTrigger t (StateT s m) where
167+ newEventWithTrigger = lift . newEventWithTrigger
168+ newFanEventWithTrigger initializer = lift $ newFanEventWithTrigger initializer
169+
170+instance MonadSubscribeEvent t m => MonadSubscribeEvent t (StateT r m) where
171+ subscribeEvent = lift . subscribeEvent
172+
173+instance MonadReflexHost t m => MonadReflexHost t (StateT s m) where
174+ type ReadPhase (StateT s m) = ReadPhase m
175+ fireEventsAndRead dm a = lift $ fireEventsAndRead dm a
176+ runHostFrame = lift . runHostFrame
177+
178+
179+instance MonadReflexCreateTrigger t m => MonadReflexCreateTrigger t (Strict.StateT s m) where
180+ newEventWithTrigger = lift . newEventWithTrigger
181+ newFanEventWithTrigger initializer = lift $ newFanEventWithTrigger initializer
182+
183+instance MonadSubscribeEvent t m => MonadSubscribeEvent t (Strict.StateT r m) where
184+ subscribeEvent = lift . subscribeEvent
185+
186+instance MonadReflexHost t m => MonadReflexHost t (Strict.StateT s m) where
187+ type ReadPhase (Strict.StateT s m) = ReadPhase m
188+ fireEventsAndRead dm a = lift $ fireEventsAndRead dm a
189+ runHostFrame = lift . runHostFrame
190+
191+
192+
193+instance MonadReflexCreateTrigger t m => MonadReflexCreateTrigger t (ContT r m) where
194+ newEventWithTrigger = lift . newEventWithTrigger
195+ newFanEventWithTrigger initializer = lift $ newFanEventWithTrigger initializer
196+
197+instance MonadSubscribeEvent t m => MonadSubscribeEvent t (ContT r m) where
198+ subscribeEvent = lift . subscribeEvent
199+
200+instance MonadReflexHost t m => MonadReflexHost t (ContT r m) where
201+ type ReadPhase (ContT r m) = ReadPhase m
202+ fireEventsAndRead dm a = lift $ fireEventsAndRead dm a
203+ runHostFrame = lift . runHostFrame
204+
205+instance MonadReflexCreateTrigger t m => MonadReflexCreateTrigger t (ExceptT e m) where
206+ newEventWithTrigger = lift . newEventWithTrigger
207+ newFanEventWithTrigger initializer = lift $ newFanEventWithTrigger initializer
208+
209+instance MonadSubscribeEvent t m => MonadSubscribeEvent t (ExceptT r m) where
210+ subscribeEvent = lift . subscribeEvent
211+
212+instance MonadReflexHost t m => MonadReflexHost t (ExceptT e m) where
213+ type ReadPhase (ExceptT e m) = ReadPhase m
214+ fireEventsAndRead dm a = lift $ fireEventsAndRead dm a
215+ runHostFrame = lift . runHostFrame
216+
217+instance (MonadReflexCreateTrigger t m, Monoid w) => MonadReflexCreateTrigger t (RWST r w s m) where
218+ newEventWithTrigger = lift . newEventWithTrigger
219+ newFanEventWithTrigger initializer = lift $ newFanEventWithTrigger initializer
220+
221+instance (MonadSubscribeEvent t m, Monoid w) => MonadSubscribeEvent t (RWST r w s m) where
222+ subscribeEvent = lift . subscribeEvent
223+
224+instance (MonadReflexHost t m, Monoid w) => MonadReflexHost t (RWST r w s m) where
225+ type ReadPhase (RWST r w s m) = ReadPhase m
226+ fireEventsAndRead dm a = lift $ fireEventsAndRead dm a
227+ runHostFrame = lift . runHostFrame
src/Reflex/Spider.hsnew file+8
@@ -0,0 +1,8 @@
1+module Reflex.Spider ( Spider
2+ , SpiderEnv
3+ , Global
4+ , SpiderHost
5+ , runSpiderHost
6+ ) where
7+
8+import Reflex.Spider.Internal
src/Reflex/Spider/Internal.hsnew file+2195
@@ -0,0 +1,2195 @@
1+{-# LANGUAGE CPP, ExistentialQuantification, GADTs, ScopedTypeVariables, TypeFamilies, FlexibleInstances, MultiParamTypeClasses, GeneralizedNewtypeDeriving, RankNTypes, BangPatterns, UndecidableInstances, EmptyDataDecls, RecursiveDo, RoleAnnotations, LambdaCase, TypeOperators, FlexibleContexts, ConstraintKinds, MagicHash, UnboxedTuples, FunctionalDependencies #-}
2+module Reflex.Spider.Internal (Spider, SpiderEnv (..), Global, SpiderHost, runSpiderHost, SpiderEventHandle (..), showSubscriberType, showEventType, ComputeM (..), MergeSubscribedParent (..), HasSpiderEnv (..), newSpiderEnv, SpiderPullM (..), SpiderPushM (..), ReadPhase (..)) where
3+
4+import qualified Reflex.Class as R
5+import qualified Reflex.Host.Class as R
6+import Data.WeakBag (WeakBag, WeakBagTicket)
7+import qualified Data.WeakBag as WeakBag
8+
9+import Data.List
10+import Data.IORef
11+import System.Mem.Weak
12+import Data.Foldable
13+import Data.Traversable
14+import Control.Monad hiding (mapM, mapM_, forM_, forM, sequence)
15+import Control.Monad.Identity hiding (mapM, mapM_, forM_, forM, sequence)
16+import Control.Monad.Reader hiding (mapM, mapM_, forM_, forM, sequence)
17+import Control.Monad.State hiding (mapM, mapM_, forM_, forM, sequence)
18+import Control.Applicative -- Unconditionally import, because otherwise it breaks on GHC 7.10.1RC2
19+import Data.Some (Some)
20+import qualified Data.Some as Some
21+import Data.Dependent.Map (DMap, DSum (..))
22+import qualified Data.Dependent.Map as DMap
23+import Data.GADT.Compare
24+import Data.Maybe
25+import Data.IntMap (IntMap)
26+import qualified Data.IntMap as IntMap
27+import Control.Monad.Ref
28+import Control.Monad.Exception
29+import Data.Monoid ((<>))
30+import Data.Functor.Compose
31+import Control.Exception
32+import Control.Concurrent
33+import Data.Function
34+import Data.These
35+import Data.Align
36+
37+import GHC.IORef (IORef (..))
38+import GHC.Base (IO (..))
39+import GHC.Stack
40+
41+import System.IO.Unsafe
42+import Unsafe.Coerce
43+import Data.Coerce
44+import Control.Monad.Primitive
45+
46+import qualified Data.List.NonEmpty as NonEmpty
47+import Data.List.NonEmpty (NonEmpty (..), nonEmpty)
48+import Data.Tree (Tree (..), Forest, drawForest)
49+
50+-- Note: must come last to silence warnings due to AMP on GHC < 7.10
51+import Prelude hiding (mapM, mapM_, any, sequence, concat)
52+
53+type SubscriberList a = WeakBag (Subscriber a)
54+type SubscriberListTicket a = WeakBagTicket (Subscriber a)
55+
56+debugPropagate :: Bool
57+
58+debugInvalidateHeight :: Bool
59+
60+debugInvalidate :: Bool
61+
62+#ifdef DEBUG
63+
64+#define DEBUG_NODEIDS
65+
66+debugPropagate = True
67+
68+debugInvalidateHeight = False
69+
70+debugInvalidate = True
71+
72+class HasNodeId a where
73+ getNodeId :: a -> Int
74+
75+instance HasNodeId (Hold p a) where
76+ getNodeId = holdNodeId
77+
78+instance HasNodeId (PushSubscribed a b) where
79+ getNodeId = pushSubscribedNodeId
80+
81+instance HasNodeId (SwitchSubscribed a) where
82+ getNodeId = switchSubscribedNodeId
83+
84+instance HasNodeId (MergeSubscribed a) where
85+ getNodeId = mergeSubscribedNodeId
86+
87+instance HasNodeId (FanSubscribed a) where
88+ getNodeId = fanSubscribedNodeId
89+
90+instance HasNodeId (CoincidenceSubscribed a) where
91+ getNodeId = coincidenceSubscribedNodeId
92+
93+instance HasNodeId (RootSubscribed a) where
94+ getNodeId = rootSubscribedNodeId
95+
96+instance HasNodeId (Pull a) where
97+ getNodeId = pullNodeId
98+
99+{-# INLINE showNodeId #-}
100+showNodeId :: HasNodeId a => a -> String
101+showNodeId = ("#"<>) . show . getNodeId
102+
103+#else
104+
105+debugPropagate = False
106+
107+debugInvalidateHeight = False
108+
109+debugInvalidate = False
110+
111+-- This must be inline, or error messages will cause memory leaks due to retaining the node in question
112+{-# INLINE showNodeId #-}
113+showNodeId :: a -> String
114+showNodeId _ = ""
115+
116+#endif
117+
118+#ifdef DEBUG_NODEIDS
119+{-# NOINLINE nextNodeIdRef #-}
120+nextNodeIdRef :: IORef Int
121+nextNodeIdRef = unsafePerformIO $ newIORef 1
122+
123+newNodeId :: IO Int
124+newNodeId = atomicModifyIORef' nextNodeIdRef $ \n -> (succ n, n)
125+
126+{-# NOINLINE unsafeNodeId #-}
127+unsafeNodeId :: a -> Int
128+unsafeNodeId a = unsafePerformIO $ do
129+ touch a
130+ newNodeId
131+#endif
132+
133+--TODO: Figure out why certain things are not 'representational', then make them representational so we can use coerce
134+--type role Hold representational
135+data Hold p a
136+ = Hold { holdValue :: !(IORef a)
137+ , holdInvalidators :: !(IORef [Weak Invalidator])
138+ , holdParent :: !(IORef (Either (Event (p a)) (EventSubscription (p a)))) -- Keeps its parent alive (will be undefined until the hold is initialized) --TODO: Probably shouldn't be an IORef
139+#ifdef DEBUG_NODEIDS
140+ , holdNodeId :: Int
141+#endif
142+ }
143+
144+data EventEnv x
145+ = EventEnv { eventEnvSpiderEnv :: !(SpiderEnv x)
146+ , eventEnvAssignments :: !(IORef [SomeAssignment]) -- Needed for Subscribe
147+ , eventEnvHoldInits :: !(IORef [SomeHoldInit]) -- Needed for Subscribe
148+ , eventEnvMergeUpdates :: !(IORef [SomeMergeUpdate])
149+ , eventEnvMergeInits :: !(IORef [SomeMergeInit]) -- Needed for Subscribe
150+ , eventEnvClears :: !(IORef [SomeClear]) -- Needed for Subscribe
151+ , eventEnvRootClears :: !(IORef [SomeRootClear])
152+ , eventEnvCurrentHeight :: !(IORef Height) -- Needed for Subscribe
153+ , eventEnvResetCoincidences :: !(IORef [SomeResetCoincidence]) -- Needed for Subscribe
154+ , eventEnvDelayedMerges :: !(IORef (IntMap [DelayedMerge]))
155+ }
156+
157+{-# INLINE runEventM #-}
158+runEventM :: EventM x a -> EventEnv x -> IO a
159+runEventM = runReaderT . unEventM
160+
161+class MonadIO m => Defer a m where
162+ getDeferralQueue :: m (IORef [a])
163+
164+{-# INLINE defer #-}
165+defer :: Defer a m => a -> m ()
166+defer a = do
167+ q <- getDeferralQueue
168+ liftIO $ modifyIORef' q (a:)
169+
170+instance Defer SomeAssignment (EventM x) where
171+ {-# INLINE getDeferralQueue #-}
172+ getDeferralQueue = EventM $ asks eventEnvAssignments
173+
174+instance Defer SomeHoldInit (EventM x) where
175+ {-# INLINE getDeferralQueue #-}
176+ getDeferralQueue = EventM $ asks eventEnvHoldInits
177+
178+instance Defer SomeHoldInit BehaviorM where
179+ {-# INLINE getDeferralQueue #-}
180+ getDeferralQueue = BehaviorM $ asks snd
181+
182+instance Defer SomeMergeUpdate (EventM x) where
183+ {-# INLINE getDeferralQueue #-}
184+ getDeferralQueue = EventM $ asks eventEnvMergeUpdates
185+
186+instance Defer SomeMergeInit (EventM x) where
187+ {-# INLINE getDeferralQueue #-}
188+ getDeferralQueue = EventM $ asks eventEnvMergeInits
189+
190+class HasCurrentHeight m where
191+ getCurrentHeight :: m Height
192+ scheduleMerge :: Height -> MergeSubscribed a -> m ()
193+
194+instance HasCurrentHeight (EventM x) where
195+ {-# INLINE getCurrentHeight #-}
196+ getCurrentHeight = EventM $ do
197+ heightRef <- asks eventEnvCurrentHeight
198+ liftIO $ readIORef heightRef
199+ {-# INLINE scheduleMerge #-}
200+ scheduleMerge height subscribed = EventM $ do
201+ delayedRef <- asks eventEnvDelayedMerges
202+ liftIO $ modifyIORef' delayedRef $ IntMap.insertWith (++) (unHeight height) [DelayedMerge subscribed]
203+
204+class HasSpiderEnv x m | m -> x where
205+ askSpiderEnv :: m (SpiderEnv x)
206+
207+instance HasSpiderEnv x (EventM x) where
208+ {-# INLINE askSpiderEnv #-}
209+ askSpiderEnv = EventM $ asks eventEnvSpiderEnv
210+
211+instance HasSpiderEnv x (SpiderHost x) where
212+ {-# INLINE askSpiderEnv #-}
213+ askSpiderEnv = SpiderHost ask
214+
215+putCurrentHeight :: Height -> EventM x ()
216+putCurrentHeight h = EventM $ do
217+ heightRef <- asks eventEnvCurrentHeight
218+ liftIO $ writeIORef heightRef h
219+
220+instance Defer SomeClear (EventM x) where
221+ {-# INLINE getDeferralQueue #-}
222+ getDeferralQueue = EventM $ asks eventEnvClears
223+
224+{-# INLINE scheduleClear #-}
225+scheduleClear :: Defer SomeClear m => IORef (Maybe a) -> m ()
226+scheduleClear r = defer $ SomeClear r
227+
228+instance Defer SomeRootClear (EventM x) where
229+ {-# INLINE getDeferralQueue #-}
230+ getDeferralQueue = EventM $ asks eventEnvRootClears
231+
232+{-# INLINE scheduleRootClear #-}
233+scheduleRootClear :: Defer SomeRootClear m => IORef (DMap k Identity) -> m ()
234+scheduleRootClear r = defer $ SomeRootClear r
235+
236+instance Defer SomeResetCoincidence (EventM x) where
237+ {-# INLINE getDeferralQueue #-}
238+ getDeferralQueue = EventM $ asks eventEnvResetCoincidences
239+
240+-- Note: hold cannot examine its event until after the phase is over
241+{-# SPECIALIZE hold :: R.Patch p => a -> Event (p a) -> EventM x (Hold p a) #-}
242+{-# SPECIALIZE hold :: R.Patch p => a -> Event (p a) -> ComputeM (Hold p a) #-}
243+hold :: (R.Patch p, Defer SomeHoldInit m) => a -> Event (p a) -> m (Hold p a)
244+hold v0 e = do
245+ valRef <- liftIO $ newIORef v0
246+ invsRef <- liftIO $ newIORef []
247+ parentRef <- liftIO $ newIORef $ Left e
248+#ifdef DEBUG_NODEIDS
249+ nodeId <- liftIO newNodeId
250+#endif
251+ let h = Hold
252+ { holdValue = valRef
253+ , holdInvalidators = invsRef
254+ , holdParent = parentRef
255+#ifdef DEBUG_NODEIDS
256+ , holdNodeId = nodeId
257+#endif
258+ }
259+ defer $ SomeHoldInit h
260+ return h
261+
262+{-
263+mkWeakIORefKey :: IORef a -> b -> IO () -> IO (Weak b)
264+mkWeakIORefKey r@(IORef (STRef r#)) b (IO f) = IO $ \s ->
265+ case mkWeak# r# b f s of (# s1, w #) -> (# s1, Weak w #)
266+-}
267+
268+{-# INLINE getHoldEventSubscription #-}
269+getHoldEventSubscription :: forall m p a x. (Defer SomeAssignment m, Defer SomeHoldInit m, Defer SomeClear m, R.Patch p, HasCurrentHeight m, Defer SomeMergeInit m, Defer SomeResetCoincidence m, HasSpiderEnv x m) => Hold p a -> m (EventSubscription (p a))
270+getHoldEventSubscription h = do
271+ ep <- liftIO $ readIORef $ holdParent h
272+ case ep of
273+ Right subd -> return subd
274+ Left e -> do
275+ subscriptionRef <- liftIO $ newIORef $ error "getHoldEventSubscription: subdRef uninitialized"
276+ {-
277+ spiderEnv <- askSpiderEnv
278+ weakSubscriptionRef <- liftIO $ mkWeakIORef subscriptionRef $ return ()
279+ wh <- liftIO $ mkWeakIORefKey (holdValue h) h $ do
280+ msr <- deRefWeak weakSubscriptionRef
281+ forM_ msr $ \sr -> do
282+ subscription <- readIORef sr
283+ atomicModifyIORef (_spiderEnv_toUnsubscribe spiderEnv) $ \x -> (SomeEventSubscription subscription : x, ())
284+-- return ()
285+ {-
286+ -- What can we do? 1) Detect GCs at the SpiderEnv level, then go over the tree
287+ 2) Have a finalizer on every SubscriberListNode that only runs if its parent is still alive, and does a normal unsubscribe if so; this would seem to double the number of weakRefs we use, but perhaps not the weakDepth, so maybe it's OK; we will probably need to move the regular call to unsubscribe into the finalizer and then use finalize instead of that call; we can have a strong reference to the SubscriberListNode, which means we just need a single weak reference to each EventSubscribed, which we can reuse in all of the finalizers that need to (potentially) unsubscribe from it
288+-}
289+ -}
290+ subscription@(_, subd) <- subscribe e =<< liftIO (newSubscriberHold h)
291+ liftIO $ writeIORef subscriptionRef subscription
292+ occ <- liftIO $ readEventSubscribed subd
293+ case occ of
294+ Nothing -> return ()
295+ Just o -> do
296+ old <- liftIO $ readIORef $ holdValue h
297+ case R.apply o old of
298+ Nothing -> return ()
299+ Just new -> do
300+ -- Need to evaluate these so that we don't retain the Hold itself
301+ v <- liftIO $ evaluate $ holdValue h
302+ i <- liftIO $ evaluate $ holdInvalidators h
303+ defer $ SomeAssignment v i new
304+ liftIO $ writeIORef (holdParent h) $ Right subscription
305+ return subscription
306+
307+type BehaviorEnv = (Maybe (Weak Invalidator, IORef [SomeBehaviorSubscribed]), IORef [SomeHoldInit])
308+
309+--type role BehaviorM representational
310+-- BehaviorM can sample behaviors
311+newtype BehaviorM a = BehaviorM { unBehaviorM :: ReaderT BehaviorEnv IO a } deriving (Functor, Applicative, MonadIO, MonadFix)
312+
313+instance Monad BehaviorM where
314+ {-# INLINE (>>=) #-}
315+ BehaviorM x >>= f = BehaviorM $ x >>= unBehaviorM . f
316+ {-# INLINE (>>) #-}
317+ BehaviorM x >> BehaviorM y = BehaviorM $ x >> y
318+ {-# INLINE return #-}
319+ return x = BehaviorM $ return x
320+ {-# INLINE fail #-}
321+ fail s = BehaviorM $ fail s
322+
323+data BehaviorSubscribed a
324+ = forall p. BehaviorSubscribedHold (Hold p a)
325+ | BehaviorSubscribedPull (PullSubscribed a)
326+
327+data SomeBehaviorSubscribed = forall a. SomeBehaviorSubscribed (BehaviorSubscribed a)
328+
329+--type role PullSubscribed representational
330+data PullSubscribed a
331+ = PullSubscribed { pullSubscribedValue :: !a
332+ , pullSubscribedInvalidators :: !(IORef [Weak Invalidator])
333+ , pullSubscribedOwnInvalidator :: !Invalidator
334+ , pullSubscribedParents :: ![SomeBehaviorSubscribed] -- Need to keep parent behaviors alive, or they won't let us know when they're invalidated
335+ }
336+
337+--type role Pull representational
338+data Pull a
339+ = Pull { pullValue :: !(IORef (Maybe (PullSubscribed a)))
340+ , pullCompute :: !(BehaviorM a)
341+#ifdef DEBUG_NODEIDS
342+ , pullNodeId :: Int
343+#endif
344+ }
345+
346+data Invalidator
347+ = forall a. InvalidatorPull (Pull a)
348+ | forall a. InvalidatorSwitch (SwitchSubscribed a)
349+
350+data RootSubscribed a = forall k. GCompare k => RootSubscribed
351+ { rootSubscribedKey :: !(k a)
352+ , rootSubscribedCachedSubscribed :: !(IORef (DMap k RootSubscribed)) -- From the original Root
353+ , rootSubscribedSubscribers :: !(SubscriberList a)
354+ , rootSubscribedOccurrence :: !(IO (Maybe a)) -- Lookup from rootOccurrence
355+ , rootSubscribedUninit :: IO ()
356+ , rootSubscribedWeakSelf :: !(IORef (Weak (RootSubscribed a))) --TODO: Can we make this a lazy non-IORef and then force it manually to avoid an indirection each time we use it?
357+#ifdef DEBUG_NODEIDS
358+ , rootSubscribedNodeId :: Int
359+#endif
360+ }
361+
362+data Root (k :: * -> *)
363+ = Root { rootOccurrence :: !(IORef (DMap k Identity)) -- The currently-firing occurrence of this event
364+ , rootSubscribed :: !(IORef (DMap k RootSubscribed))
365+ , rootInit :: !(forall a. k a -> RootTrigger a -> IO (IO ()))
366+ }
367+
368+data SomeHoldInit = forall p a. R.Patch p => SomeHoldInit !(Hold p a)
369+
370+data SomeMergeUpdate = forall k. GCompare k => SomeMergeUpdate (R.PatchDMap (DMap k Event)) (MergeSubscribed k)
371+
372+data SomeMergeInit = forall k. GCompare k => SomeMergeInit !(MergeSubscribed k) !(Event (R.PatchDMap (DMap k Event)))
373+
374+data SomeEventSubscription = forall a. SomeEventSubscription (EventSubscription a)
375+
376+-- EventM can do everything BehaviorM can, plus create holds
377+newtype EventM x a = EventM { unEventM :: ReaderT (EventEnv x) IO a } deriving (Functor, Applicative, Monad, MonadIO, MonadFix, MonadException, MonadAsyncException) -- The environment should be Nothing if we are not in a frame, and Just if we are - in which case it is a list of assignments to be done after the frame is over
378+
379+data PushSubscribed a b
380+ = PushSubscribed { pushSubscribedCachedSubscribed :: !(IORef (Maybe (PushSubscribed a b)))
381+ , pushSubscribedOccurrence :: !(IORef (Maybe b)) -- If the current height is less than our height, this should always be Nothing; during our height, this will get filled in at some point, always before our children are notified; after our height, this will be filled in with the correct value (Nothing if we are not firing, Just if we are)
382+ , pushSubscribedHeight :: !(IORef Height)
383+ , pushSubscribedSubscribers :: !(SubscriberList b)
384+ , pushSubscribedSelf :: !(Subscriber a) -- Hold this in memory to ensure our WeakReferences don't die --TODO: Probably unneeded now
385+ , pushSubscribedWeakSelf :: !(IORef (Weak (PushSubscribed a b)))
386+ , pushSubscribedParent :: !(EventSubscription a)
387+#ifdef DEBUG_NODEIDS
388+ , pushSubscribedNodeId :: Int
389+#endif
390+ }
391+
392+newtype ComputeM a = ComputeM { unComputeM :: ReaderT (IORef [SomeHoldInit]) IO a } deriving (Functor, Applicative, Monad, MonadIO, MonadFix)
393+
394+instance Defer SomeHoldInit ComputeM where
395+ getDeferralQueue = ComputeM ask
396+
397+{-# SPECIALIZE runComputeM :: ComputeM a -> EventM x a #-}
398+runComputeM :: Defer SomeHoldInit m => ComputeM a -> m a
399+runComputeM (ComputeM a) = liftIO . runReaderT a =<< getDeferralQueue
400+
401+data Push a b
402+ = Push { pushCompute :: !(a -> ComputeM (Maybe b)) -- Compute the current firing value; assumes that its parent has been computed --TODO: This only uses Defer SomeHoldInit EventM; perhaps only the hold init queue should be passed in (also, perhaps those hold inits can be accelerated - can there be a loop we would need to worry about?)
403+ , pushParent :: !(Event a)
404+ , pushSubscribed :: !(IORef (Maybe (PushSubscribed a b))) --TODO: Can we replace this with an unsafePerformIO thunk?
405+ }
406+
407+data MergeSubscribedParent k a
408+ = MergeSubscribedParent { mergeSubscribedParentSubscription :: !(EventSubscription a)
409+ , mergeSubscribedParentSubscriber :: !(Subscriber a)
410+ }
411+
412+data MergeSubscribed k
413+ = MergeSubscribed { mergeSubscribedCachedSubscribed :: !(IORef (Maybe (MergeSubscribed k))) -- From the original merge
414+ , mergeSubscribedOccurrence :: !(IORef (Maybe (DMap k Identity)))
415+ , mergeSubscribedAccum :: !(IORef (DMap k Identity)) -- This will accumulate occurrences until our height is reached, at which point it will be transferred to mergeSubscribedOccurrence
416+ , mergeSubscribedHeight :: !(IORef Height)
417+ , mergeSubscribedSubscribers :: !(SubscriberList (DMap k Identity))
418+ , mergeSubscribedChange :: !(IORef (Subscriber (R.PatchDMap (DMap k Event)), EventSubscribed (R.PatchDMap (DMap k Event))))
419+ , mergeSubscribedParents :: !(IORef (DMap k (MergeSubscribedParent k)))
420+ , mergeSubscribedWeakSelf :: !(IORef (Weak (MergeSubscribed k)))
421+#ifdef DEBUG_NODEIDS
422+ , mergeSubscribedNodeId :: Int
423+#endif
424+ }
425+
426+--TODO: DMap sucks; we should really write something better (with a functor for the value as well as the key)
427+data Merge k
428+ = Merge { mergeParents :: !(Dynamic R.PatchDMap (DMap k Event))
429+ , mergeSubscribed :: !(IORef (Maybe (MergeSubscribed k)))
430+ }
431+
432+data FanSubscribedChildren k a = FanSubscribedChildren
433+ { _fanSubscribedChildren_list :: !(SubscriberList a)
434+ , _fanSubscribedChildren_weakSelf :: !(IORef (Weak (k a, FanSubscribed k)))
435+ }
436+
437+data FanSubscribed k
438+ = FanSubscribed { fanSubscribedCachedSubscribed :: !(IORef (Maybe (FanSubscribed k)))
439+ , fanSubscribedSubscribers :: !(IORef (DMap k (FanSubscribedChildren k))) -- This DMap should never be empty
440+ , fanSubscribedParent :: !(EventSubscription (DMap k Identity))
441+#ifdef DEBUG_NODEIDS
442+ , fanSubscribedNodeId :: Int
443+#endif
444+ }
445+
446+data Fan k
447+ = Fan { fanParent :: !(Event (DMap k Identity))
448+ , fanSubscribed :: !(IORef (Maybe (FanSubscribed k)))
449+ }
450+
451+data SwitchSubscribed a
452+ = SwitchSubscribed { switchSubscribedCachedSubscribed :: !(IORef (Maybe (SwitchSubscribed a)))
453+ , switchSubscribedOccurrence :: !(IORef (Maybe a))
454+ , switchSubscribedHeight :: !(IORef Height)
455+ , switchSubscribedSubscribers :: !(SubscriberList a)
456+ , switchSubscribedOwnInvalidator :: {-# NOUNPACK #-} !Invalidator
457+ , switchSubscribedOwnWeakInvalidator :: !(IORef (Weak Invalidator))
458+ , switchSubscribedBehaviorParents :: !(IORef [SomeBehaviorSubscribed])
459+ , switchSubscribedParent :: !(Behavior (Event a))
460+ , switchSubscribedCurrentParent :: !(IORef (EventSubscription a))
461+ , switchSubscribedWeakSelf :: !(IORef (Weak (SwitchSubscribed a)))
462+#ifdef DEBUG_NODEIDS
463+ , switchSubscribedNodeId :: Int
464+#endif
465+ }
466+
467+data Switch a
468+ = Switch { switchParent :: !(Behavior (Event a))
469+ , switchSubscribed :: !(IORef (Maybe (SwitchSubscribed a)))
470+ }
471+
472+data CoincidenceSubscribed a
473+ = CoincidenceSubscribed { coincidenceSubscribedCachedSubscribed :: !(IORef (Maybe (CoincidenceSubscribed a)))
474+ , coincidenceSubscribedOccurrence :: !(IORef (Maybe a))
475+ , coincidenceSubscribedSubscribers :: !(SubscriberList a)
476+ , coincidenceSubscribedHeight :: !(IORef Height)
477+ , coincidenceSubscribedOuter :: {-# NOUNPACK #-} (Subscriber (Event a))
478+ , coincidenceSubscribedOuterParent :: !(EventSubscription (Event a))
479+ , coincidenceSubscribedInnerParent :: !(IORef (Maybe (EventSubscribed a)))
480+ , coincidenceSubscribedWeakSelf :: !(IORef (Weak (CoincidenceSubscribed a)))
481+#ifdef DEBUG_NODEIDS
482+ , coincidenceSubscribedNodeId :: Int
483+#endif
484+ }
485+
486+data Coincidence a
487+ = Coincidence { coincidenceParent :: !(Event (Event a))
488+ , coincidenceSubscribed :: !(IORef (Maybe (CoincidenceSubscribed a)))
489+ }
490+
491+data Subscriber a
492+ = forall b. SubscriberPush (a -> ComputeM (Maybe b)) (PushSubscribed a b) --TODO: Why does this have the pushCompute in it?
493+ | forall k. GCompare k => SubscriberMerge !(k a) (MergeSubscribed k) --TODO: Can we inline the GCompare?
494+ | forall k. (a ~ R.PatchDMap (DMap k Event), GCompare k) => SubscriberMergeChange (MergeSubscribed k)
495+ | forall k. (GCompare k, a ~ DMap k Identity) => SubscriberFan (FanSubscribed k)
496+ | forall p b. (a ~ p b, R.Patch p) => SubscriberHold !(Hold p b)
497+ | forall b. (a ~ Identity b) => SubscriberHoldIdentity !(Hold Identity b)
498+ | SubscriberSwitch (SwitchSubscribed a)
499+ | forall b. a ~ Event b => SubscriberCoincidenceOuter (CoincidenceSubscribed b)
500+ | SubscriberCoincidenceInner (CoincidenceSubscribed a)
501+ | SubscriberHandle
502+
503+showSubscriberType :: Subscriber a -> String
504+showSubscriberType = \case
505+ SubscriberPush _ _ -> "SubscriberPush"
506+ SubscriberMerge _ _ -> "SubscriberMerge"
507+ SubscriberFan _ -> "SubscriberFan"
508+ SubscriberMergeChange _ -> "SubscriberMergeChange"
509+ SubscriberHold _ -> "SubscriberHold"
510+ SubscriberHoldIdentity _ -> "SubscriberHoldIdentity"
511+ SubscriberSwitch _ -> "SubscriberSwitch"
512+ SubscriberCoincidenceOuter _ -> "SubscriberCoincidenceOuter"
513+ SubscriberCoincidenceInner _ -> "SubscriberCoincidenceInner"
514+ SubscriberHandle -> "SubscriberHandle"
515+
516+data Event a
517+ = forall k. GCompare k => EventRoot !(k a) !(Root k)
518+ | EventNever
519+ | forall b. EventPush !(Push b a)
520+ | forall k. (GCompare k, a ~ DMap k Identity) => EventMerge !(Merge k)
521+ | forall k. GCompare k => EventFan !(k a) !(Fan k)
522+ | EventSwitch !(Switch a)
523+ | EventCoincidence !(Coincidence a)
524+ | forall p b. (a ~ p b, R.Patch p) => EventHold !(Hold p b)
525+ | forall p b. (a ~ p b, R.Patch p) => EventDyn !(Dyn p b)
526+ | forall b. (a ~ Identity b) => EventHoldIdentity !(Hold Identity b)
527+ | forall b. (a ~ Identity b) => EventDynIdentity !(Dyn Identity b)
528+
529+showEventType :: Event a -> String
530+showEventType = \case
531+ EventRoot _ _ -> "EventRoot"
532+ EventNever -> "EventNever"
533+ EventPush _ -> "EventPush"
534+ EventMerge _ -> "EventMerge"
535+ EventFan _ _ -> "EventFan"
536+ EventSwitch _ -> "EventSwitch"
537+ EventCoincidence _ -> "EventCoincidence"
538+ EventHold _ -> "EventHold"
539+ EventDyn _ -> "EventDyn"
540+ EventHoldIdentity _ -> "EventHoldIdentity"
541+ EventDynIdentity _ -> "EventDynIdentity"
542+
543+type EventSubscription a = (Maybe (SubscriberListTicket a), EventSubscribed a)
544+
545+data EventSubscribed a
546+ = EventSubscribedRoot {-# NOUNPACK #-} (RootSubscribed a)
547+ | EventSubscribedNever
548+ | forall b. EventSubscribedPush !(PushSubscribed b a)
549+ | forall k. (GCompare k, a ~ DMap k Identity) => EventSubscribedMerge !(MergeSubscribed k)
550+ | forall k. GCompare k => EventSubscribedFan !(k a) !(FanSubscribed k)
551+ | EventSubscribedSwitch !(SwitchSubscribed a)
552+ | EventSubscribedCoincidence !(CoincidenceSubscribed a)
553+
554+unsubscribe :: EventSubscription a -> IO ()
555+unsubscribe (msln, _) = forM_ msln $ \sln -> WeakBag.remove sln
556+
557+-- These function are constructor functions that are marked NOINLINE so they are
558+-- opaque to GHC. If we do not do this, then GHC will sometimes fuse the constructor away
559+-- so any weak references that are attached to the constructors will have their
560+-- finalizer run. Using the opaque constructor, does not see the
561+-- constructor application, so it behaves like an IORef and cannot be fused away.
562+--
563+-- The result is also evaluated to WHNF, since forcing a thunk invalidates
564+-- the weak pointer to it in some cases.
565+
566+--TODO: I think all of these noinlines are now unnecessary
567+{-# NOINLINE newSubscriberPush #-}
568+newSubscriberPush :: (a -> ComputeM (Maybe b)) -> PushSubscribed a b -> IO (Subscriber a)
569+newSubscriberPush compute subd = return $! SubscriberPush compute subd
570+
571+{-# RULES
572+ "newSubscriberHold/Identity" forall h. newSubscriberHold h = newSubscriberHoldIdentity h
573+ #-}
574+{-# NOINLINE newSubscriberHold #-}
575+newSubscriberHold :: R.Patch p => Hold p a -> IO (Subscriber (p a))
576+newSubscriberHold h = return $! SubscriberHold h
577+
578+{-# NOINLINE newSubscriberHoldIdentity #-}
579+newSubscriberHoldIdentity :: Hold Identity a -> IO (Subscriber (Identity a))
580+newSubscriberHoldIdentity h = return $! SubscriberHoldIdentity h
581+
582+{-# NOINLINE newSubscriberMerge #-}
583+newSubscriberMerge :: GCompare k => k a -> MergeSubscribed k -> IO (Subscriber a)
584+newSubscriberMerge k subd = return $! SubscriberMerge k subd
585+
586+{-# NOINLINE newSubscriberMergeChange #-}
587+newSubscriberMergeChange :: GCompare k => MergeSubscribed k -> IO (Subscriber (R.PatchDMap (DMap k Event)))
588+newSubscriberMergeChange subd = return $! SubscriberMergeChange subd
589+
590+{-# NOINLINE newSubscriberFan #-}
591+newSubscriberFan :: GCompare k => FanSubscribed k -> IO (Subscriber (DMap k Identity))
592+newSubscriberFan subd = return $! SubscriberFan subd
593+
594+{-# NOINLINE newSubscriberSwitch #-}
595+newSubscriberSwitch :: SwitchSubscribed a -> IO (Subscriber a)
596+newSubscriberSwitch subd = return $! SubscriberSwitch subd
597+
598+{-# NOINLINE newSubscriberCoincidenceOuter #-}
599+newSubscriberCoincidenceOuter :: CoincidenceSubscribed b -> IO (Subscriber (Event b))
600+newSubscriberCoincidenceOuter subd = return $! SubscriberCoincidenceOuter subd
601+
602+{-# NOINLINE newSubscriberCoincidenceInner #-}
603+newSubscriberCoincidenceInner :: CoincidenceSubscribed a -> IO (Subscriber a)
604+newSubscriberCoincidenceInner subd = return $! SubscriberCoincidenceInner subd
605+
606+{-# NOINLINE newInvalidatorSwitch #-}
607+newInvalidatorSwitch :: SwitchSubscribed a -> IO Invalidator
608+newInvalidatorSwitch subd = return $! InvalidatorSwitch subd
609+
610+{-# NOINLINE newInvalidatorPull #-}
611+newInvalidatorPull :: Pull a -> IO Invalidator
612+newInvalidatorPull p = return $! InvalidatorPull p
613+
614+data Dynamic p a
615+ = DynamicHold !(Hold p a)
616+ | p ~ Identity => DynamicHoldIdentity !(Hold Identity a)
617+ | DynamicConst !a
618+ | DynamicDyn !(Dyn p a)
619+ | p ~ Identity => DynamicDynIdentity !(Dyn Identity a)
620+
621+{-# INLINE current #-}
622+current :: R.Patch p => Dynamic p a -> Behavior a
623+current = \case
624+ DynamicHold h -> BehaviorHold h
625+ DynamicHoldIdentity h -> BehaviorHoldIdentity h
626+ DynamicConst a -> BehaviorConst a
627+ DynamicDyn d -> BehaviorDyn d
628+ DynamicDynIdentity d -> BehaviorDynIdentity d
629+
630+--TODO: If you only need updated, not current, can we avoid actually constructing the Hold?
631+{-# INLINE updated #-}
632+updated :: R.Patch p => Dynamic p a -> Event (p a)
633+updated = \case
634+ DynamicHold h -> EventHold h
635+ DynamicHoldIdentity h -> EventHoldIdentity h
636+ DynamicConst _ -> EventNever
637+ DynamicDyn d -> EventDyn d
638+ DynamicDynIdentity d -> EventDynIdentity d
639+
640+newMapDyn :: (a -> b) -> Dynamic Identity a -> Dynamic Identity b
641+newMapDyn f d = DynamicDynIdentity $ unsafeDyn (fmap f $ readBehaviorTracked $ current d) (fmap (Identity . f . runIdentity) $ updated d)
642+
643+instance Functor (Dynamic Identity) where
644+ fmap f d = newMapDyn f d
645+
646+instance Applicative (Dynamic Identity) where
647+ pure = DynamicConst
648+ (<*>) = zipDynWith ($)
649+ _ *> b = b
650+ a <* _ = a
651+
652+instance R.FunctorMaybe Event where
653+ fmapMaybe f = push $ return . f
654+
655+instance Align Event where
656+ nil = EventNever
657+ align ea eb = R.fmapMaybe R.dmapToThese $ merge $ DynamicConst $ DMap.fromList [R.LeftTag :=> ea, R.RightTag :=> eb]
658+
659+--TODO: Avoid the duplication between this and R.zipDynWith
660+zipDynWith :: (a -> b -> c) -> Dynamic Identity a -> Dynamic Identity b -> Dynamic Identity c
661+zipDynWith f da db =
662+ let eab = align (updated da) (updated db)
663+ ec = flip push eab $ \o -> do
664+ (a, b) <- case o of
665+ This (Identity a) -> do
666+ b <- readBehaviorUntracked $ current db
667+ return (a, b)
668+ That (Identity b) -> do
669+ a <- readBehaviorUntracked $ current da
670+ return (a, b)
671+ These (Identity a) (Identity b) -> return (a, b)
672+ return $ Just $ Identity $ f a b
673+ in DynamicDynIdentity $ unsafeDyn (f <$> readBehaviorUntracked (current da) <*> readBehaviorUntracked (current db)) ec
674+
675+instance Monad (Dynamic Identity) where
676+ {-# INLINE return #-}
677+ return = pure
678+ {-# INLINE (>>=) #-}
679+ x >>= f = DynamicDynIdentity $ newJoinDyn $ newMapDyn f x --TODO: (>>), fail
680+ {-# INLINE (>>) #-}
681+ _ >> y = y
682+ {-# INLINE fail #-}
683+ fail _ = error "Dynamic does not support 'fail'"
684+
685+newtype Dyn p a = Dyn { unDyn :: IORef (Either (BehaviorM a, Event (p a)) (Hold p a)) }
686+
687+unsafeDyn :: BehaviorM a -> Event (p a) -> Dyn p a
688+unsafeDyn readV0 v' = Dyn $ unsafeNewIORef x $ Left x
689+ where x = (readV0, v')
690+
691+newJoinDyn :: Dynamic Identity (Dynamic Identity a) -> Dyn Identity a
692+newJoinDyn d =
693+ let readV0 = readBehaviorTracked . current =<< readBehaviorTracked (current d)
694+ eOuter = push (fmap (Just . Identity) . readBehaviorUntracked . current . runIdentity) $ updated d
695+ eInner = switch $ fmap updated $ current d
696+ eBoth = coincidence $ fmap (updated . runIdentity) $ updated d
697+ v' = unSpiderEvent $ R.leftmost $ map SpiderEvent [eBoth, eOuter, eInner]
698+ in unsafeDyn readV0 v'
699+
700+--type role Behavior representational
701+data Behavior a
702+ = forall p. BehaviorHold !(Hold p a)
703+ | BehaviorHoldIdentity !(Hold Identity a)
704+ | BehaviorConst !a
705+ | BehaviorPull !(Pull a)
706+ | BehaviorDynIdentity !(Dyn Identity a)
707+ | forall p. R.Patch p => BehaviorDyn !(Dyn p a)
708+
709+-- ResultM can read behaviors and events
710+type ResultM = EventM
711+
712+{-# NOINLINE unsafeNewIORef #-}
713+unsafeNewIORef :: a -> b -> IORef b
714+unsafeNewIORef _ b = unsafePerformIO $ newIORef b
715+
716+instance Functor Event where
717+ fmap f = push $ return . Just . f
718+
719+instance Functor Behavior where
720+ fmap f = pull . liftM f . readBehaviorTracked
721+
722+{-# NOINLINE push #-} --TODO: If this is helpful, we can get rid of the unsafeNewIORef and use unsafePerformIO directly
723+push :: forall a b. (a -> ComputeM (Maybe b)) -> Event a -> Event b
724+push f e = EventPush $ Push
725+ { pushCompute = f
726+ , pushParent = e
727+ , pushSubscribed = unsafeNewIORef (f, e) Nothing --TODO: Does the use of the tuple here create unnecessary overhead?
728+ }
729+-- DISABLED: {- RULES "push/push" forall f g e. push f (push g e) = push (maybe (return Nothing) f <=< g) e #-}
730+
731+{-# NOINLINE pull #-}
732+pull :: BehaviorM a -> Behavior a
733+pull a = BehaviorPull $ Pull
734+ { pullCompute = a
735+ , pullValue = unsafeNewIORef a Nothing
736+#ifdef DEBUG_NODEIDS
737+ , pullNodeId = unsafeNodeId a
738+#endif
739+ }
740+
741+{-# NOINLINE switch #-}
742+switch :: Behavior (Event a) -> Event a
743+switch a = EventSwitch $ Switch
744+ { switchParent = a
745+ , switchSubscribed = unsafeNewIORef a Nothing
746+ }
747+
748+coincidence :: Event (Event a) -> Event a
749+coincidence a = EventCoincidence $ Coincidence
750+ { coincidenceParent = a
751+ , coincidenceSubscribed = unsafeNewIORef a Nothing
752+ }
753+
754+propagateAndUpdateSubscribersRef :: SubscriberList a -> a -> EventM x ()
755+propagateAndUpdateSubscribersRef subscribersRef a = do
756+ propagate a subscribersRef
757+
758+-- Propagate the given event occurrence; before cleaning up, run the given action, which may read the state of events and behaviors
759+run :: [DSum RootTrigger Identity] -> ResultM x b -> SpiderHost x b
760+run roots after = do
761+ tracePropagate $ "Running an event frame with " <> show (length roots) <> " events"
762+ spiderEnv <- SpiderHost ask
763+ result <- SpiderHost $ lift $ withMVar (_spiderEnv_lock spiderEnv) $ \_ -> flip runReaderT spiderEnv $ unSpiderHost $ runFrame $ do
764+ rootsToPropagate <- forM roots $ \r@(RootTrigger (_, occRef, k) :=> a) -> do
765+ occBefore <- liftIO $ do
766+ occBefore <- readIORef occRef
767+ writeIORef occRef $! DMap.insert k a occBefore
768+ return occBefore
769+ if DMap.null occBefore
770+ then do scheduleRootClear occRef
771+ return $ Just r
772+ else return Nothing
773+ forM_ (catMaybes rootsToPropagate) $ \(RootTrigger (subscribersRef, _, _) :=> Identity a) -> do
774+ propagateAndUpdateSubscribersRef subscribersRef a
775+ delayedRef <- EventM $ asks eventEnvDelayedMerges
776+ let go = do
777+ delayed <- liftIO $ readIORef delayedRef
778+ case IntMap.minViewWithKey delayed of
779+ Nothing -> return ()
780+ Just ((currentHeight, cur), future) -> do
781+ tracePropagate $ "Running height " ++ show currentHeight
782+ putCurrentHeight $ Height currentHeight
783+ liftIO $ writeIORef delayedRef future
784+ forM_ cur $ \d -> case d of
785+ DelayedMerge subscribed -> do
786+ height <- liftIO $ readIORef $ mergeSubscribedHeight subscribed
787+ case height `compare` Height currentHeight of
788+ LT -> error "Somehow a merge's height has been decreased after it was scheduled"
789+ GT -> scheduleMerge height subscribed -- The height has been increased (by a coincidence event; TODO: is this the only way?)
790+ EQ -> do
791+ m <- liftIO $ readIORef $ mergeSubscribedAccum subscribed
792+ liftIO $ writeIORef (mergeSubscribedAccum subscribed) DMap.empty
793+ --TODO: Assert that m is not empty
794+ liftIO $ writeIORef (mergeSubscribedOccurrence subscribed) $ Just m
795+ scheduleClear $ mergeSubscribedOccurrence subscribed
796+ propagateAndUpdateSubscribersRef (mergeSubscribedSubscribers subscribed) m
797+ go
798+ go
799+ putCurrentHeight maxBound
800+ after
801+ tracePropagate "Done running an event frame"
802+ return result
803+
804+data SomeClear = forall a. SomeClear (IORef (Maybe a))
805+
806+data SomeRootClear = forall k. SomeRootClear (IORef (DMap k Identity))
807+
808+data SomeAssignment = forall a. SomeAssignment {-# UNPACK #-} !(IORef a) {-# UNPACK #-} !(IORef [Weak Invalidator]) a
809+
810+data DelayedMerge = forall k. DelayedMerge (MergeSubscribed k)
811+
812+debugFinalize :: Bool
813+debugFinalize = False
814+
815+mkWeakPtrWithDebug :: a -> String -> IO (Weak a)
816+mkWeakPtrWithDebug x debugNote = do
817+ x' <- evaluate x
818+ mkWeakPtr x' $
819+ if debugFinalize
820+ then Just $ putStrLn $ "finalizing: " ++ debugNote
821+ else Nothing
822+
823+type WeakList a = [Weak a]
824+
825+{-# INLINE withIncreasedDepth #-}
826+#ifdef DEBUG
827+withIncreasedDepth :: CanTrace x m => m a -> m a
828+withIncreasedDepth a = do
829+ spiderEnv <- askSpiderEnv
830+ liftIO $ modifyIORef' (_spiderEnv_depth spiderEnv) succ
831+ result <- a
832+ liftIO $ modifyIORef' (_spiderEnv_depth spiderEnv) pred
833+ return result
834+#else
835+withIncreasedDepth :: m a -> m a
836+withIncreasedDepth = id
837+#endif
838+
839+type CanTrace x m = (HasSpiderEnv x m, MonadIO m)
840+
841+{-# INLINE tracePropagate #-}
842+tracePropagate :: (CanTrace x m) => String -> m ()
843+tracePropagate = traceWhen debugPropagate
844+
845+{-# INLINE traceSubscribe #-}
846+traceSubscribe :: String -> IO ()
847+traceSubscribe = when debugSubscribe . liftIO . putStrLn
848+
849+{-# INLINE traceInvalidate #-}
850+traceInvalidate :: String -> IO ()
851+traceInvalidate = when debugInvalidate . liftIO . putStrLn
852+
853+{-# INLINE traceWhen #-}
854+traceWhen :: (CanTrace x m) => Bool -> String -> m ()
855+traceWhen b message = traceMWhen b $ return message
856+
857+{-# INLINE traceMWhen #-}
858+traceMWhen :: (CanTrace x m) => Bool -> m String -> m ()
859+traceMWhen b getMessage = when b $ do
860+ message <- getMessage
861+#ifdef DEBUG
862+ spiderEnv <- askSpiderEnv
863+ d <- liftIO $ readIORef $ _spiderEnv_depth spiderEnv
864+#else
865+ let d = 0
866+#endif
867+ liftIO $ putStrLn $ replicate d ' ' <> message
868+
869+whoCreatedIORef :: IORef a -> IO [String]
870+whoCreatedIORef (IORef a) = whoCreated $! a
871+
872+whoCreatedEventSubscribed :: EventSubscribed a -> IO [String]
873+whoCreatedEventSubscribed = \case
874+ EventSubscribedRoot _ -> return ["root"]
875+ EventSubscribedNever -> return ["never"]
876+ EventSubscribedPush subscribed -> whoCreatedIORef $ pushSubscribedCachedSubscribed subscribed
877+ EventSubscribedFan _ subscribed -> whoCreatedIORef $ fanSubscribedCachedSubscribed subscribed
878+ EventSubscribedMerge subscribed -> whoCreatedIORef $ mergeSubscribedCachedSubscribed subscribed
879+ EventSubscribedSwitch subscribed -> whoCreatedIORef $ switchSubscribedCachedSubscribed subscribed
880+ EventSubscribedCoincidence subscribed -> whoCreatedIORef $ coincidenceSubscribedCachedSubscribed subscribed
881+
882+walkInvalidHeightParents :: EventSubscribed a -> IO [Some EventSubscribed]
883+walkInvalidHeightParents s0 = do
884+ subscribers <- flip execStateT mempty $ ($ Some.This s0) $ fix $ \loop (Some.This s) -> do
885+ h <- liftIO $ readIORef $ eventSubscribedHeightRef s
886+ when (h == invalidHeight) $ do
887+ when (eventSubscribedHasOwnHeightRef s) $ liftIO $ writeIORef (eventSubscribedHeightRef s) invalidHeightBeingTraversed
888+ modify (Some.This s :)
889+ mapM_ loop =<< liftIO (eventSubscribedGetParents s)
890+ forM_ subscribers $ \(Some.This s) -> writeIORef (eventSubscribedHeightRef s) invalidHeight
891+ return subscribers
892+
893+groupByHead :: Eq a => [NonEmpty a] -> [(a, NonEmpty [a])]
894+groupByHead = \case
895+ [] -> []
896+ (x :| xs) : t -> case groupByHead t of
897+ [] -> [(x, xs :| [])]
898+ l@((y, yss) : t')
899+ | x == y -> (x, xs `NonEmpty.cons` yss) : t'
900+ | otherwise -> (x, xs :| []) : l
901+
902+listsToForest :: Eq a => [[a]] -> Forest a
903+listsToForest l = fmap (\(a, l') -> Node a $ listsToForest $ toList l') $ groupByHead $ catMaybes $ fmap nonEmpty l
904+
905+-- | Propagate everything at the current height
906+propagate :: a -> SubscriberList a -> EventM x ()
907+propagate a subscribers = withIncreasedDepth $ do
908+ -- Note: in the following traversal, we do not visit nodes that are added to the list during our traversal; they are new events, which will necessarily have full information already, so there is no need to traverse them
909+ --TODO: Should we check if nodes already have their values before propagating? Maybe we're re-doing work
910+ WeakBag.traverse subscribers $ \s -> case s of
911+ SubscriberPush compute subscribed -> {-# SCC "traversePush" #-} do
912+ tracePropagate $ "SubscriberPush" <> showNodeId subscribed
913+ occ <- runComputeM $ compute a
914+ case occ of
915+ Nothing -> return () -- No need to write a Nothing back into the Ref
916+ Just o -> do
917+ liftIO $ writeIORef (pushSubscribedOccurrence subscribed) occ
918+ scheduleClear $ pushSubscribedOccurrence subscribed
919+ propagate o $ pushSubscribedSubscribers subscribed
920+ SubscriberMerge k subscribed -> {-# SCC "traverseMerge" #-} do
921+ tracePropagate $ "SubscriberMerge" <> showNodeId subscribed
922+ oldM <- liftIO $ readIORef $ mergeSubscribedAccum subscribed
923+ let newM = DMap.insertWith (error $ "Same key fired multiple times for Merge" <> showNodeId subscribed) k (Identity a) oldM
924+ tracePropagate $ " DMap.size oldM = " <> show (DMap.size oldM) <> "; DMap.size newM = " <> show (DMap.size newM)
925+ liftIO $ writeIORef (mergeSubscribedAccum subscribed) $! newM
926+ when (DMap.null oldM) $ do -- Only schedule the firing once
927+ height <- liftIO $ readIORef $ mergeSubscribedHeight subscribed
928+ --TODO: assertions about height
929+ currentHeight <- getCurrentHeight
930+ when (height <= currentHeight) $ do
931+ myStack <- liftIO $ whoCreatedIORef $ mergeSubscribedCachedSubscribed subscribed
932+ if height /= invalidHeight
933+ then error $ "Height (" ++ show height ++ ") is not greater than current height (" ++ show currentHeight ++ ")\n" ++ unlines (reverse myStack)
934+ else liftIO $ do
935+ nodesInvolvedInCycle <- walkInvalidHeightParents $ EventSubscribedMerge subscribed
936+ stacks <- forM nodesInvolvedInCycle $ \(Some.This es) -> whoCreatedEventSubscribed es
937+ error $ "Causality loop found:\n" <> drawForest (listsToForest stacks)
938+ scheduleMerge height subscribed
939+ SubscriberMergeChange subscribed -> {-# SCC "traverseMergeChange" #-} do
940+ tracePropagate $ "SubscriberMerge" <> showNodeId subscribed
941+ defer $ SomeMergeUpdate a subscribed
942+ SubscriberFan subscribed -> {-# SCC "traverseFan" #-} do
943+ subs <- liftIO $ readIORef $ fanSubscribedSubscribers subscribed
944+ tracePropagate $ "SubscriberFan" <> showNodeId subscribed <> ": " ++ show (DMap.size subs) ++ " keys subscribed, " ++ show (DMap.size a) ++ " keys firing"
945+ --TODO: We need a better DMap intersection; here, we are assuming that the number of firing keys is small and the number of subscribers is large
946+ forM_ (DMap.toList a) $ \(k :=> Identity v) -> {-# SCC "SubscriberFan" #-} case DMap.lookup k subs of
947+ Nothing -> do
948+ tracePropagate "No subscriber for key"
949+ return ()
950+ Just subsubs -> do
951+ propagate v $ _fanSubscribedChildren_list subsubs
952+ return ()
953+ SubscriberHold h -> {-# SCC "traverseHold" #-} propagateSubscriberHold h a
954+ SubscriberHoldIdentity h -> {-# SCC "traverseHoldIdentity" #-} propagateSubscriberHold h a
955+ SubscriberSwitch subscribed -> {-# SCC "traverseSwitch" #-} do
956+ tracePropagate $ "SubscriberSwitch" <> showNodeId subscribed
957+ liftIO $ writeIORef (switchSubscribedOccurrence subscribed) $ Just a
958+ scheduleClear $ switchSubscribedOccurrence subscribed
959+ propagate a $ switchSubscribedSubscribers subscribed
960+ SubscriberCoincidenceOuter subscribed -> {-# SCC "traverseCoincidenceOuter" #-} do
961+ tracePropagate $ "SubscriberCoincidenceOuter" <> showNodeId subscribed
962+ outerHeight <- liftIO $ readIORef $ coincidenceSubscribedHeight subscribed
963+ tracePropagate $ " outerHeight = " <> show outerHeight
964+ (occ, innerHeight, innerSubd) <- subscribeCoincidenceInner a outerHeight subscribed
965+ tracePropagate $ " isJust occ = " <> show (isJust occ)
966+ tracePropagate $ " innerHeight = " <> show innerHeight
967+ liftIO $ writeIORef (coincidenceSubscribedInnerParent subscribed) $ Just innerSubd
968+ scheduleClear $ coincidenceSubscribedInnerParent subscribed
969+ case occ of
970+ Nothing -> do
971+ when (innerHeight > outerHeight) $ liftIO $ do -- If the event fires, it will fire at a later height
972+ writeIORef (coincidenceSubscribedHeight subscribed) innerHeight
973+ WeakBag.traverse (coincidenceSubscribedSubscribers subscribed) invalidateSubscriberHeight
974+ WeakBag.traverse (coincidenceSubscribedSubscribers subscribed) recalculateSubscriberHeight
975+ Just o -> do -- Since it's already firing, no need to adjust height
976+ liftIO $ writeIORef (coincidenceSubscribedOccurrence subscribed) occ
977+ scheduleClear $ coincidenceSubscribedOccurrence subscribed
978+ propagate o $ coincidenceSubscribedSubscribers subscribed
979+ SubscriberCoincidenceInner subscribed -> {-# SCC "traverseCoincidenceInner" #-} do
980+ tracePropagate $ "SubscriberCoincidenceInner" <> showNodeId subscribed
981+ occ <- liftIO $ readIORef $ coincidenceSubscribedOccurrence subscribed
982+ case occ of
983+ Just _ -> return () -- SubscriberCoincidenceOuter must have already propagated this event
984+ Nothing -> do
985+ liftIO $ writeIORef (coincidenceSubscribedOccurrence subscribed) $ Just a
986+ scheduleClear $ coincidenceSubscribedOccurrence subscribed
987+ propagate a $ coincidenceSubscribedSubscribers subscribed
988+ SubscriberHandle -> {-# SCC "traverseHandle" #-} tracePropagate "SubscriberHandle"
989+
990+{-# INLINE propagateSubscriberHold #-}
991+propagateSubscriberHold :: R.Patch p => Hold p a -> p a -> EventM x ()
992+propagateSubscriberHold h a = do
993+ {-# SCC "trace" #-} traceMWhen debugPropagate $ liftIO $ do
994+ invalidators <- liftIO $ readIORef $ holdInvalidators h
995+ return $ "SubscriberHold" <> showNodeId h <> ": " ++ show (length invalidators)
996+ v <- {-# SCC "read" #-} liftIO $ readIORef $ holdValue h
997+ case {-# SCC "apply" #-} R.apply a v of
998+ Nothing -> return ()
999+ Just v' -> do
1000+ {-# SCC "trace2" #-} withIncreasedDepth $ tracePropagate $ "propagateSubscriberHold: assigning Hold" <> showNodeId h
1001+ vRef <- {-# SCC "vRef" #-} liftIO $ evaluate $ holdValue h
1002+ iRef <- {-# SCC "iRef" #-} liftIO $ evaluate $ holdInvalidators h
1003+ defer $ {-# SCC "assignment" #-} SomeAssignment vRef iRef v'
1004+
1005+data SomeResetCoincidence = forall a. SomeResetCoincidence !(EventSubscription a) !(Maybe (CoincidenceSubscribed a)) -- The CoincidenceSubscriber will be present only if heights need to be reset
1006+
1007+{-# INLINE subscribeCoincidenceInner #-}
1008+subscribeCoincidenceInner :: CanSubscribe x m => Event a -> Height -> CoincidenceSubscribed a -> m (Maybe a, Height, EventSubscribed a)
1009+subscribeCoincidenceInner o outerHeight subscribedUnsafe = do
1010+ subInner <- liftIO $ newSubscriberCoincidenceInner subscribedUnsafe
1011+ subscription@(_, innerSubd) <- subscribe o subInner
1012+ innerOcc <- liftIO $ readEventSubscribed innerSubd
1013+ innerHeight <- liftIO $ readIORef $ eventSubscribedHeightRef innerSubd
1014+ let height = max innerHeight outerHeight
1015+ defer $ SomeResetCoincidence subscription $ if height > outerHeight then Just subscribedUnsafe else Nothing
1016+ return (innerOcc, height, innerSubd)
1017+
1018+{-# SPECIALIZE readBehaviorUntracked :: Behavior a -> BehaviorM a #-}
1019+{-# SPECIALIZE readBehaviorUntracked :: Behavior a -> EventM x a #-}
1020+readBehaviorUntracked :: Defer SomeHoldInit m => Behavior a -> m a
1021+readBehaviorUntracked b = do
1022+ holdInits <- getDeferralQueue
1023+ liftIO $ runBehaviorM (readBehaviorTracked b) Nothing holdInits --TODO: Specialize readBehaviorTracked to the Nothing and Just cases
1024+
1025+runBehaviorM :: BehaviorM a -> Maybe (Weak Invalidator, IORef [SomeBehaviorSubscribed]) -> IORef [SomeHoldInit] -> IO a
1026+runBehaviorM a mwi holdInits = runReaderT (unBehaviorM a) (mwi, holdInits)
1027+
1028+askInvalidator :: BehaviorM (Maybe (Weak Invalidator))
1029+askInvalidator = liftM (fmap fst . fst) $ BehaviorM ask
1030+
1031+askParentsRef :: BehaviorM (Maybe (IORef [SomeBehaviorSubscribed]))
1032+askParentsRef = liftM (fmap snd . fst) $ BehaviorM ask
1033+
1034+askBehaviorHoldInits :: BehaviorM (IORef [SomeHoldInit])
1035+askBehaviorHoldInits = liftM snd $ BehaviorM ask
1036+
1037+readBehaviorTracked :: Behavior a -> BehaviorM a
1038+readBehaviorTracked b = case b of
1039+ BehaviorHold h -> readHoldTracked h
1040+ BehaviorHoldIdentity h -> readHoldTracked h
1041+ BehaviorConst a -> return a
1042+ BehaviorPull p -> do
1043+ val <- liftIO $ readIORef $ pullValue p
1044+ case val of
1045+ Just subscribed -> do
1046+ askParentsRef >>= mapM_ (\r -> liftIO $ modifyIORef' r (SomeBehaviorSubscribed (BehaviorSubscribedPull subscribed) :))
1047+ askInvalidator >>= mapM_ (\wi -> liftIO $ modifyIORef' (pullSubscribedInvalidators subscribed) (wi:))
1048+ liftIO $ touch $ pullSubscribedOwnInvalidator subscribed
1049+ return $ pullSubscribedValue subscribed
1050+ Nothing -> do
1051+ i <- liftIO $ newInvalidatorPull p
1052+ wi <- liftIO $ mkWeakPtrWithDebug i "InvalidatorPull"
1053+ parentsRef <- liftIO $ newIORef []
1054+ holdInits <- askBehaviorHoldInits
1055+ a <- liftIO $ runReaderT (unBehaviorM $ pullCompute p) (Just (wi, parentsRef), holdInits)
1056+ invsRef <- liftIO . newIORef . maybeToList =<< askInvalidator
1057+ parents <- liftIO $ readIORef parentsRef
1058+ let subscribed = PullSubscribed
1059+ { pullSubscribedValue = a
1060+ , pullSubscribedInvalidators = invsRef
1061+ , pullSubscribedOwnInvalidator = i
1062+ , pullSubscribedParents = parents
1063+ }
1064+ liftIO $ writeIORef (pullValue p) $ Just subscribed
1065+ askParentsRef >>= mapM_ (\r -> liftIO $ modifyIORef' r (SomeBehaviorSubscribed (BehaviorSubscribedPull subscribed) :))
1066+ return a
1067+ BehaviorDyn d -> readHoldTracked =<< getDynHold d
1068+ BehaviorDynIdentity d -> readHoldTracked =<< getDynHold d
1069+
1070+{-# INLINE readHoldTracked #-}
1071+readHoldTracked :: Hold p a -> BehaviorM a
1072+readHoldTracked h = do
1073+ result <- liftIO $ readIORef $ holdValue h
1074+ askInvalidator >>= mapM_ (\wi -> liftIO $ modifyIORef' (holdInvalidators h) (wi:))
1075+ askParentsRef >>= mapM_ (\r -> liftIO $ modifyIORef' r (SomeBehaviorSubscribed (BehaviorSubscribedHold h) :))
1076+ return result
1077+
1078+{-# INLINE getDynHold #-}
1079+getDynHold :: (Defer SomeHoldInit m, R.Patch p) => Dyn p a -> m (Hold p a)
1080+getDynHold d = do
1081+ mh <- liftIO $ readIORef $ unDyn d
1082+ case mh of
1083+ Right h -> return h
1084+ Left (readV0, v') -> do
1085+ holdInits <- getDeferralQueue
1086+ v0 <- liftIO $ runBehaviorM readV0 Nothing holdInits
1087+ h <- hold v0 v'
1088+ liftIO $ writeIORef (unDyn d) $ Right h
1089+ return h
1090+
1091+{-
1092+readEvent :: Event a -> ResultM (Maybe a)
1093+readEvent e = case e of
1094+ EventRoot k r -> liftIO . liftM (coerce . DMap.lookup k) . readIORef $ rootOccurrence r
1095+ EventNever -> return Nothing
1096+ EventPush p -> do
1097+ subscribed <- getPushSubscribed p
1098+ liftIO $ readPushSubscribed subscribed
1099+ EventMerge m -> do
1100+ subscribed <- getMergeSubscribed m
1101+ liftIO $ readMergeSubscribed subscribed
1102+ EventFan k f -> do
1103+ subscribed <- getFanSubscribed f
1104+ liftIO $ readFanSubscribed k subscribed
1105+ EventSwitch s -> do
1106+ subscribed <- getSwitchSubscribed s
1107+ liftIO $ readSwitchSubscribed subscribed
1108+ EventCoincidence c -> do
1109+ subscribed <- getCoincidenceSubscribed c
1110+ liftIO $ readCoincidenceSubscribed subscribed
1111+ EventHold h -> liftIO . readEventSubscribed =<< getHoldEventSubscription h
1112+ EventDyn d -> liftIO . readEventSubscribed =<< getHoldEventSubscription =<< getDynHold d
1113+ EventHoldIdentity h -> liftIO . readEventSubscribed =<< getHoldEventSubscription h
1114+ EventDynIdentity d -> liftIO . readEventSubscribed =<< getHoldEventSubscription =<< getDynHold d
1115+-}
1116+
1117+readEventSubscribed :: EventSubscribed a -> IO (Maybe a)
1118+readEventSubscribed = \case
1119+ EventSubscribedRoot rs -> readRootSubscribed rs
1120+ EventSubscribedNever -> return Nothing
1121+ EventSubscribedPush ps -> readPushSubscribed ps
1122+ EventSubscribedMerge ms -> readMergeSubscribed ms
1123+ EventSubscribedFan k fs -> readFanSubscribed k fs
1124+ EventSubscribedSwitch ss -> readSwitchSubscribed ss
1125+ EventSubscribedCoincidence cs -> readCoincidenceSubscribed cs
1126+
1127+readRootSubscribed :: RootSubscribed a -> IO (Maybe a)
1128+readRootSubscribed = rootSubscribedOccurrence
1129+
1130+readPushSubscribed :: PushSubscribed b a -> IO (Maybe a)
1131+readPushSubscribed subscribed = do
1132+ result <- readIORef $ pushSubscribedOccurrence subscribed -- Since ResultM is always called after the final height is reached, this will always be valid
1133+ touch $ pushSubscribedSelf subscribed
1134+ return result
1135+
1136+readMergeSubscribed :: MergeSubscribed k -> IO (Maybe (DMap k Identity))
1137+readMergeSubscribed subscribed = do
1138+ result <- readIORef $ mergeSubscribedOccurrence subscribed
1139+ touch $ mergeSubscribedParents subscribed
1140+ return result
1141+
1142+readFanSubscribed :: GCompare k => k a -> FanSubscribed k -> IO (Maybe a)
1143+readFanSubscribed k fs = do
1144+ parentOcc <- readEventSubscribed $ snd $ fanSubscribedParent fs
1145+ return $ coerce $ DMap.lookup k =<< parentOcc
1146+
1147+readSwitchSubscribed :: SwitchSubscribed a -> IO (Maybe a)
1148+readSwitchSubscribed subscribed = do
1149+ result <- readIORef $ switchSubscribedOccurrence subscribed
1150+ touch $ switchSubscribedOwnInvalidator subscribed
1151+ return result
1152+
1153+readCoincidenceSubscribed :: CoincidenceSubscribed a -> IO (Maybe a)
1154+readCoincidenceSubscribed subscribed = do
1155+ result <- readIORef $ coincidenceSubscribedOccurrence subscribed
1156+ touch $ coincidenceSubscribedOuter subscribed
1157+ --TODO: do we need to touch the inner subscriber?
1158+ return result
1159+
1160+-- Always refers to 0
1161+{-# NOINLINE zeroRef #-}
1162+zeroRef :: IORef Height
1163+zeroRef = unsafePerformIO $ newIORef zeroHeight
1164+
1165+{-# SPECIALIZE getEventSubscribed :: Event a -> Subscriber a -> EventM x (EventSubscription a) #-}
1166+getEventSubscribed :: forall x m a. CanSubscribe x m => Event a -> Subscriber a -> m (EventSubscription a)
1167+getEventSubscribed e sub = case e of
1168+ EventRoot k r -> wrap EventSubscribedRoot $ liftIO . getRootSubscribed k r
1169+ EventNever -> return (Nothing, EventSubscribedNever)
1170+ EventPush p -> wrap EventSubscribedPush $ getPushSubscribed p
1171+ EventFan k f -> wrap (EventSubscribedFan k) $ getFanSubscribed k f
1172+ EventMerge m -> wrap EventSubscribedMerge $ getMergeSubscribed m
1173+ EventSwitch s -> wrap EventSubscribedSwitch $ getSwitchSubscribed s
1174+ EventCoincidence c -> wrap EventSubscribedCoincidence $ getCoincidenceSubscribed c
1175+ EventHold h -> subscribeHoldEvent h sub
1176+ EventDyn j -> getDynHold j >>= \h -> subscribeHoldEvent h sub
1177+ EventHoldIdentity h -> subscribeHoldEvent h sub
1178+ EventDynIdentity j -> getDynHold j >>= \h -> subscribeHoldEvent h sub
1179+ where
1180+ wrap :: (t -> EventSubscribed a) -> (Subscriber a -> m (SubscriberListTicket a, t)) -> m (EventSubscription a)
1181+ wrap tag getSpecificSubscribed = do
1182+ (sln, subd) <- getSpecificSubscribed sub
1183+ return (Just sln, tag subd)
1184+
1185+{-# INLINE subscribeHoldEvent #-}
1186+subscribeHoldEvent :: (CanSubscribe x m, R.Patch p) => Hold p a -> Subscriber (p a) -> m (EventSubscription (p a))
1187+subscribeHoldEvent h sub = do
1188+ (_, subd) <- getHoldEventSubscription h
1189+ sln <- liftIO $ subscribeEventSubscribed subd sub
1190+ return (sln, subd)
1191+
1192+{-# INLINE debugSubscribe #-}
1193+debugSubscribe :: Bool
1194+debugSubscribe = False
1195+
1196+{-# INLINE subscribeEventSubscribed #-}
1197+subscribeEventSubscribed :: EventSubscribed a -> Subscriber a -> IO (Maybe (SubscriberListTicket a))
1198+subscribeEventSubscribed es sub = case es of
1199+ EventSubscribedRoot r -> do
1200+ traceSubscribe $ "subscribeEventSubscribed Root"
1201+ fmap Just $ subscribeRootSubscribed r sub
1202+ EventSubscribedNever -> do
1203+ traceSubscribe $ "subscribeEventSubscribed Never"
1204+ return Nothing
1205+ EventSubscribedPush subscribed -> do
1206+ traceSubscribe $ "subscribeEventSubscribed Push"
1207+ fmap Just $ subscribePushSubscribed subscribed sub
1208+ EventSubscribedFan k subscribed -> do
1209+ traceSubscribe $ "subscribeEventSubscribed Fan"
1210+ fmap Just $ subscribeFanSubscribed k subscribed sub
1211+ EventSubscribedMerge subscribed -> do
1212+ traceSubscribe $ "subscribeEventSubscribed Merge"
1213+ fmap Just $ subscribeMergeSubscribed subscribed sub
1214+ EventSubscribedSwitch subscribed -> do
1215+ traceSubscribe $ "subscribeEventSubscribed Switch"
1216+ fmap Just $ subscribeSwitchSubscribed subscribed sub
1217+ EventSubscribedCoincidence subscribed -> do
1218+ traceSubscribe $ "subscribeEventSubscribed Coincidence"
1219+ fmap Just $ subscribeCoincidenceSubscribed subscribed sub
1220+
1221+{-# INLINE eventSubscribedHeightRef #-}
1222+eventSubscribedHeightRef :: EventSubscribed a -> IORef Height
1223+eventSubscribedHeightRef = \case
1224+ EventSubscribedRoot _ -> zeroRef
1225+ EventSubscribedNever -> zeroRef
1226+ EventSubscribedPush subscribed -> pushSubscribedHeight subscribed
1227+ EventSubscribedFan _ subscribed -> eventSubscribedHeightRef $ snd $ fanSubscribedParent subscribed
1228+ EventSubscribedMerge subscribed -> mergeSubscribedHeight subscribed
1229+ EventSubscribedSwitch subscribed -> switchSubscribedHeight subscribed
1230+ EventSubscribedCoincidence subscribed -> coincidenceSubscribedHeight subscribed
1231+
1232+{-# INLINE eventSubscribedHasOwnHeightRef #-}
1233+eventSubscribedHasOwnHeightRef :: EventSubscribed a -> Bool
1234+eventSubscribedHasOwnHeightRef = \case
1235+ EventSubscribedRoot _ -> False
1236+ EventSubscribedNever -> False
1237+ EventSubscribedPush _ -> False
1238+ EventSubscribedFan _ _ -> False
1239+ EventSubscribedMerge _ -> True
1240+ EventSubscribedSwitch _ -> True
1241+ EventSubscribedCoincidence _ -> True
1242+
1243+{-# INLINE eventSubscribedGetParents #-}
1244+eventSubscribedGetParents :: EventSubscribed a -> IO [Some EventSubscribed]
1245+eventSubscribedGetParents = \case
1246+ EventSubscribedRoot _ -> return []
1247+ EventSubscribedNever -> return []
1248+ EventSubscribedPush subscribed -> return [Some.This $ snd $ pushSubscribedParent subscribed]
1249+ EventSubscribedFan _ subscribed -> return [Some.This $ snd $ fanSubscribedParent subscribed]
1250+ EventSubscribedMerge subscribed -> do
1251+ ps <- readIORef $ mergeSubscribedParents subscribed
1252+ return $ fmap (\(_ :=> v) -> Some.This $ snd $ mergeSubscribedParentSubscription v) $ DMap.toList ps
1253+ EventSubscribedSwitch subscribed -> do
1254+ s <- readIORef $ switchSubscribedCurrentParent subscribed
1255+ return [Some.This $ snd s]
1256+ EventSubscribedCoincidence subscribed -> do
1257+ innerSubscription <- readIORef $ coincidenceSubscribedInnerParent subscribed
1258+ let outerParent = Some.This $ snd $ coincidenceSubscribedOuterParent subscribed
1259+ innerParents = maybeToList $ fmap Some.This innerSubscription
1260+ return $ outerParent : innerParents
1261+
1262+type CanSubscribe x m =
1263+ ( HasSpiderEnv x m
1264+ , HasCurrentHeight m
1265+ , Defer SomeHoldInit m, Defer SomeMergeInit m -- The things we need to lazily initialize state
1266+ , Defer SomeClear m -- Wiping out event firings
1267+ , Defer SomeAssignment m -- Updating state
1268+ , Defer SomeResetCoincidence m
1269+ )
1270+
1271+{-# INLINE subscribe #-}
1272+subscribe :: CanSubscribe x m => Event a -> Subscriber a -> m (EventSubscription a)
1273+subscribe = getEventSubscribed
1274+
1275+getRootSubscribed :: GCompare k => k a -> Root k -> Subscriber a -> IO (SubscriberListTicket a, RootSubscribed a)
1276+getRootSubscribed k r sub = do
1277+ mSubscribed <- readIORef $ rootSubscribed r
1278+ case DMap.lookup k mSubscribed of
1279+ Just subscribed -> {-# SCC "hitRoot" #-} do
1280+ sln <- subscribeRootSubscribed subscribed sub
1281+ return (sln, subscribed)
1282+ Nothing -> {-# SCC "missRoot" #-} do
1283+ weakSelf <- newIORef $ error "getRootSubscribed: weakSelfRef not initialized"
1284+ let !cached = rootSubscribed r
1285+ uninitRef <- newIORef $ error "getRootsubscribed: uninitRef not initialized"
1286+ (subs, sln) <- WeakBag.singleton sub weakSelf cleanupRootSubscribed
1287+ when debugPropagate $ putStrLn $ "getRootSubscribed: calling rootInit"
1288+ uninit <- rootInit r k $ RootTrigger (subs, rootOccurrence r, k)
1289+ writeIORef uninitRef uninit
1290+ let subscribed = RootSubscribed
1291+ { rootSubscribedKey = k
1292+ , rootSubscribedCachedSubscribed = cached
1293+ , rootSubscribedOccurrence = liftM (coerce . DMap.lookup k) $ readIORef $ rootOccurrence r
1294+ , rootSubscribedSubscribers = subs
1295+ , rootSubscribedUninit = uninit
1296+ , rootSubscribedWeakSelf = weakSelf
1297+#ifdef DEBUG_NODEIDS
1298+ , rootSubscribedNodeId = unsafeNodeId (k, r, subs)
1299+#endif
1300+ }
1301+ writeIORef weakSelf =<< mkWeakPtrWithDebug subscribed "RootSubscribed"
1302+ modifyIORef' (rootSubscribed r) $ DMap.insertWith (error $ "getRootSubscribed: duplicate key inserted into Root" <> showNodeId r) k subscribed --TODO: I think we can just write back mSubscribed rather than re-reading it
1303+ return (sln, subscribed)
1304+
1305+cleanupRootSubscribed :: RootSubscribed a -> IO ()
1306+cleanupRootSubscribed self@(RootSubscribed { rootSubscribedKey = k, rootSubscribedCachedSubscribed = cached }) = do
1307+ rootSubscribedUninit self
1308+ modifyIORef' cached $ DMap.delete k
1309+
1310+{-# INLINE subscribeRootSubscribed #-}
1311+subscribeRootSubscribed :: RootSubscribed a -> Subscriber a -> IO (SubscriberListTicket a)
1312+subscribeRootSubscribed subscribed sub = WeakBag.insert sub (rootSubscribedSubscribers subscribed) (rootSubscribedWeakSelf subscribed) cleanupRootSubscribed
1313+
1314+-- When getPushSubscribed returns, the PushSubscribed returned will have a fully filled-in
1315+{-# SPECIALIZE getPushSubscribed :: Push a b -> Subscriber b -> EventM x (SubscriberListTicket b, PushSubscribed a b) #-}
1316+getPushSubscribed :: CanSubscribe x m => Push a b -> Subscriber b -> m (SubscriberListTicket b, PushSubscribed a b)
1317+getPushSubscribed p sub = do
1318+ mSubscribed <- liftIO $ readIORef $ pushSubscribed p
1319+ case mSubscribed of
1320+ Just subscribed -> {-# SCC "hitPush" #-} do
1321+ sln <- liftIO $ subscribePushSubscribed subscribed sub
1322+ return (sln, subscribed)
1323+ Nothing -> {-# SCC "missPush" #-} do -- Not yet subscribed
1324+ subscribedRef <- liftIO $ newIORef $ error "getPushSubscribed: subscribed not yet intialized"
1325+ subscribedUnsafe <- liftIO $ unsafeInterleaveIO $ readIORef subscribedRef
1326+ s <- liftIO $ newSubscriberPush (pushCompute p) subscribedUnsafe
1327+ subscription@(_, subd) <- subscribe (pushParent p) s
1328+ parentOcc <- liftIO $ readEventSubscribed subd
1329+ occ <- runComputeM $ liftM join $ mapM (pushCompute p) parentOcc
1330+ occRef <- liftIO $ newIORef occ
1331+ when (isJust occ) $ scheduleClear occRef
1332+ weakSelf <- liftIO $ newIORef $ error "getPushSubscribed: weakSelf not initialized"
1333+ let !cached = pushSubscribed p
1334+ (subs, sln) <- liftIO $ WeakBag.singleton sub weakSelf cleanupPushSubscribed
1335+ let subscribed = PushSubscribed
1336+ { pushSubscribedCachedSubscribed = cached
1337+ , pushSubscribedOccurrence = occRef
1338+ , pushSubscribedHeight = eventSubscribedHeightRef subd -- Since pushes have the same height as their parents, share the ref
1339+ , pushSubscribedSubscribers = subs
1340+ , pushSubscribedSelf = s
1341+ , pushSubscribedWeakSelf = weakSelf
1342+ , pushSubscribedParent = subscription
1343+#ifdef DEBUG_NODEIDS
1344+ , pushSubscribedNodeId = unsafeNodeId p
1345+#endif
1346+ }
1347+ liftIO $ writeIORef weakSelf =<< mkWeakPtrWithDebug subscribed "PushSubscribed"
1348+ liftIO $ writeIORef subscribedRef subscribed
1349+ liftIO $ writeIORef (pushSubscribed p) $ Just subscribed
1350+ return (sln, subscribed)
1351+
1352+cleanupPushSubscribed :: PushSubscribed a b -> IO ()
1353+cleanupPushSubscribed self = do
1354+ unsubscribe $ pushSubscribedParent self
1355+ writeIORef (pushSubscribedCachedSubscribed self) Nothing
1356+
1357+{-# INLINE subscribePushSubscribed #-}
1358+subscribePushSubscribed :: PushSubscribed a b -> Subscriber b -> IO (SubscriberListTicket b)
1359+subscribePushSubscribed subscribed sub = WeakBag.insert sub (pushSubscribedSubscribers subscribed) (pushSubscribedWeakSelf subscribed) cleanupPushSubscribed
1360+
1361+{-# SPECIALIZE getMergeSubscribed :: GCompare k => Merge k -> Subscriber (DMap k Identity) -> EventM x (SubscriberListTicket (DMap k Identity), MergeSubscribed k) #-}
1362+getMergeSubscribed :: forall x k m. (CanSubscribe x m, GCompare k) => Merge k -> Subscriber (DMap k Identity) -> m (SubscriberListTicket (DMap k Identity), MergeSubscribed k)
1363+getMergeSubscribed m sub = do
1364+ mSubscribed <- liftIO $ readIORef $ mergeSubscribed m
1365+ case mSubscribed of
1366+ Just subscribed -> {-# SCC "hitMerge" #-} do
1367+ sln <- liftIO $ subscribeMergeSubscribed subscribed sub
1368+ return (sln, subscribed)
1369+ Nothing -> {-# SCC "missMerge" #-} do
1370+ subscribedRef <- liftIO $ newIORef $ error "getMergeSubscribed: subscribedRef not yet initialized"
1371+ subscribedUnsafe <- liftIO $ unsafeInterleaveIO $ readIORef subscribedRef
1372+ initialParents <- readBehaviorUntracked $ current $ mergeParents m
1373+ subscribers :: [(Maybe (DSum k Identity), Height, DSum k (MergeSubscribedParent k))] <- forM (DMap.toList initialParents) $ \(k :=> e) -> do
1374+ s <- liftIO $ newSubscriberMerge k subscribedUnsafe
1375+ subscription@(_, parentSubd) <- subscribe e s
1376+ parentOcc <- liftIO $ readEventSubscribed parentSubd
1377+ height <- liftIO $ readIORef $ eventSubscribedHeightRef parentSubd
1378+ return $ (fmap (\x -> k :=> Identity x) parentOcc, height, k :=> (MergeSubscribedParent subscription s))
1379+ let dm = DMap.fromDistinctAscList $ catMaybes $ map (\(x, _, _) -> x) subscribers
1380+ subscriberHeights = map (\(_, x, _) -> x) subscribers
1381+ myHeight = succHeight $ foldr maxHeight zeroHeight subscriberHeights
1382+ currentHeight <- getCurrentHeight
1383+ let (occ, accum) = if currentHeight >= myHeight -- If we should have fired by now
1384+ then (if DMap.null dm then Nothing else Just dm, DMap.empty)
1385+ else (Nothing, dm)
1386+ when (not $ DMap.null accum) $ do
1387+ scheduleMerge myHeight subscribedUnsafe
1388+ occRef <- liftIO $ newIORef occ
1389+ when (isJust occ) $ scheduleClear occRef
1390+ accumRef <- liftIO $ newIORef accum
1391+ heightRef <- liftIO $ newIORef myHeight
1392+ weakSelf <- liftIO $ newIORef $ error "getMergeSubscribed: weakSelf not yet initialized"
1393+ (subs, sln) <- liftIO $ WeakBag.singleton sub weakSelf cleanupMergeSubscribed
1394+ changeSubdRef <- liftIO $ newIORef $ error "getMergeSubscribed: changeSubdRef not yet initialized"
1395+ parentsRef <- liftIO $ newIORef $ DMap.fromDistinctAscList $ map (\(_, _, x) -> x) subscribers
1396+ let !subscribed = MergeSubscribed
1397+ { mergeSubscribedCachedSubscribed = mergeSubscribed m
1398+ , mergeSubscribedOccurrence = occRef
1399+ , mergeSubscribedAccum = accumRef
1400+ , mergeSubscribedHeight = heightRef
1401+ , mergeSubscribedSubscribers = subs
1402+ , mergeSubscribedChange = changeSubdRef
1403+ , mergeSubscribedParents = parentsRef
1404+ , mergeSubscribedWeakSelf = weakSelf
1405+#ifdef DEBUG_NODEIDS
1406+ , mergeSubscribedNodeId = unsafeNodeId m
1407+#endif
1408+ }
1409+ defer $ SomeMergeInit subscribed $ updated $ mergeParents m
1410+ liftIO $ writeIORef subscribedRef subscribed
1411+ liftIO $ writeIORef weakSelf =<< mkWeakPtrWithDebug subscribed "MergeSubscribed"
1412+ liftIO $ writeIORef (mergeSubscribed m) $ Just subscribed
1413+ return (sln, subscribed)
1414+
1415+cleanupMergeSubscribed :: MergeSubscribed k -> IO ()
1416+cleanupMergeSubscribed subscribed = do
1417+ parents <- readIORef $ mergeSubscribedParents subscribed
1418+ forM_ (DMap.toList parents) $ \(_ :=> msp) -> unsubscribe $ mergeSubscribedParentSubscription msp
1419+ -- Not necessary, because this whole MergeSubscribed is dead: writeIORef (mergeSubscribedParents subscribed) DMap.empty
1420+ writeIORef (mergeSubscribedCachedSubscribed subscribed) Nothing -- Get rid of the cached subscribed
1421+
1422+{-# INLINE subscribeMergeSubscribed #-}
1423+subscribeMergeSubscribed :: MergeSubscribed k -> Subscriber (DMap k Identity) -> IO (SubscriberListTicket (DMap k Identity))
1424+subscribeMergeSubscribed subscribed sub = WeakBag.insert sub (mergeSubscribedSubscribers subscribed) (mergeSubscribedWeakSelf subscribed) cleanupMergeSubscribed
1425+
1426+{-# SPECIALIZE getFanSubscribed :: GCompare k => k a -> Fan k -> Subscriber a -> EventM x (SubscriberListTicket a, FanSubscribed k) #-}
1427+getFanSubscribed :: (Defer SomeAssignment m, Defer SomeHoldInit m, Defer SomeMergeInit m, Defer SomeClear m, HasCurrentHeight m, Defer SomeResetCoincidence m, HasSpiderEnv x m, GCompare k) => k a -> Fan k -> Subscriber a -> m (SubscriberListTicket a, FanSubscribed k)
1428+getFanSubscribed k f sub = do
1429+ mSubscribed <- liftIO $ readIORef $ fanSubscribed f
1430+ case mSubscribed of
1431+ Just subscribed -> {-# SCC "hitFan" #-} do
1432+ sln <- liftIO $ subscribeFanSubscribed k subscribed sub
1433+ return (sln, subscribed)
1434+ Nothing -> {-# SCC "missFan" #-} do
1435+ subscribedRef <- liftIO $ newIORef $ error "getFanSubscribed: subscribedRef not yet initialized"
1436+ subscribedUnsafe <- liftIO $ unsafeInterleaveIO $ readIORef subscribedRef
1437+ s <- liftIO $ newSubscriberFan subscribedUnsafe
1438+ subscription <- subscribe (fanParent f) s
1439+ weakSelf <- liftIO $ newIORef $ error "getFanSubscribed: weakSelf not yet initialized"
1440+ (subsForK, slnForSub) <- liftIO $ WeakBag.singleton sub weakSelf cleanupFanSubscribed
1441+ subscribersRef <- liftIO $ newIORef $ DMap.singleton k $ FanSubscribedChildren subsForK weakSelf
1442+ let subscribed = FanSubscribed
1443+ { fanSubscribedCachedSubscribed = fanSubscribed f
1444+ , fanSubscribedParent = subscription
1445+ , fanSubscribedSubscribers = subscribersRef
1446+#ifdef DEBUG_NODEIDS
1447+ , fanSubscribedNodeId = unsafeNodeId f
1448+#endif
1449+ }
1450+ liftIO $ writeIORef weakSelf =<< mkWeakPtrWithDebug (k, subscribed) "FanSubscribed"
1451+ liftIO $ writeIORef subscribedRef subscribed
1452+ liftIO $ writeIORef (fanSubscribed f) $ Just subscribed
1453+ return (slnForSub, subscribed)
1454+
1455+cleanupFanSubscribed :: GCompare k => (k a, FanSubscribed k) -> IO ()
1456+cleanupFanSubscribed (k, subscribed) = do
1457+ subscribers <- readIORef $ fanSubscribedSubscribers subscribed
1458+ let reducedSubscribers = DMap.delete k subscribers
1459+ if DMap.null reducedSubscribers
1460+ then do
1461+ unsubscribe $ fanSubscribedParent subscribed
1462+ -- Not necessary in this case, because this whole FanSubscribed is dead: writeIORef (fanSubscribedSubscribers subscribed) reducedSubscribers
1463+ writeIORef (fanSubscribedCachedSubscribed subscribed) Nothing
1464+ else writeIORef (fanSubscribedSubscribers subscribed) reducedSubscribers
1465+
1466+{-# INLINE subscribeFanSubscribed #-}
1467+subscribeFanSubscribed :: GCompare k => k a -> FanSubscribed k -> Subscriber a -> IO (SubscriberListTicket a)
1468+subscribeFanSubscribed k subscribed sub = do
1469+ subscribers <- readIORef $ fanSubscribedSubscribers subscribed
1470+ case DMap.lookup k subscribers of
1471+ Nothing -> do
1472+ weakSelf <- newIORef =<< mkWeakPtrWithDebug (k, subscribed) "FanSubscribed"
1473+ (list, sln) <- WeakBag.singleton sub weakSelf cleanupFanSubscribed
1474+ writeIORef (fanSubscribedSubscribers subscribed) $! DMap.insertWith (error "subscribeEventSubscribed: key that we just failed to find is present - should be impossible") k (FanSubscribedChildren list weakSelf) subscribers
1475+ return sln
1476+ Just (FanSubscribedChildren list weakSelf) -> WeakBag.insert sub list weakSelf cleanupFanSubscribed
1477+
1478+{-# SPECIALIZE getSwitchSubscribed :: Switch a -> Subscriber a -> EventM x (SubscriberListTicket a, SwitchSubscribed a) #-}
1479+getSwitchSubscribed :: (Defer SomeHoldInit m, Defer SomeAssignment m, Defer SomeMergeInit m, Defer SomeClear m, Defer SomeResetCoincidence m, HasCurrentHeight m, HasSpiderEnv x m) => Switch a -> Subscriber a -> m (SubscriberListTicket a, SwitchSubscribed a)
1480+getSwitchSubscribed s sub = do
1481+ mSubscribed <- liftIO $ readIORef $ switchSubscribed s
1482+ case mSubscribed of
1483+ Just subscribed -> {-# SCC "hitSwitch" #-} do
1484+ sln <- liftIO $ subscribeSwitchSubscribed subscribed sub
1485+ return (sln, subscribed)
1486+ Nothing -> {-# SCC "missSwitch" #-} do
1487+ subscribedRef <- liftIO $ newIORef $ error "getSwitchSubscribed: subscribed has not yet been created"
1488+ subscribedUnsafe <- liftIO $ unsafeInterleaveIO $ readIORef subscribedRef
1489+ i <- liftIO $ newInvalidatorSwitch subscribedUnsafe
1490+ mySub <- liftIO $ newSubscriberSwitch subscribedUnsafe
1491+ wi <- liftIO $ mkWeakPtrWithDebug i "InvalidatorSwitch"
1492+ wiRef <- liftIO $ newIORef wi
1493+ parentsRef <- liftIO $ newIORef [] --TODO: This should be unnecessary, because it will always be filled with just the single parent behavior
1494+ holdInits <- getDeferralQueue
1495+ e <- liftIO $ runBehaviorM (readBehaviorTracked (switchParent s)) (Just (wi, parentsRef)) holdInits
1496+ subscription@(_, subd) <- subscribe e mySub
1497+ subscriptionRef <- liftIO $ newIORef subscription
1498+ parentOcc <- liftIO $ readEventSubscribed subd
1499+ occRef <- liftIO $ newIORef parentOcc
1500+ when (isJust parentOcc) $ scheduleClear occRef
1501+ heightRef <- liftIO $ newIORef =<< readIORef (eventSubscribedHeightRef subd)
1502+ weakSelf <- liftIO $ newIORef $ error "getSwitchSubscribed: weakSelf not yet initialized"
1503+ (subs, slnForSub) <- liftIO $ WeakBag.singleton sub weakSelf cleanupSwitchSubscribed
1504+ let !subscribed = SwitchSubscribed
1505+ { switchSubscribedCachedSubscribed = switchSubscribed s
1506+ , switchSubscribedOccurrence = occRef
1507+ , switchSubscribedHeight = heightRef
1508+ , switchSubscribedSubscribers = subs
1509+ , switchSubscribedOwnInvalidator = i
1510+ , switchSubscribedOwnWeakInvalidator = wiRef
1511+ , switchSubscribedBehaviorParents = parentsRef
1512+ , switchSubscribedParent = switchParent s
1513+ , switchSubscribedCurrentParent = subscriptionRef
1514+ , switchSubscribedWeakSelf = weakSelf
1515+#ifdef DEBUG_NODEIDS
1516+ , switchSubscribedNodeId = unsafeNodeId s
1517+#endif
1518+ }
1519+ liftIO $ writeIORef weakSelf =<< mkWeakPtrWithDebug subscribed "switchSubscribedWeakSelf"
1520+ liftIO $ writeIORef subscribedRef subscribed
1521+ liftIO $ writeIORef (switchSubscribed s) $ Just subscribed
1522+ return (slnForSub, subscribed)
1523+
1524+cleanupSwitchSubscribed :: SwitchSubscribed a -> IO ()
1525+cleanupSwitchSubscribed subscribed = do
1526+ unsubscribe =<< readIORef (switchSubscribedCurrentParent subscribed)
1527+ finalize =<< readIORef (switchSubscribedOwnWeakInvalidator subscribed) -- We don't need to get invalidated if we're dead
1528+ writeIORef (switchSubscribedCachedSubscribed subscribed) Nothing
1529+
1530+{-# INLINE subscribeSwitchSubscribed #-}
1531+subscribeSwitchSubscribed :: SwitchSubscribed a -> Subscriber a -> IO (SubscriberListTicket a)
1532+subscribeSwitchSubscribed subscribed sub = WeakBag.insert sub (switchSubscribedSubscribers subscribed) (switchSubscribedWeakSelf subscribed) cleanupSwitchSubscribed
1533+
1534+{-# SPECIALIZE getCoincidenceSubscribed :: Coincidence a -> Subscriber a -> EventM x (SubscriberListTicket a, CoincidenceSubscribed a) #-}
1535+getCoincidenceSubscribed :: forall x a m. (Defer SomeAssignment m, Defer SomeHoldInit m, Defer SomeMergeInit m, Defer SomeClear m, HasCurrentHeight m, Defer SomeResetCoincidence m, HasSpiderEnv x m) => Coincidence a -> Subscriber a -> m (SubscriberListTicket a, CoincidenceSubscribed a)
1536+getCoincidenceSubscribed c sub = do
1537+ mSubscribed <- liftIO $ readIORef $ coincidenceSubscribed c
1538+ case mSubscribed of
1539+ Just subscribed -> {-# SCC "hitCoincidence" #-} do
1540+ sln <- liftIO $ subscribeCoincidenceSubscribed subscribed sub
1541+ return (sln, subscribed)
1542+ Nothing -> {-# SCC "missCoincidence" #-} do
1543+ subscribedRef <- liftIO $ newIORef $ error "getCoincidenceSubscribed: subscribed has not yet been created"
1544+ subscribedUnsafe <- liftIO $ unsafeInterleaveIO $ readIORef subscribedRef
1545+ subOuter <- liftIO $ newSubscriberCoincidenceOuter subscribedUnsafe
1546+ outerSubscription@(_, outerSubd) <- subscribe (coincidenceParent c) subOuter
1547+ outerOcc <- liftIO $ readEventSubscribed outerSubd
1548+ outerHeight <- liftIO $ readIORef $ eventSubscribedHeightRef outerSubd
1549+ (occ, height, mInnerSubd) <- case outerOcc of
1550+ Nothing -> return (Nothing, outerHeight, Nothing)
1551+ Just o -> do
1552+ (occ, height, innerSubd) <- subscribeCoincidenceInner o outerHeight subscribedUnsafe
1553+ return (occ, height, Just innerSubd)
1554+ occRef <- liftIO $ newIORef occ
1555+ when (isJust occ) $ scheduleClear occRef
1556+ heightRef <- liftIO $ newIORef height
1557+ innerSubdRef <- liftIO $ newIORef mInnerSubd
1558+ scheduleClear innerSubdRef
1559+ weakSelf <- liftIO $ newIORef $ error "getCoincidenceSubscribed: weakSelf not yet implemented"
1560+ (subs, slnForSub) <- liftIO $ WeakBag.singleton sub weakSelf cleanupCoincidenceSubscribed
1561+ let subscribed = CoincidenceSubscribed
1562+ { coincidenceSubscribedCachedSubscribed = coincidenceSubscribed c
1563+ , coincidenceSubscribedOccurrence = occRef
1564+ , coincidenceSubscribedHeight = heightRef
1565+ , coincidenceSubscribedSubscribers = subs
1566+ , coincidenceSubscribedOuter = subOuter
1567+ , coincidenceSubscribedOuterParent = outerSubscription
1568+ , coincidenceSubscribedInnerParent = innerSubdRef
1569+ , coincidenceSubscribedWeakSelf = weakSelf
1570+#ifdef DEBUG_NODEIDS
1571+ , coincidenceSubscribedNodeId = unsafeNodeId c
1572+#endif
1573+ }
1574+ liftIO $ writeIORef weakSelf =<< mkWeakPtrWithDebug subscribed "CoincidenceSubscribed"
1575+ liftIO $ writeIORef subscribedRef subscribed
1576+ liftIO $ writeIORef (coincidenceSubscribed c) $ Just subscribed
1577+ return (slnForSub, subscribed)
1578+
1579+cleanupCoincidenceSubscribed :: CoincidenceSubscribed a -> IO ()
1580+cleanupCoincidenceSubscribed subscribed = do
1581+ unsubscribe $ coincidenceSubscribedOuterParent subscribed
1582+ writeIORef (coincidenceSubscribedCachedSubscribed subscribed) Nothing
1583+
1584+{-# INLINE subscribeCoincidenceSubscribed #-}
1585+subscribeCoincidenceSubscribed :: CoincidenceSubscribed a -> Subscriber a -> IO (SubscriberListTicket a)
1586+subscribeCoincidenceSubscribed subscribed sub = WeakBag.insert sub (coincidenceSubscribedSubscribers subscribed) (coincidenceSubscribedWeakSelf subscribed) cleanupCoincidenceSubscribed
1587+
1588+{-# INLINE merge #-}
1589+merge :: GCompare k => Dynamic R.PatchDMap (DMap k Event) -> Event (DMap k Identity)
1590+merge m = EventMerge $ Merge
1591+ { mergeParents = m
1592+ , mergeSubscribed = unsafeNewIORef m Nothing
1593+ }
1594+
1595+newtype EventSelector k = EventSelector { select :: forall a. k a -> Event a }
1596+
1597+fan :: GCompare k => Event (DMap k Identity) -> EventSelector k
1598+fan e =
1599+ let f = Fan
1600+ { fanParent = e
1601+ , fanSubscribed = unsafeNewIORef e Nothing
1602+ }
1603+ in EventSelector $ \k -> EventFan k f
1604+
1605+{- SPECIALIZE runHoldInits :: IORef [SomeHoldInit] -> IORef [SomeMergeInit] -> EventM x () #-}
1606+--runHoldInits :: (CanSubscribe x m, Defer SomeMergeUpdate m) => IORef [SomeHoldInit] -> IORef [SomeMergeInit] -> m ()
1607+runHoldInits :: IORef [SomeHoldInit] -> IORef [SomeMergeInit] -> EventM x ()
1608+runHoldInits holdInitRef mergeInitRef = do
1609+ holdInits <- liftIO $ readIORef holdInitRef
1610+ mergeInits <- liftIO $ readIORef mergeInitRef
1611+ if null holdInits && null mergeInits then return () else do
1612+ liftIO $ writeIORef holdInitRef []
1613+ liftIO $ writeIORef mergeInitRef []
1614+ mapM_ initHold holdInits
1615+ mapM_ initMerge mergeInits
1616+ runHoldInits holdInitRef mergeInitRef
1617+
1618+{- SPECIALIZE initHold :: SomeHoldInit -> EventM x () #-}
1619+--initHold :: CanSubscribe x m => SomeHoldInit -> m ()
1620+initHold :: SomeHoldInit -> EventM x ()
1621+initHold (SomeHoldInit h) = void $ getHoldEventSubscription h
1622+
1623+{- SPECIALIZE initMerge :: SomeMergeInit -> EventM x () #-}
1624+--initMerge :: (CanSubscribe x m, Defer SomeMergeUpdate m) => SomeMergeInit -> m ()
1625+initMerge :: SomeMergeInit -> EventM x ()
1626+initMerge (SomeMergeInit subscribed changed) = do
1627+ changeSub <- liftIO $ newSubscriberMergeChange subscribed
1628+ (_, changeSubd) <- subscribe changed changeSub
1629+ change <- liftIO $ readEventSubscribed changeSubd
1630+ forM_ change $ \c -> defer $ SomeMergeUpdate c subscribed
1631+ liftIO $ writeIORef (mergeSubscribedChange subscribed) (changeSub, changeSubd)
1632+
1633+-- | Run an event action outside of a frame
1634+runFrame :: EventM x a -> SpiderHost x a --TODO: This function also needs to hold the mutex
1635+runFrame a = SpiderHost $ ask >>= \spiderEnv -> lift $ do
1636+ -- Clear out pending unsubscriptions; these will need to happen eventually anyway, and might cause us a lot of extra work during the frame, so get rid of them now
1637+ pendingCleanups <- atomicModifyIORef (_spiderEnv_cleanups spiderEnv) $ \x -> ([], x)
1638+ {-# SCC "cleanups" #-} sequence_ pendingCleanups
1639+ toAssignRef <- newIORef [] -- This should only actually get used when events are firing
1640+ holdInitRef <- newIORef []
1641+ mergeUpdateRef <- newIORef []
1642+ mergeInitRef <- newIORef []
1643+ heightRef <- newIORef zeroHeight
1644+ toClearRef <- newIORef []
1645+ toClearRootRef <- newIORef []
1646+ coincidenceInfosRef <- newIORef []
1647+ delayedRef <- liftIO $ newIORef IntMap.empty
1648+ let env = EventEnv spiderEnv toAssignRef holdInitRef mergeUpdateRef mergeInitRef toClearRef toClearRootRef heightRef coincidenceInfosRef delayedRef
1649+ let go = do
1650+ result <- a
1651+ runHoldInits holdInitRef mergeInitRef -- This must happen before doing the assignments, in case subscribing a Hold causes existing Holds to be read by the newly-propagated events
1652+ return result
1653+ result <- runEventM go env
1654+ toClear <- readIORef toClearRef
1655+ forM_ toClear $ \(SomeClear ref) -> {-# SCC "clear" #-} writeIORef ref Nothing
1656+ toClearRoot <- readIORef toClearRootRef
1657+ forM_ toClearRoot $ \(SomeRootClear ref) -> {-# SCC "rootClear" #-} writeIORef ref DMap.empty
1658+ toAssign <- readIORef toAssignRef
1659+ toReconnectRef <- newIORef []
1660+ coincidenceInfos <- readIORef coincidenceInfosRef
1661+ forM_ toAssign $ \(SomeAssignment vRef iRef v) -> {-# SCC "assignment" #-} do
1662+ writeIORef vRef v
1663+ when debugInvalidate $ putStrLn $ "Invalidating Hold"
1664+ writeIORef iRef =<< invalidate toReconnectRef =<< readIORef iRef
1665+ mergeUpdates <- readIORef mergeUpdateRef
1666+ writeIORef mergeUpdateRef []
1667+ let updateMerge :: SomeMergeUpdate -> IO [SomeEventSubscription]
1668+ updateMerge (SomeMergeUpdate (R.PatchDMap p) m) = do
1669+ oldParents <- readIORef $ mergeSubscribedParents m
1670+ let f (subscriptionsToKill, ps) (k :=> Compose me) = do
1671+ (mOldSubd, newPs) <- case me of
1672+ Nothing -> return $ DMap.updateLookupWithKey (\_ _ -> Nothing) k ps
1673+ Just e -> do
1674+ s <- liftIO $ newSubscriberMerge k m
1675+ subscription <- subscribe e s
1676+ let newParent = MergeSubscribedParent subscription s
1677+ return $ DMap.insertLookupWithKey' (\_ new _ -> new) k newParent ps
1678+ liftIO $ invalidateMergeHeight k m
1679+ return (maybeToList (SomeEventSubscription . mergeSubscribedParentSubscription <$> mOldSubd) ++ subscriptionsToKill, newPs)
1680+ (subscriptionsToKill, newParents) <- flip runEventM env $ foldM f ([], oldParents) $ DMap.toList p --TODO: I think this runEventM is OK, since no events are firing at this time, but it might not be
1681+ writeIORef (mergeSubscribedParents m) $! newParents
1682+ return subscriptionsToKill
1683+ mergeSubscriptionsToKill <- concat <$> mapM updateMerge mergeUpdates
1684+ toReconnect <- readIORef toReconnectRef
1685+ switchSubscriptionsToKill <- forM toReconnect $ \(SomeSwitchSubscribed subscribed) -> {-# SCC "switchSubscribed" #-} do
1686+ oldSubscription <- readIORef $ switchSubscribedCurrentParent subscribed
1687+ wi <- readIORef $ switchSubscribedOwnWeakInvalidator subscribed
1688+ when debugInvalidate $ putStrLn $ "Finalizing invalidator for Switch" <> showNodeId subscribed
1689+ finalize wi
1690+ i <- evaluate $ switchSubscribedOwnInvalidator subscribed
1691+ wi' <- mkWeakPtrWithDebug i "wi'"
1692+ writeIORef (switchSubscribedOwnWeakInvalidator subscribed) wi'
1693+ writeIORef (switchSubscribedBehaviorParents subscribed) []
1694+ writeIORef holdInitRef [] --TODO: Should we reuse this?
1695+ e <- runBehaviorM (readBehaviorTracked (switchSubscribedParent subscribed)) (Just (wi', switchSubscribedBehaviorParents subscribed)) holdInitRef
1696+ runEventM (runHoldInits holdInitRef mergeInitRef) env --TODO: Is this actually OK? It seems like it should be, since we know that no events are firing at this point, but it still seems inelegant
1697+ --TODO: Make sure we touch the pieces of the SwitchSubscribed at the appropriate times
1698+ sub <- newSubscriberSwitch subscribed
1699+ subscription@(_, subd') <- runReaderT (unSpiderHost (runFrame ({-# SCC "subscribeSwitch" #-} subscribe e sub))) spiderEnv --TODO: Assert that the event isn't firing --TODO: This should not loop because none of the events should be firing, but still, it is inefficient
1700+ {-
1701+ stackTrace <- liftIO $ liftM renderStack $ ccsToStrings =<< (getCCSOf $! switchSubscribedParent subscribed)
1702+ liftIO $ putStrLn $ (++stackTrace) $ "subd' subscribed to " ++ case e of
1703+ EventRoot _ -> "EventRoot"
1704+ EventNever -> "EventNever"
1705+ _ -> "something else"
1706+ -}
1707+ writeIORef (switchSubscribedCurrentParent subscribed) subscription
1708+ parentHeight <- readIORef $ eventSubscribedHeightRef subd'
1709+ myHeight <- readIORef $ switchSubscribedHeight subscribed
1710+ when (parentHeight /= myHeight) $ do
1711+ writeIORef (switchSubscribedHeight subscribed) parentHeight
1712+ WeakBag.traverse (switchSubscribedSubscribers subscribed) invalidateSubscriberHeight
1713+ return $ SomeEventSubscription oldSubscription
1714+ forM_ coincidenceInfos $ \(SomeResetCoincidence subscription mcs) -> do
1715+ unsubscribe subscription
1716+ mapM_ invalidateCoincidenceHeight mcs
1717+ forM_ mergeSubscriptionsToKill $ \(SomeEventSubscription oldSubscription) -> liftIO $ unsubscribe oldSubscription
1718+ forM_ switchSubscriptionsToKill $ \(SomeEventSubscription oldSubscription) -> liftIO $ unsubscribe oldSubscription
1719+ forM_ coincidenceInfos $ \(SomeResetCoincidence _ mcs) -> mapM_ recalculateCoincidenceHeight mcs
1720+ forM_ mergeUpdates $ \(SomeMergeUpdate _ m) -> recalculateMergeHeight m
1721+ forM_ toReconnect $ \(SomeSwitchSubscribed subscribed) -> do
1722+ WeakBag.traverse (switchSubscribedSubscribers subscribed) recalculateSubscriberHeight
1723+ return result
1724+
1725+newtype Height = Height { unHeight :: Int } deriving (Show, Read, Eq, Ord, Bounded)
1726+
1727+{-# INLINE zeroHeight #-}
1728+zeroHeight :: Height
1729+zeroHeight = Height 0
1730+
1731+{-# INLINE invalidHeight #-}
1732+invalidHeight :: Height
1733+invalidHeight = Height (-1000)
1734+
1735+-- | An invalid height that is currently being traversed, e.g. by walkInvalidHeightParents
1736+{-# INLINE invalidHeightBeingTraversed #-}
1737+invalidHeightBeingTraversed :: Height
1738+invalidHeightBeingTraversed = Height (-1001)
1739+
1740+{-# INLINE succHeight #-}
1741+succHeight :: Height -> Height
1742+succHeight h@(Height a) =
1743+ if h == invalidHeight
1744+ then invalidHeight
1745+ else Height $ succ a
1746+
1747+-- Get the maximum height; if either of the inputs is invalid, the output will also be invalid
1748+{-# INLINE maxHeight #-}
1749+maxHeight :: Height -> Height -> Height
1750+maxHeight ha@(Height a) hb@(Height b) =
1751+ if ha == invalidHeight || hb == invalidHeight
1752+ then invalidHeight
1753+ else Height $ max a b
1754+
1755+invalidateSubscriberHeight :: Subscriber a -> IO ()
1756+invalidateSubscriberHeight s = case s of
1757+ SubscriberPush _ subscribed -> do
1758+ when debugInvalidateHeight $ putStrLn $ "invalidateSubscriberHeight: SubscriberPush" <> showNodeId subscribed
1759+ WeakBag.traverse (pushSubscribedSubscribers subscribed) invalidateSubscriberHeight
1760+ when debugInvalidateHeight $ putStrLn $ "invalidateSubscriberHeight: SubscriberPush" <> showNodeId subscribed <> " done"
1761+ SubscriberMerge k subscribed -> do
1762+ when debugInvalidateHeight $ putStrLn $ "invalidateSubscriberHeight: SubscriberMerge" <> showNodeId subscribed
1763+ invalidateMergeHeight k subscribed
1764+ when debugInvalidateHeight $ putStrLn $ "invalidateSubscriberHeight: SubscriberMerge" <> showNodeId subscribed <> " done"
1765+ SubscriberMergeChange _ -> return () -- Doesn't affect the height of the merge, because its changes are delayed
1766+ SubscriberFan subscribed -> do
1767+ when debugInvalidateHeight $ putStrLn $ "invalidateSubscriberHeight: SubscriberFan" <> showNodeId subscribed
1768+ subscribers <- readIORef $ fanSubscribedSubscribers subscribed
1769+ forM_ (DMap.toList subscribers) $ \(_ :=> v) -> WeakBag.traverse (_fanSubscribedChildren_list v) invalidateSubscriberHeight
1770+ when debugInvalidateHeight $ putStrLn $ "invalidateSubscriberHeight: SubscriberFan" <> showNodeId subscribed <> " done"
1771+ SubscriberHold _ -> return ()
1772+ SubscriberHoldIdentity _ -> return ()
1773+ SubscriberSwitch subscribed -> do
1774+ when debugInvalidateHeight $ putStrLn $ "invalidateSubscriberHeight: SubscriberSwitch" <> showNodeId subscribed
1775+ oldHeight <- readIORef $ switchSubscribedHeight subscribed
1776+ when (oldHeight /= invalidHeight) $ do
1777+ writeIORef (switchSubscribedHeight subscribed) invalidHeight
1778+ WeakBag.traverse (switchSubscribedSubscribers subscribed) invalidateSubscriberHeight
1779+ when debugInvalidateHeight $ putStrLn $ "invalidateSubscriberHeight: SubscriberSwitch" <> showNodeId subscribed <> " done"
1780+ SubscriberCoincidenceOuter subscribed -> do
1781+ when debugInvalidateHeight $ putStrLn $ "invalidateSubscriberHeight: SubscriberCoincidenceOuter" <> showNodeId subscribed
1782+ invalidateCoincidenceHeight subscribed
1783+ when debugInvalidateHeight $ putStrLn $ "invalidateSubscriberHeight: SubscriberCoincidenceOuter" <> showNodeId subscribed <> " done"
1784+ SubscriberCoincidenceInner subscribed -> do
1785+ when debugInvalidateHeight $ putStrLn $ "invalidateSubscriberHeight: SubscriberCoincidenceInner" <> showNodeId subscribed
1786+ invalidateCoincidenceHeight subscribed
1787+ when debugInvalidateHeight $ putStrLn $ "invalidateSubscriberHeight: SubscriberCoincidenceInner" <> showNodeId subscribed <> " done"
1788+ SubscriberHandle -> return ()
1789+
1790+--TODO: Actually use the key
1791+invalidateMergeHeight :: k a -> MergeSubscribed k -> IO ()
1792+invalidateMergeHeight _ subscribed = do
1793+ oldHeight <- readIORef $ mergeSubscribedHeight subscribed
1794+ when (oldHeight /= invalidHeight) $ do
1795+ writeIORef (mergeSubscribedHeight subscribed) invalidHeight
1796+ WeakBag.traverse (mergeSubscribedSubscribers subscribed) invalidateSubscriberHeight
1797+
1798+recalculateMergeHeight :: MergeSubscribed k -> IO ()
1799+recalculateMergeHeight subscribed = do
1800+ oldHeight <- readIORef $ mergeSubscribedHeight subscribed
1801+ when (oldHeight == invalidHeight) $ do
1802+ height <- calculateMergeHeight subscribed
1803+ when debugInvalidateHeight $ putStrLn $ "recalculateSubscriberHeight: height: " <> show height
1804+ when (height /= invalidHeight) $ do -- If height == invalidHeight, that means some of the prereqs have not yet been recomputed; when they do recompute, they'll catch this node again --TODO: this is O(n*m), where n is the number of children of this noe and m is the number that have been invalidated
1805+ writeIORef (mergeSubscribedHeight subscribed) height
1806+ WeakBag.traverse (mergeSubscribedSubscribers subscribed) recalculateSubscriberHeight
1807+
1808+invalidateCoincidenceHeight :: CoincidenceSubscribed a -> IO ()
1809+invalidateCoincidenceHeight subscribed = do
1810+ oldHeight <- readIORef $ coincidenceSubscribedHeight subscribed
1811+ when (oldHeight /= invalidHeight) $ do
1812+ writeIORef (coincidenceSubscribedHeight subscribed) invalidHeight
1813+ WeakBag.traverse (coincidenceSubscribedSubscribers subscribed) invalidateSubscriberHeight
1814+
1815+--TODO: The recalculation algorithm seems a bit funky; make sure it doesn't miss stuff or hit stuff twice; also, it should probably be lazy
1816+
1817+recalculateSubscriberHeight :: Subscriber a -> IO ()
1818+recalculateSubscriberHeight s = case s of
1819+ SubscriberPush _ subscribed -> do
1820+ when debugInvalidateHeight $ putStrLn $ "recalculateSubscriberHeight: SubscriberPush" <> showNodeId subscribed
1821+ WeakBag.traverse (pushSubscribedSubscribers subscribed) recalculateSubscriberHeight
1822+ when debugInvalidateHeight $ putStrLn $ "recalculateSubscriberHeight: SubscriberPush" <> showNodeId subscribed <> " done"
1823+ SubscriberMerge _ subscribed -> do
1824+ when debugInvalidateHeight $ putStrLn $ "recalculateSubscriberHeight: SubscriberMerge" <> showNodeId subscribed
1825+ recalculateMergeHeight subscribed
1826+ when debugInvalidateHeight $ putStrLn $ "recalculateSubscriberHeight: SubscriberMerge" <> showNodeId subscribed <> " done"
1827+ SubscriberMergeChange _ -> return ()
1828+ SubscriberFan subscribed -> do
1829+ when debugInvalidateHeight $ putStrLn $ "recalculateSubscriberHeight: SubscriberFan" <> showNodeId subscribed
1830+ subscribers <- readIORef $ fanSubscribedSubscribers subscribed
1831+ forM_ (DMap.toList subscribers) $ \(_ :=> v) -> WeakBag.traverse (_fanSubscribedChildren_list v) recalculateSubscriberHeight
1832+ when debugInvalidateHeight $ putStrLn $ "recalculateSubscriberHeight: SubscriberFan" <> showNodeId subscribed <> " done"
1833+ SubscriberHold _ -> return ()
1834+ SubscriberHoldIdentity _ -> return ()
1835+ SubscriberSwitch subscribed -> do
1836+ when debugInvalidateHeight $ putStrLn $ "recalculateSubscriberHeight: SubscriberSwitch" <> showNodeId subscribed
1837+ oldHeight <- readIORef $ switchSubscribedHeight subscribed
1838+ when (oldHeight == invalidHeight) $ do
1839+ height <- calculateSwitchHeight subscribed
1840+ when (height /= invalidHeight) $ do
1841+ writeIORef (switchSubscribedHeight subscribed) height
1842+ WeakBag.traverse (switchSubscribedSubscribers subscribed) recalculateSubscriberHeight
1843+ when debugInvalidateHeight $ putStrLn $ "recalculateSubscriberHeight: SubscriberSwitch" <> showNodeId subscribed <> " done"
1844+ SubscriberCoincidenceOuter subscribed -> do
1845+ when debugInvalidateHeight $ putStrLn $ "recalculateSubscriberHeight: SubscriberCoincidenceOuter" <> showNodeId subscribed
1846+ recalculateCoincidenceHeight subscribed
1847+ when debugInvalidateHeight $ putStrLn $ "recalculateSubscriberHeight: SubscriberCoincidenceOuter" <> showNodeId subscribed <> " done"
1848+ SubscriberCoincidenceInner subscribed -> do
1849+ when debugInvalidateHeight $ putStrLn $ "recalculateSubscriberHeight: SubscriberCoincidenceInner" <> showNodeId subscribed
1850+ recalculateCoincidenceHeight subscribed
1851+ when debugInvalidateHeight $ putStrLn $ "recalculateSubscriberHeight: SubscriberCoincidenceInner" <> showNodeId subscribed <> " done"
1852+ SubscriberHandle -> return ()
1853+
1854+recalculateCoincidenceHeight :: CoincidenceSubscribed a -> IO ()
1855+recalculateCoincidenceHeight subscribed = do
1856+ oldHeight <- readIORef $ coincidenceSubscribedHeight subscribed
1857+ when (oldHeight == invalidHeight) $ do
1858+ height <- calculateCoincidenceHeight subscribed
1859+ when (height /= invalidHeight) $ do
1860+ writeIORef (coincidenceSubscribedHeight subscribed) height
1861+ WeakBag.traverse (coincidenceSubscribedSubscribers subscribed) recalculateSubscriberHeight --TODO: This should probably be mandatory, just like with the merge and switch ones
1862+
1863+calculateMergeHeight :: MergeSubscribed k -> IO Height
1864+calculateMergeHeight subscribed = do
1865+ parents <- readIORef $ mergeSubscribedParents subscribed
1866+ if DMap.null parents then return zeroHeight else do
1867+ heights <- forM (DMap.toList parents) $ \(_ :=> es) -> do
1868+ readIORef $ eventSubscribedHeightRef $ snd $ mergeSubscribedParentSubscription es
1869+ return $ succHeight $ foldr maxHeight zeroHeight heights
1870+
1871+calculateSwitchHeight :: SwitchSubscribed a -> IO Height
1872+calculateSwitchHeight subscribed = readIORef . eventSubscribedHeightRef . snd =<< readIORef (switchSubscribedCurrentParent subscribed)
1873+
1874+calculateCoincidenceHeight :: CoincidenceSubscribed a -> IO Height
1875+calculateCoincidenceHeight subscribed = do
1876+ outerHeight <- readIORef $ eventSubscribedHeightRef $ snd $ coincidenceSubscribedOuterParent subscribed
1877+ innerHeight <- maybe (return zeroHeight) (readIORef . eventSubscribedHeightRef) =<< readIORef (coincidenceSubscribedInnerParent subscribed)
1878+ return $ if outerHeight == invalidHeight || innerHeight == invalidHeight then invalidHeight else max outerHeight innerHeight
1879+
1880+data SomeSwitchSubscribed = forall a. SomeSwitchSubscribed {-# NOUNPACK #-} (SwitchSubscribed a)
1881+
1882+invalidate :: IORef [SomeSwitchSubscribed] -> WeakList Invalidator -> IO (WeakList Invalidator)
1883+invalidate toReconnectRef wis = do
1884+ forM_ wis $ \wi -> do
1885+ mi <- deRefWeak wi
1886+ case mi of
1887+ Nothing -> do
1888+ traceInvalidate "invalidate Dead"
1889+ return () --TODO: Should we clean this up here?
1890+ Just i -> do
1891+ finalize wi -- Once something's invalidated, it doesn't need to hang around; this will change when some things are strict
1892+ case i of
1893+ InvalidatorPull p -> do
1894+ traceInvalidate $ "invalidate: Pull" <> showNodeId p
1895+ mVal <- readIORef $ pullValue p
1896+ forM_ mVal $ \val -> do
1897+ writeIORef (pullValue p) Nothing
1898+ writeIORef (pullSubscribedInvalidators val) =<< invalidate toReconnectRef =<< readIORef (pullSubscribedInvalidators val)
1899+ InvalidatorSwitch subscribed -> do
1900+ traceInvalidate $ "invalidate: Switch" <> showNodeId subscribed
1901+ modifyIORef' toReconnectRef (SomeSwitchSubscribed subscribed :)
1902+ return [] -- Since we always finalize everything, always return an empty list --TODO: There are some things that will need to be re-subscribed every time; we should try to avoid finalizing them
1903+
1904+--------------------------------------------------------------------------------
1905+-- Reflex integration
1906+--------------------------------------------------------------------------------
1907+
1908+type Spider = SpiderEnv Global
1909+
1910+data Global
1911+
1912+{-# NOINLINE globalSpiderEnv #-}
1913+globalSpiderEnv :: SpiderEnv Global
1914+globalSpiderEnv = unsafePerformIO unsafeNewSpiderEnv
1915+
1916+type role SpiderEnv nominal
1917+data SpiderEnv x = SpiderEnv
1918+ { _spiderEnv_toUnsubscribe :: !(IORef [SomeEventSubscription])
1919+ , _spiderEnv_lock :: !(MVar ())
1920+ , _spiderEnv_cleanups :: !(IORef [IO ()])
1921+#ifdef DEBUG
1922+ , _spiderEnv_depth :: !(IORef Int)
1923+#endif
1924+ }
1925+
1926+instance GEq SpiderEnv where
1927+ a `geq` b = if _spiderEnv_toUnsubscribe a == _spiderEnv_toUnsubscribe b
1928+ then Just $ unsafeCoerce Refl -- This unsafeCoerce is safe because the same SpiderEnv can't have two different 'x' arguments
1929+ else Nothing
1930+
1931+unsafeNewSpiderEnv :: forall x. IO (SpiderEnv x)
1932+unsafeNewSpiderEnv = do
1933+ toUnsubscribe <- newIORef []
1934+ lock <- newMVar ()
1935+ cleanups <- newIORef []
1936+#ifdef DEBUG
1937+ depthRef <- newIORef 0
1938+#endif
1939+ return $ SpiderEnv
1940+ { _spiderEnv_toUnsubscribe = toUnsubscribe
1941+ , _spiderEnv_lock = lock
1942+ , _spiderEnv_cleanups = cleanups
1943+#ifdef DEBUG
1944+ , _spiderEnv_depth = depthRef
1945+#endif
1946+ }
1947+
1948+newSpiderEnv :: IO (Some SpiderEnv)
1949+newSpiderEnv = Some.This <$> unsafeNewSpiderEnv
1950+
1951+newtype SpiderPullM x a = SpiderPullM { runSpiderPullM :: BehaviorM a } deriving (Functor, Applicative, Monad, MonadIO, MonadFix)
1952+
1953+newtype SpiderPushM x a = SpiderPushM { runSpiderPushM :: ComputeM a } deriving (Functor, Applicative, Monad, MonadIO, MonadFix)
1954+
1955+instance R.Reflex (SpiderEnv x) where
1956+ newtype Behavior (SpiderEnv x) a = SpiderBehavior { unSpiderBehavior :: Behavior a }
1957+ newtype Event (SpiderEnv x) a = SpiderEvent { unSpiderEvent :: Event a }
1958+ newtype Dynamic (SpiderEnv x) a = SpiderDynamic { unSpiderDynamic :: Dynamic Identity a } deriving (Functor, Applicative, Monad)
1959+ newtype Incremental (SpiderEnv x) p a = SpiderIncremental { unSpiderIncremental :: Dynamic p a }
1960+ type PullM (SpiderEnv x) = SpiderPullM x
1961+ type PushM (SpiderEnv x) = SpiderPushM x
1962+ {-# INLINABLE never #-}
1963+ never = SpiderEvent EventNever
1964+ {-# INLINABLE constant #-}
1965+ constant = SpiderBehavior . BehaviorConst
1966+ {-# INLINABLE push #-}
1967+ push f = SpiderEvent . push (coerce f) . unSpiderEvent
1968+ {-# INLINABLE pull #-}
1969+ pull = SpiderBehavior . pull . coerce
1970+ {-# INLINABLE merge #-}
1971+ merge = SpiderEvent . merge . DynamicConst . (unsafeCoerce :: DMap k (R.Event (SpiderEnv x)) -> DMap k Event)
1972+ {-# INLINABLE fan #-}
1973+ fan e = R.EventSelector $ SpiderEvent . select (fan (unSpiderEvent e))
1974+ {-# INLINABLE switch #-}
1975+ switch = SpiderEvent . switch . (unsafeCoerce :: Behavior (R.Event (SpiderEnv x) a) -> Behavior (Event a)) . unSpiderBehavior
1976+ {-# INLINABLE coincidence #-}
1977+ coincidence = SpiderEvent . coincidence . (unsafeCoerce :: Event (R.Event (SpiderEnv x) a) -> Event (Event a)) . unSpiderEvent
1978+ {-# INLINABLE current #-}
1979+ current = SpiderBehavior . current . unSpiderDynamic
1980+ {-# INLINABLE updated #-}
1981+ updated = SpiderEvent . fmap runIdentity . updated . unSpiderDynamic
1982+ {-# INLINABLE unsafeBuildDynamic #-}
1983+ unsafeBuildDynamic readV0 v' = SpiderDynamic $ DynamicDynIdentity $ unsafeDyn (coerce readV0) $ addIdentity $ unSpiderEvent v'
1984+ {-# INLINABLE unsafeBuildIncremental #-}
1985+ unsafeBuildIncremental readV0 dv = SpiderIncremental $ DynamicDyn $ unsafeDyn (coerce readV0) $ unSpiderEvent dv
1986+ {-# INLINABLE mergeIncremental #-}
1987+ mergeIncremental = SpiderEvent . merge . (unsafeCoerce :: Dynamic R.PatchDMap (DMap k (R.Event (SpiderEnv x))) -> Dynamic R.PatchDMap (DMap k Event)) . unSpiderIncremental
1988+ {-# INLINABLE currentIncremental #-}
1989+ currentIncremental = SpiderBehavior . current . unSpiderIncremental
1990+ {-# INLINABLE updatedIncremental #-}
1991+ updatedIncremental = SpiderEvent . updated . unSpiderIncremental
1992+ {-# INLINABLE incrementalToDynamic #-}
1993+ incrementalToDynamic (SpiderIncremental i) = SpiderDynamic $ DynamicDynIdentity $ unsafeDyn (readBehaviorUntracked $ current i) $ flip push (updated i) $ \p -> do
1994+ c <- readBehaviorUntracked $ current i
1995+ return $ fmap Identity $ R.apply p c --TODO: Avoid the redundant 'apply'
1996+
1997+instance R.MonadSample (SpiderEnv x) (EventM x) where
1998+ {-# INLINABLE sample #-}
1999+ sample (SpiderBehavior b) = readBehaviorUntracked b
2000+
2001+instance R.MonadHold (SpiderEnv x) (EventM x) where
2002+ {-# INLINABLE hold #-}
2003+ hold = holdSpiderEventM
2004+ {-# INLINABLE holdDyn #-}
2005+ holdDyn = holdDynSpiderEventM
2006+ {-# INLINABLE holdIncremental #-}
2007+ holdIncremental = holdIncrementalSpiderEventM
2008+
2009+holdSpiderEventM :: a -> R.Event (SpiderEnv x) a -> EventM x (R.Behavior (SpiderEnv x) a)
2010+holdSpiderEventM v0 e = liftM (SpiderBehavior . BehaviorHoldIdentity) $ hold v0 $ addIdentity $ unSpiderEvent e
2011+
2012+holdDynSpiderEventM :: a -> R.Event (SpiderEnv x) a -> EventM x (R.Dynamic (SpiderEnv x) a)
2013+holdDynSpiderEventM v0 e = liftM (SpiderDynamic . DynamicHoldIdentity) $ hold v0 $ addIdentity $ unSpiderEvent e
2014+
2015+holdIncrementalSpiderEventM :: R.Patch p => a -> R.Event (SpiderEnv x) (p a) -> EventM x (R.Incremental (SpiderEnv x) p a)
2016+holdIncrementalSpiderEventM v0 e = liftM (SpiderIncremental . DynamicHold) $ hold v0 $ unSpiderEvent e
2017+
2018+instance R.MonadHold (SpiderEnv x) (SpiderHost x) where
2019+ {-# INLINABLE hold #-}
2020+ hold v0 e = R.runHostFrame $ R.hold v0 e
2021+ {-# INLINABLE holdDyn #-}
2022+ holdDyn v0 e = R.runHostFrame $ R.holdDyn v0 e
2023+ {-# INLINABLE holdIncremental #-}
2024+ holdIncremental v0 e = R.runHostFrame $ R.holdIncremental v0 e
2025+
2026+instance R.MonadSample (SpiderEnv x) (SpiderHost x) where
2027+ {-# INLINABLE sample #-}
2028+ sample = runFrame . readBehaviorUntracked . unSpiderBehavior
2029+
2030+instance R.MonadSample (SpiderEnv x) (SpiderPullM x) where
2031+ {-# INLINABLE sample #-}
2032+ sample = coerce . readBehaviorTracked . unSpiderBehavior
2033+
2034+instance R.MonadSample (SpiderEnv x) (SpiderPushM x) where
2035+ {-# INLINABLE sample #-}
2036+ sample (SpiderBehavior b) = SpiderPushM $ readBehaviorUntracked b
2037+
2038+instance R.MonadHold (SpiderEnv x) (SpiderPushM x) where
2039+ {-# INLINABLE hold #-}
2040+ hold v0 e = R.current <$> R.holdDyn v0 e
2041+ {-# INLINABLE holdDyn #-}
2042+ holdDyn v0 (SpiderEvent e) = SpiderPushM $ liftM (SpiderDynamic . DynamicHoldIdentity) $ hold v0 $ addIdentity e
2043+ {-# INLINABLE holdIncremental #-}
2044+ holdIncremental v0 (SpiderEvent e) = SpiderPushM $ liftM (SpiderIncremental . DynamicHold) $ hold v0 e
2045+
2046+data RootTrigger a = forall k. GCompare k => RootTrigger (SubscriberList a, IORef (DMap k Identity), k a)
2047+newtype SpiderEventHandle a = SpiderEventHandle { unSpiderEventHandle :: EventSubscription a }
2048+
2049+instance R.MonadSubscribeEvent (SpiderEnv x) (SpiderHostFrame x) where
2050+ {-# INLINABLE subscribeEvent #-}
2051+ subscribeEvent e = SpiderHostFrame $ do
2052+ subscription <- subscribe (unSpiderEvent e) SubscriberHandle --TODO: Unsubscribe eventually (manually and/or with weak ref)
2053+ return $ SpiderEventHandle subscription
2054+
2055+instance R.ReflexHost (SpiderEnv x) where
2056+ type EventTrigger (SpiderEnv x) = RootTrigger
2057+ type EventHandle (SpiderEnv x) = SpiderEventHandle
2058+ type HostFrame (SpiderEnv x) = SpiderHostFrame x
2059+
2060+instance R.MonadReadEvent (SpiderEnv x) (ReadPhase x) where
2061+ {-# INLINABLE readEvent #-}
2062+ readEvent h = ReadPhase $ liftM (fmap return) $ liftIO $ do
2063+ let (sln, subscribed) = unSpiderEventHandle h
2064+ result <- readEventSubscribed subscribed
2065+ touch sln
2066+ return result
2067+
2068+instance MonadRef (EventM x) where
2069+ type Ref (EventM x) = Ref IO
2070+ {-# INLINABLE newRef #-}
2071+ {-# INLINABLE readRef #-}
2072+ {-# INLINABLE writeRef #-}
2073+ newRef = liftIO . newRef
2074+ readRef = liftIO . readRef
2075+ writeRef r a = liftIO $ writeRef r a
2076+
2077+instance MonadAtomicRef (EventM x) where
2078+ {-# INLINABLE atomicModifyRef #-}
2079+ atomicModifyRef r f = liftIO $ atomicModifyRef r f
2080+
2081+newtype SpiderHost x a = SpiderHost { unSpiderHost :: ReaderT (SpiderEnv x) IO a } deriving (Functor, Applicative, MonadFix, MonadIO, MonadException, MonadAsyncException)
2082+
2083+instance Monad (SpiderHost x) where
2084+ {-# INLINABLE (>>=) #-}
2085+ SpiderHost x >>= f = SpiderHost $ x >>= unSpiderHost . f
2086+ {-# INLINABLE (>>) #-}
2087+ SpiderHost x >> SpiderHost y = SpiderHost $ x >> y
2088+ {-# INLINABLE return #-}
2089+ return x = SpiderHost $ return x
2090+ {-# INLINABLE fail #-}
2091+ fail s = SpiderHost $ fail s
2092+
2093+runSpiderHost :: SpiderHost Global a -> IO a
2094+runSpiderHost (SpiderHost a) = runReaderT a globalSpiderEnv
2095+
2096+newtype SpiderHostFrame x a = SpiderHostFrame { runSpiderHostFrame :: EventM x a } deriving (Functor, Applicative, MonadFix, MonadIO, MonadException, MonadAsyncException)
2097+
2098+instance Monad (SpiderHostFrame x) where
2099+ {-# INLINABLE (>>=) #-}
2100+ SpiderHostFrame x >>= f = SpiderHostFrame $ x >>= runSpiderHostFrame . f
2101+ {-# INLINABLE (>>) #-}
2102+ SpiderHostFrame x >> SpiderHostFrame y = SpiderHostFrame $ x >> y
2103+ {-# INLINABLE return #-}
2104+ return x = SpiderHostFrame $ return x
2105+ {-# INLINABLE fail #-}
2106+ fail s = SpiderHostFrame $ fail s
2107+
2108+instance R.MonadSample (SpiderEnv x) (SpiderHostFrame x) where
2109+ sample = SpiderHostFrame . readBehaviorUntracked . unSpiderBehavior --TODO: This can cause problems with laziness, so we should get rid of it if we can
2110+
2111+addIdentity :: Event a -> Event (Identity a)
2112+--addIdentity = fmap Identity
2113+addIdentity = unsafeCoerce
2114+
2115+instance R.MonadHold (SpiderEnv x) (SpiderHostFrame x) where
2116+ {-# INLINABLE hold #-}
2117+ hold v0 e = SpiderHostFrame $ liftM (SpiderBehavior . BehaviorHoldIdentity) $ hold v0 $ addIdentity $ unSpiderEvent e
2118+ {-# INLINABLE holdDyn #-}
2119+ holdDyn v0 e = SpiderHostFrame $ liftM (SpiderDynamic . DynamicHoldIdentity) $ hold v0 $ addIdentity $ unSpiderEvent e
2120+ {-# INLINABLE holdIncremental #-}
2121+ holdIncremental v0 e = SpiderHostFrame $ liftM (SpiderIncremental . DynamicHold) $ hold v0 $ unSpiderEvent e
2122+
2123+newEventWithTriggerIO :: forall a. (RootTrigger a -> IO (IO ())) -> IO (Event a)
2124+newEventWithTriggerIO f = do
2125+ es <- newFanEventWithTriggerIO $ \Refl -> f
2126+ return $ select es Refl
2127+
2128+newFanEventWithTriggerIO :: GCompare k => (forall a. k a -> RootTrigger a -> IO (IO ())) -> IO (EventSelector k)
2129+newFanEventWithTriggerIO f = do
2130+ occRef <- newIORef DMap.empty
2131+ subscribedRef <- newIORef DMap.empty
2132+ let !r = Root
2133+ { rootOccurrence = occRef
2134+ , rootSubscribed = subscribedRef
2135+ , rootInit = f
2136+ }
2137+ return $ EventSelector $ \k -> EventRoot k r
2138+
2139+instance R.MonadReflexCreateTrigger (SpiderEnv x) (SpiderHost x) where
2140+ newEventWithTrigger = SpiderHost . lift . liftM SpiderEvent . newEventWithTriggerIO
2141+ newFanEventWithTrigger f = SpiderHost $ lift $ do
2142+ es <- newFanEventWithTriggerIO f
2143+ return $ R.EventSelector $ SpiderEvent . select es
2144+
2145+instance R.MonadReflexCreateTrigger (SpiderEnv x) (SpiderHostFrame x) where
2146+ newEventWithTrigger = SpiderHostFrame . EventM . liftIO . liftM SpiderEvent . newEventWithTriggerIO
2147+ newFanEventWithTrigger f = SpiderHostFrame $ EventM $ liftIO $ do
2148+ es <- newFanEventWithTriggerIO f
2149+ return $ R.EventSelector $ SpiderEvent . select es
2150+
2151+instance R.MonadSubscribeEvent (SpiderEnv x) (SpiderHost x) where
2152+ {-# INLINABLE subscribeEvent #-}
2153+ subscribeEvent = subscribeEventSpiderHost
2154+
2155+subscribeEventSpiderHost :: R.Event (SpiderEnv x) a -> SpiderHost x (R.EventHandle (SpiderEnv x) a)
2156+subscribeEventSpiderHost e = do
2157+ subscription <- runFrame $ getEventSubscribed (unSpiderEvent e) SubscriberHandle --TODO: Unsubscribe eventually (manually and/or with weak ref)
2158+ return $ SpiderEventHandle subscription
2159+
2160+newtype ReadPhase x a = ReadPhase { runReadPhase :: ResultM x a } deriving (Functor, Applicative, Monad, MonadFix)
2161+
2162+instance R.MonadSample (SpiderEnv x) (ReadPhase x) where
2163+ {-# INLINABLE sample #-}
2164+ sample = ReadPhase . R.sample
2165+
2166+instance R.MonadHold (SpiderEnv x) (ReadPhase x) where
2167+ {-# INLINABLE hold #-}
2168+ hold v0 e = ReadPhase $ R.hold v0 e
2169+ {-# INLINABLE holdDyn #-}
2170+ holdDyn v0 e = ReadPhase $ R.holdDyn v0 e
2171+ {-# INLINABLE holdIncremental #-}
2172+ holdIncremental v0 e = ReadPhase $ R.holdIncremental v0 e
2173+
2174+instance R.MonadReflexHost (SpiderEnv x) (SpiderHost x) where
2175+ type ReadPhase (SpiderHost x) = ReadPhase x
2176+ fireEventsAndRead es (ReadPhase a) = run es a
2177+ runHostFrame = runFrame . runSpiderHostFrame
2178+
2179+instance MonadRef (SpiderHost x) where
2180+ type Ref (SpiderHost x) = Ref IO
2181+ newRef = SpiderHost . newRef
2182+ readRef = SpiderHost . readRef
2183+ writeRef r = SpiderHost . writeRef r
2184+
2185+instance MonadAtomicRef (SpiderHost x) where
2186+ atomicModifyRef r = SpiderHost . atomicModifyRef r
2187+
2188+instance MonadRef (SpiderHostFrame x) where
2189+ type Ref (SpiderHostFrame x) = Ref IO
2190+ newRef = SpiderHostFrame . newRef
2191+ readRef = SpiderHostFrame . readRef
2192+ writeRef r = SpiderHostFrame . writeRef r
2193+
2194+instance MonadAtomicRef (SpiderHostFrame x) where
2195+ atomicModifyRef r = SpiderHostFrame . atomicModifyRef r
stylizedeleted−4
@@ -1,4 +0,0 @@
1−#!/usr/bin/env bash
2−set -euo pipefail
3−
4−for x in **/*.hs ; do stylish-haskell -i "$x" ; done
test/Reflex/Bench/Focused.hsnew file+361
@@ -0,0 +1,361 @@
1+{-# LANGUAGE RecursiveDo, ConstraintKinds, TypeSynonymInstances, BangPatterns, ScopedTypeVariables, TupleSections, GADTs, RankNTypes, FlexibleInstances, FlexibleContexts, MultiParamTypeClasses, GeneralizedNewtypeDeriving #-}
2+
3+module Reflex.Bench.Focused where
4+
5+import Reflex
6+import Reflex.TestPlan
7+
8+import Control.Monad.Fix
9+import Control.Monad
10+import Control.Monad.Identity
11+import Control.Applicative
12+--import Data.Foldable
13+import Data.Traversable (for)
14+
15+import Data.Maybe
16+import Data.Map (Map)
17+import qualified Data.Map as Map
18+import qualified Data.Dependent.Map as DMap
19+
20+import Data.Functor.Misc
21+
22+import Data.Tuple
23+import Data.List
24+import Data.List.Split
25+
26+import Data.Word
27+import Data.Monoid
28+
29+import Control.DeepSeq
30+
31+import Prelude hiding (sum, concat)
32+
33+
34+mergeTree :: Num a => (Monoid (f [a]), Functor f) => Int -> [f a] -> f a
35+mergeTree n es | length es <= n = sum' es
36+ | otherwise = mergeTree n subTrees
37+ where
38+ merges = chunksOf n es
39+ subTrees = map sum' merges
40+ sum' = fmap sum . mconcat . fmap (fmap (:[]) )
41+
42+mergeListDyn :: (Reflex t, MonadHold t m) => [Dynamic t a] -> m (Dynamic t [a])
43+mergeListDyn dyns = traverse (mapDyn pure) dyns >>= mconcatDyn
44+
45+mergeTreeDyn :: (Num a, Reflex t, MonadHold t m, MonadFix m) => Int -> [Dynamic t a] -> m (Dynamic t a)
46+mergeTreeDyn n dyns | length dyns <= n = sumDyns dyns
47+ | otherwise = traverse sumDyns merges >>= mergeTreeDyn n
48+ where
49+ merges = chunksOf n dyns
50+ sumDyns ds = mergeListDyn ds >>= mapDyn sum
51+
52+
53+
54+-- N events all firing for N frames
55+continuous :: TestPlan t m => Word -> Word -> m [Event t Word]
56+continuous n frames = for [1..n] $ \i -> plan $ (, i) <$> [1..frames]
57+
58+-- Fire N events once every P frames for total N frames (interspersed evenly)
59+occasional :: TestPlan t m => Word -> Word -> Word -> m [Event t Word]
60+occasional n period frames = traverse plan $ zipWith occs [1..] phases
61+ where
62+ phases = take (fromIntegral n) $ concat (repeat [1..period])
63+ occs i phase = (, i) <$> [1, phase+1..frames]
64+
65+
66+
67+-- Event which never fires (but isn't 'never')
68+never' :: TestPlan t m => m (Event t Word)
69+never' = plan []
70+
71+
72+-- Event which fires once on first frame
73+event :: TestPlan t m => m (Event t Word)
74+event = plan [(1, 0)]
75+
76+-- Event which fires constantly over N frames
77+events :: TestPlan t m => Word -> m (Event t Word)
78+events n = plan $ (\i -> (i, i)) <$> [1..n]
79+
80+
81+-- N events all originating from one event
82+fmapFan :: Reflex t => Word -> Event t Word -> [Event t Word]
83+fmapFan n e = (\i -> (+i) <$> e) <$> [1..n]
84+
85+
86+-- Make a fan using the value in the event converting it to a singleton Map
87+-- use a fan of size n (key is the value mod n)
88+fanMerge :: Reflex t => Word -> Event t Word -> Event t Word
89+fanMerge n e = leftmost $ s <$> [0..n - 1] where
90+ s k = select f (Const2 k)
91+ f = fan (toMap <$> e)
92+ toMap k = DMap.singleton (Const2 $ k `mod` n) (Identity k)
93+
94+-- Dumb version of the above using simple fmapMaybe to filter
95+fmapFanMerge :: Reflex t => Word -> Event t Word -> Event t Word
96+fmapFanMerge n e = leftmost $ s <$> [0..n - 1] where
97+ s k = flip fmapMaybe e $ \j -> if (k == j `mod` n)
98+ then Just k
99+ else Nothing
100+
101+
102+
103+-- Create an Event with a Map which is dense to size N
104+denseMap :: TestPlan t m => Word -> m (Event t (Map Word Word))
105+denseMap n = plan [(1, m)] where
106+ m = Map.fromList $ zip [1..n] [1..n]
107+
108+iter :: (a -> a) -> Word -> a -> a
109+iter _ 0 a = a
110+iter f n a = iter f (n - 1) (f a)
111+
112+iterM :: Monad m => (a -> m a) -> Word -> a -> m a
113+iterM _ 0 a = return a
114+iterM f n a = iterM f (n - 1) =<< f a
115+
116+fmapChain :: (Functor f) => Word -> f Word -> f Word
117+fmapChain = iter (fmap (+1))
118+
119+mapDynChain :: (Reflex t, MonadHold t m) => Word -> Dynamic t Word -> m (Dynamic t Word)
120+mapDynChain = iterM (mapDyn (+1))
121+
122+joinDynChain :: (Reflex t, MonadHold t m) => Word -> Dynamic t Word -> m (Dynamic t Word)
123+joinDynChain = iterM (\d -> joinDyn <$> mapDyn (const d) d)
124+
125+combineDynChain :: (Reflex t, MonadHold t m) => Word -> Dynamic t Word -> m (Dynamic t Word)
126+combineDynChain = iterM (\d -> combineDyn (+) d d)
127+
128+pingPong :: (Reflex t, MonadHold t m, MonadFix m) => Event t a -> Event t a -> m (Event t a)
129+pingPong e1 e2 = do
130+ rec
131+ d <- foldDyn (const swap) (e1, e2) e
132+ let e = switch (fst <$> current d)
133+ return e
134+
135+switchFactors :: (Reflex t, MonadHold t m) => Word -> Event t Word -> m (Event t Word)
136+switchFactors 0 e = return e
137+switchFactors i e = do
138+ b <- hold ((+1) <$> e) (e <$ ffilter ((== 0) . (i `mod`)) e)
139+ switchFactors (i - 1) (switch b)
140+
141+
142+switchChain :: (Reflex t, MonadHold t m, MonadFix m) => Word -> Event t Word -> m (Event t Word)
143+switchChain = iterM (\e -> pingPong e ((+1) <$> e))
144+
145+
146+switchChain2 :: (Reflex t, MonadHold t m, MonadFix m) => Word -> Event t Word -> m (Event t Word)
147+switchChain2 = iterM (\e -> pingPong e ((+1) <$> leftmost [e, e]))
148+
149+switchPromptlyChain :: (Reflex t, MonadHold t m) => Word -> Event t Word -> m (Event t Word)
150+switchPromptlyChain = iterM $ \e ->
151+ switchPromptlyDyn <$> holdDyn e (e <$ e)
152+
153+
154+coinChain :: Reflex t => Word -> Event t Word -> Event t Word
155+coinChain = iter $ \e -> coincidence (e <$ e)
156+
157+fanMergeChain :: Reflex t => Word -> Word -> Event t Word -> Event t Word
158+fanMergeChain width = iter $ fanMerge width
159+
160+fmapFanMergeChain :: Reflex t => Word -> Word -> Event t Word -> Event t Word
161+fmapFanMergeChain width = iter $ fmapFanMerge width
162+
163+-- | Data type for a collection (a map) which is updated by providing differences
164+data UpdatedMap t k a = UpdatedMap (Map k a) (Event t (Map k (Maybe a)))
165+
166+-- This will hopefully become a primitive (faster!)
167+switchMergeEvents :: (MonadFix m, MonadHold t m, Reflex t, Ord k) => UpdatedMap t k (Event t a) -> m (Event t (Map k a))
168+switchMergeEvents mapChanges = switch . fmap mergeMap <$> holdMap mapChanges
169+
170+-- switchMergeMaybe :: (MonadFix m, MonadHold t m, Reflex t, Ord k) => UpdatedMap t k (Event t a) -> m (Event t (Map k a))
171+-- switchMergeMaybe (UpdatedMap initial changes) = switchMergeMap initial (fmap (fromMaybe never) <$> changes)
172+
173+switchMergeBehaviors :: forall a t m k. (MonadFix m, MonadHold t m, Reflex t, Ord k) => UpdatedMap t k (Behavior t a) -> m (Behavior t (Map k a))
174+switchMergeBehaviors mapChanges = pull <$> joinMap <$> holdMap mapChanges
175+ where joinMap m = traverse sample =<< sample m
176+
177+-- | Turn an UpdatedMap into a Dynamic by applying the differences to the initial value
178+holdMapDyn :: (Reflex t, MonadHold t m, MonadFix m, Ord k) => UpdatedMap t k a -> m (Dynamic t (Map k a))
179+holdMapDyn (UpdatedMap initial changes) = foldDyn (flip (Map.foldWithKey modify)) initial changes
180+
181+ where
182+ modify k Nothing items = Map.delete k items
183+ modify k (Just item) items = Map.insert k item items
184+
185+
186+-- | Hold an UpdatedMap as a behavior by applying differences to the initial value
187+holdMap :: (Reflex t, MonadHold t m, MonadFix m, Ord k) => UpdatedMap t k a -> m (Behavior t (Map k a))
188+holdMap = (current <$>) . holdMapDyn
189+
190+
191+increasingMerge :: TestPlan t m => [a] -> m (UpdatedMap t Int a)
192+increasingMerge xs = do
193+ e <- planList (zipWith Map.singleton [1..] (Just <$> xs) ++ [reset])
194+ return (UpdatedMap Map.empty e)
195+ where
196+ reset = Map.fromList $ zip [1..] (const Nothing <$> xs)
197+
198+decreasingMerge :: (TestPlan t m) => [a] -> m (UpdatedMap t Int a)
199+decreasingMerge xs = do
200+ e <- planList (zipWith Map.singleton [1..] (const Nothing <$> xs) ++ [reset])
201+ return (UpdatedMap initial e)
202+ where
203+ initial = Map.fromList $ zip [1..] xs
204+ reset = Just <$> initial
205+
206+modifyMerge :: (TestPlan t m) => [a] -> m (UpdatedMap t Int a)
207+modifyMerge xs = do
208+ e <- planList $ zipWith Map.singleton [1..] (Just <$> xs)
209+ return (UpdatedMap initial e)
210+ where
211+ initial = Map.fromList $ zip [1..] xs
212+
213+
214+countMany :: (Reflex t, MonadHold t m, MonadFix m) => [Event t a] -> m [Behavior t Int]
215+countMany = traverse (fmap current . count)
216+
217+countManyDyn :: (Reflex t, MonadHold t m, MonadFix m) => [Event t a] -> m [Dynamic t Int]
218+countManyDyn = traverse count
219+
220+
221+-- Given a function which creates a sub-network (from a single event)
222+-- Create a plan which uses a switch to substitute it
223+switches :: TestPlan t m => Word -> (forall n. (MonadHold t n, MonadFix n) => Event t Word -> n (Event t a)) -> m (Event t a)
224+switches numFrames f = do
225+ es <- events numFrames
226+ switch <$> hold never (makeEvents es)
227+
228+ where
229+ makeEvents es = pushAlways (\n -> f (fmap (+n) es)) es
230+
231+
232+
233+-- Two sets of benchmarks, one which we're interested in testing subscribe time (as well as time to fire frames)
234+-- the other, which we're only interested in time for running frames
235+subscribing :: Word -> Word -> [(String, TestCase)]
236+subscribing n frames =
237+ [ testSub "fmapFanMerge" $ return . mergeList . fmapFan n
238+ , testSub "fanMerge" $ return . fanMerge n
239+ , testSub "fmapFan/mergeTree" $ return . mergeTree 8 . fmapFan n
240+ , testSub "fmapChain" $ return . fmapChain n
241+ , testSub "switchChain" $ switchChain n
242+ , testSub "switchPromptlyChain" $ switchPromptlyChain n
243+ , testSub "switchFactors" $ switchFactors n
244+ , testSub "coincidenceChain" $ return . coinChain n
245+ ]
246+
247+ where
248+ testSub :: (Eq a, Show a, NFData a) => String -> (forall t n. (Reflex t, MonadHold t n, MonadFix n) => Event t Word -> n (Event t a)) -> (String, TestCase)
249+ testSub name test = testE name (switches frames test)
250+
251+-- Set of benchmark to test specifically merging dynamic collections
252+-- a pattern which occurs frequently in reflex-dom collections
253+merging :: Word -> [(String, TestCase)]
254+merging n =
255+ [ testE "static events" $ mergeList <$> sparse
256+ , testE "increasing events" $ switchMergeEvents =<< increasingMerge =<< sparse
257+ , testE "decreasing events" $ switchMergeEvents =<< decreasingMerge =<< sparse
258+ , testE "modify events" $ switchMergeEvents =<< modifyMerge =<< sparse
259+
260+ , testB "static behaviors" $ pull . traverse sample <$> counters
261+ , testB "increasing behaviors" $ switchMergeBehaviors =<< increasingMerge =<< counters
262+ , testB "decreasing behaviors" $ switchMergeBehaviors =<< decreasingMerge =<< counters
263+ , testB "modify behaviors" $ switchMergeBehaviors =<< modifyMerge =<< counters
264+
265+
266+ -- , testE "increasing events/switchMerge" $ switchMergeMaybe =<< increasingMerge =<< sparse
267+ -- , testE "decreasing events/switchMerge" $ switchMergeMaybe =<< decreasingMerge =<< sparse
268+ -- , testE "modify events/switchMerge" $ switchMergeMaybe =<< modifyMerge =<< sparse
269+ ]
270+
271+ where
272+ sparse :: TestPlan t m => m [Event t Word]
273+ sparse = fmap (fmap (+1)) <$> occasional n 8 n
274+
275+ counters :: TestPlan t m => m [Behavior t Int]
276+ counters = countMany =<< sparse
277+
278+
279+
280+fans :: Word -> [(String, TestCase)]
281+fans n =
282+ [ testE "fanMapChain" $ fanMergeChain n 10 <$> e
283+ , testE "fmapFanMapChain" $ fmapFanMergeChain n 10 <$> e
284+
285+ , testE "fanMerge " $ fanMerge n <$> events n
286+
287+ , testE "fmapFanMerge " $ fmapFanMerge n <$> events n
288+ , testE "triggerMerge" $ leftmost <$> for [1..n] (\i -> plan [(i, i)])
289+
290+ ]
291+ where
292+
293+ e :: TestPlan t m => m (Event t Word)
294+ e = events 100
295+
296+
297+
298+
299+dynamics :: Word -> [(String, TestCase)]
300+dynamics n =
301+ [ testE "mapDynChain" $ fmap updated $ mapDynChain n =<< d
302+ , testE "joinDynChain" $ fmap updated $ joinDynChain n =<< d
303+ , testE "combineDynChain" $ fmap updated $ combineDynChain n =<< d
304+ , testE "dense mergeTree" $ fmap updated $ mergeTreeDyn 8 =<< dense
305+ , testE "sparse mergeTree" $ fmap updated $ mergeTreeDyn 8 =<< sparse
306+ , testE "mergeDyn" $ fmap updated $ mapDyn sum =<< mergeListDyn =<< sparse
307+ ] where
308+
309+ d :: TestPlan t m => m (Dynamic t Word)
310+ d = count =<< events 10
311+
312+ sparse, dense :: TestPlan t m => m [Dynamic t Int]
313+ sparse = countManyDyn =<< occasional n 8 16
314+ dense = countManyDyn =<< continuous n 2
315+
316+
317+firing :: Word -> [(String, TestCase)]
318+firing n =
319+ [ testE "fmapChain" $ fmapChain n <$> e
320+ , testE "switchChain2" $ switchChain2 n =<< e
321+ , testE "switchPromptlyChain" $ switchPromptlyChain n =<< e
322+ , testE "switchFactors" $ switchFactors n =<< e
323+ , testE "coincidenceChain" $ coinChain n <$> e
324+
325+ , testE "dense mergeTree" $ mergeTree 8 <$> dense
326+ , testE "sparse mergeTree" $ mergeTree 8 <$> sparse
327+
328+ -- Triggers test failure in Spider
329+ --, testE "runFrame" $ events n
330+
331+ , testB "sum counters" $ do
332+ counts <- counters
333+ return $ pull $ sum <$> traverse sample counts
334+
335+ , testB "pullChain" $ fmapChain n . current <$> (count =<< events 4)
336+ , testB "pullChain2" $ do
337+ es <- events 4
338+ e' <- plan [(0, ())]
339+
340+ b <- hold (constant 0) $
341+ pushAlways (const $ fmapChain n <$> hold 0 es) e'
342+
343+ return (join b)
344+ , testB "counters/pullTree" $ mergeTree 8 <$> counters
345+
346+ ]
347+ where
348+ e :: TestPlan t m => m (Event t Word)
349+ e = events 10
350+
351+ dense, sparse :: TestPlan t m => m [Event t Word]
352+ dense = continuous n 2
353+ sparse = occasional n 8 16
354+
355+ counters :: TestPlan t m => m [Behavior t Int]
356+ counters = countMany =<< sparse
357+
358+
359+
360+
361+
test/Reflex/Plan/Pure.hsnew file+64
@@ -0,0 +1,64 @@
1+{-# LANGUAGE GADTs, RankNTypes, FlexibleInstances, FlexibleContexts, MultiParamTypeClasses, GeneralizedNewtypeDeriving, ScopedTypeVariables #-}
2+
3+module Reflex.Plan.Pure where
4+
5+import Reflex
6+import Reflex.Pure
7+import Reflex.TestPlan
8+
9+import Control.Applicative
10+import Control.Monad.Fix
11+import Control.Monad.State
12+import Data.IntMap (IntMap)
13+import qualified Data.IntMap as IntMap
14+import Data.IntSet (IntSet)
15+import qualified Data.IntSet as IntSet
16+
17+import Data.Monoid
18+import Data.Bifunctor
19+import Data.Maybe
20+import Prelude
21+
22+
23+mapToPureEvent :: IntMap a -> Event (Pure Int) a
24+mapToPureEvent m = Event $ flip IntMap.lookup m
25+
26+type TimeM = (->) Int
27+newtype PurePlan a = PurePlan { unPlan :: StateT IntSet TimeM a } deriving (Functor, Applicative, Monad, MonadFix)
28+
29+liftPlan :: TimeM a -> PurePlan a
30+liftPlan = PurePlan . lift
31+
32+instance MonadHold (Pure Int) PurePlan where
33+ hold initial = liftPlan . hold initial
34+ holdDyn initial = liftPlan . holdDyn initial
35+ holdIncremental initial = liftPlan . holdIncremental initial
36+
37+instance MonadSample (Pure Int) PurePlan where
38+ sample = liftPlan . sample
39+
40+
41+instance TestPlan (Pure Int) PurePlan where
42+ plan occs = do
43+ PurePlan . modify $ IntSet.union (IntMap.keysSet m)
44+ return $ mapToPureEvent m
45+ where m = IntMap.fromList (first fromIntegral <$> occs)
46+
47+runPure :: PurePlan a -> (a, IntSet)
48+runPure (PurePlan p) = runStateT p mempty $ 0
49+
50+relavantTimes :: IntSet -> IntSet
51+relavantTimes occs = IntSet.fromList [0..l + 1]
52+ where l = fromMaybe 0 (fst <$> IntSet.maxView occs)
53+
54+testBehavior :: (Behavior (Pure Int) a, IntSet) -> IntMap a
55+testBehavior (b, occs) = IntMap.fromSet (sample b) (relavantTimes occs)
56+
57+testEvent :: (Event (Pure Int) a, IntSet) -> IntMap (Maybe a)
58+testEvent (Event readEvent, occs) = IntMap.fromSet readEvent (relavantTimes occs)
59+
60+
61+
62+
63+
64+
test/Reflex/Plan/Reflex.hsnew file+126
@@ -0,0 +1,126 @@
1+{-# LANGUAGE FunctionalDependencies, BangPatterns, UndecidableInstances, ConstraintKinds, GADTs, ScopedTypeVariables, FlexibleInstances, MultiParamTypeClasses, GeneralizedNewtypeDeriving, RankNTypes, RecursiveDo, FlexibleContexts, StandaloneDeriving #-}
2+module Reflex.Plan.Reflex
3+ ( TestPlan(..)
4+ , runPlan
5+ , Plan (..)
6+ , Schedule
7+ , Firing (..)
8+ , MonadIORef
9+
10+
11+ , readSchedule
12+ , readSchedule_
13+ , testSchedule
14+ , readEvent'
15+ , makeDense
16+
17+ , runTestE
18+ , runTestB
19+
20+ ) where
21+
22+import Reflex.Class
23+import Reflex.Host.Class
24+import Reflex.TestPlan
25+
26+import Control.Applicative
27+import Control.Monad
28+import Control.Monad.Identity
29+import Control.Monad.State.Strict
30+
31+import Data.Dependent.Sum (DSum (..))
32+import Data.Monoid
33+import Data.Traversable (traverse, sequenceA)
34+import Data.Maybe
35+import qualified Data.IntMap.Strict as IntMap
36+import Control.Monad.Ref
37+
38+import Data.IntMap.Strict (IntMap)
39+import Data.IORef
40+import System.Mem
41+import Control.Exception
42+import Control.DeepSeq
43+
44+-- Note: this import must come last to silence warnings from AMP
45+import Prelude
46+
47+type MonadIORef m = (MonadIO m, MonadRef m, Ref m ~ Ref IO)
48+
49+data Firing t where
50+ Firing :: IORef (Maybe (EventTrigger t a)) -> a -> Firing t
51+
52+
53+type Schedule t = IntMap [Firing t]
54+
55+-- Implementation of a TestPlan in terms of ReflexHost
56+newtype Plan t a = Plan (StateT (Schedule t) (HostFrame t) a)
57+
58+deriving instance ReflexHost t => Functor (Plan t)
59+deriving instance ReflexHost t => Applicative (Plan t)
60+deriving instance ReflexHost t => Monad (Plan t)
61+
62+deriving instance ReflexHost t => MonadSample t (Plan t)
63+deriving instance ReflexHost t => MonadHold t (Plan t)
64+deriving instance ReflexHost t => MonadFix (Plan t)
65+
66+
67+instance (ReflexHost t, MonadRef (HostFrame t), Ref (HostFrame t) ~ Ref IO) => TestPlan t (Plan t) where
68+ plan occurances = Plan $ do
69+ (e, ref) <- newEventWithTriggerRef
70+ modify (IntMap.unionWith mappend (firings ref))
71+ return e
72+
73+ where
74+ firings ref = IntMap.fromList (makeFiring ref <$> occurances)
75+ makeFiring ref (t, a) = (fromIntegral t, [Firing ref a])
76+
77+
78+firingTrigger :: (MonadReflexHost t m, MonadIORef m) => Firing t -> m (Maybe (DSum (EventTrigger t) Identity))
79+firingTrigger (Firing ref a) = fmap (:=> Identity a) <$> readRef ref
80+
81+runPlan :: (MonadReflexHost t m) => Plan t a -> m (a, Schedule t)
82+runPlan (Plan p) = runHostFrame $ runStateT p mempty
83+
84+
85+makeDense :: Schedule t -> Schedule t
86+makeDense s = fromMaybe (emptyRange 0) $ do
87+ (end, _) <- fst <$> IntMap.maxViewWithKey s
88+ return $ IntMap.union s (emptyRange end)
89+ where
90+ emptyRange end = IntMap.fromList (zip [0..end + 1] (repeat []))
91+
92+
93+-- For the purposes of testing, we add in a zero frame and extend one frame (to observe changes to behaviors
94+-- after the last event)
95+-- performGC is called at each frame to test for GC issues
96+testSchedule :: (MonadReflexHost t m, MonadIORef m, NFData a) => Schedule t -> ReadPhase m a -> m (IntMap a)
97+testSchedule schedule readResult = IntMap.traverseWithKey (\t occs -> liftIO performGC *> triggerFrame readResult t occs) (makeDense schedule)
98+
99+readSchedule :: (MonadReflexHost t m, MonadIORef m, NFData a) => Schedule t -> ReadPhase m a -> m (IntMap a)
100+readSchedule schedule readResult = IntMap.traverseWithKey (triggerFrame readResult) schedule
101+
102+readSchedule_ :: (MonadReflexHost t m, MonadIORef m, NFData a) => Schedule t -> ReadPhase m a -> m ()
103+readSchedule_ schedule readResult = mapM_ (uncurry $ triggerFrame readResult) $ IntMap.toList schedule
104+
105+triggerFrame :: (MonadReflexHost t m, MonadIORef m, NFData a) => ReadPhase m a -> Int -> [Firing t] -> m a
106+triggerFrame readResult _ occs = do
107+ triggers <- catMaybes <$> traverse firingTrigger occs
108+ liftIO . evaluate . force =<< fireEventsAndRead triggers readResult
109+
110+readEvent' :: MonadReadEvent t m => EventHandle t a -> m (Maybe a)
111+readEvent' = readEvent >=> sequenceA
112+
113+
114+-- Convenience functions for running tests producing Events/Behaviors
115+runTestB :: (MonadReflexHost t m, MonadIORef m, NFData a) => Plan t (Behavior t a) -> m (IntMap a)
116+runTestB p = do
117+ (b, s) <- runPlan p
118+ testSchedule s $ sample b
119+
120+runTestE :: (MonadReflexHost t m, MonadIORef m, NFData a) => Plan t (Event t a) -> m (IntMap (Maybe a))
121+runTestE p = do
122+ (e, s) <- runPlan p
123+ h <- subscribeEvent e
124+ testSchedule s (readEvent' h)
125+
126+
test/Reflex/Pure.hsnew file+155
@@ -0,0 +1,155 @@
1+{- | This module provides a pure implementation of Reflex, which is intended
2+to serve as a reference for the semantics of the Reflex class. All implementations
3+of Reflex should produce the same results as this implementation, although performance
4+and laziness/strictness may differ.
5+-}
6+
7+{-# LANGUAGE TypeFamilies, MultiParamTypeClasses, EmptyDataDecls, InstanceSigs, FlexibleInstances, ScopedTypeVariables #-}
8+
9+-- There are two expected orphan instances in this module:
10+-- * MonadSample (Pure t) ((->) t)
11+-- * MonadHold (Pure t) ((->) t)
12+{-# OPTIONS_GHC -fno-warn-orphans #-}
13+
14+module Reflex.Pure where
15+
16+import Reflex.Class
17+import Data.Functor.Misc
18+import Data.Functor.Identity
19+import Data.MemoTrie
20+import Data.Dependent.Map (DMap, GCompare)
21+import qualified Data.Dependent.Map as DMap
22+import Control.Monad
23+import Data.Monoid
24+
25+data Pure t
26+
27+-- | The Enum instance of t must be dense: for all x :: t, there must not exist
28+-- any y :: t such that pred x < y < x. The HasTrie instance will be used exclusively
29+-- to memoize functions of t, not for any of its other capabilities.
30+instance (Enum t, HasTrie t, Ord t) => Reflex (Pure t) where
31+
32+ newtype Behavior (Pure t) a = Behavior { unBehavior :: t -> a }
33+ newtype Event (Pure t) a = Event { unEvent :: t -> Maybe a }
34+ newtype Dynamic (Pure t) a = Dynamic { unDynamic :: t -> (a, Maybe a) }
35+ newtype Incremental (Pure t) p a = Incremental { unIncremental :: t -> (a, Maybe (p a)) }
36+
37+ type PushM (Pure t) = (->) t
38+ type PullM (Pure t) = (->) t
39+
40+ never :: Event (Pure t) a
41+ never = Event $ \_ -> Nothing
42+
43+ constant :: a -> Behavior (Pure t) a
44+ constant x = Behavior $ \_ -> x
45+
46+ push :: (a -> PushM (Pure t) (Maybe b)) -> Event (Pure t) a -> Event (Pure t) b
47+ push f e = Event $ memo $ \t -> unEvent e t >>= \o -> f o t
48+
49+ pull :: PullM (Pure t) a -> Behavior (Pure t) a
50+ pull = Behavior . memo
51+
52+ -- [UNUSED_CONSTRAINT]: The following type signature for merge will produce a warning because the GCompare instance is not used; however, removing the GCompare instance produces a different warning, due to that constraint being present in the original class definition
53+ --merge :: GCompare k => DMap k (Event (Pure t)) -> Event (Pure t) (DMap k Identity)
54+ merge events = Event $ memo $ \t ->
55+ let currentOccurrences = unwrapDMapMaybe (($ t) . unEvent) events
56+ in if DMap.null currentOccurrences
57+ then Nothing
58+ else Just currentOccurrences
59+
60+ fan :: GCompare k => Event (Pure t) (DMap k Identity) -> EventSelector (Pure t) k
61+ fan e = EventSelector $ \k -> Event $ \t -> unEvent e t >>= fmap runIdentity . DMap.lookup k
62+
63+ switch :: Behavior (Pure t) (Event (Pure t) a) -> Event (Pure t) a
64+ switch b = Event $ memo $ \t -> unEvent (unBehavior b t) t
65+
66+ coincidence :: Event (Pure t) (Event (Pure t) a) -> Event (Pure t) a
67+ coincidence e = Event $ memo $ \t -> unEvent e t >>= \o -> unEvent o t
68+
69+ current :: Dynamic (Pure t) a -> Behavior (Pure t) a
70+ current d = Behavior $ \t -> fst $ unDynamic d t
71+
72+ updated :: Dynamic (Pure t) a -> Event (Pure t) a
73+ updated d = Event $ \t -> snd $ unDynamic d t
74+
75+ unsafeBuildDynamic :: PullM (Pure t) a -> Event (Pure t) a -> Dynamic (Pure t) a
76+ unsafeBuildDynamic readV0 v' = Dynamic $ \t -> (readV0 t, unEvent v' t)
77+
78+ -- See UNUSED_CONSTRAINT, above.
79+ --unsafeBuildIncremental :: Patch p => PullM (Pure t) a -> Event (Pure t) (p a) -> Incremental (Pure t) p a
80+ unsafeBuildIncremental readV0 p = Incremental $ \t -> (readV0 t, unEvent p t)
81+
82+ mergeIncremental (Incremental f) = Event $ \t ->
83+ let results = DMap.mapMaybeWithKey (\_ (Event e) -> Identity <$> e t) $ fst $ f t
84+ in if DMap.null results
85+ then Nothing
86+ else Just results
87+
88+ currentIncremental (Incremental f) = Behavior $ \t -> fst $ f t
89+
90+ updatedIncremental (Incremental f) = Event $ \t -> snd $ f t
91+
92+ incrementalToDynamic (Incremental f) = Dynamic $ \t ->
93+ let (old, mPatch) = f t
94+ e = case mPatch of
95+ Nothing -> Nothing
96+ Just patch -> apply patch old
97+ in (old, e)
98+
99+instance Functor (Dynamic (Pure t)) where
100+ fmap f d = Dynamic $ \t -> let (cur, upd) = unDynamic d t
101+ in (f cur, fmap f upd)
102+
103+instance Applicative (Dynamic (Pure t)) where
104+ pure a = Dynamic $ \_ -> (a, Nothing)
105+ (<*>) = ap
106+
107+instance Monad (Dynamic (Pure t)) where
108+ return = pure
109+ (x :: Dynamic (Pure t) a) >>= (f :: a -> Dynamic (Pure t) b) = Dynamic $ \t ->
110+ let (curX :: a, updX :: Maybe a) = unDynamic x t
111+ (cur :: b, updOuter :: Maybe b) = unDynamic (f curX) t
112+ (updInner :: Maybe b, updBoth :: Maybe b) = case updX of
113+ Nothing -> (Nothing, Nothing)
114+ Just nextX -> let (c, u) = unDynamic (f nextX) t
115+ in (Just c, u)
116+ in (cur, getFirst $ mconcat $ map First [updBoth, updOuter, updInner])
117+
118+instance MonadSample (Pure t) ((->) t) where
119+
120+ sample :: Behavior (Pure t) a -> (t -> a)
121+ sample = unBehavior
122+
123+instance (Enum t, HasTrie t, Ord t) => MonadHold (Pure t) ((->) t) where
124+
125+ hold :: a -> Event (Pure t) a -> (t -> (Behavior (Pure t) a))
126+ hold initialValue e initialTime = Behavior f
127+ where f = memo $ \sampleTime ->
128+ -- Really, the sampleTime should never be prior to the initialTime,
129+ -- because that would mean the Behavior is being sampled before being created.
130+ if sampleTime <= initialTime
131+ then initialValue
132+ else let lastTime = pred sampleTime
133+ in case unEvent e lastTime of
134+ Nothing -> f lastTime
135+ Just x -> x
136+
137+ holdDyn :: a -> Event (Pure t) a -> (t -> (Dynamic (Pure t) a))
138+ holdDyn initialValue e initialTime =
139+ let Behavior f = hold initialValue e initialTime
140+ in Dynamic $ \t -> (f t, unEvent e t)
141+
142+ holdIncremental :: Patch p => a -> Event (Pure t) (p a) -> (t -> (Incremental (Pure t) p a))
143+ holdIncremental initialValue e initialTime = Incremental $ \t -> (f t, unEvent e t)
144+ where f = memo $ \sampleTime ->
145+ -- Really, the sampleTime should never be prior to the initialTime,
146+ -- because that would mean the Behavior is being sampled before being created.
147+ if sampleTime <= initialTime
148+ then initialValue
149+ else let lastTime = pred sampleTime
150+ lastValue = f lastTime
151+ in case unEvent e lastTime of
152+ Nothing -> lastValue
153+ Just x -> case apply x lastValue of
154+ Nothing -> lastValue
155+ Just v' -> v'
test/Reflex/Test.hsnew file+61
@@ -0,0 +1,61 @@
1+{-# LANGUAGE ConstraintKinds, GADTs, RankNTypes, FlexibleInstances, FlexibleContexts, MultiParamTypeClasses, GeneralizedNewtypeDeriving #-}
2+
3+module Reflex.Test
4+ ( testAgreement
5+ , compareResult
6+ , runTests
7+
8+ , module Reflex.TestPlan
9+
10+ ) where
11+
12+import Reflex.Spider
13+
14+import Reflex.TestPlan
15+
16+import Reflex.Plan.Pure
17+import Reflex.Plan.Reflex
18+
19+import Control.Monad
20+import Data.Monoid
21+
22+import Data.IntMap (IntMap)
23+
24+--import Data.Foldable
25+import System.Exit
26+
27+import Prelude
28+
29+
30+testAgreement :: TestCase -> IO Bool
31+testAgreement (TestE p) = do
32+ spider <- runSpiderHost $ runTestE p
33+ let results = [("spider", spider)]
34+
35+ compareResult results (testEvent $ runPure p)
36+
37+testAgreement (TestB p) = do
38+ spider <- runSpiderHost $ runTestB p
39+ let results = [("spider", spider)]
40+
41+ compareResult results (testBehavior $ runPure p)
42+
43+
44+compareResult :: (Show a, Eq a) => [(String, IntMap a)] -> IntMap a -> IO Bool
45+compareResult results expected = fmap and $ forM results $ \(name, r) -> do
46+
47+ when (r /= expected) $ do
48+ putStrLn ("Got: " ++ show (name, r))
49+ putStrLn ("Expected: " ++ show expected)
50+ return (r == expected)
51+
52+
53+runTests :: [(String, TestCase)] -> IO ()
54+runTests testCases = do
55+ results <- forM testCases $ \(name, test) -> do
56+ putStrLn $ "Test: " <> name
57+ testAgreement test
58+ exitWith $ if and results
59+ then ExitSuccess
60+ else ExitFailure 1
61+
test/Reflex/Test/CrossImpl.hsnew file+228
@@ -0,0 +1,228 @@
1+{-# LANGUAGE TypeFamilies, FlexibleContexts, FlexibleInstances, MultiParamTypeClasses, RankNTypes, GADTs, ScopedTypeVariables, FunctionalDependencies, RecursiveDo, UndecidableInstances, GeneralizedNewtypeDeriving, StandaloneDeriving, EmptyDataDecls, NoMonomorphismRestriction, TypeOperators, DeriveDataTypeable, PackageImports, TemplateHaskell, LambdaCase, BangPatterns, ConstraintKinds #-}
2+module Reflex.Test.CrossImpl (test) where
3+
4+import Prelude hiding (mapM, mapM_, sequence, sequence_, foldl, and)
5+
6+import Reflex.Class
7+import Reflex.Host.Class
8+import Reflex.Dynamic
9+import qualified Reflex.Spider.Internal as S
10+import qualified Reflex.Pure as P
11+import Control.Monad.Ref
12+
13+import Control.Monad.Identity hiding (mapM, mapM_, forM, forM_, sequence, sequence_)
14+import qualified Data.Set as Set
15+import Data.Map.Strict (Map)
16+import qualified Data.Map.Strict as Map
17+import Control.Arrow (second, (&&&))
18+import Data.Traversable
19+import Data.Foldable
20+import Control.Monad.State.Strict hiding (mapM, mapM_, forM, forM_, sequence, sequence_)
21+import Control.Monad.Writer hiding (mapM, mapM_, forM, forM_, sequence, sequence_)
22+import Data.Dependent.Map (DSum (..))
23+import System.Mem
24+import System.Exit
25+
26+import System.IO.Unsafe
27+
28+mapToPureBehavior :: Map Int a -> Behavior PureReflexDomain a
29+mapToPureBehavior m = P.Behavior $ \t -> case Map.lookupLE t m of
30+ Nothing -> error $ "mapToPureBehavior: no value for time " <> show t
31+ Just (_, v) -> v
32+
33+mapToPureEvent :: Map Int a -> Event PureReflexDomain a
34+mapToPureEvent m = P.Event $ flip Map.lookup m
35+
36+relevantTestingTimes :: (Map Int a, Map Int b) -> [Int]
37+relevantTestingTimes (b, e) = case Set.minView &&& Set.maxView $ Map.keysSet b `Set.union` Map.keysSet e of
38+ (Just (t0, _), Just (t1, _)) -> [t0..t1+1] -- We need to go to b+1 to see the result of the final event
39+ _ -> [] -- Doesn't actually make much sense
40+
41+type PureReflexDomain = P.Pure Int
42+type TimeM = (->) Int
43+
44+testPure :: (t ~ PureReflexDomain, m ~ TimeM) => ((Behavior t a, Event t b) -> m (Behavior t c, Event t d)) -> (Map Int a, Map Int b) -> (Map Int c, Map Int d)
45+testPure builder (b, e) =
46+ let (P.Behavior b', P.Event e') = ($ 0) $ builder (mapToPureBehavior b, mapToPureEvent e)
47+ relevantTimes = relevantTestingTimes (b, e)
48+ e'' = Map.mapMaybe id $ Map.fromList $ map (id &&& e') relevantTimes
49+ b'' = Map.fromList $ map (id &&& b') relevantTimes
50+ in (b'', e'')
51+
52+class MapMSignals a a' t t' | a -> t, a' -> t', a t' -> a', a' t -> a where
53+ mapMSignals :: Monad m => (forall b. Behavior t b -> m (Behavior t' b)) -> (forall b. Event t b -> m (Event t' b)) -> a -> m a'
54+
55+instance MapMSignals (Behavior t a) (Behavior t' a) t t' where
56+ mapMSignals fb _ = fb
57+
58+instance MapMSignals (Event t a) (Event t' a) t t' where
59+ mapMSignals _ fe = fe
60+
61+instance (MapMSignals a a' t t', MapMSignals b b' t t') => MapMSignals (a, b) (a', b') t t' where
62+ mapMSignals fb fe (a, b) = liftM2 (,) (mapMSignals fb fe a) (mapMSignals fb fe b)
63+
64+testSpider :: (forall m t. TestCaseConstraint t m => (Behavior t a, Event t b) -> m (Behavior t c, Event t d)) -> (Map Int a, Map Int b) -> (Map Int c, Map Int d)
65+testSpider builder (bMap, eMap) = unsafePerformIO $ S.runSpiderHost $ do
66+ (re, reTrigger) <- newEventWithTriggerRef
67+ (rb, rbTrigger) <- newEventWithTriggerRef
68+ b <- runHostFrame $ hold (error "testSpider: No value for input behavior yet") rb
69+ (b', e') <- runHostFrame $ builder (b, re)
70+ e'Handle <- subscribeEvent e' --TODO: This should be unnecessary
71+ let times = relevantTestingTimes (bMap, eMap)
72+ liftIO performGC
73+ outputs <- forM times $ \t -> do
74+ forM_ (Map.lookup t bMap) $ \val -> mapM_ (\ rbt -> fireEvents [rbt :=> Identity val]) =<< readRef rbTrigger
75+ bOutput <- sample b'
76+ eOutput <- liftM join $ forM (Map.lookup t eMap) $ \val -> do
77+ mret <- readRef reTrigger
78+ let firing = case mret of
79+ Just ret -> [ret :=> Identity val]
80+ Nothing -> []
81+ fireEventsAndRead firing $ sequence =<< readEvent e'Handle
82+ liftIO performGC
83+ return (t, (bOutput, eOutput))
84+ return (Map.fromList $ map (second fst) outputs, Map.mapMaybe id $ Map.fromList $ map (second snd) outputs)
85+
86+tracePerf :: Show a => a -> b -> b
87+tracePerf = flip const
88+
89+testAgreement :: (Eq c, Eq d, Show c, Show d) => (forall m t. TestCaseConstraint t m => (Behavior t a, Event t b) -> m (Behavior t c, Event t d)) -> (Map Int a, Map Int b) -> IO Bool
90+testAgreement builder inputs = do
91+ let identityResult = testPure builder inputs
92+ tracePerf "---------" $ return ()
93+ let spiderResult = testSpider builder inputs
94+ tracePerf "---------" $ return ()
95+ let resultsAgree = identityResult == spiderResult
96+ if resultsAgree
97+ then do putStrLn "Success:"
98+ print identityResult
99+ else do putStrLn "Failure:"
100+ putStrLn $ "Pure result: " <> show identityResult
101+ putStrLn $ "Spider result: " <> show spiderResult
102+ return resultsAgree
103+
104+type TestCaseConstraint t m = (Reflex t, MonadSample t m, MonadHold t m, MonadFix m, MonadFix (PushM t))
105+
106+data TestCase = forall a b c d. (Eq c, Eq d, Show c, Show d) => TestCase (Map Int a, Map Int b) (forall m t. TestCaseConstraint t m => (Behavior t a, Event t b) -> m (Behavior t c, Event t d))
107+
108+testCases :: [(String, TestCase)]
109+testCases =
110+ [ (,) "hold" $ TestCase (Map.singleton 0 "asdf", Map.fromList [(1, "qwer"), (2, "lkj")]) $ \(_, e) -> do
111+ b' <- hold "123" e
112+ return (b', e)
113+ , (,) "count" $ TestCase (Map.singleton 0 (), Map.fromList [(1, ()), (2, ()), (3, ())]) $ \(_, e) -> do
114+ e' <- liftM updated $ count e
115+ b' <- hold (0 :: Int) e'
116+ return (b', e')
117+ , (,) "onceE-1" $ TestCase (Map.singleton 0 "asdf", Map.fromList [(1, "qwer"), (2, "lkj")]) $ \(b, e) -> do
118+ e' <- onceE $ leftmost [e, e]
119+ return (b, e')
120+ , (,) "switch-1" $ TestCase (Map.singleton 0 "asdf", Map.fromList [(1, "qwer"), (2, "lkj")]) $ \(b, e) -> do
121+ let e' = fmap (const e) e
122+ b' <- hold never e'
123+ let e'' = switch b'
124+ return (b, e'')
125+ , (,) "switch-2" $ TestCase (Map.singleton 0 "asdf", Map.fromList [(1, "qwer"), (2, "lkj")]) $ \(b, e) -> do
126+ let e' = flip pushAlways e $ const $ do
127+ let ea = fmap (const "a") e
128+ let eb = fmap (const "b") e
129+ let eab = leftmost [ea, eb]
130+ liftM switch $ hold eab never
131+ e'' = coincidence e'
132+ return (b, e'')
133+ , (,) "switch-3" $ TestCase (Map.singleton 0 "asdf", Map.fromList [(1, "qwer"), (2, "lkj")]) $ \(b, e) -> do
134+ let e' = flip pushAlways e $ const $ do
135+ let ea = fmap (const "a") e
136+ let eb = fmap (const "b") e
137+ let eab = leftmost [ea, eb]
138+ liftM switch $ hold eab (fmap (const e) e)
139+ e'' = coincidence e'
140+ return (b, e'')
141+ , (,) "switch-4" $ TestCase (Map.singleton 0 "asdf", Map.fromList [(1, "qwer"), (2, "lkj")]) $ \(b, e) -> do
142+ let e' = leftmost [e, e]
143+ e'' <- liftM switch $ hold e' (fmap (const e) e)
144+ return (b, e'')
145+ , (,) "switchPromptly-1" $ TestCase (Map.singleton 0 (0 :: Int), Map.fromList [(1, "qwer"), (2, "lkj")]) $ \(b, e) -> do
146+ let e' = fmap (const e) e
147+ e'' <- switchPromptly never e'
148+ return (b, e'')
149+ , (,) "switchPromptly-2" $ TestCase (Map.singleton 0 (0 :: Int), Map.fromList [(1, "qwer"), (2, "lkj")]) $ \(b, e) -> do
150+ let e' = fmap (const e) e
151+ e'' <- switchPromptly never $ leftmost [e', e']
152+ return (b, e'')
153+ , (,) "switchPromptly-3" $ TestCase (Map.singleton 0 (0 :: Int), Map.fromList [(1, "qwer"), (2, "lkj")]) $ \(b, e) -> do
154+ let e' = leftmost [e, e]
155+ e'' <- switchPromptly never (fmap (const e) e')
156+ return (b, e'')
157+ , (,) "switchPromptly-4" $ TestCase (Map.singleton 0 (0 :: Int), Map.fromList [(1, "qwer"), (2, "lkj"), (3, "asdf")]) $ \(b, e) -> do
158+ let e' = leftmost [e, e]
159+ e'' <- switchPromptly never (fmap (const e') e)
160+ return (b, e'')
161+ , (,) "switch-5" $ TestCase (Map.singleton 0 (0 :: Int), Map.fromList [(1, "qwer"), (2, "lkj")]) $ \(b, e) -> do
162+ let e' = leftmost [e, e]
163+ e'' <- liftM switch $ hold never (fmap (const e') e)
164+ return (b, e'')
165+ , (,) "switchPromptly-5" $ TestCase (Map.singleton 0 (0 :: Int), Map.fromList [(1, "qwer"), (2, "lkj")]) $ \(b, e) -> do
166+ let e' = flip push e $ \_ -> do
167+ return . Just =<< onceE e
168+ e'' <- switchPromptly never e'
169+ return (b, e'')
170+ , (,) "switchPromptly-6" $ TestCase (Map.singleton 0 (0 :: Int), Map.fromList [(1, "qwer"), (2, "lkj")]) $ \(b, e) -> do
171+ let e' = flip pushAlways e $ \_ -> do
172+ switchPromptly e never
173+ e'' <- switchPromptly never e'
174+ return (b, e'')
175+ , (,) "coincidence-1" $ TestCase (Map.singleton 0 (0 :: Int), Map.fromList [(1, "qwer"), (2, "lkj")]) $ \(b, e) -> do
176+ let e' = flip pushAlways e $ \_ -> return $ fmap id e
177+ e'' = coincidence e'
178+ return (b, e'')
179+ , (,) "coincidence-2" $ TestCase (Map.singleton 0 (0 :: Int), Map.fromList [(1, "qwer"), (2, "lkj")]) $ \(b, e) -> do
180+ let e' = flip pushAlways e $ \_ -> return $ leftmost [e, e]
181+ e'' = coincidence e'
182+ return (b, e'')
183+ , (,) "coincidence-3" $ TestCase (Map.singleton 0 (0 :: Int), Map.fromList [(1, "qwer"), (2, "lkj")]) $ \(b, e) -> do
184+ let e' = flip pushAlways e $ \_ -> return $ coincidence $ fmap (const e) e
185+ e'' = coincidence e'
186+ return (b, e'')
187+ , (,) "coincidence-4" $ TestCase (Map.singleton 0 (0 :: Int), Map.fromList [(1, "qwer"), (2, "lkj"), (3, "asdf")]) $ \(b, e) -> do
188+ let e' = flip pushAlways e $ \_ -> onceE e
189+ e'' = coincidence e'
190+ return (b, e'')
191+ , (,) "coincidence-5" $ TestCase (Map.singleton 0 (0 :: Int), Map.fromList [(1, "qwer")]) $ \(b, e) -> do
192+ let eChild = flip pushAlways e $ const $ do
193+ let eNewValues = leftmost [e, e]
194+ return $ coincidence $ fmap (const eNewValues) eNewValues
195+ e' = coincidence eChild
196+ return (b, e')
197+ , (,) "coincidence-6" $ TestCase (Map.singleton 0 (0 :: Int), Map.fromList [(1, "qwer")]) $ \(b, e) -> do
198+ let eChild = flip pushAlways e $ const $ do
199+ let e' = coincidence $ fmap (const e) e
200+ return $ leftmost [e', e']
201+ e'' = coincidence eChild
202+ return (b, e'')
203+ , (,) "coincidence-7" $ TestCase (Map.singleton 0 (0 :: Int), Map.fromList [(1, "qwer"), (2, "lkj"), (3, "asdf")]) $ \(b, e) -> do
204+ let e' = leftmost [e, e]
205+ eCoincidences = coincidence $ fmap (const e') e
206+ return (b, eCoincidences)
207+ , (,) "holdWhileFiring" $ TestCase (Map.singleton 0 "zxc", Map.fromList [(1, "qwer"), (2, "lkj")]) $ \(b, e) -> do
208+ eo <- onceE e
209+ bb <- hold b $ pushAlways (const $ hold "asdf" eo) eo
210+ let b' = pull $ sample =<< sample bb
211+ return (b', e)
212+ , (,) "joinDyn" $ TestCase (Map.singleton 0 (0 :: Int), Map.fromList [(1, "qwer"), (2, "lkj")]) $ \(b, e) -> do
213+ bb <- hold "b" e
214+ bd <- hold never . fmap (const e) =<< onceE e
215+ eOuter <- liftM (pushAlways sample . fmap (const bb)) $ onceE e
216+ let eInner = switch bd
217+ e' = leftmost [eOuter, eInner]
218+ return (b, e')
219+ ]
220+
221+test :: IO ()
222+test = do
223+ results <- forM testCases $ \(name, TestCase inputs builder) -> do
224+ putStrLn $ "Test: " <> name
225+ testAgreement builder inputs
226+ exitWith $ if and results
227+ then ExitSuccess
228+ else ExitFailure 1
test/Reflex/Test/Micro.hsnew file+258
@@ -0,0 +1,258 @@
1+{-# LANGUAGE ConstraintKinds, GADTs, RankNTypes, FlexibleInstances, FlexibleContexts, MultiParamTypeClasses, GeneralizedNewtypeDeriving #-}
2+
3+module Reflex.Test.Micro (testCases) where
4+
5+import Reflex
6+import Reflex.TestPlan
7+
8+import Control.Monad
9+import Control.Applicative
10+import Data.Char
11+import Data.Monoid
12+import Data.Functor.Misc
13+
14+import Data.Foldable
15+
16+import qualified Data.Map as Map
17+
18+import Prelude
19+
20+testCases :: [(String, TestCase)]
21+testCases =
22+ [ testB "hold" $ hold "0" =<< events1
23+
24+ , testB "count" $ do
25+ b <- current <$> (count =<< events2)
26+ return $ (+ (0::Int)) <$> b
27+
28+ , testB "pull-1" $ do
29+ b <- hold "0" =<< events1
30+ return (id <$> id <$> b)
31+
32+ , testB "pull-2" $ do
33+ b1 <- behavior1
34+ return (id <$> pull $ liftA2 (<>) (sample b1) (sample b1))
35+
36+ , testB "pull-3" $ do
37+ b1 <- behavior1
38+ b2 <- behavior2
39+ return (id <$> pull $ liftA2 (<>) (sample b1) (sample b2))
40+
41+ , testB "pull-4" $ do
42+ es <- planList ["a", "b", "c"]
43+ e <- plan [(0, ())]
44+ b <- hold (constant "") $
45+ pushAlways (const $ hold "z" es) e
46+ return (join b)
47+
48+ , testE "tag-1" $ do
49+ b1 <- behavior1
50+ e <- events2
51+ return (tag b1 e)
52+
53+ , testE "tag-2" $ do
54+ b1 <- behavior1
55+ e <- events2
56+ return (tag (map toUpper <$> b1) e)
57+
58+ , testE "attach-1" $ do
59+ b1 <- behavior1
60+ e <- events2
61+ return (attachWith (++) (map toUpper <$> b1) e)
62+
63+ , testE "leftmost" $ liftA2 leftmost2 events1 events2
64+
65+ , testE "appendEvents-1" $ liftA2 appendEvents events1 events2
66+
67+ , testE "appendEvents-2" $ liftA2 appendEvents events3 events2
68+
69+ , testE "merge-1" $ do
70+ e <- events1
71+ return $ leftmost ["x" <$ e, "y" <$ e]
72+
73+ , testE "merge-2" $ do
74+ e <- events1
75+ let m = mergeMap $ Map.fromList [(1::Int, "y" <$ e), (2, "z" <$ e)]
76+ let ee = flip pushAlways e $ const $ return m
77+ return $ coincidence ee
78+
79+ , testE "onceE-1" $ do
80+ e <- events1
81+ onceE $ leftmost [e, e]
82+
83+ , testE "onceE-2" $ do
84+ e <- events1
85+ b <- hold never (e <$ e)
86+ onceE $ switch b
87+
88+ , testE "switch-1" $ do
89+ e <- events1
90+ b <- hold never (e <$ e)
91+ return $ switch b
92+
93+ , testE "switch-2" $ do
94+ e <- events1
95+ return $ coincidence $ flip pushAlways e $ const $ do
96+ switch <$> hold (leftmost ["x" <$ e, "y" <$ e, "z" <$ e]) (e <$ e)
97+
98+ , testE "switch-3" $ do
99+ e <- events1
100+ return $ coincidence $ flip pushAlways e $ const $ do
101+ switch <$> hold (leftmost ["x" <$ e, "y" <$ e, "z" <$ e]) never
102+
103+ , testE "switch-4" $ do
104+ e <- events1
105+ switch <$> hold (deep e) (e <$ e)
106+
107+ , testE "switch-5" $ do
108+ e <- events1
109+ return $ coincidence $ flip pushAlways e $ const $
110+ return $ leftmost ["x" <$ e, "y" <$ e, "z" <$ e]
111+
112+ , testE "switch-6" $ do
113+ e <- events1
114+ return $ coincidence $ flip pushAlways e $ const $ do
115+ switch <$> hold ("x" <$ e) (e <$ e)
116+
117+ , testE "switchPromptly-1" $ do
118+ e <- events1
119+ let e' = e <$ e
120+ switchPromptly never $ e <$ e'
121+
122+ , testE "switchPromptly-2" $ do
123+ e <- events1
124+ switchPromptly never $ deep (e <$ e)
125+
126+ , testE "switchPromptly-3" $ do
127+ e <- events1
128+ switchPromptly never $ (e <$ deep e)
129+
130+ , testE "switchPromptly-4" $ do
131+ e <- events1
132+ switchPromptly never $ (deep e <$ e)
133+
134+ , testE "switch-5" $ do
135+ e <- events1
136+ switch <$> hold never (deep e <$ e)
137+
138+ , testE "switchPromptly-5" $ do
139+ e <- events1
140+ switchPromptly never $ flip push e $
141+ const (Just <$> onceE e)
142+
143+ , testE "switchPromptly-6" $ do
144+ e <- events1
145+ switchPromptly never $ flip pushAlways e $
146+ const (switchPromptly e never)
147+
148+ , testE "coincidence-1" $ do
149+ e <- events1
150+ return $ coincidence $ flip pushAlways e $
151+ const $ return (id <$> e)
152+
153+ , testE "coincidence-2" $ do
154+ e <- events1
155+ return $ coincidence $ flip pushAlways e $
156+ const $ return (deep e)
157+
158+ , testE "coincidence-3" $ do
159+ e <- events1
160+ return $ coincidence $ flip pushAlways e $
161+ const $ return (coincidence (e <$ e))
162+
163+ , testE "coincidence-4" $ do
164+ e <- events1
165+ return $ coincidence $ flip pushAlways e $
166+ const (onceE e)
167+
168+ , testE "coincidence-5" $ do
169+ e <- events1
170+ return $ coincidence $ flip pushAlways e $ const $ do
171+ let e' = deep e
172+ return (coincidence (e' <$ e'))
173+
174+ , testE "coincidence-6" $ do
175+ e <- events1
176+ return $ coincidence $ flip pushAlways e $ const $ do
177+ let e' = coincidence (e <$ e)
178+ return $ deep e'
179+
180+ , testE "coincidence-7" $ do
181+ e <- events1
182+ return $ coincidence (deep e <$ e)
183+
184+ , testB "holdWhileFiring" $ do
185+ e <- events1
186+ eo <- onceE e
187+ bb <- hold (constant "x") $ pushAlways (const $ hold "a" eo) eo
188+ return $ pull $ sample =<< sample bb
189+
190+ , testE "joinDyn" $ do
191+ e <- events1
192+ bb <- hold "b" e
193+ bd <- hold never . fmap (const e) =<< onceE e
194+
195+ eOuter <- pushAlways sample . fmap (const bb) <$> onceE e
196+ let eInner = switch bd
197+ return $ leftmost [eOuter, eInner]
198+
199+ , testB "foldDyn" $ do
200+ d <- foldDyn (++) "0" =<< events1
201+ return (current d)
202+
203+ , testB "mapDyn" $ do
204+ d <- foldDyn (++) "0" =<< events1
205+ current <$> mapDyn (map toUpper) d
206+
207+ , testB "combineDyn" $ do
208+ d1 <- foldDyn (++) "0" =<< events1
209+ d2 <- mapDyn (map toUpper) =<< foldDyn (++) "0" =<< events2
210+
211+ current <$> combineDyn (<>) d1 d2
212+ , testE "fan-1" $ do
213+ e <- fmap toMap <$> events1
214+ let es = select (fanMap e) . Const2 <$> values
215+
216+ return (mergeList es)
217+
218+ , testE "fan-2" $ do
219+ e <- fmap toMap <$> events3
220+ let es = select (fanMap e) . Const2 <$> values
221+
222+ return (mergeList es)
223+
224+ , testE "fan-3" $ do
225+ f <- fanMap <$> fmap toMap <$> events3
226+ return $ select f (Const2 'c')
227+
228+ , testE "fan-4" $ do
229+ e <- fmap toMap <$> events1
230+ return $ toUpper <$> select (fanMap e) (Const2 'a')
231+
232+ , testE "fan-5" $ do
233+ e <- fmap toMap <$> events2
234+ return $ toUpper <$> select (fanMap e) (Const2 'c')
235+
236+ , testE "fan-6" $ do
237+ f <- fanMap <$> fmap toMap <$> events1
238+ return $ toList <$> mergeList [ select f (Const2 'b'), select f (Const2 'b'), select f (Const2 'e'), select f (Const2 'e') ]
239+
240+ ] where
241+
242+ events1, events2, events3 :: TestPlan t m => m (Event t String)
243+ events1 = plan [(1, "a"), (2, "b"), (5, "c"), (7, "d"), (8, "e")]
244+ events2 = plan [(1, "e"), (3, "d"), (4, "c"), (6, "b"), (7, "a")]
245+
246+ events3 = liftA2 appendEvents events1 events2
247+
248+ values = "abcde"
249+ toMap str = Map.fromList $ map (\c -> (c, c)) str
250+
251+ behavior1, behavior2 :: forall t m. TestPlan t m => m (Behavior t String)
252+ behavior1 = hold "1" =<< events1
253+ behavior2 = hold "2" =<< events2
254+
255+ deep e = leftmost [e, e]
256+ leftmost2 e1 e2 = leftmost [e1, e2]
257+
258+
test/Reflex/Test/RunAll.hsnew file+44
@@ -0,0 +1,44 @@
1+{-# LANGUAGE ConstraintKinds, GADTs, RankNTypes, FlexibleInstances, FlexibleContexts, MultiParamTypeClasses, GeneralizedNewtypeDeriving #-}
2+
3+module Main (main) where
4+
5+import Reflex.Test
6+
7+import qualified Reflex.Bench.Focused as Focused
8+import qualified Reflex.Test.Micro as Micro
9+import Data.Functor
10+import Data.Bifunctor
11+import Data.List
12+
13+import System.Exit
14+import System.Environment
15+
16+import Prelude
17+
18+matchPrefixes :: [String] -> (String -> Bool)
19+matchPrefixes [] = const True
20+matchPrefixes args = \name -> any (`isPrefixOf` name) args
21+
22+
23+main :: IO ()
24+main = do
25+ args <- getArgs
26+
27+ case args of
28+ ["--list"] -> mapM_ putStrLn (fst <$> allTests) >> exitWith (ExitFailure 1)
29+ _ -> case filter (matchPrefixes args . fst) allTests of
30+ [] -> putStrLn "filter did not match any tests" >> exitWith (ExitFailure 1)
31+ tests -> runTests tests
32+
33+ where
34+ allTests = concat
35+ [ makeGroup "micro" Micro.testCases
36+ , makeGroup "subscribing (100,40)" (Focused.subscribing 100 40)
37+ , makeGroup "firing 1000" (Focused.firing 1000)
38+ , makeGroup "merge 100" (Focused.merging 100)
39+ , makeGroup "fan 50" (Focused.fans 50)
40+ ]
41+
42+ makeGroup name tests = first (\test -> intercalate "/" [name, test]) <$> tests
43+
44+
test/Reflex/TestPlan.hsnew file+45
@@ -0,0 +1,45 @@
1+{-# LANGUAGE FunctionalDependencies, BangPatterns, UndecidableInstances, ConstraintKinds, GADTs, ScopedTypeVariables, FlexibleInstances, MultiParamTypeClasses, GeneralizedNewtypeDeriving, RankNTypes, RecursiveDo, FlexibleContexts, StandaloneDeriving #-}
2+module Reflex.TestPlan
3+ ( TestPlan(..)
4+
5+ , TestCase (..)
6+ , testE, testB
7+ , TestE, TestB
8+
9+ , planList
10+
11+ ) where
12+
13+import Reflex.Class
14+import Control.Monad.Fix
15+import Data.Word
16+import Control.DeepSeq
17+
18+
19+import Prelude
20+
21+class (Reflex t, MonadHold t m, MonadFix m) => TestPlan t m where
22+ -- | Speicify a plan of an input Event firing
23+ -- Occurances must be in the future (i.e. Time > 0)
24+ -- Initial specification is
25+
26+ plan :: [(Word, a)] -> m (Event t a)
27+
28+
29+planList :: TestPlan t m => [a] -> m (Event t a)
30+planList xs = plan $ zip [1..] xs
31+
32+type TestE a = forall t m. TestPlan t m => m (Event t a)
33+type TestB a = forall t m. TestPlan t m => m (Behavior t a)
34+
35+data TestCase where
36+ TestE :: (Show a, Eq a, NFData a) => TestE a -> TestCase
37+ TestB :: (Show a, Eq a, NFData a) => TestB a -> TestCase
38+
39+-- Helpers to declare test cases
40+testE :: (Eq a, Show a, NFData a) => String -> TestE a -> (String, TestCase)
41+testE name test = (name, TestE test)
42+
43+testB :: (Eq a, Show a, NFData a) => String -> TestB a -> (String, TestCase)
44+testB name test = (name, TestB test)
45+
test/hlint.hsdeleted−39
@@ -1,39 +0,0 @@
1−module Main where
2−
3−import Control.Monad
4−import Language.Haskell.HLint (hlint)
5−import System.Directory
6−import System.Exit (exitFailure, exitSuccess)
7−import System.FilePath
8−import System.FilePath.Find
9−
10−main :: IO ()
11−main = do
12− pwd <- getCurrentDirectory
13− let runHlint f = hlint $ f:
14− [ "--ignore=Redundant do"
15− , "--ignore=Use camelCase"
16− , "--ignore=Redundant $"
17− , "--ignore=Use &&"
18− , "--ignore=Use &&&"
19− , "--ignore=Use const"
20− , "--ignore=Use >=>"
21− , "--ignore=Use ."
22− , "--ignore=Use unless"
23− , "--ignore=Reduce duplication"
24− , "--cpp-define=USE_TEMPLATE_HASKELL"
25− , "--ignore=Use tuple-section"
26− , "--ignore=Unused LANGUAGE pragma" -- hlint3 falsely believes that TypeOperators is not needed
27− ]
28− recurseInto = and <$> sequence
29− [ fileType ==? Directory
30− , fileName /=? ".git"
31− ]
32− matchFile = and <$> sequence
33− [ extension ==? ".hs"
34− ]
35− files <- find recurseInto matchFile (pwd </> "src") --TODO: Someday fix all hints in tests, etc.
36− ideas <- fmap concat $ forM files $ \f -> do
37− putStr $ "linting file " ++ drop (length pwd + 1) f ++ "... "
38− runHlint f
39− if null ideas then exitSuccess else exitFailure
test/tests.hsdeleted−43
@@ -1,43 +0,0 @@
1−{-# LANGUAGE TemplateHaskell #-}
2−module Main where
3−
4−import Test.HUnit (runTestTT, (~:), assertEqual, errors, failures, test)
5−import Data.Patch ( Patch(apply) )
6−import Data.Patch.MapWithMove ( patchThatChangesMap )
7−import Data.Map as Map ( Map, fromList, singleton )
8−import Hedgehog (checkParallel, discover, Property, property, forAll, PropertyT, (===))
9−import Hedgehog.Gen as Gen ( int )
10−import Hedgehog.Range as Range ( linear )
11−import Control.Monad (replicateM)
12−import System.Exit (exitFailure, exitSuccess)
13−import Data.Sequence as Seq ( foldMapWithIndex, replicateM )
14−
15−main :: IO ()
16−main = do
17− counts <- runTestTT $ test [
18− "Simple Move" ~: (do
19− let mapBefore = Map.fromList [(0,1)]
20− mapAfter = Map.fromList [(0,0),(1,1)]
21− patch = patchThatChangesMap mapBefore mapAfter
22− afterPatch = apply patch mapBefore
23− assertEqual "Patch creates the same Map" (Just mapAfter) afterPatch),
24− "Property Checks" ~: propertyChecks
25− ]
26− if errors counts + failures counts == 0 then exitSuccess else exitFailure
27−
28−propertyChecks :: IO Bool
29−propertyChecks = checkParallel $$(discover)
30−
31−prop_patchThatChangesMap :: Property
32−prop_patchThatChangesMap = property $ do
33− mapBefore <- makeRandomIntMap
34− mapAfter <- makeRandomIntMap
35− let patch = patchThatChangesMap mapBefore mapAfter
36− Just mapAfter === apply patch mapBefore
37−
38−makeRandomIntMap :: Monad m => PropertyT m (Map Int Int)
39−makeRandomIntMap = do
40− let genNum = Gen.int (Range.linear 0 100)
41− length <- forAll genNum
42− listOfNumbers <- forAll $ Seq.replicateM length genNum
43− pure $ Seq.foldMapWithIndex Map.singleton listOfNumbers