4 ms·
Delimited continuations in lone Lisp
- aktuel 1y agoProject page says it runs on top of the kernel. What is the intended use case? Embedded?
- matheusmoreira 1y agoIt's often said that Linux user space is unstable. It's my hope that a language that runs directly on top of the kernel will help improve the status quo. The idea is to create executables written in lisp that talk directly to the kernel. Also it's my shot at creating the fabled lisp operating system. Those projects tend to run out of steam sooner or later due to the sheer enormity of the task. I'm hoping that building on top of Linux will help keep the scope manageable. I'm taking advantage of the fact Linux is the only kernel that actually allows complete user space replacement. Even if lone fails at this, I want to inspire others to try it. Maybe it's the Rust folks who will succeed at rewriting Linux user space in Rust. Ideologically, it's about Linux maximalism. I've been using Linux for over a decade now and I increasingly believe it should be used everywhere. I know it has lots of great features that people generally avoid because of portability concerns. I want to build a language that encourages people to use Linux to the fullest. Lone is supposed to enable programmers to easily use things like sendfile instead of read/write wherever it makes sense. I'm also thinking about making signalfd the default for signals handling. I want to make Linux systems programming as comfortable as possible. In practical terms, my long term goal is to get lone to a state where it can power my website. I want to make it powerful enough that it's viable to rewrite my static site generator in lone. I believe its power and usefulness will increase immensely once I start working on a real standard library like Ruby's.
- rdc12 1y agoLooks like this other article goes into the details. https://www.matheusmoreira.com/articles/self-contained-lone-lisp-applications https://www.matheusmoreira.com/articles/self-contained-lone-...
- thdhhghgbhy 1y agoIt's powerful, but is anyone actually using it other than in hobby projects that is, in any language?
- DonaldPShimoda 1y agoBy "it" do you mean delimited continuations? If so, my understanding is that they are the standard mode of interruption in Racket — all error-raising/handling and other continuation forms (call/cc, etc) are implemented in terms of delimited continuations because they're a more general abstraction than the older primitives. You can read about Racket's continuation model and where they're used in §10.4 of the Racket Reference [1]. [1] https://docs.racket-lang.org/reference/cont.html https://docs.racket-lang.org/reference/cont.html
- riffraff 1y agoI may be incorrect, but isn't call/cc+state equivalent to delimited continuations? I.e. one can be implemented with the other (and IIRC Racket does have state)
- wk_end 1y agoOleg covers this on his page "An argument against call/cc" (which is linked). The long-and-short of it is: only in a toy-like way; in practice even call/cc + state are insufficient primitives. https://okmij.org/ftp/continuations/against-callcc.html#traps https://okmij.org/ftp/continuations/against-callcc.html#trap...
- riffraff 1y agoThanks!
- deleted 1y ago[deleted]
- thdhhghgbhy 1y ago>delimited continuations? Yes, sorry typed this up on the run.
- alexisread 1y agoInterestingly Wat implements a callstack in a userland VM, to implement it's delimited continuations. Ergonomically it would be good to implement algebraic effects on top, but the basis is there, along with demos of exception handling, branching, fibres etc. https://github.com/manuel/wat-js https://github.com/manuel/wat-js Obviously with forths the callstack is already in userland to implement these things, might be a bit brain-bending in forth.
- kazinator 1y agoIn TXR Lisp, I made delimited continuations by copying delimited sections of the native stack ("C stack"). Intrinsic functions in the Lisp runtime are not "foreign"; capturing continuations across those is allowed. Just not across true foreign functions and certain library routines that interface with host functions, an example being ftw (file tree walk, wrapper for POSIX ftw). I have made it possible to throw exceptions out of the lambda callback invoked by ftw, but not to capture delimited continuations from there up across ftw itself. Speaking of exceptions (in the Blub language sense of "dynamic control transfers"), you would not want that to be based on delimited continuations, or certainly not ones based on this implementation technique. The pragmatic reason is that exceptions are bread-and-butter that you need in almost any everyday program, and that have to perform, whereas delimited continuations are boutique. Making exceptions work within delimited continuation contexts was also a little bit of an extra challenge/chore. When a saved continuation is resumed (blasted to the top of the current and reinstated) the exception-related wiring/plumbing inside that stack segment has to be surgically connected into that of the destination stack, for a correct graft. Basically the last/outermost exception handling frame of the grafted continuation segment has to fixed up to properly hook it into the exception frame linkage. In other words, there is a parallel exception stack consisting of linked frames, which runs through the native stack. A splice is needed there. To support the front-and-centre use case for delimited continuations, which is the ability to have yields with two-way communication (e.g. a recursive procedure can yield an item to a controlling procedure, which can then reply to it with a different item and resume it) I had to invent a new kind of dynamic control construct called "absconding". An abscond is exactly like a dynamic long return, except that it performs no unwinding! When a procedure yields, it is not unwinding. It needs to jump out to the context which is interested in the yielded item, but it must not dispose of its resources. There is the expectation that control will jump back in, and everything will need to be intact. Files that the procedure was working with must not be closed, etc. Absconding is an elegant solution which completely eliminates the need to even think about Scheme's dynamic-wind or similar. In situations in which absconding would lead to resource leakage, two mechanisms take care of it. Firstly, the program an explicitly "kill" a continuation by resuming it, and passing it a special "poison" value. When a continuation receives a poison value, it resumes as usual, and then immediately begins to unwind all the way to its prompt (top of its stack segment). After this unwinding is complete, the saved continuation object on the heap is marked as poisoned and cannot be resumed again. If that mechanism is not used, then we rely on the garbage collector to collect the resources in abandoned delimited continuations. The garbage collector does not automatically do the poisoning call on continuations. However, the application can use finalization for that; i.e. given a delimited continuation C, it could register a finalizer which calls (sys:cont-poison C). The obtain/yield implementation (stdlib/yield.tl) uses this trick.
- mbrock 1y agoI also based my Zig Lisp interpreter on delimited continuations and had a great time implementing async, resumable exceptions, dynamically scoped variables, and so on, on top of that. The single-stepping continuation-shuffling evaluation paradigm also made it really easy to expose evaluations as objects and stepping as a function. A simple form like (set! e (run (+ 1 1)) stores an evaluation—whose continuation and lexical environment can both be inspected and serialized—and (step! e) does a single evaluation step. This lets you implement not just yield-based cooperative concurrency in "user space" but something like green threads with a preemptive scheduler. Here you can see how the delimited continuation control allows binding to JavaScript's Promise API in a way that lets you treat promises as blocking computations: https://github.com/mbrock/wisp/blob/master/web/js.wisp https://github.com/mbrock/wisp/blob/master/web/js.wisp You might be interested in seeing e.g. the implementation of SEND-WITH-DEFAULT! which is one of the control control primitives: https://github.com/mbrock/wisp/blob/b8e29dba0ea61ebaf2db5e3e7f82e238dfc0a15d/core/jets-fun.zig#L561 https://github.com/mbrock/wisp/blob/b8e29dba0ea61ebaf2db5e3e... The evaluator itself is in core/step.zig if you want to check it out.
- matheusmoreira 1y agoWow that's an awesome idea. I never thought of that. Never occurred to me to expose the machine cycling mechanism. Send-with-default also seems useful. I was thinking about how to handle the case where the bottom of the stack is reached. Other languages just error out if that happens. I suppose returning a value is just as reasonable. Nice project. I'm learning zig as well.