[plt-scheme] Creating new syntax for decision tables

From: Jacob Matthews (jacobm at cs.uchicago.edu)
Date: Thu Feb 8 19:59:18 EST 2007

This can also be done pretty nicely using syntax-rules. Here's a
version that uses only pattern matching:

(module a mzscheme

  (define-syntax table
    (syntax-rules (:)
      [(table (e : c1 c2 ...) ...)
       (or (and (clause-transform c1 e) ...)
           (table (e : c2 ...) ...))]
      [(table (e :) ...)
       #f]))

  (define-syntax clause-transform
    (syntax-rules (T F *)
      [(clause-transform T e) e]
      [(clause-transform F e) (not e)]
      [(clause-transform * e) #t])))


Examples:

> (let ([a #t] [b #f] [c #t])
    (table
     (a : T T T)
     (b : T F *)
     (c : T F F)))
#f
> (let ([a #t] [b #f] [c #t])
    (table
     (a : T T *)
     (b : T F *)
     (c : T F F)))
#f
> (let ([a #t] [b #f] [c #t])
    (table
     (a : T T *)
     (b : * F *)
     (c : T F F)))
#t


Notes:
  - not thoroughly tested
  - as written it will evaluate each e several times, this can be
changed easily enough
  - of course there's no syntax error checking

-jacob



On 2/8/07, Robby Findler <robby.findler at gmail.com> wrote:
> Oops, left in a silly bug. Here you go:
>
> (module m mzscheme
>
>   (define-syntax (table stx)
>     (syntax-case stx (:)
>       [(_ clauses ...)
>        (let* ([transpose (λ (l) (apply map list l))]
>               [rewrite-clauses
>                (λ (clause)
>                  (syntax-case clause (:)
>                    [(var : tfs ...)
>                     (map
>                      (λ (tf)
>                        (syntax-case tf (T F)
>                          [T #'var]
>                          [F #'(not var)]
>                          [* #'#t]))
>                      (syntax->list (syntax (tfs ...))))]))])
>          (with-syntax ([((item ...) ...)
>                         (transpose (map rewrite-clauses
>                                         (syntax->list (syntax (clauses
> ...)))))])
>            #'(or (and item ...) ...)))]))
>
>   (λ (a b c)
>     (table
>      (a : T T F)
>      (b : F * F)
>      (c : * F T))))
>
> _________________________________________________
>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>
>

Posted on the users mailing list.