ETraceable + better call optimizations

This commit is contained in:
AF 2023-03-16 13:31:00 +03:00
parent 5965373353
commit 9ef7bc0543
23 changed files with 202 additions and 48 deletions

2
.gitignore vendored
View File

@ -218,4 +218,4 @@ dmypy.json
cython_debug/
/compiled/

View File

@ -0,0 +1,54 @@
# Copyright (c) PARRRATE T&V 2021. All rights reserved.
from typing import Optional
from bu4.evaluation.av.aftervalue import AfterValue
from bu4.evaluation.constructs.evaluable import Evaluable
from bu4.evaluation.constructs.evalue import EValue
from bu4.evaluation.targets.avanonymouscontainer import AVAnonymousContainer
from bu4.evaluation.targets.avcall import AVCall
from bu4.evaluation.targets.avtarget import AVTarget
__all__ = ('EAsync',)
class EAsync(EValue):
def __init__(self):
self.__anext: Optional[Evaluable] = None
def call(self, argument: Evaluable) -> Evaluable:
return AfterAsync(self, AVCall(argument))
async def _anext(self) -> Evaluable:
raise NotImplementedError
async def anext(self) -> Evaluable:
if self.__anext is None:
self.__anext = AVAnonymousContainer(await self._anext())
return self.__anext
class AfterAsync(EAsync):
easync: EAsync
target: AVTarget
def __init__(self, easync: EAsync, target: AVTarget):
self.easync = easync
self.target = target
super().__init__()
async def _anext(self) -> Evaluable:
if isinstance(self.easync, AfterAsync):
return AfterAsync(self.easync.easync, AAChain(self.easync, self.easync.target))
return AfterValue(await self.easync.anext(), self.target)
class AAChain(AVTarget):
def __init__(self, evaluable: AfterAsync, target: AVTarget):
if isinstance(evaluable.target, AAChain):
raise TypeError
self.evaluable = evaluable
self.target = target
def given(self, value: EValue) -> Evaluable:
return AfterValue(self.evaluable.target.given(value), self.target)

View File

@ -0,0 +1,17 @@
# Copyright (c) PARRRATE T&V 2021. All rights reserved.
from typing import Awaitable
from bu4.asynchronous.easync import EAsync
from bu4.evaluation.constructs.evaluable import Evaluable
__all__ = ('EFuture',)
class EFuture(EAsync):
def __init__(self, future: Awaitable):
self.future = future
super().__init__()
async def _anext(self) -> Evaluable:
return await self.future

View File

@ -3,11 +3,11 @@
from bu4.combinatory.lcombinatory import LCombinatory
from bu4.encoding.codes import CODE_CMBI
from bu4.evaluation.constructs.evaluable import Evaluable
from bu4.evaluation.constructs.evalue import EValue
from bu4.evaluation.constructs.etraceable import ETraceable
from bu4.indexing.constructs.indexed import Indexed
from bu4.indexing.evaluation.eivtype import eivtype
from bu4.linking.evaluation.elvtype import elvtype
from bu4.linking.constructs.linked import Linked
from bu4.linking.evaluation.elvtype import elvtype
from bu4.parsing.extensions.CodeExtension import CodeExtension
from bu4.transform.states.transformfinished import TransformFinished
from bu4.transform.states.transformstate import TransformState
@ -15,7 +15,7 @@ from bu4.transform.states.transformstate import TransformState
__all__ = ('EIC', 'IIC', 'XII', 'LIC', 'XLI',)
class EIC(EValue):
class EIC(ETraceable):
def call(self, argument: Evaluable) -> Evaluable:
return argument

View File

@ -4,11 +4,11 @@ from bu4.combinatory.lcombinatory import LCombinatory
from bu4.encoding.codes import CODE_CMBK
from bu4.evaluation.constructs.edelayed import EDelayed
from bu4.evaluation.constructs.evaluable import Evaluable
from bu4.evaluation.constructs.evalue import EValue
from bu4.evaluation.constructs.etraceable import ETraceable
from bu4.indexing.constructs.indexed import Indexed
from bu4.indexing.evaluation.eivtype import eivtype
from bu4.linking.evaluation.elvtype import elvtype
from bu4.linking.constructs.linked import Linked
from bu4.linking.evaluation.elvtype import elvtype
from bu4.parsing.extensions.CodeExtension import CodeExtension
from bu4.transform.states.transformfinished import TransformFinished
from bu4.transform.states.transformstate import TransformState
@ -16,7 +16,7 @@ from bu4.transform.states.transformstate import TransformState
__all__ = ('EKC', 'IKC', 'XIK', 'LKC', 'XLK',)
class EKC(EValue):
class EKC(ETraceable):
def call(self, argument: Evaluable) -> Evaluable:
return EDelayed(argument)

