[comp.compilers] Help needed on SML compiler

surendar@enuxha.eas.asu.edu (Surendar Chandra ) (03/26/91)

Hi,
	I am new to this group and am not sure if this query is appropriate
in this group.
	I am writing a subset of the Standard ML functional language as
part of my course project and am facing a few problems with functional
langauge implementation. I have used a SML intepreter which seems to work
okay in this case.  . However when I try to think of compiling it, I have
some problems. Specifically, I'll take a small code section.

> val a = 1 ; /* Equate the variable a to 1 */
> val b = a ; 
> fun a b = b * 2 ; /* Define a function a that takes one integer argument and
			doubles it */
> val c = a 1 ;
> fun a ( b , c ) = b * c * 2  ; 
> val d = a ( 1 , 2 ) ;

	Now, I am very familiar with UNIX and 'C' and I couldn't think of
any way to generate code for this SML construct . If I were to create a
global variable 'a' and assign it to 'b' and when I try to redefine 'a' to
a function, how do I do that? How can I generate code for langauges were
both function definition and usage is mixed up?

I was thinking of two solutions
* Generate a shadow function file which will have code for the functions and
  generate code for assignments in one file and let the link editor resolve
  references.
  Problem: It still will give a Multiply defined symbol.. error. 
* Internally generate names something like <User_defined_name>_<random_number>
  so that the 'a' which you access changes dynamically with redefinition.
  Problem: The problem is the function compiled in one file will never 
  be accessible in another file as there is no relation between what the 
  user gives and system generates.

  Now, I tried to see if there is any SML compilers around and I couldn't
find any. I would really appreciate any help..

Thanks in advance,
surendar
[There are some ML compilers around, you can ftp one from princeton.edu.  As
far as this problem goes, I'd suggest that the variables really be structures
with type tags and pointers that are dereferenced at run time.  There is no
way in C to directly handle things whose type isn't known at compile time,
you have to handle it yourself. -John]
-- 
Send compilers articles to compilers@iecc.cambridge.ma.us or
{ima | spdcc | world}!iecc!compilers.  Meta-mail to compilers-request.

cs450a03@uc780.umd.edu (Raul Rockwell) (03/27/91)

Surendar Chandra, working on a compiler for a functional language,
gives an example that basically runs:
  define function A
  use function A
  redefine function A
  use function A

Which, I think, points out the reason functional language advocates
tend to look at reassignment as a totally different operation than
assignment.  Many would say that reassignment should be unimplemented
as much as possible.

However, the best way I can see to implement something like the above
is to simply say that the last definition of a name is the one which
is advertised outside the module.  (e.g. given A@1, A@2 and A@3, where
@n indicates serial number, use A@3).

If the function associated with a name can change on-the-fly, for the
same body of code, you'll have to provide an interface for those
functions.  This might be as simple as a "case statement" or, for ill
conditioned code, might be as complex as a call to some generalized
function resolver.

The case of "redefined on the fly" occurs with the use of global
references or where function definition occurs inside a loop (on
non-constant data) or where a function's definition depends on I/O
(e.g. construct fndef from file).

Raul Rockwell
-- 
Send compilers articles to compilers@iecc.cambridge.ma.us or
{ima | spdcc | world}!iecc!compilers.  Meta-mail to compilers-request.

nick@lfcs.edinburgh.ac.uk (Nick Rothwell) (03/28/91)

>	Now, I am very familiar with UNIX and 'C' and I couldn't think of
>any way to generate code for this SML construct .

Familiarity with UNIX and C is probably a drawback in this regard. You're
thinking of how C compilers work, which doesn't help.

>If I were to create a
>global variable 'a' and assign it to 'b' and when I try to redefine 'a' to
>a function, how do I do that? How can I generate code for langauges were
>both function definition and usage is mixed up?

Sorry, I don't understand this paragraph at all.

SML isn't difficult to compile. It's statically scoped, which means you
have to create static function closures at runtime (which avoids your
problems about "redefining" things). SML's formal semantic definition
document (published by MIT press) describes how the environments are
handled (including redefinition of identifiers).

>* Generate a shadow function file which will have code for the functions
and
>  generate code for assignments in one file and let the link editor
resolve
>  references.

Er, ah, um, you mean you want to compile ML functions into C object files
with the entry point names the same as the ML functions? No problem with
the environment management as far as I can see - you just export the
environment of functions which results from elaborating (typechecking, in
effect) the declarations. But, since ML's data objects are totally
different from C's (it has first class functions, assumes garbage
collection, and so on), there's no trivial way of doing this. Equating the
ML and C name spaces is likely to lead to confusion. 

>I'd suggest that the variables really be structures
>with type tags and pointers that are dereferenced at run time.

Presumably you're thinking of accessing the ML data objects from C here. In
which case, yes, this is the way to do it (and in SML/NJ, the runtime
system and garbage collector is written in C anyway...).

        Nick.
--
-- 
Send compilers articles to compilers@iecc.cambridge.ma.us or
{ima | spdcc | world}!iecc!compilers.  Meta-mail to compilers-request.