You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

82 lines
3.0 KiB

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