Basic Definitions
Opaque Spaces and Policies
OpaqueSpace
dataclass
Bases: Generic[P, T]
, 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.
Parameters:
Name | Type | 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
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 99 100 101 |
|
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
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 |
|
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
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 |
|
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 preciely, 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.
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 |
|
__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
77 78 79 80 81 |
|
__rmatmul__
__rmatmul__(other: StreamTransformer) -> SearchPolicy[N]
Compose a search policy with a stream transformer.
Source code in src/delphyne/stdlib/policies.py
83 84 85 86 87 88 89 |
|
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).
Source code in src/delphyne/stdlib/policies.py
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 |
|
__rmatmul__
__rmatmul__(other: StreamTransformer) -> PromptingPolicy
Compose a prompting policy with a stream transformer.
Source code in src/delphyne/stdlib/policies.py
184 185 186 187 188 189 190 |
|
ContextualTreeTransformer
dataclass
Contextual tree transformer, which can be composed with search policies to modify their accepted signature.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
A
|
The type of nodes that the transformer removes from search policy signature. |
required | |
B
|
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
286 287 288 289 290 291 292 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 |
|
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
307 308 309 310 311 312 313 314 315 316 317 318 319 |
|
__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
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 |
|
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
129 130 131 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 |
|
_SearchPolicyFn
Bases: Protocol
Source code in src/delphyne/stdlib/policies.py
103 104 105 106 107 108 109 |
|
_ParametricSearchPolicyFn
Bases: Protocol
Source code in src/delphyne/stdlib/policies.py
112 113 114 115 116 117 118 119 120 |
|
_ParametricSearchPolicy
Bases: Protocol
Source code in src/delphyne/stdlib/policies.py
123 124 125 126 |
|
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
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
|
_PromptingPolicyFn
Bases: Protocol
Source code in src/delphyne/stdlib/policies.py
204 205 206 207 208 209 |
|
_ParametricPromptingPolicyFn
Bases: Protocol
Source code in src/delphyne/stdlib/policies.py
212 213 214 215 216 217 218 219 |
|
_ParametricPromptingPolicy
Bases: Protocol
Source code in src/delphyne/stdlib/policies.py
222 223 224 225 |
|
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
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 |
|
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
263 264 265 266 267 268 269 270 271 |
|
_ContextualTreeTransformerFn
Bases: Protocol
Source code in src/delphyne/stdlib/policies.py
274 275 276 277 |
|
_ParametricContextualTreeTransformerFn
Bases: Protocol
Source code in src/delphyne/stdlib/policies.py
280 281 282 283 |
|
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.
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 |
|
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. |
Parameters:
Name | Type | Description | Default |
---|---|---|---|
P
|
Inner policy type associated with the strategy. |
required | |
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
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 |
|
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
83 84 85 86 87 88 89 90 91 92 93 |
|
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(*dec_args: Any, **dec_kwargs: Any) -> 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. |
required |
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. |
required |
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. |
required |
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
129 130 131 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 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 |
|
_StrategyDecorator
Bases: Protocol
Type of the strategy
decorator, after is optional arguments are
instantiated.
Source code in src/delphyne/stdlib/strategies.py
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
|