Source code for cottax.core.maths.valued
'''A proved graph and a value at exactly each of its boundary inputs.'''
from collections.abc import Iterator
from typing import Any
from ..frozen import Frozen, OrderedMap, Violation
from .nesting import ProvedGraph
from .places import Place
[docs]
def check_nothing_owned_given(g: 'GraphWithValues') -> Iterator[Violation]:
owned = set(g.graph.graph.graph.owned_variables)
if given := tuple(p for p in g.values if p in owned):
yield OwnedGiven(given)
[docs]
class GraphWithValues[G: ProvedGraph, V: Place](Frozen):
'''
A proved graph and a value at exactly each of its boundary inputs. Its checks read
the graph's structure alone, so any proof holds; a consumer says which it takes.
'''
graph : G
values : OrderedMap[V, Any]
checks = (check_every_input_given, check_nothing_owned_given, check_only_inputs_given)
[docs]
class OwnedGiven(Violation):
places : tuple
error = ValueError
template = ('a value given at {places!r}, which the graph computes: an owned place only '
'ever means the value the run computes')
[docs]
class NotAPlace(Violation):
places : tuple
error = KeyError
template = 'a value given at {places!r}, which the graph neither reads nor computes'