News for hackers. Stuff that matters.
Re:Std: Execution, Sender/Receiver, and the Continuation Monad by naasking on Monday October 18, 2021 @09:16PM (#28913838)
To see how monads would work with C structs:

1. Implement a simple interpreter for the basic lambda calculus, no side-effects or I/O, something like [1]. You should have a discriminated union describing terms, lambdas and applications.

2. Now add two more node types to your union, one for a "print_line" command, and one for a "sequence" expression, that takes the left expression and passes it to a command on the right-hand side; the command is free to do something with the expression that was previously evaluated, or not.

This AST is the core of a monadic language. The basic operations of every monad are:

1. Wrap values in the monad: here you wrap values as an AST expression, let's call it Expr<T>.

2. Composition operator that accepts an Expr<T0> and a function T0->Expr<T1>: this is the sequence operator.

You can keep adding node types for more types of side-effects/commands (and handlers for those node types in your eval() function), but all you need is this "sequence" node type to handle an arbitrary number of side-effects in sequence, because a monad is basically a structured way to compose arbitrary computations.

Every sequence of operations won't necessarily make sense or compose well, because monads are actually too powerful to model effects, ie. they permit too much.

[1] https://github.com/io12/lambda/blob/master/lambda_calc.h

There are no comments on this comment yet.