monad-repl.scm 3.5 KB

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