Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 130 additions & 0 deletions src/v4/lens/effect.dag
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,133 @@


module v4.lens.effect

import v4.compiler.infer { InferredFacts, InferredTree }
import v4.std.dependency {
Comment thread
briansrls marked this conversation as resolved.
Resolved
BarrierBefore,
DataDependsOn,
DependencyView,
EffectDependsOn,
ResourceDependsOn
}
import v4.std.node { Node }
import v4.std.witness { Holds, Witness }

// 🟢 projection carrier — Practice 11: effect facts are read from the
// dependency projection and the inferred root, not authored as a second
// OperationEffect taxonomy.
type EffectKind
= PureEffect
| ReadEffect
| WriteEffect
| BarrierEffect

type EffectDependencyFact {
at: Node

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BLOCKING: EffectDependencyFact stores at beside dependency.source, so the substrate can represent a fact whose location disagrees with its dependency edge, violating P2 illegal states unrepresentable/single authority.

dependency: DependencyView
source_facts: Witness<InferredFacts>
dependent_facts: Witness<InferredFacts>
effect: EffectKind
}

type EffectFact {
dependencies: List<EffectDependencyFact>
}

fn effect_kind_for_dependency(view: DependencyView) -> EffectKind {
match view.kind {
DataDependsOn => ReadEffect
EffectDependsOn => WriteEffect
ResourceDependsOn => WriteEffect
BarrierBefore => BarrierEffect
_ => PureEffect
Comment thread
briansrls marked this conversation as resolved.
Resolved
}
}

fn inferred_facts_for_node(tree: InferredTree, node: Node) -> Witness<InferredFacts> {
tree.facts.lookup(node)
}

fn effect_dependency_fact(tree: InferredTree, view: DependencyView) -> EffectDependencyFact {
EffectDependencyFact {
at: view.source,
dependency: view,
source_facts: inferred_facts_for_node(tree: tree, node: view.source),
dependent_facts: inferred_facts_for_node(tree: tree, node: view.dependent),
effect: effect_kind_for_dependency(view: view)
}
}

fn effect_fact_from_dependencies(tree: InferredTree, dependencies: List<DependencyView>) -> EffectFact {
EffectFact {
dependencies: fold(dependencies, init: [], f: fn(acc, view) {
list_push(acc, effect_dependency_fact(tree: tree, view: view))
})
}
}

fn effect_fact(tree: InferredTree, dependencies: List<DependencyView>) -> EffectFact {
effect_fact_from_dependencies(tree: tree, dependencies: dependencies)
}

fn effect_witness(tree: InferredTree, dependencies: List<DependencyView>) -> Witness<EffectFact> {
Holds {
value: effect_fact(tree: tree, dependencies: dependencies)
}
}

fn effect_kind_eq(a: EffectKind, b: EffectKind) -> Bool {
Comment thread
briansrls marked this conversation as resolved.
Resolved
match a {
PureEffect => match b {
PureEffect => true
_ => false
}
ReadEffect => match b {
ReadEffect => true
_ => false
}
WriteEffect => match b {
WriteEffect => true
_ => false
}
BarrierEffect => match b {
BarrierEffect => true
_ => false
}
}
}

fn effect_dependency_fact_eq(a: EffectDependencyFact, b: EffectDependencyFact) -> Bool {
(a.at == b.at)
&& (a.dependency == b.dependency)
&& (a.source_facts == b.source_facts)
&& (a.dependent_facts == b.dependent_facts)
&& effect_kind_eq(a: a.effect, b: b.effect)
}

fn effect_dependency_count(xs: List<EffectDependencyFact>, item: EffectDependencyFact) -> Int {
fold(xs, init: 0, f: fn(acc, candidate) {
if effect_dependency_fact_eq(a: candidate, b: item) {
acc + 1
} else {
acc
}
})
}

fn effect_dependency_facts_eq(
left: List<EffectDependencyFact>,
right: List<EffectDependencyFact>
) -> Bool {
if count(left) != count(right) {
false
} else {
fold(left, init: true, f: fn(acc, item) {
acc && (effect_dependency_count(xs: left, item: item) == effect_dependency_count(xs: right, item: item))
})
}
}

