Skip to content

Homogeneous types of arithmetic operators #316

@ppolesiuk

Description

@ppolesiuk

Arithmetic operators are defined as polymorphic functions, that expects that the type parameter has corresponding method. For instance, the addition is defined as follows:

pub let (+) {type T, method add : T -> _} (x : T) = x.add

This implementation allows totality of addition, but requires additional type annotations to use it:

let f x = x + 1

ends up with the following error:

fatal error: Cannot resolve a method add associated with type #?T
...

An alternative approach would be to enforce that the arithmetic operators have homogeneous type:

pub let (+) {type T, method add : T -> T ->> _} (x : T) = x.add

This solution has two drawbacks: the (+) function is not total, and its type is less polymorphic. However these drawbacks should not pose problems in practice, as (1). totality is an internal tool for ensuring productivity and approximating purity restriction, and is not intended to be overused by a programmer; (2). real arithmetic operators are homogeneous. On the other hand, homogeneous operators makes type inference works better. For instance, functions like

let f x = x + 1

would be accepted by the type-checker.

During the last meeting we agreed that we prefer to have homogeneous arithmetic operators in the standard library.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions