Lab 4: Need of desperate assistance...

4 views
Skip to first unread message

Alexis Vanacker

unread,
Mar 10, 2010, 8:55:33 AM3/10/10
to proglang-course-2010
Hello,

We are having the hardest time simply understanding how to implement
this final lab in java...

Basically, we thought of doing a first pass in which we would store
the function names and their body (the expression) in a Hashmap
signature. Once that is done we would execute the main function
(provided it has been declared, otherwise there would be an error).

How problem here is, if we were given a simple program such as:

f x y = x + y;

main = f 5 7

Our interpreter will return the value of the expression "f 5 7". It
sees it as a function application (EApp in our grammar). We get the
function's body from the signature Hashmap... but we can't see how to
apply that body on the list of variables/integers in input!

Can anybody hint us on this?

Thanks in advance

Ramona Enache

unread,
Mar 10, 2010, 9:01:57 AM3/10/10
to proglang-c...@googlegroups.com
Hi,

It's important the way you store the function declarations. The idea
would be not to store f as x + y but as something like \x -> \y -> x +
y because the order of the arguments matters.
When you apply f to 4 and 5, first you treat the case of applying f to
4 (since function application is left-associative). You can either
substitute x with 4 in the body of the abstraction (substitution-based
approach) or work with closures, as described in the extra lecture. If
you work with substitutions, you might make sure that you don't get
variable capture.
For example (\x -> \x -> x) 1 2 should yield to 2, if the substitution
is performed correctly.
I hope that helps.

Regards,
Ramona


2010/3/10 Alexis Vanacker <alexva...@gmail.com>:

Alexis Vanacker

unread,
Mar 10, 2010, 9:20:00 AM3/10/10
to proglang-course-2010
Thank you for your quick reply!
To store the functions as Lambda-expressions, is there a way to create
a
new expression Exp inside the interpreter? Since it is a general
class,
we can't simply write something like Exp new_body = new Exp(...); can
we?

(it didn't work anyway when we tried it out).

On 10 mar, 15:01, Ramona Enache <ra.moni...@gmail.com> wrote:
> Hi,
>
> It's important the way you store the function declarations. The idea
> would be not to store f as x + y but as something like \x -> \y -> x +
> y because the order of the arguments matters.
> When you apply f to 4 and 5, first you treat the case of applying f to
> 4 (since function application is left-associative). You can either
> substitute x with 4 in the body of the abstraction (substitution-based
> approach) or work with closures, as described in the extra lecture. If
> you work with substitutions, you might make sure that you don't get
> variable capture.
> For example (\x -> \x -> x) 1 2 should yield to 2, if the substitution
> is performed correctly.
> I hope that helps.
>
> Regards,
> Ramona
>

> 2010/3/10 Alexis Vanacker <alexvanac...@gmail.com>:

Jorge Diz Pico

unread,
Mar 10, 2010, 10:38:51 AM3/10/10
to proglang-c...@googlegroups.com
Yes you can, that's the way I did it, at least.

Your code didn't work because Exp is an abstract class, try with 

ELambda e = new ELambda(...)

assuming Elambda is the name of your rule:

ELambda.   Exp ::= "\\" Ident "->" Exp;

Alex Vanacker

unread,
Mar 10, 2010, 4:06:30 PM3/10/10
to proglang-c...@googlegroups.com
Sorry to answer so late: this works! Still got a lot of work on it, but this makes it easier. Thanks a lot!

2010/3/10 Jorge Diz Pico <corre...@gmail.com>
Reply all
Reply to author
Forward
0 new messages