Wednesday, August 23, 2006

Functor Functional Features from C++ in Mynx

One feature I’ve considered adding from C++ to Mynx (as I implement the semantic checks, right now deep into expression statement sentence semantics) is a functional feature, in the functional programming paradigm it is a closure, but in C++ it is called a functor -- a class callable like a method.

In C++, a functor is created by overloading the method call operator or the parenthesis. So it is by overloading that a functor can be created...it is an implicit feature, not explicit.

Code for a functor in C++ class is:

#include <iostream>

using namespace std;

class Comparator_Functor
{
     public:
     Comparator_Functor(int i) : Number(i) { }
     bool operator()(int other_int) { return other_int == Number; }

     private:
     bool isEQ(int val){ return val == Number; }
     int Number;
};

Code to use a C++ functor is:

int main(int argc, char **argv)
{
     Comparator_Functor isZero(1);

     if (isZero(argc))
          cout << "argument count is zero";
     else
          cout << "argument count is not zero";

     cout << endl;

     return 0;
}//end main


The call to isZero is calling the class Comparator_Functor as a method - a functor. What is interesting is C++ does not explicitly support this feature - it is achieved through its features of operator overloading, implicit feature of the language.

A functor is a very interesting feature, but one fraught with peril; a functor can create the infamous Abstraction Inversion antipattern. And as the feature is achieved implicitly, it is possible to implicitly and unintentionally create.

In Mynx, class as a function would be an interesting feature to have -- a program can be considered similar a callable class, just with the restrictions of non-class use imposed, and callable only externally. So a functor would be in the same conceptual framework.

Only for Mynx, it would be explicit in use and intent to create. Code for a functor is at the same conceptual level as a program or class. The syntax for a functor is similar to a class or program unit:

functor Bool aFunctor(in Int i) is
     return i > 0;
end functor;


The primary difference from the C++ functor is that the Mynx does not require a nor use a class constructor, the functor is useable as it is...the code for using a functor without creating an instance:

