monad-repl.scm 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2014, 2015, 2016, 2022 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 (guix status)
  24. #:autoload (guix gexp) (lower-object)
  25. #:use-module ((guix derivations)
  26. #:select (derivation?
  27. derivation->output-paths built-derivations))
  28. #:use-module (ice-9 match)
  29. #:use-module (ice-9 pretty-print)
  30. #:use-module (system repl repl)
  31. #:use-module (system repl common)
  32. #:use-module (system repl command)
  33. #:use-module (system base language)
  34. #:use-module (system base compile)
  35. #:use-module (srfi srfi-26)
  36. #:export (run-in-store
  37. enter-store-monad))
  38. ;;; Comment:
  39. ;;;
  40. ;;; This modules provides a couple of REPL meta-commands that make it easier
  41. ;;; to work with monadic procedures in the store monad.
  42. ;;;
  43. ;;; Code:
  44. (define* (monad-language monad run #:optional (name 'monad))
  45. "Return a language with a special evaluator that causes monadic values
  46. to be \"run\" in MONAD using procedure RUN."
  47. (let ((scheme (lookup-language 'scheme)))
  48. (define (evaluate-monadic-expression exp env)
  49. (let ((mvalue (compile exp #:to 'value #:env env)))
  50. (run mvalue)))
  51. (make-language #:name name
  52. #:title "Monad"
  53. #:reader (language-reader scheme)
  54. #:compilers (language-compilers scheme)
  55. #:decompilers (language-decompilers scheme)
  56. #:evaluator evaluate-monadic-expression
  57. #:printer (language-printer scheme)
  58. #:make-default-environment
  59. (language-make-default-environment scheme))))
  60. (define* (default-guile-derivation store #:optional (system (%current-system)))
  61. "Return the derivation of the default "
  62. (package-derivation store (default-guile) system))
  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))))
  67. (monad-language %store-monad
  68. (cut run-with-store store <>
  69. #:guile-for-build guile)
  70. 'store-monad)))
  71. (define %build-verbosity
  72. ;; Current build verbosity level.
  73. 1)
  74. (define* (evaluate/print-with-store mvalue #:key build?)
  75. "Run monadic value MVALUE in the store monad and print its value."
  76. (with-store store
  77. (set-build-options store
  78. #:print-build-trace #t
  79. #:print-extended-build-trace? #t
  80. #:multiplexed-build-output? #t)
  81. (with-status-verbosity %build-verbosity
  82. (let* ((guile (or (%guile-for-build)
  83. (default-guile-derivation store)))
  84. (values (run-with-store store
  85. (if build?
  86. (mlet %store-monad ((obj mvalue))
  87. (if (derivation? obj)
  88. (mbegin %store-monad
  89. (built-derivations (list obj))
  90. (return
  91. (match (derivation->output-paths obj)
  92. (((_ . files) ...) files))))
  93. (return (list obj))))
  94. (mlet %store-monad ((obj mvalue))
  95. (return (list obj))))
  96. #:guile-for-build guile)))
  97. (for-each (lambda (value)
  98. (run-hook before-print-hook value)
  99. (pretty-print value))
  100. values)))))
  101. (define-meta-command ((run-in-store guix) repl (form))
  102. "run-in-store EXP
  103. Run EXP through the store monad."
  104. (evaluate/print-with-store (repl-eval repl form)))
  105. (define-meta-command ((verbosity guix) repl (level))
  106. "verbosity LEVEL
  107. Change build verbosity to LEVEL."
  108. (set! %build-verbosity (repl-eval repl level)))
  109. (define-meta-command ((lower guix) repl (form))
  110. "lower OBJECT
  111. Lower OBJECT into a derivation or store file and return it."
  112. (evaluate/print-with-store (lower-object (repl-eval repl form))))
  113. (define-meta-command ((build guix) repl (form))
  114. "build OBJECT
  115. Lower OBJECT and build it, returning its output file name(s)."
  116. (evaluate/print-with-store (lower-object (repl-eval repl form))
  117. #:build? #t))
  118. (define-meta-command ((enter-store-monad guix) repl)
  119. "enter-store-monad
  120. Enter a REPL for values in the store monad."
  121. (with-store store
  122. (let ((new (make-repl (store-monad-language store))))
  123. ;; Force interpretation so that our specially-crafted language evaluator
  124. ;; is actually used.
  125. (repl-option-set! new 'interp #t)
  126. (run-repl new))))
  127. ;;; monad-repl.scm ends here