Skip to content

Commit

Permalink
Implement foldl as a stream
Browse files Browse the repository at this point in the history
Fix the syntax class to retain a reference to the input syntax in both
foldl as well as foldr. Add a WIP tests for `foldl`.

(WIP from today's meeting)
  • Loading branch information
countvajhula committed Sep 30, 2023
1 parent 8fc8424 commit 17c3bc5
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 7 deletions.
19 changes: 14 additions & 5 deletions qi-lib/flow/core/compiler.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,22 @@
(define-syntax-class fusable-fold-operation
#:attributes (op init end)
#:datum-literals (#%host-expression #%partial-application)
(pattern (#%partial-application
((#%host-expression (~literal foldr))
(#%host-expression op)
(#%host-expression init)))
(pattern (~and (#%partial-application
((#%host-expression (~literal foldr))
(#%host-expression op)
(#%host-expression init)))
stx)
#:do [(define chirality (syntax-property #'stx 'chirality))]
#:when (and chirality (eq? chirality 'right))
#:attr end #'(foldr-cstream op init))
(pattern (~and (#%partial-application
((#%host-expression (~literal foldl))
(#%host-expression op)
(#%host-expression init)))
stx)
#:do [(define chirality (syntax-property #'stx 'chirality))]
#:when (and chirality (eq? chirality 'right))
#:attr end #'(foldr-cstream op init)))
#:attr end #'(foldl-cstream op init)))

(define-syntax-class non-fusable
(pattern (~not _:fusable-list-operation)))
Expand Down
12 changes: 11 additions & 1 deletion qi-lib/flow/core/impl.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
list->cstream-next
map-cstream-next
filter-cstream-next
foldr-cstream)
foldr-cstream
foldl-cstream)

(require racket/match
(only-in racket/function
Expand Down Expand Up @@ -264,6 +265,15 @@
(op value (loop state))))
state))))

(define-inline (foldl-cstream op init next)
(λ (state)
(let loop ([acc init] [state state])
((next (λ () acc)
(λ (state) (loop acc state))
(λ (value state)
(loop (op value acc) state)))
state))))

(define-inline (list->cstream-next done skip yield)
(λ (state)
(cond [(null? state) (done)]
Expand Down
23 changes: 23 additions & 0 deletions qi-test/tests/compiler.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,29 @@
list->cstream-next))
lst)))
values)
"deforestation in arbitrary positions")
(check-equal? (syntax->datum
(deforest-rewrite
#'(thread (#%partial-application
((#%host-expression map)
(#%host-expression string-upcase)))
(#%partial-application
((#%host-expression foldl)
(#%host-expression string-append)
(#%host-expression "I"))))))
'(thread
values
(esc
(λ (lst)
((cstream->list
(inline-compose1
(map-cstream-next
sqr)
(filter-cstream-next
odd?)
list->cstream-next))
lst)))
values)
"deforestation in arbitrary positions"))
(test-suite
"fixed point"
Expand Down
8 changes: 7 additions & 1 deletion qi-test/tests/flow.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -1540,7 +1540,13 @@
35)
(check-equal? ((☯ (~>> (filter odd?) (map sqr) (foldl + 0)))
(list 1 2 3 4 5))
35)))))
35)
(check-equal? ((☯ (~>> (map string-upcase) (foldr string-append "I")))
(list "a" "b" "c"))
"ABCI")
(check-equal? ((☯ (~>> (map string-upcase) (foldl string-append "I")))
(list "a" "b" "c"))
"CBAI")))))

(module+ main
(void (run-tests tests)))

0 comments on commit 17c3bc5

Please sign in to comment.