On Fri, 2011-08-12 at 13:08 -0400, Ken Wesson wrote: > On Fri, Aug 12, 2011 at 12:41 PM, daly <d...@axiom-developer.org> wrote: > > Clojure has immutable data structures. > > Programs are data structures. > > Therefore, programs are immutable. > > > > So is it possible to create a Clojure program that modifies itself? > > Yes, if it slaps forms together and then executes (eval `(def ~sym > ~form)) or (eval `(defn ~sym ~argvec ~form)) or similarly, or perhaps > uses alter-var-root. (May require :dynamic true set for the involved > Vars in 1.3 for functions and such to start using the new values right > away -- binding definitely does. In 1.2, alter-var-root should "just > work". Changes occur as with atom's swap!, so the function passed to > alter-var-root may potentially execute more than once.)
Consing up a new function and using eval is certainly possible but then you are essentially just working with an interpreter on the data. How does function invocation actually work in Clojure? In Common Lisp you fetch the function slot of the symbol and execute it. To modify a function you can (compile (modify-the-source fn)). This will change the function slot of the symbol so it will execute the new version of itself next time. Does anyone know the equivalent in Clojure? Would you have to invoke javac on a file and reload it? The Clojure compile function only seems to know about files, not in-memory objects. > > Clojure can probably be quite a good AI research and development platform. Well I'm starting to "prep" for the class by thinking about how to write a self-modifying function that learns in Clojure. The mixture of immutability and self-modification would imply that all of the "old" versions of the function still exist, essentially forming a set of more primitive versions that could be a interesting in its own right. Suppose, for instance, that you did have all of the prior versions. You could create a "branching learning application". The idea is that you can have a linear path of learning and then reach back, modify an old version, and create a second "branch" so now you have two different paths of learning. This idea might be very useful. One of the problems that happens in learning systems is that they "hill climb". They constantly try to get better. But if you think about "better", it might mean that you reached a local optimum (a low peak) that cannot get better. But there might be a better optimum (a higher peak) on some other path. Branched learning could look back to a lower point, and choose a second path. If the second path ends up better than the first then you can abandon the worst path. Since Clojure has immutable data and data are programs it would seem that Clojure has a way of doing this "branched learning". Clojure could certainly bring a couple interesting ideas to the AI class. Tim Daly d...@axiom-developer.org -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en