Except you can't destructively update it. So it's really more of a global constant. This is an important caveat because it affects locally scoped ts, too.
arc> (= t 1)
Error: "Can't rebind t"
arc> (= nil 1)
Error: "Can't rebind nil"
arc> (let t 4 (+ t 1))
5
arc> (let t 4 (++ t))
Error: "Can't rebind t"
I imagine that's what made it seem reserved. You still probably shouldn't name variables t or nil.
Hmm, good point. Since it does no harm to change t when it is a local variable, I'd remove the restriction for that case.
I might even drop the restriction altogether, as it doesn't seem to have much useful purpose. I can break Arc just as badly by rebinding "car" or other variables, so why this particular effort to keep me from hurting myself?
Arc doesn't really have arrays, so it doesn't have 2-dimensional arrays. One approach is to use a list of lists.
arc> (= plane '((a b c) (d e f) (g h i)))
((a b c) (d e f) (g h i))
arc> ((plane 0) 0)
a
arc> ((plane 0) 1)
b
arc> ((plane 0) 2)
c
arc> ((plane 1) 2)
f
But indexing linked lists is inefficient, so you could similarly nest hash tables.
Granted, neither solution is pretty. I suspect they aren't there because (as with many things) pg hasn't needed them yet himself.
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:
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?
(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?
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.
Yeah, but env just keeps the variables around, not their values. It's used to distinguish between locals and globals when compiling down to lambda.
> (ac '(fn (x) (+ x 1)) '())
(lambda (x) (ar-funcall2 _+ x 1))
> (ac '(+ x 1) '())
(ar-funcall2 _+ _x 1)
> (ac '(+ x 1) '(x))
(ar-funcall2 _+ x 1)
So it's not quite what you'd want. Off the top of my head, you could hack ac.scm to treat
(fn (x) body)
as something like
(fn (x) (= (locals* 'x) x) body (wipe (locals* 'x)))
Since Arc does all of its local binding with fn (either directly or by macroexpansion), this would work. ac.scm won't heed Arc-side redefinitions of fn, so I can't think of a convenient vanilla Arc implementation. As for efficiency, thread safety, etc., my idea seems gross.
Rather random thing to comment on after 2 weeks, I know. I was just reading this and noticed that your redef macro isn't really doing much: safeset doesn't get called by = unless you happen to make a defset to that effect, which seems unlikely.
arc> (def f (x) (+ x 1))
#<procedure: f>
arc> (= f [+ _ 2]) ; no redef warning
#<procedure: f>
arc> (def f (x) (+ x 3)) ; redef warning
*** redefining f
#<procedure: f>
arc> (defset blah (x)
(w/uniq g
(list (list g x)
x
`(fn (val) (safeset ,x val)))))
#<procedure>
arc> (= x 100)
100
arc> (= x 200) ; no redef warning
200
arc> (= (blah x) 300) ; redef warning
*** redefining x
300
Still an interesting idea to selectively silence safeset, though.
It seems plausible to want redefs to look different from defs.
Disabling these warnings on reload is useful, but a separate use case. I tend to ignore them on reload, but I don't want to see them when I startup lest I start ignoring them altogether.
Yeah, it's still a problem. Oddly enough, I only ever noticed it in test code that expanded ssyntax tree-wise (vs. Arc's symbol-wise ssexpand). I didn't put deliberate thought into making sscontract aware of the bug, but I think it's avoided because of careful precedence separation.
Thanks for the andf case, by the way. Hadn't thought of that one. If you find any incorrect contractions, please let me know. If you want, you can use bitbucket's issue tracker: http://bitbucket.org/fallintothis/contract/issues/
(mac wrong-on (var s . body)
(if (is var 'index)
(err "Can't use index as first arg to on.")
`(forlen index ,s
(let ,var (,s index)
,@body))))
Then
arc> (wrong-on x (prn "abcd") (prs index x) (prn))
abcd
abcd
0 a
abcd
1 b
abcd
2 c
abcd
3 d
nil
because
(macex1 '(wrong-on x (prn "abcd") (prs index x) (prn)))
is
(forlen index (prn "abcd")
(let x ((prn "abcd") index)
(prs index x) (prn)))
Notice that (prn "abcd") is evaluated once at the start, then once on each iteration.
Versus the arc.arc on:
arc> (on x (prn "abcd") (prs index x) (prn))
abcd
0 a
1 b
2 c
3 d
nil
because
(macex1 '(on x (prn "abcd") (prs index x) (prn)))
is
(let gs1763 (prn "abcd")
(forlen index gs1763
(let x (gs1763 index)
(prs index x) (prn))))
Here, (prn "abcd") is evaluated only once. It's bound to the gensym gs1763, which won't clash with any variable names you already have (not strictly, cf. http://arclanguage.org/item?id=5104, but that's the idea).
My definition of on seems to eval the list only once anyway, but now I'm feeling paranoid about where else I've missed this and taken a performance hit.