caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] lisp to ocaml
@ 2005-09-18  1:08 Jonathan Roewen
  2005-09-18  2:56 ` Jon Harrop
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Jonathan Roewen @ 2005-09-18  1:08 UTC (permalink / raw)
  To: caml-list

Hi,

Does anyone know of a tool that can convert lisp to ocaml (or
something other ML dialect)? I've tried googling and couldn't find
anything. Just all the parentheses gets a bit confusing for a first
look at lisp ;-)

Jonathan


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

* Re: [Caml-list] lisp to ocaml
  2005-09-18  1:08 [Caml-list] lisp to ocaml Jonathan Roewen
@ 2005-09-18  2:56 ` Jon Harrop
  2005-09-18 14:06 ` Thomas Fischbacher
  2005-09-19 13:08 ` Christoph Bauer
  2 siblings, 0 replies; 9+ messages in thread
From: Jon Harrop @ 2005-09-18  2:56 UTC (permalink / raw)
  To: caml-list

On Sunday 18 September 2005 02:08, Jonathan Roewen wrote:
> Does anyone know of a tool that can convert lisp to ocaml (or
> something other ML dialect)? I've tried googling and couldn't find
> anything. Just all the parentheses gets a bit confusing for a first
> look at lisp ;-)

You could do a "straightforward" conversion by starting with a simple Lisp 
interpreter written in OCaml and partially specialising it over the Lisp 
program.

However, if you find Lisp's brackets confusing then you're likely to find the 
enormous amount of boxing, unboxing and dynamic type checking that would 
result even more confusing. You could probably get rid of much of that but it 
would greatly complicate the translator.

Interestingly, I just proposed writing a staged Scheme interpreter to Walid 
Taha, as a possible MetaOCaml example. If one existed then you could just 
look at the OCaml code that it generated.

-- 
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
Objective CAML for Scientists
http://www.ffconsultancy.com/products/ocaml_for_scientists


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

* Re: [Caml-list] lisp to ocaml
  2005-09-18  1:08 [Caml-list] lisp to ocaml Jonathan Roewen
  2005-09-18  2:56 ` Jon Harrop
@ 2005-09-18 14:06 ` Thomas Fischbacher
  2005-09-18 14:17   ` yoann padioleau
  2005-09-18 15:44   ` Jon Harrop
  2005-09-19 13:08 ` Christoph Bauer
  2 siblings, 2 replies; 9+ messages in thread
From: Thomas Fischbacher @ 2005-09-18 14:06 UTC (permalink / raw)
  To: Jonathan Roewen; +Cc: caml-list


On Sun, 18 Sep 2005, Jonathan Roewen wrote:

> Does anyone know of a tool that can convert lisp to ocaml (or
> something other ML dialect)?

For ANSI Common LISP, this could at best work at the level of a LISP 
interpreter (or compiler) written im ML. You cannot directly compile LISP 
code to ML code in the sense that

(defun factorial (n)
  (labels
      ((walk (so-far todo)
	 (if (= todo 0)
	     so-far
	   (walk (* so-far todo) (- todo 1)))))
    (walk 1 n)))

would become

let factorial n =
  let rec walk so_far todo =
      if todo=0
      then so_far
      else walk (so_far*todo) (todo-1)
  in walk 1 n
;;

for a ton of reasons.

> Just all the parentheses gets a bit confusing for a first
> look at lisp ;-)

The trick is:

(1) Do not look at the parentheses. Read and write code by indentation.

(2) Use a text editor that helps you with this.

(3) paren-sensitive syntax highlighting is a great thing.

You will find the parens to be much less confusing once you learned not to 
look at them. :-)


-- 
regards,               tf@cip.physik.uni-muenchen.de              (o_
 Thomas Fischbacher -  http://www.cip.physik.uni-muenchen.de/~tf  //\
(lambda (n) ((lambda (p q r) (p p q r)) (lambda (g x y)           V_/_
(if (= x 0) y (g g (- x 1) (* x y)))) n 1))                  (Debian GNU)


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

* Re: [Caml-list] lisp to ocaml
  2005-09-18 14:06 ` Thomas Fischbacher
