kumi 0.0.4 → 0.0.6

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.
Files changed (89) hide show
  1. checksums.yaml +4 -4
  2. data/CLAUDE.md +160 -8
  3. data/README.md +278 -200
  4. data/{documents → docs}/AST.md +29 -29
  5. data/{documents → docs}/DSL.md +3 -3
  6. data/{documents → docs}/SYNTAX.md +107 -24
  7. data/docs/features/README.md +45 -0
  8. data/docs/features/analysis-cascade-mutual-exclusion.md +89 -0
  9. data/docs/features/analysis-type-inference.md +42 -0
  10. data/docs/features/analysis-unsat-detection.md +71 -0
  11. data/docs/features/array-broadcasting.md +170 -0
  12. data/docs/features/input-declaration-system.md +42 -0
  13. data/docs/features/performance.md +16 -0
  14. data/examples/federal_tax_calculator_2024.rb +43 -40
  15. data/examples/game_of_life.rb +97 -0
  16. data/examples/simple_rpg_game.rb +1000 -0
  17. data/examples/static_analysis_errors.rb +178 -0
  18. data/examples/wide_schema_compilation_and_evaluation_benchmark.rb +1 -1
  19. data/lib/kumi/analyzer/analysis_state.rb +37 -0
  20. data/lib/kumi/analyzer/constant_evaluator.rb +22 -16
  21. data/lib/kumi/analyzer/passes/broadcast_detector.rb +251 -0
  22. data/lib/kumi/analyzer/passes/{definition_validator.rb → declaration_validator.rb} +8 -7
  23. data/lib/kumi/analyzer/passes/dependency_resolver.rb +106 -26
  24. data/lib/kumi/analyzer/passes/input_collector.rb +105 -23
  25. data/lib/kumi/analyzer/passes/name_indexer.rb +2 -2
  26. data/lib/kumi/analyzer/passes/pass_base.rb +11 -28
  27. data/lib/kumi/analyzer/passes/semantic_constraint_validator.rb +110 -0
  28. data/lib/kumi/analyzer/passes/toposorter.rb +45 -9
  29. data/lib/kumi/analyzer/passes/type_checker.rb +34 -11
  30. data/lib/kumi/analyzer/passes/type_consistency_checker.rb +2 -1
  31. data/lib/kumi/analyzer/passes/type_inferencer.rb +128 -21
  32. data/lib/kumi/analyzer/passes/unsat_detector.rb +312 -13
  33. data/lib/kumi/analyzer/passes/visitor_pass.rb +4 -3
  34. data/lib/kumi/analyzer.rb +41 -24
  35. data/lib/kumi/atom_unsat_solver.rb +45 -0
  36. data/lib/kumi/cli.rb +449 -0
  37. data/lib/kumi/compiler.rb +194 -16
  38. data/lib/kumi/constraint_relationship_solver.rb +638 -0
  39. data/lib/kumi/domain/validator.rb +0 -4
  40. data/lib/kumi/error_reporter.rb +6 -6
  41. data/lib/kumi/evaluation_wrapper.rb +20 -4
  42. data/lib/kumi/explain.rb +28 -28
  43. data/lib/kumi/export/node_registry.rb +26 -12
  44. data/lib/kumi/export/node_serializers.rb +1 -1
  45. data/lib/kumi/function_registry/collection_functions.rb +117 -9
  46. data/lib/kumi/function_registry/function_builder.rb +4 -3
  47. data/lib/kumi/function_registry.rb +8 -2
  48. data/lib/kumi/input/type_matcher.rb +3 -0
  49. data/lib/kumi/input/validator.rb +0 -3
  50. data/lib/kumi/parser/declaration_reference_proxy.rb +36 -0
  51. data/lib/kumi/parser/dsl_cascade_builder.rb +19 -8
  52. data/lib/kumi/parser/expression_converter.rb +80 -12
  53. data/lib/kumi/parser/input_builder.rb +40 -9
  54. data/lib/kumi/parser/input_field_proxy.rb +46 -0
  55. data/lib/kumi/parser/input_proxy.rb +3 -3
  56. data/lib/kumi/parser/nested_input.rb +15 -0
  57. data/lib/kumi/parser/parser.rb +2 -0
  58. data/lib/kumi/parser/schema_builder.rb +10 -9
  59. data/lib/kumi/parser/sugar.rb +171 -18
  60. data/lib/kumi/schema.rb +3 -1
  61. data/lib/kumi/schema_instance.rb +69 -3
  62. data/lib/kumi/syntax/array_expression.rb +15 -0
  63. data/lib/kumi/syntax/call_expression.rb +11 -0
  64. data/lib/kumi/syntax/cascade_expression.rb +11 -0
  65. data/lib/kumi/syntax/case_expression.rb +11 -0
  66. data/lib/kumi/syntax/declaration_reference.rb +11 -0
  67. data/lib/kumi/syntax/hash_expression.rb +11 -0
  68. data/lib/kumi/syntax/input_declaration.rb +12 -0
  69. data/lib/kumi/syntax/input_element_reference.rb +12 -0
  70. data/lib/kumi/syntax/input_reference.rb +12 -0
  71. data/lib/kumi/syntax/literal.rb +11 -0
  72. data/lib/kumi/syntax/root.rb +1 -0
  73. data/lib/kumi/syntax/trait_declaration.rb +11 -0
  74. data/lib/kumi/syntax/value_declaration.rb +11 -0
  75. data/lib/kumi/types/compatibility.rb +8 -0
  76. data/lib/kumi/types/validator.rb +1 -1
  77. data/lib/kumi/vectorization_metadata.rb +108 -0
  78. data/lib/kumi/version.rb +1 -1
  79. data/scripts/generate_function_docs.rb +22 -10
  80. metadata +38 -17
  81. data/CHANGELOG.md +0 -25
  82. data/lib/kumi/domain.rb +0 -8
  83. data/lib/kumi/input.rb +0 -8
  84. data/lib/kumi/syntax/declarations.rb +0 -23
  85. data/lib/kumi/syntax/expressions.rb +0 -30
  86. data/lib/kumi/syntax/terminal_expressions.rb +0 -27
  87. data/lib/kumi/syntax.rb +0 -9
  88. data/test_impossible_cascade.rb +0 -51
  89. /data/{documents → docs}/FUNCTIONS.md +0 -0
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 243e6f2e2f7f65919f41fae1eddf877796e6221674e41dc5d32a36c708efab03
4
- data.tar.gz: 8649def0a1299dd416d00480daa9fb0dfe3b6e418c4ea6c1ad869c873ad3eedf
3
+ metadata.gz: 91e6e3b6ce1e4911d37588ba8aaf9b89f8129d23fe93b05268756c417249da90
4
+ data.tar.gz: f019de05a9e05957184fc649d187306fa7cd3736cb9d04c56d6080d4a3cde9de
5
5
  SHA512:
6
- metadata.gz: e677d95d1ef34f8144b1f829259ad716e8f3822ab216957cf34a0058b4f7651843232202dc485a62e4c44077761a91b4db419963b649fae4a429042bf49be410
7
- data.tar.gz: 3e7795238333d362f35c71b9478b01f4ff396110dc517a320dc304288a1ab8deac0a3af8b99c3d8f87cfd47a9a91f1bbd46509eb0055db4faa042b9b6ca14bd9
6
+ metadata.gz: 6d6439258bcb876374c5f7ca3bafa4e59faabf112e3cfe2dac532f752b2b7ef8533bf888c4e6feb1509995069a103a258b37c6b18b0164487e5215e03dd85b10
7
+ data.tar.gz: 6e64f9725569836d7a17b83aa82f316f01d4a9169a1323cee3d1d5fcf7d3782902b9c0695bcdb3735a4c206906463a4b7ca3e14f39f253ba23bf23d572ce95aa
data/CLAUDE.md CHANGED
@@ -3,6 +3,7 @@
3
3
  !! Remember, this gem is not on production yet, so no backward compatilibity is necessary. But do not change the public interfaces (e.g. DSL, Schema) without explicitly requested or demanded.
4
4
  !! We are using zeitwerk, i.e.: no requires
5
5
  !! Do not care about linter or coverage unless asked to do so.
