gnu: glog: Update to 0.4.0.
[guix.git] / tests / monads.scm
blob18bf4119bed816dc85f1cbe5fc0252239cfd43ef
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015, 2016 Ludovic Courtès <ludo@gnu.org>
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/>.
19 (define-module (test-monads)
20   #:use-module (guix tests)
21   #:use-module (guix store)
22   #:use-module (guix monads)
23   #:use-module (guix grafts)
24   #:use-module (guix derivations)
25   #:use-module (guix packages)
26   #:use-module (gnu packages)
27   #:use-module (gnu packages bootstrap)
28   #:use-module ((gnu packages base) #:select (coreutils))
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))
35 ;; Test the (guix monads) module.
37 (define %store
38   (open-connection-for-tests))
40 ;; Globally disable grafts because they can trigger early builds.
41 (%graft? #f)
43 (define %monads
44   (list %identity-monad %store-monad %state-monad))
46 (define %monad-run
47   (list identity
48         (cut run-with-store %store <>)
49         (cut run-with-state <> '())))
51 (define-syntax-rule (values->list exp)
52   (call-with-values (lambda () exp)
53     list))
56 (test-begin "monads")
58 (test-assert "monad?"
59   (and (every monad? %monads)
60        (every (compose procedure? monad-bind) %monads)
61        (every (compose procedure? monad-return) %monads)))
63 ;; The 3 "monad laws": <http://www.haskell.org/haskellwiki/Monad_laws>.
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)))
72                (= (run (>>= (return number) f))
73                   (run (f number))))))
74          %monads
75          %monad-run))
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))
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)))
94              (let ((number (return (random 777))))
95                (= (run (>>= (>>= number f) g))
96                   (run (>>= number (lambda (x) (>>= (f x) g))))))))
97          %monads
98          %monad-run))
100 (test-assert "lift"
101   (every (lambda (monad run)
102            (let ((f (lift1 1+ monad))
103                  (g (apply lift1 1+ (list monad))))
104              (with-monad monad
105                (let ((number (random 777)))
106                  (= (run (>>= (return number) f))
107                     (run (>>= (return number) g))
108                     (1+ number))))))
109          %monads
110          %monad-run))
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))
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))
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)))
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)))
162 (test-assert "package-file + package->cross-derivation"
163   (run-with-store %store
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)))
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)))
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)))
182 (test-assert "mapm"
183   (every (lambda (monad run)
184            (with-monad monad
185              (equal? (run (mapm monad (lift1 1+ monad) (iota 10)))
186                      (map 1+ (iota 10)))))
187          %monads
188          %monad-run))
190 (test-assert "sequence"
191   (every (lambda (monad run)
192            (let* ((input (iota 100))
193                   (order '()))
194              (define (frob i)
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)))
203              (and (equal? input
204                           (run (sequence monad (map frob input))))
206                   ;; Make sure this is from left to right.
207                   (equal? order (reverse input)))))
208          %monads
209          %monad-run))
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))
221 (test-assert "anym"
222   (every (lambda (monad run)
223            (eq? (run (with-monad monad
224                        (anym monad
225                              (lift1 (lambda (x)
226                                       (and (odd? x) 'odd!))
227                                     monad)
228                              (append (make-list 1000 0)
229                                      (list 1 2)))))
230                 'odd!))
231          %monads
232          %monad-run))
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)))
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      '())))
260 (test-end "monads")