[plt-scheme] MzScheme as embedded engine for LilyPond ?

From: Eli Barzilay (eli at barzilay.org)
Date: Thu Feb 3 17:36:47 EST 2005

I cannot reply to everything but I'll do some.  Specifically, I don't
have experience with using mzscheme as an embedded application -- just
with extending mzscheme itself, but it should be similar.  (Someone
please correct me if this is wrong.)

On Feb  3, Han-Wen Nienhuys wrote:
> * How well is embedding MzScheme supported? I've read "Inside PLT
>   MzScheme". Am I correct that there is no way to add "coded" types
>   to MzScheme?

There is -- you need to define some struct that has `Scheme_Object' as
its first field (I'm talking about v299), something like this 

  static Scheme_Type foo_tag;
  typedef struct foo_struct {
    Scheme_Object so;
    int x;
    void* p;
  } foo_struct;

and you need to initialize foo_tag like this:

  foo_tag = scheme_make_type("<foo>");

> * Can I force symbols to be conservatively GC'd?  Inside lilypond
>   source code, we have a macro ly_symbol2scm(), eg.

MzScheme uses a conservative GC by default.  See the "Inside MzScheme"
manual for various memory issues.

>   SCM foo = bar();
>   if (foo == ly_symbol2scm ("bla-bla"))
>      ..
>   ly_symbol2scm interns "bla-bla", and stores the outcome, so that
>   only the first invocation of ly_symbol2scm() is costly. Of course,
>   this only works if the actual SCM isn't moved around.

Ah, if you're talking about interning symbols then of course mzschem
has it...  Again, see the manual I mentioned.  (BTW, you can read it
on-line from http://download.plt-scheme.org/doc (either as a pdf or

>   (I believe the MzScheme term for SCM is Scheme_Object*)


> * Is there a possibility to read a single expression precisely off
>   an input stream?

You mean using `read'?  Of course.

>   In LilyPond input files, you can escape to Scheme, using # , for
>   example:
>     \score { #(make-music 'NoteEvent) }
>   here, the lexer gives control to the Scheme interpreter when it
>   reaches # . The Scheme interpreter reads up to the ) and evals the
>   result. The lexer then continues at the )
>   Is this somehow possible with MzScheme? 

Yes -- there is a library (parser-tools) that provides you with
lex/yacc-like tools.  If you want something that makes heavy use of
Scheme code, you can also use the preprocessor library.  In any case,
the best approach here would be to write this code in Scheme.

> * Is there support for read extensions?  We have a special feature,
>   that allows us to parse LilyPond code inside one of these embedded
>   Scheme fragments, for example

There is no real reader extension support, but using the preprocessor
it is easy to modify things when you load modules (mztext which is one
component has a similar approach, where some command character is used
to dispatch to different handlers).  But if you only need to escape
from LilyPond to Scheme and back, then you don't need to change the
syntax dynamically...

> * Is there support for stack-traces/source annotation?  If I make an
>   error in GUILE in debug mode, I get 
> 	byrd:~/usr/src/lilypond$ guile --debug b.scm
> 	Backtrace:
> 	In unknown file:
> 	   ?: 0* [primitive-load "b.scm"]
> 	In b.scm:
> 	   9: 1* [Y]
> 	   7: 2  [x 0]
> 	   3: 3  [0]
> 	b.scm:3:3: In expression (a):
> 	b.scm:3:3: Wrong type to apply: 0
>   a nice stack trace, with line/column numbers of the calling points.
>   How do I get that in MzScheme?  I've understood that this is only
>   supported in DrScheme, but I need to switch between both modes on
>   the fly. In other words,
>     lilypond --debug foo.ly
>   would use the MzScheme with locations, and 
>     lilypond foo.ly
>   would use the non-debugging version.  Is this possible at all?

mzscheme -M errortrace

> * Is there support for doc strings and self-documentation?  In
>   LilyPond, almost all coded subroutines come with docstrings, and
>   would show up if you did
>     (apropos "ly:music?")

There is no standard facility for docstrings.  Cooking one up is
really simple, but the common approach is to provide documentation
that is used with Help-Desk.

          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                  http://www.barzilay.org/                 Maze is Life!

Posted on the users mailing list.