@ 2005-09-18 14:17   ` yoann padioleau
  2005-09-18 14:37     ` Thomas Fischbacher
  2005-09-18 15:44   ` Jon Harrop
  1 sibling, 1 reply; 9+ messages in thread
From: yoann padioleau @ 2005-09-18 14:17 UTC (permalink / raw)
  To: Thomas Fischbacher; +Cc: Jonathan Roewen, caml-list

>> Just all the parentheses gets a bit confusing for a first
>> look at lisp ;-)
>>
>
> The trick is:
>
> (1) Do not look at the parentheses. Read and write code by  
> indentation.
>
> (2) Use a text editor that helps you with this.
>
> (3) paren-sensitive syntax highlighting is a great thing.
>

You can also in some scheme (such as plt scheme) use other symbols  
than parenthesis such as '[' ']'.
It reduces the number of () and makes some code looks better.

for instance

(defun factorial (n)
   (cond [(= n 0) 1]
              [(> n 0)  (* n (fact (- n 1)))]
   ))

> You will find the parens to be much less confusing once you learned  
> not to
> look at them. :-)
>
>
> -- 
> regards,               tf@cip.physik.uni-muenchen.de              (o_
>  Thomas Fischbacher -  http://www.cip.physik.uni-muenchen.de/~tf  //\
> (lambda (n) ((lambda (p q r) (p p q r)) (lambda (g x y)           V_/_
> (if (= x 0) y (g g (- x 1) (* x y)))) n 1))                   
> (Debian GNU)
>
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>
>



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

* Re: [Caml-list] lisp to ocaml
  2005-09-18 14:17   ` yoann padioleau
@ 2005-09-18 14:37     ` Thomas Fischbacher
  0 siblings, 0 replies; 9+ messages in thread
From: Thomas Fischbacher @ 2005-09-18 14:37 UTC (permalink / raw)
  To: yoann padioleau; +Cc: Jonathan Roewen, caml-list


On Sun, 18 Sep 2005, yoann padioleau wrote:

> You can also in some scheme (such as plt scheme) use other symbols than
> parenthesis such as '[' ']'.
> It reduces the number of () and makes some code looks better.
> 
> for instance
> 
> (defun factorial (n)
> (cond [(= n 0) 1]
> [(> n 0)  (* n (fact (- n 1)))]
> )) 

This gets somewhat off-topic now, but just for completeness, let me 
mention that one can easily modify the ANSI CL reader in such a way 
that some characters are handled by custom code, and just make [] or even 
{} behave as if they also were (). This works for every ANSI-compliant CL.
(Not that I would recommend this in any way.)

I see the attempt to learn ocaml as a first step to understand Lisp 
with somewhat mixed feelings. (Even though right now, I teach it for more 
or less just that reason...)

Certainly, it is very useful in many situations to know some OCaml, but if
one just wants to learn Lisp, but is too spoilt by some syntax-rich 
languages that the uniformity of Lisp's parens turns out to be a problem,
it might or might not be more appropriate to instead start with a LISP 
with a bit more syntactic sugar on top of it, which can use 
reader-dispatch to parse blocks of semicolon-delimited infix statements...

-- 
regards,               tf@cip.physik.uni-muenchen.de              (o_
 Thomas Fischbacher -  http://www.cip.physik.uni-muenchen.de/~tf  //\
(lambda (n) ((lambda (p q r) (p p q r)) (lambda (g x y)           V_/_
(if (= x 0) y (g g (- x 1) (* x y)))) n 1))                  (Debian GNU)


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

* Re: [Caml-list] lisp to ocaml
  2005-09-18 14:06 ` Thomas Fischbacher
  2005-09-18 14:17   ` yoann padioleau
@ 2005-09-18 15:44   ` Jon Harrop
  2005-09-18 16:08     ` Thomas Fischbacher
  1 sibling, 1 reply; 9+ messages in thread
From: Jon Harrop @ 2005-09-18 15:44 UTC (permalink / raw)
  To: caml-list

On Sunday 18 September 2005 15:06, Thomas Fischbacher wrote:
> You cannot directly compile LISP code to ML code in the sense that
>
> (defun factorial (n)
>   (labels
>       ((walk (so-far todo)
> 	 (if (= todo 0)
> 	     so-far
> 	   (walk (* so-far todo) (- todo 1)))))
>     (walk 1 n)))
>
> would become
>
> let factorial n =
>   let rec walk so_far todo =
>       if todo=0
>       then so_far
>       else walk (so_far*todo) (todo-1)
>   in walk 1 n
> ;;
>
> for a ton of reasons.

In this case, what else is needed beyond replacing operators with those 
suitable for a generalised numeric type and constructing numeric literals?

-- 
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
Objective CAML for Scientists
http://www.ffconsultancy.com/products/ocaml_for_scientists


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

* Re: [Caml-list] lisp to ocaml
  2005-09-18 15:44   ` Jon Harrop
@ 2005-09-18 16:08     ` Thomas Fischbacher
  2005-09-18 17:10       ` brogoff
  0 siblings, 1 reply; 9+ messages in thread
From: Thomas Fischbacher @ 2005-09-18 16:08 UTC (permalink / raw)
  To: Jon Harrop; +Cc: caml-list


On Sun, 18 Sep 2005, Jon Harrop wrote:

> > You cannot directly compile LISP code to ML code in the sense that
> > (...)
> > for a ton of reasons.

> In this case, what else is needed beyond replacing operators with those 
> suitable for a generalised numeric type and constructing numeric literals?

I have to correct my statement:

