cisco/ChezScheme

Primitive macro matching procedure application?

Closed this issue · 6 comments

Is there a primitive macro that matches on procedure application, something along the lines of

> (define-syntax (#apply code)
    (syntax-case code ()
      [(procedure args ...) #'(procedure 1 2 3)]))
> (+ 1 1)
6
> (- 10 "hello")
-4

I know how to achieve the same effect with

> (define-syntax (my-123-language code)
    (syntax-case code ()
      [(_ (procedure args ...)) #'(procedure 1 2 3)]))
> (my-123-language (+ 1 1))
6
> (my-123-language (- 10 "hello"))
-4

which is probably a much better approach because it clearly delineates the change in semantics from the rest of the program but I'm just curious.

You can set current-eval, I think it will have the same effect.

thread parameter: current-eval
libraries: (chezscheme)

current-eval determines the evaluation procedure used by the procedures eval, load, and new-cafe. current-eval is initially bound to the value of compile. (In Petite Chez Scheme, it is initially bound to the value of interpret.) The evaluation procedure should expect one or two arguments: an object to evaluate and an optional environment. The second argument might be an annotation (Section 11.11).

(current-eval interpret)
(+ 1 1) <graphic> 2

(current-eval (lambda (x . ignore) x))
(+ 1 1) <graphic> (+ 1 1)

https://cisco.github.io/ChezScheme/csug9.5/system.html#./system:s39

I don't think that will do because I need the procedure identifier at compile time if I want to implement static typing for example.

@bjornkihlberg Do I understand correctly, that you want something like #%app in Racket?

In Racket an application will expand into (#%app proc-expr arg ...).
A user can then redefine the meaning of application by providing his own #%app.

@soegaard That's correct.

As far as I know, there is no equivalent in Chez Scheme.

Alright, no problems!

I actually think it's much better to explicitly demarcate changes in semantics anyway. eg

(let ([x (typed ((lambda ([x : fixnum]) (add1 x)) 5))])
  (add1 x))