-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.html
219 lines (184 loc) · 6.36 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
<!doctype html>
<html>
<head>
<title>CodeMirror: Lambda Calculus mode</title>
<meta charset="utf-8"/>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.1/codemirror.min.css">
<link rel="stylesheet" href="./theme/codewars.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro">
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/awsm_theme_black.min.css">
<style>
.CodeMirror { border-top: 1px solid black; border-bottom: 1px solid black; }
[v-cloak] { display: none !important; }
</style>
</head>
<body class="dark">
<header class="!mb-0">
<h1>
<a href="https://github.com/codewars/lambda-calculus" title="Lambda Calculus">
<img alt="Lambda Calculus" src="https://raw.githubusercontent.com/codewars/lambda-calculus/main/logo/logo-white.svg" width="64" height="64">
</a>
<p class="mt-4 text-2xl text-center">Lambda Calculus mode for CodeMirror</p>
</h1>
<p class="text-center"><code>text/x-lambdacalc</code></p>
<p class="text-center"><a href="https://github.com/codewars/codemirror-lambda-calculus">@codewars/codemirror-lambda-calculus</a></p>
</header>
<main class="mt-6" v-scope>
<form>
<textarea id="code">
# Ignored arguments
false = \ _a b . b
true = \ a _b . a
not = \ b . b false true
const = true
# Multiple definition
true = not false
# Invalid whitespace (tabs)
whitespace = ()
# Bare lambda
(\ f x . f (x x))
# Bare term
const f x
# Symbols
< a b c > => < a a a >
# Unbound
some-func = \ local . true non-existent local
# Out of scope args
other-func = \ x . const (\ scoped-arg . x ()) scoped-arg x
# Debug mode on
#debug
# Invalid names - Debug
%value = ()
# Bare lambda - Debug
(\ f x . f (x x))
# Bare term - Debug
const f x
# Symbols - Debug
< a b c > => < a a a >
# Unbound - Debug
some-func = \ local . true non-existent local
# Out of scope args - Debug
other-func = \ x . const (\ scoped-arg . x ()) scoped-arg x
# Debug mode off
#debug
# More code
zero = false
succ = \ n f x . f (n f x)
is-z = \ n . n (const false) true
add = \ a b f x . a f (b f x)
mul = \ a b f . a (b f)
three = succ (succ (succ zero))
mt = mul three
nine = mul three three
pair = \ a b c . c a b
fst = \ c . c true
snd = \ c . c false
nil = pair false ()
null = \ l . not (fst l)
head = \ l . fst (snd l)
tail = \ l . snd (snd l)
cons = \ x xs . pair true (pair x xs)
replicate = \ n v . n (cons v) nil
repeat = \ v . cons v (repeat v)
foldr = \ f b as . null as b (f (head as) (foldr f b (tail as)))
pred = \ n f x . foldr (const f) x (tail (replicate n ())) # Bit janky lol
map = \ f xs . null xs nil (cons (f (head xs)) (map f (tail xs)))
sum = foldr add zero
drop = \ n . n tail
take = \ n xs . is-z n nil (cons (head xs) (take (pred n) (tail xs)))
col = \ n xs . head (drop n xs)
colS = \ n xs . cons (col n xs) (cons (col (succ n) xs) (cons (col (succ (succ n)) xs) (nil)))
row = \ n xs . map (col n) xs
row-s = \ n xs . cons (row n xs) (cons (row (succ n) xs) (cons (row (succ (succ n)) xs) (nil)))
chunk = \ a b xs . row-s a (colS b xs)
append = \ as bs . null as bs (cons (head as) (append (tail as) bs))
concat = foldr append nil
eq? = \ a b . is-z a (is-z b) (is-z b false (eq? (pred a) (pred b)))
all = foldr (\ a b . a b false) true
allf = \ f xs . all (map f xs)
</textarea>
</form>
<div class="mx-auto mt-8 w-96 grid grid-flow-row grid-cols-4 grid-rows-2 gap-1">
<input type="text"
v-model="evaluation"
@keyup.enter="evaluate()"
class="col-span-3"
/>
<button @click="evaluate()" class="col-span-1">Evaluate</button>
<code v-cloak class="px-2 py-1 mt-1 col-span-4 bg-white/10" v-show="result">{{ result }}</code>
<code v-cloak class="px-2 py-1 mt-1 col-span-4 bg-red-800/60" v-show="error">{{ error }}</code>
</div>
<div class="mx-auto mt-4 w-80">
<fieldset>
<legend>Options</legend>
<div class="grid grid-cols-2 grid-rows-3 gap-1">
<label class="!m-0" for="purity"><code>purity</code></label>
<select id="purity" v-model="purity">
<option v-for="opt in purityOptions" :value="opt" :selected="opt === purity">{{ opt }}</option>
</select>
<label class="!m-0" for="num-encoding"><code>numEncoding</code></label>
<select id="num-encoding" v-model="numEncoding">
<option v-for="opt in numEncodingOptions" :value="opt" :selected="opt === numEncoding">{{ opt }}</option>
</select>
<label class="!m-0" for="verbosity"><code>verbosity</code></label>
<select id="verbosity" v-model="verbosity">
<option v-for="opt in verbosityOptions" :value="opt" :selected="opt === verbosity">{{ opt }}</option>
</select>
</div>
</fieldset>
</div>
</main>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.1/codemirror.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.1/addon/edit/matchbrackets.min.js"></script>
<script type="module">
import "virtual:windi.css";
import { createApp } from "https://unpkg.com/[email protected]?module";
import * as LC from "https://unpkg.com/@codewars/lambda-calculus";
import { defineMode } from "./lambdacalc.js";
defineMode(CodeMirror);
const editor = CodeMirror.fromTextArea(document.getElementById("code"), {
mode: "lambdacalc",
lineNumbers: true,
matchBrackets: true,
theme: "codewars",
specialChars: /\\/,
specialCharPlaceholder: () => {
const elem = document.createElement("span");
elem.setAttribute("cm-text", "\\");
elem.innerHTML = "λ";
return elem;
}
});
editor.setSize(500, 400);
const toLambda = (s) => s.replace(/\\/g, "λ");
createApp({
purity: "Let",
purityOptions: ["Let", "LetRec", "PureLC"],
verbosity: "Calm",
verbosityOptions: ["Calm", "Concise", "Loquacious", "Verbose"],
numEncoding: "Church",
numEncodingOptions: ["None", "Church", "Scott", "BinaryScott"],
evaluation: "not false",
result: "",
error: "",
evaluate() {
this.result = "";
this.error = "";
const code = editor.getValue();
const val = this.evaluation;
const compile = LC.compileWith({
purity: this.purity,
verbosity: this.verbosity,
numEncoding: this.numEncoding,
});
try {
const { result } = compile(`${code}\n\nresult = ${val}`);
this.result = toLambda(result.term + "");
} catch (e) {
this.error = e.message || e.name;
}
},
}).mount();
</script>
</body>
</html>