Commit | Line | Data |
---|---|---|
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) | |
dbbc248a LC |
98 | (let ((f (lift1 1+ monad)) |
99 | (g (apply lift1 1+ (list monad)))) | |
b860f382 LC |
100 | (with-monad monad |
101 | (let ((number (random 777))) | |
102 | (= (run (>>= (return number) f)) | |
dbbc248a | 103 | (run (>>= (return number) g)) |
b860f382 LC |
104 | (1+ number)))))) |
105 | %monads | |
106 | %monad-run)) | |
107 | ||
751630c9 LC |
108 | (test-assert ">>= with more than two arguments" |
109 | (every (lambda (monad run) | |
110 | (let ((1+ (lift1 1+ monad)) | |
111 | (2* (lift1 (cut * 2 <>) monad))) | |
112 | (with-monad monad | |
113 | (let ((number (random 777))) | |
114 | (= (run (>>= (return number) | |
115 | 1+ 1+ 1+ | |
116 | 2* 2* 2*)) | |
117 | (* 8 (+ number 3))))))) | |
118 | %monads | |
119 | %monad-run)) | |
120 | ||
405a9d4e LC |
121 | (test-assert "mbegin" |
122 | (every (lambda (monad run) | |
123 | (with-monad monad | |
124 | (let* ((been-there? #f) | |
125 | (number (mbegin monad | |
126 | (return 1) | |
127 | (begin | |
128 | (set! been-there? #t) | |
129 | (return 2)) | |
130 | (return 3)))) | |
131 | (and (= (run number) 3) | |
132 | been-there?)))) | |
133 | %monads | |
134 | %monad-run)) | |
135 | ||
b860f382 LC |
136 | (test-assert "mlet* + text-file + package-file" |
137 | (run-with-store %store | |
138 | (mlet* %store-monad ((guile (package-file %bootstrap-guile "bin/guile")) | |
139 | (file (text-file "monadic" guile))) | |
140 | (return (equal? (call-with-input-file file get-string-all) | |
141 | guile))) | |
142 | #:guile-for-build (package-derivation %store %bootstrap-guile))) | |
143 | ||
c90ddc8f LC |
144 | (test-assert "package-file, default system" |
145 | ;; The default system should be the one at '>>=' time, not the one at | |
146 | ;; invocation time. See <http://bugs.gnu.org/18002>. | |
147 | (run-with-store %store | |
148 | (mlet* %store-monad | |
149 | ((system -> (%current-system)) | |
150 | (file (parameterize ((%current-system "foobar64-linux")) | |
151 | (package-file coreutils "bin/ls"))) | |
152 | (cu (package->derivation coreutils))) | |
153 | (return (string=? file | |
154 | (string-append (derivation->output-path cu) | |
155 | "/bin/ls")))) | |
156 | #:guile-for-build (package-derivation %store %bootstrap-guile))) | |
157 | ||
4231f05b LC |
158 | (test-assert "package-file + package->cross-derivation" |
159 | (run-with-store %store | |
b4469d8c LC |
160 | (mlet* %store-monad ((target -> "mips64el-linux-gnu") |
161 | (file (package-file coreutils "bin/ls" | |
162 | #:target target)) | |
163 | (xcu (package->cross-derivation coreutils target))) | |
4231f05b LC |
164 | (let ((output (derivation->output-path xcu))) |
165 | (return (string=? file (string-append output "/bin/ls"))))) | |
166 | #:guile-for-build (package-derivation %store %bootstrap-guile))) | |
167 | ||
0a90af15 LC |
168 | (test-assert "interned-file" |
169 | (run-with-store %store | |
170 | (mlet* %store-monad ((file -> (search-path %load-path "guix.scm")) | |
171 | (a (interned-file file)) | |
172 | (b (interned-file file "b"))) | |
173 | (return (equal? (call-with-input-file file get-string-all) | |
174 | (call-with-input-file a get-string-all) | |
175 | (call-with-input-file b get-string-all)))) | |
176 | #:guile-for-build (package-derivation %store %bootstrap-guile))) | |
177 | ||
b860f382 LC |
178 | (test-assert "mapm" |
179 | (every (lambda (monad run) | |
180 | (with-monad monad | |
b734996f | 181 | (equal? (run (mapm monad (lift1 1+ monad) (iota 10))) |
b860f382 LC |
182 | (map 1+ (iota 10))))) |
183 | %monads | |
184 | %monad-run)) | |
185 | ||
186 | (test-assert "sequence" | |
187 | (every (lambda (monad run) | |
188 | (let* ((input (iota 100)) | |
189 | (order '())) | |
190 | (define (frob i) | |
f62435e2 LC |
191 | (mlet monad ((foo (return 'foo))) |
192 | ;; The side effect here is used to keep track of the order in | |
193 | ;; which monadic values are bound. Perform the side effect | |
194 | ;; within a '>>=' so that it is performed when the return | |
195 | ;; value is actually bound. | |
196 | (set! order (cons i order)) | |
197 | (return i))) | |
b860f382 LC |
198 | |
199 | (and (equal? input | |
f62435e2 | 200 | (run (sequence monad (map frob input)))) |
b860f382 LC |
201 | |
202 | ;; Make sure this is from left to right. | |
203 | (equal? order (reverse input))))) | |
204 | %monads | |
205 | %monad-run)) | |
206 | ||
207 | (test-assert "listm" | |
208 | (every (lambda (monad run) | |
209 | (run (with-monad monad | |
210 | (let ((lst (listm monad | |
211 | (return 1) (return 2) (return 3)))) | |
212 | (mlet monad ((lst lst)) | |
213 | (return (equal? '(1 2 3) lst))))))) | |
214 | %monads | |
215 | %monad-run)) | |
216 | ||
593c3fe6 LC |
217 | (test-assert "anym" |
218 | (every (lambda (monad run) | |
219 | (eq? (run (with-monad monad | |
b734996f LC |
220 | (anym monad |
221 | (lift1 (lambda (x) | |
222 | (and (odd? x) 'odd!)) | |
223 | monad) | |
224 | (append (make-list 1000 0) | |
225 | (list 1 2))))) | |
593c3fe6 LC |
226 | 'odd!)) |
227 | %monads | |
228 | %monad-run)) | |
229 | ||
81a97734 LC |
230 | (test-equal "set-current-state" |
231 | (list '(a a d) 'd) | |
232 | (values->list | |
233 | (run-with-state | |
234 | (mlet* %state-monad ((init (current-state)) | |
235 | (init2 (set-current-state 'b))) | |
236 | (mbegin %state-monad | |
237 | (set-current-state 'c) | |
238 | (set-current-state 'd) | |
239 | (mlet %state-monad ((last (current-state))) | |
240 | (return (list init init2 last))))) | |
241 | 'a))) | |
242 | ||
243 | (test-equal "state-push etc." | |
244 | (list '((z . 2) (p . (1)) (a . (1))) '(2 1)) | |
245 | (values->list | |
246 | (run-with-state | |
247 | (mbegin %state-monad | |
248 | (state-push 1) ;(1) | |
249 | (state-push 2) ;(2 1) | |
250 | (mlet* %state-monad ((z (state-pop)) ;(1) | |
251 | (p (current-state)) | |
252 | (a (state-push z))) ;(2 1) | |
253 | (return `((z . ,z) (p . ,p) (a . ,a))))) | |
254 | '()))) | |
255 | ||
b860f382 LC |
256 | (test-end "monads") |
257 | ||
258 | \f | |
259 | (exit (= (test-runner-fail-count (test-runner-current)) 0)) |