1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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
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
352
353
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
"""
Type stubs for pattern_core Python bindings.
This module provides Python-friendly bindings for pattern-core, enabling
Python developers to programmatically construct and operate on Pattern and Subject instances.
"""
# Type variables for generic pattern operations
=
=
= # For para return type
"""
Value represents property value types that can be stored in Subject properties.
Supports standard types (string, int, decimal, boolean, symbol) and extended types
(array, map, range, measurement).
"""
"""
Create a string value.
Args:
s: String value
Returns:
String value instance
"""
...
"""
Create an integer value.
Args:
i: Integer value
Returns:
Integer value instance
"""
...
"""
Create a decimal value.
Args:
f: Decimal/float value
Returns:
Decimal value instance
"""
...
"""
Create a boolean value.
Args:
b: Boolean value
Returns:
Boolean value instance
"""
...
"""
Create a symbol value.
Args:
s: Symbol identifier string
Returns:
Symbol value instance
"""
...
"""
Create an array value.
Args:
items: List of Value instances
Returns:
Array value instance
"""
...
"""
Create a map value.
Args:
items: Dictionary mapping strings to Value instances
Returns:
Map value instance
"""
...
"""
Create a range value.
Args:
lower: Lower bound (inclusive), None for unbounded
upper: Upper bound (inclusive), None for unbounded
Returns:
Range value instance
"""
...
"""
Create a measurement value.
Args:
value: Numeric measurement value
unit: Unit string (e.g., "meters", "kg")
Returns:
Measurement value instance
"""
...
"""Extract string value. Raises TypeError if not a string."""
...
"""Extract integer value. Raises TypeError if not an integer."""
...
"""Extract decimal value. Raises TypeError if not a decimal."""
...
"""Extract boolean value. Raises TypeError if not a boolean."""
...
"""Extract array value. Raises TypeError if not an array."""
...
"""Extract map value. Raises TypeError if not a map."""
...
"""
Self-descriptive value type with identity, labels, and properties.
Subjects can represent nodes in a graph or any entity with properties and labels.
"""
"""
Create a new Subject.
Args:
identity: Symbol identifier (string)
labels: Set of label strings (optional)
properties: Map of property names to Value instances (optional)
"""
...
"""Get the subject's identity symbol."""
...
"""Get all labels as a Python set."""
...
"""Get all properties as a Python dictionary."""
...
"""
Add a label to the subject.
Args:
label: Label string to add
"""
...
"""
Remove a label from the subject.
Args:
label: Label string to remove
"""
...
"""
Check if subject has a specific label.
Args:
label: Label string to check
Returns:
True if label exists, False otherwise
"""
...
"""
Get property value by name.
Args:
name: Property name
Returns:
Value instance if property exists, None otherwise
"""
...
"""
Set property value.
Args:
name: Property name
value: Property value (Value instance or Python native type)
"""
...
"""
Remove a property.
Args:
name: Property name to remove
"""
...
"""
Create an identity-only Subject with no labels or properties.
Use as a lightweight reference for add_relationship source/target args
when you only have an identity string.
Example::
g.add_relationship(rel, Subject.from_id("alice"), Subject.from_id("bob"))
"""
...
"""
Create a SubjectBuilder for fluent subject construction.
Args:
identity: The identity string for the subject
Returns:
SubjectBuilder for chaining
Example:
>>> subject = Subject.build("alice").label("Person").property("name", "Alice").done()
"""
...
"""
Recursive, nested structure (s-expression-like) that can hold any value type.
Pattern[V] is fully generic - V can be primitives, objects, or even other Patterns,
enabling nesting types like Pattern[Pattern[T]].
A pattern consists of a valu and zero or more elements (patterns).
The value decorates or says something about the pattern represented by the elements.
Atomic patterns have no elements.
"""
"""
Create an atomic pattern (no elements).
Args:
value: The value for this pattern (any Python type)
Returns:
Atomic Pattern instance
"""
...
"""
Create a pattern with value decoration and elements.
The value decorates or describes the pattern represented by the elements.
Args:
value: The value decoration for this pattern
elements: List of Pattern instances that form the pattern
Returns:
Pattern instance with value and elements
"""
...
"""
Alias for point(). Lift a value into a Pattern.
This follows the functional programming convention where
'of' is used to lift a value into a functor/applicative.
Args:
value: The value for this pattern (any Python type)
Returns:
Atomic Pattern instance
"""
...
"""
Convert a list of values into a list of patterns.
Applies Pattern.of() (which is Pattern.point()) uniformly to every value,
lifting each into a Pattern. Works on any type including Patterns.
Args:
values: List of values to convert (any type)
Returns:
List of Pattern instances
Example:
>>> # From primitives
>>> patterns = Pattern.from_values([1, 2, 3])
>>> len(patterns)
3
>>> # From patterns (creates Pattern<Pattern<T>>)
>>> p1 = Pattern.point("a")
>>> patterns = Pattern.from_values([p1])
>>> patterns[0].value # This is a Pattern!
Pattern(value="a", elements=0)
"""
...
"""Get the pattern's value (can be any Python type including Pattern)."""
...
"""Get the pattern's elements (the patterns that make up this pattern)."""
...
"""
Check if pattern is atomic (has no elements).
Returns:
True if pattern has no elements, False otherwise
"""
...
"""
Get the number of direct elements in this pattern.
Returns:
Number of elements
"""
...
"""
Get the total number of nodes in the pattern tree.
Returns:
Total node count (including this node)
"""
...
"""
Get the maximum nesting depth of the pattern.
Returns:
Maximum depth (0 for atomic patterns)
"""
...
"""
Get all values as a flat list (pre-order traversal).
Returns:
List of all values (any type) in traversal order
"""
...
"""
Check if any value satisfies the predicate.
Args:
predicate: Function that takes a value and returns bool
Returns:
True if any value satisfies predicate, False otherwise
"""
...
"""
Check if all values satisfy the predicate.
Args:
predicate: Function that takes a value and returns bool
Returns:
True if all values satisfy predicate, False otherwise
"""
...
"""
Filter patterns by predicate.
Args:
predicate: Function that takes a Pattern and returns bool
Returns:
List of patterns that satisfy predicate
"""
...
"""
Find first pattern matching predicate.
Args:
predicate: Function that takes a Pattern and returns bool
Returns:
First matching pattern, or None if not found
"""
...
"""
Check if patterns have identical structure.
Args:
other: Pattern to compare with
Returns:
True if patterns match structurally
"""
...
"""
Check if this pattern contains other as a subpattern.
Args:
other: Pattern to search for
Returns:
True if other is a subpattern
"""
...
"""
Transform values while preserving structure.
Args:
func: Function that takes a value and returns a new value
Returns:
New Pattern with transformed values
"""
...
"""
Fold over all values with an accumulator.
Args:
init: Initial accumulator value
func: Function that takes (accumulator, value) and returns new accumulator
Returns:
Final accumulator value
"""
...
"""
Paramorphism: structure-aware fold with access to pattern structure.
At each node, applies func to (current_pattern, element_results) where
element_results is a list of results from recursively processing elements.
Evaluation is bottom-up (elements before parents) and left-to-right.
Atomic patterns receive an empty list for element_results.
This enables structure-dependent computations like depth-weighted sums,
computing multiple statistics in one pass (sum, count, depth), and
structure-preserving transformations.
Args:
func: Function taking (Pattern[V], List[R]) -> R
- First arg is the pattern at current position (including value and elements)
- Second arg is list of results from recursive para on elements
- Returns result of type R
Returns:
Result of applying func across the entire pattern structure
Example:
>>> # Depth-weighted sum: value + sum of element results
>>> pattern = Pattern.pattern(1, [
... Pattern.point(2),
... Pattern.pattern(3, [Pattern.point(4)])
... ])
>>> result = pattern.para(lambda p, rs: p.value + sum(rs))
>>> # Result: 1 + (2 + 0) + (3 + (4 + 0)) = 1 + 2 + 7 = 10
>>> # Atomic pattern receives empty list
>>> atomic = Pattern.point(5)
>>> result = atomic.para(lambda p, rs: p.value + sum(rs))
>>> # Result: 5 + 0 = 5
>>> # Equivalent to fold for simple value aggregation
>>> result = pattern.para(lambda p, rs: p.value + sum(rs))
>>> fold_result = pattern.fold(0, lambda acc, v: acc + v)
>>> # Both produce same sum of values
"""
...
"""
Combine two patterns associatively.
Args:
other: Pattern to combine with
Returns:
Combined Pattern
"""
...
"""
Create patterns by combining three lists pointwise (zipWith3).
Takes three lists and combines them element-wise to create relationship patterns.
Each resulting pattern has value from values list and elements [left, right].
This is useful for creating relationships from separate lists of source nodes,
target nodes, and relationship values.
Args:
left: First list of patterns (e.g., source nodes)
right: Second list of patterns (e.g., target nodes)
values: List of values for the new patterns (e.g., relationship types)
Returns:
List of patterns where each has value from values and elements [left[i], right[i]]
Example:
>>> sources = [Pattern.point("Alice"), Pattern.point("Bob")]
>>> targets = [Pattern.point("Company"), Pattern.point("Project")]
>>> rel_types = ["WORKS_FOR", "MANAGES"]
>>> relationships = Pattern.zip3(sources, targets, rel_types)
"""
...
"""
Create patterns by applying a function to pairs from two lists (zipWith2).
Takes two lists of patterns and applies a function to each pair to compute
the value for the resulting pattern. Useful when relationship values are
derived from the patterns being connected.
Args:
left: First list of patterns (e.g., source nodes)
right: Second list of patterns (e.g., target nodes)
value_fn: Function that computes value from each pair of patterns
Returns:
List of patterns where each has value computed by value_fn
Example:
>>> people = [Pattern.point("Alice"), Pattern.point("Bob")]
>>> companies = [Pattern.point("TechCorp"), Pattern.point("StartupInc")]
>>> relationships = Pattern.zip_with(people, companies,
... lambda p, c: f"{p.value}_WORKS_AT_{c.value}")
"""
...
"""
Extract value at current position (comonad operation).
Returns:
The pattern's value (can be any type)
"""
...
"""
Apply function to all contexts (comonad operation).
Args:
func: Function that takes a Pattern and returns a value
Returns:
New Pattern with func applied to all contexts
"""
...
"""
Decorate each position with its depth.
Returns:
Pattern where each value is replaced with its depth (int)
"""
...
"""
Decorate each position with its subtree size.
Returns:
Pattern where each value is replaced with subtree size (int)
"""
...
"""
Decorate each position with path from root.
Returns:
Pattern where each value is replaced with path indices (List[int])
"""
...
"""
Validate pattern structure against rules.
Args:
rules: ValidationRules instance
Raises:
ValidationError: If validation fails
"""
...
"""
Analyze pattern structure.
Returns:
StructureAnalysis instance with analysis results
"""
...
"""Configuration for pattern validation."""
"""
Create validation rules.
Args:
max_depth: Maximum allowed nesting depth (None for unlimited)
max_elements: Maximum allowed elements per pattern (None for unlimited)
"""
...
"""Get maximum depth constraint."""
...
"""Get maximum elements constraint."""
...
"""Error raised when pattern validation fails."""
"""Get error message."""
...
"""Get name of violated rule."""
...
"""Get location in pattern where violation occurred."""
...
"""Result of pattern structure analysis."""
"""Get human-readable summary."""
...
"""Get count of nodes at each depth."""
...
"""Get element counts at each level."""
...
"""Get description of nesting patterns."""
...
"""
Fluent Subject builder.
Created via ``Subject.build(identity)``. Chain ``.label()`` and ``.property()``
calls, then finalize with ``.done()``.
Example::
subject = Subject.build("alice").label("Person").property("name", "Alice").done()
"""
"""Add a label. Returns self for chaining."""
...
"""Add a property. Accepts native Python types. Returns self for chaining."""
...
"""Finalize the builder and return the constructed Subject."""
...
"""
Ergonomic graph builder and query interface.
Zero configuration — create, add elements, and query without managing
classifiers or policies.
Example::
g = StandardGraph()
print(g.node_count) # 0
"""
"""Create an empty StandardGraph."""
...
"""Create from a list of Pattern instances."""
...
# --- Element addition ---
"""Add a node to the graph. Returns self for chaining."""
...
"""Add a relationship to the graph. Returns self for chaining.
Pass Subject objects for source and target; use Subject.from_id("id") when you only have a string."""
...
"""Add a walk to the graph. Returns self for chaining.
Pass a list of Subject objects; use Subject.from_id("id") for string-only references."""
...
"""Add an annotation to the graph. Returns self for chaining.
Pass the Subject for the annotated element; use Subject.from_id("id") for string-only references."""
...
"""Add a single pattern (classified by shape). Returns self for chaining."""
...
# --- Element access ---
"""Get a node by identity. Returns None if not found."""
...
"""Get a relationship by identity. Returns None if not found."""
...
"""Get a walk by identity. Returns None if not found."""
...
"""Get an annotation by identity. Returns None if not found."""
...
# --- Counts ---
"""Number of nodes."""
...
"""Number of relationships."""
...
"""Number of walks."""
...
"""Number of annotations."""
...
"""True if the graph has no elements."""
...
"""True if any reconciliation conflicts exist."""
...
# --- Iteration ---
"""All nodes as list of (id, Pattern) tuples."""
...
"""All relationships as list of (id, Pattern) tuples."""
...
"""All walks as list of (id, Pattern) tuples."""
...
"""All annotations as list of (id, Pattern) tuples."""
...
# --- Graph-native queries ---
"""Source node of a relationship. Returns None if not found."""
...
"""Target node of a relationship. Returns None if not found."""
...
"""All neighbor nodes of a node (both directions)."""
...
"""Number of incident relationships for a node."""
...
...
...
=