3 ms·
First class environments is one of my favourite features of Scheme, and I still don't understand why I've never seen it elsewhere. However, it does make Scheme
by shakna 10y ago
First class environments is one of my favourite features of Scheme, and I still don't understand why I've never seen it elsewhere.
However, it does make Scheme uniquely suitable for an embedded language, or turning Scheme into a host language.
This [0] sort of demonstrates the magic that first class environments add, and how they make re-writing a language, whilst its running, a breeze.
It gives you safety in eval, and means that the kind of methods that other languages need to use to create, say a Flask-like API, are just plain overkill.
[0] http://www.cs.rpi.edu/academics/courses/fall00/ai/scheme/reference/schintro-v14/schintro_123.html http://www.cs.rpi.edu/academics/courses/fall00/ai/scheme/ref...
- setra 10y agoA closure (which many languages have) is not really a first class environment. Checkout: http://lambda-the-ultimate.org/node/3861 http://lambda-the-ultimate.org/node/3861
- chc4 10y agoLua 5.2 has getfenv/setfenv, which lets you view and modify the environment of functions. It even lets you set a metatable on the env, so you can do stuff like tracing variable gets or sets.
- shakna 10y agoWhat would the Lua equivalent of this be? (let ((a +)) (null-environment)) Edit: I do really want to know if there is a simple answer. Being able to embed Luajit for some things like this would be awesome.
- jpfr 10y agoAssuming f is a function, just do local env = {} env['args'] = {...} setfenv(f, env) return f()
- shakna 10y agoThat is kinda cool, but it does look like a lot of work to bind multiple functions safely into the same environment in any kind of runtime way. Absolutely doable though.
- jpfr 10y agoThat's not difficult at all. You can just say env['f'] = myfunc and the function will be available as f within the environment.
- marktangotango 10y agoCan be a little verbose as this example shows[1], but works really well in practice. [1] http://stackoverflow.com/a/6982080 http://stackoverflow.com/a/6982080
- shakna 10y agoThat does look really interesting, I'll keep it in mind for the future!
- michaelsbradley 10y agoThe R Language has them: https://cran.r-project.org/doc/manuals/r-release/R-lang.html#Environment-objects https://cran.r-project.org/doc/manuals/r-release/R-lang.html...
- shakna 10y agoSadly, parent.env lets you break the sandbox of the environment. Which makes eval evil again.
- kazinator 10y ago> and I still don't understand why I've never seen it elsewhere. Because reflecting upon environments at run-time is a silly eval hack that defies compilation. An environment that is exposed to the code at run-time cannot be efficiently treated by the compiler. Lisp compilers throw away lexical environments when the dust settles. An environment isn't something that is there, but you are blocked from accessing it by the evil Lisp implementation that doesn't provide an API. It in fact isn't there, except when the code is being inefficiently interpreted.
- shakna 10y agoIt can be ineffecient to generate and pass environments around, but it doesn't have to be, nor does it have to be a hack. Compiling with continuations [0] gives you a very efficient output, and a lexical environment that is easily modified, and intuitive to exist. Combine that with Cheney on the MTA, and first class environments are no more costly than a lambda. It is also possible to compile the initial environment [1], which makes it simpler to JIT any functions later linked to the structure. I'm not sure why you think a compiler has to throw away the environment, its certainly not what MIT Scheme does, as environments are special forms that persist as you go down and up the stack. [2] [0] http://matt.might.net/articles/cps-conversion/ http://matt.might.net/articles/cps-conversion/ [1] https://github.com/gambit/gambit/commit/09284455ae7c7e8dff4a71b8a60e5315e04fbe61 https://github.com/gambit/gambit/commit/09284455ae7c7e8dff4a... [2] https://groups.csail.mit.edu/mac/ftpdir/scheme-7.4/doc-html/scheme_14.html https://groups.csail.mit.edu/mac/ftpdir/scheme-7.4/doc-html/...