MENTAL
 Main Menu
 Language
 Principles
 Polymorphism


Polymorphism
 POLYMORPHISM

MENTAL, a polymorphic language without overload

"Polymorphism is the quality of that which has or can have different forms" (R.A.E. Dictionary).

"Polymorphism is the quality or state of existing in or assuming different forms" (Merriam-Webster Dictionary).



Concept

Polymorphism means "many forms". It is the opposite of monomorphism and refers, in general, to the occurrence of elements of different types in some language expression.

If an expression has different meanings depending on the types of its components, the polymorphic expression is said to be "overloaded" (of semantics, it goes without saying). If the semantics are maintained throughout, the polymorphism is without overloading.


Types of polymorphic expressions
Implicit and explicit polymorphism

They apply to procedure headers. When type parameters are defined (and therefore in the call arguments), we speak of explicit polymorphism.

In implicit polymorphism there is no type parameter, being the language processor the one in charge of "discovering" the types of the arguments.


Resolution

Polymorphism with overloading requires an additional process called "resolution", which consists in determining the semantics to apply among the different possible ones. This resolution can take place at compile time (static polymorphism) or at run time (dynamic polymorphism).

Resolution can be a relatively simple process (for example in arithmetic operators by looking at arguments) or more complex. Resolution is more difficult if identifier declarations do not exist or are optional.

In overload resolution, Tennenbaum [1974] distinguishes between:
  1. Forward resolution. It determines the set of possible types of an operator from its operands.

  2. Backward resolution. Based on the expected type according to the context.
For example, in Ada, overload resolution is performed by one forward and one backward pass.


Which type of polymorphism is of more interest?

Polymorphism with overloading has two drawbacks:
  1. It creates confusion, as there are different semantics at play.
  2. A resolution process has to be performed, which consumes resources.
In contrast, polymorphism without overloading maintains semantics at all times and also consistency. Therefore, we are only interested in polymorphism without overloading.


Advantages of polymorphism without overloading
  1. It facilitates the specification of algorithms, orienting them to their conceptual or semantic aspect, freeing the coding of low-level aspects.

  2. Generalizes coding. The same algorithm can be used to access and manipulate different data structures, regardless of the type of the constituent elements. For example, a function that returns the length of a list, that accesses an element of a list, etc. regardless of the type of its elements.

  3. Simplifies programming. No need to code different procedures for each type of element.

  4. It implies a relaxation of strict operational standards or rules and thus provides a greater degree of freedom and flexibility.

  5. Polymorphic operations bring out a common essence, an abstraction ultimately that is independent of the elements on which it operates.

  6. It provides an indispensable combinatorial system to relate elements of different types, favoring creativity.

MENTAL and Polymorphism

Semantic Primitives without Overloading

MENTAL is a polymorphic language without overloading. For example, in the expression x+y, x and y can be any type of expression, but the semantics are preserved. That is, if x and y are two matrices and are equal, the result is 2*x. And if we add two expressions equal to x+y, the result is 2*⟨x+y.

All semantic primitives are polymorphic without overloading, i.e., the semantics associated with each primitive is always the same. That is, addition is always addition, substitution is always substitution, and so on. It must necessarily be so, since the universality of primitives demands it. And furthermore, polymorphism is intimately related to freedom, to the possibility of forming expressions without restrictions. When a polymorphic expression has no possibility to evaluate itself, it evaluates itself.


Examples of polymorphic expressions with semantic primitives
  1. ( a/b 17.5 ⟨( f(x y) = x+y+7 )⟩ ) // sequence

  2. { a/b f(3 4) ⟨( f(x y) = x+y+7 )⟩ } // set

  3. ( ⟨( f(x y) = x+y+7 )⟩ + 12 ) // sum

  4. (x+y+z = {a b c}) // substitution

  5. ( ⟨( f(x y) = x+y+7 )& ⟩ = 33 ) // substitution

  6. color/⟨( f(x y) = x+y+7 )⟩ // particularization

  7. (a/b ≡ 33) // equivalence

  8. (33 ← a/b) // condition

  9. (a/b 17.5 "text")↓ // access to the contents of a stream

Derived operations and polymorphism

Derived operations can be fully polymorphic or partially polymorphic, depending on their definitions. When variables are of a certain type, that indicates that type is necessarily required to make sense. If the type does not conform to the intended type, the expression is self-evaluating. Examples:
Integers and polymorphism

An integer, for example 1234, is a sequence of digits, because it represents, in a compact form, the sequence (1 2 3 4). Depending on the operation, it is treated as a number or as a sequence. Examples:

1234*5 // ev. 6170 (1234 is treated as an integer)

(1234 ∪ ab) // ev. 1234ab (1234 is treated as sequence)



Condition as existence

An expression that acts as a condition is interpreted as existence. Example:

The expression (x ← a=b) is interpreted as x if the equality a=b exists (is satisfied).


Operators with attributes

In order to simplify the number of operators, the same operator can be used with different attributes to facilitate the conceptual connection to the operator. For example, Examples:
  1. Vector sum:

    ⟨( x +/v y) =: ( [x\⌊1…x#⌋ + y\⌊1…y#⌋] ) )⟩

    (a = (11 22 33))
    (b = (1 2 3))
    (a +/v b) // ev. (11+1 + 22+2 + 33+3) ev. (12 24 36)


  2. Scalar product of two vectors:

    ⟨( x */e y) =: +⊣( [x\⌊1…x#⌋ * y\⌊1…y#⌋] ) )⟩

    (a = (11 22 33))
    (b = (1 2 3)
    (a */e b) // ev. (11*1 + 22*2 + 33*3) ev. 154


Bibliography