-
Notifications
You must be signed in to change notification settings - Fork 3
/
test_hhprolog_by_cdn.html
328 lines (270 loc) · 5.08 KB
/
test_hhprolog_by_cdn.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
<!DOCTYPE html>
<!--
Author: Carlo Capelli
Version: 1.0.0
License: MIT
Copyright (c) 2017,2018 Carlo Capelli
-->
<html>
<head>
<meta charset="utf-8">
<title>test hhprolog (served by jsDelivr) in browser</title>
<!--
<script src="https://cdn.jsdelivr.net/gh/CapelliC/hitchhicker-prolog@3f475c0b1cc765ca642d24b6573ace998d93087b/hhprolog-es6.js"></script>
-->
<script src="https://cdn.jsdelivr.net/gh/CapelliC/hitchhicker-prolog@8424f251246b5f45d5a7ee7046e3e32d29b8282c/hhprolog-es6.js"></script>
<!--
<script src="https://cdn.jsdelivr.net/gh/CapelliC/hitchhicker-prolog@1.0.0/hhprolog-es6.js"></script>
-->
</head>
<body>
<script>
// CC
const mini = `
a 1 .
a 2 .
goal Y
if
a Y .
`
const memb = `
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
const add = `
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 .
`
const queens = `
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 .
`
const sud4x = `
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 .
`
const perms = `
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 9 10 11 .
goal Y
if
input X and
nrev X Y and
perm X Y and
perm Y X .
`
const lambdas = `
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 .
nine _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 _5 and
_5 holds s _6 and
_6 holds s _7 and
_7 holds s _8 and
_8 holds s zero .
goal Lam
if
nine Size and
genClosedLambdaTerm Size Lam .
`
const mperms = `
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 9 10 11 and
_1 lists 11 10 9 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 .
`
const prog = new Prog(mini)
prog.ppCode()
const t0 = Date.now()
prog.run(false)
console.log('elapsed secs', (Date.now() - t0) / 1000)
</script>
</body>