How do I provide a name as both a match expander and contracted function?

22 views
Skip to first unread message

Jack Firth

unread,
Dec 30, 2019, 10:07:43 PM12/30/19
to Racket Users
If I'm making my own data types, one common thing I want to do is make smart constructors that double as match expanders. I could use `define-match-expander` with two transformers, but then I can't add a contract to the smart constructor with `contract-out`. If I try to write this:

(provide
 (contract-out
  [foo (->* (widget?) (#:name symbol?) foo?)]))

(define (make-foo widget #:name [name #f])
  ...)

(define-match-expander foo
  (syntax-parser [(_ widget) #'(? foo? (app foo-widget widget))])
  (syntax-parser [(_ arg ...) #'(make-foo arg ...)]))

...then the use of `contract-out` hides the fact that `foo` is a match expander. What should I do?

Sorawee Porncharoenwase

unread,
Dec 30, 2019, 10:33:49 PM12/30/19
to Jack Firth, Racket Users
IIUC, this is what Alex Knauth did in https://github.com/mbutterick/txexpr/pull/8.

--
You received this message because you are subscribed to the Google Groups "Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to racket-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/2dac85e9-8058-437c-b5bc-a92f659c02f6%40googlegroups.com.

Eric Griffis

unread,
Dec 31, 2019, 2:11:30 PM12/31/19
to Racket Users, Jack Firth
Hi Jack,

It can be done with make-rename-transformer.

(define (foo? obj) (equal? obj foo-impl))
(define foo-widget list)

(define/contract (foo-impl _) (-> number? symbol?) 'a-foo)


(define-match-expander foo
  (syntax-parser [(_ widget) #'(? foo? (app foo-widget widget))])
  (make-rename-transformer #'foo-impl))

; (foo #t) ; contract violation

(foo 123)

Eric


On Mon, Dec 30, 2019 at 7:07 PM Jack Firth <jackh...@gmail.com> wrote:
--
Reply all
Reply to author
Forward
0 new messages