This commit is contained in:
Pat Garrity 2024-07-31 10:09:47 -05:00
parent 0ba9ac30ea
commit 825980ca31
Signed by: pfm
GPG key ID: 5CA5D21BAB7F3A76
2 changed files with 88 additions and 6 deletions

View file

@ -379,7 +379,12 @@ const <name>: <type> = <value>
```
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

View file

@ -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
```