Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at ocaml.org.

Browse thread
Meta-programming?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2002-05-15 (03:49)
From: felixgrupp <felixprog@h...>
Subject: Re: Meta-programming?
This is the problem with static languages. In Lisp, for example, it 
is easy to take a string and execute it as code, but in OCaml, the 
compiler won´t compile your code unless the type of every identifier 
is known. This way, it is guaranteed that the program will never try 
to do anything illegal, such as trying to multiply two strings.

If it were possible to read code from a file and execute it, there 
would be no way for the compiler to know what kind function calls the 
program would try to do, and so could not guarantee the safety of the 
program. A lot of other problems would appear too. For example, the 
code read from the file might contain declarations or function calls 
that influences the rest of the program (after the contents of the 
file had been executed), making it impossible for the compiler to 
know very much at all about how to compile the program. It would have 
to include a version of it self and parser et cetera, and also all 
the library files... On top of witch, the program would also run very 
much slower because it would have to do a lot of type checking and 
other stuff at run time, wich otherwise the compiler takes care of. 

All of this explains why one would like to use a static programming 
language: safety, speed and (to a lesser degree) size of the compiled 
code.

So, what you have to do is write a small parser yourself (preferably 
using the OCaml library) and an interpreter implementing what ever 
functionality you need. This is a typical example of what advocates 
of dynamic languages are always claiming: any interesting program 
will end up implementing a new (small or large) language.


You might want to check out MetaOCaml at

http://cs-www.cs.yale.edu/homes/taha/MetaOCaml

They use a model called staged computation, which seems to be a lot 
more disciplined than eg Lisp. I am rather curious myself about what 
can be done in it.

/Felix

--- In ocaml_beginners@y..., stalkern2 <stalkern2@t...> wrote:
> Hello to everybody I'm wondering whether in Ocaml one can roll up a 
> string:string and then use a command that takes that string as a 
command, 
> something like: interpret_this = fun s:string -> commands.
> Actually, I'm using configuration files where everything is a 
string, and I'd 
> like to turn them into initializations for a program. If I had only 
a few  
> initializations, I could manage them one by one, but when they are 
many, I'd 
> rather have a program reading them. The point is, in a 
configuration file the 
> name of a variable is a "name", i.e. a string. Well, I can't say in 
my 
> program 
> let "varName" = value,
> I have to say 
> let varName = value.
> 
> So I thought that maybe it is possible to say 
> some_meta-command "let varName = value".
> 
> Any idea?
> 
> Thanks 
> Ernesto