Wafl Home

Program Structure Tutorial

Polymorphism

Polymorphism is the property of a function to be well defined for arguments of different data types. Implicit polymorphism is very important feature of programming language Wafl.

The type inference premises that each of local definitions is fully polymorphic, i.e. that any argument of any function can be of any data type. Step by step, these polymorphic types are reduced to types for which expressions that define functions are applicable. In following example two polymorphic functions are defined and used with different arguments.

Function add, defined as:

add(x,y) = x + y;

is of type (Value['1] * Value['1] -> Value['1]). That suggests that (1) it is applicable to arguments of any value type (Integer, Float or String), (2) both arguments must be of a same type and (3) the result is of the same type.

Function sub, defined as:

sub(x,y) = x - y;

is of type (Numeric['1] * Numeric['1] -> Numeric['1]). Thus, (1) it is applicable to arguments of any numeric type (Integer or Float), (2) both arguments must be of a same type and (3) the result is of the same type.

Both Numeric['1] and Value['1] are combined data types. They can be further reduced to different primitive data types. Type '1 represents a polymorphic type name. Same polymorphic type names in a type must reduce to same specific types in each function application.

In each particular function application, the function type has to be reduced to non-polymorphic data type. However, different applications of a same polymorphic function may reduce to different types in a same program. If used in functions definitions, polymorphic functions can be used in a polymorphic way, with no further data type reduction, or with reduction to more specific but yet another polymorfic type. Their types will be reduced further when the application of defining function induces that.

Function f, defined as:

f(x,y,z) = sub( add(x,y), z);

is of type (Numeric['1] * Numeric['1] * Numeric['1] -> Numeric['1]). Thus, (1) it is applicable to three arguments of any numeric type (Integer or Float), (2) all arguments must be of a same type and (3) the result is of the same type. The type of function add is reduced from Value['1] to Numeric['1], while the type of function is sub is not reduced.

In the following example, function add is successfully applied to a pair of integers, a pair of floats and a pair of strings, and functions sub and f are successfully applied to integers and floats. In the last expression, function add is applied to both integers and floats.

Source code:

{#
add(1,2),
add(1.3,2.3),
add('a','b'),
sub(5,1),
sub(5.4,1.2),
f(5,4,3),
f(1.2, 3.4, 1.3),
add( asFloat(add(1,2)), 3.1 )
#}
where{
    add(x,y) = x + y;
    sub(x,y) = x - y;
    f(x,y,z) = sub( add(x,y), z);
}

Result:

{# 3, 3.600000, "ab", 4, 4.200000, 6, 3.300000, 6.100000 #}

 

Table of Contents

Let's Start

Program Structure

Primitive Data Types

List

Tuple

Record

HTML

Command Line Interpreter

Using Web Servers

Syntax

Examples

Tips

The most of examples evaluates with both command line and Web server Wafl interpreters. If any example is based on specific features of an interpreter, it is explicitly annotated.