6
+ !! IMPORTANT: Communication style - Write direct, factual statements. Avoid promotional language, unnecessary claims, or marketing speak. State what the system does, not what benefits it provides. Use TODOs for missing information rather than placeholder claims.
6
7
 
7
8
  This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
8
9
 
@@ -27,6 +28,19 @@ Kumi is a declarative decision-modeling compiler for Ruby that transforms comple
27
28
  - `gem build kumi.gemspec` - Build the gem
28
29
  - `gem install ./kumi-*.gem` - Install locally built gem
29
30
 
31
+ ### Kumi CLI
32
+ - `./bin/kumi -i` - Start interactive REPL mode for rapid schema testing
33
+ - `./bin/kumi -f schema.rb -d data.json` - Execute schema file with input data
34
+ - `./bin/kumi -f schema.rb -k key1,key2` - Extract specific keys from schema
35
+ - `./bin/kumi -f schema.rb -e key_name` - Explain how a key is computed
36
+ - `./bin/kumi -f schema.rb -d data.json -o json` - Output results in JSON format
37
+
38
+ **CLI Features**:
39
+ - Interactive REPL with schema loading, data manipulation, and live evaluation
40
+ - File-based schema execution with JSON/YAML input data support
41
+ - Selective key extraction and explain functionality for debugging
42
+ - Multiple output formats (pretty, JSON, YAML) for integration
43
+
30
44
  ## Architecture Overview
31
45
 
32
46
  ### Core Components
@@ -47,9 +61,17 @@ Kumi is a declarative decision-modeling compiler for Ruby that transforms comple
47
61
  **Syntax Tree** (`lib/kumi/syntax/`):
48
62
  - `node.rb` - Base node class with location tracking
49
63
  - `root.rb` - Root schema node containing inputs, attributes, and traits
50
- - `declarations.rb` - Attribute and trait declaration nodes
51
- - `expressions.rb` - Expression nodes (calls, lists, cascades)
52
- - `terminal_expressions.rb` - Terminal nodes (literals, field references, bindings, field declarations)
64
+ - `value_declaration.rb` - Value declaration nodes (formerly Attribute)
65
+ - `trait_declaration.rb` - Trait declaration nodes (formerly Trait)
66
+ - `input_declaration.rb` - Input field declaration nodes (formerly FieldDecl)
67
+ - `call_expression.rb` - Function call expression nodes
68
+ - `array_expression.rb` - Array expression nodes (formerly ListExpression)
69
+ - `hash_expression.rb` - Hash expression nodes (for future hash literals)
70
+ - `cascade_expression.rb` - Cascade expression nodes (conditional values)
71
+ - `case_expression.rb` - Case expression nodes (formerly WhenCaseExpression)
72
+ - `literal.rb` - Literal value nodes
73
+ - `input_reference.rb` - Input field reference nodes (formerly FieldRef)
74
+ - `declaration_reference.rb` - Declaration reference nodes (formerly Binding)
53
75
 
54
76
  **Analyzer** (`lib/kumi/analyzer.rb`):
55
77
  - Multi-pass analysis system that validates schemas and builds dependency graphs
@@ -91,6 +113,58 @@ Kumi is a declarative decision-modeling compiler for Ruby that transforms comple
91
113
  - `domain/enum_analyzer.rb` - Enumeration domain analysis and validation
92
114
  - `domain/violation_formatter.rb` - Formats domain violation error messages
93
115
 
