Skip to content

Commit

Permalink
Fully replace #%deforestable with the new implementation
Browse files Browse the repository at this point in the history
This replaces the original implementation of the `#%deforestable` core
form with the new one that had been named `#%deforestable2` during the
process of incrementally integrating the proof-of-concept for this
scheme. With that complete, this now gets rid of the hardcoded codegen
for the `qi/list` forms in the Qi compiler, instead replacing it with
the ability to specify this in the `define-deforestable` macro which
then conveys the codegen through to the final stage of compilation via
a compile-time datatype.

This also removes some tests that are no longer relevant and updates
others.

The recent Qi meeting notes provide more context on this approach.
  • Loading branch information
countvajhula committed Nov 23, 2024
1 parent 65f180a commit 1b33ca5
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 96 deletions.
50 changes: 5 additions & 45 deletions qi-lib/flow/core/compiler/1000-qi0.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@
[(~datum appleye)
#'call]
[e:clos-form (clos-parser #'e)]
[e:deforestable2-form (deforestable2-parser #'e)]
[e:deforestable-form (deforestable-parser #'e)]
;; escape hatch for racket expressions or anything
;; to be "passed through"
Expand Down Expand Up @@ -393,58 +392,19 @@ the DSL.
(qi0->racket (~> (-< (~> (gen args) △) _)
onex))))]))

(define (deforestable2-clause-parser c)
(define (deforestable-clause-parser c)
(syntax-parse c
[((~datum f) e) #'(qi0->racket e)]
[((~datum e) e) #'e]))

(define (deforestable2-parser e)
(define (deforestable-parser e)
(syntax-parse e
#:datum-literals (#%deforestable2)
[(#%deforestable2 _name info c ...)
(let ([es^ (map deforestable2-clause-parser (attribute c))])
#:datum-literals (#%deforestable)
[(#%deforestable _name info c ...)
(let ([es^ (map deforestable-clause-parser (attribute c))])
(match-let ([(deforestable-info codegen) (syntax-local-value #'info)])
(apply codegen es^)))]))

(define (deforestable-parser stx)
(syntax-parse stx
[((~datum #%deforestable) (~datum filter) (proc:clause))
#'(lambda (v)
(filter (qi0->racket proc) v))]
[((~datum #%deforestable) (~datum filter-map) (proc:clause))
#'(lambda (v)
(filter-map (qi0->racket proc) v))]
[((~datum #%deforestable) (~datum map) (proc:clause))
#'(lambda (v)
(map (qi0->racket proc) v))]
[((~datum #%deforestable) (~datum foldl) (proc:clause) (init:expr))
#'(lambda (v)
(foldl (qi0->racket proc) init v))]
[((~datum #%deforestable) (~datum foldr) (proc:clause) (init:expr))
#'(lambda (v)
(foldr (qi0->racket proc) init v))]
[((~datum #%deforestable) (~datum range) () (arg:expr ...))
#'(lambda ()
(range arg ...))]
[((~datum #%deforestable) (~datum take) () (n:expr))
#'(lambda (v)
(take v n))]
[((~datum #%deforestable) (~datum car))
#'car]
[((~datum #%deforestable) (~datum cadr))
#'cadr]
[((~datum #%deforestable) (~datum caddr))
#'caddr]
[((~datum #%deforestable) (~datum cadddr))
#'cadddr]
[((~datum #%deforestable) (~datum list-ref) () (n:expr))
#'(lambda (v)
(list-ref v n))]
[((~datum #%deforestable) (~datum length))
#'length]
[((~datum #%deforestable) (~or* (~datum empty?) (~datum null?)))
#'empty?]))

(define (blanket-template-form-parser stx)
(syntax-parse stx
;; "prarg" = "pre-supplied argument"
Expand Down
50 changes: 25 additions & 25 deletions qi-lib/flow/core/compiler/deforest/syntax.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
(define-syntax-class fsp-range
#:attributes (blanket? fine? arg pre-arg post-arg)
#:literal-sets (fs-literals)
#:datum-literals (#%deforestable2 range)
(pattern (#%deforestable2 range _info ((~datum e) the-arg) ...)
#:datum-literals (#%deforestable range)
(pattern (#%deforestable range _info ((~datum e) the-arg) ...)
#:attr arg #'(the-arg ...)
#:attr pre-arg #f
#:attr post-arg #f
Expand Down Expand Up @@ -78,29 +78,29 @@
(define-syntax-class fst-filter
#:attributes (f)
#:literal-sets (fs-literals)
#:datum-literals (#%deforestable2 filter)
(pattern (#%deforestable2 filter _info ((~datum f) f-uncompiled))
#:datum-literals (#%deforestable filter)
(pattern (#%deforestable filter _info ((~datum f) f-uncompiled))
#:attr f (run-passes #'f-uncompiled)))

(define-syntax-class fst-map
#:attributes (f)
#:literal-sets (fs-literals)
#:datum-literals (#%deforestable2 map)
(pattern (#%deforestable2 map _info ((~datum f) f-uncompiled))
#:datum-literals (#%deforestable map)
(pattern (#%deforestable map _info ((~datum f) f-uncompiled))
#:attr f (run-passes #'f-uncompiled)))

(define-syntax-class fst-filter-map
#:attributes (f)
#:literal-sets (fs-literals)
#:datum-literals (#%deforestable2 filter-map)
(pattern (#%deforestable2 filter-map _info ((~datum f) f-uncompiled))
#:datum-literals (#%deforestable filter-map)
(pattern (#%deforestable filter-map _info ((~datum f) f-uncompiled))
#:attr f (run-passes #'f-uncompiled)))

(define-syntax-class fst-take
#:attributes (n)
#:literal-sets (fs-literals)
#:datum-literals (#%deforestable2 take)
(pattern (#%deforestable2 take _info ((~datum e) n))))
#:datum-literals (#%deforestable take)
(pattern (#%deforestable take _info ((~datum e) n))))

(define-syntax-class fst-syntax0
(pattern (~or _:fst-filter
Expand All @@ -123,8 +123,8 @@
(define-syntax-class fsc-foldr
#:attributes (op init)
#:literal-sets (fs-literals)
#:datum-literals (#%deforestable2 foldr)
(pattern (#%deforestable2
#:datum-literals (#%deforestable foldr)
(pattern (#%deforestable
foldr
_info
((~datum f) op-uncompiled)
Expand All @@ -134,8 +134,8 @@
(define-syntax-class fsc-foldl
#:attributes (op init)
#:literal-sets (fs-literals)
#:datum-literals (#%deforestable2 foldl)
(pattern (#%deforestable2
#:datum-literals (#%deforestable foldl)
(pattern (#%deforestable
foldl
_info
((~datum f) op-uncompiled)
Expand All @@ -144,18 +144,18 @@

(define-syntax-class cad*r-datum
#:attributes (countdown)
#:datum-literals (#%deforestable2 car cadr caddr cadddr)
(pattern (#%deforestable2 car _info) #:attr countdown #'0)
(pattern (#%deforestable2 cadr _info) #:attr countdown #'1)
(pattern (#%deforestable2 caddr _info) #:attr countdown #'2)
(pattern (#%deforestable2 cadddr _info) #:attr countdown #'3))
#:datum-literals (#%deforestable car cadr caddr cadddr)
(pattern (#%deforestable car _info) #:attr countdown #'0)
(pattern (#%deforestable cadr _info) #:attr countdown #'1)
(pattern (#%deforestable caddr _info) #:attr countdown #'2)
(pattern (#%deforestable cadddr _info) #:attr countdown #'3))

(define-syntax-class fsc-list-ref
#:attributes (pos name)
#:literal-sets (fs-literals)
#:datum-literals (#%deforestable2 list-ref)
#:datum-literals (#%deforestable list-ref)
;; TODO: need #%host-expression wrapping idx?
(pattern (#%deforestable2 list-ref _info ((~datum e) idx))
(pattern (#%deforestable list-ref _info ((~datum e) idx))
#:attr pos #'idx
#:attr name #'list-ref)
;; TODO: bring wrapping #%deforestable out here?
Expand All @@ -165,13 +165,13 @@

(define-syntax-class fsc-length
#:literal-sets (fs-literals)
#:datum-literals (#%deforestable2 length)
(pattern (#%deforestable2 length _info)))
#:datum-literals (#%deforestable length)
(pattern (#%deforestable length _info)))

(define-syntax-class fsc-empty?
#:literal-sets (fs-literals)
#:datum-literals (#%deforestable2 empty?) ; note: null? expands to empty?
(pattern (#%deforestable2 empty? _info)))
#:datum-literals (#%deforestable empty?) ; note: null? expands to empty?
(pattern (#%deforestable empty? _info)))

(define-syntax-class fsc-default
#:datum-literals (cstream->list)
Expand Down
7 changes: 1 addition & 6 deletions qi-lib/flow/core/syntax.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
fold-right-form
loop-form
clos-form
deforestable-form
deforestable2-form)
deforestable-form)

(require syntax/parse)

Expand Down Expand Up @@ -140,7 +139,3 @@ See comments in flow.rkt for more details.
(define-syntax-class deforestable-form
(pattern
((~datum #%deforestable) arg ...)))

(define-syntax-class deforestable2-form
(pattern
((~datum #%deforestable2) arg ...)))
5 changes: 1 addition & 4 deletions qi-lib/flow/extended/expander.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,7 @@ core language's use of #%app, etc.).
(esc ex:racket-expr)

;; core form to express deforestable operations
(#%deforestable name:id (f:closed-floe ...) (arg:racket-expr ...))
(#%deforestable name:id (f:closed-floe ...+))
(#%deforestable name:id)
(#%deforestable2 name:id info:id e:deforestable-clause ...)
(#%deforestable name:id info:id e:deforestable-clause ...)

;; backwards compat macro extensibility via Racket macros
(~> ((~var ext-form (starts-with "qi:")) expr ...)
Expand Down
6 changes: 3 additions & 3 deletions qi-lib/macro.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
(only-in "flow/extended/expander.rkt"
qi-macro
esc
#%deforestable2)
#%deforestable)
qi/flow/space
(for-syntax qi/flow/aux-syntax)
syntax/parse/define
Expand Down Expand Up @@ -124,10 +124,10 @@
#:fail-unless (= (length (attribute e))
(length (attribute arg-name)))
"Wrong number of arguments!"
#`(#%deforestable2 #,name #,info [tag e] ...)]
#`(#%deforestable #,name #,info [tag e] ...)]
;; TODO, check: instead of `car`, does `(car)` produce
;; a useful syntax error?
[_:id #`(#%deforestable2 #,name #,info)])])))
[_:id #`(#%deforestable #,name #,info)])])))

(define-syntax define-deforestable
(syntax-parser
Expand Down
7 changes: 4 additions & 3 deletions qi-test/tests/expander.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,11 @@
(#%host-expression 1)
__))))
(test-expand "#%deforestable"
#'(#%deforestable name (_) (_))
#'(#%deforestable name info (f 0) (e 0))
#'(#%deforestable name
(_)
((#%host-expression _)))))
info
(f (gen (#%host-expression 0)))
(e (#%host-expression 0)))))
(test-suite
"utils"
;; this is just temporary until we properly track source expressions through
Expand Down
10 changes: 0 additions & 10 deletions qi-test/tests/flow.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -285,16 +285,6 @@
(check-equal? ((☯ (esc (first (list + *)))) 3 7)
10
"normal racket expressions"))
(test-suite
"#%deforestable"
(check-equal? ((☯ (#%deforestable filter (odd?))) (list 1 2 3)) (list 1 3))
(check-equal? ((☯ (#%deforestable map (sqr))) (list 1 2 3)) (list 1 4 9))
(check-equal? ((☯ (#%deforestable foldl (+) (0))) (list 1 2 3)) 6)
(check-equal? ((☯ (#%deforestable foldr (+) (0))) (list 1 2 3)) 6)
(check-equal? ((☯ (#%deforestable range () (3)))) (list 0 1 2))
(check-equal? ((☯ (#%deforestable range () (0 3)))) (list 0 1 2))
(check-equal? ((☯ (#%deforestable range () (0 5 2)))) (list 0 2 4))
(check-equal? ((☯ (#%deforestable take () (2))) (list 1 2 3)) (list 1 2)))
(test-suite
"elementary boolean gates"
(test-suite
Expand Down

0 comments on commit 1b33ca5

Please sign in to comment.