View File

@ -4,13 +4,13 @@ from bu4.combinatory.lcombinatory import LCombinatory
from bu4.encoding.codes import CODE_CMBS
from bu4.evaluation.av.aftervalue import AfterValue
from bu4.evaluation.constructs.evaluable import Evaluable
from bu4.evaluation.constructs.evalue import EValue
from bu4.evaluation.constructs.etraceable import ETraceable
from bu4.evaluation.targets.avanonymouscontainer import AVAnonymousContainer
from bu4.evaluation.targets.avcall import AVCall
from bu4.indexing.constructs.indexed import Indexed
from bu4.indexing.evaluation.eivtype import eivtype
from bu4.linking.evaluation.elvtype import elvtype
from bu4.linking.constructs.linked import Linked
from bu4.linking.evaluation.elvtype import elvtype
from bu4.parsing.extensions.CodeExtension import CodeExtension
from bu4.transform.states.transformfinished import TransformFinished
from bu4.transform.states.transformstate import TransformState
@ -18,7 +18,7 @@ from bu4.transform.states.transformstate import TransformState
__all__ = ('ESC', 'ESC1', 'ESC2', 'ISC', 'XIS', 'LSC', 'XLS',)
class ESC2(EValue):
class ESC2(ETraceable):
def __init__(self, argument: Evaluable, lambda_: Evaluable):
self.argument = AVAnonymousContainer(argument).after_value
self.lambda_ = lambda_
@ -42,7 +42,7 @@ class ESC2(EValue):
return f'/{self.argument}/{self.lambda_}S'
class ESC1(EValue):
class ESC1(ETraceable):
def __init__(self, lambda_: Evaluable):
self.lambda_ = AVAnonymousContainer(lambda_).after_value
@ -53,7 +53,7 @@ class ESC1(EValue):
return f'/{self.lambda_}S'
class ESC(EValue):
class ESC(ETraceable):
def call(self, argument: Evaluable) -> Evaluable:
return ESC1(argument)

View File

@ -94,7 +94,7 @@ def lccall(argument: Linked, lambda_: Linked) -> TransformState[Linked]:
elif isinstance(argument, LCombinatory) and isinstance(lambda_, LCombinatory):
return TransformFinished(LCCall(argument, lambda_))
else:
return TransformFinished(LCall(argument, lambda_))
return TransformFinished(LCall.new(argument, lambda_))
def to_combinatory(linked: Linked) -> TransformState[Linked]:

View File

@ -15,11 +15,14 @@ class AfterValue(Evaluable):
def next(self) -> Evaluable:
if isinstance(self.evaluable, EValue):
if isinstance(self.target, AVChain):
self.evaluable, self.target = self.target.target.given(self.evaluable), self.target.aftertarget
self.target.evaluable.evaluable = self.evaluable
self.evaluable, self.target = self.target.evaluable.target.given(
self.evaluable), self.target.target
return self
return self.target.given(self.evaluable)
if isinstance(self.evaluable, AfterValue):
self.evaluable, self.target = self.evaluable.evaluable, AVChain(self.evaluable.target, self.target)
self.evaluable, self.target = self.evaluable.evaluable, AVChain(self.evaluable, self.target)
del self.target.evaluable.evaluable
self.evaluable = self.evaluable.next()
return self
@ -28,12 +31,15 @@ class AfterValue(Evaluable):
class AVChain(AVTarget):
def __init__(self, target: AVTarget, aftertarget: AVTarget):
def __init__(self, evaluable: AfterValue, target: AVTarget):
if isinstance(evaluable.target, AVChain):
raise TypeError
self.evaluable = evaluable
self.target = target
self.aftertarget = aftertarget
def given(self, value: EValue) -> Evaluable:
return AfterValue(self.target.given(value), self.aftertarget)
self.evaluable.evaluable = value
return AfterValue(self.evaluable.target.given(value), self.target)
def __str__(self):
return f'{self.target}->{self.aftertarget}'
return f'{self.evaluable.target}->{self.target}'

View File

@ -3,7 +3,7 @@
from typing import TypeVar
from bu4.evaluation.constructs.evaluable import Evaluable
from bu4.evaluation.constructs.evalue import EValue
from bu4.evaluation.constructs.etraceable import ETraceable
from bu4.evaluation.targets.avanonymouscontainer import AVAnonymousContainer
__all__ = ('EDelayed',)
@ -11,7 +11,7 @@ __all__ = ('EDelayed',)
T = TypeVar('T')
class EDelayed(EValue):
class EDelayed(ETraceable):
def __init__(self, evaluable: Evaluable):
self.evaluable = AVAnonymousContainer(evaluable).after_value

