List Info

Thread: "ocaml_beginners"::[] A question about functions




"ocaml_beginners"::[] A question about functions
country flaguser name
United States
2007-05-08 14:17:45

Hi !

Sorry if its a weird question, but I was wondering if there is a way
to create functions dymanically. For example, is it possible to have a
function that given a string "(x + y) * z" will return a function of
type int -> int -> int -> int, whose output is the sum of its first
two inputs multiplied by the third. So, I could do something like:

# let f = strange_function "(x + y) * z";;
val f : int -> int -> int -> int = <fun>;
# f 1 2 3;;
- : int = 9

# let f = strange_function "x +. y./.2.&quot;;;
val f : float -> float -> float = <fun>;
# f 1. 2.;;
- : float = 2.

Etc. So, its as if strange_function kicks off OCaml compiler, and then
imports the byte code into the current "environment&quot; (sorry, I don't
know the terminology for this yet).

Thank you !
Anton

__._,_.___
.

__,_._,___
Re: "ocaml_beginners"::[] A question about functions
country flaguser name
France
2007-05-08 14:34:06

LE MARDI 08 MAI 2007 à 19:17 +0000, ANTON_BELOV A éCRIT :
> SORRY IF ITS A WEIRD QUESTION, BUT I WAS WONDERING IF THERE IS A WAY
> TO CREATE FUNCTIONS DYMANICALLY. FOR EXAMPLE, IS IT POSSIBLE TO HAVE A
> FUNCTION THAT GIVEN A STRING "(X + Y) * Z" WILL RETURN A FUNCTION OF
> TYPE INT -> INT -> INT -> INT, WHOSE OUTPUT IS THE SUM OF ITS FIRST
&GT; TWO INPUTS MULTIPLIED BY THE THIRD. SO, I COULD DO SOMETHING LIKE:

IT'S DEFINITELY POSSIBLE BUT THERE IS (TO MY KNOWLEDGE) NO EASY WAY TO
DO THAT IN THE LANGUAGE.

IF I HAD TO DO THAT, I WOULD USE OCAMLLEX AND OCAMLYACC TO CREATE A
SMALL SCRIPTING LANGUAGE AND THE "STRANGE_FUNCTION" FUNCTION WOULD EMBED
AN AST WITH AN INTERPRETER TO CREATE A NEW FUNCTION.

MY ONLY CONCERN IS ON HOW TO CREATE A FUNCTION WITH THE GOOD SIGNATURE.
SO MAYBE IT WOULD BE NECESSARY TO GIVE A LIST OF VARIANT TO THE RETURNED
FUNCTION (WHICH WOULD BE OF TYPE (VARIABLE LIST -> VARIABLE) OR
SOMETHING LIKE THAT). OR MAYBE USING FORMAT STRING (BUT I DON'T KNOW HOW
TO USE THEM).

BUT I DON'T THINK THAT THERE IS A WAY THAT WOULD GENERATE REAL BYTE CODE
THAT COULD BE LOADED AT RUN TIME (EVEN IF LOADING THE CODE IS POSSIBLE
WITH THE DYN MODULE, I THINK THAT THERE IS A PROBLEM TO TYPE CORRECTLY
THE RETURNED FUNCTION IF IT IS NOT KNOWN AT COMPILE TIME).

MATHIAS

__._,_.___
.

__,_._,___
Re: "ocaml_beginners"::[] A question about functions
country flaguser name
United States
2007-05-08 14:56:34

On Tue, 8 May 2007, anton_belov wrote:

> Hi !
>
> Sorry if its a weird question, but I was wondering if there is a way
> to create functions dymanically. For example, is it possible to have a
> function that given a string "(x + y) * z" will return a function of
> type int -> int -> int -> int, whose output is the sum of its first
&gt; two inputs multiplied by the third. So, I could do something like:
&gt;
> # let f = strange_function "(x + y) * z";;
> val f : int -> int -> int -> int = <fun>;
> # f 1 2 3;;
> - : int = 9
>
> # let f = strange_function "x +. y./.2.&quot;;;
> val f : float -> float -> float = <fun>;
> # f 1. 2.;;
>; - : float = 2.

You want something a bit like Lisp's eval. While this is possible in
OCaml (Turing completeness, yadda yadda), it's not at all easy. You're
probably better off looking for a different solution.

William D. Neumann

---

"There's just so many extra children, we could just feed the
children to these tigers. We don't need them, we're not doing
anything with them.

Tigers are noble and sleek; children are loud and messy.&quot;

-- Neko Case

Life is unfair. Kill yourself or get over it.
-- Black Box Recorder

__._,_.___
.

__,_._,___
Re: "ocaml_beginners"::[] A question about functions
country flaguser name
United Kingdom
2007-05-08 14:54:41

On Tue, May 08, 2007 at 07:17:45PM -0000, anton_belov wrote:
&gt; Sorry if its a weird question, but I was wondering if there is a way
> to create functions dymanically. For example, is it possible to have a
> function that given a string "(x + y) * z" will return a function of
> type int -> int -> int -> int, whose output is the sum of its first
&gt; two inputs multiplied by the third. So, I could do something like:

