From mboxrd@z Thu Jan 1 00:00:00 1970 From: Eric Schulte Subject: Re: input data for babel blocks Date: Mon, 30 Sep 2013 18:04:17 -0600 Message-ID: <87siwlrgun.fsf@gmail.com> References: Mime-Version: 1.0 Content-Type: text/plain Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:35047) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VQnY0-0005CN-W5 for emacs-orgmode@gnu.org; Mon, 30 Sep 2013 20:10:45 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VQnXw-0006Lz-72 for emacs-orgmode@gnu.org; Mon, 30 Sep 2013 20:10:40 -0400 Received: from mail-pd0-x233.google.com ([2607:f8b0:400e:c02::233]:40563) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VQnXv-0006K6-R9 for emacs-orgmode@gnu.org; Mon, 30 Sep 2013 20:10:36 -0400 Received: by mail-pd0-f179.google.com with SMTP id v10so6351738pde.24 for ; Mon, 30 Sep 2013 17:10:34 -0700 (PDT) List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: Alan Schmitt Cc: emacs-org list Alan Schmitt writes: > Hello, > > In my quest for analyzing my data in org mode tables, I'm trying to see > if I can use my favorite language (i.e., ocaml). I'm thus looking at how > to input such tables in a caml program. I found that the following works > well: > > --8<---------------cut here---------------start------------->8--- > #+name: data > | 12 | > | 42 | > > #+BEGIN_SRC ocaml :var x=data > x > #+END_SRC > > #+RESULTS: > : - : int array array = [|[|12|]; [|42|]|] > --8<---------------cut here---------------end--------------->8--- > > (The only "bad" thing is that a single value is an array, but this is > probably related to the following thing.) Unfortunately (for interaction > with org mode), caml is strongly typed, and arrays must be > homogeneous. Thus the following fails: > > --8<---------------cut here---------------start------------->8--- > #+name: myinput > | x | 12 | > | y | 24 | > > #+BEGIN_SRC ocaml :var x=myinput > x > #+END_SRC > --8<---------------cut here---------------end--------------->8--- > > with the error in the toplevel: > > --8<---------------cut here---------------start------------->8--- > # let x = [|[|"x"; 12|]; [|"y"; 24|]|];; > x;; > "org-babel-ocaml-eoe";; > Characters 17-19: > let x = [|[|"x"; 12|]; [|"y"; 24|]|];; > ^^ > Error: This expression has type int but an expression was expected of type > string > --8<---------------cut here---------------end--------------->8--- > > I would like to change the parsing of table in ob-ocaml so that it > generates an array of _tuples_, for instance for the previous example: > > #+BEGIN_SRC ocaml > let x = [| ("x", 12); ("y", 24) |];; > #+END_SRC > > I looked at the code, and this seems to be the relevant part: > > #+BEGIN_SRC emacs-lisp > (defun org-babel-variable-assignments:ocaml (params) > "Return list of ocaml statements assigning the block's variables." > (mapcar > (lambda (pair) (format "let %s = %s;;" (car pair) > (org-babel-ocaml-elisp-to-ocaml (cdr pair)))) > (mapcar #'cdr (org-babel-get-header params :var)))) > > (defun org-babel-ocaml-elisp-to-ocaml (val) > "Return a string of ocaml code which evaluates to VAL." > (if (listp val) > (concat "[|" (mapconcat #'org-babel-ocaml-elisp-to-ocaml val "; ") "|]") > (format "%S" val))) > #+END_SRC > > I tried tweaking this to the following: > > #+BEGIN_SRC emacs-lisp > (defun org-babel-variable-assignments:ocaml (params) > "Return list of ocaml statements assigning the block's variables." > (mapcar > (lambda (pair) (format "let %s = %s;;" (car pair) > (org-babel-ocaml-elisp-to-ocaml (cdr pair)))) > (mapcar #'cdr (org-babel-get-header params :var)))) > > (defun org-babel-ocaml-elisp-to-ocaml (val) > "Return a string of ocaml code which evaluates to VAL." > (if (listp val) > (concat "[|" (mapconcat #'org-babel-ocaml-elisp-to-ocaml-tuple val "; ") "|]") > (format "%S" val))) > > (defun org-babel-ocaml-elisp-to-ocaml-tuple (val) > "Return a string of ocaml code which evaluates to VAL, as a tuple." > (if (listp val) > (concat "(" (mapconcat #'org-babel-ocaml-elisp-to-ocaml-tuple val ", ") ")") > (format "%S" val))) > #+END_SRC > > Now the example seems to work: > > --8<---------------cut here---------------start------------->8--- > #+name: myinput > | x | 12 | > | y | 24 | > > #+BEGIN_SRC ocaml :var x=myinput > x > #+END_SRC > > #+RESULTS: > : - : (string * int) array = [|("x", 12); ("y", 24)|] > --8<---------------cut here---------------end--------------->8--- > > (and the previous example is even nicer: > --8<---------------cut here---------------start------------->8--- > #+name: data > | 12 | > | 42 | > > #+BEGIN_SRC ocaml :var x=data > x > #+END_SRC > > #+RESULTS: > : - : int array = [|12; 42|] > --8<---------------cut here---------------end--------------->8--- > ) > > I have the following questions for the list: > - can I always assume that tables are passed as lists of lists? Currently this is the default behavior in (I believe) every language. > > - would the patch above be a useful way to deal with this? My problem with the patch above is that it makes OCaml different from every other language (especially ob-haskell which has similar type restraints), and that it doesn't work for tables with different alignment, e.g., | x | y | | 0 | 1 | I guess one possible "correct" solution would be to use a variant type with something like the following. type orgCell = | Int of int | Float of float | String of string > - is there a way to specify the :var parsing in a code block or in the > table? > Currently there is not. Perhaps there is an elegant solution using a new header argument to control how values are represented in literal source code. This is an interesting question. I'm not sure what is best here, but ideally any solution will generalize to other strongly typed languages, will support all possible tables, and will work simply for simple tables allowing users to use tables without having to jump through typed hoops. Cheers, > > Thanks, > > Alan > -- Eric Schulte https://cs.unm.edu/~eschulte PGP: 0x614CA05D