fmease / lushui

The reference compiler of the Lushui programming language
Apache License 2.0
7 stars 0 forks source link

Optional laziness #40

Open fmease opened 4 years ago

fmease commented 4 years ago

The first version of this feature is going to look it is specified below. This will probably not be the final form of it.

New keyword lazy which goes on function parameters (after , and before field). Makes the type of the affected parameter equal to external.core.unit.Unit -> A with A being the original type in the source code. Most importantly, when applying a function with a lazy parameter, the argument is not immediately evaluated but rewritten into a thunk, that is a function from unit to the value. Note that outside of the function, that means when you apply it, the type of the argument must be of type A and cannot be of type Unit -> A (this should help type inference, unless mistaken), but inside of it, i.e. in the body of the function as well as the explicit type annotation of the body, it is of type Unit -> A. To evaluate the value, one can of course simply apply it to core.unit.unit, conventionally, however, one uses the function core.unit.force.

Example

;; core.bool:

use external.core.(unit.force bool.(Bool false true))

if (,A: Type) (condition: Bool) (lazy consequent alternate: A): A =
    case condition of
        true => force consequent
        false => force alternate

;; user code:

use external.core.(bool.if nat.(Nat =< -))

result (switch: Nat): Nat =
    if (=< switch 100)
        0
        (- switch 100)

Alternative Designs

Coercions that should be legal: