remove sequence/stacked/stages/chain

This commit is contained in:
AF 2022-09-07 23:38:29 +03:00
parent 11ace6bc5b
commit 6ea5be228c
25 changed files with 40 additions and 507 deletions

View File

@ -35,7 +35,7 @@ class ListBridge(
left_bridge.reduce(reduce),
right_bridge.reduce(reduce),
)
return reduce.merge(left, right)
return await reduce.merge(left, right)
def loose(self) -> Reducer[Element, Out]:
return self

View File

@ -1,4 +1,4 @@
from typing import Generic, TypeVar
from typing import Any, Callable, Coroutine, Generic, TypeVar
__all__ = ('Mapper',)
@ -9,3 +9,9 @@ Mapped = TypeVar('Mapped')
class Mapper(Generic[Element, Mapped]):
async def map(self, element: Element) -> Mapped:
raise NotImplementedError
def bind(self, target: Callable[[], Coroutine[Any, Any, Element]]) -> Callable[[], Coroutine[Any, Any, Mapped]]:
async def bound() -> Mapped:
return await self.map(await target())
return bound

View File

@ -27,13 +27,17 @@ class MapReduce(Reduce[Element, Out], Generic[Element, Out, Mapped]):
outc: Callable[[], Coroutine[Any, Any, Out]],
elementc: Callable[[], Coroutine[Any, Any, Element]],
) -> Out:
async def element():
return await self.mapper.map(await elementc())
return await self.reduce_mapped.reducec(outc, self.mapper.bind(elementc))
return await self.reduce_mapped.reducec(outc, lambda: element())
async def merge(self, left: Out, right: Out) -> Out:
return await self.reduce_mapped.merge(left, right)
def merge(self, left: Out, right: Out) -> Out:
return self.reduce_mapped.merge(left, right)
async def mergec(
self,
leftc: Callable[[], Coroutine[Any, Any, Out]],
rightc: Callable[[], Coroutine[Any, Any, Out]],
) -> Out:
return await self.reduce_mapped.mergec(leftc, rightc)
def loose(self) -> Reduce[Element, Out]:
return self

View File

@ -14,7 +14,7 @@ class PureReduce(Reduce[Pure, Pure], Generic[Pure]):
async def reduce(self, out: Pure, element: Pure) -> Pure:
return self.pure(out, element)
def merge(self, left: Pure, right: Pure) -> Pure:
async def merge(self, left: Pure, right: Pure) -> Pure:
return self.pure(left, right)
def pure(self, left: Pure, right: Pure) -> Pure:

View File

@ -23,5 +23,13 @@ class Reduce(Generic[Element, Out]):
out, element = await gather(out2(), element2())
return await self.reduce(out, element)
def merge(self, left: Out, right: Out) -> Out:
async def merge(self, left: Out, right: Out) -> Out:
raise NotImplementedError
async def mergec(
self,
leftc: Callable[[], Coroutine[Any, Any, Out]],
rightc: Callable[[], Coroutine[Any, Any, Out]],
) -> Out:
left, right = await gather(leftc(), rightc())
return await self.merge(left, right)

View File

@ -1,11 +0,0 @@
__all__ = (
'CompositionDispatcher',
'DispatchMapper',
'SequenceDispatch',
'SequenceDispatcher', 'FirstDispatcher', 'LastDispatcher', 'PairDispatcher',
)
from ._compositiondispatcher import CompositionDispatcher
from ._dispatchmapper import DispatchMapper
from ._sequencedispatch import SequenceDispatch
from ._sequencedispatcher import FirstDispatcher, LastDispatcher, PairDispatcher, SequenceDispatcher

View File

@ -1,42 +0,0 @@
from typing import Generic, TypeVar
from rainbowadn.core import *
from rainbowadn.flow.core import *
from ._sequencedispatch import *
__all__ = ('CompositionDispatch',)
Element = TypeVar('Element')
Out = TypeVar('Out')
Middle = TypeVar('Middle')
class CompositionDispatch(
SequenceDispatch[Element, Out],
Generic[Element, Out, Middle],
):
def __init__(
self,
domain: Mapper[Element, Middle],
codomain: SequenceDispatch[Middle, Out]
):
assert isinstance(domain, Mapper)
assert isinstance(codomain, SequenceDispatch)
self.domain = domain
self.codomain = codomain
async def on_first(self, element: Element) -> Out:
return await self.codomain.on_first(await self.domain.map(element))
async def on_last(self, element: Element) -> Out:
return await self.codomain.on_last(await self.domain.map(element))
async def on_pair(self, previous: Element, element: Element) -> Out:
previous_middle, element_middle = await gather(
self.domain.map(previous),
self.domain.map(element),
)
return await self.codomain.on_pair(
previous_middle,
element_middle
)

View File

@ -1,50 +0,0 @@
from typing import Generic, TypeVar
from rainbowadn.flow.core import *
from ._compositiondispatch import *
from ._sequencedispatch import *
from ._sequencedispatcher import *
__all__ = ('CompositionDispatcher',)
Element = TypeVar('Element')
Out = TypeVar('Out')
Middle = TypeVar('Middle')
class CompositionDispatcher(
SequenceDispatcher[Middle, Out],
Generic[Middle, Out, Element]
):
def __init__(
self,
mapper: Mapper[Element, Middle],
dispatcher: SequenceDispatcher[Element, Out]
):
assert isinstance(mapper, Mapper)
assert isinstance(dispatcher, SequenceDispatcher)
self.mapper = mapper
self.dispatcher = dispatcher
async def dispatch(self, dispatch: SequenceDispatch[Middle, Out]) -> Out:
cd: SequenceDispatch[Element, Out] = CompositionDispatch(self.mapper, dispatch)
assert isinstance(cd, SequenceDispatch)
return await self.dispatcher.dispatch(
cd
)
@classmethod
def mapper(cls, mapper: Mapper[Element, Middle]) -> Mapper[
SequenceDispatcher[Element, Out],
SequenceDispatcher[Middle, Out],
]:
assert isinstance(mapper, Mapper)
def wrap(dispatcher: SequenceDispatcher[Element, Out]) -> SequenceDispatcher[Middle, Out]:
assert isinstance(dispatcher, SequenceDispatcher)
return CompositionDispatcher(
mapper,
dispatcher
)
return CallableMapper(wrap)

View File

@ -1,26 +0,0 @@
from typing import Generic, TypeVar
from rainbowadn.flow.core import *
from ._sequencedispatch import *
from ._sequencedispatcher import *
__all__ = ('DispatchMapper',)
Element = TypeVar('Element')
Out = TypeVar('Out')
class DispatchMapper(
Mapper[SequenceDispatcher[Element, Out], Out],
Generic[Out, Element]
):
def __init__(self, dispatch: SequenceDispatch[Element, Out]):
assert isinstance(dispatch, SequenceDispatch)
self.dispatch = dispatch
async def map(self, element: SequenceDispatcher[Element, Out]) -> Out:
assert isinstance(element, SequenceDispatcher)
return await element.dispatch(self.dispatch)
def loose(self) -> Mapper[SequenceDispatcher[Element, Out], Out]:
return self

View File

@ -1,17 +0,0 @@
from typing import Generic, TypeVar
__all__ = ('SequenceDispatch',)
Element = TypeVar('Element')
Out = TypeVar('Out')
class SequenceDispatch(Generic[Element, Out]):
async def on_first(self, element: Element) -> Out:
raise NotImplementedError
async def on_last(self, element: Element) -> Out:
raise NotImplementedError
async def on_pair(self, previous: Element, element: Element) -> Out:
raise NotImplementedError

View File

@ -1,41 +0,0 @@
from typing import Generic, TypeVar
from ._sequencedispatch import *
__all__ = ('SequenceDispatcher', 'FirstDispatcher', 'LastDispatcher', 'PairDispatcher',)
Element = TypeVar('Element')
Out = TypeVar('Out')
class SequenceDispatcher(Generic[Element, Out]):
async def dispatch(self, dispatch: SequenceDispatch[Element, Out]) -> Out:
raise NotImplementedError
class FirstDispatcher(SequenceDispatcher[Element, Out]):
def __init__(self, element: Element):
self.element = element
async def dispatch(self, dispatch: SequenceDispatch[Element, Out]) -> Out:
assert isinstance(dispatch, SequenceDispatch)
return await dispatch.on_first(self.element)
class LastDispatcher(SequenceDispatcher[Element, Out]):
def __init__(self, element: Element):
self.element = element
async def dispatch(self, dispatch: SequenceDispatch[Element, Out]) -> Out:
assert isinstance(dispatch, SequenceDispatch)
return await dispatch.on_last(self.element)
class PairDispatcher(SequenceDispatcher[Element, Out]):
def __init__(self, previous: Element, element: Element):
self.previous = previous
self.element = element
async def dispatch(self, dispatch: SequenceDispatch[Element, Out]) -> Out:
assert isinstance(dispatch, SequenceDispatch)
return await dispatch.on_pair(self.previous, self.element)

View File

@ -1,7 +0,0 @@
__all__ = (
'StackedReduce',
'StackedReducer',
)
from ._stackedreduce import StackedReduce
from ._stackedreducer import StackedReducer

View File

@ -1,26 +0,0 @@
from typing import Generic, TypeVar
from rainbowadn.flow.core import *
from rainbowadn.flow.sequence import *
from rainbowadn.nullability import *
__all__ = ('StackedDispatch',)
Element = TypeVar('Element')
Out = TypeVar('Out')
class StackedDispatch(SequenceDispatch[Element, Out], Generic[Element, Out]):
def __init__(self, stacked: Reduce[tuple[Nullable[Element], Element], Out], out: Out):
assert isinstance(stacked, Reduce)
self.stacked = stacked
self.out = out
async def on_first(self, element: Element) -> Out:
return await self.stacked.reduce(self.out, (Null(), element))
async def on_last(self, element: Element) -> Out:
return self.out
async def on_pair(self, previous: Element, element: Element) -> Out:
return await self.stacked.reduce(self.out, (NotNull(previous), element))

View File

@ -1,31 +0,0 @@
from typing import Generic, TypeVar
from rainbowadn.flow.core import *
from rainbowadn.flow.sequence import *
from rainbowadn.nullability import *
from ._stackeddispatch import *
__all__ = ('StackedReduce',)
Stacked = TypeVar('Stacked')
Out = TypeVar('Out')
class StackedReduce(
Reduce[SequenceDispatcher[Stacked, Out], Out],
Generic[Out, Stacked]
):
def __init__(self, stacked: Reduce[tuple[Nullable[Stacked], Stacked], Out]):
assert isinstance(stacked, Reduce)
super().__init__(stacked.initial)
self.stacked = stacked
async def reduce(self, out: Out, element: SequenceDispatcher[Stacked, Out]) -> Out:
assert isinstance(element, SequenceDispatcher)
return await element.dispatch(StackedDispatch(self.stacked, out))
def merge(self, left: Out, right: Out) -> Out:
return self.stacked.merge(left, right)
def loose(self) -> Reduce[SequenceDispatcher[Stacked, Out], Out]:
return self

View File

@ -1,37 +0,0 @@
from typing import Generic, TypeVar
from rainbowadn.flow.core import *
from rainbowadn.flow.sequence import *
from rainbowadn.nullability import *
from ._stackedreduce import *
__all__ = ('StackedReducer',)
Stacked = TypeVar('Stacked')
Out = TypeVar('Out')
class StackedReducer(
Reducer[tuple[Nullable[Stacked], Stacked], Out],
Generic[Out, Stacked]
):
def __init__(self, stacked: Reducer[SequenceDispatcher[Stacked, Out], Out]):
assert isinstance(stacked, Reducer)
self.stacked = stacked
async def reduce(self, reduce: Reduce[tuple[Nullable[Stacked], Stacked], Out]) -> Out:
assert isinstance(reduce, Reduce)
stacked_reduce: StackedReduce[Out, Stacked] = StackedReduce(reduce)
dispatcher_reduce: Reduce[SequenceDispatcher[Stacked, Out], Out] = stacked_reduce.loose()
assert isinstance(dispatcher_reduce, Reduce)
return await self.stacked.reduce(dispatcher_reduce)
def loose(self) -> Reducer[tuple[Nullable[Stacked], Stacked], Out]:
return self
@classmethod
def mapper(cls) -> Mapper[
Reducer[SequenceDispatcher[Stacked, Out], Out],
Reducer[tuple[Nullable[Stacked], Stacked], Out],
]:
return CallableMapper(cls)

View File