fn effect_fact_eq(a: EffectFact, b: EffectFact) -> Bool {
effect_dependency_facts_eq(left: a.dependencies, right: b.dependencies)
}
108 changes: 108 additions & 0 deletions src/v4/lens/idempotency.dag
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,111 @@

module v4.lens.idempotency

import v4.compiler.infer { InferredTree }
import v4.lens.effect {
BarrierEffect,
EffectDependencyFact,
PureEffect,
ReadEffect,
WriteEffect,
effect_dependency_fact_eq,
effect_fact_from_dependencies
}
import v4.std.dependency { DependencyView }
import v4.std.node { Node }
import v4.std.witness { Holds, Witness }

// 🟢 projection carrier — idempotency/cancellation/redundancy read the
// effect projection and algebraic laws; this slice preserves one lens
// mechanism and avoids a parallel cancellation taxonomy.
type IdempotencyVerdict
= Idempotent
| NonIdempotent

type IdempotencyEffectFact {
at: Node
effect: EffectDependencyFact
verdict: IdempotencyVerdict
}

type IdempotencyFact {
effects: List<IdempotencyEffectFact>
}

fn idempotency_verdict_for_effect(effect: EffectDependencyFact) -> IdempotencyVerdict {
match effect.effect {
PureEffect => Idempotent
ReadEffect => Idempotent
BarrierEffect => Idempotent
WriteEffect => NonIdempotent
Comment thread
briansrls marked this conversation as resolved.
Resolved
}
}

fn idempotency_effect_fact(effect: EffectDependencyFact) -> IdempotencyEffectFact {
IdempotencyEffectFact {
at: effect.at,
effect: effect,
verdict: idempotency_verdict_for_effect(effect: effect)
}
}

fn idempotency_fact(tree: InferredTree, dependencies: List<DependencyView>) -> IdempotencyFact {
let effects = effect_fact_from_dependencies(tree: tree, dependencies: dependencies)
IdempotencyFact {
effects: fold(effects.dependencies, init: [], f: fn(acc, effect) {
list_push(acc, idempotency_effect_fact(effect: effect))
})
}
}

fn idempotency_witness(tree: InferredTree, dependencies: List<DependencyView>) -> Witness<IdempotencyFact> {
Holds {
value: idempotency_fact(tree: tree, dependencies: dependencies)
}
}

fn idempotency_verdict_eq(a: IdempotencyVerdict, b: IdempotencyVerdict) -> Bool {
match a {
Idempotent => match b {
Idempotent => true
_ => false
}
NonIdempotent => match b {
NonIdempotent => true
_ => false
}
}
}

fn idempotency_effect_fact_eq(a: IdempotencyEffectFact, b: IdempotencyEffectFact) -> Bool {
(a.at == b.at)
&& effect_dependency_fact_eq(a: a.effect, b: b.effect)
&& idempotency_verdict_eq(a: a.verdict, b: b.verdict)
}

fn idempotency_effect_count(xs: List<IdempotencyEffectFact>, item: IdempotencyEffectFact) -> Int {
fold(xs, init: 0, f: fn(acc, candidate) {
if idempotency_effect_fact_eq(a: candidate, b: item) {
acc + 1
} else {
acc
}
})
}

fn idempotency_effect_facts_eq(
left: List<IdempotencyEffectFact>,
right: List<IdempotencyEffectFact>
) -> Bool {
if count(left) != count(right) {
false
} else {
fold(left, init: true, f: fn(acc, item) {
acc && (idempotency_effect_count(xs: left, item: item) == idempotency_effect_count(xs: right, item: item))
})
}
}

fn idempotency_fact_eq(a: IdempotencyFact, b: IdempotencyFact) -> Bool {
idempotency_effect_facts_eq(left: a.effects, right: b.effects)
}
134 changes: 134 additions & 0 deletions src/v4/lens/ownership.dag
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,137 @@

module v4.lens.ownership

import v4.compiler.infer { InferredFacts, InferredTree }
import v4.std.dependency {
BindsTo,
Contains,
DataDependsOn,
DependencyView,
EffectDependsOn,
ResourceDependsOn
}
import v4.std.node { Node }
import v4.std.witness { Holds, Witness }

