Re: [racket-users] Re: Redex: making a #lang with native lambda, app, etc.

2019-01-03 Thread William J. Bowman
Joey,

An early version of Cur did this (which worked well for an initial model, but 
was terrible for performance and debugging).
I don't have anything written up, but you can try reading the code and pinging 
me with questions:
  
https://github.com/wilbowma/cur/blob/9e575a8c220c0cf399ec3f90e6994f1098b35fb2/cur-lib/cur/curnel/redex-impl/lang.rkt#L2

Basically, each macro (`#%lambda`, `#%app`, ...) wrapped the surface syntax with
`(reflect (term (eval ...)))`, where `eval` runs the Redex reduction relation
and `reflect` tries to prettify Redex quoted terms as surface syntax (and
usually fails; most of the output was still quoted).

It also has implementations of `provide`, `require`, and `module+` which thread
state through modules that `eval` and the type system require.

Hope it helps!

--
William J. Bowman

On Wed, Jan 02, 2019 at 05:47:25PM -0800, Joey Eremondi wrote:
> Thanks for the response!
> 
> That's not quite what I want. That will quote lambdas and applications into 
> my redex models, but I also want it to reduce them by my reduction relation.
> 
> The link I posted discussed how to do that, but only when you write 
> programs in the syntax of the model.
> 
> Sorry for not being clear about the top-interaction. There's nothing 
> specific about top-interaction, I'm having the same problem with 
> module-begin.
> I'm trying to get them both to evaluate the terms using my redex model.
> 
> The local-expand function in the article you give looks promising, but I'm 
> still figuring out how exactly to use it, particularly with the list of 
> expressions for module-begin.
> 
> 
> On Monday, December 31, 2018 at 8:47:49 PM UTC-8, Sorawee Porncharoenwase 
> wrote:
> >
> > I'm a novice too, but here's my attempt to help.
> >  
> >
> >> I'd like to use (define-syntax) to make lambda, #%app, etc. all expand 
> >> into terms in my model.
> >>
> >
> > This works for me.  Is this what you want?
> >
> > #lang racket/base
> >
> > (require redex)
> >
> > (define-language L
> >   (term ::=
> > (TermLambda var term)
> > var
> > (TermApp term term)))
> >
> > (define-syntax-rule (lambda (x) b)
> >   (term (TermLambda x b)))
> >
> > (default-language L)
> >
> > (lambda (y) y)
> > ;; => '(TermLambda y y)
> >
> > (term (substitute ,(lambda (y) y) y z))
> > ;; => '(TermLambda z z)
> >
> >  
> >
> >> But the problem I'm running into is that #%top-interaction applies (term) 
> >> to its argument, which means that  'lambda' and other macros get quoted 
> >> and 
> >> not expanded.
> >>
> >
> > I don't totally understand how #%top-interaction is relevant here. It only 
> > comes up when you use the REPL, and even then I think it shouldn't do 
> > something like that. What are you trying to do in the REPL? And can't you 
> > redefine #%top-interaction so that it doesn't do what you don't want it to 
> > do?
> >  
> >
> >> Is there a way to make sure that the macros are expanded *before* they 
> >> are passed to term, so that (term) receives valid syntax trees for my 
> >> model, instead of quoted lambdas and apps?
> >>
> >
> > See 
> > https://lexi-lambda.github.io/blog/2018/10/06/macroexpand-anywhere-with-local-apply-transformer/.
> >  
> > You can also just use the gist here: 
> > https://gist.github.com/lexi-lambda/65d69043023b519694f50dfca2dc7d33
> >  
> >
> 
> -- 
> 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.


Re: [racket-users] Re: Redex: making a #lang with native lambda, app, etc.

2019-01-03 Thread Shu-Hung You
Deferring everything to runtime could avoid macros as much as
possible. You can implement input parsing as a function and use the
macro only to glue things together.

#lang racket/base

(require (for-syntax racket/base)
 syntax/parse/define
 redex/reduction-semantics)

(provide (rename-out
  [my-module-begin #%module-begin]))

(define-simple-macro (my-module-begin form ...)
  (#%module-begin (handle-expression 'form) ...))

And the function handle-expression can parse the quoted expression
into the syntax of your model, invoke Redex and print the result:

(define (handle-expression expr)
  (define t (parse-input expr))
  (define results (apply-reduction-relation* R t))
  (write (car results)))

(define (parse-input expr)
  )

(where parse-input is a normal function that turns λ into TermLambda
and inserts TermApp when appropriate.)

On Wed, Jan 2, 2019 at 7:47 PM Joey Eremondi  wrote:
>
> Thanks for the response!
>
> That's not quite what I want. That will quote lambdas and applications into 
> my redex models, but I also want it to reduce them by my reduction relation.
>
> The link I posted discussed how to do that, but only when you write programs 
> in the syntax of the model.
>
> Sorry for not being clear about the top-interaction. There's nothing specific 
> about top-interaction, I'm having the same problem with module-begin.
> I'm trying to get them both to evaluate the terms using my redex model.
>
> The local-expand function in the article you give looks promising, but I'm 
> still figuring out how exactly to use it, particularly with the list of 
> expressions for module-begin.
>
>
> On Monday, December 31, 2018 at 8:47:49 PM UTC-8, Sorawee Porncharoenwase 
> wrote:
>>
>> I'm a novice too, but here's my attempt to help.
>>
>>>
>>> I'd like to use (define-syntax) to make lambda, #%app, etc. all expand into 
>>> terms in my model.
>>
>>
>> This works for me.  Is this what you want?
>>
>> #lang racket/base
>>
>> (require redex)
>>
>> (define-language L
>>   (term ::=
>> (TermLambda var term)
>> var
>> (TermApp term term)))
>>
>> (define-syntax-rule (lambda (x) b)
>>   (term (TermLambda x b)))
>>
>> (default-language L)
>>
>> (lambda (y) y)
>> ;; => '(TermLambda y y)
>>
>> (term (substitute ,(lambda (y) y) y z))
>> ;; => '(TermLambda z z)
>>
>>
>>>
>>> But the problem I'm running into is that #%top-interaction applies (term) 
>>> to its argument, which means that  'lambda' and other macros get quoted and 
>>> not expanded.
>>
>>
>> I don't totally understand how #%top-interaction is relevant here. It only 
>> comes up when you use the REPL, and even then I think it shouldn't do 
>> something like that. What are you trying to do in the REPL? And can't you 
>> redefine #%top-interaction so that it doesn't do what you don't want it to 
>> do?
>>
>>>
>>> Is there a way to make sure that the macros are expanded *before* they are 
>>> passed to term, so that (term) receives valid syntax trees for my model, 
>>> instead of quoted lambdas and apps?
>>
>>
>> See 
>> https://lexi-lambda.github.io/blog/2018/10/06/macroexpand-anywhere-with-local-apply-transformer/.
>>  You can also just use the gist here: 
>> https://gist.github.com/lexi-lambda/65d69043023b519694f50dfca2dc7d33
>>
>
> --
> 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.