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
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
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Combinator Logic Playground</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        .combinator {
            transition: all 0.2s ease;
        }
        .combinator.dragging {
            opacity: 0.5;
            transform: scale(1.1);
        }
        .drop-zone {
            min-height: 100px;
            transition: all 0.2s ease;
        }
        .drop-zone.highlight {
            background-color: rgba(147, 197, 253, 0.3);
            border-color: #3b82f6;
        }
        .expression-item {
            position: relative;
        }
        .expression-item:hover .remove-btn {
            opacity: 1;
        }
        .remove-btn {
            opacity: 0;
            transition: opacity 0.2s;
            position: absolute;
            top: -8px;
            right: -8px;
            width: 20px;
            height: 20px;
            border-radius: 50%;
            background-color: #ef4444;
            color: white;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 10px;
            cursor: pointer;
        }
        @keyframes pulse {
            0%, 100% { transform: scale(1); }
            50% { transform: scale(1.05); }
        }
        .reduction-step {
            animation: pulse 0.5s ease;
        }
    </style>
</head>
<body class="bg-gray-50 min-h-screen">
    <div class="container mx-auto px-4 py-8">
        <header class="mb-8 text-center">
            <h1 class="text-3xl font-bold text-blue-600 mb-2">Combinator Logic Playground</h1>
            <p class="text-gray-600">Drag and drop combinators to form expressions and reduce them</p>
        </header>

        <div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
            <!-- Combinator Palette -->
            <div class="bg-white rounded-lg shadow-md p-4">
                <h2 class="text-xl font-semibold text-gray-800 mb-4 flex items-center">
                    <i class="fas fa-shapes mr-2 text-blue-500"></i> Combinators
                </h2>
                <div class="grid grid-cols-2 gap-3">
                    <div class="combinator bg-blue-100 text-blue-800 py-3 px-4 rounded-lg cursor-move flex items-center justify-center font-mono font-bold"
                         draggable="true" data-type="S">
                        <span>S</span>
                    </div>
                    <div class="combinator bg-green-100 text-green-800 py-3 px-4 rounded-lg cursor-move flex items-center justify-center font-mono font-bold"
                         draggable="true" data-type="K">
                        <span>K</span>
                    </div>
                    <div class="combinator bg-purple-100 text-purple-800 py-3 px-4 rounded-lg cursor-move flex items-center justify-center font-mono font-bold"
                         draggable="true" data-type="I">
                        <span>I</span>
                    </div>
                    <div class="combinator bg-yellow-100 text-yellow-800 py-3 px-4 rounded-lg cursor-move flex items-center justify-center font-mono font-bold"
                         draggable="true" data-type="B">
                        <span>B</span>
                    </div>
                    <div class="combinator bg-red-100 text-red-800 py-3 px-4 rounded-lg cursor-move flex items-center justify-center font-mono font-bold"
                         draggable="true" data-type="C">
                        <span>C</span>
                    </div>
                    <div class="combinator bg-indigo-100 text-indigo-800 py-3 px-4 rounded-lg cursor-move flex items-center justify-center font-mono font-bold"
                         draggable="true" data-type="W">
                        <span>W</span>
                    </div>
                </div>
                <div class="mt-4">
                    <div class="combinator bg-gray-200 text-gray-800 py-2 px-3 rounded-lg cursor-move flex items-center justify-center font-mono text-sm"
                         draggable="true" data-type="variable">
                        <span>Variable (drag to edit)</span>
                    </div>
                </div>
            </div>

            <!-- Workspace -->
            <div class="bg-white rounded-lg shadow-md p-4">
                <h2 class="text-xl font-semibold text-gray-800 mb-4 flex items-center">
                    <i class="fas fa-edit mr-2 text-blue-500"></i> Workspace
                </h2>
                <div id="expression-container" class="drop-zone border-2 border-dashed border-gray-300 rounded-lg p-4 mb-4 min-h-32">
                    <p class="text-gray-400 text-center py-8" id="empty-message">Drag combinators here to build your expression</p>
                    <div class="flex flex-wrap gap-2" id="expression-items"></div>
                </div>
                <div class="flex flex-wrap gap-2">
                    <button id="reduce-btn" class="bg-blue-500 hover:bg-blue-600 text-white px-4 py-2 rounded-lg flex items-center disabled:opacity-50" disabled>
                        <i class="fas fa-play mr-2"></i> Reduce
                    </button>
                    <button id="reset-btn" class="bg-gray-500 hover:bg-gray-600 text-white px-4 py-2 rounded-lg flex items-center">
                        <i class="fas fa-redo mr-2"></i> Reset
                    </button>
                    <button id="add-parens-btn" class="bg-purple-500 hover:bg-purple-600 text-white px-4 py-2 rounded-lg flex items-center">
                        <i class="fas fa-parentheses mr-2"></i> Add Parentheses
                    </button>
                </div>
            </div>

            <!-- Reduction Steps -->
            <div class="bg-white rounded-lg shadow-md p-4">
                <h2 class="text-xl font-semibold text-gray-800 mb-4 flex items-center">
                    <i class="fas fa-list-ol mr-2 text-blue-500"></i> Reduction Steps
                </h2>
                <div id="reduction-steps" class="space-y-2 max-h-96 overflow-y-auto p-2">
                    <p class="text-gray-400 text-center py-8">Your reduction steps will appear here</p>
                </div>
            </div>
        </div>

        <!-- Help Section -->
        <div class="mt-8 bg-white rounded-lg shadow-md p-4">
            <h2 class="text-xl font-semibold text-gray-800 mb-4 flex items-center">
                <i class="fas fa-question-circle mr-2 text-blue-500"></i> Combinator Definitions
            </h2>
            <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
                <div class="bg-blue-50 p-3 rounded-lg">
                    <h3 class="font-mono font-bold text-blue-800">S x y z = x z (y z)</h3>
                    <p class="text-gray-600 text-sm">Substitution combinator</p>
                </div>
                <div class="bg-green-50 p-3 rounded-lg">
                    <h3 class="font-mono font-bold text-green-800">K x y = x</h3>
                    <p class="text-gray-600 text-sm">Constant combinator</p>
                </div>
                <div class="bg-purple-50 p-3 rounded-lg">
                    <h3 class="font-mono font-bold text-purple-800">I x = x</h3>
                    <p class="text-gray-600 text-sm">Identity combinator</p>
                </div>
                <div class="bg-yellow-50 p-3 rounded-lg">
                    <h3 class="font-mono font-bold text-yellow-800">B x y z = x (y z)</h3>
                    <p class="text-gray-600 text-sm">Composition combinator</p>
                </div>
                <div class="bg-red-50 p-3 rounded-lg">
                    <h3 class="font-mono font-bold text-red-800">C x y z = x z y</h3>
                    <p class="text-gray-600 text-sm">Swap combinator</p>
                </div>
                <div class="bg-indigo-50 p-3 rounded-lg">
                    <h3 class="font-mono font-bold text-indigo-800">W x y = x y y</h3>
                    <p class="text-gray-600 text-sm">Duplication combinator</p>
                </div>
            </div>
        </div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            // DOM elements
            const expressionContainer = document.getElementById('expression-container');
            const expressionItems = document.getElementById('expression-items');
            const emptyMessage = document.getElementById('empty-message');
            const reduceBtn = document.getElementById('reduce-btn');
            const resetBtn = document.getElementById('reset-btn');
            const addParensBtn = document.getElementById('add-parens-btn');
            const reductionSteps = document.getElementById('reduction-steps');
            
            // Store the current expression and reduction steps
            let currentExpression = [];
            let steps = [];
            
            // Add drag events to all combinators
            document.querySelectorAll('.combinator').forEach(combinator => {
                combinator.addEventListener('dragstart', dragStart);
            });
            
            // Setup drop zones
            expressionContainer.addEventListener('dragover', dragOver);
            expressionContainer.addEventListener('dragleave', dragLeave);
            expressionContainer.addEventListener('drop', drop);
            
            // Button events
            reduceBtn.addEventListener('click', reduceExpression);
            resetBtn.addEventListener('click', resetWorkspace);
            addParensBtn.addEventListener('click', addParentheses);
            
            // Drag start handler
            function dragStart(e) {
                this.classList.add('dragging');
                e.dataTransfer.setData('text/plain', this.dataset.type);
                e.dataTransfer.effectAllowed = 'move';
            }
            
            // Drag over handler
            function dragOver(e) {
                e.preventDefault();
                this.classList.add('highlight');
                e.dataTransfer.dropEffect = 'move';
            }
            
            // Drag leave handler
            function dragLeave() {
                this.classList.remove('highlight');
            }
            
            // Drop handler
            function drop(e) {
                e.preventDefault();
                this.classList.remove('highlight');
                
                const combinatorType = e.dataTransfer.getData('text/plain');
                addCombinatorToExpression(combinatorType);
            }
            
            // Add combinator to expression
            function addCombinatorToExpression(type, isParens = false) {
                // Hide empty message if this is the first item
                if (currentExpression.length === 0) {
                    emptyMessage.style.display = 'none';
                    reduceBtn.disabled = false;
                }
                
                // Create a unique ID for this combinator
                const id = Date.now().toString(36) + Math.random().toString(36).substr(2);
                
                // Add to current expression
                currentExpression.push({
                    id,
                    type,
                    isParens
                });
                
                // Update the UI
                updateExpressionUI();
            }
            
            // Update the expression UI
            function updateExpressionUI() {
                expressionItems.innerHTML = '';
                
                currentExpression.forEach((item, index) => {
                    const itemEl = document.createElement('div');
                    itemEl.className = 'expression-item';
                    itemEl.dataset.id = item.id;
                    
                    let bgColor, textColor;
                    
                    if (item.isParens) {
                        itemEl.className += ' cursor-default';
                        itemEl.innerHTML = '(';
                    } else {
                        itemEl.className += ' cursor-move';
                        itemEl.draggable = true;
                        itemEl.addEventListener('dragstart', dragStart);
                        
                        switch(item.type) {
                            case 'S':
                                bgColor = 'bg-blue-100';
                                textColor = 'text-blue-800';
                                break;
                            case 'K':
                                bgColor = 'bg-green-100';
                                textColor = 'text-green-800';
                                break;
                            case 'I':
                                bgColor = 'bg-purple-100';
                                textColor = 'text-purple-800';
                                break;
                            case 'B':
                                bgColor = 'bg-yellow-100';
                                textColor = 'text-yellow-800';
                                break;
                            case 'C':
                                bgColor = 'bg-red-100';
                                textColor = 'text-red-800';
                                break;
                            case 'W':
                                bgColor = 'bg-indigo-100';
                                textColor = 'text-indigo-800';
                                break;
                            case 'variable':
                                bgColor = 'bg-gray-200';
                                textColor = 'text-gray-800';
                                break;
                        }
                        
                        itemEl.className += ` ${bgColor} ${textColor} py-2 px-3 rounded-lg font-mono font-bold`;
                        itemEl.textContent = item.type === 'variable' ? 'x' : item.type;
                        
                        // Add remove button
                        const removeBtn = document.createElement('span');
                        removeBtn.className = 'remove-btn';
                        removeBtn.innerHTML = '<i class="fas fa-times"></i>';
                        removeBtn.addEventListener('click', (e) => {
                            e.stopPropagation();
                            removeCombinator(item.id);
                        });
                        itemEl.appendChild(removeBtn);
                    }
                    
                    expressionItems.appendChild(itemEl);
                });
            }
            
            // Remove combinator from expression
            function removeCombinator(id) {
                currentExpression = currentExpression.filter(item => item.id !== id);
                
                if (currentExpression.length === 0) {
                    emptyMessage.style.display = 'block';
                    reduceBtn.disabled = true;
                }
                
                updateExpressionUI();
            }
            
            // Add parentheses to expression
            function addParentheses() {
                if (currentExpression.length === 0) return;
                
                // Add opening parenthesis
                addCombinatorToExpression('(', true);
                
                // Add closing parenthesis at the end
                setTimeout(() => {
                    addCombinatorToExpression(')', true);
                }, 0);
            }
            
            // Reset workspace
            function resetWorkspace() {
                currentExpression = [];
                steps = [];
                reductionSteps.innerHTML = '<p class="text-gray-400 text-center py-8">Your reduction steps will appear here</p>';
                emptyMessage.style.display = 'block';
                reduceBtn.disabled = true;
                updateExpressionUI();
            }
            
            // Reduce expression
            function reduceExpression() {
                if (currentExpression.length === 0) return;
                
                // Clear previous steps
                steps = [];
                reductionSteps.innerHTML = '';
                
                // Get the current expression as a string
                let expr = currentExpression.map(item => {
                    if (item.isParens) {
                        return item.type === '(' ? '(' : ')';
                    }
                    return item.type === 'variable' ? 'x' : item.type;
                }).join(' ');
                
                // Add initial expression to steps
                addReductionStep(expr, 'Initial expression');
                
                // Simple reduction logic (this is a simplified version)
                // In a real implementation, you'd need a proper reduction algorithm
                let reduced = false;
                
                // Try to find reducible expressions
                for (let i = 0; i < expr.length - 2; i++) {
                    const subExpr = expr.substr(i, 3);
                    
                    // Check for K combinator pattern: K x y β†’ x
                    if (subExpr.startsWith('K ') && expr[i+2] !== ' ' && expr[i+3] !== ' ') {
                        const before = expr.substring(0, i);
                        const after = expr.substring(i + 4);
                        expr = before + expr[i+2] + after;
                        addReductionStep(expr, 'Applied K combinator: K x y β†’ x');
                        reduced = true;
                        break;
                    }
                    
                    // Check for I combinator pattern: I x β†’ x
                    if (subExpr.startsWith('I ') && expr[i+2] !== ' ') {
                        const before = expr.substring(0, i);
                        const after = expr.substring(i + 3);
                        expr = before + expr[i+2] + after;
                        addReductionStep(expr, 'Applied I combinator: I x β†’ x');
                        reduced = true;
                        break;
                    }
                }
                
                if (!reduced) {
                    addReductionStep(expr, 'No further reductions possible', true);
                }
            }
            
            // Add a reduction step to the UI
            function addReductionStep(expression, explanation, isFinal = false) {
                const stepEl = document.createElement('div');
                stepEl.className = `bg-gray-50 p-3 rounded-lg mb-2 reduction-step ${isFinal ? 'border-l-4 border-blue-500' : ''}`;
                
                const exprEl = document.createElement('div');
                exprEl.className = 'font-mono text-lg mb-1';
                exprEl.textContent = expression;
                
                const explanationEl = document.createElement('div');
                explanationEl.className = 'text-sm text-gray-600';
                explanationEl.textContent = explanation;
                
                stepEl.appendChild(exprEl);
                stepEl.appendChild(explanationEl);
                
                reductionSteps.appendChild(stepEl);
                stepEl.scrollIntoView({ behavior: 'smooth' });
            }
        });
    </script>
<p style="border-radius: 8px; text-align: center; font-size: 12px; color: #fff; margin-top: 16px;position: fixed; left: 8px; bottom: 8px; z-index: 10; background: rgba(0, 0, 0, 0.8); padding: 4px 8px;">Made with <img src="https://enzostvs-deepsite.hf.space/logo.svg" alt="DeepSite Logo" style="width: 16px; height: 16px; vertical-align: middle;display:inline-block;margin-right:3px;filter:brightness(0) invert(1);"><a href="https://enzostvs-deepsite.hf.space" style="color: #fff;text-decoration: underline;" target="_blank" >DeepSite</a> - 🧬 <a href="https://enzostvs-deepsite.hf.space?remix=MarcdeFalco/combinators" style="color: #fff;text-decoration: underline;" target="_blank" >Remix</a></p></body>
</html>