Commit | Line | Data |
---|---|---|
b9b86078 | 1 | ;;; GNU Guix --- Functional package management for GNU |
8c9ef2c3 | 2 | ;;; Copyright © 2014, 2015, 2016 Ludovic Courtès <ludo@gnu.org> |
b9b86078 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 (guix monad-repl) | |
20 | #:use-module (guix store) | |
21 | #:use-module (guix monads) | |
e87f0591 LC |
22 | #:use-module (guix utils) |
23 | #:use-module (guix packages) | |
b9b86078 LC |
24 | #:use-module (ice-9 pretty-print) |
25 | #:use-module (system repl repl) | |
26 | #:use-module (system repl common) | |
27 | #:use-module (system repl command) | |
28 | #:use-module (system base language) | |
29 | #:use-module (system base compile) | |
30 | #:use-module (srfi srfi-26) | |
31 | #:export (run-in-store | |
32 | enter-store-monad)) | |
33 | ||
34 | ;;; Comment: | |
35 | ;;; | |
36 | ;;; This modules provides a couple of REPL meta-commands that make it easier | |
37 | ;;; to work with monadic procedures in the store monad. | |
38 | ;;; | |
39 | ;;; Code: | |
40 | ||
41 | (define* (monad-language monad run #:optional (name 'monad)) | |
42 | "Return a language with a special evaluator that causes monadic values | |
43 | to be \"run\" in MONAD using procedure RUN." | |
44 | (let ((scheme (lookup-language 'scheme))) | |
45 | (define (evaluate-monadic-expression exp env) | |
46 | (let ((mvalue (compile exp #:to 'value #:env env))) | |
47 | (run mvalue))) | |
48 | ||
49 | (make-language #:name name | |
50 | #:title "Monad" | |
51 | #:reader (language-reader scheme) | |
52 | #:compilers (language-compilers scheme) | |
53 | #:decompilers (language-decompilers scheme) | |
54 | #:evaluator evaluate-monadic-expression | |
55 | #:printer (language-printer scheme) | |
56 | #:make-default-environment | |
57 | (language-make-default-environment scheme)))) | |
58 | ||
e87f0591 LC |
59 | (define* (default-guile-derivation store #:optional (system (%current-system))) |
60 | "Return the derivation of the default " | |
61 | (package-derivation store (default-guile) system)) | |
62 | ||
8c9ef2c3 LC |
63 | (define (store-monad-language store) |
64 | "Return a compiler language for the store monad using STORE." | |
65 | (let ((guile (or (%guile-for-build) | |
66 | (default-guile-derivation store)))) | |
b9b86078 | 67 | (monad-language %store-monad |
e87f0591 LC |
68 | (cut run-with-store store <> |
69 | #:guile-for-build guile) | |
b9b86078 LC |
70 | 'store-monad))) |
71 | ||
72 | (define-meta-command ((run-in-store guix) repl (form)) | |
73 | "run-in-store EXP | |
74 | Run EXP through the store monad." | |
e87f0591 LC |
75 | (with-store store |
76 | (let* ((guile (or (%guile-for-build) | |
77 | (default-guile-derivation store))) | |
78 | (value (run-with-store store (repl-eval repl form) | |
79 | #:guile-for-build guile))) | |
80 | (run-hook before-print-hook value) | |
81 | (pretty-print value)))) | |
b9b86078 LC |
82 | |
83 | (define-meta-command ((enter-store-monad guix) repl) | |
84 | "enter-store-monad | |
85 | Enter a REPL for values in the store monad." | |
8c9ef2c3 LC |
86 | (with-store store |
87 | (let ((new (make-repl (store-monad-language store)))) | |
88 | ;; Force interpretation so that our specially-crafted language evaluator | |
89 | ;; is actually used. | |
90 | (repl-option-set! new 'interp #t) | |
91 | (run-repl new)))) | |
b9b86078 LC |
92 | |
93 | ;;; monad-repl.scm ends here |