Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Expr type explicit, add Literal constructor #20

Merged
merged 4 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
7 changes: 7 additions & 0 deletions effectful/internals/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
Symbol,
Term,
Variable,
Literal,
)

from ..ops import core
Expand Down Expand Up @@ -53,6 +54,11 @@ class _BaseVariable(Generic[T]):
type: Type[T]


@dataclasses.dataclass
class _BaseLiteral(Generic[T]):
value: T


@runtime.weak_memoize
def base_define(m: Type[T] | Callable[Q, T]) -> Operation[..., T]:
if not typing.TYPE_CHECKING:
Expand Down Expand Up @@ -82,6 +88,7 @@ def defop(fn: Callable[..., S]) -> _BaseOperation[..., S]:
core.register(core.define(Operation), None, _BaseOperation)
core.register(core.define(Term), None, _BaseTerm)
core.register(core.define(Variable), None, _BaseVariable)
core.register(core.define(Literal), None, _BaseLiteral)
core.register(core.define(Interpretation), None, dict)
core.register(core.define(Symbol), None, str)
core.register(core.define(Context), None, dict)
11 changes: 9 additions & 2 deletions effectful/ops/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,18 @@ def default(self, *args: P.args, **kwargs: P.kwargs) -> T_co: ...
Interpretation = Mapping[Operation[..., T], Callable[..., V]]


class Literal(Protocol[T]):
value: T


Expr = Literal[T] | "Term[T]" | "Variable[T]"


@typing.runtime_checkable
class Term(Protocol[T]):
op: Operation[..., T]
args: Iterable["Term[T]" | T | "Variable[T]"]
kwargs: Mapping[str, "Term[T]" | T | "Variable[T]"]
args: Iterable[Expr[T]]
kwargs: Mapping[str, Expr[T]]


Symbol = str # TODO replace with extensional protocol type
Expand Down
Loading