Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Algebraic effects #3

Open
probeiuscorp opened this issue Sep 10, 2024 · 0 comments
Open

Algebraic effects #3

probeiuscorp opened this issue Sep 10, 2024 · 0 comments

Comments

@probeiuscorp
Copy link
Owner

Much of my love for pure functional programming comes from React, where context is everything for its usability and composability. As such, I very much want to add algebraic effects to the language. For example, consider division (syntax speculative):

effect DivideByZero *
div: forall a: Fraction e. a -> a -> (a + (effect DivideByZero e))

If we wanted to terminate the program upon a divide-by-zero:

main = doAllTheThings | handle {
  DivideByZero = undefined
}

If we wanted to wrap in Maybe:

mdiv = handle {
  DivideByZero = None
} . Some . div

Context lookup

effect config {
  timeout: Double
}
nestedCall: IO () & (effect config) = wait config.timeout

where the value config has type ({ timeout: Double }) & (effect config)

Semantics

findAverage = samples: List Num. {
  divAll: forall e. List $ (Num + DivideByZero e)
  divAll = samples <$> (div . length samples)
  sum: forall: e. Num + DivideByZero e
  sum = foldr (+) 0 divAll
} | .count
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant