| 1 |
;;; |
| 2 |
;;; SRFI-247 - Syntactic monads |
| 3 |
;;; |
| 4 |
;;; Copyright (c) 2024 Shiro Kawai <[email protected]> |
| 5 |
;;; |
| 6 |
;;; Redistribution and use in source and binary forms, with or without |
| 7 |
;;; modification, are permitted provided that the following conditions |
| 8 |
;;; are met: |
| 9 |
;;; |
| 10 |
;;; 1. Redistributions of source code must retain the above copyright |
| 11 |
;;; notice, this list of conditions and the following disclaimer. |
| 12 |
;;; |
| 13 |
;;; 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
;;; notice, this list of conditions and the following disclaimer in the |
| 15 |
;;; documentation and/or other materials provided with the distribution. |
| 16 |
;;; |
| 17 |
;;; 3. Neither the name of the authors nor the names of its contributors |
| 18 |
;;; may be used to endorse or promote products derived from this |
| 19 |
;;; software without specific prior written permission. |
| 20 |
;;; |
| 21 |
;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 |
;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 |
;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 |
;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 |
;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 |
;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 27 |
;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 28 |
;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 |
;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 |
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 |
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 |
;;; |
| 33 |
|
| 34 |
(define-module srfi.247 |
| 35 |
(use util.match) |
| 36 |
(export define-syntactic-monad)) |
| 37 |
(select-module srfi.247) |
| 38 |
|
| 39 |
(define-syntax define-syntactic-monad |
| 40 |
(er-macro-transformer |
| 41 |
(^[f r c] |
| 42 |
(match f |
| 43 |
[(_ name svar ...) |
| 44 |
(quasirename r |
| 45 |
`(define-syntax ,name |
| 46 |
(er-macro-transformer |
| 47 |
(^[ff rr cc] |
| 48 |
(define lambda. (rr'lambda)) |
| 49 |
(define define. (rr'define)) |
| 50 |
(define case-lambda. (rr'case-lambda)) |
| 51 |
(define let. (rr'let)) |
| 52 |
(define let*-values. (rr'let*-values)) |
| 53 |
(define svars ',svar) |
| 54 |
(match ff |
| 55 |
[(_ (? (cut cc lambda. <>)) formals . body) |
| 56 |
`(,lambda. (,@svars . ,formals) ,@body)] |
| 57 |
[(_ (? (cut cc define. <>)) (name . formals) . body) |
| 58 |
`(,define. (,name ,@svars . ,formals) ,@body)] |
| 59 |
[(_ (? (cut cc case-lambda. <>)) (formals . body) ...) |
| 60 |
`(,case-lambda. |
| 61 |
,@(map (^[ff bb] `([,@svars . ,ff] ,@bb)) formals body))] |
| 62 |
[(_ (? (cut cc let*-values. <>)) ([formals init] ...) . body) |
| 63 |
`(,let*-values. |
| 64 |
,(map (^[ff ii] `[(,@svars . ,ff) ,ii]) formals init) |
| 65 |
,@body)] |
| 66 |
[(_ (? (cut cc let. <>)) loop ([var init] ...) . body) |
| 67 |
(let* ([vars+init (map list var init)] |
| 68 |
[svars+init |
| 69 |
(map (^[svar] |
| 70 |
(if-let1 init (alist-ref vars+init svar) |
| 71 |
`(,svar ,(car init)) |
| 72 |
`(,svar ,svar))) |
| 73 |
svars)] |
| 74 |
[others+init |
| 75 |
(remove (^[var+init] (memv (car var+init) svars)) |
| 76 |
vars+init)]) |
| 77 |
`(,let. ,loop |
| 78 |
(,@svars+init |
| 79 |
,@others+init) |
| 80 |
,@body))] |
| 81 |
[(_ proc ((var init) ...) arg ...) |
| 82 |
(let* ([vars+init (map list var init)] |
| 83 |
[svar-args |
| 84 |
(map (^[svar] |
| 85 |
(if-let1 init (alist-ref vars+init svar) |
| 86 |
(car init) |
| 87 |
svar)) |
| 88 |
svars)]) |
| 89 |
`(,proc ,@svar-args ,@arg))] |
| 90 |
[(_ proc) |
| 91 |
`(,proc ,@svars)] |
| 92 |
[else |
| 93 |
(errorf "Malformed ~a: ~s" ',name ff)])))))] |
| 94 |
[_ (error "Malformed define-syntactic-monad:" f)])))) |