JSAbrahams / mamba

🐍 The Mamba programming language, because we care about safety
MIT License
85 stars 3 forks source link

Do we want total functions #111

Closed JSAbrahams closed 2 years ago

JSAbrahams commented 5 years ago

Issue, or potential issue

We have pure functions, but we don't have a way of defining a total function, which is guaranteed to halt. The question is, do we want total functions, or is their use-case too specific?

Potential solutions

Add total keyword. A total function is defined on all inputs and therefore halts on all inputs. Therefore, a total function:

A function that is not total, is a "partial function".

consider the following examples:

def neg(x: Bool): Bool => not x

This returns the negation of its input, is both pure and total.

def f(x: Int): Int =>
    call_count <- call_count + 1 
    return 100 / x

Is neither pure nor total. It is not defined for the input 0, and it performs a side effect. Though in the current implementation of Mamba, this is still seen as pure, as it adhere to the rules of the language for being pure (see #110 ).

def f(x: Int): Int => 
    call_count <- call_count + 1
    return x

Is total but not pure. It is defined on all inputs.

Code-wise I think having a total token would not hinder the readability of the language too much. I.e. def pure total f (x: MyClass) => ... is still readable.

JSAbrahams commented 2 years ago

I don't think this should be a language feature, actually. The use-case is a bit too specific for a general purpose programming language.