147*_*7pm 4 emacs org-mode haskell-stack
在沮丧地徘徊之后,我决定再次在 Emacs 组织模式下尝试 Haskell。我正在使用 Haskell stack-ghci(8.6.3)、Emacs 26.2、org-mode 9.2.3 和intero. 这个代码块
#+begin_src haskell :results raw :session *haskell*
pyth2 :: Int -> [(Int, Int, Int)]
pyth2 n =
[ (x, y, z)
| x <- [1 .. n]
, y <- [x .. n]
, z <- [y .. n]
, x ^ 2 + y ^ 2 == z ^ 2
]
#+end_src
Run Code Online (Sandbox Code Playgroud)
产生这个结果:
*Main| *Main| *Main| *Main| *Main|
<interactive>:59:16: error: Variable not in scope: n
<interactive>:60:16: error: Variable not in scope: n
<interactive>:61:16: error: Variable not in scope: n
Run Code Online (Sandbox Code Playgroud)
然而,这
#+begin_src haskell :results raw
tripleMe x = x + x + x
#+end_src
Run Code Online (Sandbox Code Playgroud)
工作正常。我已经将和 单独的代码块都添加了:set +m,但ghci.conf没有效果。此代码hs在单独的 REPL 中运行的单独文件中运行良好。pyth2单独文件中的代码也可以从 org-mode 启动的 REPL 中调用并运行得很好。不知道如何继续。如有必要,可以包含 Emacs 初始化信息。
在组织模式邮件列表上,我得到的答案基本上与您的说法相同,D. Gillis。他有一个类似的解决方法,实际上更以组织模式为中心。在您的代码块将放置在这个“抽屉”的标题下
:PROPERTIES:
:header-args:haskell: :prologue ":{\n" :epilogue ":}\n"
:END:
Run Code Online (Sandbox Code Playgroud)
然后(可能在局部变量中)运行
#+begin_src haskell :results output
:set prompt-cont ""
#+end_src
Run Code Online (Sandbox Code Playgroud)
由于未知的原因,我不得不包括在内:results output,否则会发生“期望字符串”的神秘错误。
在其他一些注意事项上,haskell babel 不响应/关心该:session选项,即,当您运行代码块时,REPL*haskell*启动,这将是唯一的 REPL。另外,haskell-mode启动的 REPL 不能与现有的组织模式启动的 REPL 配合良好,即,如果您从 启动 REPL haskell-mode,它会杀死原始的组织模式*haskkell*REPL,并且任何运行组织模式代码块的新尝试都可以'没有看到这个新的、非*haskell*REPL 的。然后如果你终止haskell-modeREPL 并尝试运行 org-mode 块,你会得到
executing Haskell code block...
inferior-haskell-start-process: List contains a loop: ("--no-build" "--no-load" "--ghci-options=-ferror-spans" "--no-build" "--no-load" . #2)
Run Code Online (Sandbox Code Playgroud)
...你被浇灭了——似乎没有什么可以动摇它,没有任何重新启动/刷新,也没有杀死,重新加载文件,即,需要完全重新启动 Emacs。谁知道更好的解决方案,请告诉我们。
这是一个 GHCi 问题。
当你的代码直接复制到 GHCi 中时也会出现同样的错误,当它遇到等号后的新行时也会出现解析错误。第一个错误没有出现在这里,因为 org-babel 只显示最后一个表达式的值(在这种情况下,由列表理解引起的错误)。
我并不完全熟悉 Haskell 模式如何将代码发送到 GHCi,但看起来它涉及将缓冲区作为文件加载到 GHCi 中,这可能就是为什么您没有从hs文件中工作的原因。
有几个选项可以解决这个问题,但没有一个是完全理想的:
pyth2 n = [)。:{和包裹整个函数定义:}。前两个选项要求您将代码格式化为 GHCi 可接受的格式。在您的示例中,第一个选项可能不会太糟糕,但对于所有多行声明(例如模式匹配函数声明)来说,这并不总是那么简单。第二个选项的缺点是它需要在代码中添加括号,而这些括号在实际源代码中不应该存在。
为了解决添加无关括号的问题,我编写了一个 Elisp 命令 ( my-org-babel-execute-haskell-blocks),将这些括号放在它找到的代码块周围,评估区域,然后删除括号。请注意,此功能要求块与所有其他代码至少有一个空行分开。
调用my-org-babel-execute-haskell-blocks您的示例声明函数没有任何错误。
编辑:我提供的前一个函数无法处理模式匹配声明。我已经重写了这个函数来解决这个问题,并注意评论。这个新功能应该更有用。但是,值得注意的是,我没有以复杂的方式处理多行注释,因此带有多行注释的代码块可能无法正确包装。
(defun my-org-babel-execute-haskell-blocks ()
"Wraps :{ and :} around all multi-line blocks and then evaluates the source block.
Multi-line blocks are those where all non-indented, non-comment lines are declarations using the same token."
(interactive)
(save-excursion
;; jump to top of source block
(my-org-jump-to-top-of-block)
(forward-line)
;; get valid blocks
(let ((valid-block-start-ends (seq-filter #'my-haskell-block-valid-p (my-get-babel-blocks))))
(mapcar #'my-insert-haskell-braces valid-block-start-ends)
(org-babel-execute-src-block)
(mapcar #'my-delete-inserted-haskell-braces (reverse valid-block-start-ends)))))
(defun my-get-blocks-until (until-string)
(let ((block-start nil)
(block-list nil))
(while (not (looking-at until-string))
(if (looking-at "[[:space:]]*\n")
(when (not (null block-start))
(setq block-list (cons (cons block-start (- (point) 1))
block-list)
block-start nil))
(when (null block-start)
(setq block-start (point))))
(forward-line))
(when (not (null block-start))
(setq block-list (cons (cons block-start (- (point) 1))
block-list)))))
(defun my-get-babel-blocks ()
(my-get-blocks-until "#\\+end_src"))
(defun my-org-jump-to-top-of-block ()
(forward-line)
(org-previous-block 1))
(defun my-empty-line-p ()
(beginning-of-line)
(= (char-after) 10))
(defun my-haskell-type-declaration-line-p ()
(beginning-of-line)
(and (not (looking-at "--"))
(looking-at "^.*::.*$")))
(defun my-insert-haskell-braces (block-start-end)
(let ((block-start (car block-start-end))
(block-end (cdr block-start-end)))
(goto-char block-end)
(insert "\n:}")
(goto-char block-start)
(insert ":{\n")))
(defun my-delete-inserted-haskell-braces (block-start-end)
(let ((block-start (car block-start-end))
(block-end (cdr block-start-end)))
(goto-char block-start)
(delete-char 3)
(goto-char block-end)
(delete-char 3)))
(defun my-get-first-haskell-token ()
"Gets all consecutive non-whitespace text until first whitespace"
(save-excursion
(beginning-of-line)
(let ((starting-point (point)))
(re-search-forward ".*?[[:blank:]\n]")
(goto-char (- (point) 1))
(buffer-substring-no-properties starting-point (point)))))
(defun my-haskell-declaration-line-p ()
(beginning-of-line)
(or (looking-at "^.*=.*$") ;; has equals sign
(looking-at "^.*\n[[:blank:]]*|")
(looking-at "^.*where[[:blank:]]*$")))
(defun my-haskell-block-valid-p (block-start-end)
(let ((block-start (car block-start-end))
(block-end (cdr block-start-end))
(line-count 0))
(save-excursion
(goto-char block-start)
(let ((token 'nil)
(is-valid t))
;; eat top comments
(while (or (looking-at "--")
(looking-at "{-"))
(forward-line))
(when (my-haskell-type-declaration-line-p)
(progn
(setq token (my-get-first-haskell-token)
line-count 1)
(forward-line)))
(while (<= (point) block-end)
(let ((current-token (my-get-first-haskell-token)))
(cond ((string= current-token "") ; line with indentation
(when (null token) (setq is-valid nil))
(setq line-count (+ 1 line-count)))
((or (string= (substring current-token 0 2) "--") ;; skip comments
(string= (substring current-token 0 2) "{-"))
'())
((and (my-haskell-declaration-line-p)
(or (null token) (string= token current-token)))
(setq token current-token
line-count (+ 1 line-count)))
(t (setq is-valid nil)
(goto-char (+ 1 block-end))))
(forward-line)))
(and is-valid (> line-count 1))))))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
536 次 |
| 最近记录: |