Wednesday, July 13, 2011

Everything in a func?

Here is an issue which is trivial semantically, but potentially has a more substantial effect on the look and feel of the language.  Currently, reflecting to some extent the Pascal heritage, ParaSail uses the terms "function," "procedure," and "operator" for the various kinds of operations.  Functions must have at least one output, procedures may not have any outputs, and operators may have zero or more outputs, but have a name given in quotes and are used for defining operations that connect into special syntax or semantics, such as unary or binary operators, implicit conversions from universal types, etc.

As we go about creating more examples in ParaSail, and compare with various other recent languages, we find the use of these three distinct terms not particularly helpful, and certainly more verbose than most other languages.  On the other hand, the terms do communicate something about the appropriate use of the operations.

We are now leaning toward replacing all three terms with the term "func".  Our sense is this would give the syntax a somewhat less heavy, and perhaps more modern feel.  We would retain the requirement to echo the term at the end of the operation, hence:

func GCD(X, Y : Integer) -> Integer is
    case X =? Y of
      [#less] => return GCD(X, Y mod X);
      [#equal] => return X;
      [#greater] => return GCD(X mod Y, Y);
    end case;
end func GCD;

func Print(F : ref var File; X : Integer) is
    if abs X >= 10 then
        Print(F, X/10);
    end if;
    Print(F, '0' + (X rem 10))
end func Print;

func "|"(X : Set; E : Elem) -> Set is
    return Union(X, Make_Set(E)); 
end func "|";

The above three "func"s would be "function GCD," "procedure Print," and operator "|" using the current ParaSail syntax.  Given that we are already abbreviating "constant" to const and "variable" to var in ParaSail, it doesn't seem inconsistent to adopt a standard abbreviation here for function/procedure/operator, and func is one that appears in a number of languages.

Now this is an issue about which anyone and everyone probably has an opinion...


  1. Why not using `feature' which is a terminology used in Eiffel for the past 25 years, or just simply `routine'.

  2. I suppose the first question is whether to use a single term for all three kinds of operations. The second question is what term to use.

    Eiffel doesn't really use the term "feature" in this way. They instead rely on the presence of "()" to indicate they are defining an operation as opposed to a component, and use "feature" as a dividing line to separate the class header from the class definitions.

    One explicit goal in ParaSail has been to use terms that are in wide use in multiple languages, and hopefully easily recognizable and intuitive, rather than relying on new or rarely used terminology.

    Unfortunately, these sorts of things are very subjective...

  3. In user code, "func" for either "function" or "procedure" works for me; Ada uses "subprogram" in a similar way in the manual (not the syntax).

    For operators, I'd prefer something more than just the presence of quotes.

  4. > For operators, I'd prefer something more than just the presence of quotes.

    Any suggestions? e.g.:

      op "*"(Left, Right : Integer) -> Integer;
      oper "*"(Left, Right : Integer) -> Integer;
      operator "*"(Left, Right : Integer) -> Integer;


  5. Ok, we have settled on "func" for functions and procedures and "op" for operators. The latest ParaSail reference manual incorporating these changes has been posted to the Google group:

  6. I just realized that in Ada, the only syntax difference between operators and functions is the presence of quotes (I knew that before, I just didn't think of it in this context). So there is precedence for that, and it's never bothered me.

    It does seem that the presence of "op" instead of "func" makes things clearer. But I could go either way.

    Sorry to partially flip-flop on you :(.

  7. I think the use of "op" rather than "func" does clarify things, and since operators have special significance to the syntax, it seems worth distinguishing them. Most languages that allow user-defined operators have some sort of unique syntax for declaring them, and just using quotes is perhaps more subtle than necessary.