-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.rkt
627 lines (537 loc) · 20.9 KB
/
main.rkt
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
#lang racket/base
(provide
quote
cons
list
racket-term
define-term-syntax-rule
==
=/=
absento
stringo
numbero
symbolo
fresh
conde
partial-apply
specialize-partial-apply
finish-apply
fallback
gather
staged
time-staged
later
fail
trace
defrel
defrel/staged
defrel-partial
defrel-partial/staged
defrel/staged/fallback
defrel-partial/staged/fallback
run
run*
(rename-out [i:generated-code generated-code])
quasiquote
(for-space mk quasiquote)
unquote)
;; https://github.com/michaelballantyne/syntax-spec
(require syntax-spec-v2
;; for a nasty workaround
(for-syntax (only-in syntax-spec-v2/private/ee-lib/main
compile-reference
lookup compile-binder!
compiled-from))
(for-syntax racket/base
racket/syntax
syntax/parse
racket/match
racket/list
syntax/id-set)
(prefix-in i: "private/internals.rkt"))
(begin-for-syntax
;; stage is one of: runtime, staging-time, multistage
(struct simple-rel [stage args-count] #:prefab)
(struct partial-rel [stage args1-count args2-count] #:prefab)
(define-persistent-symbol-table relation-info)
(define-persistent-symbol-table staged-relation-generator)
(define (register-simple-rel! name stage args)
(symbol-table-set! relation-info name (simple-rel stage (length args))))
(define (check-simple-rel name use-stage use-args)
(match (symbol-table-ref relation-info name)
[(simple-rel def-stage def-arg-count)
(unless (or (eq? def-stage 'multistage) (eq? def-stage use-stage))
(raise-syntax-error #f (format "def stage ~a should match use stage ~a for relation" def-stage use-stage) name))
(when (not (= def-arg-count (length use-args)))
(raise-syntax-error #f "wrong number of arguments to relation" name))
def-stage]
[_ (raise-syntax-error #f "relation application expects relation" name)]))
(define (name-generator! name)
(compile-binder! (compiled-from name) #:table staged-relation-generator))
(define (reference-generator name)
(compile-reference (compiled-from name) #:table staged-relation-generator))
)
(syntax-spec
(binding-class term-var)
(binding-class relation-name)
(extension-class term-macro
#:binding-space mk)
(nonterminal quoted
#:description "quoted value"
n:number
b:boolean
s:id
()
(a:quoted . d:quoted))
(nonterminal term
#:description "miniKanren term"
#:allow-extension term-macro
((~literal quote) t:quoted)
((~literal cons) t1:term t2:term)
((~literal list) t:term ...)
(#%term-var x:term-var)
(racket-term e:racket-expr)
(~> v:id
(if (lookup #'v (binding-class-predicate term-var))
#'(#%term-var v)
#'(racket-term v)))
(~> n:number
#'(quote n))
(~> b:boolean
#'(quote b)))
(nonterminal goal
#:bind-literal-set goal-literals
(== t1:term t2:term)
(=/= t1:term t2:term)
(absento t1:term t2:term)
(symbolo t1:term)
(numbero t1:term)
(stringo t1:term)
(trace name:id t:term-var ...+)
(fresh (x:term-var ...) g:goal ...+)
#:binding (scope (bind x) g)
(conde [g:goal ...+] ...+)
(partial-apply v:term-var rel:relation-name arg:term ...)
(specialize-partial-apply v:term-var rel:relation-name arg:term ...)
(finish-apply v:term-var rel:relation-name arg:term ...)
(fallback body:goal)
(fallback fb:goal body:goal)
(gather body:goal)
(staged g:goal)
(time-staged g:goal)
(later g:goal)
fail
(#%rel-app r:relation-name arg:term ...)
(#%rel-app/fallback r:id arg:term ...)
(~> (r:id arg ...)
#'(#%rel-app r arg ...)))
(host-interface/definition
(defrel (r:relation-name arg:term-var ...)
g:goal ...+)
#:binding [(export r) (scope (bind arg) g)]
#:lhs
[(register-simple-rel! #'r 'runtime (attribute arg))
#'r]
#:rhs
[#'(lambda (arg ...)
(i:relation-body
(compile-runtime-goal g) ...))])
(host-interface/definition
(defrel/staged (r:relation-name arg:term-var ...)
g:goal ...+)
#:binding [(export r) (scope (bind arg) g)]
#:lhs
[#:with r-generator (name-generator! #'r)
(register-simple-rel! #'r 'multistage (attribute arg))
#'(r-generator r)]
#:rhs
[#:with (gl ...) (lift-fallbacks! (attribute g) #'r (attribute arg))
#'(values
(lambda (arg ...)
(i:relation-body
(compile-now-goal gl) ...))
(lambda (arg ...)
(i:relation-body
(compile-now-for-runtime-goal gl) ...)))])
(host-interface/definition
(defrel-partial
(r:relation-name rep:term-var [now-arg:term-var ...+] [later-arg:term-var ...+])
g:goal ...)
#:binding [(export r) (scope (bind rep now-arg later-arg) g)]
#:lhs
[(symbol-table-set!
relation-info #'r
(partial-rel 'runtime (length (attribute now-arg)) (length (attribute later-arg))))
#'r]
#:rhs
[#'(lambda (rep now-arg ... later-arg ...)
(i:relation-body
(compile-runtime-goal g) ...))])
(host-interface/definition
(defrel-partial/staged
(r:relation-name rep:term-var [now-arg:term-var ...+] [later-arg:term-var ...+])
g:goal ...)
#:binding [(export r) (scope (bind rep now-arg later-arg) g)]
#:lhs
[#:with r-generator (name-generator! #'r)
(symbol-table-set!
relation-info #'r
(partial-rel 'multistage (length (attribute now-arg)) (length (attribute later-arg))))
#'(r-generator r)]
#:rhs
[#:with (gl ...) (lift-fallbacks! (attribute g) #'r (append (attribute now-arg) (attribute later-arg)))
#'(values
(lambda (rep now-arg ... later-arg ...)
(i:relation-body
(compile-now-goal gl) ...))
(lambda (rep now-arg ... later-arg ...)
(i:relation-body
(compile-now-for-runtime-goal gl) ...)))])
(host-interface/expression
(run n:racket-expr (q:term-var ...+) g:goal ...+)
#:binding (scope (bind q) g)
#'(i:run n (q ...) (compile-runtime-goal g) ...))
(host-interface/expression
(run* (q:term-var ...+) g:goal ...+)
#:binding (scope (bind q) g)
#'(i:run* (q ...) (compile-runtime-goal g) ...)))
(begin-for-syntax
(require syntax/transformer)
(define term-var-transformer
(make-variable-like-transformer
(lambda (id)
#`(term-variable #,id)))))
(define-syntax compile-term
(syntax-parser
#:literals (quote cons list #%term-var racket-term)
[(_ (#%term-var x:id))
#'x]
[(_ (racket-term e))
;; Disable access to term variables right now because of bug in combination
;; with syntax-local-lift-expression in staged
#'(with-reference-compilers (#;[term-var term-var-transformer])
(unwrap-term e #'e))]
[(_ (quote t))
#'(quote t)]
[(_ (cons t1 t2))
#'(cons (compile-term t1) (compile-term t2))]
[(_ (list t ...))
#'(list (compile-term t) ...)]))
(begin-for-syntax
(define-syntax-class binary-constraint
#:literal-sets (goal-literals)
(pattern ==
#:attr c #'i:==
#:attr l #'i:l==)
(pattern =/=
#:attr c #'i:=/=
#:attr l #'i:l=/=)
(pattern absento
#:attr c #'i:absento
#:attr l #'i:labsento))
(define-syntax-class unary-constraint
#:literal-sets (goal-literals)
(pattern symbolo
#:attr c #'i:symbolo
#:attr l #'i:lsymbolo)
(pattern numbero
#:attr c #'i:numbero
#:attr l #'i:lnumbero)
(pattern stringo
#:attr c #'i:stringo
#:attr l #'i:lstringo))
(define (free-vars-of-binding-form binder-vars body-goals)
(define body-vars
(for/fold ([body-vars (immutable-free-id-set)])
([g body-goals])
(free-id-set-union body-vars (free-vars g))))
(free-id-set-subtract
body-vars
(immutable-free-id-set binder-vars)))
;; TODO: is it valid to use free-identifier equality on these names?
(define (free-vars goal-stx)
(syntax-parse goal-stx
#:literals (#%term-var quote fresh partial-apply specialize-partial-apply)
[(#%term-var x) (immutable-free-id-set (list #'x))]
[(partial-apply r _ arg ...)
(free-id-set-add
(free-vars #'(arg ...))
#'r)]
[(specialize-partial-apply r _ arg ...)
(free-id-set-add
(free-vars #'(arg ...))
#'r)]
[(quote _) (immutable-free-id-set)]
[(fresh (x ...) g ...)
(free-vars-of-binding-form (attribute x) (attribute g))]
[(a . d)
(free-id-set-union (free-vars #'a) (free-vars #'d))]
[_ (immutable-free-id-set)]))
(define (lift-fallbacks! now-goals rel-name rel-args)
(define counter!
(let ([val 0])
(lambda ()
(set! val (+ 1 val))
val)))
(define (recur now-goal)
(syntax-parse now-goal
#:literal-sets (goal-literals)
[(fallback body)
#:with (var ...) (append rel-args
(free-id-set->list
(free-id-set-subtract
(free-vars (attribute body))
(immutable-free-id-set rel-args))))
#:with fallback-name (format-id #f "~a/~a" rel-name (counter!))
#:with bodyl (recur (attribute body))
#:with lifted (syntax-local-lift-expression
#'(lambda (var ...) (compile-now-for-runtime-goal bodyl)))
#'(fallback (later (#%rel-app/fallback fallback-name lifted (#%term-var var) ...)) bodyl)]
[(fallback fb body)
#:with bodyl (recur (attribute body))
#'(fallback fb bodyl)]
[(fresh (x:id ...) g ...)
#:with (gl ...) (map recur (attribute g))
#'(fresh (x ...) gl ...)]
[(conde [g ...] ...)
#:with ([gl ...] ...) (map (lambda (l) (map recur l)) (attribute g))
#'(conde [gl ...] ...)]
[(gather body)
#:with bodyl (recur (attribute body))
#'(gather bodyl)]
[_ this-syntax]))
(map recur now-goals)))
(define-syntax compile-runtime-goal
(syntax-parser
#:literal-sets (goal-literals)
[(_ (trace id x ...))
#'(i:project (x ...)
(begin
(displayln (list 'id x ...))
i:succeed))]
[(_ (#%rel-app r:id arg ...))
(check-simple-rel #'r 'runtime (attribute arg))
#'(r (compile-term arg) ...)]
[(_ (#%rel-app/fallback r:id fn:id arg ...))
#'(fn (compile-term arg) ...)]
[(_ (partial-apply v:id rel:id arg ...))
(match (symbol-table-ref relation-info #'rel)
[(partial-rel stage now-args-count later-args-count)
(when (not (= now-args-count (length (attribute arg))))
(raise-syntax-error #f "wrong number of now-stage arguments to relation" #'r))
(with-syntax ([(later-placeholders ...) (make-list later-args-count #'_)])
#'(i:partial-apply v (rel ((compile-term arg) ...) (later-placeholders ...))))]
[_ (raise-syntax-error #f "partial-apply expects relation defined by defrel-partial" #'r)])]
[(_ (~and stx (specialize-partial-apply v:id rel:id arg ...)))
(raise-syntax-error #f "not allowed in runtime goal" #'stx)]
[(_ (finish-apply v:id rel:id arg ...))
(match (symbol-table-ref relation-info #'rel)
[(partial-rel stage now-args-count later-args-count)
(when (not (= later-args-count (length (attribute arg))))
(raise-syntax-error #f "wrong number of later-stage arguments to relation" #'r))
(with-syntax ([(now-placeholders ...) (make-list now-args-count #'_)])
#'(i:finish-apply v (rel (now-placeholders ...) ((compile-term arg) ...))))]
[_ (raise-syntax-error #f "finish-apply expects relation defined by defrel-partial" #'r)])]
[(_ (constraint:binary-constraint t1 t2))
#'(constraint.c (compile-term t1) (compile-term t2))]
[(_ (constraint:unary-constraint t))
#'(constraint.c (compile-term t))]
[(_ (fresh (x:id ...) g ...))
#'(i:fresh (x ...) (compile-runtime-goal g) ...)]
[(_ (conde [g ...] ...))
#'(i:conde [(compile-runtime-goal g) ...] ...)]
[(_ fail)
#'i:fail]
[(_ (staged g))
#:with (var ...) (free-id-set->list (free-vars #'g))
#:with (gl) (lift-fallbacks! (list #'g) #'staged '())
#:with staged-f (syntax-local-lift-expression #'(i:generate-staged (var ...) (compile-now-goal gl)))
#'(staged-f var ...)]
[(_ (time-staged g))
#:with (var ...) (free-id-set->list (free-vars #'g))
#:with (gl) (lift-fallbacks! (list #'g) #'staged '())
#:with staged-f (syntax-local-lift-expression #'(time (i:generate-staged (var ...) (compile-now-goal gl))))
#'(staged-f var ...)]
[(_ (~and stx (~or (later . _) (fallback . _) (gather . _))))
(raise-syntax-error #f "not allowed in runtime goal" #'stx)]
[_ (raise-syntax-error #f "unexpected goal syntax" this-syntax)]))
(define-syntax compile-now-goal
(syntax-parser
#:literal-sets (goal-literals)
[(_ (trace id x ...))
;(error 'compile-now-goal "TODO not supported")
#'(i:project (x ...)
(begin
(displayln (list 'id x ...))
i:succeed))]
[(_ (#%rel-app r:id arg ...))
(check-simple-rel #'r 'staging-time (attribute arg))
(define/syntax-parse r-generator (reference-generator #'r))
#'(r-generator (compile-term arg) ...)]
[(_ (~and stx (partial-apply v:id rel:id arg ...)))
(raise-syntax-error #f "partial-apply not supported in staging-time code" #'stx)]
[(_ (specialize-partial-apply v:id rel:id arg ...))
(match (symbol-table-ref relation-info #'rel)
[(partial-rel stage now-args-count later-args-count)
(when (not (= now-args-count (length (attribute arg))))
(raise-syntax-error #f "wrong number of now-stage arguments to relation" #'r))
(with-syntax ([(later-placeholders ...) (make-list later-args-count #'_)]
[gen-rel (reference-generator #'rel)])
#'(i:specialize-partial-apply v (gen-rel rel ((compile-term arg) ...) (later-placeholders ...))))]
[_ (raise-syntax-error #f "specialize-partial-apply expects relation defined by defrel-partial" #'r)])]
[(_ (== t1 t2))
#'(i:==/staging-time (compile-term t1) (compile-term t2))]
[(_ (constraint:binary-constraint t1 t2))
#'(constraint.c (compile-term t1) (compile-term t2))]
[(_ (constraint:unary-constraint t))
#'(constraint.c (compile-term t))]
[(_ (fresh (x:id ...) g ...))
#'(i:fresh (x ...) (compile-now-goal g) ...)]
[(_ (conde [g ...] ...))
#'(i:conde [(compile-now-goal g) ...] ...)]
[(_ (fallback fb body))
#'(i:fallback
(compile-now-goal fb)
(compile-now-goal body))]
[(_ (gather body))
#'(i:gather (lambda () (compile-now-goal body)))]
[(_ fail) #'i:fail]
[(_ (later g))
#'(compile-later-goal g)]
[(_ (~and stx (~or (finish-apply . _)
(staged . _))))
(raise-syntax-error #f "not supported in staging-time code" #'stx)]
[_ (raise-syntax-error #f "unexpected goal syntax" this-syntax)]))
(define-syntax compile-now-for-runtime-goal
(lambda (stx)
(define/syntax-parse (_ now-goal) stx)
(syntax-parse stx
#:literal-sets (goal-literals)
[(_ (trace id x ...))
#'(compile-runtime-goal now-goal)]
[(_ (#%rel-app r:id arg ...))
#'(compile-runtime-goal now-goal)]
[(_ (#%rel-app/fallback r:id fn:id arg ...))
#'(compile-runtime-goal now-goal)]
[(_ (~and stx (partial-apply v:id rel:id arg ...)))
(raise-syntax-error #f "partial-apply not supported in staging-time code" #'stx)]
[(_ (specialize-partial-apply v:id rel:id arg ...))
#'(compile-runtime-goal (partial-apply v rel arg ...))]
[(_ (== t1 t2))
#'(compile-runtime-goal now-goal)]
[(_ (constraint:binary-constraint t1 t2))
#'(compile-runtime-goal now-goal)]
[(_ (constraint:unary-constraint t))
#'(compile-runtime-goal now-goal)]
[(_ (fresh (x:id ...) g ...))
#'(i:fresh (x ...) (compile-now-for-runtime-goal g) ...)]
[(_ (conde [g ...] ...))
#'(i:conde [(compile-now-for-runtime-goal g) ...] ...)]
[(_ (~and stx (fallback fb body)))
#;(raise-syntax-error #f "fallback not supported in multistage code" #'stx)
#'(compile-now-for-runtime-goal fb)]
[(_ (gather body))
#'(compile-now-for-runtime-goal body)]
[(_ fail)
#'(compile-runtime-goal now-goal)]
[(_ (later g))
#'(compile-runtime-goal g)]
[(_ (~and stx (~or (finish-apply . _)
(staged . _))))
(raise-syntax-error #f "not supported in generator code" #'stx)]
[_ (raise-syntax-error #f "unexpected goal syntax" this-syntax)])))
(define-syntax compile-later-goal
(syntax-parser
#:literal-sets (goal-literals)
[(_ (#%rel-app r:id arg ...))
(check-simple-rel #'r 'runtime (attribute arg))
#'(i:lapp r (compile-term arg) ...)]
[(_ (#%rel-app/fallback r:id fn:id arg ...))
#'(i:linvoke-fallback r fn (compile-term arg) ...)]
[(_ (partial-apply v:id rel:id arg ...))
(match (symbol-table-ref relation-info #'rel)
[(partial-rel stage now-args-count later-args-count)
(when (not (= now-args-count (length (attribute arg))))
(raise-syntax-error #f "wrong number of now-stage arguments to relation" #'r))
(with-syntax ([(later-placeholders ...) (make-list later-args-count #'_)])
#'(i:lpartial-apply v (rel ((compile-term arg) ...) (later-placeholders ...))))]
[_ (raise-syntax-error #f "partial-apply expects relation defined by defrel-partial" #'r)])]
[(_ (~and stx (specialize-partial-apply v:id rel:id arg ...)))
(raise-syntax-error #f "not supported in generated code" #'stx)]
[(_ (finish-apply v:id rel:id arg ...))
(match (symbol-table-ref relation-info #'rel)
[(partial-rel stage now-args-count later-args-count)
(when (not (= later-args-count (length (attribute arg))))
(raise-syntax-error #f "wrong number of later-stage arguments to relation" #'r))
(with-syntax ([(now-placeholders ...) (make-list now-args-count #'_)])
#'(i:lfinish-apply v (rel (now-placeholders ...) ((compile-term arg) ...))))]
[_ (raise-syntax-error #f "finish-apply expects relation defined by defrel-partial" #'r)])]
[(_ (constraint:binary-constraint t1 t2))
#'(constraint.l (compile-term t1) (compile-term t2))]
[(_ (constraint:unary-constraint t))
#'(constraint.l (compile-term t))]
[(_ (fresh (x:id ...) g ...))
#'(i:fresh (x ...) (compile-later-goal g) ...)]
[(_ (conde [g ...] ...))
#'(i:gather
(lambda ()
(i:conde
[(compile-later-goal g) ...] ...)))]
[(_ fail) #'i:lfail]
[(_ (~and stx (~or (fallback . _)
(gather . _)
(staged . _)
(later . _))))
(raise-syntax-error #f "not supported in generated code" #'stx)]
[_ (raise-syntax-error #f "unexpected goal syntax" this-syntax)]))
(define-dsl-syntax quasiquote term-macro
(syntax-parser
[(~describe
"`<datum>"
(_ q))
(let recur ([stx #'q] [level 0])
(syntax-parse stx #:datum-literals (unquote quasiquote)
[(unquote e)
(if (= level 0)
#'e
#`(cons (quote unquote) #,(recur #'(e) (- level 1))))]
[(unquote . rest)
(raise-syntax-error 'unquote "bad unquote syntax" stx)]
[(quasiquote e)
#`(cons (quote quasiquote) #,(recur #'(e) (+ level 1)))]
[(a . d)
#`(cons #,(recur #'a level) #,(recur #'d level))]
[(~or* v:identifier v:number v:boolean v:string) #'(quote v)]
[() #'(quote ())]))]))
(require syntax-spec-v2/private/ee-lib/errors racket/match)
(struct term-variable [value])
(define (unwrap-term v blame-stx)
(match v
[atom
#:when (or (symbol? atom)
(string? atom)
(number? atom)
(null? atom)
(boolean? atom))
atom]
[(term-variable val)
val]
[(cons a d)
(cons (unwrap-term a blame-stx) (unwrap-term d blame-stx))]
[_ (raise-argument-error/stx 'term "term-or-term-variable?" v blame-stx)]))
(define-syntax define-term-syntax-rule
(syntax-parser
[(_ (name . pat)
template)
#'(define-dsl-syntax name term-macro
(syntax-rules ()
[(_ . pat)
template]))]))
(define-syntax-rule
(defrel/staged/fallback (name arg ...) body ...)
(defrel/staged (name arg ...)
(fallback (fresh () body ...))))
(define-syntax-rule
(defrel-partial/staged/fallback (r rep [now-arg ...] [later-arg ...]) body ...)
(defrel-partial/staged (r rep [now-arg ...] [later-arg ...])
(fallback (fresh () body ...))))