tensorlogic-cli 0.1.0-alpha.2

TensorLogic command-line interface and library for compiling logical expressions to tensor graphs
Documentation
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
# TensorLogic CLI Tutorial

**Complete guide to using TensorLogic CLI for logic-to-tensor compilation**

---

## Table of Contents

1. [Introduction]#introduction
2. [Installation]#installation
3. [Quick Start]#quick-start
4. [Core Concepts]#core-concepts
5. [Basic Usage]#basic-usage
6. [Advanced Features]#advanced-features
7. [Workflows]#workflows
8. [Best Practices]#best-practices
9. [Troubleshooting]#troubleshooting
10. [Reference]#reference

---

## Introduction

### What is TensorLogic CLI?

TensorLogic CLI is a command-line tool and library for compiling logical expressions (predicates, quantifiers, logical operators) into executable tensor computation graphs (einsum operations). It bridges symbolic logic and numerical tensor computation, enabling:

- **Neural-symbolic AI**: Train neural networks with logical constraints
- **Differentiable reasoning**: Backpropagate through logical rules
- **Knowledge graph queries**: Execute graph queries as tensor operations
- **Constraint satisfaction**: Express constraints as differentiable functions

### Why Use TensorLogic?

**Traditional Approach:**
```
Logic Rules → Symbolic Solver → Boolean Results
```
- Not differentiable
- Hard to integrate with ML pipelines
- Discrete outputs only

**TensorLogic Approach:**
```
Logic Rules → Tensor Graph → Continuous/Discrete Outputs
```
- Fully differentiable
- Native tensor operations (GPU-ready)
- Supports fuzzy logic, probabilities, and Boolean logic
- Integrates seamlessly with PyTorch, TensorFlow, JAX

---

## Installation

### From crates.io (Recommended)

```bash
cargo install tensorlogic-cli
```

### From Source

```bash
git clone https://github.com/cool-japan/tensorlogic.git
cd tensorlogic
cargo install --path crates/tensorlogic-cli
```

### Verify Installation

```bash
tensorlogic --version
tensorlogic --help
```

---

## Quick Start

### Your First Compilation

```bash
# Compile a simple predicate
tensorlogic "knows(alice, bob)"
```

**Output:**
```
✓ Compilation successful
  Graph: 1 tensors, 0 nodes, 0 inputs, 1 outputs

EinsumGraph {
    tensors: ["knows[ab]"],
    nodes: [],
    inputs: [],
    outputs: [0],
}
```

### Logical Operations

```bash
# AND operation
tensorlogic "knows(x, y) AND likes(y, z)"

# OR operation
tensorlogic "person(x) OR robot(x)"

# NOT operation
tensorlogic "NOT mortal(x)"

# Implication
tensorlogic "knows(x, y) -> likes(x, y)"
```

### With Analysis

```bash
tensorlogic "knows(x, y) AND likes(y, z)" --analyze
```

**Output:**
```
Graph Analysis Metrics:
  Tensors: 3
  Nodes: 1
  Depth: 1
  Estimated FLOPs: 20,000
  Estimated Memory: 80,000 bytes
```

---

## Core Concepts

### 1. Logical Expressions

TensorLogic supports standard first-order logic:

```
Predicates:    pred(x, y)
Operators:     AND, OR, NOT, IMPLIES
Quantifiers:   EXISTS x IN Domain. expr
               FORALL x IN Domain. expr
Arithmetic:    x + y, x * y, x / y
Comparisons:   x = y, x < y, x > y, x <= y, x >= y
Conditionals:  IF cond THEN x ELSE y
```

### 2. Compilation Strategies

Six strategies map logic to tensor operations:

