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