[ prog / sol / mona ]

prog


Pattern Matching for MIT Scheme

1 2018-10-28 00:49

This is a port of Alex Shinn’s portable pattern matcher for MIT/GNU Scheme.

https://gitlab.com/massma/mit-scheme-match

Where were you when I needed you?

2 2018-10-28 00:54 *

Instead I had to use Oleg Kiselyov's simple match macro :

(define-syntax match
  (syntax-rules ()
    ((__ exp clause ...)
      (let ((val-to-match exp))
 (match* val-to-match clause ...)))))

(define (match-failure val)
  (error "failed match" val))

(define-syntax match*
  (syntax-rules (else)
    ((__ val (else exp ...))
      (let () exp ...))
    ((__ val)
      (match-failure val))
    ((__ val (pattern () exp ...) . clauses)
      (let ((fail (lambda () (match* val . clauses))))
   ; note that match-pattern may do binding. Here,
   ; other clauses are outside of these binding
 (match-pattern val pattern (let () exp ...) (fail))))
    ((__ val (pattern guard exp ...) . clauses)
      (let ((fail (lambda () (match* val . clauses))))
 (match-pattern val pattern
   (if guard (let () exp ...) (fail))
   (fail))))
))

; (match-pattern val pattern kt kf)
(define-syntax match-pattern
  (syntax-rules (_ quote unquote)
    ((__ val _ kt kf) kt)
    ((__ val () kt kf)
      (if (null? val) kt kf))
    ((__ val (quote lit) kt kf)
      (if (equal? val (quote lit)) kt kf))
    ((__ val (unquote var) kt kf)
      (let ((var val)) kt))
    ((__ val (x . y) kt kf)
      (if (pair? val)
 (let ((valx (car val))
       (valy (cdr val)))
   (match-pattern valx x
     (match-pattern valy y kt kf)
     kf))
 kf))
    ((__ val lit kt kf)
      (if (equal? val (quote lit)) kt kf))))

Well, it worked. Still, I could rewrite the concerned parts one of these days. I haven't yet tested Adam Massma's port of the portable hygienic pattern matcher.

3 2018-10-28 01:01 *

>>2
There were two other alternatives that I didn't feel very compelled to use. Of course that doesn't mean they're worthless.

- https://github.com/axch/pattern-case
- https://github.com/mpacula/Scheme-Power-Tools

What I really needed at that time was a port of Alex Shin's pattern matcher. It wasn't published yet. Weird synchronicity, someone was working on it. The code in >>1's link is only 3 weeks old.

4 2020-06-13 18:00 *

I'm unique in this thread. I've recently decided that pattern matching as it is traditionally implemented is harmful. While you gain a more convenient syntax for operating on compound data structures to gain this syntax you have to completely throw away data abstraction for non-literals. We need a way to succinctly express these same operations on compound data in such a way that we can overload the literal syntax used to correspond to arbitrary abstract data. This would mean decomposing a some abstract data through its assessors preserving abstraction barriers.

5 2020-07-15 21:27 *

>>4
Can you elaborate?

6 2020-09-21 02:09 *

>>5
What would you like elaborated?

7 2020-10-04 18:27

>>4
GHC Haskell has this with the ViewPatterns extension.
https://downloads.haskell.org/ghc/latest/docs/html/users_guide/glasgow_exts.html#extension-ViewPatterns

8 2020-10-05 01:48

>>7
It's true, there are also pattern synonyms:
https://gitlab.haskell.org/ghc/ghc/-/wikis/pattern-synonyms
https://www.microsoft.com/en-us/research/wp-content/uploads/2016/08/pattern-synonyms-Haskell16.pdf

9


VIP:

do not edit these