View File

@ -1,17 +1,13 @@
# Copyright (c) PARRRATE T&V 2021. All rights reserved.
from bu4.evaluation.constructs.evaluable import Evaluable
from bu4.evaluation.constructs.evalue import EValue
from bu4.evaluation.constructs.evoid import EVoid
__all__ = ('EException',)
class EException(EValue):
class EException(EVoid):
def __init__(self, name: bytes):
self.name = name
def call(self, argument: Evaluable) -> Evaluable:
return self
def __str__(self):
return f'«{self.name.decode()}»'

View File

@ -1,14 +1,10 @@
# Copyright (c) PARRRATE T&V 2021. All rights reserved.
from bu4.evaluation.constructs.evaluable import Evaluable
from bu4.evaluation.constructs.evalue import EValue
from bu4.evaluation.constructs.evoid import EVoid
__all__ = ('ENull',)
class ENull(EValue):
def call(self, argument: Evaluable) -> Evaluable:
return self
class ENull(EVoid):
def __str__(self):
return f'?'

View File

@ -0,0 +1,11 @@
# Copyright (c) PARRRATE T&V 2021. All rights reserved.
import abc
from bu4.evaluation.constructs.evalue import EValue
__all__ = ('ETraceable',)
class ETraceable(EValue, abc.ABC):
pass

View File

@ -0,0 +1,30 @@
# Copyright (c) PARRRATE T&V 2021. All rights reserved.
import abc
from bu4.evaluation.constructs.evaluable import Evaluable
from bu4.evaluation.constructs.evalue import EValue
__all__ = ('EVoid', 'ECoVoid',)
class EVoid(EValue, abc.ABC):
def call(self, argument: Evaluable) -> Evaluable:
if isinstance(argument, ECoVoid):
return argument.call(self)
else:
return self
class ECoVoid(EValue):
def void(self) -> EVoid:
raise NotImplementedError
def covoid(self, void: EVoid) -> Evaluable:
raise NotImplementedError
def call(self, argument: Evaluable) -> Evaluable:
if isinstance(argument, EVoid):
return self.covoid(argument)
else:
return self.void()

View File

@ -0,0 +1,32 @@
# Copyright (c) PARRRATE T&V 2021. All rights reserved.
from typing import Type, Generic, TypeVar
from bu4.evaluation.av.aftervalue import AfterValue
from bu4.evaluation.constructs.evaluable import Evaluable
from bu4.evaluation.constructs.evalue import EValue
from bu4.evaluation.targets.avtarget import AVTarget
__all__ = ('AVConvert',)
T = TypeVar('T')
class AVConvert(AVTarget, Generic[T]):
def __init__(self, target_type: Type[T]):
self.target_type = target_type
def iterate(self, value: EValue) -> Evaluable:
raise NotImplementedError
def given_typed(self, value: T) -> Evaluable:
raise NotImplementedError
def given(self, value: EValue) -> Evaluable:
if isinstance(value, self.target_type):
return self.given_typed(value)
else:
return AfterValue(self.iterate(value), self)
def __str__(self):
return f'<{self.target_type}>'

View File

@ -3,14 +3,14 @@
from bu4.evaluation.constructs.attachable import Attachable
from bu4.evaluation.constructs.eattachable import EAttachable
from bu4.evaluation.constructs.evaluable import Evaluable
from bu4.evaluation.constructs.evalue import EValue
from bu4.evaluation.constructs.etraceable import ETraceable
from bu4.indexing.evaluation.eivtype import eivtype
from bu4.indexing.evaluation.lambdaeiv import LambdaEiv
__all__ = ('EILambda',)
class EILambda(EValue):
class EILambda(ETraceable):
def __init__(self, ev: eivtype, value: Attachable[eivtype], *, memoize: bool):
self.ev = ev
self.value = value

View File

@ -1,5 +1,9 @@
# Copyright (c) PARRRATE T&V 2021. All rights reserved.
from pathlib import Path
from textwrap import fill
from time import time_ns
from bu4.combinatory.tocombinatory import to_combinatory
from bu4.evaluation.constructs.evalue import EValue
from bu4.evaluation.sync import sync
@ -17,6 +21,11 @@ def isynced(source: str, *, combinatory=False) -> EValue:
linked = transform(parsed.link(set()))
if combinatory:
linked = transform(to_combinatory(linked))
root = Path('compiled') / f'{round(time_ns())}'
root.mkdir(parents=True)
(root / 'source.bu4').write_text(source, encoding='utf-8')
indexed = transform(linked.index([]))
evaluable = indexed.attach([])
bytesrc = ' '.join(f'{chr(c)} ' if c in range(0x20, 0xff) else f'{c:02x}' for c in bytes(indexed))
(root / 'compiled.txt').write_text(fill(bytesrc, width=120), encoding='utf-8')
return sync(evaluable)

