emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* org-babel and OCaml - help?
@ 2010-07-16 15:12 Erik L. Arneson
  2010-07-16 19:59 ` [PATCH] Fix list assignments in ob-ocaml.el Erik L. Arneson
  2010-07-16 22:30 ` org-babel and OCaml - help? Eric Schulte
  0 siblings, 2 replies; 10+ messages in thread
From: Erik L. Arneson @ 2010-07-16 15:12 UTC (permalink / raw)
  To: emacs-orgmode-mXXj517/zsQ

Hi everybody,

I have just started playing around with org-babel, and it's really
awesome!  I've had great luck with emacs-lisp source blocks, and when I
saw that OCaml source blocks were also supported, I started testing
those out.  Right away I ran into trouble, though.  Observe the example
below:

--8<---------------cut here---------------start------------->8---
#+tblname: example-table
| 1 | 2 |
| 2 | 3 |
| 3 | 5 |
| 4 | 6 |

#+source: ocaml-length
#+begin_src ocaml :var table=example-table
  List.length table
#+end_src

#+results: ocaml-length
: Characters 14-15:
:   let table = ((1 2) (2 3) (3 5) (4 6));
:                 ^
: Error: This expression is not a function; it cannot be applied

#+results: ocaml-length
--8<---------------cut here---------------end--------------->8---

It looks as though the 'table' variable is being passed as Lisp code
instead of OCaml.  Is there something that my setup is missing, or does
the OCaml code perhaps need more help?  Maybe I did something wrong?

(I'd love to work on fixing up the OCaml interface, if that's needed.)

-- 
Erik Arneson <dybbuk-qPb3x58jdZkAvxtiuMwx3w@public.gmane.org>
  GPG Key ID: 1024D/62DA1D25
  Office: +1.541.291.9776
  Skype: callto://pymander


_______________________________________________
Emacs-orgmode mailing list
Please use `Reply All' to send replies to the list.
Emacs-orgmode-mXXj517/zsQ@public.gmane.org
http://lists.gnu.org/mailman/listinfo/emacs-orgmode

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

* [PATCH] Fix list assignments in ob-ocaml.el
  2010-07-16 15:12 org-babel and OCaml - help? Erik L. Arneson
@ 2010-07-16 19:59 ` Erik L. Arneson
  2010-07-16 20:59   ` Erik L. Arneson
  2010-07-17  0:41   ` Bernt Hansen
  2010-07-16 22:30 ` org-babel and OCaml - help? Eric Schulte
  1 sibling, 2 replies; 10+ messages in thread
From: Erik L. Arneson @ 2010-07-16 19:59 UTC (permalink / raw)
  To: emacs-orgmode-mXXj517/zsQ

[-- Attachment #1: Type: text/plain, Size: 222 bytes --]

I'm sorry if this is not the correct format to send patches in, but it's
my first patch.  Hope it looks good.

This patch fixes the OCaml list assignment when using a table as a var
to an OCaml source block in org-babel.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: Fix OCaml list assignment --]
[-- Type: text/x-patch, Size: 1713 bytes --]

diff --git a/lisp/ob-ocaml.el b/lisp/ob-ocaml.el
index 8cdffc9..43be08c 100644
--- a/lisp/ob-ocaml.el
+++ b/lisp/ob-ocaml.el
@@ -50,12 +50,44 @@
 (defvar org-babel-ocaml-eoe-indicator "\"org-babel-ocaml-eoe\";;")
 (defvar org-babel-ocaml-eoe-output "org-babel-ocaml-eoe")
 
+(defun org-babel-ocaml-quote-item (itm)
+  (cond ((stringp itm)
+	 (format "\"%s\"" itm))
+	(t
+	 (format "%s" itm))))
+
+(defun org-babel-ocaml-list-from-elisp (lst)
+  "Construct OCaml list from elisp list LST.
+
+As LST is expected to be an org-mode table, the result will actually
+be a list of n-tuples, such as [(1, 2, 3); (2, 3, 4)]."
+  (concat "["
+	  (mapconcat 
+	   #'(lambda (itm)
+	       (cond ((listp itm)
+		      (concat "("
+			      (mapconcat #'org-babel-ocaml-quote-item itm ", ")
+			      ")"))
+		     ((stringp itm)
+		      (format "\"%s\"" itm))
+		     (t
+		      (format "%s" itm))))
+	   lst "; ")
+	  "]"))
+
+(defun org-babel-ocaml-assign-elisp (name value colnames-p rownames-p)
+  "Construct OCaml code assigning the elisp VALUE to a variable named NAME."
+  (if (listp value)
+      (format "let %s = %s in\n"
+	      name (org-babel-ocaml-list-from-elisp value))
+    (format "let %s = %s in\n" name value)))
+
 (defun org-babel-expand-body:ocaml (body params &optional processed-params)
   "Expand BODY according to PARAMS, return the expanded body."
   (let ((vars (nth 1 (or processed-params (org-babel-process-params params)))))
     (concat
      (mapconcat
-      (lambda (pair) (format "let %s = %s;" (car pair) (cdr pair)))
+      (lambda (pair) (org-babel-ocaml-assign-elisp (car pair) (cdr pair) nil nil))
       vars "\n") "\n" body "\n")))
 
 (defun org-babel-execute:ocaml (body params)

[-- Attachment #3: Type: text/plain, Size: 149 bytes --]


-- 
Erik Arneson <dybbuk-qPb3x58jdZkAvxtiuMwx3w@public.gmane.org>
  GPG Key ID: 1024D/62DA1D25
  Office: +1.541.291.9776
  Skype: callto://pymander

[-- Attachment #4: Type: text/plain, Size: 222 bytes --]

_______________________________________________
Emacs-orgmode mailing list
Please use `Reply All' to send replies to the list.
Emacs-orgmode-mXXj517/zsQ@public.gmane.org
http://lists.gnu.org/mailman/listinfo/emacs-orgmode

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

* Re: [PATCH] Fix list assignments in ob-ocaml.el
  2010-07-16 19:59 ` [PATCH] Fix list assignments in ob-ocaml.el Erik L. Arneson
@ 2010-07-16 20:59   ` Erik L. Arneson
  2010-07-17 18:08     ` Eric Schulte
  2010-07-17  0:41   ` Bernt Hansen
  1 sibling, 1 reply; 10+ messages in thread
From: Erik L. Arneson @ 2010-07-16 20:59 UTC (permalink / raw)
  To: emacs-orgmode-mXXj517/zsQ

dybbuk-qPb3x58jdZkAvxtiuMwx3w@public.gmane.org (Erik L. Arneson) writes:
> This patch fixes the OCaml list assignment when using a table as a var
> to an OCaml source block in org-babel.

Please ignore this patch.  It still needs some work.  Values being
returned from OCaml don't seem to be read correctly now.  Oops!

(I'm still really new to org-babel, so this is kind of a hairy learning
process.)

-- 
Erik Arneson <dybbuk-qPb3x58jdZkAvxtiuMwx3w@public.gmane.org>
  GPG Key ID: 1024D/62DA1D25
  Office: +1.541.291.9776
  Skype: callto://pymander


_______________________________________________
Emacs-orgmode mailing list
Please use `Reply All' to send replies to the list.
Emacs-orgmode-mXXj517/zsQ@public.gmane.org
http://lists.gnu.org/mailman/listinfo/emacs-orgmode

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

* Re: org-babel and OCaml - help?
  2010-07-16 15:12 org-babel and OCaml - help? Erik L. Arneson
  2010-07-16 19:59 ` [PATCH] Fix list assignments in ob-ocaml.el Erik L. Arneson
@ 2010-07-16 22:30 ` Eric Schulte
  2010-07-18 14:30   ` Erik L. Arneson
  1 sibling, 1 reply; 10+ messages in thread
From: Eric Schulte @ 2010-07-16 22:30 UTC (permalink / raw)
  To: Erik L. Arneson; +Cc: emacs-orgmode

Hi Erik,

dybbuk@LNouv.com (Erik L. Arneson) writes:

> Hi everybody,
>
> I have just started playing around with org-babel, and it's really
> awesome!  I've had great luck with emacs-lisp source blocks, and when I
> saw that OCaml source blocks were also supported, I started testing
> those out.  Right away I ran into trouble, though.  Observe the example
> below:
>
> #+tblname: example-table
> | 1 | 2 |
> | 2 | 3 |
> | 3 | 5 |
> | 4 | 6 |
>
> #+source: ocaml-length
> #+begin_src ocaml :var table=example-table
>   List.length table
> #+end_src
>
> #+results: ocaml-length
> : Characters 14-15:
> :   let table = ((1 2) (2 3) (3 5) (4 6));
> :                 ^
> : Error: This expression is not a function; it cannot be applied
>
> #+results: ocaml-length
>
> It looks as though the 'table' variable is being passed as Lisp code
> instead of OCaml.  Is there something that my setup is missing, or does
> the OCaml code perhaps need more help?  Maybe I did something wrong?
>

Nope you diagnosed the problem exactly, this isn't an issue with your
setup, but rather with the current babel<->ocaml integration.  The
language specific interaction functionality tends to evolve by need, and
I don't think many people have been banging on ocaml through Babel up to
this point.

I've just pushed up a commit with teaches Babel how to feed tables to
ocaml, so your example above re-written as below should now work.

--8<---------------cut here---------------start------------->8---
#+tblname: example-table
| 1 |
| 2 |
| 3 |
| 4 |

#+source: ocaml-length
#+begin_src ocaml :var table=example-table
  Array.length table;;
#+end_src

#+results: ocaml-length
: 4
--8<---------------cut here---------------end--------------->8---

>
> (I'd love to work on fixing up the OCaml interface, if that's needed.)

I'd love to have your help!  The relevant code is located in
org/lisp/ob-ocaml.el, you can see in the most recent commit the changes
that I've just made for integrating table handling into this file.

You'll notice by comparing ob-ocaml to some of the more mature code
files like ob-R, ob-python, ob-ruby that there are many areas in which
ob-ocaml could grow in functionality.

Cheers -- Eric

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

* Re: [PATCH] Fix list assignments in ob-ocaml.el
  2010-07-16 19:59 ` [PATCH] Fix list assignments in ob-ocaml.el Erik L. Arneson
  2010-07-16 20:59   ` Erik L. Arneson
@ 2010-07-17  0:41   ` Bernt Hansen
  1 sibling, 0 replies; 10+ messages in thread
From: Bernt Hansen @ 2010-07-17  0:41 UTC (permalink / raw)
  To: Erik L. Arneson; +Cc: public-emacs-orgmode-mXXj517/zsQ



dybbuk-qPb3x58jdZkAvxtiuMwx3w@public.gmane.org (Erik L. Arneson) writes:

> I'm sorry if this is not the correct format to send patches in, but it's
> my first patch.  Hope it looks good.

Comments like this should not be part of the commit message.  The
subject line (after [PATCH] is taken as the first line of the commit
message and the body of the email up to the --- separator is the body of
the commit message.

If you want to add extra detail for the mailing list which should not be
included in the commit message (like your text above) it should be put
between the --- and the diffstat.

You can find an example of a patch with extra detail which is not part
of the commit message here:

http://permalink.gmane.org/gmane.emacs.orgmode/26832

Text in the email between the --- separator and the diffstat is ignored
by 'git am' and 'git apply' so whomever is applying your patch won't
have to edit it manually to remove it.

git format-patch will create a patch file in the correct format
(assuming you've made your changes and committed them in your local
repository.  If it is a single patch

   git format-patch -1

will create a single file with the patch contents.  You can also use 
git send-email --annotate -1 
assuming you have set up the mailing list details in git.

I have the following in my .git/config for org-mode
,----[ .git/config ]
| [sendemail]
| 	to = emacs-orgmode@gnu.org
`----

so git send-email -1 --annotate creates a patch of the current HEAD
commit and drops me in an editor where I can insert extra details
between the --- separator and the diffstat.

The -1 (dash one) specifies how many commits to create patches for.
-3 would take the last 3 commits and create numbered files (or emails)
for submission to the mailing list.

HTH,
Bernt

PS. I know you said to ignore this patch but I thought I'd provide this
    detail anyway.  It's possible to apply a patch manually with fixups
    if it is in not in the correct format for git but it takes extra
    effort by the maintainer.  If you already have commits for your
    changes in git then creating patches in the correct format is easy.

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

* Re: Re: [PATCH] Fix list assignments in ob-ocaml.el
  2010-07-16 20:59   ` Erik L. Arneson
@ 2010-07-17 18:08     ` Eric Schulte
  0 siblings, 0 replies; 10+ messages in thread
From: Eric Schulte @ 2010-07-17 18:08 UTC (permalink / raw)
  To: Erik L. Arneson; +Cc: emacs-orgmode

dybbuk@LNouv.com (Erik L. Arneson) writes:

> dybbuk@LNouv.com (Erik L. Arneson) writes:
>> This patch fixes the OCaml list assignment when using a table as a var
>> to an OCaml source block in org-babel.
>
> Please ignore this patch.  It still needs some work.  Values being
> returned from OCaml don't seem to be read correctly now.  Oops!
>
> (I'm still really new to org-babel, so this is kind of a hairy learning
> process.)

Hi Erik,

One good way to gain familiarity with Org-Babel is to look at the other
language specific files, and to run a code block in one of these
languages, stepping through the language specific evaluation functions
using Edebug [1].

If you have specific babel code/implementation questions you can always
post them to this list, preferably with a "[babel]" string in the
subject line.

Also, if you plan on contributing to Babel, you will need to assign
copyright for your contributions to Emacs (see [2]).

Always happy to have more people hacking away at Babel.

Best -- Eric

Footnotes: 
[1]  http://www.gnu.org/software/emacs/emacs-lisp-intro/html_node/edebug.html

[2]  http://orgmode.org/worg/org-contribute.php

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

* Re: org-babel and OCaml - help?
  2010-07-16 22:30 ` org-babel and OCaml - help? Eric Schulte
@ 2010-07-18 14:30   ` Erik L. Arneson
  2010-07-18 17:07     ` Eric Schulte
  0 siblings, 1 reply; 10+ messages in thread
From: Erik L. Arneson @ 2010-07-18 14:30 UTC (permalink / raw)
  To: emacs-orgmode-mXXj517/zsQ

"Eric Schulte" <schulte.eric-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> writes:
> Hi Erik,

Hi, Eric!

> I've just pushed up a commit with teaches Babel how to feed tables to
> ocaml, so your example above re-written as below should now work.

I already had half-a-patch (as you saw elsewhere on the list) that took
a slightly different approach than you did.  Because a row might
contain, say, both text, integers, and floating point numbers, I thought
that lists of tuples might be easier to use.  Arrays of tuples might
work even better, I would guess.  I'll play around with the options and
see what I can come up with.

> I'd love to have your help!  The relevant code is located in
> org/lisp/ob-ocaml.el, you can see in the most recent commit the changes
> that I've just made for integrating table handling into this file.
>
> You'll notice by comparing ob-ocaml to some of the more mature code
> files like ob-R, ob-python, ob-ruby that there are many areas in which
> ob-ocaml could grow in functionality.

Great, I will start hacking away at it.  What's the process I need to go
through to sign FSF papers?

-- 
Erik Arneson <dybbuk-qPb3x58jdZkAvxtiuMwx3w@public.gmane.org>
  GPG Key ID: 1024D/62DA1D25
  Office: +1.541.291.9776
  Skype: callto://pymander


_______________________________________________
Emacs-orgmode mailing list
Please use `Reply All' to send replies to the list.
Emacs-orgmode-mXXj517/zsQ@public.gmane.org
http://lists.gnu.org/mailman/listinfo/emacs-orgmode

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

* Re: Re: org-babel and OCaml - help?
  2010-07-18 14:30   ` Erik L. Arneson
@ 2010-07-18 17:07     ` Eric Schulte
  2010-07-21 16:56       ` [babel] " Erik L. Arneson
  0 siblings, 1 reply; 10+ messages in thread
From: Eric Schulte @ 2010-07-18 17:07 UTC (permalink / raw)
  To: Erik L. Arneson; +Cc: emacs-orgmode

dybbuk@LNouv.com (Erik L. Arneson) writes:

> "Eric Schulte" <schulte.eric@gmail.com> writes:
>> Hi Erik,
>
> Hi, Eric!
>
>> I've just pushed up a commit with teaches Babel how to feed tables to
>> ocaml, so your example above re-written as below should now work.
>
> I already had half-a-patch (as you saw elsewhere on the list) that took
> a slightly different approach than you did.  Because a row might
> contain, say, both text, integers, and floating point numbers, I thought
> that lists of tuples might be easier to use.  Arrays of tuples might
> work even better, I would guess.  I'll play around with the options and
> see what I can come up with.
>
>> I'd love to have your help!  The relevant code is located in
>> org/lisp/ob-ocaml.el, you can see in the most recent commit the changes
>> that I've just made for integrating table handling into this file.
>>
>> You'll notice by comparing ob-ocaml to some of the more mature code
>> files like ob-R, ob-python, ob-ruby that there are many areas in which
>> ob-ocaml could grow in functionality.
>
> Great, I will start hacking away at it.  What's the process I need to go
> through to sign FSF papers?

Hi Erik,

See http://orgmode.org/worg/org-contribute.php#sec-2 for information on
FSF copyright assignment.

Cheers -- Eric

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

* [babel] Re: org-babel and OCaml - help?
  2010-07-18 17:07     ` Eric Schulte
@ 2010-07-21 16:56       ` Erik L. Arneson
  2010-07-21 22:19         ` [babel] Re: org-babel and OCaml Eric Schulte
  0 siblings, 1 reply; 10+ messages in thread
From: Erik L. Arneson @ 2010-07-21 16:56 UTC (permalink / raw)
  To: emacs-orgmode-mXXj517/zsQ

"Eric Schulte" <schulte.eric-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> writes:
> dybbuk-qPb3x58jdZkAvxtiuMwx3w@public.gmane.org (Erik L. Arneson) writes:
>> Great, I will start hacking away at it.  What's the process I need to go
>> through to sign FSF papers?
>
> See http://orgmode.org/worg/org-contribute.php#sec-2 for information on
> FSF copyright assignment.

I've submitted the paperwork, so now I'm just waiting to hear back from
them.  I'm still curious, though, as to how much you'd like me to change the
back-end behavior (i.e. the generated OCaml code and the way return
values are read) before checking with the community for their thoughts
and such.

The first table I tried to test on looked something like this:

--8<---------------cut here---------------start------------->8---
#+tblname: test
| John | 5 | 1.02 |
| Mary | 6 | 9.00 |
| Bob  | 7 | 0.50 |
--8<---------------cut here---------------end--------------->8---

With the patch you recently submitted, this makes an invalid array of
arrays in OCaml, so that's why I was thinking it should generate an
array of tuples instead, so the output might look like this:

--8<---------------cut here---------------start------------->8---
let test = 
  [| ("John", 5, 1.02);
     ("Mary", 6, 9.00);
     ("Bob", 7, 0.50) |]
--8<---------------cut here---------------end--------------->8---

It doesn't seem like many people are using ob-ocaml.el yet, so I can
probably just make that change, document it, and then hope for the best,
right?

