| 1 | ;;; SPDX-FileCopyrightText: 2021 Wolfgang Corcoran-Mathe <[email protected]> |
| 2 | ;;; |
| 3 | ;;; SPDX-License-Identifier: MIT |
| 4 | |
| 5 | ;;; Pattern-matching macro for trie values. |
| 6 | ;;; |
| 7 | ;;; Based on William Byrd's pmatch modification of Oleg Kiselyov's |
| 8 | ;;; simple linear pattern-matcher. |
| 9 | |
| 10 | ;;; Syntax: |
| 11 | ;;; |
| 12 | ;;; (tmatch exp <clause> ... [<else-clause>]) |
| 13 | ;;; |
| 14 | ;;; <clause> ::= (<pattern> [<guard>] exp ...) |
| 15 | ;;; <else-clause> ::= (else exp ...) |
| 16 | ;;; <guard> ::= (guard boolean-exp ...) |
| 17 | ;;; <pattern> ::= empty |
| 18 | ;;; | (leaf <pattern> <pattern>) |
| 19 | ;;; | (branch <pattern> <pattern> <pattern> <pattern>) |
| 20 | |
| 21 | ;; (define-syntax tmatch |
| 22 | ;; (syntax-rules (else) |
| 23 | ;; ((tmatch exp (e ...) ...) |
| 24 | ;; (tmatch-aux #f exp (e ...) ...)) |
| 25 | ;; ((tmatch name exp (e ...) ...) |
| 26 | ;; (tmatch-aux name exp (e ...) ...)))) |
| 27 | |
| 28 | (define-syntax tmatch |
| 29 | (syntax-rules (else guard) |
| 30 | ((tmatch (f x ...) cs ...) |
| 31 | (let ((v (f x ...))) |
| 32 | (tmatch v cs ...))) |
| 33 | ((tmatch v) |
| 34 | (error "tmatch: no clause matched" v)) |
| 35 | ((tmatch _ (else e0 e1 ...)) (begin e0 e1 ...)) |
| 36 | ((tmatch v (pat (guard g ...) e0 e1 ...) cs ...) |
| 37 | (let ((fk (lambda () (tmatch v cs ...)))) |
| 38 | (tpat v pat (if (and g ...) (begin e0 e1 ...) (fk)) (fk)))) |
| 39 | ((tmatch v (pat e0 e1 ...) cs ...) |
| 40 | (let ((fk (lambda () (tmatch v cs ...)))) |
| 41 | (tpat v pat (begin e0 e1 ...) (fk)))))) |
| 42 | |
| 43 | ;; Uses pmatch's `ppat' auxilliary macro, see below. |
| 44 | (define-syntax tpat |
| 45 | (syntax-rules (empty leaf branch unquote) |
| 46 | ((tpat v empty kt kf) (if v kf kt)) |
| 47 | ((tpat v (leaf pkey pval) kt kf) |
| 48 | (if (leaf? v) |
| 49 | (let ((key (leaf-key v)) (value (leaf-value v))) |
| 50 | (ppat key pkey (ppat value pval kt kf) kf)) |
| 51 | kf)) |
| 52 | ((tpat v (branch pp pm pl pr) kt kf) |
| 53 | (if (branch? v) |
| 54 | (let ((pfx (branch-prefix v)) |
| 55 | (bit (branch-branching-bit v)) |
| 56 | (left (branch-left v)) |
| 57 | (right (branch-right v))) |
| 58 | (ppat pfx |
| 59 | pp |
| 60 | (ppat bit pm (ppat left pl (ppat right pr kt kf) kf) kf) |
| 61 | kf)) |
| 62 | kf)))) |
| 63 | |
| 64 | ;; Shorthands for a unary function that immediately pattern-matches |
| 65 | ;; its trie parameter. |
| 66 | (define-syntax tmatch-lambda |
| 67 | (syntax-rules () |
| 68 | ((tmatch-lambda cs ...) |
| 69 | (lambda (arg) (tmatch arg cs ...))))) |
| 70 | |
| 71 | ;;; pmatch, by Oleg Kiselyov, rev. Will Byrd. |
| 72 | ;;; The original public-domain code can be found at |
| 73 | ;;; http://okmij.org/ftp/Scheme/match-case-simple.scm |
| 74 | |
| 75 | ;; This is a new version of pmatch (August 8, 2012). |
| 76 | ;; It has two important new features: |
| 77 | ;; 1. It allows for a name to be given to the pmatch if an error ensues. |
| 78 | ;; 2. A line from the specification has been removed. (see below). Without |
| 79 | ;; that line removed, it was impossible for a pattern to be (quote ,x), |
| 80 | ;; which might be worth having especially when we write an interpreter |
| 81 | ;; for Scheme, which includes quote as a language form. |
| 82 | |
| 83 | ;;; Code written by Oleg Kiselyov |
| 84 | ;; (http://pobox.com/~oleg/ftp/) |
| 85 | ;;; |
| 86 | ;;; Taken from leanTAP.scm |
| 87 | ;;; http://kanren.cvs.sourceforge.net/kanren/kanren/mini/leanTAP.scm?view=log |
| 88 | |
| 89 | ; A simple linear pattern matcher |
| 90 | ; It is efficient (generates code at macro-expansion time) and simple: |
| 91 | ; it should work on any R5RS (and R6RS) Scheme system. |
| 92 | |
| 93 | ; (pmatch exp <clause> ...[<else-clause>]) |
| 94 | ; <clause> ::= (<pattern> <guard> exp ...) |
| 95 | ; <else-clause> ::= (else exp ...) |
| 96 | ; <guard> ::= boolean exp | () |
| 97 | ; <pattern> :: = |
| 98 | ; ,var -- matches always and binds the var |
| 99 | ; pattern must be linear! No check is done |
| 100 | ; _ -- matches always |
| 101 | ; 'exp -- comparison with exp (using equal?) REMOVED (August 8, 2012) |
| 102 | ; exp -- comparison with exp (using equal?) |
| 103 | ; (<pattern1> <pattern2> ...) -- matches the list of patterns |
| 104 | ; (<pattern1> . <pattern2>) -- ditto |
| 105 | ; () -- matches the empty list |
| 106 | |
| 107 | ;; We've removed the name parameter for now, since it seems to cause |
| 108 | ;; problems for the expander in many Schemes. |
| 109 | |
| 110 | ;; (define-syntax pmatch |
| 111 | ;; (syntax-rules (else guard) |
| 112 | ;; ((pmatch v (e ...) ...) |
| 113 | ;; (pmatch-aux #f v (e ...) ...)) |
| 114 | ;; ((pmatch v name (e ...) ...) |
| 115 | ;; (pmatch-aux name v (e ...) ...)))) |
| 116 | |
| 117 | (define-syntax pmatch |
| 118 | (syntax-rules (else guard) |
| 119 | ((pmatch (rator rand ...) cs ...) |
| 120 | (let ((v (rator rand ...))) ; avoid multiple evals |
| 121 | (pmatch v cs ...))) |
| 122 | ((pmatch v) ; no more clauses |
| 123 | (error "pmatch failed" v)) |
| 124 | ((pmatch _ (else e0 e ...)) (begin e0 e ...)) |
| 125 | ((pmatch v (pat (guard g ...) e0 e ...) cs ...) |
| 126 | (let ((fk (lambda () (pmatch v cs ...)))) |
| 127 | (ppat v pat (if (and g ...) (begin e0 e ...) (fk)) (fk)))) |
| 128 | ((pmatch v (pat e0 e ...) cs ...) |
| 129 | (let ((fk (lambda () (pmatch v cs ...)))) |
| 130 | (ppat v pat (begin e0 e ...) (fk)))))) |
| 131 | |
| 132 | (define-syntax ppat |
| 133 | (syntax-rules (? unquote) |
| 134 | ((ppat _ ? kt _) kt) ; the ? wildcard always matches |
| 135 | ((ppat v () kt kf) (if (null? v) kt kf)) |
| 136 | ; ((ppat v (quote lit) kt kf) (if (equal? v (quote lit)) kt kf)) |
| 137 | ((ppat v (unquote var) kt _) (let ((var v)) kt)) |
| 138 | ((ppat v (x . y) kt kf) |
| 139 | (if (pair? v) |
| 140 | (let ((vx (car v)) (vy (cdr v))) |
| 141 | (ppat vx x (ppat vy y kt kf) kf)) |
| 142 | kf)) |
| 143 | ((ppat v lit kt kf) (if (equal? v (quote lit)) kt kf)))) |
| 144 | |
| 145 | ;;; Shorthands for functions that immediately pattern-match their |
| 146 | ;;; parameter(s). |
| 147 | |
| 148 | ;; One-argument form. |
| 149 | (define-syntax pmatch-lambda |
| 150 | (syntax-rules () |
| 151 | ((pmatch-lambda cs ...) |
| 152 | (lambda (arg) (pmatch arg cs ...))))) |
| 153 | |
| 154 | ;; Multi-argument form. |
| 155 | (define-syntax pmatch-lambda* |
| 156 | (syntax-rules () |
| 157 | ((pmatch-lambda* cs ...) |
| 158 | (lambda args (pmatch args cs ...))))) |