Nicolas, Perhaps a rules engine could be of use to you? I have to put in a small plug for Jess: <http://www.jessrules.com/> Jess is a rules engine designed for integration with Java, especially for writing expert systems and other situations in which you might want forward- chaining reasoning. It can load files off disk or be supplied rules programmatically and the syntax isn't too complex (and rather Lispy); it would be pretty easy to write some Clojure macros that generate sexps to get handed off to it, for example, and it definitely has a restricted syntax and domain such that bad Jess code won't blow up your whole app. I don't know enough about your situation to know if it's applicable, but you did say rule-based so I thought I'd mention it.
On Jun 23, 2009, at 4:12 AM, Nicolas Oury wrote: > > Hello, > > Thank you very much for your answers. > I know generating code at runtime (evaling) is not something very > common, but that's something I need to do. > > I am writing a simulation software for a rule-based modelisation > language. > Typically, this kind of thing does millions of time the same loop, > where > it applies some rules chosen by the user at runtime. > In my experiments in Common LISP (even in ABCL), there is a huge pay- > off > to generate and compile some code once you know what the rules of the > simulation are. And then to run the compiled code. > (It's better to compile and run than to interpret, isn't it? > Especially > in a tight loop executed millions of time.) > > So I need, to compile some expressions before I run the loop that > keeps > evaluating these expressions. So there are a few solutions: > > - do it at compile-time, using macros. But that means that the end- > user > needs to be more knowledgeable that he will probably be. And he > needs to > have a full environment. This solution is not user friendly. And you > cannot add/remove a rule at run time. > > - use the ASM lib shipped with clojure. I could do that, but it's > really > more difficult than generating clojure code. > > - Generating clojure code at runtime and have it compiled. This is > what > I try to do. As I need to generate a few functions (and even multi > methods), It would be cleaner to generate a namespace and write > a program in it. Have it compiled and called from the main program. > This is what the REPL does, so it must be possible. > I agree macros help a lot for this kind of things because it > simplifies > a lot the code I generate at runtime. But without some way of > compiling > at runtime, macros alone can't do what I want. > >> >> I see the repl in main.clj does something similar to you in its >> with-bindings, but the reason is to make those vars able to be set!, >> not to establish the external namespace for the eval. In your eval >> string, the ns is the default 'user ns. To use a different ns, you >> would need to establish that inside the string. >> >> > > That was what I was trying to mimic. Allow *ns* to be settable in my > eval, in order to be able to create a new namespace. > > With the help of Chouser's answer, I managed to write an example that > actually compiles something at runtime: > > (defn -main [] > (binding [*ns* *ns*] > (eval '(ns Blop)) > (println *ns*) > (eval '(defn f [x] (+ x x))) > (eval '(println (f 5))) > (eval '(println (Blop/f 5))) > )) > I will try to think how encapsulate this usage pattern in a higher > level > way. > > Generating a .class for the namespace would be useful too, even if > less > critical. Is there a way to trick an eval into believing, it is a > compiler? (I know the trick must involve *compile-files* and > *compile-path* in some way, but I can't manage to make this work.) > > Are there other people on the mailing list compiling (calling eval) at > runtime that might be interested? — Daniel Lyons --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to [email protected] Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~----------~----~----~----~------~----~------~--~---
