-
Notifications
You must be signed in to change notification settings - Fork 3
/
hhprolog_intro.html
679 lines (566 loc) · 13.6 KB
/
hhprolog_intro.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
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
<!DOCTYPE html>
<!--
An interface showcasing hhprolog basics.
Hitchhiker Prolog original Java code by Paul Tarau.
The reference document: http://www.cse.unt.edu/~tarau/research/2017/eng.pdf
Version: 1.0.1
License: MIT
Copyright (c) 2018,2019 Carlo Capelli
-->
<html>
<head>
<title>hhprolog intro</title>
<meta charset="utf-8">
<script src="hhprolog.js"></script>
</head>
<body>
<style>
#main {
display: flex;
flex-direction: column;
justify-content: space-between;
}
.row {
display: flex;
flex-direction: row;
justify-content: space-between;
}
.controls {
display: flex;
flex-direction: column;
}
.boxed {
box-shadow: 8px 8px 5px #444;
background-image: linear-gradient(180deg, #fff, #ddd 40%, #ccc);
}
.spacing {
border: 1px solid #333;
padding: 8px 12px;
}
.running {
cursor: wait;
}
.hscroll {
overflow: auto;
}
.hidden {
display: none;
}
</style>
<div id=main>
<div class="spacing row">
<div class="controls spacing">
<select id=choice_script>
<option>?</option>
</select>
<button id=run disabled=true>Run</button>
<label>print solutions
<input id=print_sol type=checkbox disabled=true>
</label>
<label>Show VM code
<input id=print_vm_code type=checkbox>
</label>
</div>
<div class="boxed spacing">
<div>Solutions</div>
<pre id=run_output></pre>
</div>
</div>
<div class="spacing row source">
<div class="boxed spacing">
<div>Prolog source</div>
<pre id=script_pl></pre>
</div>
<div class="boxed spacing">
<div>VM code (Readable NL)</div>
<pre id=script_nl></pre>
</div>
<div id=script_bin_box class="boxed spacing hscroll hidden">
<div>VM code (Readable binary dump)</div>
<pre id=script_bin></pre>
</div>
</div>
</div>
<script>
/*
function timeoutPromise(message, interval) {
return new Promise((resolve, reject) => {
if (message === '' || typeof message !== 'string') {
reject('Message is empty or not a string')
} else if (interval < 0 || typeof interval !== 'number') {
reject('Interval is negative or not a number')
} else {
setTimeout(function(){
resolve(message)
}, interval)
}
})
}
*/
window.onload = function() {
choice_script.innerHTML += Object.keys(scripts).map(function(s) { return '<option id='+s+'>'+s+'</option>' })
}
var trace = console.log
function settext(e, t) {
e.innerHTML = t || ''
}
function gettext(e) {
return e.innerHTML
}
function textln(e, l) {
e.innerHTML += `\n` + (l || '')
}
function clear() {
[script_pl, script_nl, script_bin, run_output].forEach(function(e) { settext(e) })
}
// create an hhprolog instance with currently selected script,
// binding IO to HTML5 elements
function runnable(out_elem) {
settext(out_elem = out_elem || run_output)
return new Prog({
writeln: function(l) {
textln(out_elem, l)
},
nl_source: gettext(script_nl)
})
}
print_vm_code.onchange = function() {
script_bin_box.classList[print_vm_code.checked ? 'remove' : 'add']('hidden')
}
function disabled(elements, disabled) {
elements.forEach(function(e) {e.disabled = disabled})
}
function ltrim(t) {
return t.split('\n').map(l => l.substring(6)).join('\n')
}
choice_script.onchange = function() {
clear()
var choice = choice_script.value
if (choice !== '?') {
settext(script_pl, ltrim(scripts[choice].pl))
settext(script_nl, ltrim(scripts[choice].nl))
if (print_vm_code.checked)
runnable(script_bin).ppCode()
disabled([run, print_sol, print_vm_code], false)
}
else {
disabled([run, print_sol, print_vm_code], true)
}
}
run.onclick = function() {
//timeoutPromise('hello', 2000).then(alert)
if (print_vm_code.checked)
runnable(script_bin).ppCode()
try {
run.disabled = true
main.classList.add('running')
setTimeout(function() {
var prog = runnable()
var t0 = Date.now()
prog.run(print_sol.checked)
prog.options.writeln('elapsed secs:' + (Date.now() - t0) / 1000)
run.disabled = false
main.classList.remove('running')
})
}
catch(e) {
alert(e)
}
}
var scripts = {
// CC
mini: {
pl: `
a(1).
a(2).
goal(Y):-a(Y).
`,
nl: `
a 1 .
a 2 .
goal Y
if
a Y .
`
},
memb: {
pl: `
memb(E,[E|_]).
memb(E,[_|T]) :- memb(E,T).
goal(E):-memb(E,[0,1,2,3,4,5,6,7,8,9,10,11]).
`,
nl: `
memb E _0 and
_0 holds list E _1 .
memb E _0 and
_0 holds list _1 T
if
memb E T .
goal E
if
memb E _0 and
_0 lists 0 1 2 3 4 5 6 7 8 9 10 11 .
`
},
// PT
add: {
pl: `
add(0,X,X).
add(s(X),Y,s(Z)):-add(X,Y,Z).
goal(R):-add(s(s(0)),s(s(0)),R).
`,
nl: `
add 0 X X .
add _0 Y _1 and
_0 holds s X and
_1 holds s Z
if
add X Y Z .
goal R
if
add _0 _1 R and
_0 holds s _2 and
_2 holds s 0 and
_1 holds s _3 and
_3 holds s 0 .
`,
},
queens: {
pl: `
place_queen(I,[I|_],[I|_],[I|_]).
place_queen(I,[_|Cs],[_|Us],[_|Ds]):-place_queen(I,Cs,Us,Ds).
place_queens([],_,_,_).
place_queens([I|Is],Cs,Us,[_|Ds]):-
place_queens(Is,Cs,[_|Us],Ds),
place_queen(I,Cs,Us,Ds).
gen_places([],[]).
gen_places([_|Qs],[_|Ps]):-gen_places(Qs,Ps).
qs(Qs,Ps):-gen_places(Qs,Ps),place_queens(Qs,Ps,_,_).
goal(Ps):-qs([0,1,2,3,4,5,6,7,8,9],Ps).
`,
nl: `
place_queen I _0 _1 _2 and
_0 holds list I _3 and
_1 holds list I _4 and
_2 holds list I _5 .
place_queen I _0 _1 _2 and
_0 holds list _3 Cs and
_1 holds list _4 Us and
_2 holds list _5 Ds
if
place_queen I Cs Us Ds .
place_queens nil _0 _1 _2 .
place_queens _0 Cs Us _1 and
_0 holds list I Is and
_1 holds list _2 Ds
if
place_queens Is Cs _3 Ds and
_3 holds list _4 Us and
place_queen I Cs Us Ds .
gen_places nil nil .
gen_places _0 _1 and
_0 holds list _2 Qs and
_1 holds list _3 Ps
if
gen_places Qs Ps .
qs Qs Ps
if
gen_places Qs Ps and
place_queens Qs Ps _0 _1 .
goal Ps
if
qs _0 Ps and
_0 lists 0 1 2 3 4 5 6 7 8 9 .
`
},
sud4x: {
pl: `
/* note - this will take a lot ... */
s4x4([
[
[S11,S12, S13,S14],
[S21,S22, S23,S24],
[S31,S32, S33,S34],
[S41,S42, S43,S44]
],
[
[S11,S21, S31,S41],
[S12,S22, S32,S42],
[S13,S23, S33,S43],
[S14,S24, S34,S44]
],
[
[S11,S12, S21,S22],
[S13,S14, S23,S24],
[S31,S32, S41,S42],
[S33,S34, S43,S44]
]
]):-
true.
/* this generates the 288 puzzles - and solves one if given clues */
sudoku(Xss):-
s4x4([Xss|Xsss]),
map11(permute,[1,2,3,4],[Xss|Xsss]).
map1x(_,_,[]).
map1x(F,Y,[X|Xs]):- call(F,Y,X),map1x(F,Y,Xs).
map11(_,_,[]).
map11(F,X,[Y|Ys]):-
map1x(F,X,Y),
map11(F,X,Ys).
permute([],[]).
permute([X|Xs],Zs):-permute(Xs,Ys),ins(X,Ys,Zs).
ins(X,Xs,[X|Xs]).
ins(X,[Y|Xs],[Y|Ys]):-ins(X,Xs,Ys).
goal(Xss):-sudoku(Xss).
`,
nl: `
s4x4 _0 and
_0 lists _1 _2 _3 and
_1 lists _4 _5 _6 _7 and
_4 lists S11 S12 S13 S14 and
_5 lists S21 S22 S23 S24 and
_6 lists S31 S32 S33 S34 and
_7 lists S41 S42 S43 S44 and
_2 lists _8 _9 _10 _11 and
_8 lists S11 S21 S31 S41 and
_9 lists S12 S22 S32 S42 and
_10 lists S13 S23 S33 S43 and
_11 lists S14 S24 S34 S44 and
_3 lists _12 _13 _14 _15 and
_12 lists S11 S12 S21 S22 and
_13 lists S13 S14 S23 S24 and
_14 lists S31 S32 S41 S42 and
_15 lists S33 S34 S43 S44 .
sudoku Xss
if
s4x4 _0 and
_0 holds list Xss Xsss and
map11 permute _1 _2 and
_1 lists 1 2 3 4 and
_2 holds list Xss Xsss .
map1x _0 _1 nil .
map1x F Y _0 and
_0 holds list X Xs
if
F Y X and
map1x F Y Xs .
map11 _0 _1 nil .
map11 F X _0 and
_0 holds list Y Ys
if
map1x F X Y and
map11 F X Ys .
permute nil nil .
permute _0 Zs and
_0 holds list X Xs
if
permute Xs Ys and
ins X Ys Zs .
ins X Xs _0 and
_0 holds list X Xs .
ins X _0 _1 and
_0 holds list Y Xs and
_1 holds list Y Ys
if
ins X Xs Ys .
goal Xss
if
sudoku Xss .
`,
},
perms: {
pl: `
eq(X,X).
sel(X,[X|Xs],Xs).
sel(X,[Y|Xs],[Y|Ys]):-sel(X,Xs,Ys).
perm([],[]).
perm([X|Xs],Zs):-
perm(Xs,Ys),
sel(X,Zs,Ys).
app([],Xs,Xs).
app([X|Xs],Ys,[X|Zs]):-app(Xs,Ys,Zs).
nrev([],[]).
nrev([X|Xs],Zs):-nrev(Xs,Ys),app(Ys,[X],Zs).
input([1,2,3,4,5,6,7,8]).
goal(Y):-input(X),nrev(X,Y),perm(X,Y),perm(Y,X).
`,
nl: `
eq X X .
sel X _0 Xs and
_0 holds list X Xs .
sel X _0 _1 and
_0 holds list Y Xs and
_1 holds list Y Ys
if
sel X Xs Ys .
perm nil nil .
perm _0 Zs and
_0 holds list X Xs
if
perm Xs Ys and
sel X Zs Ys .
app nil Xs Xs .
app _0 Ys _1 and
_0 holds list X Xs and
_1 holds list X Zs
if
app Xs Ys Zs .
nrev nil nil .
nrev _0 Zs and
_0 holds list X Xs
if
nrev Xs Ys and
app Ys _1 Zs and
_1 lists X .
input _0 and
_0 lists 1 2 3 4 5 6 7 8 .
goal Y
if
input X and
nrev X Y and
perm X Y and
perm Y X .
`
},
lambdas: {
pl: `
% generates all lambda terms of size 4
genLambda(X,Vs,N,N):-memb(X,Vs).
genLambda(l(X,A),Vs,s(N1),N2):-genLambda(A,[X|Vs],N1,N2).
genLambda(a(A,B),Vs,s(N1),N3):-genLambda(A,Vs,N1,N2),genLambda(B,Vs,N2,N3).
memb(X,[X|_]).
memb(X,[_|Xs]):-memb(X,Xs).
genClosedLambdaTerm(L,T):-genLambda(T,[],L,zero).
four(s(s(s(s(zero))))).
goal(Lam):-four(Size),genClosedLambdaTerm(Size,Lam).
`,
nl: `
genLambda X Vs N N
if
memb X Vs .
genLambda _0 Vs _1 N2 and
_0 holds l X A and
_1 holds s N1
if
genLambda A _2 N1 N2 and
_2 holds list X Vs .
genLambda _0 Vs _1 N3 and
_0 holds a A B and
_1 holds s N1
if
genLambda A Vs N1 N2 and
genLambda B Vs N2 N3 .
memb X _0 and
_0 holds list X _1 .
memb X _0 and
_0 holds list _1 Xs
if
memb X Xs .
genClosedLambdaTerm L T
if
genLambda T nil L zero .
four _0 and
_0 holds s _1 and
_1 holds s _2 and
_2 holds s _3 and
_3 holds s _4 and
_4 holds s zero .
goal Lam
if
four Size and
genClosedLambdaTerm Size Lam .
`
},
mperms: {
pl: `
metaint([]).
metaint([G|Gs]):-cls([G|Bs],Gs),metaint(Bs).
cls([sel(X,[X|Xs],Xs)|Tail],Tail).
cls([sel(X,[Y|Xs],[Y|Ys]),sel(X,Xs,Ys)|Tail],Tail).
cls([perm([],[])|Tail],Tail).
cls([perm([X|Xs],Zs),perm(Xs,Ys),sel(X,Zs,Ys)|Tail],Tail).
input([1,2,3,4,5,6,7,8],[8,7,6,5,4,3,2,1]).
goal(Y):-input(X,Y),metaint([perm(X,Y),perm(Y,X)]).
`,
nl: `
metaint nil .
metaint _0 and
_0 holds list G Gs
if
cls _1 Gs and
_1 holds list G Bs and
metaint Bs .
cls _0 Tail and
_0 holds list _1 Tail and
_1 holds sel X _2 Xs and
_2 holds list X Xs .
cls _0 Tail and
_0 holds list _1 _2 and
_1 holds sel X _3 _4 and
_3 holds list Y Xs and
_4 holds list Y Ys and
_2 holds list _5 Tail and
_5 holds sel X Xs Ys .
cls _0 Tail and
_0 holds list _1 Tail and
_1 holds perm nil nil .
cls _0 Tail and
_0 holds list _1 _2 and
_1 holds perm _3 Zs and
_3 holds list X Xs and
_2 holds list _4 _5 and
_4 holds perm Xs Ys and
_5 holds list _6 Tail and
_6 holds sel X Zs Ys .
input _0 _1 and
_0 lists 1 2 3 4 5 6 7 8 and
_1 lists 8 7 6 5 4 3 2 1 .
goal Y
if
input X Y and
metaint _0 and
_0 lists _1 _2 and
_1 holds perm X Y and
_2 holds perm Y X .
`
},
combine: {
pl: `
member(X,[X|_]).
member(X,[_|L]):-member(X,L).
maplist(_,[],[]).
maplist(P,[L1|L1s],[L2|L2s]) :- call(P,L1,L2), maplist(P,L1s,L2s).
combine(Ls,Rs) :- maplist(member,Rs,Ls).
goal(R) :- combine([[1,2],[a,b],[8,9],[x,y]],R).
`,
nl: `
member X _0 and
_0 holds list X _1 .
member X _0 and
_0 holds list _1 L
if
member X L .
maplist _0 nil nil .
maplist P _0 _1 and
_0 holds list L1 L1s and
_1 holds list L2 L2s
if
P L1 L2 and
maplist P L1s L2s .
combine Ls Rs
if
maplist member Rs Ls .
goal R
if
combine _0 R and
_0 lists _1 _2 _3 _4 and
_1 lists 1 2 and
_2 lists a b and
_3 lists 8 9 and
_4 lists x y.
`
}
};
</script>
</body>
</html>