all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* bug#40852: Support compilation of Haskell in org mode babel blocks.
@ 2020-04-25 19:04 Roland Coeurjoly
  2020-04-27 18:20 ` bug#40852: Fix patch Roland Coeurjoly
                   ` (2 more replies)
  0 siblings, 3 replies; 25+ messages in thread
From: Roland Coeurjoly @ 2020-04-25 19:04 UTC (permalink / raw)
  To: 40852


[-- Attachment #1.1: Type: text/plain, Size: 803 bytes --]

Haskell code can be both compiled (for example with ghc), or interpreted
(with ghci).

Until now, org babel had only support for interpretation.

Haskell is weird in that some code for the interpreter cannot be compiled
and viceversa.
For example, in ghci (the interpreter) you are required to use let to
declare functions
<https://stackoverflow.com/questions/28927716/org-babel-for-haskell-not-works-of-eval-haskell-block/40920873>
.

In this patch I add support for compilation with the header argument
:compile yes.
The function to compile haskell is almost a copy paste of the C funcion in
ob-C.el.

By default I retain the original behavior, i.e. interpreting the block.

I have tested this patch in emacs-27.0.91.


It is my first patch to GNU Emacs and I am a newbie with both elisp and
haskell.

[-- Attachment #1.2: Type: text/html, Size: 1040 bytes --]

[-- Attachment #2: 0001-Add-Haskell-specific-header-argument-compile-to-comp.patch --]
[-- Type: text/x-patch, Size: 4247 bytes --]

From f7a7273dbf034445e0bf48c2eb2d2d16fed6668d Mon Sep 17 00:00:00 2001
From: Roland Coeurjoly <rolandcoeurjoly@gmail.com>
Date: Sat, 25 Apr 2020 20:35:22 +0200
Subject: [PATCH] Add Haskell specific header argument compile, to compile
 instead of interpret the body of source block

---
 lisp/org/ob-haskell.el | 75 +++++++++++++++++++++++++++++++++++++++---
 1 file changed, 70 insertions(+), 5 deletions(-)

diff --git a/lisp/org/ob-haskell.el b/lisp/org/ob-haskell.el
index e004a3405e..c945136f33 100644
--- a/lisp/org/ob-haskell.el
+++ b/lisp/org/ob-haskell.el
@@ -23,12 +23,13 @@
 
 ;;; Commentary:
 
-;; Org-Babel support for evaluating haskell source code.  This one will
-;; be sort of tricky because haskell programs must be compiled before
+;; Org-Babel support for evaluating Haskell source code.
+;; Haskell programs must be compiled before
 ;; they can be run, but haskell code can also be run through an
 ;; interactive interpreter.
 ;;
-;; For now lets only allow evaluation using the haskell interpreter.
+;; By default we evaluate using the Haskell interpreter.
+;; To use the compiler, specify :compile yes in the header.
 
 ;;; Requirements:
 
@@ -60,8 +61,64 @@ org-babel-haskell-eoe
 
 (defvar haskell-prompt-regexp)
 
-(defun org-babel-execute:haskell (body params)
-  "Execute a block of Haskell code."
+(defcustom org-babel-Haskell-compiler "ghc"
+  "Command used to compile a Haskell source code file into an executable.
+May be either a command in the path, like ghc
+or an absolute path name, like /usr/local/bin/ghc
+parameter may be used, like ghc -v"
+  :group 'org-babel
+  :version "27.0"
+  :type 'string)
+
+(defconst org-babel-header-args:haskell '((compile . :any))
+  "Haskell-specific header arguments.")
+
+(defun org-babel-Haskell-execute (body params)
+  "This function should only be called by `org-babel-execute:haskell'"
+  (let* ((tmp-src-file (org-babel-temp-file
+			"Haskell-src-"
+                        ".hs"))
+         (tmp-bin-file
+          (org-babel-process-file-name
+           (org-babel-temp-file "Haskell-bin-" org-babel-exeext)))
+         (cmdline (cdr (assq :cmdline params)))
+         (cmdline (if cmdline (concat " " cmdline) ""))
+         (flags (cdr (assq :flags params)))
+         (flags (mapconcat 'identity
+		           (if (listp flags) flags (list flags)) " "))
+         (libs (org-babel-read
+	        (or (cdr (assq :libs params))
+	            (org-entry-get nil "libs" t))
+	        nil))
+         (libs (mapconcat #'identity
+		          (if (listp libs) libs (list libs))
+		          " ")))
+    (with-temp-file tmp-src-file (insert body))
+    (org-babel-eval
+     (format "%s -o %s %s %s %s"
+             org-babel-Haskell-compiler
+	     tmp-bin-file
+	     flags
+	     (org-babel-process-file-name tmp-src-file)
+	     libs) "")
+    (let ((results
+	   (org-babel-eval
+	    (concat tmp-bin-file cmdline) "")))
+      (when results
+        (setq results (org-trim (org-remove-indentation results)))
+        (org-babel-reassemble-table
+         (org-babel-result-cond (cdr (assq :result-params params))
+	   (org-babel-read results t)
+	   (let ((tmp-file (org-babel-temp-file "Haskell-")))
+	     (with-temp-file tmp-file (insert results))
+	     (org-babel-import-elisp-from-file tmp-file)))
+         (org-babel-pick-name
+	  (cdr (assq :colname-names params)) (cdr (assq :colnames params)))
+         (org-babel-pick-name
+	  (cdr (assq :rowname-names params)) (cdr (assq :rownames params)))))
+      )))
+
+(defun org-babel-interpret-Haskell (body params)
   (require 'inf-haskell)
   (add-hook 'inferior-haskell-hook
             (lambda ()
@@ -96,6 +153,14 @@ org-babel-execute:haskell
      (org-babel-pick-name (cdr (assq :rowname-names params))
 			  (cdr (assq :rowname-names params))))))
 
+
+(defun org-babel-execute:haskell (body params)
+  "Execute a block of Haskell code."
+  (setq compile (string= (cdr (assq :compile params)) "yes"))
+  (if (not compile)
+      (org-babel-interpret-Haskell body params)
+    (org-babel-Haskell-execute body params)))
+
 (defun org-babel-haskell-initiate-session (&optional _session _params)
   "Initiate a haskell session.
 If there is not a current inferior-process-buffer in SESSION
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 25+ messages in thread

end of thread, other threads:[~2020-10-26 20:26 UTC | newest]

Thread overview: 25+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-04-25 19:04 bug#40852: Support compilation of Haskell in org mode babel blocks Roland Coeurjoly
2020-04-27 18:20 ` bug#40852: Fix patch Roland Coeurjoly
2020-04-28 14:56 ` Fwd: Support compilation of Haskell in org mode babel blocks Roland Coeurjoly
2020-05-04 15:26   ` Nicolas Goaziou
2020-05-04 16:39     ` Roland Coeurjoly
2020-05-04 21:49       ` Nicolas Goaziou
2020-05-05 21:16         ` Roland Coeurjoly
2020-05-05 21:31           ` Nicolas Goaziou
2020-05-05 21:42             ` Roland Coeurjoly
2020-05-22  7:04             ` Roland Coeurjoly
2020-05-22 15:30               ` Nicolas Goaziou
2020-05-22 16:17                 ` Roland Coeurjoly
2020-05-23  5:12                   ` Kyle Meyer
2020-05-23 10:30                     ` Roland Coeurjoly
2020-05-23 14:02                       ` Bastien
2020-05-23 14:51                         ` Roland Coeurjoly
2020-05-23 15:21                           ` Bastien
2020-05-25  7:19                           ` Nicolas Goaziou
2020-05-04 15:29   ` Nicolas Goaziou
2020-05-04 15:42     ` Roland Coeurjoly
2020-05-04 21:52       ` Nicolas Goaziou
2020-05-05 21:23         ` Roland Coeurjoly
2020-09-30  3:35 ` bug#40852: " Lars Ingebrigtsen
2020-10-26 20:18   ` Roland Coeurjoly
2020-10-26 20:26     ` bug#40852: (no subject) Lars Ingebrigtsen

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.