View source on GitHub |
A representation of a block of code in TFF's internal language.
Inherits From: ComputationBuildingBlock
, TypedObject
tff.framework.Block(
local_symbols: Iterable[tuple[str, ComputationBuildingBlock]],
result: tff.framework.ComputationBuildingBlock
)
A block is a syntactic structure that consists of a sequence of local name bindings followed by a result. The bindings are interpreted sequentially, with bindings later in the sequence in the scope of those listed earlier, and the result in the scope of the entire sequence. The usual hiding rules apply.
An informal concise notation for blocks is the following, with name_k
representing the names defined locally for the block, value_k
the values
associated with them, and result
being the expression that reprsents the
value of the block construct.
let name_1=value_1, name_2=value_2, ..., name_n=value_n in result
Blocks are technically a redundant abstraction, as they can be equally well
represented by lambda expressions. A block of the form let x=y in z
is
roughly equivalent to (x -> z)(y)
. Although redundant, blocks have a use
as a way to reduce TFF computation ASTs to a simpler, less nested and more
readable form, and are helpful in AST transformations as a mechanism that
prevents possible naming conflicts.
An example use of a block expression to flatten a nested structure below:
z = federated_sum(federated_map(x, federated_broadcast(y)))
An equivalent form in a more sequential notation using a block expression:
let
v1 = federated_broadcast(y),
v2 = federated_map(x, v1)
in
federated_sum(v2)
Raises | |
---|---|
TypeError
|
if the arguments are of the wrong types. |
Attributes | |
---|---|
locals
|
|
proto
|
Returns a serialized form of this object as a pb.Computation instance. |
result
|
|
type_signature
|
Returns the TFF type of this object (an instance of tff.Type ).
|
Methods
children
children() -> Iterator[tff.framework.ComputationBuildingBlock
]
Returns an iterator yielding immediate child building blocks.
compact_representation
compact_representation()
Returns the compact string representation of this building block.
formatted_representation
formatted_representation()
Returns the formatted string representation of this building block.
from_proto
@classmethod
from_proto( computation_proto: pb.Computation ) -> 'Block'
Returns an instance of a derived class based on 'computation_proto'.
Args | |
---|---|
computation_proto
|
An instance of pb.Computation. |
Returns | |
---|---|
An instance of a class that implements 'ComputationBuildingBlock' and that contains the deserialized logic from in 'computation_proto'. |
Raises | |
---|---|
NotImplementedError
|
if computation_proto contains a kind of computation for which deserialization has not been implemented yet. |
ValueError
|
if deserialization failed due to the argument being invalid. |
structural_representation
structural_representation()
Returns the structural string representation of this building block.
__eq__
__eq__(
other: object
) -> bool
Return self==value.