Perché ricevo questi errori e come posso risolverli?

Oct 20 2020

Sto scrivendo un interprete in Standard ML ma ho problemi con la mia sintassi in questa funzione e non riesco a capire cosa c'è che non va.

Ecco il codice pertinente:

| 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

Ecco da dove proviene SetExp:

datatype expression =
    SetExp of (string * expression)

L'esecuzione di questo mi dà un lungo elenco di errori, ma penso che questa sia la sezione pertinente. La riga 62 è la riga che inizia con truein 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

Risposte

2 molbdnilo Oct 21 2020 at 08:14

Stai passando troppi argomenti per rhoSet- rimuovere la coppia finale.

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

Puoi anche renderlo più leggibile con un condizionale:

| eval (rho, SetExp (name, value)) =
    if rhoContains rho name
    then rhoSet rho name value
    else globalSet (name, value)
1 qouify Oct 21 2020 at 08:13

Per SML, nell'espressione rhoSet rho name value (rho, value), rhoSet rho name valueè una funzione ed (rho, value)è l'argomento di questa funzione. Tuttavia, per definizione di rhoSet, rhoSet rho name valueè necessariamente un elenco e non una funzione. Questo è il significato del messaggio di errore.