================================================================================
fexpr

I take this note, related to the deferred evaluation of arguments I would like
to support.

https://news.ycombinator.com/item?id=43023101

The main problem is that a language with fexprs is inherently not compilable.
A second problem is that for some fexprs, compilation semantics cannot be found.
A fexpr definition adds a new special operator to an interpreter.
The existence of fexprs means that the repertoire of special operators is open-ended.
But a compiler depends on there being a fixed set of special operators known in advance.
For each kind of form the compiler has a case, which implements the translation scheme.

Fexprs do have an advantage over macros: lack of hygiene issues.
The local variables in a fexpr are clearly in a different lexical environment
from the variables of the form that it operates on/with.
The fexpr function has the lexical environment of the fexpr form as an argument.
When the interpreter invokes a fexpr, it hands the fexpr the current lexical environment,
and the fexpr form. Whenever the fexpr code needs to evaluate some part of that form,
like a variable reference, it explicitly calls eval, and passes eval that lexical environment.
In no way does that get mixed up with the interpretation of the fexpr itself.
There can be no capture issue. An fexpr would never need gensyms, or contain
a mistake you cannot use them.


================================================================================
MOSTflexiPL (Modular, Statically Typed, Flexibly Extensible Programming Language)
http://flexipl.info/

The approach to achieve complete syntactic extensibility in MOSTflexiPL can be summarized by the formula:
Everything is an expression, i. e., the application of an operator to subexpressions.
However, the notion of operators used here is much more general than usual:
Operators can have any number of names (or operator symbols), and they can be applied to any number of operands.
This apparently rather small generalization of operators allows to subsume all relevant programming language constructs under this general notion.
In fact, constants, variables, functions, control structures, type constructors, and even declaration forms simply become different kinds of operators.
If it is now possible to freely define new operators, all aspects of the language become in fact extensible.

To investigate:
http://flexipl.info/tutorial/


---------------------------------------------------------------------------------
http://christian.heinleins.net/apples/context/

APPLEs: Advanced Procedural Programming Language Elements
http://christian.heinleins.net/pureappl/
During 2009, the project has been renamed to STEEL (Statically Typed Extensible Expression Language)
and later on to  MOSTflexiPL (Modular, Statically Typed, Flexibly Extensible Programming Language),
since the syntactic extensibility of the language has become the predominant goal as well as the most ambitious challenge.


Implicit parameters

If functions have very long parameter lists, and many of these parameters are simply passed down to nested function invocations,
it is tempting to replace these parameters with global variables in order to shorten formal parameter and actual argument lists.
Since the use of global variables is generally discouraged, however, a better solution is to use implicit parameters,
which are part of a function's signature just like normal parameters, but which need not be passed explicitly in function calls.
If an explicit argument value for an implicit parameter is missing in a call, an entity of the same name from the static calling context is used implicitly instead;
if no such entity exists, or if it has an incompatible type, the call is rejected by the compiler.
Therefore, in contrast to normal explicit parameters whose names are irrelevant for callers, the names of implicit parameters constitute an important part of a function's signature.

Alternatively, a concept similar to environment variables in operating systems can be used in combination with so-called dynamic parameters:
If an explicit argument value for a dynamic parameter is missing in a call,
an exported variable of the same name and type provided by the dynamic calling context is used implicitly if possible.
If no such variable is found, the parameter's default value (which is mandatory for dynamic parameters) is used.

Implicit and dynamic parameters are also useful for passing additional context information to overloaded operators,
which by their very nature have a very limited number of explicit parameters (usually one for unary and two for binary operators).
As an example, operators performing arbitrary precision floating point arithmetic might have additional implicit or dynamic parameters
specifying the actually desired precision or the preferred rounding mode.


---------------------------------------------------------------------------------
https://www.haskell.org/hugs/pages/users_guide/implicit-parameters.html
Implicit parameters

