[ANNOUNCE] New package typed-compose

50 views
Skip to first unread message

unlimitedscolobb

unread,
Jan 4, 2021, 3:52:11 PM1/4/21
to Racket Users
Hello,

I am glad to announce typed-compose, a small package defining some utilities for composing functions in Typed Racket:


Typed Racket's compose only takes two arguments, because in general it is difficult to specify that the return types and the argument types should be the same for two successive functions in the argument list. This package defines some further utilities to allow compose-ing more than two functions more comfortable in Typed Racket.

This is my first ever Racket package, so I'm taking all kinds of feedback.

-
Sergiu

philngu...@gmail.com

unread,
Jan 10, 2021, 12:00:58 AM1/10/21
to Racket Users
Nice package. I don't have an account and don't know how to do pull request on Marvid thing, but I suggest making a macro generating `compose-n` for arbitrary (statically known) n, and export it along side with the predefined compose-n functions, something along these lines:

#lang typed/racket/base

(provide make-compose
         compose-3 compose-4)

(require (for-syntax racket/base
                     racket/match
                     racket/list
                     racket/syntax
                     syntax/parse))

(define-for-syntax (make-compose-type n)
  (with-syntax* ([(t ...) (generate-temporaries (make-list n 't))]
                 [a (generate-temporary 'a)]
                 [(_ ... t₀) #'(a t ...)]
                 [(F ...)
                  (let step ([u #'a] [ts (syntax->list #'(t ...))])
                    (match ts
                      ['() '()]
                      [(cons t ts*) (cons #`(#,t → #,u) (step t ts*))]))])
    #'(∀ (a t ...) (F ... → t₀ → a))))

(define-syntax make-compose
  (syntax-parser
    [(_ n:nat)
     (with-syntax* ([(f ...) (generate-temporaries (make-list (syntax-e #'n) 'f))]
                    [x (generate-temporary 'x)]
                    [T (make-compose-type (syntax-e #'n))]
                    [body (foldr (λ (fᵢ res) #`(#,fᵢ #,res)) #'x (syntax->list #'(f ...)))])
       #'(ann (λ (f ...) (λ (x) body)) T))]))

(define compose-3 (make-compose 3))
(define compose-4 (make-compose 4))
;; and so on



unlimitedscolobb

unread,
Jan 10, 2021, 1:00:29 PM1/10/21
to Racket Users
Thank you for the feedback!

I think this macro is a very nice idea and should indeed allow removing most compose-i functions, except maybe for compose-3 and compose-4, which can be examples of use of make-compose.

I added a Contributing section to the README of the Git repo: https://git.marvid.fr/scolobb/typed-compose#contributing . Do you think you can make the modifications directly a git clone of my repository and submit the changes to me as patches?

-
Sergiu
Reply all
Reply to author
Forward
0 new messages