summaryrefslogtreecommitdiff
path: root/2d/signals.scm
blob: 04de9c024cf8eb0209a0bf10cf8c922d169426be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
;;; guile-2d
;;; Copyright (C) 2013 David Thompson <dthompson2@worcester.edu>
;;;
;;; Guile-2d is free software: you can redistribute it and/or modify it
;;; under the terms of the GNU Lesser General Public License as
;;; published by the Free Software Foundation, either version 3 of the
;;; License, or (at your option) any later version.
;;;
;;; Guile-2d is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;; Lesser General Public License for more details.
;;;
;;; You should have received a copy of the GNU Lesser General Public
;;; License along with this program.  If not, see
;;; <http://www.gnu.org/licenses/>.

;;; Commentary:
;;
;; Simple functional reactive programming API.
;;
;;; Code:

(define-module (2d signals)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-9)
  #:use-module (srfi srfi-26)
  #:use-module (2d coroutine)
  #:export (<signal>
            signal?
            make-signal
            signal-ref
            signal-ref-maybe
            signal-transformer
            signal-filter
            signal-connectors
            signal-connect!
            signal-disconnect!
            signal-clear!
            signal-set!
            signal-constant
            signal-lift
            signal-lift2
            signal-lift3
            signal-lift4
            signal-merge
            signal-combine
            signal-fold
            signal-count
            signal-if
            signal-and
            signal-or
            signal-when
            signal-unless))

;;;
;;; Signals
;;;

;; Signals are time-varying values. For example, a signal could
;; represent the mouse position at the current point in time. The
;; signals API provides an abstraction over regular event-based
;; programming. State mutation is hidden away and a functional,
;; declarative interface is exposed.
(define-record-type <signal>
  (%make-signal value transformer filter connectors)
  signal?
  (value signal-ref %signal-set!)
  (transformer signal-transformer)
  (filter signal-filter)
  (connectors signal-connectors %set-signal-connectors!))

(define (identity-transform value old from)
  "Return VALUE unchanged."
  value)

(define (keep-all value old from)
  "Keep all values."
  #t)

(define* (make-signal #:optional #:key
                      (transformer identity-transform)
                      (filter keep-all)
                      (init #f)
                      (connectors '()))
  "Create a new signal with initial value INIT that uses the given
TRANSFORMER procedure to process incoming values from another
signal.  Additionally, the signal will be connected to all of the
signals in the list CONNECTORS."
  (let ((signal (%make-signal init transformer filter '())))
    (for-each (cut signal-connect! <> signal) connectors)
    signal))

(define (signal-ref-maybe object)
  "Dereferences OBJECT if it is a signal and returns OBJECT
otherwise."
  (if (signal? object)
      (signal-ref object)
      object))

(define (%signal-transform signal value from)
  "Call the transform procedure for SIGNAL with VALUE."
  ((signal-transformer signal) value (signal-ref signal) from))

(define (signal-connect! signal listener)
  "Attach LISTENER to SIGNAL. When the value of SIGNAL changes, the
value will be propagated to LISTENER."
  (%set-signal-connectors!
   signal
   (cons listener (signal-connectors signal)))
  (signal-receive! listener (signal-ref signal) signal))

(define (signal-disconnect! signal listener)
  "Detach LISTENER from SIGNAL."
  (%set-signal-connectors!
   signal
   (delete listener (signal-connectors signal) eq?)))

(define (signal-clear! signal)
  "Detach all connectors from SIGNAL."
  (%set-signal-connectors! signal '()))

(define* (signal-set! signal value #:optional (from #f))
  "Set VALUE for SIGNAL from the connected signal FROM and
propagate VALUE to all connected signals. "
  (let ((value (%signal-transform signal value from)))
    (%signal-set! signal value)
    (for-each (cut signal-receive! <> value signal)
              (signal-connectors signal))))

(define (signal-keep? signal value from)
  "Call the filter procedure for SIGNAL with VALUE."
  ((signal-filter signal) value (signal-ref signal) from))

(codefine (signal-receive! signal value from)
  "Receive VALUE for SIGNAL from the connected signal FROM.  VALUE
will be set if it passes through the filter."
  (when (signal-keep? signal value from)
    (signal-set! signal value from)))

;;;
;;; Primitive signals
;;;

;; TODO: Write a macro for generating lifts
(define (signal-lift transformer signal)
  "Create a new signal that lifts the procedure TRANSFORMER of arity 1
onto SIGNAL."
  (make-signal
   #:transformer (lambda (value prev from)
                   (transformer value))
   #:connectors (list signal)))

(define (signal-lift2 transformer signal1 signal2)
  "Create a new signal that lifts the procedure TRANSFORMER of arity 2
onto SIGNAL1 and SIGNAL2."
  (make-signal
   #:transformer (lambda (value prev from)
                   (transformer (signal-ref signal1)
                                (signal-ref signal2)))
   #:connectors (list signal1 signal2)))

(define (signal-lift3 transformer signal1 signal2 signal3)
  "Create a new signal that lifts the procedure TRANSFORMER of arity 3
onto SIGNAL1, SIGNAL2, and SIGNAL3."
  (make-signal
   #:transformer (lambda (value prev from)
                   (transformer (signal-ref signal1)
                                (signal-ref signal2)
                                (signal-ref signal3)))
   #:connectors (list signal1 signal2 signal3)))

(define (signal-lift4 transformer signal1 signal2 signal3 signal4)
  "Create a new signal that lifts the procedure TRANSFORMER of arity 4
onto SIGNAL1, SIGNAL2, SIGNAL3, and SIGNAL4."
  (make-signal
   #:transformer (lambda (value prev from)
                   (transformer (signal-ref signal1)
                                (signal-ref signal2)
                                (signal-ref signal3)
                                (signal-ref signal4)))
   #:connectors (list signal1 signal2 signal3 signal4)))

(define (signal-merge . signals)
  "Create a new signal that merges every signal in the list SIGNALS
into one. The value of the new signal is the value of the most
recently changed signal in the list."
  (make-signal #:connectors signals))

(define (signal-combine . signals)
  "Create a new signal that combines the values of SIGNALS into a
list."
  (make-signal
   #:transformer (lambda (value prev from)
                   (map signal-ref signals))
   #:connectors signals))

(define (signal-fold proc init signal)
  "Create a new signal that accumulates the current and previous
values of SIGNAL using PROC."
  (make-signal
   #:init init
   #:transformer (lambda (value prev from)
                   (proc value prev))
   #:connectors (list signal)))

(define (signal-count signal)
  "Create a new signal that increments a counter every time the value
of SIGNAL changes."
  ;; Initial value is -1 to compensate for the inital signal update
  ;; when connecting.
  (signal-fold (lambda (new old) (1+ old)) -1 signal))

(define (signal-constant constant signal)
  "Create a new signal that emits the value CONSTANT whenever a new
value is received from SIGNAL."
  (signal-lift (lambda (value) constant) signal))

(define (signal-if predicate consequent alternate)
  "Create a new signal that emits the value of the signal CONSEQUENT
when the value of the signal PREDICATE is true and the value of the
signal ALTERNATE otherwise."
  (make-signal
   #:transformer (lambda (value prev from)
                   (if (signal-ref predicate)
                       (signal-ref consequent)
                       (signal-ref alternate)))
   #:connectors (list predicate
                      consequent
                      alternate)))

(define (signal-and . signals)
  "Create a new signal that performs a logical AND operation on the
values of SIGNALS."
  (make-signal
   #:transformer (lambda (value prev from)
                   (let loop ((signals signals)
                              (prev #t))
                     (cond ((null? signals)
                            (signal-ref prev))
                           ((signal-ref (car signals))
                            (loop (cdr signals) (car signals)))
                           (else
                            #f))))
   #:connectors signals))

(define (signal-or . signals)
  "Create a new signal that performs a logicla OR operation the values
of SIGNALS."
  (make-signal
   #:transformer (lambda (value prev from)
                   (let loop ((signals signals))
                     (cond ((null? signals)
                            #f)
                           ((signal-ref (car signals))
                            (signal-ref (car signals)))
                           (else
                            (loop (cdr signals))))))
   #:connectors signals))

(define (signal-when predicate init consequent)
  "Create a new signal that keeps the value from CONSEQUENT only when
PREDICATE is true.  INIT specifies the value that is set if PREDICATE
is never true."
  (make-signal
   #:init init
   #:filter (lambda (value prev from)
              (signal-ref predicate))
   #:transformer (lambda (value prev from)
                   (signal-ref consequent))
   #:connectors (list predicate consequent)))

(define (signal-unless predicate init consequent)
  "Create a new signal that drops the value from CONSEQUENT only when
PREDICATE is true.  INIT specifies the value that is set if PREDICATE
is never true."
  (make-signal
   #:init init
   #:filter (lambda (value prev from)
              (not (signal-ref predicate)))
   #:transformer (lambda (value prev from)
                   (signal-ref consequent))
   #:connectors (list predicate consequent)))