[racket-dev] racket/match is broken

From: Jay McCarthy (jay.mccarthy at gmail.com)
Date: Wed Oct 5 17:13:29 EDT 2011

I found an interesting error in racket/match in my 330 class this afternoon.

   (test-case "app pattern"
              (check = 4 (match 3 [(app add1 y) y])))
   (test-case "app pattern (step 1)"
              (check = 2 (match (list 1 3)
                           [(list 0 (app add1 y)) y]
                           [(list 1 (app sub1 y)) y])))
   (test-case "app pattern (step 2aa)"
              (check = 2 (match (cons 1 3)
                           [(cons 0 (app error y)) y]
                           [(cons 1 (app sub1 y)) y])))
   (test-case "app pattern (step 2ab)"
              (check = 2 (match (vector 1 3)
                           [(vector 0 (app error y)) y]
                           [(vector 1 (app sub1 y)) y])))
   (test-case "app pattern (step 2)"
              (check = 2 (match (list 1 3)
                           [(list 0 (app error y)) y]
                           [(list 1 (app sub1 y)) y])))

If you run these you'll see that the final one ends in an error.
Basically, it calls the app function on the second element of the list
even though the head of the list has failed to pattern match. This is
very dangerous, in my opinion because it means code like...

(match (list password target)
  [(list (== right-password) (app launch-the-missiles success?))
   success?]
  [_
   "Sorry, no nukes for you")

will destroy Canada even though it should not.

Jay

-- 
Jay McCarthy <jay at cs.byu.edu>
Assistant Professor / Brigham Young University
http://faculty.cs.byu.edu/~jay

"The glory of God is Intelligence" - D&C 93


Posted on the dev mailing list.