| Strategy | Use Case | AND | OR | NOT |
|----------|----------|-----|-----|-----|
| **soft_differentiable** | Neural training | `a * b` | `a + b - ab` | `1 - a` |
| **hard_boolean** | Discrete logic | `min(a,b)` | `max(a,b)` | `1 - a` |
| **fuzzy_godel** | Gödel fuzzy logic | `min(a,b)` | `max(a,b)` | `1 - a` |
| **fuzzy_product** | Product fuzzy logic | `a * b` | `a + b - ab` | `1 - a` |
| **fuzzy_lukasiewicz** | Łukasiewicz logic | `max(0,a+b-1)` | `min(1,a+b)` | `1 - a` |
| **probabilistic** | Probabilities | `a * b` | `a + b - ab` | `1 - a` |

### 3. Domains

Domains define the universe of discourse for variables:

```bash
--domains Person:100    # Person domain with 100 individuals
--domains City:50       # City domain with 50 cities
```

Default domain `D` is created automatically if not specified.

### 4. Einsum Graphs

Output format showing tensor computations:

```
EinsumGraph {
    tensors: [list of tensors],
    nodes: [computation nodes],
    inputs: [input indices],
    outputs: [output indices]
}
```

---

## Basic Usage

### Example 1: Social Network

```bash
tensorlogic "EXISTS x. (knows(alice, x) AND knows(x, bob))" \
  --domains Person:100 \
  --strategy soft_differentiable \
  --validate \
  --analyze
```

**Meaning**: Does Alice know someone who knows Bob? (Friend of friend)

### Example 2: Access Control

```bash
tensorlogic "admin(user) OR (owns(user, resource) AND NOT locked(resource))" \
  --domains User:1000 \
  --domains Resource:5000 \
  --strategy hard_boolean \
  --output-format json \
  --output access_policy.json
```

**Meaning**: Access granted if user is admin OR (owns resource AND resource not locked)

### Example 3: Recommendation

```bash
tensorlogic "likes(user, item) OR EXISTS x. (likes(user, x) AND similar(x, item))" \
  --domains User:10000 \
  --domains Item:50000 \
  --strategy probabilistic \
  --output-format stats
```

**Meaning**: Recommend items user likes OR similar to items they like

---

## Advanced Features

### Interactive REPL

```bash
tensorlogic repl
```

```
TensorLogic Interactive REPL
Type '.help' for commands, '.exit' to quit

tensorlogic> .domain Person 100
✓ Added domain 'Person' with size 100

tensorlogic> .strategy fuzzy_godel
✓ Strategy set to: fuzzy_godel

tensorlogic> EXISTS x IN Person. knows(x, alice)
✓ Compilation successful
  Graph: 2 tensors, 2 nodes, depth 2

tensorlogic> .history
   1: .domain Person 100
   2: .strategy fuzzy_godel
   3: EXISTS x IN Person. knows(x, alice)

tensorlogic> .exit
```

### Batch Processing

Create `rules.txt`:
```
knows(alice, bob)
likes(bob, charlie)
friend(alice, charlie)
# Transitive friendship
EXISTS x. (friend(alice, x) AND friend(x, charlie))
```

Process all rules:
```bash
tensorlogic batch rules.txt
```

### Watch Mode

Auto-recompile on file changes:

```bash
tensorlogic watch my_rules.tl
```

### Visualization

Generate Graphviz diagrams:

```bash
tensorlogic "knows(x, y) AND likes(y, z)" \
  --output-format dot > graph.dot

dot -Tpng graph.dot -o graph.png
dot -Tsvg graph.dot -o graph.svg
```

### Optimization

Optimize compiled graphs:

```bash
tensorlogic optimize "complex_expression(x, y, z)" \
  --input-format expr \
  --level aggressive \
  --stats \
  --verbose
```

Optimization levels:
- **none**: No optimization
- **basic**: Dead code elimination
- **standard**: DCE + identity simplification
- **aggressive**: All passes + reordering

### Benchmarking

Measure compilation performance:

```bash
tensorlogic benchmark "knows(x, y) AND likes(y, z)" \
  --iterations 100 \
  --execute \
  --backend scirs2-cpu \
  --json > benchmark_results.json
```