View File

@ -24,7 +24,7 @@ class PCall(Parsed):
lambda argument: AfterTransform(
TransformStart(lambda: self.lambda_.link(promise)),
ATLambda(
lambda lambda_: TransformFinished(LCall(argument, lambda_))
lambda lambda_: TransformFinished(LCall.new(argument, lambda_))
)
)
)

View File

@ -6,6 +6,7 @@ from bu4.evaluation.constructs.evaluable import Evaluable
from bu4.evaluation.targets.avcall import AVCall
from bu4.indexing.constructs.icall import ICall
from bu4.indexing.constructs.indexed import Indexed
from bu4.linking.constructs.ldelayed import LDelayed
from bu4.linking.constructs.linked import Linked
from bu4.linking.evaluation.elvtype import elvtype
from bu4.transform.states.aftertransform import AfterTransform
@ -21,6 +22,10 @@ class LCall(Linked):
argument: Linked
lambda_: Linked
@classmethod
def new(cls, argument: Linked, lambda_: Linked) -> Linked:
return lambda_.value if isinstance(lambda_, LDelayed) else LCall(argument, lambda_)
def __init__(self, argument: Linked, lambda_: Linked):
self.argument = argument
self.lambda_ = lambda_

View File

@ -23,7 +23,7 @@ class LLambda(Linked):
value: Linked
def __new__(cls, name: bytes, value: Linked):
return Linked.__new__(LLambda) if name in value.future else LDelayed(value)
return Linked.__new__(cls) if name in value.future else LDelayed(value)
def __init__(self, name: bytes, value: Linked):
self.name = name
@ -31,7 +31,7 @@ class LLambda(Linked):
self.used = name in value.future
self.memoize = name in value.multifuture
self.future = self.value.future - {name}
self.multifuture = self.future if self.used else self.value.multifuture
self.multifuture = self.future if self.used else self.value.future
def attach(self, env: elvtype) -> Evaluable:
return (

View File

@ -2,7 +2,7 @@
from bu4.evaluation.constructs.eattachable import EAttachable
from bu4.evaluation.constructs.evaluable import Evaluable
from bu4.evaluation.constructs.evalue import EValue
from bu4.evaluation.constructs.etraceable import ETraceable
from bu4.linking.constructs.linked import Linked
from bu4.linking.evaluation.elvtype import elvtype
from bu4.linking.evaluation.lambdaelv import LambdaElv
@ -10,7 +10,7 @@ from bu4.linking.evaluation.lambdaelv import LambdaElv
__all__ = ('ELLambda',)
class ELLambda(EValue):
class ELLambda(ETraceable):
def __init__(self, env: elvtype, name: bytes, value: Linked):
self.env = env
self.name = name

View File

@ -1,13 +1,8 @@
# Copyright (c) PARRRATE T&V 2021. All rights reserved.
from bu4.combinatory.lic import EIC
from bu4.combinatory.lkc import EKC
from bu4.combinatory.lsc import ESC, ESC1, ESC2
from bu4.evaluation.constructs.edelayed import EDelayed
from bu4.evaluation.constructs.evalue import EValue
from bu4.evaluation.constructs.etraceable import ETraceable
from bu4.evaluation.sync import sync
from bu4.indexing.evaluation.eilambda import EILambda
from bu4.linking.evaluation.ellambda import ELLambda
from bu4.tracing.probe import Probe
__all__ = ('trace', 'probe_index_reset',)
@ -19,7 +14,7 @@ def trace(lambda_: EValue):
global _probe_index
size = 0
while True:
if not isinstance(lambda_, (ELLambda, EILambda, EIC, EKC, ESC, ESC1, ESC2, EDelayed)):
if not isinstance(lambda_, ETraceable):
return size, lambda_
lambda_ = sync(lambda_.call(Probe(_probe_index)))
_probe_index += 1

View File

@ -1,6 +1,8 @@
# Copyright (c) PARRRATE T&V 2021. All rights reserved.
import random
import shutil
from pathlib import Path
from bu4.common_interface import with_common_interface
from bu4.evaluation.constructs.proxy import antiproxy, eproxy
@ -10,6 +12,7 @@ from bu4.linkable.toolchain.readfile import readfile
from bu4.tracing.deep_trace import deep_trace
from timesample import TimeSample
shutil.rmtree(Path('compiled'), ignore_errors=True)
with TimeSample('all'):
with TimeSample('sys0'):
with TimeSample('compilation'):

View File

@ -26,4 +26,4 @@
[YC]
|
_exp
}
}