is go an acceptable cml?

21 September 2016 9:29 PM (go | concurrency | ml | ignorance | guile | clojure | core.async)

Yesterday I tried to summarize the things I know about Concurrent ML, and I came to the tentative conclusion that Go (and any Go-like system) was an acceptable CML. Turns out I was both wrong and right.

you were wrong when you said everything's gonna be all right

I was wrong, in the sense that programming against the CML abstractions lets you do more things than programming against channels-and-goroutines. Thanks to Sam Tobin-Hochstadt to pointing this out. As an example, consider a little process that tries to receive a message off a channel, and times out otherwise:

func withTimeout(ch chan int, timeout int) (result int) {
  var timeoutChannel chan int;
  var msg int;
  go func() {
    timeoutChannel <- 0
  select {
    case msg = <-ch: return msg;
    case msg = <-timeoutChannel: return 0;

I think that's the first Go I've ever written. I don't even know if it's syntactically valid. Anyway, I think we see how it should work. We return the message from the channel, unless the timeout happens before.

But, what if the message is itself a composite message somehow? For example, say we have a transformer that reads a value from a channel and adds 1 to it:

func onePlus(in chan int) (result chan int) {
  var out chan int
  go func () { out <- 1 + <-in }()
  return out

What if we do a withTimeout(onePlus(numbers), 0)? Assume the timeout fires first and that's the result that select chooses. There's still that onePlus goroutine out there trying to read from in and at some point probably it will succeed, but nobody will read its value. At that point the number just vanishes into the ether. Maybe that's OK in certain domains, but certainly not in general!

What CML gives you is the ability to express an event (which is kinda like a possibility of sending or receiving a message on a channel) in such a way that we don't run into this situation. Specifically with the wrap combinator, we would make an event such that receiving on numbers would run a function on the received message and return that as the message value -- which is of course the same as what we have, except that in CML the select wouldn't actually read the message off unless it select'd that channel for input.

Of course in Go you could just rewrite your program, so that the select statement looks like this:

select {
  case msg = <-ch: return msg + 1;
  case msg = <-timeoutChannel: return 0;

But here we're operating at a lower level of abstraction; we were forced to intertwingle our concerns of adding 1 and our concerns of timeout. CML is more expressive than Go.

you were right when you said we're all just bricks in the wall

However! I was right in the sense that you can build a CML system on top of Go-like systems (though possibly not Go in particular). Thanks to Vesa Karvonen for this comment and the link to their proof-of-concept CML implementation in Clojure's core.async. I understand Vesa also has an implementation in F# as well.

Folks should read Vesa's code, after reading the Reppy papers of course; it's delightfully short and expressive. The basic idea is that event composition operators like choose and wrap build up data structures instead of doing things. The sync operation then grovels through those data structures to collect a list of channels to pass on to core.async's equivalent of select. When select returns, sync determines which event that chosen channel and message corresponds to, and proceeds to "activate" the event (and, as a side effect, possibly issue NACK messages to other channels).

Provided you can map from the chosen select channel/message back to the event, (something that core.async can mostly do, with a caveat; see the code), then you can build CML on top of channels and goroutines.

o/~ yeah you were wrong o/~

On the other hand! One advantage of CML is that its events are not limited to channel sends and receives. I understand that timeouts, thread joins, and maybe some other event types are first-class event kinds in many CML systems. Michael Sperber, current Scheme48 maintainer and functional programmer, tells me that simply wrapping events in channels+goroutines works but can incur a big performance overhead relative to supporting those event types natively, due to the need to make the new goroutine and channel and the scheduling costs. He quotes 10X as the overhead!

So although CML and Go appear to be inter-expressible, maybe a proper solution will base the simple channel send/receive interface on CML rather than the other way around.

Also, since these events are now second-class, it must be OK to lose these events, for the same reason that the naïve withTimeout could lose a message from numbers. This is the case for timeouts usually but maybe you have to think about this more, and possibly provide an infinite stream of the message. (Of course the wrapper goroutine would be collected if the channel becomes unreachable.)

you were right when you said this is the end

I've long wondered how contemporary musicians deal with the enormous, crushing weight of recorded music. I don't really pick any more but hoo am I feeling this now. I think for Guile, I will continue hacking on fibers in a separate library, and I think that things will remain that way for the next couple years and possibly more. We need more experience and more mistakes before blessing and supporting any particular formulation of highly concurrent programming. I will say though that I am delighted that we are able to actually do this experimentation on a library level and I look forward to seeing what works out :)

Thanks again to Vesa, Michael, and Sam for sharing their time and knowledge; all errors are of course mine. Happy hacking!

25 responses

  1. Brandon says:

    You seem to have some spam in the comments on this article (and the last one).

  2. Siteforyou says:

    you were wrong when you said everything's gonna be all right !!! Hahaha... Funny but well said :D

  3. squidooit says:

    not wrong

  4. Ariana Gallo says:

    I have analyzed CML's thoroughly in my studies. One of the papers I wrote via Rush essay review argues that CMLs are in most cases acceptable, but there are times when they are not. Thanks for the great insight.

  5. duck life says:

    There is a lot of gorgeous website but i always like most fantastic which allow me lots of features of the access of my favorite game and on e of the most important thing i also want to mention here because everybody want play free online games.

  6. says:

    This is a great for the game is the most important comment.

  7. Sudoku game says:

    I like to play sudoku online with full enjoy and fun because it is logical game and it is manage our mind logically.

  8. roblox robux says:

    hey friends roblox robux is now with best ever new resources added lets try it out guys if you want some amazing game then try my link

  9. Nick says:

    Really interesting article, thank you for sharing.

  10. Green Trust Cash says:

    The other type may be the building solely loan product, that's credited in the event the creating is complete, after which it the borrowed funds must be paid back or perhaps substituted by just a home loan. Thus, most lenders require that the loan's installments don ot exceed 40% of your available income at the time of applying for the loan. But with poor credit secured loans, you don't need to face this type of hassle.

  11. hosting 1$ says:

    CML is a computer language, I could not understand this coding.

  12. gmail sign in says:

    Your blog is very informative and great. Its very great read for me because your writing skills is so good and you will write this post in very good manner. Thanks!

  13. Godaddy Renewal Coupon says:

    Its constantly charming when you can not only be taught, yet furthermore locked in! I am certain you had pleasant organization this survey

  14. Delhi Escorts says:

    It is extraordinary to see this article, I am eager to move forward in my next article. I want to spoil your articles, I want to find out your next post.

  15. tree service says:

    Usually I never comment on blogs but your article is so convincing that I never stop myself to say something about it. You’re doing a great job Man,Keep Working.

  16. fredluis says:

    Amazing website, i don't want to move forward when i read this awesome feedback. tile installer

  17. denda says:

    Oh man these are AWESOME! Pinning so I can try them all later! Thanks so much for the feature! xoxo
    hill climb racing

  18. Chandigarh escorts | Call girls in Chandigarh says:

    We accessible here everlastingly to serve you Quality Escort Service in Chandigarh.Our work environment is open 24 X 7 hours every day to fulfill the needs of our customer Take our administration and include colourfull zest in your sexual coexistence..
    Follow us:_

  19. Chandigarh escorts says:

    We've maximum lovely escorts in Chandigarh they're exact of their work they can fulfill their consumer if you selected Chandigarh name women you by no means omit this meating....
    Follow us:_

  20. Jim says:

    What a fantastic article - keep up the great work!

  21. Click here says:

    This is definitely acceptable.

  22. Lion Lawns says:

    Your blog is very informative and great. Its very great read for me because your writing skills is so good and you will write this post in very good manner. Thanks!

  23. Assignment Paraphrasing Services Uk says:

    Very good work. easy to understand. Thank you for giving me the information.

  24. TV Series Jackets says:

    I always like your article because you have provided every time informative post...Thanks!

  25. Custom WordPress development says:

    ITSolution24x7 – Your Creative, Flexible and Affordable Partner We provide Custom WordPress development services to help you achieve optimum results.

Leave a Reply