-
Notifications
You must be signed in to change notification settings - Fork 9
/
interact.scm
52 lines (45 loc) · 1.75 KB
/
interact.scm
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
(load "query.scm")
;; To use this interactor, first supply a query/hint, then input commands.
;; Supported command patterns:
;; good-path: Show expansion path of next ground-truth step.
;; steps-remaining: List expansion paths of remaining ground-truth steps.
;; ((or 0 1) ...): Follow path to recursive constraint and expand it.
;; A path is a list of binary digits, where 0 means left and 1 means right.
(define (in)
(define request (read))
(cond
((or (eq? 'good-path request)
(eq? 'steps-remaining request)
(and (pair? request)
(eq? 'jump-to-steps-remaining (car request))))
request)
((pair? request)
(map (lambda (n)
(cond
((= 0 n) #t)
((= 1 n) #f)
(else (error 'in (format "invalid path segment ~s" n)))))
request))
((eof-object? request) (exit))
(else (error 'in (format "unexpected request: ~s" request)))))
(define (show ss)
(printf "~s\n" (cadr (stream-pretty ss)))
;; Use pretty-print instead of printf for saner debugging.
;(pretty-print (cadr (stream-pretty ss)))
)
(define (out response)
(define (bool->bit b) (if b 0 1))
(define (bools->bits bs) (map bool->bit bs))
(define output
(cond
((eq? 'good-path (car response)) (bools->bits (cadr response)))
((eq? 'follow-path (car response)) (cadr response))
((eq? 'steps-remaining (car response)) (map bools->bits (cadr response)))
(else (error 'out (format "unrecognized output: ~s" response)))))
(printf "~s\n" output))
(define (read-query/hint) (eval (read)))
(define ss/hint (read-query/hint))
(define hint (car ss/hint))
(define ss (cadr ss/hint))
;; See the definition in transparent.scm for details.
(interact in show out hint (clean ss) #f #t)