Comments (7)
I may be wrong, but it looks like the only use of Typeable is here.
These should be replaceable with unsafeCoerce
(this seems to be a running theme in my solutions!), as long as fromUniverse
is never called with the right channel but expecting the wrong type.
from reactive-banana.
That would probably work, but I'm hesitant to use unsafeCoerce
again. Instead, I'd like to use an existing package, like vault
or thelike. The Universe
type might be an interesting addition to the vault
package; after all, it's a disjoint sum, dual to the "infinite" product Vault
.
from reactive-banana.
To be honest, I'm not entirely sure what the Universe
type's semantics are; the closest API that does not let the user perform an unsafe coercion seems to be:
data Universe
data Portal a
toUniverse :: a -> (Portal a, Universe)
fromUniverse :: Portal a -> Universe -> Maybe a
where fromUniverse p u
is Just x
iff (p,u) = toUniverse x
and Nothing
otherwise. The issue with this API is that two different calls of toUniverse
with the same arguments are distinguishable from another, because the portal of one will not work with the universe of the other; thus breaking referential transparency. Or possibly
data Universe
data Portal a
newPortal :: IO (Portal a)
toUniverse :: Portal a -> a -> Universe
fromUniverse :: Portal a -> Universe -> Maybe a
where fromUniverse p u
is Just x
iff u = toUniverse p x
, and Nothing
otherwise. The issue there is that after one uses toUniverse
on a portal, that portal cannot be used to create any more universes, or type-safety is violated; so toUniverse
would have to be in IO (or at least ST), which seems a shame.
I admit that I can't figure out a use for these safe APIs — it seems like portals would only be useful through an existential, and I'm not sure how you'd achieve that in practice — but after looking at fromAddHandler's implementation I've implemented a variant of the first API with toUniverse
in ST (or IO) and reusing the keys used for vaults in place of portals in the universe branch of my fork of vault
, to distract myself from some ugly Template Haskell issues in another project.
Unfortunately this required turning (Key k)
pattern matches into (Key (I.Key k))
pattern matches, but since these are only used in the small implementation modules of the structures themselves, it shouldn't matter too much. The runtime representation is identical.
from reactive-banana.
I think that the second API looks good. It must be safe because it can be implemented in terms of Vault
. :-)
type Universe = Vault
type Portal a = Key a
newPortal = newKey
toUniverse key a = insert key a empty
fromUniverse = lookup
Of course, the type
is to be understood as a newtype
whose implementation is not exported.
Since this is so very simple, it may not be worth adding it to the vault
package, after all; or at least under a different name, something paralleling Data.Dynamic
would probably be more fitting. (I originally chose Universe
because it's "the universal type" which contains all others.)
from reactive-banana.
Hmm, yes, it can be implemented in terms of Vault, but the problem is that the same Portal can be used with multiple universes. There's no type error, but you can still get "the wrong value" out.
I suppose this isn't a problem; you might even want to do it intentionally, to "modify" (persistently) the value inside the universe.
from reactive-banana.
Ah, I see. You mentioned the same issue with Vault
. One can add a type parameter and make it Vault s
to enforce different keys for different vaults, but that's probably more trouble than it's worth.
Thanks a lot for this discussion, I now have a clear idea of how to get rid of the Typeable
constraint by using Vault
. I'll leave this open until I get around to implementing the change.
from reactive-banana.
I've removed the Typable
constraint from the fromAddHandler
function with the help of the Data.Vault
module from the vault
package.
from reactive-banana.
Related Issues (20)
- Successful evaluation for an undefined network HOT 5
- `reactive-banana` can't be compiled on GHC 9.2 HOT 2
- I created a Matrix channel HOT 8
- An interesting `Applicative`-ish way of combining `Event`s HOT 4
- Add `MonadMoment` instances for all monad transformers in `transformers` HOT 1
- Release 1.3.0.0 HOT 3
- Join Stackage HOT 4
- `Behavior` constantly re-evaluating when caching is expected HOT 4
- Unexpected memory growth HOT 6
- Event handler not seeing early events. HOT 12
- `Behavior`s can hold onto more memory than they need to HOT 2
- Memory leak with dynamic behavior switching HOT 16
- Gloss GUI events not responding HOT 5
- Idea: add a combinator to track when a behavior is a different value HOT 14
- Call `unregister` for garbage collected events made by `AddHandler`. HOT 9
- Discrepancy in model and implementation HOT 15
- Optimize representation of Latch HOT 1
- Not compatible with these-1.2 (disallowed) HOT 2
- Simultaneous events can have undefined behavior within execute HOT 2
- source-code formatter? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from reactive-banana.