-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Derived combinators to sell EDSLs in the paper #19
Comments
The only one of these that we can generate code for right now is |
This is a really simple combinator, but it would be nice to have an infinite loop combinator loop :: SSM () -> SSM ()
loop = while' true' would also be great to have a do-while loop kind of construct: doWhile :: Exp Bool -> SSM () -> SSM ()
doWhile c b = b >> while' c b note that
until :: Exp Bool -> SSM () -> SSM ()
until c = while $ not c All basic sugar, but nice to have when writing programs. |
One-shot and its variations: Basic one-shot: oneShot :: SSMTime -> Bool -> Ref () -> Ref Bool -> SSM ()
oneShot delay low i o = while True $ do
wait [i]
o <~ not low
after delay, o <~ low A more type-generic version that acts more like a functor, mapping over values of the input reference: oneShotMap :: SSMTime -> Exp b -> (Exp a -> Exp b) -> Ref a -> Ref b -> SSM ()
oneShotMap delay low f i o = while True $ do
wait [i]
o <~ f (deref i)
after delay, o <~ low We can redefine oneShot delay low = oneShotMap delay low (const $ not low) For the following variations, I'll use the type-specific version for brevity.The above implementation will issue an additional instantaneous assignment if it receives two consecutive input signals before it is able to reset the output signal. While the additional assignment may seem redundant, it has the effect of waking up waiting processes (though with no change in value). That may or may not be desirable. This implementation ensures that only alternating assignments are made: oneShot' :: SSMTime -> Exp Bool -> Ref () -> Ref Bool -> SSM ()
oneShot' delay low i o = while True $ do
wait [i]
when (deref o != low) $ o <~ not low
after delay, o <~ low -- extends scheduled shut-off time Note that if the scheduled Meahwhile, if we wanted to imitate the behavior of the one-shot circuit shown here, we need to make a few changes. First, the input needs to be a oneShotW :: SSMTime -> Exp Bool -> Ref Bool -> Ref Bool -> SSM ()
oneShotW delay low i o = while True $ do
-- wait until i <~ true
while True $ do
wait [i]
when (deref i) $ break
-- raise o
o <~ not low
-- set alarm
wake <- var ()
after delay, wake <~ ()
wait [wake]
-- reset o
o <~ low Note that here we use an internal alarm rather than waiting on The implementation of The fact that oneShotW_ :: SSMTime -> SSMTime -> Exp Bool -> Ref Bool -> Ref Bool -> SSM ()
oneShotW_ latency delay low i o = while True $ do
-- wait until i <~ true
while True $ do
wait [i]
when (deref i) $ break
-- raise o
after latency, o <~ not low
-- set alarm
wake <- var ()
after delay, wake <~ ()
wait [wake]
-- reset o
after latency, o <~ low
oneShot'_ :: SSMTime -> SSMTime -> Exp Bool -> Ref () -> Ref Bool -> SSM ()
oneShot'_ latency delay low i o = while True $ do
wait [i]
when (deref o != low) $ after latency, o <~ not low
after (latency + delay), o <~ low We have a number of interesting combinators that we can extract from this example. This one translates instantaneous assignments to one variable to delayed assignments on another. When scheduled as a low priority process, this can be used to forward instananeous assignments to high priority output handlers: latency :: SSMTime -> Ref a -> Ref a -> SSM ()
latency d i o = while True $ do
wait [i]
after d, o <~ deref i The idea is that This implementation assumes that With the current runtime system, there isn't a way to distinguish between an instantaneous and delayed assignment. Having such a feature may be useful for writing low-priority processes that convert only instantaneous assignments into delayed assignments, i.e., mimicking a default delay. From waitUntil :: [Ref a] -> SSMExp Bool -> SSM ()
waitUntil refs cond = while True $ wait refs >> when cond break |
"The fact that oneShotW uses instantaneous assignment precludes the use of high priority output handlers for o that can only evaluate delayed updates. We can adjust the implementations of oneShotW and oneShot' to only use delayed assignment, parametrized by some small latency that is used in place of the instananeous assignment" This sounds like a really good place for adding perhaps a new primitive RTS function. Immediate assignment which wakes up everyone. This primitive would be available only to us as compiler developers, and not exposed to programmers. Or perhaps it could be, but very well documented and with a name like Things like the |
With an EDSL, implementing derived combinators from the primitives becomes very easy. If we can think of some nice derived operators to showcase in the August paper, that would be nice. The first candidate (stolen from Stephens paper) is the
waitAll
combinator.wait
waits for any of the references to be written to, whilewaitAll
will wait for all of them to be written at least once.Another simple combinators is a simple delay, which behaves similarly to a conventional
sleep
function.Another pattern that I think might be useful to make a combinator for is that of alternating a reference between two values (e.g blinking a led with a set interval).
This can naturally be generalized to a more general
cycle
primitive which does the same but for a sequence of values. We can not implement this yet, as we need to add support for lists to do that.With this, alternate in turn just becomes a specific case of
cycle
.Now we see another pattern that we could perhaps abstract away (but that would be mainly for our own sake, not for the end programmer). These derived combinators all define some procedure that does the work and issues a fork call to that procedure. I am not sure how to generalize it right now, since we pass in the procedure & parameter names, but maybe it's something we can look at in the future.
@j-hui mentioned a combinator that would make allocation and deallocation of a resource explicit. The idea is that we want to fork a process that allocates some resource, passes that resource to additional processes that operate on them, and then deallocates the resource. Very similar to these C patterns:
I don't think we can derive such a combinator right now, but it's interesting to look at. It would surely be a very handy combinator for handling memory resources etc when we are developing IoT applications. I sketched this rough code up yesterday:
As my comment says, it's not possible to generate code for
superviseprocess
yet. Not sure what the best way to go around this is. Specialization? @koengit ?The text was updated successfully, but these errors were encountered: