473,625 Members | 2,853 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ocaml to python

Is there any way to convert ocaml code to python? but not manually

thx
Feb 19 '07 #1
2 3205
Gigs_ wrote:
Is there any way to convert ocaml code to python? but not manually
Translating between dissimilar high-level languages is very difficult, so
difficult that it is hard to do such a task justice by hand, let alone
automating the procedure.

If you must do it then write a compiler that converts OCaml's intermediate
representation into Python (after pattern match compilation), or write an
OCaml bytecode interpreter in Python.

For example, the following simple OCaml code is difficult to write in
Python:

let rec ( +: ) f g = match f, g with
| `Q n, `Q m -`Q (n +/ m)
| `Q (Int 0), e | e, `Q (Int 0) -e
| f, `Add(g, h) -f +: g +: h
| f, g -`Add(f, g)

let rec ( *: ) f g = match f, g with
| `Q n, `Q m -`Q (n */ m)
| `Q (Int 0), e | e, `Q (Int 0) -`Q (Int 0)
| `Q (Int 1), e | e, `Q (Int 1) -e
| f, `Mul(g, h) -f *: g *: h
| f, g -`Mul(f, g)

let rec simplify = function
| `Q _ | `Var _ as e -e
| `Add(f, g) -simplify f +: simplify g
| `Mul(f, g) -simplify f *: simplify g;;

OCaml compiles the pattern matches first, which gives an intermediate
representation much closer to something Python/Lisp can handle:

(seq
(letrec
(+:/71
(function f/72 g/73
(catch
(catch
(if (isint f/72) (exit 3)
(if (!= (field 0 f/72) 81) (exit 3)
(let (n/74 (field 1 f/72))
(catch
(if (isint g/73) (exit 4)
(if (!= (field 0 g/73) 81) (exit 4)
(makeblock 0 81
(apply (field 0 (global Num!)) n/74
(field 1 g/73)))))
with (4)
(switch n/74
case tag 0: (if (!= (field 0 n/74) 0) (exit 3) g/73)
default: (exit 3))))))
with (3)
(if (isint g/73) (exit 2)
(let (variant/113 (field 0 g/73))
(if (!= variant/113 81)
(if (!= variant/113 3254785) (exit 2)
(let (match/112 (field 1 g/73))
(apply +:/71 (apply +:/71 f/72 (field 0 match/112))
(field 1 match/112))))
(let (match/110 (field 1 g/73))
(switch match/110
case tag 0:
(if (!= (field 0 match/110) 0) (exit 2) f/72)
default: (exit 2)))))))
with (2) (makeblock 0 3254785 (makeblock 0 f/72 g/73)))))
(apply (field 1 (global Toploop!)) "+:" +:/71))
(letrec
(*:/83
(function f/84 g/85
(catch
(catch
(catch
(catch
(catch
(if (isint f/84) (exit 10)
(if (!= (field 0 f/84) 81) (exit 10)
(let (n/86 (field 1 f/84))
(catch
(if (isint g/85) (exit 11)
(if (!= (field 0 g/85) 81) (exit 11)
(makeblock 0 81
(apply (field 5 (global Num!)) n/86
(field 1 g/85)))))
with (11)
(switch n/86
case tag 0:
(if (!= (field 0 n/86) 0) (exit 10) (exit 5))
default: (exit 10))))))
with (10)
(if (isint g/85) (exit 9)
(if (!= (field 0 g/85) 81) (exit 9)
(let (match/121 (field 1 g/85))
(switch match/121
case tag 0:
(if (!= (field 0 match/121) 0) (exit 9) (exit 5))
default: (exit 9))))))
with (9)
(if (isint f/84) (exit 8)
(if (!= (field 0 f/84) 81) (exit 8)
(let (match/124 (field 1 f/84))
(switch match/124
case tag 0:
(if (!= (field 0 match/124) 1) (exit 8) g/85)
default: (exit 8))))))
with (8)
(if (isint g/85) (exit 7)
(let (variant/130 (field 0 g/85))
(if (!= variant/130 81)
(if (!= variant/130 3855332) (exit 7)
(let (match/129 (field 1 g/85))
(apply *:/83 (apply *:/83 f/84 (field 0 match/129))
(field 1 match/129))))
(let (match/127 (field 1 g/85))
(switch match/127
case tag 0:
(if (!= (field 0 match/127) 1) (exit 7) f/84)
default: (exit 7)))))))
with (7) (makeblock 0 3855332 (makeblock 0 f/84 g/85)))
with (5) [0: 81 [0: 0]])))
(apply (field 1 (global Toploop!)) "*:" *:/83))
(let
(*:/83 (apply (field 0 (global Toploop!)) "*:")
+:/71 (apply (field 0 (global Toploop!)) "+:"))
(letrec
(simplify/97
(function e/98
(let (variant/135 (field 0 e/98))
(if (!= variant/135 3254785)
(if (!= variant/135 3855332) e/98
(let (match/132 (field 1 e/98))
(apply *:/83 (apply simplify/97 (field 0 match/132))
(apply simplify/97 (field 1 match/132)))))
(let (match/131 (field 1 e/98))
(apply +:/71 (apply simplify/97 (field 0 match/131))
(apply simplify/97 (field 1 match/131))))))))
(apply (field 1 (global Toploop!)) "simplify" simplify/97))))

I suspect your best bet is to write an interface between the two (e.g.
XMLRPC) and keep the OCaml as OCaml.

--
Dr Jon D Harrop, Flying Frog Consultancy
OCaml for Scientists
http://www.ffconsultancy.com/product...ex.html?usenet
Feb 19 '07 #2
Gigs_ wrote:
Is there any way to convert ocaml code to python? but not manually
For Python and ocaml, my bookmark contains this link:

http://www.programs4all.net/programs...-for-Ocaml.htm

But no ocaml to Python compiler...

Feb 20 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2381
by: Emile van Sebille | last post by:
QOTW: "If we get 2.3.3c1 out in early December, we could release 2.3.3 final before the end of the year, and start 2004 with a 100% bug-free codebase <wink>." -- Tim Peters "cjOr proWe vbCould vbSettle prpFor noEnglish prpIn adjHungarian noNotation :-)" -- noPeter Jack Jensen reveals that python on mac continues as an osx option only starting with python2.4.
5
3512
by: Max Powers | last post by:
I've spent an interesting day searching the web for help with calling OCaml functions from a python program and have come to the newsgroup for some advice (unfortunately I'm learning both languages at the same time, so maybe I've missed something obvious!) The situation is this; I've got a large legacy program that is written in ocaml, within which there's a parser. I'd like to be able to call that parser from my python program (actually...
0
1446
by: Bryan | last post by:
i've read a few posts about using ocaml as an extension language for python together. has anyone done this? can you write the entire extension in ocaml without having to explicitly write an additional c wrapper? i was told that ocaml has exportable functions with c bindings. if someone has successfully done this, would you mind posting/sharing an example hello world extension? thanks, bryan
4
1870
by: Jelle Feringa // EZCT / Paris | last post by:
After reading about extending python with C/Fortran in the excellent Python Scripting for Computational Science book by Hans Langtangen, I'm wondering whether there's not a more pythonic way of extending python. And frankly I think there is: OCAML Fortunately there is already a project up and running allowing you to extend python with OCAMl: http://pycaml.sourceforge.net/ Since I haven't got actual experience programming CAML I'd like...
0
8253
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8692
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8635
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8354
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7182
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4089
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4192
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2621
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.