@ -1,45 +0,0 @@
from typing import TypeVar
from rainbowadn.flow.core import *
from rainbowadn.flow.sequence import *
from rainbowadn.flow.stacked import *
from rainbowadn.flow.verification.core import *
from rainbowadn.nullability import *
__all__ = ('chain_verification',)
Link = TypeVar('Link')
Chain = TypeVar('Chain')
def chain_verification(
chain_to_dispatched: Mapper[Chain, Reducer[SequenceDispatcher[Link, bool], bool]],
link_verification: Verification[tuple[Nullable[Link], Link]],
) -> Verification[
Chain
]:
assert isinstance(chain_to_dispatched, Mapper)
assert isinstance(link_verification, Verification)
dispatched_to_stacked: Mapper[
Reducer[SequenceDispatcher[Link, bool], bool],
Reducer[tuple[Nullable[Link], Link], bool]
] = StackedReducer.mapper()
assert isinstance(dispatched_to_stacked, Mapper)
chain_to_stacked: Mapper[
Chain,
Reducer[tuple[Nullable[Link], Link], bool]
] = Composition(
chain_to_dispatched,
dispatched_to_stacked
)
assert isinstance(chain_to_stacked, Mapper)
stacked_verification: Verification[
Reducer[tuple[Nullable[Link], Link], bool]
] = ReduceVerification(link_verification).loose()
assert isinstance(stacked_verification, Verification)
verification: Verification[Chain] = CompositionVerification(
chain_to_stacked,
stacked_verification
)
assert isinstance(verification, Verification)
return verification

View File

@ -1,7 +0,0 @@
__all__ = (
'StageVerification',
'StageVerificationProtocol',
)
from ._stageverification import StageVerification
from ._stageverificationprotocol import StageVerificationProtocol

View File

@ -1,71 +0,0 @@
from typing import Generic, TypeVar
from rainbowadn.core import *
from rainbowadn.flow.core import *
from rainbowadn.flow.sequence import *
from rainbowadn.flow.verification.core import *
from rainbowadn.nullability import *
from ._stageverificationdispatch import *
from ._stageverificationprotocol import *
__all__ = ('StageVerification',)
Base = TypeVar('Base')
Stage = TypeVar('Stage')
Header = TypeVar('Header')
State = TypeVar('State')
class StageVerification(
Verification[tuple[Nullable[State], Header, State]],
Generic[Base, Stage, Header, State]
):
def __init__(
self,
protocol: StageVerificationProtocol[Base, Stage, Header],
mapper: Mapper[State, tuple[Base, Reducer[SequenceDispatcher[Stage, bool], bool]]],
):
assert isinstance(protocol, StageVerificationProtocol)
assert isinstance(mapper, Mapper)
self.protocol = protocol
self.mapper = mapper
async def _base(self, state: Nullable[State]) -> Nullable[State]:
assert isinstance(state, Nullable)
if state.null():
return Null()
else:
base: Base
base, _ = await self.mapper.map(state.resolve())
return NotNull(base)
async def verify(self, element: tuple[Nullable[State], Header, State]) -> bool:
assert isinstance(element, tuple)
previous: Nullable[State]
header: Header
state: State
previous, header, state = element
assert isinstance(previous, Nullable)
previous_base: Nullable[State]
base: Base
reducer: Reducer[SequenceDispatcher[Stage, bool], bool]
previous_base, (base, reducer) = await gather(
self._base(previous),
self.mapper.map(state),
)
assert isinstance(previous_base, Nullable)
assert isinstance(reducer, Reducer)
verification: Verification[Reducer[SequenceDispatcher[Stage, bool], bool]] = StageVerificationDispatch(
self.protocol,
previous_base,
header,
base
).reduce_verification()
assert isinstance(verification, Verification)
assert_true(
await verification.verify(reducer)
)
return True
def loose(self) -> Verification[tuple[Nullable[State], Header, State]]:
return self

View File

@ -1,51 +0,0 @@
from typing import Generic, TypeVar
from rainbowadn.core import *
from rainbowadn.flow.core import *
from rainbowadn.flow.sequence import *
from rainbowadn.flow.verification.core import *
from rainbowadn.nullability import *
from ._stageverificationprotocol import *
__all__ = ('StageVerificationDispatch',)
Base = TypeVar('Base')
Stage = TypeVar('Stage')
Header = TypeVar('Header')
class StageVerificationDispatch(
SequenceDispatch[Stage, bool],
Generic[Base, Stage, Header]
):
def __init__(
self,
protocol: StageVerificationProtocol[Base, Stage, Header],
previous: Nullable[Base],
header: Header,
base: Base,
):
assert isinstance(protocol, StageVerificationProtocol)
assert isinstance(previous, Nullable)
self.protocol = protocol
self.previous = previous
self.header = header
self.base = base
async def on_first(self, element: Stage) -> bool:
assert_true(await self.protocol.initial(self.previous, self.header, element))
return True
async def on_last(self, element: Stage) -> bool:
assert_true(await self.protocol.final(element, self.base))
return True
async def on_pair(self, previous: Stage, element: Stage) -> bool:
assert_true(await self.protocol.intermediate(previous, element))
return True
def _verification(self) -> Verification[SequenceDispatcher[Stage, bool]]:
return MapperVerification(DispatchMapper(self).loose())
def reduce_verification(self) -> Verification[Reducer[SequenceDispatcher[Stage, bool], bool]]:
return ReduceVerification(self._verification()).loose()

