[plt-scheme] Creating new syntax for decision tables

From: Robby Findler (robby.findler at gmail.com)
Date: Thu Feb 8 18:14:52 EST 2007

I would just write a program to process the argument, using
syntax-case. First read across the line, replacing each T with the
variable, each F with not of the variable, and each * with #t. Then,
traspose, stick an and and an or in there, and you're done. Like this:

(module m mzscheme

  (define-syntax (table stx)
    (syntax-case stx (:)
      [(_ clauses ...)
       (let* ([transpose (λ (l) (apply map list l))]
              [rewrite-ele
               (λ (tf)
                 (syntax-case tf (T F)
                   [T #'var]
                   [F #'(not var)]
                   [* #'#t]))]
              [rewrite-clauses
               (λ (clause)
                 (syntax-case clause (:)
                   [(var : tfs ...)
                    (map rewrite-ele (syntax->list (syntax (tfs ...))))]))])
         (with-syntax ([((item ...) ...)
                        (transpose (map rewrite-clauses
                                        (syntax->list (syntax (clauses
...)))))])
           #'(or (and item ...) ...)))]))

  (provide table)
  (printf "~s\n"
          (syntax-object->datum
           (expand-once
            #'(table
               (a : T T F)
               (b : F * F)
               (c : * F T))))))

Robby

On 2/8/07, Andrew Gacek <andrew.gacek at gmail.com> wrote:
> Hi,
>
> I would like to introduce new syntax into scheme which would allow for
> more concise descriptions of large boolean formulas of a certain type.
> Specifically, I would like to be able to say something like
>
> (table
>   (a : T T F)
>   (b : F * F)
>   (c : * F T))
>
> Where each column going down represents a possible way this table
> might be true. For instance the first row is true if a is true and b
> is false (the * means we don't care about the value of c). Thus I
> would like the above expression to expand to,
>
> (or
>   (and a (not b) #t)
>   (and a #t (not c))
>   (and (not a) (not b) c))
>
> I ran into two problems doing this. The first was that I didn't know
> how to convert the T, F, and * into appropriate transformations. The
> second is that the macro needs to expand one column at a time, but the
> "..." notation in scheme seems to favor expanding things one row at a
> time.
>
> Thanks,
> Andrew Gacek
> _________________________________________________
>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>

Posted on the users mailing list.