0

I have a little trouble understanding the evaluation of function bindings and function calls in SML.

A simple example of a function binding and a function call:

val w = 12;

fun pow(x:int,n:int) = 
    if n = 0 
    then 1 
    else x * pow(x,n-1)

pow((1+1),3);


A little theory:

Formal parameter — the identifier used in a method to stand for the value that is passed into the method by a caller. In the above function binding, the formal arguments are x and n.

Actual parameter — the actual value that is passed into the method by a caller. In the function call below, the actual arguments are (1+1) and 3.



My two questions:

1.) When we type-check function bindings, we store, in the static environment, the types of earlier declarations, types of function arguments and the functions type, so it looks something like this:

[w=int, x=int, n=int, pow=int*int->int]

Now to the evaluation. It says here (on slide 6) that the variable "pow" is added to the dynamic environment. I'm a little confused about this. Shouldn't it store the whole function binding, so the dynamic environment would look something like this?

[pow = whole function body]


2.)When we call the function above we pass the actual parameters (1+1) and 3 to the pow function. I've read that after these expressions are evaluated ( 1+1 = 2 and 3 = 3) the values are assigned to the formal parameters. What type of parameters is then actually used for the evaluation of a function call, if the values are assigned to the formal parameters? By definition, the formal parameters shouldn't have much to do with this.

Thank you in advance for your help.

EDIT: found the page, which says, that the evaluated actual parameters are assigned to the formal parameters in the method's definition.

TheAptKid
  • 1,559
  • 3
  • 25
  • 47
  • 2
    'Formal parameters' are a description of the source code, not the product of the compiler. The compiler produces byte/machine code which says something like "Load the first argument into registers A and B, Load the second argument into register C, Jump execution to address 1000," where address 1000 contains some instructions for multiplying registers A and B storing the result in B, decrementing register C, and returning register B then jumping execution to the next code block if register C = 0. After compilation 'x' 'n' and 'pow' are gone (except perhaps when debugging). – ben rudgers Aug 07 '14 at 13:28

1 Answers1

0
  1. Adding "pow" to the environment is the same thing as binding its name to its value, i.e. the function itself.

  2. It doesn't make much sense to reason about SML's evaluation model based on some document about Java, but in this case you might say that during evaluation of a function body, the names of the formal parameters are bound to the values of the actual parameters in the environment.

molbdnilo
  • 64,751
  • 3
  • 43
  • 82
  • Thank you for your answers. I still don't understand: (1) how, specifically, the binding for a function would look like. I understand that a variable binding would look something like [x=3]. What about a function binding [pow=...?] (2) I had a question on my exam, which asked, what kind of parameters (formal or actual) are used at the evaluation of a function call. Since I didn't know what those actually meant, I had trouble answering the question. – TheAptKid Aug 14 '14 at 14:18
  • 1: It would look like [pow = {body of pow}], I suppose. 2. I think that the question is confusing. The *value* used is that of the actual parameter, and it's used by binding the name of the formal parameter to the value of the actual parameter. – molbdnilo Aug 14 '14 at 14:35