Arc Forumnew | comments | leaders | submitlogin
1 point by fallintothis 2869 days ago | link | parent

Cool solution! Of course, handling arbitrarily-long history and globals would be more complicated. Maybe you could modify = to push old values onto a stack?

One advantage to an ac.scm approach is that you don't need to use the special ufn. If it worked on fn itself, then any local variables -- in a def, let, with, mac, etc. -- would be undoable. In an attempt to do this, I added a bit to your macro-based approach:

  (def undo-fn (symbols)
    (let backups (map1 [uniq] symbols)
      `(let ,backups (list ,@symbols)
         (fn () (= ,@(mappend list symbols backups))))))

  (mac w/undoable (parms . body)
    `(let undo ,(undo-fn parms)
       ,@body))

  (mac w/undo (expr)
    ((afn (tree)
       (let tree (macex tree)
         (if (caris tree 'fn)
              (let (lambda args . body) tree
                `(fn ,args (w/undoable ,args ,@(self body))))
             (atom tree)
              tree
             (cons (self (car tree))
                   (self (cdr tree))))))
     expr))
Certainly a hack, but it gives us:

  arc> (w/undo (let x 5
                 (prn "original: " x)
                 (++ x)
                 (prn "new: " x)
                 (undo)
                 (prn "undone: " x)
                 nil))
  original: 5
  new: 6
  undone: 5
  nil

  arc> (w/undo
         (def f (x)
           (prn "original: " x)
           (++ x)
           (prn "new: " x)
           (undo)
           (prn "undone: " x)
           nil))
  #<procedure: f>
  arc> (f 5)
  original: 5
  new: 6
  undone: 5
  nil
  arc> (f 10)
  original: 10
  new: 11
  undone: 10
  nil

  arc> (w/undo
         (def f (x)
           (prn "=== OUTSIDE LET ===")
           (prn "original: " x)
           (= x (let x (+ x 1)
                  (prn "=== INSIDE LET ===")
                  (prn "    original: " x)
                  (++ x)
                  (prn "    new: " x)
                  (undo)
                  (prn "    undone: " x)
                  x))
           (prn "new: " x)
           (undo)
           (prn "undone: " x)
           nil))
  *** redefining f
  #<procedure: f>
  arc> (f 5)
  === OUTSIDE LET ===
  original: 5
  === INSIDE LET ===
      original: 6
      new: 7
      undone: 6
  new: 6
  undone: 5
  nil


1 point by shader 2852 days ago | link

All of the solutions so far have been interesting, but I was thinking more of being able to undo any change to any variable, instead of just the few variables declared as parameters.

The idea would be something like this:

  (with x 5 y 4
    (w/undo
      (++ x)           -> x=6
      (undo x)         -> x returns to 5
      (= y (++ x))     -> x = y = 6
      (side-efn x y)   -> x and y are modified
      (undo)           -> x = y = 6 again
      (reset)))         -> x = 5, y = 4
It seems like being able to undo side effects caused by function calls to arbitrary variables would require overloading = (or more likely sref).

Maybe if you did that you could replace variables that are modified with objects that when evaluated return their current value, but which can undo and maybe even redo by changing the return value to an earlier or later value on the stack. They should also be added to a list of modified variables owned by the w/undo macro, so that it can reset all of their values, and also commit their final value when the block is completed.

Would this even be possible? I'm not sure that it would be, since I think arc uses lexical scoping, which means that an arbitrary function call would use its own context's definition of sref, and thus wouldn't pay attention if we redefined it. Maybe since sref is a global variable, it could be replaced and then reset by w/undo? Meta w/undo!

Anyway, this concept of making variables able to remember their past and return previous values reminds me of lazy evaluation and memoization. Maybe their's some sort of connection that could be used to unify them simply, and add them to arc as a unit?

-----

2 points by rocketnia 2851 days ago | link

What happens if side-efn is this?

  (mac side-efn (x y)
     ; NOTE: This form is only intended to be used in the case where x and y are
     ; raw symbols, as they are in forms like (side-efn foo bar).
     `(= ,x (- ,y ,x)    ; temp = old y - old x
         ,y (- ,y ,x)    ; new y = old x (calculated as old y - temp)
         ,x (+ ,y ,x)))  ; new x = old y (calculated as old x + temp)
Should 'undo reverse just one of these assignments or all three? Should it make any difference if these assignments are three separate = forms within a (do ...) block?

A top-level undo could be nifty, but on the Arc top level even the macro environment gets to change from expression to expression, so it can be a bit different. To allow undo on a finer scale raises the question of just how to measure the previous transaction. How many assignments should be undone, and what if they're hidden away in a call to a function nobody likes to read? What about assignments for scopes whose dynamic extent has ended?

-----

1 point by shader 2848 days ago | link

I'm not entirely sure. Now that I think about it, fine grained undo is really a different concept from global state restoration, and is commonly only done for variables that the programmer a) knows about and b) has in mind the place he would like to restore to.

This means that finer grained undo would be more accurately implemented with save and restore functions, as opposed to just an undo function.

The global undo should work the same was as previously stated, more like a reset, and going all the way back to the start point, which may or may not be the beginning of the w/undo block.

Maybe a better system would be two pairs of save and restore functions, one that works on individual symbols, and the other that redefines '= to store the old value in a table if it didn't already exist, so that reset could restore it.

-----