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
|
# Next Steps: Immutable Real-World Programming Features
## Overview
This document outlines the plan for extending the Simple Scripting Language to support real-world programming scenarios while maintaining its immutable, functional design philosophy.
## Core Principles
- **Immutability First**: All data structures are immutable
- **Transformation Over Mutation**: Operations return new data rather than modifying existing data
- **Functional Composition**: Complex operations built from simple, composable functions
- **Type Safety**: Enhanced pattern matching and type checking
- **Performance**: Efficient persistent data structures
## Phase 1: String Operations and Type System
### 1.1 String Operations
**Goal**: Add essential string manipulation capabilities
**New Functions**:
```javascript
// String operations as functions
length : string.length "hello"; // 5
contains : string.contains "hello" "ll"; // true
startsWith : string.startsWith "hello" "he"; // true
endsWith : string.endsWith "hello" "lo"; // true
substring : string.substring "hello" 1 3; // "el"
// String concatenation
result : string.concat "Hello" " " "World"; // "Hello World"
// String transformation
uppercase : string.upper "hello"; // "HELLO"
lowercase : string.lower "HELLO"; // "hello"
trimmed : string.trim " hello "; // "hello"
```
**Implementation**:
- Add `string.` namespace for string operations
- Add string operation functions to standard library
- No new syntax - uses existing function call patterns
- Extend existing string literal support
### 1.2 Runtime Type Checking with `is` Keyword
**Goal**: Add explicit, optional type checking mechanism using the `is` keyword
**Design Philosophy**:
- Type checking is purely additive - no breaking changes to existing code
- Works seamlessly with existing case expression syntax
- Returns boolean values, fitting the functional style
- Simple implementation with clear semantics
**New Features**:
```javascript
// Basic type checking
isNumber : x -> x is number;
isString : x -> x is string;
isTable : x -> x is table;
isFunction : x -> x is function;
// Type-safe operations in case expressions
safeStringOp : x -> case x of
x is string : string.length x
_ : "not a string";
// Complex type validation
validateUser : user -> case user of
user.name is string and user.age is number : true
_ : false;
// Type guards in pattern matching
processData : data -> case data of
data is table : "processing table"
data is string : "processing string"
data is number : "processing number"
_ : "unknown type";
```
**Supported Types**:
- `number` (both integers and floats)
- `string`
- `boolean`
- `table` (objects and arrays)
- `function`
**Implementation**:
- Add `IS` token type and type-specific tokens (`NUMBER_TYPE`, `STRING_TYPE`, etc.)
- Update lexer to recognize `is` keyword and type names
- Extend parser to handle type checking expressions in case patterns
- Add type checking logic in interpreter that returns boolean values
- No changes to existing syntax or semantics
## Phase 2: Persistent Data Structures
### 2.1 Immutable Tables with Transformations
**Goal**: Replace mutable table operations with immutable transformations
**Current Problem**:
```javascript
// This won't work (mutable)
cache[key] = value;
```
**Solution - Functional Immutable Transformations**:
```javascript
// Functional transformations using table namespace
cache : {};
cache1 : table.set cache "user.1" "Alice";
cache2 : table.set cache1 "user.2" "Bob";
value : table.get cache2 "user.1"; // "Alice"
cache3 : table.delete cache2 "user.1";
// Nested updates with dot notation
user : {name: "Alice", profile: {age: 25}};
updatedUser : table.set user "profile.age" 26;
// Complex transformations using composition
cache1 : pipe
(table.set "user.1" "Alice")
(table.set "user.2" "Bob")
(table.set "user.3" "Charlie")
cache;
// Table merging
combined : table.merge table1 table2;
```
**Implementation**:
- Add `table.set`, `table.get`, `table.delete`, `table.merge` functions
- Implement efficient structural sharing for immutable updates
- Add nested key support (e.g., "profile.age")
- All functions return new tables, never modify originals
- Use existing function composition patterns
### 2.2 APL-Inspired Table Primitives
**Goal**: Enhance tables with APL-style operations for ergonomic data manipulation
**Design Philosophy**:
- Use existing table syntax for array-like behavior
- Add APL-inspired primitives for vectorized operations
- Keep immutable transformations
- Provide concise, expressive data manipulation
**New Features**:
```javascript
// Table creation (array-like using existing syntax)
numbers : {1, 2, 3, 4, 5};
mixed : {1, "hello", true, {key: "value"}};
// All table operations namespaced for consistency
first : table.first numbers; // First element
last : table.last numbers; // Last element
length : table.length numbers; // Size/shape
// Less common operations (namespaced)
rest : table.drop 1 numbers; // Drop first element
slice : table.slice numbers 2 3; // Elements 2-3
// Vectorized operations
doubled : table.add numbers numbers; // Element-wise addition
squared : table.multiply numbers numbers; // Element-wise multiplication
incremented : table.add numbers 1; // Scalar addition to each element
// Reductions (all namespaced)
sum : table.sum numbers; // Sum reduction
max : table.max numbers; // Maximum reduction
min : table.min numbers; // Minimum reduction
product : table.product numbers; // Product reduction
average : table.average numbers; // Average reduction
// Scan operations (namespaced)
runningSum : table.scan table.sum numbers; // Running sum scan
runningProduct : table.scan table.product numbers; // Running product scan
// Table metadata operations
keys : table.keys table; // Get all keys
values : table.values table; // Get all values
reversed : table.reverse table; // Reverse order
sorted : table.sort table; // Sort
unique : table.unique table; // Remove duplicates
```
**Implementation**:
- Add `table.` namespace for all table operations
- Extend lexer to recognize table operation keywords
- Add vectorized operation logic in interpreter
- Implement reduction and scan operations
- Add table metadata operations
## Phase 3: Higher-Order Functions for Collections
### 3.1 Enhanced Standard Library
**Goal**: Add collection processing functions
**New Functions**:
```javascript
// Table processing (using namespaced primitives)
numbers : {1, 2, 3, 4, 5};
doubled : map @double numbers; // {2, 4, 6, 8, 10}
evens : filter @isEven numbers; // {2, 4}
sum : table.sum numbers; // 15 (sum reduction)
// Table metadata operations
table : {a: 1, b: 2, c: 3};
keys : table.keys table; // {a, b, c}
values : table.values table; // {1, 2, 3}
pairs : table.pairs table; // {{a, 1}, {b, 2}, {c, 3}}
// Advanced operations with tables
nested : {{1, 2}, {3, 4}, {5}};
flattened : table.flatten nested; // {1, 2, 3, 4, 5}
grouped : table.groupBy @isEven numbers; // {true: {2, 4}, false: {1, 3, 5}}
// Table operations
reversed : table.reverse numbers; // {5, 4, 3, 2, 1}
sorted : table.sort numbers; // {1, 2, 3, 4, 5}
unique : table.unique {1, 2, 2, 3, 3, 4}; // {1, 2, 3, 4}
```
**Implementation**:
- Extend existing `map`, `filter`, `reduce` to work with tables
- Implement vectorized operations for tables
- Add reduction and scan operations
- Implement table metadata operations
### 3.2 Table Generation Helpers
**Goal**: Add convenient table creation functions
**New Functions**:
```javascript
// Table generation helpers
range : table.range 1 5; // {1, 2, 3, 4, 5}
repeated : table.repeat "hello" 3; // {"hello", "hello", "hello"}
// Use existing map/filter instead of comprehensions
squares : map (x -> x * x) {1, 2, 3, 4, 5};
evens : filter @isEven {1, 2, 3, 4, 5};
```
## Phase 4: Error Handling with Error Type
### 4.1 Error Type
**Goal**: Provide consistent error handling without complex monads
**Implementation**:
```javascript
// Simple error type
error : message -> {type: "error", message: message};
// Safe operations with error handling
safeDivide : x y -> case y of
0 : error "division by zero"
_ : x / y;
safeParseNumber : str -> case str of
str is number : str
_ : error "invalid number";
// Error checking
isError : value -> value is error;
getErrorMessage : error -> case error of
{type: "error", message: m} : m;
```
## Phase 5: Real-World Scenario Support
### 5.1 User Management System
**Immutable Implementation**:
```javascript
// User validation
isValidEmail : email -> case email of
email contains "@" : true
_ : false;
createUser : name email age -> case (isValidEmail email) and (isValidAge age) of
true : {name: name, email: email, age: age, status: "active"}
false : error "invalid user data";
// User management
users : {};
user1 : createUser "Alice" "alice@example.com" 25;
users1 : table.set users "user.1" user1;
user2 : createUser "Bob" "bob@example.com" 30;
users2 : table.set users1 "user.2" user2;
// Safe user lookup
findUser : email users -> case users of
{} : error "user not found"
_ : case (table.first users).email = email of
true : table.first users
false : findUser email (table.drop 1 users);
```
### 5.2 Shopping Cart System
**Immutable Implementation**:
```javascript
// Cart operations
emptyCart : {items: {}, total: 0};
addItem : cart item -> {
items: table.set cart.items (table.length cart.items + 1) item,
total: cart.total + item.price
};
removeItem : cart itemId -> {
items: filter (item -> item.id != itemId) cart.items,
total: calculateTotal (filter (item -> item.id != itemId) cart.items)
};
// Discount application
applyDiscount : cart discountPercent -> {
items: cart.items,
total: cart.total * (1 - discountPercent / 100)
};
```
### 5.3 Data Processing Pipeline
**Immutable Implementation**:
```javascript
// Data processing
salesData : {
{month: "Jan", sales: 1000, region: "North"},
{month: "Feb", sales: 1200, region: "North"},
{month: "Mar", sales: 800, region: "South"}
};
// Filter by region
filterByRegion : data region -> filter (item -> item.region = region) data;
// Calculate totals using sum reduction
sumSales : data -> table.sum (map (item -> item.sales) data);
// Process pipeline
northData : filterByRegion salesData "North";
northTotal : sumSales northData;
```
## Implementation Timeline
### Week 1-2: String Operations and Runtime Type Checking
- [ ] String concatenation operator
- [ ] String method implementations
- [ ] `is` keyword and type checking tokens
- [ ] Type checking in case expressions
- [ ] Type validation functions in standard library
### Week 3-4: Table Primitives with Namespacing
- [ ] `table.` namespace for all table operations
- [ ] Vectorized operations for tables
- [ ] Reduction and scan operations
- [ ] Table metadata operations
- [ ] Performance optimization
### Week 5-6: Higher-Order Functions
- [ ] Enhanced standard library
- [ ] Collection processing functions
- [ ] Table-specific operations
- [ ] Utility functions
### Week 7-8: Error Handling
- [ ] Error type implementation
- [ ] Error handling patterns
- [ ] Error checking functions
- [ ] Integration with existing operations
### Week 9-10: Real-World Scenarios
- [ ] User management system
- [ ] Shopping cart system
- [ ] Data processing pipeline
- [ ] Integration testing
## Benefits of Runtime Type Checking Approach
### Simplicity
- **Minimal Implementation**: Only need to add `is` keyword and type checking logic
- **No Breaking Changes**: Existing code continues to work unchanged
- **Clear Semantics**: `x is number` is obviously a boolean expression
- **Consistent Syntax**: Works seamlessly with existing case expressions
### Functional Design
- **Boolean Results**: Type checking returns true/false, fitting functional style
- **Composable**: Can combine with logical operators (`and`, `or`, `not`)
- **Pattern Matching**: Integrates naturally with case expressions
- **No Side Effects**: Pure functions for type validation
### Extensibility
- **Easy to Add Types**: Simple to extend for new types (arrays, tuples, etc.)
- **Custom Types**: Can implement custom type checking via functions
- **Performance**: Runtime overhead only when explicitly used
- **Optional**: No requirement to use type checking in existing code
## Testing Strategy
### Unit Tests
- String operation tests
- Runtime type checking tests (`is` keyword)
- Type validation function tests
- Data structure transformation tests
- Higher-order function tests
- Error handling tests
### Integration Tests
- Real-world scenario tests
- Performance tests
- Edge case tests
- Error handling tests
### Performance Benchmarks
- Data structure operation performance
- Memory usage analysis
- Transformation efficiency
- Scalability testing
## Success Metrics
- [ ] All real-world scenarios in `tests/17_real_world_scenarios.txt` pass
- [ ] Performance within acceptable bounds (no more than 2x slower than current)
- [ ] Memory usage remains reasonable
- [ ] Code remains readable and maintainable
- [ ] Backward compatibility maintained
## Future Considerations
### Advanced Features (Post-v1.0)
- Lazy evaluation for large collections
- Parallel processing capabilities
- Advanced type system with generics
- Macro system for code generation
- Module system for code organization
### Performance Optimizations
- Structural sharing for immutable data structures
- Compile-time optimizations
- JIT compilation for hot paths
- Memory pooling for temporary objects
This plan maintains the language's functional, immutable design while adding the capabilities needed for real-world programming scenarios.
|