Arc Forumnew | comments | leaders | submitlogin
1 point by akkartik 3734 days ago | link | parent

My refactoring with several general utilities:

  (mac init args
    `(unless (bound ',(car args))
       (= ,@args)))

  (mac after* block
    (let (body finally) (split-by block ':do)
         (do ,@body)
         (do ,@finally))))

  (mac redef(var expr)
       (set disable-redef-warnings*)
       (= ,var ,expr)
       (wipe disable-redef-warnings*)))

  (mac after-exec(fnname args . body)
    `(let old ,fnname
        (redef ,fnname
              (fn ,args
                (let result (old ,@args)

  ;; dynamic scope when writing tests
  (mac shadow(var expr)
    (let stack (globalize stringify.var "-stack")
         (init ,stack ())
         (push ,var ,stack)
         (redef ,var ,expr))))

  (mac unshadow(var)
    (let stack (globalize stringify.var "-stack")
        (if (or (~bound ',stack)
                (empty ,stack))
           (prn "*** couldn't unshadow " ',var)
           (redef ,var (pop ,stack)))

  (mac shadowing(var expr . body)
       (shadow ,var ,expr)
       (unshadow ,var)))

  (mac count-cons(expr)
    (w/uniq cons-count
    `(shadowing cons cons
        (let ,cons-count 0
          (after-exec cons(x y)
            (++ ,cons-count))
            (prn "consed " (plural ,cons-count "time") "."))))))

  (def split-by(seq delim)
    (case type.seq
      cons    (split seq (or (pos delim seq) (len seq)))
      string  (split seq (or (posmatch delim seq) (len seq)))
              (err "bad type for split-by")))

1 point by fallintothis 3720 days ago | link

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)
                 `(fn (val) (safeset ,x val)))))
  arc> (= x 100)
  arc> (= x 200)        ; no redef warning
  arc> (= (blah x) 300) ; redef warning
  *** redefining x
Still an interesting idea to selectively silence safeset, though.


1 point by akkartik 3718 days ago | link

Ah, that's embarrassing :)

So all I need is this then without any change to safeset:

  (mac redef(f args . body) `(= ,f (fn ,args ,@body)))


1 point by fallintothis 3718 days ago | link

Right. So then after-exec becomes (keeping the presumably-intended variable capture)

  (mac after-exec (fnname args . body)
    `(let old ,fnname
       (redef ,fnname ,args
         (let result (old ,@args)
And your uses of redef when you aren't redefining a function should be changed to = (e.g., in shadow).

It might still be cool to toggle safeset warnings. With Arc's single namespace, it's annoying to

  (load "foo.arc")
just to have it spit out redefinition warnings. But silencing safeset isn't really the answer to that problem.


1 point by akkartik 3718 days ago | link

Yes. When I updated anarki I decided to just do

  (= redef =) ; how pretty!
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.


1 point by akkartik 3734 days ago | link

redef controls whether the "redefining var" warnings are displayed. I changed the safeset macro thus at the start of arc.arc:

  (assign disable-redef-warnings* nil)
  (assign safeset (annotate 'mac
                    (fn (var val)
                      `(do (if (is nil disable-redef-warnings*)
                           (if (bound ',var)
                               (do (disp "*** redefining " (stderr))
                                   (disp ',var (stderr))
                                   (disp #\newline (stderr)))))
                           (assign ,var ,val)))))