gnu: linux-libre: Update to 3.18.3
[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
40 (list %identity-monad %store-monad))
41
42(define %monad-run
43 (list identity
44 (cut run-with-store %store <>)))
45
46\f
47(test-begin "monads")
48
aeb7ec5c
LC
49(test-assert "monad?"
50 (and (every monad? %monads)
51 (every (compose procedure? monad-bind) %monads)
52 (every (compose procedure? monad-return) %monads)))
53
b860f382
LC
54;; The 3 "monad laws": <http://www.haskell.org/haskellwiki/Monad_laws>.
55
56(test-assert "left identity"
57 (every (lambda (monad run)
58 (let ((number (random 777)))
59 (with-monad monad
60 (define (f x)
61 (return (* (1+ number) 2)))
62
63 (= (run (>>= (return number) f))
64 (run (f number))))))
65 %monads
66 %monad-run))
67
68(test-assert "right identity"
69 (every (lambda (monad run)
70 (with-monad monad
71 (let ((number (return (random 777))))
72 (= (run (>>= number return))
73 (run number)))))
74 %monads
75 %monad-run))
76
77(test-assert "associativity"
78 (every (lambda (monad run)
79 (with-monad monad
80 (define (f x)
81 (return (+ 1 x)))
82 (define (g x)
83 (return (* 2 x)))
84
85 (let ((number (return (random 777))))
86 (= (run (>>= (>>= number f) g))
87 (run (>>= number (lambda (x) (>>= (f x) g))))))))
88 %monads
89 %monad-run))
90
91(test-assert "lift"
92 (every (lambda (monad run)
93 (let ((f (lift1 1+ monad)))
94 (with-monad monad
95 (let ((number (random 777)))
96 (= (run (>>= (return number) f))
97 (1+ number))))))
98 %monads
99 %monad-run))
100
405a9d4e
LC
101(test-assert "mbegin"
102 (every (lambda (monad run)
103 (with-monad monad
104 (let* ((been-there? #f)
105 (number (mbegin monad
106 (return 1)
107 (begin
108 (set! been-there? #t)
109 (return 2))
110 (return 3))))
111 (and (= (run number) 3)
112 been-there?))))
113 %monads
114 %monad-run))
115
b860f382
LC
116(test-assert "mlet* + text-file + package-file"
117 (run-with-store %store
118 (mlet* %store-monad ((guile (package-file %bootstrap-guile "bin/guile"))
119 (file (text-file "monadic" guile)))
120 (return (equal? (call-with-input-file file get-string-all)
121 guile)))
122 #:guile-for-build (package-derivation %store %bootstrap-guile)))
123
c90ddc8f
LC
124(test-assert "package-file, default system"
125 ;; The default system should be the one at '>>=' time, not the one at
126 ;; invocation time. See <http://bugs.gnu.org/18002>.
127 (run-with-store %store
128 (mlet* %store-monad
129 ((system -> (%current-system))
130 (file (parameterize ((%current-system "foobar64-linux"))
131 (package-file coreutils "bin/ls")))
132 (cu (package->derivation coreutils)))
133 (return (string=? file
134 (string-append (derivation->output-path cu)
135 "/bin/ls"))))
136 #:guile-for-build (package-derivation %store %bootstrap-guile)))
137
4231f05b
LC
138(test-assert "package-file + package->cross-derivation"
139 (run-with-store %store
b4469d8c
LC
140 (mlet* %store-monad ((target -> "mips64el-linux-gnu")
141 (file (package-file coreutils "bin/ls"
142 #:target target))
143 (xcu (package->cross-derivation coreutils target)))
4231f05b
LC
144 (let ((output (derivation->output-path xcu)))
145 (return (string=? file (string-append output "/bin/ls")))))
146 #:guile-for-build (package-derivation %store %bootstrap-guile)))
147
0a90af15
LC
148(test-assert "interned-file"
149 (run-with-store %store
150 (mlet* %store-monad ((file -> (search-path %load-path "guix.scm"))
151 (a (interned-file file))
152 (b (interned-file file "b")))
153 (return (equal? (call-with-input-file file get-string-all)
154 (call-with-input-file a get-string-all)
155 (call-with-input-file b get-string-all))))
156 #:guile-for-build (package-derivation %store %bootstrap-guile)))
157
b860f382
LC
158(test-assert "mapm"
159 (every (lambda (monad run)
160 (with-monad monad
161 (equal? (run (mapm monad (lift1 1+ monad) (map return (iota 10))))
162 (map 1+ (iota 10)))))
163 %monads
164 %monad-run))
165
166(test-assert "sequence"
167 (every (lambda (monad run)
168 (let* ((input (iota 100))
169 (order '()))
170 (define (frob i)
f62435e2
LC
171 (mlet monad ((foo (return 'foo)))
172 ;; The side effect here is used to keep track of the order in
173 ;; which monadic values are bound. Perform the side effect
174 ;; within a '>>=' so that it is performed when the return
175 ;; value is actually bound.
176 (set! order (cons i order))
177 (return i)))
b860f382
LC
178
179 (and (equal? input
f62435e2 180 (run (sequence monad (map frob input))))
b860f382
LC
181
182 ;; Make sure this is from left to right.
183 (equal? order (reverse input)))))
184 %monads
185 %monad-run))
186
187(test-assert "listm"
188 (every (lambda (monad run)
189 (run (with-monad monad
190 (let ((lst (listm monad
191 (return 1) (return 2) (return 3))))
192 (mlet monad ((lst lst))
193 (return (equal? '(1 2 3) lst)))))))
194 %monads
195 %monad-run))
196
593c3fe6
LC
197(test-assert "anym"
198 (every (lambda (monad run)
199 (eq? (run (with-monad monad
200 (let ((lst (list (return 1) (return 2) (return 3))))
201 (anym monad
202 (lambda (x)
203 (and (odd? x) 'odd!))
204 lst))))
205 'odd!))
206 %monads
207 %monad-run))
208
b860f382
LC
209(test-end "monads")
210
211\f
212(exit (= (test-runner-fail-count (test-runner-current)) 0))