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.
- checksums.yaml +4 -4
- data/CLAUDE.md +160 -8
- data/README.md +278 -200
- data/{documents → docs}/AST.md +29 -29
- data/{documents → docs}/DSL.md +3 -3
- data/{documents → docs}/SYNTAX.md +107 -24
- data/docs/features/README.md +45 -0
- data/docs/features/analysis-cascade-mutual-exclusion.md +89 -0
- data/docs/features/analysis-type-inference.md +42 -0
- data/docs/features/analysis-unsat-detection.md +71 -0
- data/docs/features/array-broadcasting.md +170 -0
- data/docs/features/input-declaration-system.md +42 -0
- data/docs/features/performance.md +16 -0
- data/examples/federal_tax_calculator_2024.rb +43 -40
- data/examples/game_of_life.rb +97 -0
- data/examples/simple_rpg_game.rb +1000 -0
- data/examples/static_analysis_errors.rb +178 -0
- data/examples/wide_schema_compilation_and_evaluation_benchmark.rb +1 -1
- data/lib/kumi/analyzer/analysis_state.rb +37 -0
- data/lib/kumi/analyzer/constant_evaluator.rb +22 -16
- data/lib/kumi/analyzer/passes/broadcast_detector.rb +251 -0
- data/lib/kumi/analyzer/passes/{definition_validator.rb → declaration_validator.rb} +8 -7
- data/lib/kumi/analyzer/passes/dependency_resolver.rb +106 -26
- data/lib/kumi/analyzer/passes/input_collector.rb +105 -23
- data/lib/kumi/analyzer/passes/name_indexer.rb +2 -2
- data/lib/kumi/analyzer/passes/pass_base.rb +11 -28
- data/lib/kumi/analyzer/passes/semantic_constraint_validator.rb +110 -0
- data/lib/kumi/analyzer/passes/toposorter.rb +45 -9
- data/lib/kumi/analyzer/passes/type_checker.rb +34 -11
- data/lib/kumi/analyzer/passes/type_consistency_checker.rb +2 -1
- data/lib/kumi/analyzer/passes/type_inferencer.rb +128 -21
- data/lib/kumi/analyzer/passes/unsat_detector.rb +312 -13
- data/lib/kumi/analyzer/passes/visitor_pass.rb +4 -3
- data/lib/kumi/analyzer.rb +41 -24
- data/lib/kumi/atom_unsat_solver.rb +45 -0
- data/lib/kumi/cli.rb +449 -0
- data/lib/kumi/compiler.rb +194 -16
- data/lib/kumi/constraint_relationship_solver.rb +638 -0
- data/lib/kumi/domain/validator.rb +0 -4
- data/lib/kumi/error_reporter.rb +6 -6
- data/lib/kumi/evaluation_wrapper.rb +20 -4
- data/lib/kumi/explain.rb +28 -28
- data/lib/kumi/export/node_registry.rb +26 -12
- data/lib/kumi/export/node_serializers.rb +1 -1
- data/lib/kumi/function_registry/collection_functions.rb +117 -9
- data/lib/kumi/function_registry/function_builder.rb +4 -3
- data/lib/kumi/function_registry.rb +8 -2
- data/lib/kumi/input/type_matcher.rb +3 -0
- data/lib/kumi/input/validator.rb +0 -3
- data/lib/kumi/parser/declaration_reference_proxy.rb +36 -0
- data/lib/kumi/parser/dsl_cascade_builder.rb +19 -8
- data/lib/kumi/parser/expression_converter.rb +80 -12
- data/lib/kumi/parser/input_builder.rb +40 -9
- data/lib/kumi/parser/input_field_proxy.rb +46 -0
- data/lib/kumi/parser/input_proxy.rb +3 -3
- data/lib/kumi/parser/nested_input.rb +15 -0
- data/lib/kumi/parser/parser.rb +2 -0
- data/lib/kumi/parser/schema_builder.rb +10 -9
- data/lib/kumi/parser/sugar.rb +171 -18
- data/lib/kumi/schema.rb +3 -1
- data/lib/kumi/schema_instance.rb +69 -3
- data/lib/kumi/syntax/array_expression.rb +15 -0
- data/lib/kumi/syntax/call_expression.rb +11 -0
- data/lib/kumi/syntax/cascade_expression.rb +11 -0
- data/lib/kumi/syntax/case_expression.rb +11 -0
- data/lib/kumi/syntax/declaration_reference.rb +11 -0
- data/lib/kumi/syntax/hash_expression.rb +11 -0
- data/lib/kumi/syntax/input_declaration.rb +12 -0
- data/lib/kumi/syntax/input_element_reference.rb +12 -0
- data/lib/kumi/syntax/input_reference.rb +12 -0
- data/lib/kumi/syntax/literal.rb +11 -0
- data/lib/kumi/syntax/root.rb +1 -0
- data/lib/kumi/syntax/trait_declaration.rb +11 -0
- data/lib/kumi/syntax/value_declaration.rb +11 -0
- data/lib/kumi/types/compatibility.rb +8 -0
- data/lib/kumi/types/validator.rb +1 -1
- data/lib/kumi/vectorization_metadata.rb +108 -0
- data/lib/kumi/version.rb +1 -1
- data/scripts/generate_function_docs.rb +22 -10
- metadata +38 -17
- data/CHANGELOG.md +0 -25
- data/lib/kumi/domain.rb +0 -8
- data/lib/kumi/input.rb +0 -8
- data/lib/kumi/syntax/declarations.rb +0 -23
- data/lib/kumi/syntax/expressions.rb +0 -30
- data/lib/kumi/syntax/terminal_expressions.rb +0 -27
- data/lib/kumi/syntax.rb +0 -9
- data/test_impossible_cascade.rb +0 -51
- /data/{documents → docs}/FUNCTIONS.md +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 91e6e3b6ce1e4911d37588ba8aaf9b89f8129d23fe93b05268756c417249da90
|
4
|
+
data.tar.gz: f019de05a9e05957184fc649d187306fa7cd3736cb9d04c56d6080d4a3cde9de
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
- `
|
51
|
-
- `
|
52
|
-
- `
|
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
|
124
|
-
on
|
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. `
|
200
|
-
11. `
|
201
|
-
12. `
|
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):
|