program useFunctorStateless is

     if(aFunctor(3)
         IO.putln("Greater than zero");
     else
         IO.putln("Lesser or equal to zero");
     end if;

end program;


The main question is if a functor should simply be a method like a static or stateless method, or if a functor should be created with specific parameters. The same program with a stateful or created functor:

program useFunctorStateless is

     aFunctor af to 0; //create instance of functor unlike stateless

     if(af(3)
         IO.putln("Greater than zero");
     else
         IO.putln("Lesser or equal to zero");
     end if;

end program;

The stateful functor carries information from its creation, so that a functor can use different parameters to achieve different behaviors, instance impacts the behavior of an instance of a functor.

The stateless functor is less like a functional closure and more like an anonymous static method of a class. The stateful functor has the advantage that if no parameters are desired, it can be stateless. For the stateless functor, it is very difficult except to pass in some object instance that is used (a delegate) which obviates the whole point of a functor.

The syntax for a stateful functor is similar to a stateless, the instance creation is defined inside a clause that begins with the keyword as:

functor Bool aFunctor(in Int i) as (in Int j) is
     return i > j;
end functor;


The second parameter list is similar to the parameter list for a constructor in a class, only in this case for a functor.

An important distinction between a Mynx functor and the C++ is that the Mynx functor is intentionally explicit, and so avoids implicit features to achieve the functor. It is not as easy to accidentally or implicitly use an abstraction inversion as it is with C++. The functor is intentionally explicit, not implict through an overloaded operator in the class unit.

Sometime in the future, I might add this construct to the Mynx language. I don't want to start re-designing the language as I implement the semantic checks phase--but the feature would be nice, or would it? That's something I'm going to consider. But for now, to use a building under construction analogy, I do not want to change the building lobby or courtyard while the construction is ongoing.

Labels: , , ,

Saturday, August 12, 2006

Mynx and Java Classloader "DLL Hell" or Beware What Programming Language Features You Wish to Have -- You Might Use Them

An opinion-piece or "rant" by Mike Vanier entitled "Scalability of Programming Languages" one interesting point was that of Java's classloaders in the statement "On the other hand, my colleague Donnie Pinkston, who has forgotten more about Java than I'll ever know, has pointed out that it's very easy to get into classloader hell for projects that define their own classloaders (which apparently is often necessary), so it's not all a bed of roses"

Coming from the C++ world (and DLL hell in Visual C++ for Win32) its an interesting comment, one that as a Java developer seems to be akin to saying the automobile with internal combustion engine is a "bomb on wheels so is unsafe"...theoretically possible, but in practice not so. It seems that using classloaders to create "DLL hell" is exploited on a J2EE server to load the class in its own virtual space, so multiple classloaders can load the same Java class without interference. Echidna which runs multiple Java classes (creating a multiprocess JVM) without problems does this as explained in "Understanding the Java Classloader" article.

The Java classloader is exploited on a J2EE server to achieve multiprocess functionality--but the Java classloader (if you read original articles about it) was not originally intended for that purpose -- it was to load class files off the internet, from URL's via HTTP, etc. Creating "DLL hell" seems to be the side-effect of using a feature for a property or effect...and one that Java seems to have avoided as compared to C++ DLL hell.

Since Mynx compiles into high-level language (HLL) Java/C#/C++ code...does that mean a Mynx developer will have access to a Mynx classloader? No, Mynx, as it is intended to also compile into HLL C# (and perhaps C++ code) will not export features of the implementing process language. A Mynx developer could if so motivated really write an implied method and class to get into the lower level details of the JVM, so Mynx will not stop a developer, but it will not make it a language feature. If you really want to get into the sub-language, you can, but the Mynx programming language does not mirror HLL features explicitly.

Java was originally designed as a language to create apps for smart consumer electronics devices (remember FirstPerson?) then later when this market failed to emerge, the Internet or networked computers. Mynx is a high-level application language but not for a consumer electronic device (although many programming languages wind up in use in areas where they were not originally intended...Java being a case in point), and not especially for Internet/network computers...although it is possible using the underlying Java language and JVM.

Mynx's goals and applications are outlined in the Mynx Programming Language -- the Mynx tome for the language.

A class loading mechanism seems "cool" and neat, but any feature in a programming language can open up a Pandora's box...if Java's horrific (not!) DLL hell is any example of using a feature not intended for...that change in a feature can be good or ill for a language. But at the "big picture" perspective, Mynx is simply not designed to be a Java (or even C#) replacement, so does not need to be a fun-house mirror of distorted sub-set of features. A developer "can" get into the Java underneath (another important principle is to enable a developer and programmer to do there job...a straight-jacket language is more harm than good) but the language does not make it a feature for easy use. So unless a programmer or developer "really" wants to, they will not by an easy quick-and-dirty feature in the language. C++ allows you to overload and redefine the new and delete operators, but it does not make it so easy to be quick-and-dirty...so if you want to you can, but it is not something easily done without specific intention. Intent is what programming is all about, not only what a class or method does, but how it does it.

Labels: , ,

Monday, August 07, 2006

Getting Functional -- Literally; New Features from the Functional Paradigm

I've been reading a variety of material relating to programming languages to inhale the current thinking (so to speak) and it seems that two object-oriented languages C# (3.0) and Python are getting functional -- adding the lambda capability for anonymous functions. Most of the debate and discussion seems to center around usability, paradigm, and future speculation about how functional features will either destroy or become the darling of the language.

One area that is overlooked in such discussions is the compiler writer. Adding features adds a level of compiler checks and processing that adds an exception to the rule. Functional features mixed into object-oriented add a use-case rule to check for during the compilation process. Its either a case of the feature being a form of syntactic sugar (in which case it could be translated to another core language feature that is not an exception use-case rule), or as an actual feature requiring its own parsing, syntax element, semantic checks, and code synthesis. Syntactic sugar seems easier as its a case of converting what looks like a new feature into (at least internally in the abstract syntax elements) an existing feature behind the curtain of the compiler theater. But it adds complexity in order to map from the new feature into an existing one, even behind the scene -- and information about errors and problems has to be returned in the form of the feature, not the transformed one. As an actual feature not translated it would be a new syntax element, although errors and problems would be specific to the new feature. So either way there is a level of complexity added (although I'm being general).

But a new feature is a temptation to add, to make a programming language more like an application with a new feature, but the downside is that compiler writer's task of incorporating the new feature either way is more complex. To what degree? My guesstimate would be that for each feature added, it is a linear or arithmetic progression of features in the language, but the compiler and the compiler writer it is a geometric progression (for non syntax sugar features).

Feature: 1+2+3+4 = 7
Complexity: 1*2*3*4 = 24

The complexity can reach a point where all features become syntax sugar, forcing the core language to be minimal, so to follow the idea "less is more" why not keep a programming language like it in the first place. The functionality of the compiler increases where, to use a common expression "More is Less."

This leads to the question useful for evaluating a new programming language feature F1 in programming language P, is F1 some syntax for an equivalent feature FO so that F1 > FO in P. If the feature is syntax sugar, is it a feature that can be part of the language to save on expressing it equivalently? The answer should not be ambigious, or simply to save on typing -- there should be a clear rationale beyond syntactic sugar to sweeten the programming language P with a new feature F1 that is possible with F0. If the new feature F1 is not syntactic sugar that is a different kind of animal, but if it is the question is more clear about adding it in terms of the equivalent feature F0. As I look at features in Mynx, even functional ones, many I have discounted as syntax sugar that is only creeping featuritis and complexity -- but some syntactic sugar is useful -- for explicitness of syntax, whereas non-syntax sugar features are different. So in some ways Mynx has baroque syntax sugar of features, but for reasons than adding a feature.

For example, Java and C# have classes and interfaces, the equivalent in Mynx is all classes, and a virtual class. Mynx also has a program syntax unit, primarily to distinguish a class = library from a program = application, although a class with a main method in Java (or if added in Mynx) is equivalent. The Mynx program is a form of syntax sugar...but it explicitly distinguishes a library unit from an application unit in syntax of the programming language.

As I designed, implemented, and now am implementing the semantic checks (and looking ahead to code synthesis) much of a classic paper I read (ironically as a CS undergrad taking LISP) "Worse is Better" by Richard Gabriel is more meaningful and sensible than when I read it as a dry monograph for a class that might be a question on a quiz. A language is not just a programming language, it is a language designer's philosophy on how to express and think about solutions to problems in software.

Labels: , , , , ,

Website Spy Software