-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
292 lines (265 loc) · 13.9 KB
/
index.html
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
<!DOCTYPE html>
<html>
<!--Copyright (C) Swastik Roy, 2018-->
<head>
<meta charset="UTF-8">
<meta name="description" content="Lambda Calculus Evaluator">
<meta name="keywords" content="Lambda Calculus, Lambda, Functional Programming">
<meta name="author" content="Swastik Roy">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<title>Lambda Calculus Calculator</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="dist/scripts/scripts.min.js"></script>
<link rel="stylesheet" href="dist/css/styles.min.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.13/semantic.min.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.13/semantic.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<style>
.vue-unloaded{
display:none;
}
.notloaded{
display: none !important;
}
</style>
</head>
<body>
<div style="text-align:center; margin-top:80px;" class="my-preloader">
<h4>Loading Lambda Evaluator</h4>
<div class="spinner">
<div class="dot1"></div>
<div class="dot2"></div>
</div>
</div>
<div class="container notloaded" id="lambda" class="vue-unloaded" style="min-height:560px;">
<br /><br />
<h2 style="text-align:center;">Lambda Calculator</h2>
<br />
<div class="row">
<div class="col-sm-6">
<div class="ui form" style="margin: 0 auto;">
<div class="field">
<h4>Lambda Calculus Expression</h4>
<div class="ui selection dropdown">
<input type="hidden" value="1" id="ev-method">
<i class="dropdown icon"></i>
<div class="default text">Normal Order Evaluation</div>
<div class="menu">
<div class="item" data-value="1">Normal Order Evaluation</div>
<div class="item" data-value="2">Call By Value</div>
<div class="item" data-value="3">Call By Name</div>
</div>
</div>
<textarea v-on:keyup.enter="evaluate" onkeypress="return isEnterKey(event)" v-model="input" id="input"></textarea>
</div>
</div>
<br />
<div v-on:click="evaluate" class="ui submit button" style="float:right;">Evaluate</div>
<div class="ui dropdown">
<div class="text">Select from examples</div>
<i class="dropdown icon"></i>
<div class="menu">
<div class="item" v-for="option in examples" v-on:click="input=option"><span v-html="escapeLambda(option)"></span></div>
</div>
</div>
</div>
<div class="col-sm-6">
<div class="ui form">
<div class="field">
<h4>Output</h4>
<div style="border:solid 2px #ccc; border-radius:4px; min-height:200px; padding:10px;">
<h3><span v-if="input" style="font-size:0.7em">Input Expression:</span>
<span v-html="inputEscaped"></span></h3>
<div class="ui negative message" v-if="error">
<i class="close icon"></i>
<div class="header">
Invalid Input
</div>
<p>{{error}}
</p>
</div>
<transition
name="bounce"
enter-active-class="bounceInLeft"
leave-active-class="bounceOutRight"
>
<p v-if="parenthesized">Parenthesized Output <span v-html="parenthesized"></span></p>
</transition>
<p v-if="outputExpanded">Expanded Output <span v-html="escapeLambda(outputExpanded)"></span></p>
<div v-for="(result, index) in results" >
<h4><strong>Step {{index+1}} :</strong> <span v-html="result"></span></h4>
<br />
</div>
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-offset-3 col-sm-6">
<br /><br />
<h3 style="text-align:center;">How to use this evaluator</h3>
<div class="ui list">
<a class="item">
<div class="content">
<div class="header">How to write Lambda(λ) in input?</div>
<div class="description">Use captial letter 'L' to denote Lambda. Eg. "(Lx.x) x" for "(λx.x) x"</div>
</div>
</a>
<a class="item">
<div class="content">
<div class="header">How to write applications?</div>
<div class="description">For application "T1 T2", there must be a white space between T1 and T2.<br /> Eg. it must be "(Lx.x) (Lx.x)", not "(Lx.x)(Lx.x)"</div>
</div>
</a>
<a class="item">
<div class="content">
<div class="header">How to group expressions?</div>
<div class="description">You can group expressesions with parenthesis. Please do no use curly braces or square brackets</div>
</div>
</a>
<a class="item">
<div class="content">
<div class="header">When does the evualator throw error?</div>
<div class="description">If the parenthesis are not matched. Also, numbers greater than 20 can not be converted to church numeral for now.</div>
</div>
</a>
<a class="item">
<div class="content">
<div class="header">What are reserved expressions?</div>
<div class="description">Reserved expressions are mult, succ, true, false, any natural number between 1 to 20. <br />
If you use these keywords as expression, they will be expanded to their lambda form.</div>
</div>
</a>
</div>
<br />
<br /><br />
</div>
<div class="col-sm-offset-3 col-sm-6" style="text-align:center;">
<h3>FAQ</h3>
<div class="ui list">
<a class="item" href="https://en.wikipedia.org/wiki/Lambda_calculus" target="_blank">What is a Lambda Calculus?</a>
<a class="item" href="https://www.quora.com/Why-is-it-called-lambda-calculus" target="_blank">Why call it "Lambda"?</a>
<a class="item" href="https://en.wikipedia.org/wiki/Evaluation_strategy" target="_blank">What is Normal Order Evaluation? Call By Value? Call By Name?</a>
</div>
</div>
</div>
</div>
<footer>
<div class="container notloaded" style="margin-top:50px;">
<p style="text-align:center;">Copyright © 2017 Swastik Roy</p>
</div>
</footer>
<script>
var color = ["black", "red", "blue", "green", "darkblue", "orange"];
function isEnterKey(evt)
{
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode == 13)
return false;
return true;
}
$(document).ready(function(){
$('.ui.dropdown').dropdown();
});
var Method = {
NORMAL_ORDER_EVALUATION: '1',
CALL_BY_VALUE : '2',
CALL_BY_NAME: '3'
}
var re = new RegExp('L', 'g');
var vm = new Vue({
el: '#lambda',
data: {
input: '',
selectedMethod: '',
lastName: 'Bar',
results: [],
parenthesized: false,
outputExpanded: null,
error: null,
examples: ["(Lx.x) x", "(Lx.x (Lx.x) x) x", "((Lx. x)(Lx. x x)) ((Lx. x x) (Lx. x x))",
"0 a b", "1 a b", "3 a b", "plus 3 4"]
},
methods: {
escapeLambda: function(input){
return input.replace(re, "λ");
},
precheckInput: function(input){
input = input.replace(/(\r\n|\n|\r)/gm,"");
var pendingParens = 0;
for(var x = 0; x< input.length; x++){
if(input[x] == '(')pendingParens++;
if(input[x] == ')')pendingParens--;
}
if(pendingParens != 0){
this.error ="Error: Unmatched parenthesis";
this.results = [];
this.parenthesized = false;
return {
error: "Error: Unmatched parenthesis"
}
}
this.error = null;
return input;
},
evaluate: function(){
var selectedMethod = $("#ev-method").val();
var Evaluator =NormalOrderEvaluator;
var inputVal = this.precheckInput(this.input);
if(inputVal.error)return;
if(selectedMethod == Method.NORMAL_ORDER_EVALUATION){
Evaluator = NormalOrderEvaluator;
}
else if(selectedMethod == Method.CALL_BY_VALUE){
Evaluator = CallByValueEvaluator;
}
else if(selectedMethod == Method.CALL_BY_NAME){
Evaluator = CallByNameEvaluator;
}
var results = [];
var output = parse_expression(inputVal);
this.parenthesized = output.text.replace(re, "λ");
var expandedExp = expandExpression(output);
if(JSON.stringify(expandedExp) != JSON.stringify(output)){
output = expandedExp;
refreshText(output)
this.outputExpanded = output.text;
}
else{
this.outputExpanded = false;
}
setRedexLevels(output,0);
var result;
while(true){
result = {redexIndex: output.text};
setRedexIndexes(output,0, result);
var oldString = JSON.stringify(output);
output = Evaluator.evaluate(output);
colored = colorParens(output.text, result.redexIndex);
text = colored.replace(re, "λ");
if(oldString == JSON.stringify(output)){
break;
}
results.push(text);
}
this.results = results;
if(results.length == 0){
this.results.push("Can not be reduced");
}
return false;
}
},
mounted: function(){
$(".notloaded").removeClass("notloaded");
$(".my-preloader").addClass("notloaded");
},
computed: {
inputEscaped: function () {
return this.input.replace(re, "λ");
}
}
});
</script>
</body>
</html>