From 74f11f191c79f4d49924d820bd6152818f5c0fab Mon Sep 17 00:00:00 2001 From: Vitaly Bogdanov Date: Wed, 10 Jul 2024 12:56:01 +0300 Subject: [PATCH 1/2] Add explanation of the scope of the variables for minimal MeTTa --- docs/minimal-metta.md | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/docs/minimal-metta.md b/docs/minimal-metta.md index 19769768c..f0f707759 100644 --- a/docs/minimal-metta.md +++ b/docs/minimal-metta.md @@ -171,6 +171,35 @@ separate alternative. can collect the list of alternatives using `collapse-bind` filter them and return filtered items to the plan using `superpose-bind`. +## Scope of a variable + +Variable name should be unique inside a separate expression evaluated. The +reason is that the whole expression is a variable scope. For example one can +write the expression `(chain (unify $parent Bob () ()) $_ $parent)`. And value +of the `$parent` is returned correctly. + +When variable is passed as an argument to the function call and matched by a +value then the value is assigned to the variable. If variable passed as an +actual argument is matched by a formal argument variable then it is referenced +by the formal argument variable. In this case the actual argument variable can +receive a value outsides of its scope. + +For example the following code (written using MeTTa runner syntax) returns `B`: +``` +(= (foo $b) (function (chain (unify B $b () ()) $_ (return ())))) +!(chain (eval (foo $a)) $_ $a) +``` + +If two separate expressions in the atomspace has the variable with the same +name inside then the scopes of the variables are different. Consider the +following example: +``` +(= (foo) (function (chain (unify A $a () ()) $_ (return ())))) +!(chain (eval (foo)) $_ $a) +``` +Here value will not be assigned to the `$a` from the caller expression because +two variables has different scopes and not referenced by each other. + # Examples Examples of the programs written using minimal MeTTa interpreter: @@ -347,11 +376,6 @@ Nevertheless defining `eval` through `unify` requires rework of the grounded functions interface to allow calling them by executing `unify` instructions. Which is an interesting direction to follow. -## Scope of variables - -Scope of the variable inside instructions is not described in this -specification. It is a clear gap and one of the TODO items. - ## Special matching syntax Sometimes it is convenient to change the semantics of the matching within a From c069f18f1c71be745b6ab1fd08f8cdd8660e0de9 Mon Sep 17 00:00:00 2001 From: Vitaly Bogdanov Date: Thu, 11 Jul 2024 17:47:47 +0300 Subject: [PATCH 2/2] Review comments: fix wordings and mistakes Co-authored-by: luketpeterson <36806965+luketpeterson@users.noreply.github.com> --- docs/minimal-metta.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/docs/minimal-metta.md b/docs/minimal-metta.md index b56af5dca..645a18ca8 100644 --- a/docs/minimal-metta.md +++ b/docs/minimal-metta.md @@ -173,12 +173,12 @@ return filtered items to the plan using `superpose-bind`. ## Scope of a variable -Variable name should be unique inside a separate expression evaluated. The +Each separately evaluated expression is a variable scope, and therefore variable names are treated as unique inside an expression. reason is that the whole expression is a variable scope. For example one can write the expression `(chain (unify $parent Bob () ()) $_ $parent)`. And value of the `$parent` is returned correctly. -When variable is passed as an argument to the function call and matched by a +When a variable is passed as an argument to a function call and matched by a value then the value is assigned to the variable. If variable passed as an actual argument is matched by a formal argument variable then it is referenced by the formal argument variable. In this case the actual argument variable can @@ -190,15 +190,15 @@ For example the following code (written using MeTTa runner syntax) returns `B`: !(chain (eval (foo $a)) $_ $a) ``` -If two separate expressions in the atomspace has the variable with the same -name inside then the scopes of the variables are different. Consider the +If two separate expressions in the space have a variable with the same +name, but the variables reside in independent scopes, then the variables are different. Consider the following example: ``` (= (foo) (function (chain (unify A $a () ()) $_ (return ())))) !(chain (eval (foo)) $_ $a) ``` -Here value will not be assigned to the `$a` from the caller expression because -two variables has different scopes and not referenced by each other. +Here the value will not be assigned to the `$a` from the caller expression because +each of the two variables has a different scope and they do not reference each other. # Examples