From a76c8354774ea9929b1b68ee74b0c8e034c88712 Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Wed, 3 Jul 2024 14:39:32 -0400 Subject: [PATCH] xo-expression: refactor: xtract -> method regularize_layer_vars() --- include/xo/expression/Lambda.hpp | 8 ++++++ src/expression/Lambda.cpp | 44 ++++++++++++++++++-------------- 2 files changed, 33 insertions(+), 19 deletions(-) diff --git a/include/xo/expression/Lambda.hpp b/include/xo/expression/Lambda.hpp index 18039982..d4ec9c54 100644 --- a/include/xo/expression/Lambda.hpp +++ b/include/xo/expression/Lambda.hpp @@ -91,6 +91,14 @@ namespace xo { /** compute free-variable set for this lambda **/ std::set calc_free_variables() const; + /** ensure at most one Variable instance with a particular name + * in this lambda, but ignore nested lambdas. + * + * Goal is to unify variables that can use the same binding + * path to determine memory location at runtime. + **/ + void regularize_layer_vars(); + private: /** lambda name. Initially supporting only form like * (define (foo x y z) diff --git a/src/expression/Lambda.cpp b/src/expression/Lambda.cpp index 30b579cf..833c998f 100644 --- a/src/expression/Lambda.cpp +++ b/src/expression/Lambda.cpp @@ -69,25 +69,9 @@ namespace xo { return retval; } /*calc_free_variables*/ - Lambda::Lambda(const std::string & name, - TypeDescr lambda_type, - const rp & local_env, - const ref::rp & body) - : FunctionInterface(exprtype::lambda, lambda_type), - name_{name}, - body_{body}, - local_env_{local_env} + void + Lambda::regularize_layer_vars() { - stringstream ss; - ss << "double"; - ss << "("; - for (std::size_t i = 0, n = this->n_arg(); i < n; ++i) { - if (i > 0) - ss << ","; - ss << "double"; - } - ss << ")"; - /* regularize local_env+body: make sure exactly one instance * (i.e. with object identity) of a Variable appears * within one layer of a lambda body. @@ -133,11 +117,33 @@ namespace xo { } }); } + } /*regularize_layer_vars*/ + + Lambda::Lambda(const std::string & name, + TypeDescr lambda_type, + const rp & local_env, + const ref::rp & body) + : FunctionInterface(exprtype::lambda, lambda_type), + name_{name}, + body_{body}, + local_env_{local_env} + { + stringstream ss; + ss << "double"; + ss << "("; + for (std::size_t i = 0, n = this->n_arg(); i < n; ++i) { + if (i > 0) + ss << ","; + ss << "double"; + } + ss << ")"; this->type_str_ = ss.str(); this->free_var_set_ = this->calc_free_variables(); - body_->attach_envs(local_env_); + this->regularize_layer_vars(); + + this->body_->attach_envs(local_env_); } /*ctor*/ void