jl8n
jl8n

Reputation: 53

Why am I getting these errors, and how can I solve them?

I'm writing an interpreter in Standard ML but I'm having trouble with my the syntax in this function, and I cannot figure out what's wrong.

Here's the relevant code:

| eval (rho, SetExp (name, value)) =
    (case rhoContains rho name of
        true    => rhoSet rho name value (rho, value)
    |   false   => globalSet (name, value))


fun rhoSet [] key value = [(key, value)]
  | rhoSet ((elt as (k, v)) :: tail) key value =
        if key = k then (key, value) :: tail else elt :: rhoSet tail key value


fun rhoContains rho name =
    case rhoGet rho name of SOME _ => true | NONE => false

This is where SetExp comes from:

datatype expression =
    SetExp of (string * expression)

Running this gives me a long list of errors but I think this is the relevant section. Line 62 is the line that starts with true in eval:

eval.sml:62: error: Type error in function application.
  Function: rhoSet rho name value : (string * expression) list
  Argument: (rho, value) : (string * expression) list * expression
  Reason: Value being applied does not have a function type

Upvotes: 1

Views: 59

Answers (2)

molbdnilo
molbdnilo

Reputation: 66371

You're passing too many arguments to rhoSet - remove the trailing pair.

| eval (rho, SetExp (name, value)) =
    (case rhoContains rho name of
        true    => rhoSet rho name value
    |   false   => globalSet (name, value))

You can also make this more readable with a conditional:

| eval (rho, SetExp (name, value)) =
    if rhoContains rho name
    then rhoSet rho name value
    else globalSet (name, value)

Upvotes: 2

qouify
qouify

Reputation: 3900

For SML, in the expression rhoSet rho name value (rho, value), rhoSet rho name value is a function and (rho, value) is the argument of this function. However, by the definition of rhoSet, rhoSet rho name value is necessarily a list and not a function. That's what the error message means.

Upvotes: 1

Related Questions