3 hints:
- `type-check` can call any helper functions defined with
`define-for-syntax`
- Turnstile uses `local-expand` to expand any macros in subterms (in your
case, any macros in `x` and `y`)
- start using `syntax-parse` :)

On Tue, Jul 18, 2017 at 6:11 PM, Matthias Felleisen <matth...@ccs.neu.edu>
wrote:

>
> Take a look at the Turnstile package, its documentation, and the paper
> that comes with it (http://www.ccs.neu.edu/racket/pubs/#popl17-ckg).
>
>
> > On Jul 18, 2017, at 5:37 PM, Sam Waxman <samwax...@gmail.com> wrote:
> >
> > Hello,
> >
> > Suppose I'd like a type-checking macro that looks something like this:
> >
> > (define-syntax (type-check stx)
> >  (syntax-case stx ()
> >    [(_ (+ x y))  (if (and (number? (type-check #'x))
> >                           (number? (type-check #'y)))
> >                      #'Number #'(error "bad types!"))]
> >    [*cases for other expressions]))
> >
> > The macro will examine the syntax it's given and determine the type that
> will be produced at runtime.
> >
> > The key here is that type-check needs to call itself, but that's not
> possible. Type-check is defined to be used in phase 0, but the recursive
> call to type-check takes place in phase 1. If the recursive call to
> type-check makes a recursive-call, then this second call would be running
> in phase 2 and so on. The compiler won't allow me to use this macro in a
> phase it's not defined for, so this code is no good.
> >
> > So, first question: How can I get a macro to call itself recursively
> such as in this case? I can't wrap the entire if in a syntax object,
> because I want to throw that error in phase 1, so the condition of the if
> statement needs to be evaluated inside this macro.
> >
> > Second, even if I get type-check to work in all phases, if I call
> type-check with (+ 1 2), the recursive call to (type-check #'x) will get
> #'x as its "argument" instead of 1, no?
> >
> > It almost feels like I should be doing this with a regular old function
> that takes in syntax, and produces a type, but to my knowledge, I can't use
> ... in runtime expressions such as the following
> >
> > (define (type-check stx)
> >  (syntax-case stx ()
> >    [(body) *dummy-value*]
> >    [(body ...) (begin (type-check #'body) ...)]))
> >
> > This will result in a syntax-error telling me that ... can't be used as
> an expression like this, which makes it impossible to deal with ... forms.
> >
> > Any advice?
> >
> > Thanks in advance.
> >
> > --
> > 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+unsubscr...@googlegroups.com.
> > For more options, visit https://groups.google.com/d/optout.
>
> --
> 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+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to