[plt-scheme] Inlined language from a language module

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Fri Mar 20 12:54:30 EDT 2009

At Fri, 20 Mar 2009 10:46:34 -0600, Neil Toronto wrote:
> I'm writing an s-expression-based DSL for statistical inference called
> RevBayes. It's currently defined in a language module, which is great
> because I *must* have tight control over the identifiers the DSL has
> access to.
> 
> [...]
> 
> What I'd like is a way to inline RevBayes code with a macro, and have it
> expand exactly as if it had been defined in a separate module.

You can strip the lexical context off the RevBayes code, then combine
with with a macro-introduced import of the restricted language.

Here's a little example, where `with-mini' is the form you can uses in
`scheme' (or any language) to write an expression in the restricted
language.

---- mini.ss ----------------------------------------

#lang scheme

;; A mini-language that just provides "1+" and 0

(define-syntax-rule (1+ n)
  (+ 1 n))
(define-syntax-rule (datum . 0) '0)

(define-syntax-rule (module-begin e)
  (#%module-begin e))

(provide 1+
         (rename-out [module-begin #%module-begin])
         (rename-out [datum #%datum]))

---- example using mini.ss --------------------------

#lang s-exp "mini.ss"

;; can only use the constrained languages
(1+ (1+ 0))

---- with-mini.ss -----------------------------------

#lang scheme
(require (for-syntax syntax/strip-context))
(provide with-mini)

(define-syntax (with-mini stx)
  (syntax-case stx ()
    [(_ expr)
     #`(begin
         (require "mini.ss")
         #,(strip-context #'expr))]))

---- example using with-mini.ss ---------------------

#lang scheme
(require "with-mini.ss")

; expression in `with-mini' is constrained to the 
; "mini.ss" language:
(with-mini (1+ 0)) 



Posted on the users mailing list.