// 🟢 projection carrier — ownership is read from dependency/resource shape.
// This lens does not model a target-specific borrow checker; emit/eval
// consume this map when they realize the target memory model.
type OwnershipMode
= OwnedContainment
| SharedRead
| ExclusiveMutation
| BorrowedDependency

type OwnershipDependencyFact {
owner: Node

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BLOCKING: OwnershipDependencyFact duplicates owner and subject beside dependency.source and dependency.dependent, so ownership facts can drift from the edge they claim to project from.

subject: Node
dependency: DependencyView
owner_facts: Witness<InferredFacts>
subject_facts: Witness<InferredFacts>
mode: OwnershipMode
}

type OwnershipFact {
dependencies: List<OwnershipDependencyFact>
}

fn ownership_mode_for_dependency(view: DependencyView) -> OwnershipMode {
match view.kind {
Contains => OwnedContainment
BindsTo => BorrowedDependency
DataDependsOn => SharedRead
EffectDependsOn => ExclusiveMutation
ResourceDependsOn => ExclusiveMutation
Comment thread
briansrls marked this conversation as resolved.
Resolved
_ => BorrowedDependency
}
}

fn ownership_inferred_facts_for_node(tree: InferredTree, node: Node) -> Witness<InferredFacts> {
tree.facts.lookup(node)
}

fn ownership_dependency_fact(tree: InferredTree, view: DependencyView) -> OwnershipDependencyFact {
OwnershipDependencyFact {
owner: view.source,
subject: view.dependent,
dependency: view,
owner_facts: ownership_inferred_facts_for_node(tree: tree, node: view.source),
subject_facts: ownership_inferred_facts_for_node(tree: tree, node: view.dependent),
mode: ownership_mode_for_dependency(view: view)
}
}

fn ownership_fact_from_dependencies(tree: InferredTree, dependencies: List<DependencyView>) -> OwnershipFact {
OwnershipFact {
dependencies: fold(dependencies, init: [], f: fn(acc, view) {
list_push(acc, ownership_dependency_fact(tree: tree, view: view))
})
}
}

fn ownership_fact(tree: InferredTree, dependencies: List<DependencyView>) -> OwnershipFact {
ownership_fact_from_dependencies(tree: tree, dependencies: dependencies)
}

fn ownership_witness(tree: InferredTree, dependencies: List<DependencyView>) -> Witness<OwnershipFact> {
Holds {
value: ownership_fact(tree: tree, dependencies: dependencies)
}
}

fn ownership_mode_eq(a: OwnershipMode, b: OwnershipMode) -> Bool {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BLOCKING: ownership_mode_eq hand-rolls equality over the new OwnershipMode coproduct even though structural identity is derived and the helper has no Practice 10 disposition/receipt.

match a {
OwnedContainment => match b {
OwnedContainment => true
_ => false
}
SharedRead => match b {
SharedRead => true
_ => false
}
ExclusiveMutation => match b {
ExclusiveMutation => true
_ => false
}
BorrowedDependency => match b {
BorrowedDependency => true
_ => false
}
}
}

fn ownership_dependency_fact_eq(a: OwnershipDependencyFact, b: OwnershipDependencyFact) -> Bool {
(a.owner == b.owner)
&& (a.subject == b.subject)
&& (a.dependency == b.dependency)
&& (a.owner_facts == b.owner_facts)
&& (a.subject_facts == b.subject_facts)
&& ownership_mode_eq(a: a.mode, b: b.mode)
}

fn ownership_dependency_count(xs: List<OwnershipDependencyFact>, item: OwnershipDependencyFact) -> Int {
fold(xs, init: 0, f: fn(acc, candidate) {
if ownership_dependency_fact_eq(a: candidate, b: item) {
acc + 1
} else {
acc
}
})
}

fn ownership_dependency_facts_eq(
left: List<OwnershipDependencyFact>,
right: List<OwnershipDependencyFact>
) -> Bool {
if count(left) != count(right) {
false
} else {
fold(left, init: true, f: fn(acc, item) {
acc && (ownership_dependency_count(xs: left, item: item) == ownership_dependency_count(xs: right, item: item))
})
}
}

fn ownership_fact_eq(a: OwnershipFact, b: OwnershipFact) -> Bool {
ownership_dependency_facts_eq(left: a.dependencies, right: b.dependencies)
}
Loading
Loading