-- 
Erik Arneson <dybbuk-qPb3x58jdZkAvxtiuMwx3w@public.gmane.org>
  GPG Key ID: 1024D/62DA1D25
  Office: +1.541.291.9776
  Skype: callto://pymander


_______________________________________________
Emacs-orgmode mailing list
Please use `Reply All' to send replies to the list.
Emacs-orgmode-mXXj517/zsQ@public.gmane.org
http://lists.gnu.org/mailman/listinfo/emacs-orgmode

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

* Re: [babel] Re: org-babel and OCaml
  2010-07-21 16:56       ` [babel] " Erik L. Arneson
@ 2010-07-21 22:19         ` Eric Schulte
  0 siblings, 0 replies; 10+ messages in thread
From: Eric Schulte @ 2010-07-21 22:19 UTC (permalink / raw)
  To: Erik L. Arneson; +Cc: emacs-orgmode

Hi Erik,

dybbuk@LNouv.com (Erik L. Arneson) writes:

> "Eric Schulte" <schulte.eric@gmail.com> writes:
>> dybbuk@LNouv.com (Erik L. Arneson) writes:
>>> Great, I will start hacking away at it.  What's the process I need to go
>>> through to sign FSF papers?
>>
>> See http://orgmode.org/worg/org-contribute.php#sec-2 for information on
>> FSF copyright assignment.
>
> I've submitted the paperwork, so now I'm just waiting to hear back from
> them.  I'm still curious, though, as to how much you'd like me to change the
> back-end behavior (i.e. the generated OCaml code and the way return
> values are read) before checking with the community for their thoughts
> and such.
>

Judging from the traffic on the mailing list I don't think there are
many users of the Babel ocaml implementation.

You're certainly free to experiment with as wide of a range of
implementation options as you like.  Given that any patches will
necessarily move through this list, I think that we're guaranteed that
anyone interested in the ob-ocaml implementation will have a chance to
participate in the development.

>
> The first table I tried to test on looked something like this:
>
> #+tblname: test
> | John | 5 | 1.02 |
> | Mary | 6 | 9.00 |
> | Bob  | 7 | 0.50 |
>
> With the patch you recently submitted, this makes an invalid array of
> arrays in OCaml, so that's why I was thinking it should generate an
> array of tuples instead, so the output might look like this:
>
> let test = 
>   [| ("John", 5, 1.02);
>      ("Mary", 6, 9.00);
>      ("Bob", 7, 0.50) |]
>

The only issue I see with that approach is that different dimensions of
the incoming value are treated differently, i.e. the first dimension is
an array, the second is a tuple, how would the third of fourth
dimensions be represented?

It looks like nested arrays may be possible using Matrix primitives (see
[1]) maybe this would be preferable?

>
> It doesn't seem like many people are using ob-ocaml.el yet, so I can
> probably just make that change, document it, and then hope for the best,
> right?

I'd recommend that you experiment with new changes locally, and then
send patches to the list where they can be reviewed and applied.

Cheers -- Eric

Footnotes: 
[1]  http://caml.inria.fr/resources/doc/faq/core.en.html#data-structures

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

end of thread, other threads:[~2010-07-21 22:19 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-07-16 15:12 org-babel and OCaml - help? Erik L. Arneson
2010-07-16 19:59 ` [PATCH] Fix list assignments in ob-ocaml.el Erik L. Arneson
2010-07-16 20:59   ` Erik L. Arneson
2010-07-17 18:08     ` Eric Schulte
2010-07-17  0:41   ` Bernt Hansen
2010-07-16 22:30 ` org-babel and OCaml - help? Eric Schulte
2010-07-18 14:30   ` Erik L. Arneson
2010-07-18 17:07     ` Eric Schulte
2010-07-21 16:56       ` [babel] " Erik L. Arneson
2010-07-21 22:19         ` [babel] Re: org-babel and OCaml Eric Schulte

Code repositories for project(s) associated with this public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).