Basic Definitions
Opaque Spaces and Policies
OpaqueSpace
dataclass
Bases: Space[T]
A space defined by a mapping from the ambient inner policy to a search stream.
Opaque spaces can be defined from strategy instances
(StrategyInstance
) or from queries (Query
) via the using
method. Crucially, policies are unaware of how an opaque space was
created, preserving abstraction.
Class Type Parameters:
Name | Bound or Constraints | Description | Default |
---|---|---|---|
P
|
Type parameter for the ambient inner policy type. |
required | |
T
|
Type parameter for the element type. |
required |
Attributes:
Name | Type | Description |
---|---|---|
stream |
Callable[[PolicyEnv, P], Stream[T]]
|
Maps the ambient inner policy to a search stream. |
Source code in src/delphyne/stdlib/opaque.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
|
from_query
staticmethod
from_query(
query: AbstractQuery[T1], get_policy: Callable[[P1, Sequence[Tag]], PromptingPolicy]
) -> SpaceBuilder[OpaqueSpace[P1, T1]]
Create an opaque space from a query.
The Query.using
method is a more ergonomic wrapper.
Source code in src/delphyne/stdlib/opaque.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
|
from_strategy
staticmethod
from_strategy(
strategy: StrategyComp[N, P2, T1],
get_policy: Callable[[P1, Sequence[Tag]], Policy[N, P2]],
) -> SpaceBuilder[OpaqueSpace[P1, T1]]
Create an opaque space from a strategy instance.
The StrategyInstance.using
method is a more ergonomic wrapper.
Source code in src/delphyne/stdlib/opaque.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
|
Opaque
Opaque = SpaceBuilder[OpaqueSpace[P, T]]
A convenience type alias for an opaque space builder.
IPDict
IPDict = Mapping[str, Policy[Any, Any] | PromptingPolicy]
Type of an Inner-Policy Dictionary.
Inner-Policy dictionaries allow to define strategies in a more concise way in exchange for less static type safety.
Normally, an inner policy type must be defined for every strategy, and
opaque spaces are created from queries or strategy by passing the
using
method a mapping from the ambient inner policy to a proper
sub-policy, often in the form of an anonymous function:
@dataclass class MyInnerPolicy:
foo: PromptingPolicy
# etc
def my_strategy() -> Strategy[Branch, MyInnerPolicy, str]:
x = yield from branch(Foo().using(lambda p: p.foo))
# etc
As an alternative, one can have a strategy use an inner policy
dictionary, by passing ellipses (...
) to the using
method:
def my_strategy() -> Strategy[Branch, IPDict, str]:
x = yield from branch(Foo().using(...))
# etc
When doing so, a simple Python dictionary can be used as an inner
policy, whose keys are space tags (the same tags can be referenced in
demonstration tests). In the example above, and since a spaces induced
by a query inherits its name as a tag by default, one can define an
inner policy for my_strategy
as:
{"Foo": foo_prompting_policy, ...}
A conjunction of tags can also be specified, separated by &
(without
spaces). For example, {"tag1&tag2": pp, ...}
associates prompting
policies pp
to spaces with both tags tag1
and tag2
. New tags can
be added to a space builder using the SpaceBuilder.tagged
method.
Info
If several entries of the inner policy dictionary apply for a given
instance of .using(...)
, a runtime error is raised.
See tests/example_strategies:generate_number
for another example.
Policy
dataclass
Bases: Generic[N, P]
, AbstractPolicy[PolicyEnv, N, P]
A pair of a search policy and of an inner policy.
More precisely, a policy for trees with effects N
(contravariant)
gathers a search policy handling N
along with an inner policy
object of type P
(covariant).
Values of this type can be built concisely using the &
operator
defined on type SearchPolicy
.
Source code in src/delphyne/stdlib/policies.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
|
SearchPolicy
dataclass
Bases: AbstractSearchPolicy[PolicyEnv, N]
A search policy takes as arguments a tree with a given signature
(covariant type parameter N
), a global policy environment, and an
inner policy with appropriate type, and returns a search stream.
SearchPolicy
is a subclass of AbstractSearchPolicy
, which
provides convenience features such as support for the @
composition operator (for composing search policies with stream
transformers and tree transformers) and the &
operator for pairing
a search policy with an inner policy.
Search policies can be conveniently defined using the
search_policy
decorator. See dfs
for an example.
Source code in src/delphyne/stdlib/policies.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
|
__and__
__and__(other: P) -> Policy[N, P]
Pair a search policy with an inner policy to form a policy.
Source code in src/delphyne/stdlib/policies.py
80 81 82 83 84 |
|
__rmatmul__
__rmatmul__(other: StreamTransformer) -> SearchPolicy[N]
Compose a search policy with a stream transformer.
Source code in src/delphyne/stdlib/policies.py
86 87 88 89 90 91 92 |
|
PromptingPolicy
dataclass
Bases: AbstractPromptingPolicy[PolicyEnv]
A prompting policy takes as arguments a query (attached to a
specific node) and a global policy environment, and returns a search
stream (SearchStream
).
PromptingPolicy
is a subclass of AbstractPromptingPolicy
, which
provides convenience features such as support for the @
composition operator (for composing prompting policies with stream
transformers).
Prompting policies can be conveniently defined using the
prompting_policy
decorator. See the definition of few_shot
for
an example.
Source code in src/delphyne/stdlib/policies.py
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
|
__rmatmul__
__rmatmul__(other: StreamTransformer) -> PromptingPolicy
Compose a prompting policy with a stream transformer.
Source code in src/delphyne/stdlib/policies.py
191 192 193 194 195 196 197 |
|
ContextualTreeTransformer
dataclass
Wrapper for a function that maps trees to trees, possibly changing their signature. Can depend on the global policy environment (hence the contextual aspect).
Contextual tree transformers can be composed with search policies to
modify their accepted signature. They can be convniently defined
using the contextual_tree_transformer
decorator. See
elim_compute
and elim_messages
for examples.
Class Type Parameters:
Name | Bound or Constraints | Description | Default |
---|---|---|---|
A
|
Node
|
The type of nodes that the transformer removes from search policy signature. |
required |
B
|
Node
|
The type of nodes that the transformer adds to search policy
signature (or the bottom type |
required |
Attributes:
Name | Type | Description |
---|---|---|
fn |
_ContextualTreeTransformerFn[A, B]
|
A function that takes a policy environment and an inner
policy as arguments (hence the contextual aspect) and
returns a pure tree transformer ( |
Source code in src/delphyne/stdlib/policies.py
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 |
|
pure
staticmethod
pure(fn: PureTreeTransformerFn[A, B]) -> ContextualTreeTransformer[A, B]
Create a contextual tree transformer from a pure tree transformer.
Source code in src/delphyne/stdlib/policies.py
320 321 322 323 324 325 326 327 328 329 330 331 332 |
|
__rmatmul__
__rmatmul__(search_policy: SearchPolicy[B | N]) -> SearchPolicy[A | N]
Compose a contextual tree transformer with a search policy.
Source code in src/delphyne/stdlib/policies.py
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 |
|
Convenience Decorators
search_policy
search_policy(fn: _ParametricSearchPolicyFn[N, A]) -> _ParametricSearchPolicy[N, A]
Convenience decorator for creating parametric search policies (i.e., functions that return search policies).
See dfs
for an example.
Attributes:
Name | Type | Description |
---|---|---|
fn |
A function that takes a tree, a policy environment, an inner
policy, and additional parameters as arguments and returns a
search stream generator ( |
Returns:
Type | Description |
---|---|
_ParametricSearchPolicy[N, A]
|
A function that takes the additional parameters of |
_ParametricSearchPolicy[N, A]
|
arguments and returns a search policy ( |
Source code in src/delphyne/stdlib/policies.py
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
|
_SearchPolicyFn
Bases: Protocol
Source code in src/delphyne/stdlib/policies.py
106 107 108 109 110 111 112 |
|
_ParametricSearchPolicyFn
Bases: Protocol
Source code in src/delphyne/stdlib/policies.py
115 116 117 118 119 120 121 122 123 |
|
_ParametricSearchPolicy
Bases: Protocol
Source code in src/delphyne/stdlib/policies.py
126 127 128 129 |
|
prompting_policy
prompting_policy(
fn: _ParametricPromptingPolicyFn[A],
) -> _ParametricPromptingPolicy[A]
Convenience decorator for creating parametric prompting policies (i.e., functions that return prompting policies).
See the definition of few_shot
for an example.
Attributes:
Name | Type | Description |
---|---|---|
fn |
A function that takes an attached query, a policy
environment, and additional parameters as arguments and
returns a search stream generator ( |
Returns:
Type | Description |
---|---|
_ParametricPromptingPolicy[A]
|
A function that takes the additional parameters of |
_ParametricPromptingPolicy[A]
|
arguments and returns a prompting policy ( |
Source code in src/delphyne/stdlib/policies.py
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
|
_PromptingPolicyFn
Bases: Protocol
Source code in src/delphyne/stdlib/policies.py
211 212 213 214 215 216 |
|
_ParametricPromptingPolicyFn
Bases: Protocol
Source code in src/delphyne/stdlib/policies.py
219 220 221 222 223 224 225 226 |
|
_ParametricPromptingPolicy
Bases: Protocol
Source code in src/delphyne/stdlib/policies.py
229 230 231 232 |
|
contextual_tree_transformer
contextual_tree_transformer(
f: _ParametricContextualTreeTransformerFn[A, B, C],
) -> Callable[C, ContextualTreeTransformer[A, B]]
A convenience decorator for defining contextual tree transformers.
See the implementation of elim_messages
for an example.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
f
|
_ParametricContextualTreeTransformerFn[A, B, C]
|
A function that takes a policy environment, an inner policy,
and additional parameters as arguments and returns a pure
tree transformer ( |
required |
Returns:
Type | Description |
---|---|
Callable[C, ContextualTreeTransformer[A, B]]
|
A function that takes the additional parameters of |
Callable[C, ContextualTreeTransformer[A, B]]
|
arguments and returns a contextual tree transformer |
Callable[C, ContextualTreeTransformer[A, B]]
|
Source code in src/delphyne/stdlib/policies.py
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 |
|
PureTreeTransformerFn
Bases: Protocol
A function that maps any tree with signature A | N
to a tree with
signature B | N
, for all N
.
Source code in src/delphyne/stdlib/policies.py
270 271 272 273 274 275 276 277 278 |
|
_ContextualTreeTransformerFn
Bases: Protocol
Source code in src/delphyne/stdlib/policies.py
281 282 283 284 |
|
_ParametricContextualTreeTransformerFn
Bases: Protocol
Source code in src/delphyne/stdlib/policies.py
287 288 289 290 |
|
Strategies
StrategyInstance
dataclass
Bases: StrategyComp[N, P, T]
A strategy computation that can be reified into a search tree, obtained by instantiating a strategy function.
StrategyInstance
is a subclass of StrategyComp
that adds
convenience features such as the using
method for building opaque
spaces. The strategy
decorator can be used to wrap strategy
functions so as to have them return StrategyInstance
objects.
Class Type Parameters:
Name | Bound or Constraints | Description | Default |
---|---|---|---|
N
|
Node
|
Signature of the strategy. |
required |
P
|
Inner policy type associated with the strategy. |
required | |
T
|
Return type of the strategy. |
required |
Source code in src/delphyne/stdlib/strategies.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
|
using
using(
get_policy: Callable[[Pout], Policy[N, P]] | EllipsisType,
/,
inner_policy_type: type[Pout] | None = None,
) -> Opaque[Pout, T]
Turn a strategy instance into an opaque space by providing a mapping from the ambient inner policy to an appropriate policy.
Attributes:
Name | Type | Description |
---|---|---|
get_policy |
A function that maps the ambient inner policy to
a policy (i.e., a pair of a search policy and of an
inner policy) to use for answering the query.
Alternatively, if the ellipsis value |
|
inner_policy_type |
Ambient inner policy type for the outer strategy from which the strategy is called. This information is not used at runtime but it can be provided to help type inference when necessary. |
Type Parameters:
Name | Bound or Constraints | Description | Default |
---|---|---|---|
Pout
|
Ambient inner policy type associated with the outer strategy from which the strategy is called. |
required |
Source code in src/delphyne/stdlib/strategies.py
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
|
run_toplevel
run_toplevel(
env: PolicyEnv, policy: Policy[N, P], monitor: TreeMonitor = TreeMonitor()
) -> Stream[T]
Reify a strategy into a tree and run it using a given policy.
Source code in src/delphyne/stdlib/strategies.py
87 88 89 90 91 92 93 94 95 96 97 |
|
strategy
strategy(
f: Callable[A, Strategy[N, P, T]],
) -> Callable[A, StrategyInstance[N, P, T]]
strategy(
*,
name: str | None = None,
ret: TypeAnnot[Any] | NoTypeInfo = NoTypeInfo(),
inherit_tags: Callable[..., Sequence[SpaceBuilder[Any]]] | None = None,
) -> _StrategyDecorator
strategy(
f: Callable[..., Any] | None = None,
/,
*,
name: str | None = None,
ret: TypeAnnot[Any] | NoTypeInfo = NoTypeInfo(),
inherit_tags: Callable[..., Sequence[SpaceBuilder[Any]]] | None = None,
) -> Any
Standard parametric decorator for wrapping strategy functions into
functions returning StrategyInstance
objects.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
optional
|
Name of the strategy. If not provided, the name attribute of the strategy function is used instead. The name of the strategy is used in defining default tags and when visualizing traces. |
None
|
ret
|
optional
|
Return type of the strategy function. If not provided, it is obtained by inspecting type annotations. This information is used when visualizing traces and for serializing top-level success values when running commands. |
NoTypeInfo()
|
inherit_tags
|
optional
|
A function that maps all arguments from the decorated strategy function to a sequence of space builders from which tags must be inherited. By default, nothing is inherited. |
None
|
Info
strategy()(f)
can be shortened as @strategy(f)
, hence the
overloading of the type of strategy
.
Runtime use of type annotations
The type annotations for the arguments and return type of a strategy function are leveraged at runtime in two ways:
- To improve the rendering of values when visualizing traces
(e.g., using YAML serialization instead of
pprint
). - To unserialize arguments for the top-level strategy when specified in JSON or YAML and serialize its return value.
In summary, type annotations are fully optional, except when trying to unserialize (resp. serialize) the arguments (resp. return type) of a top-level strategy involving custom data types (and not just JSON values such as integers, strings, dictionaries...).
Source code in src/delphyne/stdlib/strategies.py
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
|
_StrategyDecorator
Bases: Protocol
Type of the strategy
decorator, after is optional arguments are
instantiated.
Source code in src/delphyne/stdlib/strategies.py
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
|