Generators
Generators can be used to create universally quantified variables in a way that preserves type safety.
Example:
TreeNodeType = str
class AncestorOf(BaseModel, Fact):
ancestor: TreeNodeType
descendant: TreeNodeType
@axiom
def ancestor_transitivity_axiom() -> bool:
'''For all x,y,z, if AncestorOf(x,z) and AncestorOf(z,y), then AncestorOf(x,y)'''
return all(
AncestorOf(ancestor=x, descendant=y)
for x, y, z in gen3(TreeNodeType, TreeNodeType, TreeNodeType)
if AncestorOf(ancestor=x, descendant=z) and AncestorOf(ancestor=z, descendant=y)
)
The above axiom defines a universally quantified statement over the variables x, y, and z, whose range is all strings.
Note that while the semantics of the above program are consistent with Python semantics, actually executing the above code would take infinite time as there are infinite strings. Instead, the python is treated as a logical specification.
Composable Generators:
You can also use the composable Gen class to define generators with type-safe composition:
@axiom
def ancestor_transitivity_axiom() -> bool:
'''For all x,y,z, if AncestorOf(x,z) and AncestorOf(z,y), then AncestorOf(x,y)'''
return all(
AncestorOf(ancestor=x, descendant=y)
for x, y, z in Gen(TreeNodeType) * Gen(TreeNodeType) * Gen(TreeNodeType)
if AncestorOf(ancestor=x, descendant=z) and AncestorOf(ancestor=z, descendant=y)
)
The * operator creates a cartesian product of generators, maintaining type information
for the parser.
Gen
Bases: Generic[T]
A composable generator with type signatures for logical specifications.
This class enables creating type-safe, composable generators that can be combined
using operators like * (cartesian product) and + (interleaving).
.. note:: Like the gen1/gen2/gen3 functions, Gen is used for logical specifications and is parsed by the AST parser rather than executed directly.
Example:
@axiom
def my_axiom() -> bool:
return all(
P(x, y, z)
for x, y, z in Gen(str) * Gen(int) * Gen(float)
if Q(x, y)
)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type_or_factory
|
Union[Type[T], Callable[[], Iterator[T]]]
|
Either a type (for logical specifications) or a callable that returns an iterator (for runtime execution) |
required |
Source code in src/typedlogic/generators.py
118 119 120 121 122 123 124 125 126 127 128 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 224 225 226 227 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 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 | |
types
property
Return the tuple of types this generator produces.
__init__(type_or_factory)
Initialize a generator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type_or_factory
|
Union[Type[T], Callable[[], Iterator[T]]]
|
Either a Type for specification purposes, or a callable that returns an iterator for runtime execution |
required |
Source code in src/typedlogic/generators.py
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | |
_infinite_type_iter(t)
staticmethod
Generate infinite instances of a type (for specification purposes).
Source code in src/typedlogic/generators.py
166 167 168 169 170 | |
__iter__()
Return a fresh iterator.
Source code in src/typedlogic/generators.py
177 178 179 | |
__mul__(other)
Combine two generators to produce a cartesian product.
The result flattens nested tuples so that Gen(A) * Gen(B) * Gen(C) yields
(a, b, c) rather than ((a, b), c).
Example:
Gen(int) * Gen(str) # produces Iterator[Tuple[int, str]]
Gen(int) * Gen(str) * Gen(float) # produces Iterator[Tuple[int, str, float]]
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Gen[U]
|
Another Gen to combine with |
required |
Returns:
| Type | Description |
|---|---|
Gen[Tuple[T, U]]
|
A new Gen producing tuples from the cartesian product |
Source code in src/typedlogic/generators.py
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 | |
__add__(other)
Interleave two generators.
Elements are yielded alternating from each generator until one is exhausted, then remaining elements from the other generator are yielded.
Example:
gen1 + gen2 # produces elements alternating from both
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Gen[T]
|
Another Gen to interleave with |
required |
Returns:
| Type | Description |
|---|---|
Gen[T]
|
A new Gen with interleaved elements |
Source code in src/typedlogic/generators.py
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | |
map(f)
Apply a function to each element.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
Callable[[T], U]
|
A function to apply to each element |
required |
Returns:
| Type | Description |
|---|---|
Gen[U]
|
A new Gen with the function applied |
Source code in src/typedlogic/generators.py
248 249 250 251 252 253 254 255 256 257 258 259 260 | |
filter(predicate)
Filter elements by a predicate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predicate
|
Callable[[T], bool]
|
A function returning True for elements to keep |
required |
Returns:
| Type | Description |
|---|---|
Gen[T]
|
A new Gen with only matching elements |
Source code in src/typedlogic/generators.py
262 263 264 265 266 267 268 269 270 271 272 273 274 275 | |
take(n)
Take first n elements.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Number of elements to take |
required |
Returns:
| Type | Description |
|---|---|
Gen[T]
|
A new Gen yielding at most n elements |
Source code in src/typedlogic/generators.py
277 278 279 280 281 282 283 284 285 286 287 288 289 290 | |
gen(*types)
A generator that yields tuples of values of the specified types.
Note: this is more weakly typed than the arity-specific :ref:gen1, :ref:gen2, and :ref:gen3 functions.
.. note:: These are generally used in defining axioms using python syntax, rather than executed directly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
types
|
Type[Any]
|
|
()
|
Returns:
| Type | Description |
|---|---|
Generator[Tuple[Any, ...], None, None]
|
|
Source code in src/typedlogic/generators.py
61 62 63 64 65 66 67 68 69 70 71 72 73 | |
gen1(type1)
A generator that yields individual values of the specified type.
.. note:: This is generally used in defining axioms using python syntax, rather than executed directly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type1
|
Type[T1]
|
|
required |
Returns:
| Type | Description |
|---|---|
Generator[T1, None, None]
|
|
Source code in src/typedlogic/generators.py
76 77 78 79 80 81 82 83 84 85 86 | |
gen2(type1, type2)
A generator that yields arity 2 tuples of values of the specified types.
.. note:: This is generally used in defining axioms using python syntax, rather than executed directly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type1
|
Type[T1]
|
|
required |
type2
|
Type[T2]
|
|
required |
Returns:
| Type | Description |
|---|---|
Generator[Tuple[T1, T2], None, None]
|
|
Source code in src/typedlogic/generators.py
89 90 91 92 93 94 95 96 97 98 99 100 | |
gen3(type1, type2, type3)
A generator that yields arity 3 tuples of values of the specified types.
.. note:: This is generally used in defining axioms using python syntax, rather than executed directly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type1
|
Type[T1]
|
|
required |
type2
|
Type[T2]
|
|
required |
type3
|
Type[T3]
|
|
required |
Returns:
| Type | Description |
|---|---|
Generator[Tuple[T1, T2, T3], None, None]
|
|
Source code in src/typedlogic/generators.py
103 104 105 106 107 108 109 110 111 112 113 114 115 | |
gen_range(start, end)
Generate integers in a range [start, end).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start
|
int
|
Start of range (inclusive) |
required |
end
|
int
|
End of range (exclusive) |
required |
Returns:
| Type | Description |
|---|---|
Gen[int]
|
A Gen yielding integers in the range |
Source code in src/typedlogic/generators.py
299 300 301 302 303 304 305 306 307 | |
gen_list(items)
Generate from a list (cycles through it infinitely).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
items
|
List[T]
|
List of items to cycle through |
required |
Returns:
| Type | Description |
|---|---|
Gen[T]
|
A Gen yielding items from the list |
Source code in src/typedlogic/generators.py
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 | |
gen_const(value)
Generate the same value infinitely.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
T
|
The value to yield |
required |
Returns:
| Type | Description |
|---|---|
Gen[T]
|
A Gen yielding the value infinitely |
Source code in src/typedlogic/generators.py
327 328 329 330 331 332 333 334 335 336 337 338 339 | |
gen_product(*gens)
Combine N generators into a single generator of tuples (cartesian product).
This is a helper function that combines multiple generators using the * operator.
Example:
gen_product(Gen(int), Gen(str), Gen(float))
# equivalent to: Gen(int) * Gen(str) * Gen(float)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
gens
|
Gen[Any]
|
Variable number of Gen instances |
()
|
Returns:
| Type | Description |
|---|---|
Gen[Tuple[Any, ...]]
|
A Gen producing tuples from the cartesian product |
Source code in src/typedlogic/generators.py
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 | |