-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
271 lines (270 loc) · 7.96 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
<!doctype html>
<html>
<head>
<title>Various type theories in JS</title>
<meta charset='UTF-8'>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<meta name='keywords' content='type theory, lambda calculus, dependent types'>
<meta name='description' content='Sandbox for computing with various type theories.'>
<link rel='icon' type='image/x-icon' href='data:image/x-icon;base64,'>
<style>
@font-face {
font-family: 'univers';
src: url('fonts/UniversNextTypewriter.woff') format('woff'),
url('fonts/UniversNextTypewriter.woff2') format('woff2');
font-weight: normal;
font-style: normal;
font-display: swap }
@font-face {
font-family: 'literata';
src: url('fonts/Literata-Regular.woff2') format('woff2'),
url('fonts/Literata-Regular.woff') format('woff');
font-weight: normal;
font-style: normal;
font-display: swap }
@font-face {
font-family: 'literata';
src: url('fonts/Literata-Bold.woff2') format('woff2'),
url('fonts/Literata-Bold.woff') format('woff');
font-weight: bold;
font-style: normal;
font-display: swap }
html {
margin: 0;
--pad: 10px;
font-size: 16px }
body {
margin: 1px 2px 1px 1px;
overflow: hidden;
background: #222;
color: #eee }
main {
display: grid;
grid-template-columns: 16rem 1fr;
width: 100%;
grid-gap: 1px }
main.nav-collapse {
grid-template-columns: 1.8rem 1fr }
nav {
font-family: 'literata';
grid-row: 1 / span 2;
padding: .4rem;
display: flex;
gap: .4rem;
flex-flow: column nowrap }
main.nav-collapse > nav {
display: block;
cursor: pointer }
main.nav-collapse > nav:hover {
border: .2rem #555 solid;
background-color: #222; }
main.nav-collapse > nav > * { display: none }
nav > .ui-title {
font-size: large;
font-weight: bold }
nav > .arrow {
font-size: large;
--cursorY: 50vh;
position: absolute;
display: none;
left: .7rem;
top: calc(var(--cursorY) - .7rem);
user-select: none }
main.nav-collapse > nav > .arrow::before { content: "→" }
main:not(.nav-collapse) > nav > .arrow::before { content: "←" }
main.nav-collapse > nav:hover > .arrow { display: block }
main:not(.nav-collapse) > nav > .arrow {
display: block;
left: 13.8rem;
cursor: pointer }
main:not(.nav-collapse) > nav > .arrow:hover {
top: calc(var(--cursorY) - .9rem);
left: 13.6rem;
border: .2rem #555 solid }
#ui { width: fit-content }
#ui > input[type='radio'] { display: none }
#ui > input[type='radio'] + label {
display: block;
white-space: pre-wrap;
padding: .1rem;
cursor: pointer }
#ui > input[type='radio']:not(:checked) + label:hover { background-color: #ff6700 }
#ui > input[type='radio']:checked + label {
background-color: #eee;
color: #000 }
#source, #log {
width: calc(100% - 2 * var(--pad) - 1em + 1px);
height: calc(50vh - 2 * var(--pad) - 1em - 1.5px);
box-shadow: 0 0 0 1px #444;
background: transparent;
padding: var(--pad);
border: .5rem solid #222;
line-height: 1.2rem;
font-size: inherit;
font-family: 'univers';
font-variant-ligatures: none }
#source {
margin: 0;
outline: 0;
min-height: 1.6rem;
max-height: calc(100vh - 22px - 2rem);
resize: vertical;
color: #eee }
#log {
overflow-y: auto;
word-break: break-all;
white-space: pre-wrap }
#repl {
position: absolute;
top: 10px;
right: 10px;
font-family: 'literata' }
#repl > * {
background-color: #eee;
color: #000;
white-space: pre;
padding: .1rem;
cursor: pointer }
#run[data-state='load']::before { content: "Load" }
#run[data-state='load']:hover,
#run[data-state='run'] ~ .run-option:hover {
background-color: #ff6700;
color: #eee }
#run[data-state='run'] ~ .run-option { display: none }
#repl:hover > #run[data-state='run'] ~ .run-option { display: block }
#run[data-state='run']::before { content: "Run" }
hr {
border: 0;
height: 0;
border-top: 1px solid #333 }
</style>
</head>
<body>
<main>
<nav>
<div class="arrow"></div>
<span class="ui-title">Choose type theory:</span>
<form id="ui">
<input type="radio" name="tts" id="1"/><label for="1">Untyped lambdas</label>
<input type="radio" name="tts" id="2"/><label for="2">Dependent types</label>
<input type="radio" name="tts" id="3"/><label for="3">DT with holes</label>
<input type="radio" name="tts" id="4"/><label for="4">DT with implicit arguments</label>
<input type="radio" name="tts" id="5" /><label for="5">DT with metavariable pruning</label>
<input type="radio" name="tts" id="6" checked/><label for="6">DT with first class polymorphism</label>
</form>
</nav>
<textarea id="source" spellcheck="false"></textarea>
<div id="log"></div>
</main>
<div id="repl">
<div id="run" data-state="load"></div>
</div>
<template id="run-option">
<div class="run-option"></div>
</template>
<script src="js/tt.js"></script>
<script src="js/machine.js"></script>
<script>
var app = new $.Machine({ S: null, tt: null, ready: null });
let sourceTextbox = $('#source'),
logDiv = $('#log'),
runButton = $('#run'),
adjustHeight = () => logDiv.style.height =
window.innerHeight - 23 - sourceTextbox.offsetHeight + 'px',
log = (o = {}, isErr = false) => {
const str = isErr ? o : Object.entries(o)
.reduce((str, [k, v]) => str + (/\r\n?|\n/g.test(v) ? `${k}:\n${v}\n\n` : `${k}: ${v}\n`), '');
logDiv.childNodes.length && logDiv.appendChild(document.createElement('hr'));
logDiv.appendChild(document.createTextNode(str));
console.log(str) };
new MutationObserver(adjustHeight).observe(
sourceTextbox, { attributes: true, attributeFilter: [ "style" ] });
$.targets({
load () {
app.emit("selectTT", $("#ui > input[type='radio']:checked").id);
let navClick = { click () {
$("main").classList.remove("nav-collapse");
$.queries({
nav: { click: "click" },
"nav > .arrow": arrowClick
})
} },
arrowClick = { click (e) {
e.stopPropagation();
$("main").classList.add("nav-collapse");
$.queries({
"nav > .arrow": { click: "click" },
nav: navClick
})
} };
$.queries({ "nav > .arrow": arrowClick })
},
app: {
selectTT (i) {
this.tt = parseInt(i);
app.emit("fetchExample");
if (this.S === null) this.S = TT({debug: false, which: i});
else {
this.S.select(i)
}
},
fetchExample () {
fetch(`examples/${[
"01-eval-closures-debruijn",
"02-typecheck-closures-debruijn",
"03-holes",
"04-implicit-args",
"05-pruning",
"06-first-class-poly"
][this.tt - 1]}.r2`).then(res => res.text())
.then(t => sourceTextbox.innerHTML = t)
},
ready (o) {
$("#run").dataset.state = "run";
this.ready = o;
for (let meth in o) {
let opt = $.load("run-option", "#repl")[0][0];
opt.innerText = meth;
opt.dataset.method = meth;
}
$.queries({ "#repl > .run-option": { click () {
app.emit("run", async () => {
log(await app.state().ready[this.dataset.method].run())
})
} } })
},
run (fn) {
fn().catch(e => { log(e.message, true); "stack" in e ? console.warn(e) : console.warn(e.message) })
.then(() => $('#log').scrollTo(0, 1e6))
},
reset () {
if ($("#run").dataset.state === "run") {
$("#run").dataset.state = "load";
$.all("#run ~ .run-option").map(el => el.remove())
}
}
}
})
$.queries({
"#run": {
click () {
if (this.dataset.state === "load") app.emit("run", async () => {
let { ready } = await app.state().S.import({code: sourceTextbox.value});
app.emit("ready", ready)
})
}
},
"#source": {
input () { app.emit("reset") }
},
"#ui > input[type='radio']": {
change (e) { if (e.target.checked) {
app.emit("selectTT", this.id);
app.emit("reset")
} }
}
})
</script>
<noscript><h6>Only viewable with JavaScript enabled.</h6></noscript>
</body>
</html>