r/Clojure•
czan

[ANN] dont-give-up: Common Lisp style interactive restarts

I've been playing around a little bit with Common Lisp lately, and I've found the interactive restarts to be pretty amazing. So, I've implemented something similar to them in Clojure/Cider!

https://github.com/czan/dont-give-up

I don't really know my way around nrepl/Cider, but I managed to hack something together that works! If you know anything about nrepl middleware or writing extensions to Cider I would really appreciate your help!

Here's an example of what it looks like (taken from the readme):

(require '[dont-give-up.core :refer (with-restarts with-handlers read-unevaluated-value use-restart)])

(defn div [n d]
  (with-restarts [(:use-value [value]
                    :arguments #'read-unevaluated-value
                    :describe "Provide a value to use."
                    value)
                  (:use-denominator [value]
                    :arguments #'read-unevaluated-value
                    :describe "Provide a new denominator and retry."
                    (div n value))]
    (/ n d)))

(div 3 0)
;; ArithmeticException: Divide by zero
;; 
;; The following restarts are available:
;;   [1] use-value - Provide a value to use instead.
;;   [2] use-denominator - Provide a value to use as the denominator.
;;   [q] abort - Rethrow the exception.
;; 
;; ----------------------
;; java.lang.ArithmeticException: Divide by zero
;; 	at clojure.lang.Numbers.divide(Numbers.java:158)
;;  ...

The restarts can also be triggered programmatically. See the readme for an example.

Posted Apr 6, 2018, 3:50 AM UTC

3315
soundslogical•

Cool. I’ve been using Common Lisp (SBCL) too, and as a newcomer to both languages I’ve found the experience so much nicer than Clojure when hitting exceptions. Nice stack trace, readable error message, the chance to correct your mistake and continue as if nothing happened.

Clojure’s a beautiful language with so much going for it, but there’s a lot of room for improvement on the ergonomics of errors.

8
punkisdead•

You might want to check out this talk that Chris Houser gave a few years ago. https://www.youtube.com/watch?v=zp0OEDcAro0

5
czanOP•

I've actually seen that! I think he's definitely right about the programmatic stuff. The main downside to his approach is that it's hard for tools to interact with your restarts. There's no way for a tool to ask "which restarts are currently available?", or to invoke a restart interactively without programming it all ahead of time.

The main advantage to dont-give-up is that it will prompt you with the error, and interactively ask you what to do. This makes the interaction at the repl more "dynamic".

3
punkisdead•

I'm going to have to take a closer look at what you've done. One of the killer features I loved about CL was the conditions/restarts mechanism. I'd love to see it get some traction in the Clojure community.

2
czanOP•

I'm hoping that this work, and in particular making it work with nrepl and Cider, will give people an incentive to go that way. I'm trying to make it so these restarts interoperate with JVM exceptions as much as is possible, so even if you register restarts, other people can just handle ordinary exceptions and ignore the restarts completely.

My hope is that eventually people will start to add restarts into libraries, where it makes sense, which will then enhance the end-user experience at the repl. That's still a long way off, though. I'm also pessimistic about my ability to get people on board for something like that, but I guess we'll see how things go!

2
WalterGR•

You might want to check out this talk that Chris Houser gave a few years ago. https://www.youtube.com/watch?v=zp0OEDcAro0

To save people a click:

Condition Systems in an Exceptional Language - Chris Houser

Length: 33m31s

Published by: ClojureTV

Published on: Nov 17, 2015

"Some errors, however, are sufficiently obscure to escape detection for a surprisingly long time." -Brooker and Wheeler, 1952

Clojure does not come with a Condition system as powerful as Common Lisp's. Instead, we have Exceptions, which start unwinding the stack the moment they're thrown, destroying information as they go. The values of locals disappear, and there is no ability to continue from the location of the error. Several libraries solve this problem, but they do not compose well with each other. We will discuss these libraries, how they work, and why they don't compose. We will consider alternatives that may compose better, and the tradeoffs and amongst all of these.

About the speaker: Chris, a.k.a. Chouser, co-authored "The Joy of Clojure" and enjoys using Clojure regularly with the LonoCloud team at ViaSat.

2
czanOP•

Not really worth a new post, but I've added two nice things since this post:

  • You now always get a "retry" handler, which retries the evaluation from the top. This can be helpful in cases where the error is something that you can fix externally (or, in future, by redefining things)

  • The nrepl middleware now also wraps futures and agents, so any errors which happen during their execution can be interactively restarted. When combined with the above, and judicious use of vars to add indirection for redefinition, means you can redefine faulty behaviours happening in other threads.

2
freakhill•

nice :)!

1
vvvvalvalval•

Shameless plug: this is a bit similar to scope-capture's Breakpoints.

1
czanOP•

Oh, cool! That is similar! I hadn't seen that before. I recently saw a presentation on scope-capture, but this feature wasn't mentioned.

Those breakpoints require you to decide ahead of time to register where you want to break, though. Restarts allow a more interactive approach to dealing with errors. Ideally we'd still be able to evaluate other forms while one thread is suspended waiting for a restart, to let you redefine other vars, but that requires more modifications to Cider.

2
vvvvalvalval•

Those breakpoints require you to decide ahead of time to register where you want to break, though. Restarts allow a more interactive approach to dealing with errors.

I do not see how - with dont-give-up, you still have to anticipate where to put with-restarts, correct? Or is there something I'm missing?

1
czanOP•

Yeah, you have to register restarts, but that can be thought of as a "static" property. So, for instance, a library can register sensible restarts letting me recover in reasonable ways. Then I, as a user of the library, can use its restarts when those errors occur without modifying the library.

For example, a library might attempt to load a file, and in case there's an error (let's say file not found) it could register a restart (allowing me to provide it with a new filename to try). Without modifying the library, or my code, I could then recover from the error and continue executing my program.

2
vvvvalvalval•

Ah I see, that's interesting.

1
Ironballs•

Good God. I am authoring a library right now and this is exactly what I need. I think. I have a socket connection and I want to defer the reconnect / crash functionality to the user of the library.

edit what I'm after is something like conditionals from CL, not restarts, it seems. Or something like actor supervision trees from Erlang.

1