ob-haskell.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. ;;; ob-haskell.el --- Babel Functions for Haskell -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2009-2022 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte
  4. ;; Maintainer: Lawrence Bottorff <borgauf@gmail.com>
  5. ;; Keywords: literate programming, reproducible research
  6. ;; Homepage: https://orgmode.org
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; Org Babel support for evaluating Haskell source code.
  20. ;; Haskell programs must be compiled before
  21. ;; they can be run, but haskell code can also be run through an
  22. ;; interactive interpreter.
  23. ;;
  24. ;; By default we evaluate using the Haskell interpreter.
  25. ;; To use the compiler, specify :compile yes in the header.
  26. ;;; Requirements:
  27. ;; - haskell-mode: https://www.iro.umontreal.ca/~monnier/elisp/#haskell-mode
  28. ;; - inf-haskell: https://www.iro.umontreal.ca/~monnier/elisp/#haskell-mode
  29. ;; - (optionally) lhs2tex: https://people.cs.uu.nl/andres/lhs2tex/
  30. ;;; Code:
  31. (require 'ob)
  32. (require 'org-macs)
  33. (require 'comint)
  34. (declare-function haskell-mode "ext:haskell-mode" ())
  35. (declare-function run-haskell "ext:inf-haskell" (&optional arg))
  36. (declare-function inferior-haskell-load-file
  37. "ext:inf-haskell" (&optional reload))
  38. (declare-function org-entry-get "org" (pom property &optional inherit literal-nil))
  39. (defvar org-babel-tangle-lang-exts)
  40. (add-to-list 'org-babel-tangle-lang-exts '("haskell" . "hs"))
  41. (defvar org-babel-default-header-args:haskell
  42. '((:padlines . "no")))
  43. (defvar org-babel-haskell-lhs2tex-command "lhs2tex")
  44. (defvar org-babel-haskell-eoe "\"org-babel-haskell-eoe\"")
  45. (defvar haskell-prompt-regexp)
  46. (defcustom org-babel-haskell-compiler "ghc"
  47. "Command used to compile a Haskell source code file into an executable.
  48. May be either a command in the path, like \"ghc\" or an absolute
  49. path name, like \"/usr/local/bin/ghc\". The command can include
  50. a parameter, such as \"ghc -v\"."
  51. :group 'org-babel
  52. :package-version '(Org "9.4")
  53. :type 'string)
  54. (defconst org-babel-header-args:haskell '((compile . :any))
  55. "Haskell-specific header arguments.")
  56. (defun org-babel-haskell-execute (body params)
  57. "This function should only be called by `org-babel-execute:haskell'."
  58. (let* ((tmp-src-file (org-babel-temp-file "Haskell-src-" ".hs"))
  59. (tmp-bin-file
  60. (org-babel-process-file-name
  61. (org-babel-temp-file "Haskell-bin-" org-babel-exeext)))
  62. (cmdline (cdr (assq :cmdline params)))
  63. (cmdline (if cmdline (concat " " cmdline) ""))
  64. (flags (cdr (assq :flags params)))
  65. (flags (mapconcat #'identity
  66. (if (listp flags)
  67. flags
  68. (list flags))
  69. " "))
  70. (libs (org-babel-read
  71. (or (cdr (assq :libs params))
  72. (org-entry-get nil "libs" t))
  73. nil))
  74. (libs (mapconcat #'identity
  75. (if (listp libs) libs (list libs))
  76. " ")))
  77. (with-temp-file tmp-src-file (insert body))
  78. (org-babel-eval
  79. (format "%s -o %s %s %s %s"
  80. org-babel-haskell-compiler
  81. tmp-bin-file
  82. flags
  83. (org-babel-process-file-name tmp-src-file)
  84. libs)
  85. "")
  86. (let ((results (org-babel-eval (concat tmp-bin-file cmdline) "")))
  87. (when results
  88. (setq results (org-trim (org-remove-indentation results)))
  89. (org-babel-reassemble-table
  90. (org-babel-result-cond (cdr (assq :result-params params))
  91. (org-babel-read results t)
  92. (let ((tmp-file (org-babel-temp-file "Haskell-")))
  93. (with-temp-file tmp-file (insert results))
  94. (org-babel-import-elisp-from-file tmp-file)))
  95. (org-babel-pick-name
  96. (cdr (assq :colname-names params)) (cdr (assq :colnames params)))
  97. (org-babel-pick-name
  98. (cdr (assq :rowname-names params)) (cdr (assq :rownames params))))))))
  99. (defun org-babel-interpret-haskell (body params)
  100. (require 'inf-haskell)
  101. (add-hook 'inferior-haskell-hook
  102. (lambda ()
  103. (setq-local comint-prompt-regexp
  104. (concat haskell-prompt-regexp "\\|^λ?> "))))
  105. (let* ((session (cdr (assq :session params)))
  106. (result-type (cdr (assq :result-type params)))
  107. (full-body (org-babel-expand-body:generic
  108. body params
  109. (org-babel-variable-assignments:haskell params)))
  110. (session (org-babel-haskell-initiate-session session params))
  111. (comint-preoutput-filter-functions
  112. (cons 'ansi-color-filter-apply comint-preoutput-filter-functions))
  113. (raw (org-babel-comint-with-output
  114. (session org-babel-haskell-eoe t full-body)
  115. (insert (org-trim full-body))
  116. (comint-send-input nil t)
  117. (insert org-babel-haskell-eoe)
  118. (comint-send-input nil t)))
  119. (results (mapcar #'org-strip-quotes
  120. (cdr (member org-babel-haskell-eoe
  121. (reverse (mapcar #'org-trim raw)))))))
  122. (org-babel-reassemble-table
  123. (let ((result
  124. (pcase result-type
  125. (`output (mapconcat #'identity (reverse results) "\n"))
  126. (`value (car results)))))
  127. (org-babel-result-cond (cdr (assq :result-params params))
  128. result (org-babel-script-escape result)))
  129. (org-babel-pick-name (cdr (assq :colname-names params))
  130. (cdr (assq :colname-names params)))
  131. (org-babel-pick-name (cdr (assq :rowname-names params))
  132. (cdr (assq :rowname-names params))))))
  133. (defun org-babel-execute:haskell (body params)
  134. "Execute a block of Haskell code."
  135. (let ((compile (string= "yes" (cdr (assq :compile params)))))
  136. (if (not compile)
  137. (org-babel-interpret-haskell body params)
  138. (org-babel-haskell-execute body params))))
  139. (defun org-babel-haskell-initiate-session (&optional _session _params)
  140. "Initiate a haskell session.
  141. If there is not a current inferior-process-buffer in SESSION
  142. then create one. Return the initialized session."
  143. (require 'inf-haskell)
  144. (or (get-buffer "*haskell*")
  145. (save-window-excursion (run-haskell) (sleep-for 0.25) (current-buffer))))
  146. (defun org-babel-load-session:haskell (session body params)
  147. "Load BODY into SESSION."
  148. (save-window-excursion
  149. (let* ((buffer (org-babel-prep-session:haskell session params))
  150. (load-file (concat (org-babel-temp-file "haskell-load-") ".hs")))
  151. (with-temp-buffer
  152. (insert body) (write-file load-file)
  153. (haskell-mode) (inferior-haskell-load-file))
  154. buffer)))
  155. (defun org-babel-prep-session:haskell (session params)
  156. "Prepare SESSION according to the header arguments in PARAMS."
  157. (save-window-excursion
  158. (let ((buffer (org-babel-haskell-initiate-session session)))
  159. (org-babel-comint-in-buffer buffer
  160. (mapc (lambda (line)
  161. (insert line)
  162. (comint-send-input nil t))
  163. (org-babel-variable-assignments:haskell params)))
  164. (current-buffer))))
  165. (defun org-babel-variable-assignments:haskell (params)
  166. "Return list of haskell statements assigning the block's variables."
  167. (mapcar (lambda (pair)
  168. (format "let %s = %s"
  169. (car pair)
  170. (org-babel-haskell-var-to-haskell (cdr pair))))
  171. (org-babel--get-vars params)))
  172. (defun org-babel-haskell-var-to-haskell (var)
  173. "Convert an elisp value VAR into a haskell variable.
  174. The elisp VAR is converted to a string of haskell source code
  175. specifying a variable of the same value."
  176. (if (listp var)
  177. (concat "[" (mapconcat #'org-babel-haskell-var-to-haskell var ", ") "]")
  178. (format "%S" var)))
  179. (defvar org-export-copy-to-kill-ring)
  180. (declare-function org-export-to-file "ox"
  181. (backend file
  182. &optional async subtreep visible-only body-only
  183. ext-plist post-process))
  184. (defun org-babel-haskell-export-to-lhs (&optional arg)
  185. "Export to a .lhs file with all haskell code blocks escaped.
  186. When called with a prefix argument the resulting
  187. .lhs file will be exported to a .tex file. This function will
  188. create two new files, base-name.lhs and base-name.tex where
  189. base-name is the name of the current Org file.
  190. Note that all standard Babel literate programming
  191. constructs (header arguments, no-web syntax etc...) are ignored."
  192. (interactive "P")
  193. (let* ((contents (buffer-string))
  194. (haskell-regexp
  195. (concat "^\\([ \t]*\\)#\\+begin_src[ \t]haskell*\\(.*\\)[\r\n]"
  196. "\\([^\000]*?\\)[\r\n][ \t]*#\\+end_src.*"))
  197. (base-name (file-name-sans-extension (buffer-file-name)))
  198. (tmp-file (org-babel-temp-file "haskell-"))
  199. (tmp-org-file (concat tmp-file ".org"))
  200. (tmp-tex-file (concat tmp-file ".tex"))
  201. (lhs-file (concat base-name ".lhs"))
  202. (tex-file (concat base-name ".tex"))
  203. (command (concat org-babel-haskell-lhs2tex-command
  204. " " (org-babel-process-file-name lhs-file)
  205. " > " (org-babel-process-file-name tex-file)))
  206. (preserve-indentp org-src-preserve-indentation)
  207. indentation)
  208. ;; escape haskell source-code blocks
  209. (with-temp-file tmp-org-file
  210. (insert contents)
  211. (goto-char (point-min))
  212. (while (re-search-forward haskell-regexp nil t)
  213. (save-match-data (setq indentation (length (match-string 1))))
  214. (replace-match (save-match-data
  215. (concat
  216. "#+begin_export latex\n\\begin{code}\n"
  217. (if (or preserve-indentp
  218. (string-match "-i" (match-string 2)))
  219. (match-string 3)
  220. (org-remove-indentation (match-string 3)))
  221. "\n\\end{code}\n#+end_export\n"))
  222. t t)
  223. (indent-code-rigidly (match-beginning 0) (match-end 0) indentation)))
  224. (save-excursion
  225. ;; export to latex w/org and save as .lhs
  226. (require 'ox-latex)
  227. (find-file tmp-org-file)
  228. ;; Ensure we do not clutter kill ring with incomplete results.
  229. (let (org-export-copy-to-kill-ring)
  230. (org-export-to-file 'latex tmp-tex-file))
  231. (kill-buffer nil)
  232. (delete-file tmp-org-file)
  233. (find-file tmp-tex-file)
  234. (goto-char (point-min)) (forward-line 2)
  235. (insert "%include polycode.fmt\n")
  236. ;; ensure all \begin/end{code} statements start at the first column
  237. (while (re-search-forward "^[ \t]+\\\\begin{code}[^\000]+\\\\end{code}" nil t)
  238. (replace-match (save-match-data (org-remove-indentation (match-string 0)))
  239. t t))
  240. (setq contents (buffer-string))
  241. (save-buffer) (kill-buffer nil))
  242. (delete-file tmp-tex-file)
  243. ;; save org exported latex to a .lhs file
  244. (with-temp-file lhs-file (insert contents))
  245. (if (not arg)
  246. (find-file lhs-file)
  247. ;; process .lhs file with lhs2tex
  248. (message "running %s" command) (shell-command command) (find-file tex-file))))
  249. (provide 'ob-haskell)
  250. ;;; ob-haskell.el ends here