Implicit parameters are implemented as described in Implicit parameters: dynamic scoping with static types, J Lewis, MB Shields, E Meijer, J Launchbury, 27th ACM Symposium on Principles of Programming Languages (POPL'00), Boston, Jan 2000.
JLF : this paper is not free.

A variable is called dynamically bound when it is bound by the calling context of a function and statically bound when bound by the callee's context.
In Haskell, all variables are statically bound.
Dynamic binding of variables is a notion that goes back to Lisp, but was later discarded in more modern incarnations, such as Scheme, as dynamic binding can be very confusing in an untyped language.
Unfortunately typed languages, in particular Hindley-Milner typed languages like Haskell, only support static scoping of variables.

However, by a simple extension to the type class system of Haskell, we can support dynamic binding.
Basically, we express the use of a dynamically bound variable as a constraint on the type.
These constraints lead to types of the form (?x::t') => t, which says "this function uses a dynamically-bound variable ?x of type t'".
For example, the following expresses the type of a sort function, implicitly parameterized by a comparison function named cmp.

sort :: (?cmp :: a -> a -> Bool) => [a] -> [a]

---------------------------------------------------------------------------------
https://ocharles.org.uk/blog/posts/2014-12-11-implicit-params.html
Implicit parameters

Implicit parameters act like parameters to a function, except the caller never has to apply the function to the argument.
Instead, the argument is automatically passed to the function by merely being in scope.

Without implicit parameter
    > type LogFunction = String -> IO ()
    >
    > type Present = String
    >
    > queueNewChristmasPresents :: LogFunction -> [Present] -> IO ()
    > queueNewChristmasPresents logMessage presents = do
    >   mapM (logMessage . ("Queueing present for delivery: " ++)) presents
    >   return ()

With implicit parameter
    > queueNewChristmasPresents2 :: (?logMessage :: LogFunction) => [Present] -> IO ()
    > queueNewChristmasPresents2 presents = do
    >   mapM (?logMessage . ("Queueing present for delivery: " ++)) presents
    >   return ()

The LogFunction is no longer a parameter to the function, but is rather part of the context of the function - constraints that must be satisfied when we use the program.
The body of the program is mostly the same, other than the leading ? that prefixes implicit parameters.
To supply an implicit parameter, all we need to do is bring an appropriately named variable into scope
    > ex2 :: IO ()
    > ex2 = do
    >   -- Specifies its own logger
    >   ex1
    >
    >   -- We can locally define a new logging function
    >   let ?logMessage = \t -> putStrLn (zipWith (\i c -> if even i
    >                                                      then c
    >                                                      else toUpper c)
    >                                            [0..]
    >                                            t)
    >   queueNewChristmasPresents2 ["Category Theory Books"]


================================================================================

Notes taken while searching how to implement this todo :
<<
The arguments passed to the coactivity with resume should be available like traditional
arguments (use arg...). Currently they are returned by yield as an array.
>>
Implemented by adding support for :
context = .context~parentContext : let retrieve the context of the yield's caller.
.context~args=<an array of arguments> : let store the arguments passed to resume.



Changes made in sanbox :

ContextClass.hpp
class RexxArray;
...
class RexxContext : public RexxObject
    RexxObject *setArgs(RexxArray *);
    RexxObject *getParentContextObject();


ContextClass.cpp
/**
 * Set the arguments used to invoke the current context
 */
RexxObject *RexxContext::setArgs(RexxArray *arguments)
{
    checkValid();
    // this is required and must be an array
    arguments = arrayArgument(arguments, ARG_ONE);
    arguments = (RexxArray *)arguments->copy();
    activation->setMethodArgumentList(arguments->data(), arguments->size());
    return OREF_NULL; // no return value
}


ContextClass.cpp
/**
 * @return The parent's context.
 */
RexxObject *RexxContext::getParentContextObject()
{
    checkValid();
    return activation->getParentContextObject();
}


CPPCode.cpp
CPPM(RexxContext::setArgs),
CPPM(RexxContext::getParentContextObject),


RexxActivation.hpp
 class RexxActivation : public RexxActivationBase {
   inline void              setMethodArgumentList(RexxObject **_arglist, size_t _argcount) {this->arglist = _arglist; this->argcount = _argcount;};
   RexxObject *getParentContextObject();


RexxActivation.cpp
/**
 * Get the context object for the parent of this activation.
 *
 * @return The created context object.
 */
RexxObject *RexxActivation::getParentContextObject()
{
    RexxActivation *parentActivation = this->senderActivation();
    if (parentActivation == OREF_NULL) return TheNilObject;
    return parentActivation->getContextObject();
}


setup.cpp
  defineKernelMethod(CHAR_ARGSEQUALS    ,TheRexxContextBehaviour, CPPM(RexxContext::setArgs), 1);
  defineKernelMethod(CHAR_PARENTCONTEXT ,TheRexxContextBehaviour, CPPM(RexxContext::getParentContextObject), 0);


RexxConstants.hpp
CHARCONSTANT(ARGSEQUALS, "ARGS=");
CHARCONSTANT(PARENTCONTEXT, "PARENTCONTEXT");
CHARCONSTANT(THREADLOCAL, "THREADLOCAL");


=============================================================================
arg
parse arg
use arg
forward : impacté ? oui : ::method.coactive


getProgramArgumentlist : pas utilisé
getProgramArgumentCount : utilisé uniquement dans RexxNativeActivation.cpp
getMethodArgumentList
getMethodArgumentCount

    interpreter/classes/ContextClass.cpp
        RexxObject *RexxContext::getArgs()
        {
            checkValid();
            RexxObject **arglist = activation->getMethodArgumentList();
            size_t size = activation->getMethodArgumentCount();
            return new (size, arglist) RexxArray;
        }

    interpreter/execution/RexxActivation.hpp
        class RexxActivation : public RexxActivationBase {
            RexxObject         **arglist;       /* activity argument list            */
            size_t               argcount;      /* the count of arguments            */
            ----
            RexxObject *run(RexxObject *_receiver, RexxString *msgname, RexxObject **_arglist, size_t _argcount, RexxInstruction * start, ProtectedObject &resultObj);
            ----
            inline RexxObject     ** getMethodArgumentList() {return this->arglist;};
            ----
            inline size_t            getMethodArgumentCount() { return argcount; }
            ----
            inline RexxObject *      getMethodArgument(size_t position) {
               if (position > getMethodArgumentCount()) {
                   return OREF_NULL;
               }
               else {
                   return arglist[position-1];
               }
            }
            ----
            inline RexxArray        *getArguments() { return new_array(argcount, arglist); }

    interpreter/expression/BuiltinFunctions.cpp
        BUILTIN(ARG)
            RexxObject **arglist = context->getMethodArgumentList();
            size_t size = context->getMethodArgumentCount();

    interpreter/instructions/ParseInstruction.cpp
        void RexxInstructionParse::execute(...
                case SUBKEY_ARG:                   /* PARSE ARG instruction             */
                    multiple = true;                 /* have an argument list             */
                    /* get the current argument list     */
                    argList = context->getMethodArgumentList();
                    argCount = context->getMethodArgumentCount();
                    break;

    interpreter/instructions/UseStrictInstruction.cpp
        void RexxInstructionUseStrict::execute(...
            // get the argument information from the context
            RexxObject **arglist = context->getMethodArgumentList();
            size_t argcount = context->getMethodArgumentCount();

==============================================================

RexxNativeActivation::run

    // retrieve the argument signatures and process them
    uint16_t *types = (*methp)((RexxMethodContext *)&context, NULL);
    processArguments(argcount, arglist, types, arguments, MAX_NATIVE_ARGUMENTS);

--> see "rexx args to C args.txt"

=============================================================
The problem of wrong argument number is not fixed in ooRexx5 :
(10,20,30,)~dimensions= --> [4] (ok)
.array~of(10,20,30,)~dimensions= --> [3] (not ok, should be 4)


Review how to fix ooRexx 4.2:
RexxSource::argList
    called by
    RexxSource::argArray

ok, now fixed.

Before fix:
a(3,3)~of(,20,,40,,)=
an Array (shape [3,3], 4 items)
[1,2] : 20
[2,1] : 40
[2,3] : 20
[3,2] : 40

After fix:
a(3,3)~of(,20,,40,,)=
an Array (shape [3,3], 3 items)
[1,2] : 20
[2,1] : 40
[3,2] : 20
