Search code examples
schemechicken-scheme

Pattern matching compile error in Chicken compiler but not in Chicken interpreter


I am trying to get pattern matching working, but I can only get it working in the Chicken interpreter - not the compiler.

Here is an example of it in the interpreter:

CHICKEN
(c) 2008-2015, The CHICKEN Team
(c) 2000-2007, Felix L. Winkelmann
Version 4.10.0 (rev b259631)
freebsd-unix-clang-x86-64 [ 64bit manyargs dload ptables ]
compiled 2015-08-04 on yves.more-magic.net (Linux)

#;1> (use matchable)
; loading /usr/local/lib/chicken/7/matchable.import.so ...
; loading /usr/local/lib/chicken/7/chicken.import.so ...
; loading /usr/local/lib/chicken/7/lolevel.import.so ...
; loading /usr/local/lib/chicken/7/matchable.so ...
#;2> (match '((1 2) (3 4)) [(a . b) b] [() 0])
((3 4))
#;3>

Here is the compiled version:

(declare (uses matchable))

(match '((1 2) (3 4))
       [(a . b) b]
       [() 0])

This fails (csc src/test.scm):

Syntax error: (src/test.scm:4) - malformed expression: (a . b)
inside expression `(match ...)'


Expansion history:

<syntax>          (##core#begin (match (quote ((1 2) (3 4))) ((a . b) b) (() 0)))
<syntax>          (match (quote ((1 2) (3 4))) ((a . b) b) (() 0))
<syntax>          (quote ((1 2) (3 4)))
<syntax>          (##core#quote ((1 2) (3 4)))
<syntax>          ((a . b) b)
<syntax>          (##core#let ((g0 (a . b))) (g0 b))
<syntax>          (a . b)       <--

What did I miss?


Solution

  • You'll need to load the import library at compile time. The declare statement simply says that it depends on matchable at runtime.

    Simply do the same you did in the interpreter: (use matchable) instead of (declare (uses matchable)).