### Profiling

Detailed performance breakdown:

```bash
tensorlogic profile "complex_rule(x, y)" \
  --warmup 5 \
  --runs 20 \
  --validate \
  --json
```

---

## Workflows

### Workflow 1: Development Cycle

```bash
# 1. Create rule file
cat > policy.tl << 'EOF'
FORALL u IN User. (
  admin(u) OR (
    verified(u) AND NOT suspended(u)
  )
)
EOF

# 2. Watch for changes
tensorlogic watch policy.tl &

# 3. Edit policy.tl in your editor
# 4. See live compilation results
# 5. When satisfied, validate
tensorlogic policy.tl --validate --analyze

# 6. Export for use
tensorlogic policy.tl --output-format json --output policy.json
```

### Workflow 2: Performance Tuning

```bash
# 1. Baseline benchmark
tensorlogic benchmark my_rule.tl --iterations 100 > baseline.txt

# 2. Compare strategies
for strategy in soft_differentiable hard_boolean fuzzy_godel; do
  echo "=== $strategy ==="
  tensorlogic my_rule.tl --strategy $strategy --output-format stats
done

# 3. Profile bottlenecks
tensorlogic profile my_rule.tl --verbose

# 4. Optimize
tensorlogic optimize my_rule.tl --level aggressive --stats
```

### Workflow 3: Integration Testing

```bash
# test_rules.sh
#!/bin/bash

RULES_DIR="rules/"
TESTS_PASSED=0
TESTS_FAILED=0

for rule_file in "$RULES_DIR"/*.tl; do
  echo "Testing $rule_file..."

  if tensorlogic "$rule_file" --validate --quiet; then
    echo "✓ PASS: $rule_file"
    ((TESTS_PASSED++))
  else
    echo "✗ FAIL: $rule_file"
    ((TESTS_FAILED++))
  fi
done

echo ""
echo "Results: $TESTS_PASSED passed, $TESTS_FAILED failed"
[ $TESTS_FAILED -eq 0 ]
```

### Workflow 4: Documentation Generation

```bash
# Generate visual documentation for all rules
mkdir -p docs/graphs

for rule in rules/*.tl; do
  basename="${rule%.tl}"

  # Generate DOT
  tensorlogic "$rule" --output-format dot > "docs/graphs/${basename}.dot"

  # Generate PNG
  dot -Tpng "docs/graphs/${basename}.dot" -o "docs/graphs/${basename}.png"

  # Generate stats
  tensorlogic "$rule" --output-format stats > "docs/graphs/${basename}_stats.txt"
done
```

---

## Best Practices

### 1. Start Simple

```bash
# ✓ Good: Start with simple expressions
tensorlogic "knows(x, y)"
tensorlogic "knows(x, y) AND likes(y, z)"

# ✗ Avoid: Complex expressions from the start
tensorlogic "FORALL x. EXISTS y. ((a(x,y) AND b(y,z)) OR (c(x) AND d(z)))"
```

### 2. Use Appropriate Strategies

```bash
# Neural training: soft_differentiable
tensorlogic rule.tl --strategy soft_differentiable

# Boolean logic: hard_boolean
tensorlogic rule.tl --strategy hard_boolean

# Probabilities: probabilistic
tensorlogic rule.tl --strategy probabilistic
```

### 3. Always Validate During Development

```bash
tensorlogic rule.tl --validate --analyze
```

### 4. Use Domains Explicitly

```bash
# ✓ Good: Explicit domains
tensorlogic "EXISTS x IN Person. knows(x, alice)" \
  --domains Person:100

# ✗ Avoid: Relying on default domain
tensorlogic "EXISTS x. knows(x, alice)"
```

### 5. Profile Before Optimizing

```bash
# 1. Profile to find bottlenecks
tensorlogic profile rule.tl --verbose

# 2. Then optimize
tensorlogic optimize rule.tl --level aggressive
```