116
+ ## DSL Syntax Requirements
117
+
118
+ ### Critical Syntax Rules
119
+
120
+ **Module Definition Structure** (REQUIRED for CLI):
121
+ ```ruby
122
+ # CORRECT - CLI can find and load this
123
+ module SchemaName
124
+ extend Kumi::Schema
125
+
126
+ schema do
127
+ # schema definition here
128
+ end
129
+ end
130
+
131
+ # INCORRECT - CLI cannot load standalone schemas
132
+ schema do # This won't work with CLI
133
+ # schema definition
134
+ end
135
+ ```
136
+
137
+ **Function Call Syntax**:
138
+ - **Symbol style**: `fn(:function_name, arg1, arg2)` - Always works, explicit
139
+ - **Method style**: `fn.function_name(arg1, arg2)` - Also works, more readable
140
+ - **Incorrect**: `fn()` - Empty function calls cause parse errors
141
+ - **Incorrect**: `fn.function_name()` - Empty method calls cause parse errors
142
+
143
+ **Arithmetic Operations**:
144
+ - **Sugar Syntax**: `input.field1 + input.field2` - Works for input fields and value references
145
+ - **Function Syntax**: `fn(:add, input.field1, input.field2)` - Always works, more explicit
146
+ - **Mixed**: Use sugar for simple operations, functions for complex ones
147
+
148
+ **Cascade Condition Syntax**:
149
+ ```ruby
150
+ # CORRECT - use symbols for trait references in cascades
151
+ value :status do
152
+ on trait_name, "Result"
153
+ base "Default"
154
+ end
155
+
156
+ # INCORRECT - bare identifiers don't work in cascade conditions
157
+ value :status do
158
+ on trait_name, "Result" # This will fail
159
+ base "Default"
160
+ end
161
+ ```
162
+
163
+ **UnsatDetector Considerations**:
164
+ - Cascades with mutually exclusive conditions are valid (e.g., `amount < 100` vs `amount >= 100`)
165
+ - Values that depend on such cascades are also valid (fixed in recent update)
166
+ - Use clear, non-contradictory trait names to avoid confusion
167
+
94
168
  ### Key Patterns
95
169
 
96
170
  **DSL Structure**:
@@ -120,8 +194,8 @@ end
120
194
  In cascade expressions (`value :name do ... end`), trait references use **symbols**, not bare identifiers:
121
195
  ```ruby
122
196
  value :status do
123
- on :adult, "Adult Status" # ✅ Correct - use :trait_name symbol
124
- on :verified, "Verified User"
197
+ on adult, "Adult Status" # ✅ Correct - use trait_name symbol
198
+ on verified, "Verified User"
125
199
  base "Unverified"
126
200
  end
127
201
 
@@ -170,6 +244,7 @@ end
170
244
  The `examples/` directory contains comprehensive examples showing Kumi usage patterns:
171
245
  - `cascade_demonstration.rb` - Demonstrates cascade logic with UnsatDetector fixes (working)
172
246
  - `working_comprehensive_schema.rb` - Complete feature showcase (current best practices, working)
247
+ - Mathematical predicate examples - Safe mutual recursion patterns using cascade mutual exclusion
173
248
  - `federal_tax_calculator_2024.rb` - Real-world tax calculation example (working)
174
249
  - `tax_2024.rb` - Simple tax example with explain functionality (working)
175
250
  - `wide_schema_compilation_and_evaluation_benchmark.rb` - Performance benchmark for wide schemas (compilation and evaluation scaling)
@@ -196,9 +271,53 @@ The `examples/` directory contains comprehensive examples showing Kumi usage pat
196
271
  7. `lib/kumi/analyzer/passes/type_checker.rb` - Type validation with enhanced error messages
197
272
  8. `spec/kumi/input_block_spec.rb` - Input block syntax and behavior
198
273
  9. `spec/integration/compiler_integration_spec.rb` - End-to-end test examples
199
- 10. `documents/DSL.md` - Concise DSL syntax reference
200
- 11. `documents/AST.md` - AST node types and structure reference
201
- 12. `documents/SYNTAX.md` - Comprehensive sugar vs sugar-free syntax comparison with examples
274
+ 10. `docs/DSL.md` - Concise DSL syntax reference
275
+ 11. `docs/AST.md` - AST node types and structure reference
276
+ 12. `docs/SYNTAX.md` - Comprehensive sugar vs sugar-free syntax comparison with examples
277
+ 13. `lib/kumi/cli.rb` - CLI implementation with REPL and file execution
278
+ 14. `examples/simple_tax_schema.rb` - CLI-compatible schema example
279
+ 15. `docs/features/analysis-cascade-mutual-exclusion.md` - Cascade mutual exclusion detection feature documentation
280
+ 16. `docs/features/array-broadcasting.md` - Array broadcasting and vectorization system documentation
281
+
282
+ ## CLI Usage and Best Practices
283
+
284
+ ### Schema File Requirements
285
+ - **Must use module structure**: `module SchemaName; extend Kumi::Schema; schema do ... end; end`
286
+ - **Must have proper require**: `require_relative "../lib/kumi"` at the top
287
+ - **Must have input block**: Even empty `input {}` blocks are required
288
+ - **Avoid inline definitions**: Don't define schemas directly in methods or blocks
289
+
290
+ ### CLI Development Workflow
291
+ 1. **Start with REPL**: Use `./bin/kumi -i` for rapid prototyping and testing
292
+ 2. **Test with files**: Create `.rb` schema files and `.json/.yaml` data files
293
+ 3. **Iterate quickly**: Use `-k key1,key2` to focus on specific outputs
294
+ 4. **Debug with explain**: Use `-e key_name` to understand computation flow
295
+ 5. **Validate with different data**: Test edge cases with varied input data
296
+
297
+ ### Common CLI Patterns
298
+ ```bash
299
+ # Interactive development
300
+ ./bin/kumi -i
301
+ kumi> schema examples/my_schema.rb
302
+ kumi> data test_data.json
303
+ kumi> get result
304
+
305
+ # File-based execution
306
+ ./bin/kumi -f examples/tax_schema.rb -d examples/tax_data.json -k total_tax,effective_rate
307
+
308
+ # Debugging computations
309
+ ./bin/kumi -f examples/complex_schema.rb -d examples/data.json -e complex_calculation
310
+
311
+ # Output formats for integration
312
+ ./bin/kumi -f schema.rb -d data.json -k results -o json | jq '.results'
313
+ ```
314
+
315
+ ### Troubleshooting Schema Issues
316
+ - **Parse Errors**: Check function syntax (avoid empty `fn()` calls)
317
+ - **Module Not Found**: Ensure proper module structure and naming
318
+ - **UnsatDetector Errors**: Review trait logic for contradictions
319
+ - **Type Errors**: Check input block type declarations match usage
320
+ - **Runtime Errors**: Use explain to trace computation dependencies
202
321
 
203
322
  ## Input Block System Details
204
323
 
@@ -239,6 +358,39 @@ input do
239
358
  end
240
359
  ```
241
360
 
361
+ ### Array Broadcasting System
362
+
363
+ **Automatic Vectorization**: Field access on array inputs (`input.items.price`) applies operations element-wise with intelligent map/reduce detection.
364
+
365
+ **Basic Broadcasting**:
366
+ ```ruby
367
+ input do
368
+ array :line_items do
369
+ float :price
370
+ integer :quantity
371
+ string :category
372
+ end
373
+ end
374
+
375
+ # Element-wise computation - broadcasts over each item
376
+ value :subtotals, input.line_items.price * input.line_items.quantity
377
+ trait :is_taxable, (input.line_items.category != "digital")
378
+ ```
379
+
380
+ **Aggregation Operations**: Functions consuming arrays automatically detected:
381
+ ```ruby
382
+ value :total_subtotal, fn(:sum, subtotals)
383
+ value :avg_price, fn(:avg, input.line_items.price)
384
+ value :max_quantity, fn(:max, input.line_items.quantity)
385
+ ```
386
+
387
+ **Implementation Components**:
388
+ - **InputElementReference** AST nodes for nested field access paths
389
+ - **BroadcastDetector** analyzer pass identifies vectorized vs scalar operations
390
+ - **Compiler** generates appropriate map/reduce functions based on usage context
391
+ - **Type Inference** automatically infers types for array element operations
392
+ - Supports arbitrary depth field access with nested arrays and hashes
393
+
242
394
  ### Trait Syntax Evolution
243
395
 
244
396
  **Current Syntax** (recommended):