User:Thepigdog/Programming Language Styles

From Knowino
Jump to: navigation, search

[edit] Language Styles

There are several major families of languages

The major difference between the C family and the Pascal family is in the syntax of the declarations,

C++ Pascal
long a a : integer
long fact(long n) function fact(integer n) : integer
long primes[10] primes: array[0..9] of integer

The C++ family seems to be more popular because of its brevity. But it has the disadvantage that the type is not a separate element in the language. For example in,

long *(*MyFunctionPointer)(double p);

the type and declaration are all blended together.

The C++ language has a strange syntax for generic types (templates). This is tied to the implementation strategy used. Template classes are viewed as (textual) copies of classes. Logicaly a type parameter is no different from any other parameter.

The C++ family and the Pascal family styles may be generalised as,

C++ C style but generalised Pascal style generalised
template <long i, class T>
class MyClass
{
public:
    bool MyFunction(long i, T j)
    {
        return i * j;
    }
}
 class MyClass(long i, class T)
 {
 public:
     bool MyFunction(long i, T j)
     {
         result = i * i
     }
 }
 MyClass : class(i : integer, T : class)
 {
 public:
     MyFunction : (i : integer, j : T) -> bool
     {
         result = i * i
     }
 }

The "->" symbol is used here instead of function,

function MyFunction(i : integer, j : T) : long (i : integer, j : T) -> long

The inheritance of a class should not have to be defined in the class declaration. In set theory the subset relationship may be defined in a statement,

A \subset B

So it makes sense to have an equivalent statement in a programming language. The following statements are equivalent,

A inherits B class A

{

inherits B;

}

class A : B

{ }

Only virtual inheritance makes sense in a logical model. So there is no need for the virtual keyword. Private, protected, and public inheritance is rarely used in practice.

The body of a class or function declaration contains a series of statements or declarations separated by ";". The semicolon means "and". In an imperative language the order is important ( A; B is not equivalent to B; A) so this fact is obscured. By identifying variables in an imperative language as "roles" the meaning of ";" becomes clear.

[edit] Functions

The traditional view of a call to a function is that it is a representation of the value returned by the function. This is the view implicit in the C++ style of declarations.

An alternative view is is that a function call represents an object, that is then executed to give the result. The "@" symbol in front of the function call represents this intermediate object. The "^" symbol represents the execution of the call.

x : long = fact(5);
y : (long n) -> long = @fact(5);
z : long = y^;

This mechanism allows a function call to be created, and stored for later execution.

The result of a function needs to be represented in the function body. The Pascal way in ungainly because it makes it harder to change the name of the function. The C++ approach is hard to represent in logic, because it violates the flow of control by immediately exiting the function. The reserved word "result" is introduced to represent the return value of the function.

There is another implicit return value in a statement. A statement implicitly returns a boolean value. So,

Dog.GoodDog: () -> bool
{
Clean();
WellBehaved();
GoodLooking();
}

should mean that the function result is obtained by anding together the results of calling the functions Clean, WellBehaved and GoodLooking. So the default behaviour of a function should be return the value of an expression,

Multiply : (a: long, b: long) -> long
{
a * b
}

This is a nice clean syntax but it makes it harder to introduce conditions into the evaluation. So if the result keyword is used, the expression must be a boolean condition, and result is the value. For example in,

Fact : (n: long) -> long
{
if n = 0 then
result = 1
else
result = n * Fact(n-1)
}

The body is a boolean expression, but result represents the value returned by the function call.

[edit] Features to be removed

The following features go against the natural representation of a program as logic,

These features have no place in a logic based language.

Personal tools
Namespaces
Variants
Actions
Navigation
Community
Toolbox