No, but look at MetaOCaml:

http://www.metaocaml.org/

Rich.

--
Richard Jones
Red Hat

__._,_.___
.

__,_._,___
Re: "ocaml_beginners"::[] A question about functions
country flaguser name
United States
2007-05-08 15:00:23


If it's just arithmetic, writing a simple recursive descent parser is
not too hard, and it could return closures instead of an AST.

--Jonathan

On May 8, 2007, at 3:34 PM, Mathias Kende wrote:

> Le mardi 08 mai 2007 à 19:17 +0000, anton_belov a écrit :
> > Sorry if its a weird question, but I was wondering if there is a way
> > to create functions dymanically. For example, is it possible to
> have a
> > function that given a string "(x + y) * z" will return a function of
> > type int -> int -> int -> int, whose output is the sum of its first
&gt; > two inputs multiplied by the third. So, I could do something like:
&gt;
> It's definitely possible but there is (to my knowledge) no easy way to
> do that in the language.
>
&gt; If I had to do that, I would use ocamllex and ocamlyacc to create a
> small scripting language and the "strange_function" function would
> embed
&gt; an ast with an interpreter to create a new function.
> My only concern is on how to create a function with the good
> signature.
> So maybe it would be necessary to give a list of variant to the
> returned
> function (which would be of type (variable list -> variable) or
> something like that). Or maybe using format string (but I don't
> know how
> to use them).
&gt;
> But I don't think that there is a way that would generate real byte
> code
>; that could be loaded at run time (even if loading the code is possible
> with the Dyn module, I think that there is a problem to type correctly
> the returned function if it is not known at compile time).
&gt;
> Mathias
>
>
>

__._,_.___
.

__,_._,___
Re: "ocaml_beginners"::[] A question about functions
country flaguser name
France
2007-05-08 15:09:08

LE MARDI 08 MAI 2007 à 16:00 -0400, JONATHAN BRYANT A éCRIT :
>
> IF IT'S JUST ARITHMETIC, WRITING A SIMPLE RECURSIVE DESCENT PARSER IS
> NOT TOO HARD, AND IT COULD RETURN CLOSURES INSTEAD OF AN AST.

YES, BUT HOW DO YOU GIVE THE PROPER TYPE TO THE RETURNED FUNCTION ?

__._,_.___
.

__,_._,___
Re: "ocaml_beginners"::[] A question about functions
country flaguser name
United States
2007-05-08 15:16:57

On Tue, 8 May 2007, Richard Jones wrote:

> On Tue, May 08, 2007 at 07:17:45PM -0000, anton_belov wrote:
&gt;> Sorry if its a weird question, but I was wondering if there is a way
>> to create functions dymanically. For example, is it possible to have a
>&gt; function that given a string "(x + y) * z" will return a function of
>&gt; type int -> int -> int -> int, whose output is the sum of its first
&gt;> two inputs multiplied by the third. So, I could do something like:
&gt;
> No, but look at MetaOCaml:
>
&gt; http://www.metaocaml.org/

Well, metaocaml doesn't do this for you either. Though looking at
metaocaml is a good thing to do anyway.

William D. Neumann

---

"There's just so many extra children, we could just feed the
children to these tigers. We don't need them, we're not doing
anything with them.

Tigers are noble and sleek; children are loud and messy.&quot;

-- Neko Case

Life is unfair. Kill yourself or get over it.
-- Black Box Recorder

__._,_.___
.

__,_._,___
"ocaml_beginners"::[] Re: A question about functions
country flaguser name
United States
2007-05-08 15:34:22

Thank you all for such a quick and informative reply !!!

__._,_.___
.

__,_._,___
Re: "ocaml_beginners"::[] A question about functions
country flaguser name
United Kingdom
2007-05-08 23:29:27

On Tuesday 08 May 2007 20:17, anton_belov wrote:
&gt; Hi !
>
> Sorry if its a weird question, but I was wondering if there is a way
> to create functions dymanically. For example, is it possible to have a
> function that given a string "(x + y) * z" will return a function of
> type int -> int -> int -> int, whose output is the sum of its first
&gt; two inputs multiplied by the third. So, I could do something like:
&gt;
> # let f = strange_function "(x + y) * z";;
> val f : int -> int -> int -> int = <fun>;
> # f 1 2 3;;
> - : int = 9

Have a look at our parser and interpreter examples:

http://www.ffconsultancy.com/ocaml/benefits/parsing.html
http://www.ffconsultancy.com/ocaml/benefits/interpreter.html

You can return boxed values that must be unboxed (essentially dynamic typing).

--
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
The F#.NET Journal
http://www.ffconsultancy.com/products/fsharp_journal/?e

__._,_.___
.

__,_._,___
[1-9]

about | contact  Other archives ( Real Estate discussion Medical topics )