buzz-language / buzz

👨‍🚀 buzz, A small/lightweight statically typed scripting language
https://buzz-lang.dev
MIT License
1.2k stars 34 forks source link

Algebraic effects #93

Closed iacore closed 1 year ago

iacore commented 1 year ago

With some changes, this language could support algebraic effects described in https://koka-lang.github.io/koka/doc/book.html.

  1. add return type to exception
  2. inside catch, allow resume to continue from where exception was triggered
  3. new effect syntax

New syntax:

effect ReadValue<A> {
    fun read_one() > A 
}

fun willFail() > num !> ReadValue<num> {
    return read_one();
}

try {
    num result = needEffect();
    | result should be 42
} catch ReadValue<num>::read_one() {
    resume 42; | return value to effect
}
giann commented 1 year ago

Not sure I understand the example your provided. I don't really get the effect idea either. What do you think this could bring to the table?

Reading from koka's doc, effects seem to be a strongly functional oriented language concept. Buzz is not really a functional language even though you can use it as such.

I'd argue also that what you propose is syntactic sugar:

protocol ReadValue {
    fun readOne(<A>) > A;
}

fun willFail() > num !> ReadValue {
    return readOne(); | Here I don't get your example: are you calling the ReadValue's method?
}

try {
    num result = needEffect();
} catch (ReadValue effect) {
    | not sure what you wanted to do here, `resume` is the keyword to continue a yielded fiber
}
iacore commented 1 year ago

Languages like smalltalk allow the program to continue from where the exception was thrown.

Algebraic effects allow you to do the same thing, but type checked. For example, it allows you to expose system API as an effect, then type check the program (make sure it only uses certain API).

Where all effects can be type-checked, there are no "side" effects.

giann commented 1 year ago

Languages like smalltalk allow the program to continue from where the exception was thrown.

Interesting idea.

I'm not planning on adding any new features of that caliber though. Buzz should remain relatively simple.