Skip to content

Commit a0f5e01

Browse files
authored
Remove intp argument from apply (#333)
1 parent 731e446 commit a0f5e01

7 files changed

Lines changed: 19 additions & 30 deletions

File tree

effectful/handlers/numpyro.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def _to_named(a):
8585
return a
8686

8787
# Convert to a term in a context that does not evaluate distribution constructors.
88-
def _apply(intp, op, *args, **kwargs):
88+
def _apply(op, *args, **kwargs):
8989
typ = op.__type_rule__(*args, **kwargs)
9090
if issubclass(typ, dist.Distribution):
9191
return defdata(op, *args, **kwargs)
@@ -133,7 +133,7 @@ def _to_positional(a, indices):
133133
return a
134134

135135
# Convert to a term in a context that does not evaluate distribution constructors.
136-
def _apply(intp, op, *args, **kwargs):
136+
def _apply(op, *args, **kwargs):
137137
typ = op.__type_rule__(*args, **kwargs)
138138
if issubclass(typ, dist.Distribution):
139139
return defdata(op, *args, **kwargs)

effectful/handlers/pyro.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ def _to_named(a):
369369
return a
370370

371371
# Convert to a term in a context that does not evaluate distribution constructors.
372-
def _apply(intp, op, *args, **kwargs):
372+
def _apply(op, *args, **kwargs):
373373
typ = op.__type_rule__(*args, **kwargs)
374374
if issubclass(
375375
typ, pyro.distributions.torch_distribution.TorchDistribution
@@ -417,7 +417,7 @@ def _to_positional(a, indices):
417417
return a
418418

419419
# Convert to a term in a context that does not evaluate distribution constructors.
420-
def _apply(intp, op, *args, **kwargs):
420+
def _apply(op, *args, **kwargs):
421421
typ = op.__type_rule__(*args, **kwargs)
422422
if issubclass(
423423
typ, pyro.distributions.torch_distribution.TorchDistribution

effectful/handlers/torch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def _torch_getitem_sizeof(
7272

7373
return defdata(torch_getitem, x, key)
7474

75-
def _apply(_, op, *args, **kwargs):
75+
def _apply(op, *args, **kwargs):
7676
args, kwargs = tree.map_structure(defterm, (args, kwargs))
7777
return defdata(op, *args, **kwargs)
7878

effectful/ops/semantics.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
from effectful.ops.types import Expr, Interpretation, NotHandled, Operation, Term
1212

1313

14-
@defop
15-
def apply(intp: Interpretation, op: Operation, *args, **kwargs) -> Any:
14+
@defop # type: ignore
15+
def apply[**P, T](op: Operation[P, T], *args: P.args, **kwargs: P.kwargs) -> T:
1616
"""Apply ``op`` to ``args``, ``kwargs`` in interpretation ``intp``.
1717
1818
Handling :func:`apply` changes the evaluation strategy of terms.
@@ -41,14 +41,15 @@ def apply(intp: Interpretation, op: Operation, *args, **kwargs) -> Any:
4141
mul(add(1, 2), 3)
4242
4343
"""
44-
from effectful.internals.runtime import interpreter
44+
from effectful.internals.runtime import get_interpretation
4545

46+
intp = get_interpretation()
4647
if op in intp:
47-
return interpreter(intp)(intp[op])(*args, **kwargs)
48+
return intp[op](*args, **kwargs)
4849
elif apply in intp:
49-
return intp[apply](intp, op, *args, **kwargs)
50+
return intp[apply](op, *args, **kwargs)
5051
else:
51-
return op.__default_rule__(*args, **kwargs)
52+
return op.__default_rule__(*args, **kwargs) # type: ignore
5253

5354

5455
@defop # type: ignore
@@ -208,7 +209,7 @@ def runner(intp: Interpretation):
208209
from effectful.internals.runtime import get_interpretation, interpreter
209210

210211
@interpreter(get_interpretation())
211-
def _reapply[**P, S](_, op: Operation[P, S], *args: P.args, **kwargs: P.kwargs):
212+
def _reapply[**P, S](op: Operation[P, S], *args: P.args, **kwargs: P.kwargs):
212213
return op(*args, **kwargs)
213214

214215
with interpreter({apply: _reapply, **intp}):
@@ -316,7 +317,7 @@ def typeof[T](term: Expr[T]) -> type[T]:
316317
"""
317318
from effectful.internals.runtime import interpreter
318319

319-
with interpreter({apply: lambda _, op, *a, **k: op.__type_rule__(*a, **k)}):
320+
with interpreter({apply: lambda op, *a, **k: op.__type_rule__(*a, **k)}):
320321
if isinstance(term, Term):
321322
# If term is a Term, we evaluate it to get its type
322323
tp = evaluate(term)
@@ -353,7 +354,7 @@ def fvsof[S](term: Expr[S]) -> set[Operation]:
353354

354355
_fvs: set[Operation] = set()
355356

356-
def _update_fvs(_, op, *args, **kwargs):
357+
def _update_fvs(op, *args, **kwargs):
357358
_fvs.add(op)
358359
arg_ctxs, kwarg_ctxs = op.__fvs_rule__(*args, **kwargs)
359360
bound_vars = set().union(

effectful/ops/syntax.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -958,10 +958,7 @@ def _(op, *args, **kwargs):
958958
if c:
959959
res = evaluate(
960960
v,
961-
intp={
962-
apply: lambda _, op, *a, **k: defdata(op, *a, **k),
963-
**{op: renaming[op] for op in c},
964-
},
961+
intp={apply: defdata, **{op: renaming[op] for op in c}},
965962
)
966963
if isinstance(i, int):
967964
args_[i] = res
@@ -1067,12 +1064,7 @@ def trace[**P, T](value: Callable[P, T]) -> Callable[P, T]:
10671064
)
10681065
bound_sig.apply_defaults()
10691066

1070-
with interpreter(
1071-
{
1072-
apply: lambda _, op, *a, **k: defdata(op, *a, **k),
1073-
call: call.__default_rule__,
1074-
}
1075-
):
1067+
with interpreter({apply: defdata, call: call.__default_rule__}):
10761068
body = value(
10771069
*[a() for a in bound_sig.args],
10781070
**{k: v() for k, v in bound_sig.kwargs.items()},

effectful/ops/types.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,9 @@ def __fvs_rule__(
7272

7373
@typing.final
7474
def __call__(self, *args: Q.args, **kwargs: Q.kwargs) -> V:
75-
from effectful.internals.runtime import get_interpretation
7675
from effectful.ops.semantics import apply
7776

78-
return apply.__default_rule__(get_interpretation(), self, *args, **kwargs) # type: ignore
77+
return apply.__default_rule__(self, *args, **kwargs) # type: ignore
7978

8079
def __repr__(self):
8180
return f"{self.__class__.__name__}({self.__name__}, {self.__signature__})"
@@ -146,7 +145,7 @@ def term_str(term):
146145
)
147146
return str(term)
148147

149-
def _apply(_, op, *args, **kwargs) -> str:
148+
def _apply(op, *args, **kwargs) -> str:
150149
args_str = ", ".join(map(term_str, args)) if args else ""
151150
kwargs_str = (
152151
", ".join(f"{k}={term_str(v)}" for k, v in kwargs.items())

tests/test_ops_semantics.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
import effectful.handlers.numbers # noqa: F401
1010
from effectful.ops.semantics import (
11-
apply,
1211
coproduct,
1312
evaluate,
1413
fvsof,
@@ -578,7 +577,6 @@ def g(x: str, y: bool) -> str:
578577
runner(i)
579578
product(i, i)
580579
coproduct(i, i)
581-
apply(i, f, 1)
582580
evaluate(0, intp=i)
583581

584582
# include tests with inlined interpretation, because mypy might do inference
@@ -593,7 +591,6 @@ def g(x: str, y: bool) -> str:
593591
{f: lambda x: x + 1, g: lambda x, y: x + str(y)},
594592
{f: lambda x: x + 1, g: lambda x, y: x + str(y)},
595593
)
596-
apply({f: lambda x: x + 1, g: lambda x, y: x + str(y)}, f, 1)
597594
evaluate(0, intp={f: lambda x: x + 1, g: lambda x, y: x + str(y)})
598595

599596

0 commit comments

Comments
 (0)