egglog 9.0.0__cp313-cp313-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of egglog might be problematic. Click here for more details.

egglog/thunk.py ADDED
@@ -0,0 +1,95 @@
1
+ from __future__ import annotations
2
+
3
+ from dataclasses import dataclass
4
+ from typing import TYPE_CHECKING, Generic, TypeVar
5
+
6
+ from typing_extensions import TypeVarTuple, Unpack
7
+
8
+ if TYPE_CHECKING:
9
+ from collections.abc import Callable
10
+
11
+
12
+ __all__ = ["Thunk", "split_thunk"]
13
+
14
+ T = TypeVar("T")
15
+ TS = TypeVarTuple("TS")
16
+ V = TypeVar("V")
17
+
18
+
19
+ def split_thunk(fn: Callable[[], tuple[T, V]]) -> tuple[Callable[[], T], Callable[[], V]]:
20
+ s = _Split(fn)
21
+ return s.left, s.right
22
+
23
+
24
+ @dataclass
25
+ class _Split(Generic[T, V]):
26
+ fn: Callable[[], tuple[T, V]]
27
+
28
+ def left(self) -> T:
29
+ return self.fn()[0]
30
+
31
+ def right(self) -> V:
32
+ return self.fn()[1]
33
+
34
+
35
+ @dataclass
36
+ class Thunk(Generic[T, Unpack[TS]]):
37
+ """
38
+ Cached delayed function call.
39
+ """
40
+
41
+ state: Resolved[T] | Unresolved[T, Unpack[TS]] | Resolving | Error
42
+
43
+ @classmethod
44
+ def fn(cls, fn: Callable[[Unpack[TS]], T], *args: Unpack[TS]) -> Thunk[T, Unpack[TS]]:
45
+ """
46
+ Create a thunk based on some functions and some partial args.
47
+
48
+ If the function is called while it is being resolved recursively it will raise an exception.
49
+ """
50
+ return cls(Unresolved(fn, args))
51
+
52
+ @classmethod
53
+ def value(cls, value: T) -> Thunk[T]:
54
+ return Thunk(Resolved(value))
55
+
56
+ def __call__(self) -> T:
57
+ match self.state:
58
+ case Resolved(value):
59
+ return value
60
+ case Unresolved(fn, args):
61
+ self.state = Resolving()
62
+ try:
63
+ res = fn(*args)
64
+ except Exception as e:
65
+ self.state = Error(e)
66
+ raise e from None
67
+ else:
68
+ self.state = Resolved(res)
69
+ return res
70
+ case Resolving():
71
+ msg = "Recursively resolving thunk"
72
+ raise ValueError(msg)
73
+ case Error(e):
74
+ raise e
75
+
76
+
77
+ @dataclass
78
+ class Resolved(Generic[T]):
79
+ value: T
80
+
81
+
82
+ @dataclass
83
+ class Unresolved(Generic[T, Unpack[TS]]):
84
+ fn: Callable[[Unpack[TS]], T]
85
+ args: tuple[Unpack[TS]]
86
+
87
+
88
+ @dataclass
89
+ class Resolving:
90
+ pass
91
+
92
+
93
+ @dataclass
94
+ class Error:
95
+ e: Exception
@@ -0,0 +1,113 @@
1
+ """Provides a class for solving type constraints."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from collections import defaultdict
6
+ from dataclasses import dataclass, field
7
+ from itertools import chain, repeat
8
+ from typing import TYPE_CHECKING
9
+
10
+ from typing_extensions import assert_never
11
+
12
+ from .declarations import *
13
+
14
+ if TYPE_CHECKING:
15
+ from collections.abc import Collection, Iterable
16
+
17
+
18
+ __all__ = ["TypeConstraintError", "TypeConstraintSolver"]
19
+
20
+
21
+ class TypeConstraintError(RuntimeError):
22
+ """Typing error when trying to infer the return type."""
23
+
24
+
25
+ @dataclass
26
+ class TypeConstraintSolver:
27
+ """
28
+ Given some typevars and types, solves the constraints to resolve the typevars.
29
+ """
30
+
31
+ _decls: Declarations = field(repr=False)
32
+ # Mapping of class name to mapping of bound class typevar to type
33
+ _cls_typevar_index_to_type: defaultdict[str, dict[ClassTypeVarRef, JustTypeRef]] = field(
34
+ default_factory=lambda: defaultdict(dict)
35
+ )
36
+
37
+ def bind_class(self, ref: JustTypeRef) -> None:
38
+ """
39
+ Bind the typevars of a class to the given types.
40
+ Used for a situation like Map[int, str].create().
41
+ """
42
+ name = ref.name
43
+ cls_typevars = self._decls.get_class_decl(name).type_vars
44
+ if len(cls_typevars) != len(ref.args):
45
+ raise TypeConstraintError(f"Mismatch of typevars {cls_typevars} and {ref}")
46
+ bound_typevars = self._cls_typevar_index_to_type[name]
47
+ for i, arg in enumerate(ref.args):
48
+ bound_typevars[cls_typevars[i]] = arg
49
+
50
+ def infer_arg_types(
51
+ self,
52
+ fn_args: Collection[TypeOrVarRef],
53
+ fn_return: TypeOrVarRef,
54
+ fn_var_args: TypeOrVarRef | None,
55
+ return_: JustTypeRef,
56
+ cls_name: str | None,
57
+ ) -> tuple[Iterable[JustTypeRef], tuple[JustTypeRef, ...] | None]:
58
+ """
59
+ Given a return type, infer the argument types. If there is a variable arg, it returns an infinite iterable.
60
+
61
+ Also returns the bound type params if the class name is passed in.
62
+ """
63
+ self.infer_typevars(fn_return, return_, cls_name)
64
+ arg_types: Iterable[JustTypeRef] = [self.substitute_typevars(a, cls_name) for a in fn_args]
65
+ if fn_var_args:
66
+ # Need to be generator so it can be infinite for variable args
67
+ arg_types = chain(arg_types, repeat(self.substitute_typevars(fn_var_args, cls_name)))
68
+ bound_typevars = (
69
+ tuple(
70
+ v
71
+ # Sort by the index of the typevar in the class
72
+ for _, v in sorted(
73
+ self._cls_typevar_index_to_type[cls_name].items(),
74
+ key=lambda kv: self._decls.get_class_decl(cls_name).type_vars.index(kv[0]),
75
+ )
76
+ )
77
+ if cls_name
78
+ else None
79
+ )
80
+ return arg_types, bound_typevars
81
+
82
+ def infer_typevars(self, fn_arg: TypeOrVarRef, arg: JustTypeRef, cls_name: str | None = None) -> None:
83
+ match fn_arg:
84
+ case TypeRefWithVars(cls_name, fn_args):
85
+ if cls_name != arg.name:
86
+ raise TypeConstraintError(f"Expected {cls_name}, got {arg.name}")
87
+ for inner_fn_arg, inner_arg in zip(fn_args, arg.args, strict=True):
88
+ self.infer_typevars(inner_fn_arg, inner_arg, cls_name)
89
+ case ClassTypeVarRef():
90
+ if cls_name is None:
91
+ msg = "Cannot infer typevar without class name"
92
+ raise RuntimeError(msg)
93
+
94
+ class_typevars = self._cls_typevar_index_to_type[cls_name]
95
+ if fn_arg in class_typevars:
96
+ if class_typevars[fn_arg] != arg:
97
+ raise TypeConstraintError(f"Expected {class_typevars[fn_arg]}, got {arg}")
98
+ else:
99
+ class_typevars[fn_arg] = arg
100
+ case _:
101
+ assert_never(fn_arg)
102
+
103
+ def substitute_typevars(self, tp: TypeOrVarRef, cls_name: str | None = None) -> JustTypeRef:
104
+ match tp:
105
+ case ClassTypeVarRef():
106
+ assert cls_name is not None
107
+ try:
108
+ return self._cls_typevar_index_to_type[cls_name][tp]
109
+ except KeyError as e:
110
+ raise TypeConstraintError(f"Not enough bound typevars for {tp} in class {cls_name}") from e
111
+ case TypeRefWithVars(name, args):
112
+ return JustTypeRef(name, tuple(self.substitute_typevars(arg, cls_name) for arg in args))
113
+ assert_never(tp)