View File

@ -1,20 +0,0 @@
from typing import Generic, TypeVar
from rainbowadn.nullability import *
__all__ = ('StageVerificationProtocol',)
Base = TypeVar('Base')
Stage = TypeVar('Stage')
Header = TypeVar('Header')
class StageVerificationProtocol(Generic[Base, Stage, Header]):
async def initial(self, previous: Nullable[Base], header: Header, stage: Stage) -> bool:
raise NotImplementedError
async def final(self, stage: Stage, base: Base) -> bool:
raise NotImplementedError
async def intermediate(self, previous: Stage, stage: Stage) -> bool:
raise NotImplementedError

View File

@ -58,14 +58,9 @@ class BinaryReducerAction(
lambda: aidentity(case.split.key),
)
left, right = await gather(
reduce_left(),
self.on(case.protocolizedr()),
)
return self.reduce.merge(
left,
right,
return await self.reduce.mergec(
reduce_left,
lambda: self.on(case.protocolizedr()),
)

View File

@ -145,12 +145,12 @@ class FlowCheque(StaticMentionable, RecursiveMentionable):
@classmethod
async def _transaction_minted(cls, transaction: FlowTransaction) -> Iterable[FlowCoin]:
assert isinstance(transaction, FlowTransaction)
return await FlowIterate.iterate(ResolveMapper.wrap_reducer(await transaction.minted_reducer()), [])
return await FlowIterate.iterate(ResolveMapper.wrap_reducer(await transaction.minted_reducer()))
@classmethod
async def _transaction_used(cls, transaction: FlowTransaction) -> Iterable[FlowCoin]:
assert isinstance(transaction, FlowTransaction)
return await FlowIterate.iterate(ResolveMapper.wrap_reducer(await transaction.used_reducer()), [])
return await FlowIterate.iterate(ResolveMapper.wrap_reducer(await transaction.used_reducer()))
@classmethod
async def _transaction_usedx(cls, transaction: FlowTransaction) -> Iterable[KeyValue[FlowCoin, FlowTransaction]]:

View File

@ -11,6 +11,9 @@ class FlowIterate(
Reduce[Element, Iterable[Element]],
Generic[Element]
):
def __init__(self):
super().__init__(())
async def reduce(self, out: Iterable[Element], element: Element) -> Iterable[Element]:
def wrap() -> Iterable[Element]:
yield from out
@ -18,7 +21,7 @@ class FlowIterate(
return wrap()
def merge(self, left: Iterable[Element], right: Iterable[Element]) -> Iterable[Element]:
async def merge(self, left: Iterable[Element], right: Iterable[Element]) -> Iterable[Element]:
def wrap() -> Iterable[Element]:
yield from left
yield from right
@ -31,7 +34,6 @@ class FlowIterate(
@classmethod
async def iterate(
cls,
reducer: Reducer[Element, Iterable[Element]],
initial: Iterable[Element]
reducer: Reducer[Element, Iterable[Element]]
) -> Iterable[Element]:
return await reducer.reduce(cls(initial))
return await reducer.reduce(cls())

View File

@ -19,7 +19,7 @@ class TestBridge(unittest.IsolatedAsyncioTestCase):
async def test_iterator(self):
set_gather_linear()
bridge = ListBridge(list(range(13)))
print(list(await bridge.reduce(FlowIterate([]).loose())))
print(list(await bridge.reduce(FlowIterate().loose())))
@classmethod
async def abt_of(cls, *plains: bytes) -> ActiveBinaryTree[Plain, Integer]:

View File

@ -57,7 +57,7 @@ async def _generate_transaction(
],
keys
)
for coinhp in await (await transaction.minted_reducer()).reduce(FlowIterate([])):
for coinhp in await (await transaction.minted_reducer()).reduce(FlowIterate()):
minted.add(coinhp)
return transaction