ljos / ob-prolog Goto Github PK
View Code? Open in Web Editor NEWOrg babel functions for prolog evaluation.
Org babel functions for prolog evaluation.
The function definition of org-babel-load-session:prolog
has unnecessary BODY and PARAMS parameters.
body
is never used in the function's body. The only thing params
is used for in the function is a let*
binding of params
; that new, lexically-scoped params
is never used.
This may sound vague, but I'm on Emacs 26.3 and have latest org-mode, latest stable SWI-Prolog, but nothing I do seems to make Prolog in org-mode work. Can you perhaps give us a more detailed setup and execution tutorial?
When running the external command the variables don't show up in the result. Is this a good thing or a bad thing? Should something be done about this?
Right now ob-prolog only outputs strings in the normal prolog format. We should be able to covert the prolog output into the different data formats that org-babel supports.
Should use the :prolog
and :epilog
keys to expand the source body.
if we have a :goal append(a, b, C)
and a
is (setq a '(x))
and b
is (setq b '(y)
the goal should return a list [a,b]
.
There might have to be a way to differentiate between a prolog symbol and elisp symbol. There is a possibility we can use the same syntax as the var expansion, or that we use the vars themselves as expansions for the goal.
The alternatives are:
:goal append(a, b, C)
:goal append(A=a, B=b, C)
:goal append(=a, =b, C)
:var A=a :var B=b :goal append(A, B, C)
I might be partial to 3; 2 is also OK. I just don't completely understand what the assignment is supposed to mean in this instance as the variable name won't be used later.
Update:
I think I am going to go with 3, but with a small change, the =
is only used when evaluating elisp inside the goal. If there is no =
then it is just a prolog constant or variable (depending on capitalization).
org-babel-prolog--parse-goal
uses preceding-sexp
. Commit e6cfa098ae23e34c5415642e2f848a92982924ef in Emacs defines elisp--preceding-sexp
and adds the expression (define-obsolete-function-alias 'preceding-sexp 'elisp--preceding-sexp "25.1")
to elisp-mode.el.
It's not yet clear to me what is gained by obsoleting preceding-sexp
. I will ask in the mailing list and post any findings here.
Hi! Thanks for this package, it will really help if I can get it to work.
When I add a block like
#+begin_src prolog goal: a(X).
a(pizza).
#+end_src
and execute it I just get Code block produced no output
. Am I maybe missing something obvious, or how can I debug this? It is not entirely clear to me how to specify the goal.
For some reason I cannot get a simple Prolog snippet to work in ob-prolog:
#+BEGIN_SRC prolog :goal a(X)
a(a).
#+END_SRC
C-c C-c
gives the following error:
ERROR: Prolog initialisation failed:
ERROR: create_prolog_flag/3: Arguments are not sufficiently instantiated
I'm a bit surprised to see this error, ob-prolog does not seem to set a SWI-Prolog flag from its code?
My Prolog-related configuration is as follows:
(org-babel-do-load-languages
'org-babel-load-languages
'(...
(prolog . t)))
(autoload 'run-prolog "prolog" "Start a Prolog sub-process." t)
(autoload 'prolog-mode "prolog" "Major mode for editing Prolog programs." t)
(setq prolog-system 'swi)
(add-to-list 'auto-mode-alist '("\\.pl$" . prolog-mode))
(setq prolog-indent-width 2)
When calling a goal with a predicate that doesn't exist yet in the session, but is close enough in spelling to a other predicate in the session will freeze the session at Correct to "predicate"?
, and need a yes/no answer.
To fix, look into:
comint-output-filter-functions
-- It should be possible to create a function that takes the last output and check if we are in the Correct to
-state.In executing prolog as an external command, characters used by the shell crashes makes the goal fail.
Here's my test code using latest ob-prolog (following : (http://www.cpp.edu/~jrfisher/www/prolog_tutorial/contents.html) )
#+BEGIN_SRC prolog
['2_2.pl']
#+END_SRC
where 2_2.pl is
factorial(0,1).
factorial(N,F) :-
N>0,
N1 is N-1,
factorial(N1,F1),
F is N * F1.
/------------------------------/
factorial(0,F,F).
factorial(N,A,F) :-
N > 0,
A1 is N*A,
N1 is N -1,
factorial(N1,A1,F).
Doing C-c C-c gives no output, saying (from *Messages*)
executing Prolog code block...
executing Prolog source code block
Wrote /tmp/babel-96244bw/ob-input-96248t0
Error reading results: (beginning-of-buffer)
Code block produced no output.
Then I try this:
#+BEGIN_SRC prolog
factorial(10,What).
#+END_SRC
. . . and get the same response. I try the raw code in 2_2.pl in a code block, again, same response. Please advise. (BTW, thank you very much for your prompt answer to my last question.)
...is the error I get when trying to work through test-ob-prolog.org on this code block (after C-c C-c)
* Simple running session
#+NAME: session-test
#+HEADER: :session *prolog-1*
#+HEADER: :goal fourtyone(A)
#+BEGIN_SRC prolog
fourtyone(A) :- A is 41.
#+END_SRC
I'm using prolog-mode from https://bruda.ca/emacs/prolog_mode_for_emacs
So if I don't use this bruda.ca prolog-mode, rather, just the default batteries-included Emacs version, your ob-prolog does work. Still. . . .
I am not complete sure how this is going to be done yet as prolog doesn't have variables in the traditional sense. What I imagine, though, is that we use built in database in prolog and store the data there.
Something like recorda('aVariableThatIsOne', 1)
. This way we can collect it later. It will be a bit more cumbersome to use, but right now it is the best way I can think of.
Could you enlighten us on exactly how the goal querying works? For example, I've got a query that has multiple answers, but I seem to only get the first one.
`child(john,sue).
child(john,sam).
child(jane,sue).
child(jane,sam).
child(sue,george).
child(sue,gina).
male(john).
male(sam).
male(george).
female(june).
female(sue).
female(jane).
parent(Y,X) :- child(X,Y).
father(Y,X) :- child(X,Y), male(Y).
opp_sex(X,Y) :- male(X), female(Y).
opp_sex(Y,X) :- male(X), female(Y).
grand_father(X,Z) :- father(X,Y), parent(Y,Z).`
`#+NAME: grandfather1
#+HEADER: :session prolog-sess
#+HEADER: :goal gf(X,Z)
#+BEGIN_SRC prolog
gf(X,Z) :- grand_father(X,Z).
#+END_SRC
#+RESULTS: grandfather1
| A | = | george, |
| B | = | john. |`
There should be a george, jane match too. So how do you get the SWI ";" behavior?
See here for header arguments:
http://orgmode.org/manual/Specific-header-arguments.html
I'm having the same issues as described in #21 and in #18
I'm trying it with swi and gnu-prolog, when (setq org-babel-prolog-command "swipl")
the following block:
#+begin_src prolog
write('hello').
#+end_src
produces no output. There's a message "Code block produced no output.", and indeed, there's none whatsoever;
When (setq org-babel-prolog-command "gprolog")
, the same block produces:
| GNU | Prolog | 1.4.5 | (64 | bits) | | |
| Compiled | Aug | 20 | 2018, | 15:27:00 | with | clang |
| By | Daniel | Diaz | | | | |
| Copyright | (C) | 1999-2018 | Daniel | Diaz | | |
| | | ?- | | | | |
And nothing else. What am I missing?
On my original system, as well on a built-from-scratch system, babel prolog won't work for me. I've got latest swi-prolog 7.4.0 installed, along with Emacs 25.1.1, and org 9.0.4, along with ob-prolog 20170102.953, and prolog (major mode) 1.22. Anything I put in a code block, e.g.,
#+BEGIN_SRC prolog
[likes.pl].
#+END_SRC
results in (from *Messages*)
executing Prolog code block...
executing Prolog source code block
org-babel-variable-assignments:prolog: Wrong number of arguments: (1 . 1), 2
Doing C-h f brings up
org-babel-variable-assignments:prolog is a compiled Lisp function in
‘ob-prolog.el’.
(org-babel-variable-assignments:prolog PARAMS)
Not documented.
. . . and I found it here in ob-prolog.el, but can't fathom what is going wrong.
Regular, non-org prolog major mode interactions work fine.
Debugger entered--Lisp error: (error "Lisp nesting exceeds `max-lisp-eval-depth'")
(mapconcat (function identity) (org-babel-prolog--elisp-to-pl value) ", ")
(concat "[" (mapconcat (function identity) (org-babel-prolog--elisp-to-pl value) ", ") "]")
(cond ((stringp value) (format "'%s'" (replace-regexp-in-string "'" "'" value))) ((listp value) (concat "[" (mapconcat (function identity) (org-babel-prolog--elisp-to-pl value) ", ") "]")) (t value))
org-babel-prolog--elisp-to-pl((1 2 3))
(mapconcat (function identity) (org-babel-prolog--elisp-to-pl value) ", ")
(concat "[" (mapconcat (function identity) (org-babel-prolog--elisp-to-pl value) ", ") "]")
(cond ((stringp value) (format "'%s'" (replace-regexp-in-string "'" "'" value))) ((listp value) (concat "[" (mapconcat (function identity) (org-babel-prolog--elisp-to-pl value) ", ") "]")) (t value))
org-babel-prolog--elisp-to-pl((1 2 3))
(repeated many times)
Also, but not sure if related:
#+HEADER: :system swipl
#+HEADER: :goal question_1(X)
#+BEGIN_SRC prolog
q1_range(Low, High, [Low | Acc]) :-
Low < High,
Low1 is Low + 1,
q1_range(Low1, High, Acc).
q1_range(Low, High, [High]) :- Low = High, !.
question_1(1, X) :- q1_range(4, 6, X).
question_1(N, [X | Rest]) :-
Low is 4,
High is N * 2 + 2,
q1_range(Low, High, X),
N0 is N - 1,
question_1(N0, Rest).
question_1(Acc) :-
question_1(4, Raw),
flatten(Raw, Flat),
list_to_set(Flat, Acc).
#+END_SRC
#+RESULTS:
: /bin/bash: -c: line 0: syntax error near unexpected token `('
: /bin/bash: -c: line 0: `swipl -q -l /tmp/babel-3811bZc/prolog-38118FG -t question_1(X)'
There's a problem with escaping the command for the shell.
If I modify ob-prolog.el
to be:
(defun org-babel-prolog-evaluate-external-process (system goal body)
(let* ((tmp-file (org-babel-temp-file "prolog-"))
(command (concat (format "%s -q -l %s" system tmp-file)
(when goal (concat " -t "
(shell-quote-argument goal))))))
(write-region (org-babel-chomp body) nil tmp-file nil 'no-message)
(with-temp-buffer
(call-process-shell-command command nil t)
(buffer-string))))
(note shell-quote-argument goal
), then it works for me (but now I wonder whether it would have worked for you, or maybe it's a different version of call-process-shell-command
, which escaped the arguments all by itself? I'm using Emacs 25.0.50.1.
Dear all,
Sorry for this noob question, but when adding a header to a
prolog block like this one:
#+HEADER: :session prolog-1
Emacs hangs and I have to stop the execution. Otherwise, it works. I am running Debian Buster.
I have an Org-mode file with the block
#+BEGIN_SRC prolog :goal main
main :-
write('Hello, world!').
#+END_SRC
Running org-ctrl-c-ctrl-c
on that block logs the message "org-babel-variable-assignments:prolog: Symbol’s function definition is void: org-babel--get-vars".
The result of evaluating (version)
is "GNU Emacs 25.3.1 (x86_64-unknown-linux-gnu, GTK+ Version 3.22.28) of 2018-03-07".
The result of evaluating (org-version)
is "8.2.10".
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.