gnu: Add 'patches'.
[jackhill/guix/guix.git] / tests / monads.scm
CommitLineData
b860f382 1;;; GNU Guix --- Functional package management for GNU
462a3fa3 2;;; Copyright © 2013, 2014, 2015 Ludovic Courtès <ludo@gnu.org>
b860f382
LC
3;;;
4;;; This file is part of GNU Guix.
5;;;
6;;; GNU Guix is free software; you can redistribute it and/or modify it
7;;; under the terms of the GNU General Public License as published by
8;;; the Free Software Foundation; either version 3 of the License, or (at
9;;; your option) any later version.
10;;;
11;;; GNU Guix is distributed in the hope that it will be useful, but
12;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14;;; GNU General Public License for more details.
15;;;
16;;; You should have received a copy of the GNU General Public License
17;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
18
19(define-module (test-monads)
c1bc358f 20 #:use-module (guix tests)
b860f382
LC
21 #:use-module (guix store)
22 #:use-module (guix monads)
23 #:use-module (guix derivations)
e87f0591 24 #:use-module (guix packages)
b860f382
LC
25 #:use-module (gnu packages)
26 #:use-module (gnu packages bootstrap)
4231f05b 27 #:use-module ((gnu packages base) #:select (coreutils))
b860f382
LC
28 #:use-module (ice-9 match)
29 #:use-module (rnrs io ports)
30 #:use-module (srfi srfi-1)
31 #:use-module (srfi srfi-26)
32 #:use-module (srfi srfi-64))
33
405a9d4e 34;; Test the (guix monads) module.
b860f382
LC
35
36(define %store
c1bc358f 37 (open-connection-for-tests))
b860f382
LC
38
39(define %monads
81a97734 40 (list %identity-monad %store-monad %state-monad))
b860f382
LC
41
42(define %monad-run
43 (list identity
81a97734
LC
44 (cut run-with-store %store <>)
45 (cut run-with-state <> '())))
46
47(define-syntax-rule (values->list exp)
48 (call-with-values (lambda () exp)
49 list))
b860f382
LC
50
51\f
52(test-begin "monads")
53
aeb7ec5c
LC
54(test-assert "monad?"
55 (and (every monad? %monads)
56 (every (compose procedure? monad-bind) %monads)
57 (every (compose procedure? monad-return) %monads)))
58
b860f382
LC
59;; The 3 "monad laws": <http://www.haskell.org/haskellwiki/Monad_laws>.
60
61(test-assert "left identity"
62 (every (lambda (monad run)
63 (let ((number (random 777)))
64 (with-monad monad
65 (define (f x)
66 (return (* (1+ number) 2)))
67
68 (= (run (>>= (return number) f))
69 (run (f number))))))
70 %monads
71 %monad-run))
72
73(test-assert "right identity"
74 (every (lambda (monad run)
75 (with-monad monad
76 (let ((number (return (random 777))))
77 (= (run (>>= number return))
78 (run number)))))
79 %monads
80 %monad-run))
81
82(test-assert "associativity"
83 (every (lambda (monad run)
84 (with-monad monad
85 (define (f x)
86 (return (+ 1 x)))
87 (define (g x)
88 (return (* 2 x)))
89
90 (let ((number (return (random 777))))
91 (= (run (>>= (>>= number f) g))
92 (run (>>= number (lambda (x) (>>= (f x) g))))))))
93 %monads
94 %monad-run))
95
96(test-assert "lift"
97 (every (lambda (monad run)
98 (let ((f (lift1 1+ monad)))
99 (with-monad monad
100 (let ((number (random 777)))
101 (= (run (>>= (return number) f))
102 (1+ number))))))
103 %monads
104 %monad-run))
105
751630c9
LC
106(test-assert ">>= with more than two arguments"
107 (every (lambda (monad run)
108 (let ((1+ (lift1 1+ monad))
109 (2* (lift1 (cut * 2 <>) monad)))
110 (with-monad monad
111 (let ((number (random 777)))
112 (= (run (>>= (return number)
113 1+ 1+ 1+
114 2* 2* 2*))
115 (* 8 (+ number 3)))))))
116 %monads
117 %monad-run))
118
405a9d4e
LC
119(test-assert "mbegin"
120 (every (lambda (monad run)
121 (with-monad monad
122 (let* ((been-there? #f)
123 (number (mbegin monad
124 (return 1)
125 (begin
126 (set! been-there? #t)
127 (return 2))
128 (return 3))))
129 (and (= (run number) 3)
130 been-there?))))
131 %monads
132 %monad-run))
133
b860f382
LC
134(test-assert "mlet* + text-file + package-file"
135 (run-with-store %store
136 (mlet* %store-monad ((guile (package-file %bootstrap-guile "bin/guile"))
137 (file (text-file "monadic" guile)))
138 (return (equal? (call-with-input-file file get-string-all)
139 guile)))
140 #:guile-for-build (package-derivation %store %bootstrap-guile)))
141
c90ddc8f
LC
142(test-assert "package-file, default system"
143 ;; The default system should be the one at '>>=' time, not the one at
144 ;; invocation time. See <http://bugs.gnu.org/18002>.
145 (run-with-store %store
146 (mlet* %store-monad
147 ((system -> (%current-system))
148 (file (parameterize ((%current-system "foobar64-linux"))
149 (package-file coreutils "bin/ls")))
150 (cu (package->derivation coreutils)))
151 (return (string=? file
152 (string-append (derivation->output-path cu)
153 "/bin/ls"))))
154 #:guile-for-build (package-derivation %store %bootstrap-guile)))
155
4231f05b
LC
156(test-assert "package-file + package->cross-derivation"
157 (run-with-store %store
b4469d8c
LC
158 (mlet* %store-monad ((target -> "mips64el-linux-gnu")
159 (file (package-file coreutils "bin/ls"
160 #:target target))
161 (xcu (package->cross-derivation coreutils target)))
4231f05b
LC
162 (let ((output (derivation->output-path xcu)))
163 (return (string=? file (string-append output "/bin/ls")))))
164 #:guile-for-build (package-derivation %store %bootstrap-guile)))
165
0a90af15
LC
166(test-assert "interned-file"
167 (run-with-store %store
168 (mlet* %store-monad ((file -> (search-path %load-path "guix.scm"))
169 (a (interned-file file))
170 (b (interned-file file "b")))
171 (return (equal? (call-with-input-file file get-string-all)
172 (call-with-input-file a get-string-all)
173 (call-with-input-file b get-string-all))))
174 #:guile-for-build (package-derivation %store %bootstrap-guile)))
175
b860f382
LC
176(test-assert "mapm"
177 (every (lambda (monad run)
178 (with-monad monad
b734996f 179 (equal? (run (mapm monad (lift1 1+ monad) (iota 10)))
b860f382
LC
180 (map 1+ (iota 10)))))
181 %monads
182 %monad-run))
183
184(test-assert "sequence"
185 (every (lambda (monad run)
186 (let* ((input (iota 100))
187 (order '()))
188 (define (frob i)
f62435e2
LC
189 (mlet monad ((foo (return 'foo)))
190 ;; The side effect here is used to keep track of the order in
191 ;; which monadic values are bound. Perform the side effect
192 ;; within a '>>=' so that it is performed when the return
193 ;; value is actually bound.
194 (set! order (cons i order))
195 (return i)))
b860f382
LC
196
197 (and (equal? input
f62435e2 198 (run (sequence monad (map frob input))))
b860f382
LC
199
200 ;; Make sure this is from left to right.
201 (equal? order (reverse input)))))
202 %monads
203 %monad-run))
204
205(test-assert "listm"
206 (every (lambda (monad run)
207 (run (with-monad monad
208 (let ((lst (listm monad
209 (return 1) (return 2) (return 3))))
210 (mlet monad ((lst lst))
211 (return (equal? '(1 2 3) lst)))))))
212 %monads
213 %monad-run))
214
593c3fe6
LC
215(test-assert "anym"
216 (every (lambda (monad run)
217 (eq? (run (with-monad monad
b734996f
LC
218 (anym monad
219 (lift1 (lambda (x)
220 (and (odd? x) 'odd!))
221 monad)
222 (append (make-list 1000 0)
223 (list 1 2)))))
593c3fe6
LC
224 'odd!))
225 %monads
226 %monad-run))
227
81a97734
LC
228(test-equal "set-current-state"
229 (list '(a a d) 'd)
230 (values->list
231 (run-with-state
232 (mlet* %state-monad ((init (current-state))
233 (init2 (set-current-state 'b)))
234 (mbegin %state-monad
235 (set-current-state 'c)
236 (set-current-state 'd)
237 (mlet %state-monad ((last (current-state)))
238 (return (list init init2 last)))))
239 'a)))
240
241(test-equal "state-push etc."
242 (list '((z . 2) (p . (1)) (a . (1))) '(2 1))
243 (values->list
244 (run-with-state
245 (mbegin %state-monad
246 (state-push 1) ;(1)
247 (state-push 2) ;(2 1)
248 (mlet* %state-monad ((z (state-pop)) ;(1)
249 (p (current-state))
250 (a (state-push z))) ;(2 1)
251 (return `((z . ,z) (p . ,p) (a . ,a)))))
252 '())))
253
b860f382
LC
254(test-end "monads")
255
256\f
257(exit (= (test-runner-fail-count (test-runner-current)) 0))