Rholang Lab #1 Notes:

The main theme was a gradual progression from cell to account.

contract Cell(get, set, state) {
	select {
		case ret <- get; val <- state {
			ret!(val) | state!(val) | cell(get, set, state)
		}
		case newval <- set, _ <- state {
			state!(newval) | cell(get, set, state)
		}
	}
}

// Note that for the general case, we could use function discipline to apply any transformation to mod, but for this purpose, simple inc/dec is fine.
contract ModCell(get, mod, state) {
	select {
		case ret <- get; val <- state {
			ret!(val) | state!(val) | cell(get, mod, state)
		}
		case delta <- mod, val <- state {
			state!(val + delta) | cell(get, mod, state)
		}
	}
}

contract Balance(get, mod, state) {
	select {
		case ret <- get; val <- state {
			ret!(val) | state!(val) | cell(get, mod, state)
		}
		case (delta, ack) <- mod, val <- state {
			// We probably should have if/else sugar that desugars to case on a bool.
			if (val + delta) >= 0 {
				state!(val + delta) | ack!(true) | cell(get, mod, state)
			} else {
				state!(val) | ack!(false) | cell(get, mod, state)
			}
		}
	}
}

contract Account(get, mod, xfer, state) {
	select {
		case ret <- get; val <- state {
			ret!(val) | state!(val) | cell(get, mod, xfer, state)
		}
		case (delta, ack) <- mod, val <- state {
			// We probably should have if/else sugar that desugars to case on a bool.
			if (val + delta) >= 0 {
				state!(val + delta) | ack!(true) | cell(get, mod, xfer, state)
			} else {
				state!(val) | ack!(false) | cell(get, mod, xfer, state)
			}
		}
		// This could more directly use the state name of the src Account.
		case (delta, srcmod, ack) <- xfer; val <- state {
			if (val + delta) < 0 {
				state!(val) | ack!(false) | cell(get, mod, xfer, state)
			} else {
				new ret in {
					srcmod!(-delta, ret)
					select {
						case true <- ret {
							state!(val + delta) | ack!(true) | cell(get, mod, xfer, state)
						}
						case false <- ret {
							state(!val) | ack(!false) | cell(get, mod, xfer, state)
						}
					}
				} // end new
			} // end else
		} // end xfer case
	} // end select
} // end contract