### 6. Use Quiet Mode in Scripts

```bash
#!/bin/bash
if tensorlogic rule.tl --validate --quiet; then
  echo "Validation passed"
else
  echo "Validation failed"
  exit 1
fi
```

---

## Troubleshooting

### Issue: "Compilation failed"

**Cause**: Syntax error in expression

**Solution**:
```bash
# Use debug mode to see details
tensorlogic "your expression" --debug
```

### Issue: "Validation failed: Free variable"

**Cause**: Unbound variable in expression

**Solution**:
```bash
# Add quantifier or domain
tensorlogic "EXISTS x IN Domain. your_expr" --domains Domain:100
```

### Issue: "Unknown compilation strategy"

**Cause**: Typo in strategy name

**Solution**:
```bash
# List valid strategies
tensorlogic --help | grep strategy

# Use correct name
tensorlogic rule.tl --strategy soft_differentiable
```

### Issue: Performance is slow

**Solutions**:
```bash
# 1. Reduce domain sizes during development
tensorlogic rule.tl --domains Person:10  # Instead of 1000

# 2. Use optimization
tensorlogic optimize rule.tl --level aggressive

# 3. Profile to find bottlenecks
tensorlogic profile rule.tl --verbose

# 4. Try different strategies
tensorlogic rule.tl --strategy hard_boolean  # Often faster than soft
```

### Issue: Out of memory

**Solutions**:
```bash
# 1. Reduce domain sizes
--domains User:100  # Instead of 10000

# 2. Simplify expression
# Break complex rules into smaller pieces

# 3. Use streaming/batch processing
tensorlogic batch rules.txt
```

---

## Reference

### Command Quick Reference

```bash
# Basic compilation
tensorlogic "expression" [OPTIONS]

# Subcommands
tensorlogic repl                      # Interactive mode
tensorlogic batch FILES...            # Batch processing
tensorlogic watch FILE                # Watch mode
tensorlogic completion SHELL          # Shell completion
tensorlogic config show|path|init     # Configuration
tensorlogic convert FILE              # Format conversion
tensorlogic execute EXPR              # Execute with backend
tensorlogic optimize EXPR             # Optimize graph
tensorlogic backends                  # List backends
tensorlogic benchmark EXPR            # Benchmark
tensorlogic profile EXPR              # Profile
tensorlogic cache stats|clear         # Cache management
```

### Common Options

```
-f, --input-format FORMAT    # expr, json, yaml
-o, --output FILE           # Output file
-F, --output-format FORMAT  # graph, dot, json, stats
-s, --strategy STRATEGY     # Compilation strategy
-d, --domains NAME:SIZE     # Domain definition
--validate                  # Enable validation
--debug                     # Debug output
-a, --analyze              # Show metrics
-q, --quiet                # Quiet mode
--no-color                 # Disable colors
```

### Configuration File

Location: `~/.tensorlogicrc` or `./.tensorlogicrc`

```toml
# Default compilation strategy
strategy = "soft_differentiable"

# Enable colored output
colored = true

# Enable validation by default
validate = false

# Default domains
[domains]
Person = 100
City = 50

# REPL settings
[repl]
prompt = "tensorlogic> "
history_file = ".tensorlogic_history"
max_history = 1000

# Watch mode settings
[watch]
debounce_ms = 500
clear_screen = true
show_timestamps = true
```

Initialize configuration:
```bash
tensorlogic config init
tensorlogic config edit
```

---

## Next Steps

1. **Try the examples**: Explore `examples/` directory
2. **Read the cookbook**: See [COOKBOOK.md]COOKBOOK.md for recipes
3. **API documentation**: `cargo doc --open -p tensorlogic-cli`
4. **Join the community**: [GitHub Discussions]https://github.com/cool-japan/tensorlogic/discussions

---

**Happy Compiling!** 🚀