Env: environments as values – one pair (bindings . parent) – read by name.
An environment is read by name: (Env bindings e), (Env names e), (Env parent e), (Env has? e ‘name), (Env cell e ‘name); (Env current) and (Env root) answer one.
EnvEnvironments as values: one pair (bindings . parent), as (Env current), (Env root) and (Fn env f) answer. A frame’s bindings are an alist of (name . value) and its parent the environment it was made in; the root’s bindings are a tree and its parent nil, and a scoped module’s are a tree under the root. Not the process environment, which Sys environ and Sys getenv read.
(Env %flatten e)The bindings of environment e as a fresh alist: a frame’s copied, the root’s and a module’s tree walked in order.
Examples:
(first (Env %flatten ((fn (_ k) (Env current)) 7))) => ('k . 7)(Env %copy-alist l acc)A fresh copy of an alist of (name . value) cells, in the same order.
Examples:
(Env %copy-alist (list (pair 'a 1) (pair 'b 2)) ()) => (('a . 1) ('b . 2))(Env %walk-tree node acc)Every entry of a (entry . (left . right)) tree, as fresh pairs, onto acc.
Examples:
(Env %walk-tree (pair (pair 'b 2) (pair (pair (pair 'a 1) (pair () ())) (pair (pair 'c 3) (pair () ())))) ()) => (('a . 1) ('b . 2) ('c . 3))(Env bindings e)The bindings of e’s own frame, as a fresh alist of (name . value): a frame’s in its own order, newest first; the root’s tree flattened. The enclosing frames are not included – (Env parent e) reaches them.
Parameters:
ANY — An environmentReturns: LIST — An alist of (name . value)
Examples:
(first (Env bindings ((fn (_ k) (Env current)) 7))) => ('k . 7)(Env names e)The names bound in e’s own frame, in the order (Env bindings e) lists them.
Parameters:
ANY — An environmentReturns: LIST — Symbols
Examples:
(List includes? 'k (Env names ((fn (_ k) (Env current)) 7))) => #t(Env parent e)The environment e was made in, or () for the root.
Parameters:
ANY — An environmentReturns: ANY — An environment, or ()
Examples:
(null? (Env parent (Env root))) => #t(Env has? e name)Whether name resolves in e: in its own frame or any enclosing one, as lookup finds it. A name that is not a symbol raises a type error.
Parameters:
ANY — An environmentSYMBOL — A nameReturns: BOOL — #t when name is bound in e or an environment enclosing it
Examples:
(Env has? (Env root) 'first) => #t
(Env has? (Env root) 'no-such-name-zz) => #f(Env cell e name)The binding name resolves to in e, as the engine’s lookup finds it, in its own frame or an enclosing one: the (name . value) cell itself, whose rest is the binding, so a later set! or def of the name in that frame is read through it. nil when name is unbound in e. A name that is not a symbol raises a type error.
Parameters:
ANY — An environmentSYMBOL — A nameReturns: ANY — The (name . value) cell, or nil
Examples:
(eq? (rest (Env cell (Env root) 'first)) first) => #t
(null? (Env cell (Env root) 'no-such-name-zz)) => #t(Env root)The root environment of the running base: where top-level forms bind, every frame’s outermost parent, with the library’s names in its tree and no parent of its own.
Returns: ANY — The root environment
Examples:
(null? (Env parent (Env root))) => #tEnv/currentThe environment the form (Env current) is evaluated in: a frame inside a closure body, the root at the top level.
Returns: ANY — An environment
Examples:
(eval 'k ((fn (_ k) (Env current)) 7)) => 7