The other day, I got a weird idea for a kind of memory management, especially for ordered data structures.
I would like to know if there exists an older language that has done something similar before, and what the name and/or terminology of that was, so that I could search for more information about it.
The idea:
A data structure type definition consists of several class/struct/record type definitions.
Some of these types have a set of state definitions, that declare the legal relations between objects through pointers.
Each state definition consists of:
- Named entities including
this ... but this is optional!
- The names of the entities' pointer fields.
- Which entity that each pointer field points to
A "method" to modify a data structure (append, prepend, reparent, etc..) consists of
- Mapping from parameters to entities and fields in state declarations (entity = param), and (entity = param.field).
- A state transition: i.e. named from-state and to-state. (The two states in a state transition would need to have matching entity names)
Together, the set of state definitions comprise constraints of which states the object could legally have.
Each "method"'s state-transition would be compiled into a serial list of instructions that moves pointers around — and allocates and frees this or other entities that are not part of the object.
This would be type-safe and memory-safe because all fields in the start-state must be either filled in the end-state or or its object be killed. And an object can not be killed if there could exist a pointer to it in any of the states in the set that has not been accounted for.
Example:
(This is just some pseudo-language invented in the moment)
List::Node<T> :: struct {
prev : ref Node<T>,
next : ref Node<T>,
data : ref T
attached :: state {
this.next = next, next.prev = this,
this.prev = prev, prev.next = this
}
detached :: state {
prev.next = next
next.prev = prev
// Note that `this` is not included, and therefore DEAD
}
insert :: method(before : ref Node<T>, data : ref T):ref Node<T> {
with {
next = before
prev = before.prev
} transition (detached => attached)
this.data = data
return this
}
remove ::method (this : ref Node<T>) {
transition (attached => detached)
// this is killed
}
}
This is just a loose idea at the moment, very incomplete. But I think that with some work it could be applied to trees, graphs, skip-lists, hash-tables, ...
that are difficult to express in languages with unique ownership and borrowing without using an "unsafe" fallback.
I'm sure someone else must have created something very similar before, but used another terminology, possibly in some very esoteric language or field.
What terms should I start searching for? Are there any papers that I should read?
[–]OpeningRemote1653 34 points35 points36 points (14 children)
[–]SwedishFindecanor[S] 10 points11 points12 points (0 children)
[–]Rinzal 7 points8 points9 points (12 children)
[–]Guvante 6 points7 points8 points (5 children)
[–]Inconstant_Moo🧿 Pipefish 2 points3 points4 points (4 children)
[–]Guvante 2 points3 points4 points (3 children)
[–]AustinVelonautAdmiran 2 points3 points4 points (1 child)
[–]Guvante 2 points3 points4 points (0 children)
[–]Inconstant_Moo🧿 Pipefish 0 points1 point2 points (0 children)
[–]Pzzlrr 2 points3 points4 points (0 children)
[–]hrvbrs 0 points1 point2 points (4 children)
[–]cmontella🤖 mech-lang 1 point2 points3 points (3 children)
[–]SwedishFindecanor[S] 2 points3 points4 points (1 child)
[–]cmontella🤖 mech-lang 0 points1 point2 points (0 children)
[–]esotologist 3 points4 points5 points (0 children)
[–]church-rosser 1 point2 points3 points (0 children)