You cannot compile Common Lisp code to ML code that expresses the same 
idea short of piping it through an intelligent human who is proficient 
with both languages, and can use intelligent judgment to make decisions 
about what "the same idea" is supposed to mean.

The question whether there is an interesting subset of Common LISP which 
can automatically be mapped directly to ML in such a way that there is a 
direct correspondence between the fundamental data types (i.e. lists are 
represented as lists) is a different one.

May I ask you to have a try here:

(defun flatten (tree)
  (cond
   ((consp tree)
    (append (flatten (car tree)) (flatten (cdr tree))))
   ((null tree) nil)
   (t
    (list tree))))

(flatten
 '("foo" (bar (1337 "baz") ("some" "more") ("pieces" "of" ("data")) "here")))

-- 
regards,               tf@cip.physik.uni-muenchen.de              (o_
 Thomas Fischbacher -  http://www.cip.physik.uni-muenchen.de/~tf  //\
(lambda (n) ((lambda (p q r) (p p q r)) (lambda (g x y)           V_/_
(if (= x 0) y (g g (- x 1) (* x y)))) n 1))                  (Debian GNU)


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

* Re: [Caml-list] lisp to ocaml
  2005-09-18 16:08     ` Thomas Fischbacher
@ 2005-09-18 17:10       ` brogoff
  0 siblings, 0 replies; 9+ messages in thread
From: brogoff @ 2005-09-18 17:10 UTC (permalink / raw)
  To: Thomas Fischbacher; +Cc: Jon Harrop, caml-list

On Sun, 18 Sep 2005, Thomas Fischbacher wrote:
> On Sun, 18 Sep 2005, Jon Harrop wrote:
>
> > > You cannot directly compile LISP code to ML code in the sense that
> > > (...)
> > > for a ton of reasons.
>
> > In this case, what else is needed beyond replacing operators with those
> > suitable for a generalised numeric type and constructing numeric literals?
>
> I have to correct my statement:
>
> You cannot compile Common Lisp code to ML code that expresses the same
> idea short of piping it through an intelligent human who is proficient
> with both languages, and can use intelligent judgment to make decisions
> about what "the same idea" is supposed to mean.

That's not surprising, considering that if you substitute SML for Common Lisp
and OCaml for ML, the statement remains true, even though the differences
between languages is far less.

I can't claim to be intelligent, but I'm at least human, and I've done the
experiment.

I translated some small (about 5_000 LOC) programs from SML to OCaml a few
years ago, and while at first I wanted to use CamlP4, in the end I just rolled
up my sleeves and used Andreas Rossberg's side by side table of differences
to refresh my rusty SML. I imagine any nontrivial Lisp code would be harder.
Some Schemes may be easier than Lisp, since there are dialects of Scheme that
are very ML influenced with modules, exceptions, and pattern matching.

-- Brian



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

* Re: [Caml-list] lisp to ocaml
  2005-09-18  1:08 [Caml-list] lisp to ocaml Jonathan Roewen
  2005-09-18  2:56 ` Jon Harrop
  2005-09-18 14:06 ` Thomas Fischbacher
@ 2005-09-19 13:08 ` Christoph Bauer
  2 siblings, 0 replies; 9+ messages in thread
From: Christoph Bauer @ 2005-09-19 13:08 UTC (permalink / raw)
  To: jonathan.roewen; +Cc: caml-list

Hi,

> Hi,
>
> Does anyone know of a tool that can convert lisp to ocaml (or
> something other ML dialect)? I've tried googling and couldn't find
> anythinge Just all the parentheses gets a bit confusing for a first
> look at lisp ;t)

camlp4/scheme could be interesting.

input file `example.ml':
(define f (lambda (n) (+ n 1)))


output of
% camlp4 pa_scheme.cmo pr_o.cmo example.ml
let f n = n + 1

This extension is included in the ocaml distribution under

ocaml-3.08.4/camlp4/unmaintained/scheme

You have to install it by yourself, e.g.

make && cp *.cmo *.cmi `ocamlc -where`/camlp4


regards,
Christoph Bauer


-- 
let () = let rec f a w i j = Printf.printf "%.20f\r" a; let a1 = a *. i /. j in
if w then f a1 false (i +. 2.0) j else f a1 true i (j +. 2.0) in f 2.0 false 2.0 1.0


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

end of thread, other threads:[~2005-09-18 17:10 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-09-18  1:08 [Caml-list] lisp to ocaml Jonathan Roewen
2005-09-18  2:56 ` Jon Harrop
2005-09-18 14:06 ` Thomas Fischbacher
2005-09-18 14:17   ` yoann padioleau
2005-09-18 14:37     ` Thomas Fischbacher
2005-09-18 15:44   ` Jon Harrop
2005-09-18 16:08     ` Thomas Fischbacher
2005-09-18 17:10       ` brogoff
2005-09-19 13:08 ` Christoph Bauer

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 NNTP newsgroup(s).