| 1 |
;;; |
| 2 |
;;; SRFI-210 - Procedures and syntax for multiple values |
| 3 |
;;; |
| 4 |
;;; Copyright (c) 2021-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.210 |
| 35 |
(use util.match) |
| 36 |
(export apply/mv call/mv list/mv vector/mv box/mv value/mv |
| 37 |
coarity set!-values with-values case-receive bind/mv |
| 38 |
|
| 39 |
list-values vector-values box-values value identity |
| 40 |
compose-left compose-right map-values bind/list bind/box bind)) |
| 41 |
(select-module srfi.210) |
| 42 |
|
| 43 |
;; These syntaxes can run more efficiently if supported in VM natively; |
| 44 |
;; i.e. the producer could directly place mv results in the VM stack. |
| 45 |
;; For now, we emulate them. |
| 46 |
|
| 47 |
(define-syntax apply/mv |
| 48 |
(syntax-rules () |
| 49 |
[(_ operator producer) |
| 50 |
(with-values producer operator)] |
| 51 |
[(_ operator operand ... producer) |
| 52 |
(apply operator operand ... (values->list producer))])) |
| 53 |
|
| 54 |
(define-syntax call/mv |
| 55 |
(syntax-rules () |
| 56 |
[(_ consumer) (consumer)] |
| 57 |
[(_ consumer producer) (with-values producer consumer)] |
| 58 |
[(_ consumer producer ...) |
| 59 |
(apply consumer (append (values->list producer) ...))])) |
| 60 |
|
| 61 |
(define-syntax list/mv |
| 62 |
(syntax-rules () |
| 63 |
[(_ producer) (values->list producer)] |
| 64 |
[(_ element ... producer) |
| 65 |
(list* element ... (values->list producer))])) |
| 66 |
|
| 67 |
(define-syntax vector/mv |
| 68 |
(syntax-rules () |
| 69 |
[(_ producer) (with-values producer vector)] |
| 70 |
[(_ element ... producer) |
| 71 |
(apply vector element ... (values->list producer))])) |
| 72 |
|
| 73 |
(define-syntax box/mv |
| 74 |
(syntax-rules () |
| 75 |
[(_ producer) (with-values producer box)] |
| 76 |
[(_ element ... producer) |
| 77 |
(apply box element ... (values->list producer))])) |
| 78 |
|
| 79 |
(define-syntax value/mv |
| 80 |
(syntax-rules () |
| 81 |
[(_ index producer) (values-ref producer index)] |
| 82 |
[(_ index element ... producer) |
| 83 |
(apply value index element ... (values->list producer))])) |
| 84 |
|
| 85 |
(define-syntax coarity |
| 86 |
(syntax-rules () |
| 87 |
[(_ producer) |
| 88 |
(length (values->list producer))])) |
| 89 |
|
| 90 |
;; set!-values - builtin |
| 91 |
|
| 92 |
(define-syntax with-values |
| 93 |
(syntax-rules () |
| 94 |
[(_ producer consumer) |
| 95 |
(receive vals producer |
| 96 |
(apply consumer vals))])) |
| 97 |
|
| 98 |
(define-syntax case-receive |
| 99 |
(syntax-rules () |
| 100 |
[(_ producer clause ...) |
| 101 |
(with-values producer |
| 102 |
(case-lambda clause ...))])) |
| 103 |
|
| 104 |
(define-syntax bind/mv |
| 105 |
(syntax-rules () |
| 106 |
[(_ producer transducer ...) |
| 107 |
(bind/list (values->list producer) transducer ...)])) |
| 108 |
|
| 109 |
(define (list-values list) (apply values list)) |
| 110 |
|
| 111 |
(define (vector-values vec) (apply values (vector->list vec))) |
| 112 |
|
| 113 |
(define (box-values box) (unbox box)) |
| 114 |
|
| 115 |
(define (value k . objs) (list-ref objs k)) |
| 116 |
|
| 117 |
(define identity values) ;; This differs from Gauche's built-in |
| 118 |
|
| 119 |
(define (compose-left . transducers) (apply compose (reverse transducers))) |
| 120 |
|
| 121 |
(define (compose-right . transducers) (apply compose transducers)) |
| 122 |
|
| 123 |
(define (map-values proc) |
| 124 |
(^ xs (list-values (map proc xs)))) |
| 125 |
|
| 126 |
(define (bind/list lis . transducers) |
| 127 |
(match transducers |
| 128 |
[() (list-values lis)] |
| 129 |
[(transducer) (apply transducer lis)] |
| 130 |
[(transducer . transducers) |
| 131 |
(apply bind/list (values->list (apply transducer lis)) transducers)])) |
| 132 |
|
| 133 |
(define (bind/box bx . transducers) |
| 134 |
(apply bind/list (values->list (unbox bx)) transducers)) |
| 135 |
|
| 136 |
(define (bind obj . transducers) |
| 137 |
(apply bind/list (list obj) transducers)) |