From 825980ca316fe3d5c63f457488394fa928678b82 Mon Sep 17 00:00:00 2001 From: Pat Garrity Date: Wed, 31 Jul 2024 10:09:47 -0500 Subject: [PATCH] WIP --- docs/revision2.md | 31 ++++++++++++---- docs/sketch-atomics-mutation.md | 63 +++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 6 deletions(-) create mode 100644 docs/sketch-atomics-mutation.md diff --git a/docs/revision2.md b/docs/revision2.md index 93b88f1..10abc33 100644 --- a/docs/revision2.md +++ b/docs/revision2.md @@ -379,7 +379,12 @@ const : = ``` const foo: String = "foo" const bar: Int32 = 12 -record Foo(x: String, y: Int32) + +record Foo + x: String, + y: Int32, +end record + const baz: Foo = Foo(foo, bar) ``` @@ -391,15 +396,29 @@ where input of type `A` produces a result of type `B`. ### Functions Without Arguments -Technically, there are _no_ functions without arguments in Ava. However, the -`Unit` type (the empty tuple) may be used to express that case. +Technically, there are _no_ functions without arguments in Ava. All functions +are pure, and thus a function without arguments is just a value. The `Unit` type +can be used as a "nothing" argument, as it's a singleton object. ``` -fn example: Unit -> Int32 - λ _ => 10 +given A +record IO + thunk: Unit -> A +end record + +given A +fn io_new: (Unit -> A) -> IO A + λ thunk => IO(thunk = thunk) end fn -let demonstrate: Int32 = example () +fn demonstrate: Unit -> IO Int32 + λ () => + block + let foo: Int32 = 10 + let bar: IO Int32 = io_new λ () => foo + bar + end block +end fn ``` ### Functions With Multiple Arguments diff --git a/docs/sketch-atomics-mutation.md b/docs/sketch-atomics-mutation.md new file mode 100644 index 0000000..33b5c5f --- /dev/null +++ b/docs/sketch-atomics-mutation.md @@ -0,0 +1,63 @@ +# Sketch: Atomics and Mutation + +What does it look like to mutate state outside of the scope of a function? + +## Problem Statement + +I want the ability to _modify_, _in-place_, the value of some reference or some +value on the stack. I want the ability to do this _outside_ of the scope of a +single function. + +## Example Functionality + +What is a `State`? How is a `state_set` implementation possible? + +``` +fn example: State String -> String -> IO Unit + λ state newValue => state_set state newValue +end fn +``` + +## Problem: Thread-Safe Access + +- All access to the `State` "thing" must be thread-safe. + +## Problem: Representation + +- Ava, to this point, has not had any record-mutable state or the notion of such + an object. +- Ava does not and will not have inheritance (precondition). +- Must Ava have a mutable primitive? +- How could Ava support mutability without direct access? This implies that + something like `mut` as a `record` field modifier is not sufficient. + +## Idea: Support via Modifiers + +The following implementation is incomplete and does not enforce any thread +safety, but illustrates how `private` access could help this work. + +Also, it demonstrates that anonymous function syntax needs to be considered. + +``` +given A +record State + private mut data: A, +end record + +given A +fn state_get: State A -> IO A + λ state => defer (λ => state.data) +end fn + +given A +fn state_set: State A -> A -> IO State A + λ state newValue => map (defer (λ => state.data = newValue)) (λ => state) +end fn + +given A +fn state_modify: State A -> (A -> A) -> IO State A + λ state f => + map + (defer λ => state.data = f state.data) + λ => state +```